From b8dacc731616bc719e65d83e4397058ff9bb645d Mon Sep 17 00:00:00 2001 From: Steve Kelly Date: Tue, 2 Jul 2019 20:00:29 -0400 Subject: [PATCH] use base setop names, closes #39 --- examples/csg.jl | 18 +++++++++--------- examples/example001.jl | 2 +- examples/fea.jl | 2 +- examples/radiused_union.jl | 2 +- src/Descartes.jl | 3 +-- src/constructors.jl | 29 ++++++++++++++++++++++++++++- 6 files changed, 41 insertions(+), 15 deletions(-) diff --git a/examples/csg.jl b/examples/csg.jl index 8c86070..ed02884 100644 --- a/examples/csg.jl +++ b/examples/csg.jl @@ -22,17 +22,17 @@ translate([24,0,0]) { using Descartes -a = translate([-24,0,0])CSGUnion( - Cuboid([15,15,15], center=true), - Sphere(10)) +a = translate([-24,0,0])union( + Cuboid([15,15,15], center=true), + Sphere(10)) -b = CSGIntersect( - Cuboid([15,15,15], center=true), - Sphere(10)) +b = intersect( + Cuboid([15,15,15], center=true), + Sphere(10)) -c = translate([24,0,0])CSGDiff( - Cuboid([15,15,15], center=true), - Sphere(10)) +c = translate([24,0,0])diff( + Cuboid([15,15,15], center=true), + Sphere(10)) m = HomogenousMesh(a,b,c) diff --git a/examples/example001.jl b/examples/example001.jl index 233a440..e2fdc1b 100644 --- a/examples/example001.jl +++ b/examples/example001.jl @@ -45,7 +45,7 @@ function example001() rotate(pi/2, rot)Cylinder(r, h, center=true) end - CSGDiff( + diff( Sphere(r_from_dia(size)), rotcy([0, 0, 0], cy_r, cy_h), rotcy([1, 0, 0], cy_r, cy_h), diff --git a/examples/fea.jl b/examples/fea.jl index c5556e8..f467597 100644 --- a/examples/fea.jl +++ b/examples/fea.jl @@ -14,7 +14,7 @@ for i = 1:hole_ct h = translate([hole_interval*i, -1, beam_size[3]/2])* rotate(-pi/2, [1,0,0])* Cylinder(hole_d/2, beam_size[2]+2, center=false) - global c = CSGDiff(c, h) + global c = diff(c, h) end save("fea.ply",HomogenousMesh(c)) diff --git a/examples/radiused_union.jl b/examples/radiused_union.jl index 31385d0..525b13e 100644 --- a/examples/radiused_union.jl +++ b/examples/radiused_union.jl @@ -4,7 +4,7 @@ c2 = Cuboid([4,4.0,4]) cyl1 = rotate(pi/6, [1,0,0])translate([2,2,0])Cylinder(1.0,10.0) u = Shell(0.5)RadiusedCSGUnion(1,c2, cyl1) -u2 = CSGDiff(u, Cuboid([2,2,2])) +u2 = diff(u, Cuboid([2,2,2])) m = HomogenousMesh(u2) diff --git a/src/Descartes.jl b/src/Descartes.jl index 7ffc3b4..1f4e43e 100644 --- a/src/Descartes.jl +++ b/src/Descartes.jl @@ -1,6 +1,6 @@ module Descartes -import Base: * +import Base: *, union, diff, intersect import GeometryTypes: HyperRectangle, HomogenousMesh, SignedDistanceField @@ -8,7 +8,6 @@ import GeometryTypes: HyperRectangle, using GeometryTypes, FileIO, StaticArrays, - # JLD, Meshing, MeshIO, LinearAlgebra diff --git a/src/constructors.jl b/src/constructors.jl index f9c54b2..8619913 100644 --- a/src/constructors.jl +++ b/src/constructors.jl @@ -40,7 +40,11 @@ function Piping(r, pts) Piping(Float64(r), ptsc, SMatrix{4,4}(1.0*I), SMatrix{4,4}(1.0*I)) end -# CSG +# +# +# CSG Operations +# +# # Union function CSGUnion(l::AbstractPrimitive{N1,T1}, r::AbstractPrimitive{N2,T2}) where {N1, N2, T1, T2} @@ -57,6 +61,11 @@ CSGUnion(x::AbstractPrimitive) = x CSGUnion(::Nothing, x::AbstractPrimitive) = x CSGUnion(x::AbstractPrimitive, ::Nothing) = x +union(l::AbstractPrimitive, r::AbstractPrimitive...) = CSGUnion(l,r...) + +union(x::AbstractPrimitive) = x +union(::Nothing, x::AbstractPrimitive) = x +union(x::AbstractPrimitive, ::Nothing) = x # Radiused Union function RadiusedCSGUnion(radius::Real, l::AbstractPrimitive{N1,T1}, r::AbstractPrimitive{N2,T2}) where {N1, N2, T1, T2} @@ -79,16 +88,34 @@ CSGDiff(x::AbstractPrimitive) = x CSGDiff(::Nothing, x::AbstractPrimitive) = x CSGDiff(x::AbstractPrimitive, ::Nothing) = x +diff(l::AbstractPrimitive, r::AbstractPrimitive...) = CSGDiff(l,r...) + +diff(x::AbstractPrimitive) = x +diff(::Nothing, x::AbstractPrimitive) = x +diff(x::AbstractPrimitive, ::Nothing) = x + +# Intersect function CSGIntersect(l::AbstractPrimitive{N1,T1}, r::AbstractPrimitive{N2,T2}) where {N1, N2, T1, T2} N1 == N2 || error("cannot create CSG between objects in R$N1 and R$N2") return CSGIntersect{N1,T1, typeof(l), typeof(r)}(l,r) end +function CSGIntersect(l::AbstractPrimitive{N1,T1}, r::AbstractPrimitive{N2,T2}...) where {N1, N2, T1, T2} + N1 == N2 || error("cannot create CSG between objects in R$N1 and R$N2") + return CSGIntersect(l,CSGIntersect(r[1], r[2:end]...)) +end + CSGIntersect(x::AbstractPrimitive) = x CSGIntersect(::Nothing, x::AbstractPrimitive) = x CSGIntersect(x::AbstractPrimitive, ::Nothing) = x +intersect(l::AbstractPrimitive, r::AbstractPrimitive...) = CSGIntersect(l,r...) + +intersect(x::AbstractPrimitive) = x +intersect(::Nothing, x::AbstractPrimitive) = x +intersect(x::AbstractPrimitive, ::Nothing) = x + # Shell function Shell(r)