From e2f044ee8e09f162ac19158db0843f8be307c8bc Mon Sep 17 00:00:00 2001 From: Twan Koolen Date: Mon, 10 Dec 2018 13:04:10 -0500 Subject: [PATCH] Add Cone primitive. --- src/MeshCat.jl | 1 + src/geometry.jl | 17 ++++++++++++++++- src/lowering.jl | 12 ++++++++++++ test/visualizer.jl | 7 +++++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/MeshCat.jl b/src/MeshCat.jl index 98b4270..03affc6 100644 --- a/src/MeshCat.jl +++ b/src/MeshCat.jl @@ -43,6 +43,7 @@ export Object, HyperEllipsoid, HyperCylinder, PointCloud, + Cone, Triad, Mesh, Points, diff --git a/src/geometry.jl b/src/geometry.jl index e142073..11cf37a 100644 --- a/src/geometry.jl +++ b/src/geometry.jl @@ -6,7 +6,6 @@ end GeometryTypes.origin(geometry::HyperEllipsoid{N, T}) where {N, T} = geometry.center radii(geometry::HyperEllipsoid{N, T}) where {N, T} = geometry.radii -center(geometry::HyperEllipsoid) = origin(geometry) @deprecate HyperCylinder(length::T, radius) where {T} Cylinder{3, T}(Point(0., 0., 0.), Point(0, 0, length), radius) @@ -26,10 +25,20 @@ struct Triad <: AbstractGeometry{3, Float64} Triad(scale=20.0) = new(scale) end +struct Cone{N, T} <: AbstractGeometry{N, T} + origin::Point{N, T} + apex::Point{N, T} + r::T +end + +GeometryTypes.origin(geometry::Cone) = geometry.origin + +center(geometry::HyperEllipsoid) = origin(geometry) center(geometry::HyperRectangle) = minimum(geometry) + 0.5 * widths(geometry) center(geometry::HyperCube) = minimum(geometry) + 0.5 * widths(geometry) center(geometry::HyperSphere) = origin(geometry) center(geometry::Cylinder) = origin(geometry) + geometry.extremity / 2 +center(geometry::Cone) = (origin(geometry) + geometry.apex) / 2 """ $(SIGNATURES) @@ -51,3 +60,9 @@ function intrinsic_transform(g::Cylinder{3}) R = rotation_between(SVector(0, 1, 0), g.extremity) Translation(center(g)) ∘ LinearMap(R) end + +function intrinsic_transform(g::Cone{3}) + # Three.js wants a cone to lie along the y axis + R = rotation_between(SVector(0, 1, 0), g.apex - g.origin) + Translation(center(g)) ∘ LinearMap(R) +end diff --git a/src/lowering.jl b/src/lowering.jl index c4cedfc..18dfb50 100644 --- a/src/lowering.jl +++ b/src/lowering.jl @@ -96,6 +96,18 @@ function lower(g::HyperEllipsoid{3}) ) end +function lower(g::Cone{3}) + # Radius and height are always 1 because we handle these + # in intrinsic_transform + Dict{String, Any}( + "uuid" => string(uuid1()), + "type" => "ConeGeometry", + "radius" => g.r, + "height" => norm(g.apex - g.origin), + "radialSegments" => 100, + ) +end + function lower(t::Triad) attributes = Dict{String, Any}( "position" => lower([Point3f0(0, 0, 0), Point3f0(t.scale, 0, 0), diff --git a/test/visualizer.jl b/test/visualizer.jl index 947696f..5a651f7 100644 --- a/test/visualizer.jl +++ b/test/visualizer.jl @@ -82,6 +82,13 @@ end setobject!(v[:triad], Triad(0.2)) settransform!(v[:triad], Translation(0, 3, 0.2)) end + + @testset "cone" begin + setobject!(v[:cone], + Cone(Point(1., 1., 1.), Point(1., 1., 1.2), 0.1), + MeshLambertMaterial(color=colorant"indianred")) + settransform!(v[:cone], Translation(-1, 2.5, -1)) + end end @testset "meshes" begin