Skip to content

Commit

Permalink
Merge pull request #49 from sjkelly/sjk/coordinate_trans
Browse files Browse the repository at this point in the history
initial pass at supporting CoordinateTransformations
  • Loading branch information
sjkelly authored May 16, 2020
2 parents 018f9ab + c9313c8 commit 1f41559
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 182 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ authors = ["Steve Kelly <kd2cca@gmail.com>"]

[deps]
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
CoordinateTransformations = "150eb455-5306-5404-9cee-2592286d6298"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MeshIO = "7269a6da-0436-5bbc-96c2-40638cbb6118"
Meshing = "e6723b4c-ebff-59f1-b4b7-d97aa5274f73"
Rotations = "6038ab10-8711-5258-84ad-4b1120ba62dc"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
Meshing = "0.5"
GeometryBasics = "0.2"
Meshing = "0.5"
20 changes: 8 additions & 12 deletions src/Descartes.jl
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
module Descartes

import Base: *, union, diff, intersect
import GeometryBasics: HyperRectangle,
import GeometryBasics: AbstractMesh,
HyperRectangle,
Vec,
Mesh

using GeometryBasics,
FileIO,
using FileIO,
StaticArrays,
Meshing,
MeshIO,
LinearAlgebra
LinearAlgebra,
CoordinateTransformations,
Rotations

include("types.jl")
include("constructors.jl")
include("operations.jl")
include("frep.jl")
include("hyperrectangles.jl")
include("meshes.jl")
#include("hashing.jl")
include("cache.jl")
include("visualize.jl")

# GeometryTypes
export Point

# 3d primitives
export Cuboid, Cylinder, Sphere, Piping

# 2d primitives

export Square, Circle

# transforms
export Transform, Translation, translate, rotate
export translate, rotate

# operations
export Shell, LinearExtrude
Expand Down
22 changes: 7 additions & 15 deletions src/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ function Square(dims;center=false)
y = dims[2]
lb = SVector(0.,0.)
center && (lb = -SVector{2,Float64}(x,y)/2)
Square(SVector{2,Float64}(x,y), lb, SMatrix{3,3}(1.0*I), SMatrix{3,3}(1.0*I))
Square(SVector{2,Float64}(x,y), lb)
end

Square(x,y,z;center=false) = Square([x,y],center=center)

function Cuboid(dims;center=false)
@assert length(dims) == 3
lb = SVector(0.,0.,0.)
center && (lb = -SVector{3,Float64}(dims)/2)
Cuboid(SVector{3,Float64}(dims), lb, SMatrix{4,4}(1.0*I), SMatrix{4,4}(1.0*I))
center && (lb = -SVector{3,Float64}(dims...)/2)
Cuboid(SVector{3,Float64}(dims...), lb)
end

Cuboid(x,y,z;center=false) = Cuboid([x,y,z],center=center)
Expand All @@ -26,16 +26,7 @@ function Cylinder(r, h; center=false)
rn, hn = Float64(r), Float64(h)
b = 0.0
center && (b = -h/2)
Cylinder(rn, hn, b, SMatrix{4,4}(one(rn)*I), SMatrix{4,4}(one(rn)*I))
end

function Circle(r)
rn = Float64(r)
Circle(rn, SMatrix{3,3}(one(rn)*I), SMatrix{3,3}(one(rn)*I))
end

function Sphere(r)
Sphere(Float64(r), SMatrix{4,4}(1.0*I), SMatrix{4,4}(1.0*I))
Cylinder(rn, hn, b)
end

#function PrismaticCylinder(sides, height::T, radius::T) where {T}
Expand All @@ -44,7 +35,7 @@ end

function Piping(r, pts)
ptsc = [SVector{3,Float64}(pt...) for pt in pts]
Piping(Float64(r), ptsc, SMatrix{4,4}(1.0*I), SMatrix{4,4}(1.0*I))
Piping(Float64(r), ptsc)
end

#
Expand Down Expand Up @@ -130,5 +121,6 @@ function Shell(r)
end

function LinearExtrude(p::AbstractPrimitive{2,T}, d) where {T}
LinearExtrude{3, T, typeof(p)}(p, convert(T, d), SMatrix{4,4}(1.0*I), SMatrix{4,4}(1.0*I))
#TODO promote type
LinearExtrude{3, T, typeof(p)}(p, convert(T, d))
end
57 changes: 28 additions & 29 deletions src/frep.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,37 @@ function _radius(a,b,r)
end
end

function FRep(p::MapContainer{3,T,P}, v) where {T,P}
FRep(p.primitive, p.inv(v))
end


function FRep(p::MapContainer{2,T,P}, v) where {T,P}
FRep(p.primitive, p.inv(SVector(v[1],v[2])))
end

function FRep(p::Sphere, v)
it = p.inv_transform
x = v[1]*it[1,1]+v[2]*it[1,2]+v[3]*it[1,3]+it[1,4]
y = v[1]*it[2,1]+v[2]*it[2,2]+v[3]*it[2,3]+it[2,4]
z = v[1]*it[3,1]+v[2]*it[3,2]+v[3]*it[3,3]+it[3,4]
x = v[1]
y = v[2]
z = v[3]
sqrt(x*x + y*y + z*z) - p.radius
end

function FRep(p::Cylinder, v)
it = p.inv_transform
x = v[1]*it[1,1]+v[2]*it[1,2]+v[3]*it[1,3]+it[1,4]
y = v[1]*it[2,1]+v[2]*it[2,2]+v[3]*it[2,3]+it[2,4]
z = v[1]*it[3,1]+v[2]*it[3,2]+v[3]*it[3,3]+it[3,4]
x = v[1]
y = v[2]
z = v[3]
max(-z+p.bottom, z-p.height-p.bottom, sqrt(x*x + y*y) - p.radius)
end

