diff --git a/src/Meshes.jl b/src/Meshes.jl index 5ce0a66e0..9720c37e7 100644 --- a/src/Meshes.jl +++ b/src/Meshes.jl @@ -226,6 +226,8 @@ export angles, innerangles, normal, + base, + apex, ≗, # multi-geometries diff --git a/src/geometries/polytopes/pyramid.jl b/src/geometries/polytopes/pyramid.jl index d478658f9..c71e16d0c 100644 --- a/src/geometries/polytopes/pyramid.jl +++ b/src/geometries/polytopes/pyramid.jl @@ -6,22 +6,31 @@ Pyramid(p1, p2, p3, p4, p5) A pyramid with points `p1`, `p2`, `p3`, `p4`, `p5`. + +The first four points form the base and the last is the apex. """ @polytope Pyramid 3 5 nvertices(::Type{<:Pyramid}) = 5 +function base(p::Pyramid) + a, b, c, d, o = p.vertices + Quadrangle(a, b, c, d) +end + +apex(p::Pyramid) = last(p.vertices) + ==(p₁::Pyramid, p₂::Pyramid) = p₁.vertices == p₂.vertices Base.isapprox(p₁::Pyramid, p₂::Pyramid; atol=atol(lentype(p₁)), kwargs...) = all(isapprox(v₁, v₂; atol, kwargs...) for (v₁, v₂) in zip(p₁.vertices, p₂.vertices)) -function (pyramid::Pyramid)(u, v, w) +function (p::Pyramid)(u, v, w) if (u < 0 || u > 1) || (v < 0 || v > 1) || (w < 0 || w > 1) - throw(DomainError((u, v, w), "pyramid(u, v, w) is not defined for u, v, w outside [0, 1]³.")) + throw(DomainError((u, v, w), "p(u, v, w) is not defined for u, v, w outside [0, 1]³.")) end - a, b, c, d, o = vertices(pyramid) - q = Quadrangle(a, b, c, d) + q = base(p) + o = apex(p) s = Segment(q(u, v), o) s(w) end diff --git a/src/predicates/isconvex.jl b/src/predicates/isconvex.jl index f7970d0b5..9501f7e81 100644 --- a/src/predicates/isconvex.jl +++ b/src/predicates/isconvex.jl @@ -66,6 +66,8 @@ _isconvex(p::Polygon, ::Val{3}) = isconvex(proj2D(p)) isconvex(h::Hexahedron) = all(isconvex, boundary(h)) +isconvex(p::Pyramid) = isconvex(base(p)) + isconvex(m::Multi) = isapproxequal(measure(convexhull(m)), measure(m)) # -------------- diff --git a/test/polytopes.jl b/test/polytopes.jl index aed253662..6dbbaedf7 100644 --- a/test/polytopes.jl +++ b/test/polytopes.jl @@ -954,6 +954,8 @@ end equaltest(p) isapproxtest(p) vertextest(p) + @test base(p) == Quadrangle(cart(0, 0, 0), cart(1, 0, 0), cart(1, 1, 0), cart(0, 1, 0)) + @test apex(p) == cart(0, 0, 1) p = Pyramid(cart(0, 0, 0), cart(1, 0, 0), cart(1, 1, 0), cart(0, 1, 0), cart(0, 0, 1)) @test sprint(show, p) == "Pyramid((x: 0.0 m, y: 0.0 m, z: 0.0 m), ..., (x: 0.0 m, y: 0.0 m, z: 1.0 m))" diff --git a/test/predicates.jl b/test/predicates.jl index 39ebc1f73..41fe4835a 100644 --- a/test/predicates.jl +++ b/test/predicates.jl @@ -328,6 +328,10 @@ end @test isconvex(h0) @test isconvex(h1) @test isconvex(h2) + p = Pyramid(cart(0, 0, 0), cart(1, 0, 0), cart(1, 1, 0), cart(0, 1, 0), cart(0, 0, 1)) + @test isconvex(p) + p = Pyramid(cart(0, 0, 0), cart(1, 0, 0), cart(0.25, 0.25, 0), cart(0, 1, 0), cart(0, 0, 1)) + @test !isconvex(p) end @testitem "issubset" setup = [Setup] begin