Skip to content

Commit

Permalink
Merge pull request #79 from tkoolen/tk/add-cone-primitive
Browse files Browse the repository at this point in the history
Add Cone primitive.
  • Loading branch information
rdeits authored Dec 11, 2018
2 parents 5490ddb + e2f044e commit d267509
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/MeshCat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export Object,
HyperEllipsoid,
HyperCylinder,
PointCloud,
Cone,
Triad,
Mesh,
Points,
Expand Down
17 changes: 16 additions & 1 deletion src/geometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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
12 changes: 12 additions & 0 deletions src/lowering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
7 changes: 7 additions & 0 deletions test/visualizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d267509

Please sign in to comment.