function FRep(p::Circle, v)
it = p.inv_transform
x = v[1]*it[1,1]+v[2]*it[1,2]+it[1,3]
y = v[1]*it[2,1]+v[2]*it[2,2]+it[2,3]
sqrt(x*x + y*y) - p.radius
norm(v) - p.radius
end


function FRep(p::Cuboid, v)
it = p.inv_transform
x = v[1]*it[1,1]+v[2]*it[1,2]+v[3]*it[1,3]+it[1,4]
y = v[1]*it[2,1]+v[2]*it[2,2]+v[3]*it[2,3]+it[2,4]
z = v[1]*it[3,1]+v[2]*it[3,2]+v[3]*it[3,3]+it[3,4]
x = v[1]
y = v[2]
z = v[3]
dx, dy, dz = p.dimensions
lbx, lby,lbz = p.lowercorner
max(-x+lbx, x-dx-lbx,
Expand All @@ -45,9 +47,8 @@ function FRep(p::Cuboid, v)
end

function FRep(p::Square, v)
it = p.inv_transform
x = v[1]*it[1,1]+v[2]*it[1,2]+it[1,3]
y = v[1]*it[2,1]+v[2]*it[2,2]+it[2,3]
x = v[1]
y = v[2]
dx, dy = p.dimensions
lbx, lby = p.lowercorner
max(-x+lbx, x-dx-lbx,
Expand Down Expand Up @@ -80,32 +81,30 @@ end

function FRep(p::Piping{T}, v) where {T}
num_pts = length(p.points)
pt = Point(v[1],v[2],v[3])

val = typemax(T)

for i = 1:num_pts-1
e1 = p.points[i]
e2 = p.points[i+1]
v = e2 - e1
w = pt - e1
w = v - e1
if dot(w,v) <= 0
nv = norm(pt - e1)
nv = norm(v - e1)
elseif dot(v,v) <= dot(w,v)
nv = norm(pt - e2)
nv = norm(v - e2)
else
nv = norm(cross(pt-e1,pt-e2))/norm(e2-e1)
nv = norm(cross(v-e1,v-e2))/norm(e2-e1)
end
val = min(nv, val)
end
val - p.radius
end

function FRep(p::LinearExtrude, v)
it = p.inv_transform
x = v[1]*it[1,1]+v[2]*it[1,2]+v[3]*it[1,3]+it[1,4]
y = v[1]*it[2,1]+v[2]*it[2,2]+v[3]*it[2,3]+it[2,4]
z = v[1]*it[3,1]+v[2]*it[3,2]+v[3]*it[3,3]+it[3,4]
x = v[1]
y = v[2]
z = v[3]
r = FRep(p.primitive, v)
max(max(-z,z-p.distance), r)
end
24 changes: 11 additions & 13 deletions src/hyperrectangles.jl
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
function HyperRectangle(square::Square{T}) where {T}
orig = HyperRectangle{2,T}(square.lowercorner, square.dimensions)
transform(square.transform, orig)
HyperRectangle{2,T}(square.lowercorner, square.dimensions)
end

function HyperRectangle(cube::Cuboid{T}) where {T}
orig = HyperRectangle{3,T}(cube.lowercorner, cube.dimensions)
transform(cube.transform, orig)
HyperRectangle{3,T}(cube.lowercorner, cube.dimensions)
end

function HyperRectangle(sphere::Sphere{T}) where {T}
#TODO?
orig = HyperRectangle{3,T}(fill(-sphere.radius,3), fill(sphere.radius*2,3))
transform(sphere.transform, orig)
HyperRectangle{3,T}(fill(-sphere.radius,3), fill(sphere.radius*2,3))
end

function HyperRectangle(p::Cylinder{T}) where {T}
orig = HyperRectangle{3,T}(Vec(-p.radius,-p.radius,0), Vec(p.radius*2,p.radius*2,p.height))
transform(p.transform, orig)
HyperRectangle{3,T}(Vec(-p.radius,-p.radius,0), Vec(p.radius*2,p.radius*2,p.height))
end

function HyperRectangle(p::Circle{T}) where {T}
orig = HyperRectangle{2,T}(Vec(-p.radius,-p.radius), Vec(p.radius*2,p.radius*2))
transform(p.transform, orig)
HyperRectangle{2,T}(Vec(-p.radius,-p.radius), Vec(p.radius*2,p.radius*2))
end

function HyperRectangle(p::Piping{T}) where {T}
maxx, maxy, maxz = typemin(Float64), typemin(Float64), typemin(Float64)
minx, miny, minz = typemax(Float64), typemax(Float64), typemax(Float64)
for pt in p.points
newx, newy, newz = transform(p.transform, pt)
newx, newy, newz = pt
maxx = max(newx,maxx)
maxy = max(newy,maxy)
maxz = max(newz,maxz)
Expand All @@ -41,6 +35,10 @@ function HyperRectangle(p::Piping{T}) where {T}
HyperRectangle(minv, maxv-minv)
end

function HyperRectangle(m::MapContainer)
transform(m.map, HyperRectangle(m.primitive))
end

function HyperRectangle(csg::CSGUnion)
union(HyperRectangle(csg.left), HyperRectangle(csg.right))
end
Expand All @@ -53,7 +51,7 @@ function HyperRectangle(csg::CSGIntersect)
intersect(HyperRectangle(csg.left), HyperRectangle(csg.right))
end

function HyperRectangle(csg::CSGDiff{N, T, L, R}) where {N, T, L, R}
function HyperRectangle(csg::CSGDiff)
diff(HyperRectangle(csg.left), HyperRectangle(csg.right))
end

Expand Down
Loading

0 comments on commit 1f41559

Please sign in to comment.