Skip to content

Commit 0983e44

Browse files
cserteGT3juliohmJoshuaLampert
authored
implement isconvex for Pyramid (#1266)
* implement specialized isconvex for Pyramid * use internal field access instead of accessor in Pyramid * Export base in Polytope section for consistency * document pyramid base order and convexity requirement * add test for pyramid base * Incorporate Joshua's suggestions * fix typo and use base function for pyramid * Apply suggestion from @juliohm * Add apex for Pyramid * Update src/geometries/polytopes/pyramid.jl Co-authored-by: Joshua Lampert <[email protected]> * Apply suggestion from @juliohm * Apply suggestion from @juliohm * format test/predicates.jl --------- Co-authored-by: Júlio Hoffimann <[email protected]> Co-authored-by: Joshua Lampert <[email protected]>
1 parent 7c2fbc3 commit 0983e44

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed

src/Meshes.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ export
226226
angles,
227227
innerangles,
228228
normal,
229+
base,
230+
apex,
229231
,
230232

231233
# multi-geometries

src/geometries/polytopes/pyramid.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,31 @@
66
Pyramid(p1, p2, p3, p4, p5)
77
88
A pyramid with points `p1`, `p2`, `p3`, `p4`, `p5`.
9+
10+
The first four points form the base and the last is the apex.
911
"""
1012
@polytope Pyramid 3 5
1113

1214
nvertices(::Type{<:Pyramid}) = 5
1315

16+
function base(p::Pyramid)
17+
a, b, c, d, o = p.vertices
18+
Quadrangle(a, b, c, d)
19+
end
20+
21+
apex(p::Pyramid) = last(p.vertices)
22+
1423
==(p₁::Pyramid, p₂::Pyramid) = p₁.vertices == p₂.vertices
1524

1625
Base.isapprox(p₁::Pyramid, p₂::Pyramid; atol=atol(lentype(p₁)), kwargs...) =
1726
all(isapprox(v₁, v₂; atol, kwargs...) for (v₁, v₂) in zip(p₁.vertices, p₂.vertices))
1827

19-
function (pyramid::Pyramid)(u, v, w)
28+
function (p::Pyramid)(u, v, w)
2029
if (u < 0 || u > 1) || (v < 0 || v > 1) || (w < 0 || w > 1)
21-
throw(DomainError((u, v, w), "pyramid(u, v, w) is not defined for u, v, w outside [0, 1]³."))
30+
throw(DomainError((u, v, w), "p(u, v, w) is not defined for u, v, w outside [0, 1]³."))
2231
end
23-
a, b, c, d, o = vertices(pyramid)
24-
q = Quadrangle(a, b, c, d)
32+
q = base(p)
33+
o = apex(p)
2534
s = Segment(q(u, v), o)
2635
s(w)
2736
end

src/predicates/isconvex.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ _isconvex(p::Polygon, ::Val{3}) = isconvex(proj2D(p))
6666

6767
isconvex(h::Hexahedron) = all(isconvex, boundary(h))
6868

69+
isconvex(p::Pyramid) = isconvex(base(p))
70+
6971
isconvex(m::Multi) = isapproxequal(measure(convexhull(m)), measure(m))
7072

7173
# --------------

test/polytopes.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,8 @@ end
954954
equaltest(p)
955955
isapproxtest(p)
956956
vertextest(p)
957+
@test base(p) == Quadrangle(cart(0, 0, 0), cart(1, 0, 0), cart(1, 1, 0), cart(0, 1, 0))
958+
@test apex(p) == cart(0, 0, 1)
957959

958960
p = Pyramid(cart(0, 0, 0), cart(1, 0, 0), cart(1, 1, 0), cart(0, 1, 0), cart(0, 0, 1))
959961
@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))"

test/predicates.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ end
328328
@test isconvex(h0)
329329
@test isconvex(h1)
330330
@test isconvex(h2)
331+
p = Pyramid(cart(0, 0, 0), cart(1, 0, 0), cart(1, 1, 0), cart(0, 1, 0), cart(0, 0, 1))
332+
@test isconvex(p)
333+
p = Pyramid(cart(0, 0, 0), cart(1, 0, 0), cart(0.25, 0.25, 0), cart(0, 1, 0), cart(0, 0, 1))
334+
@test !isconvex(p)
331335
end
332336

333337
@testitem "issubset" setup = [Setup] begin

0 commit comments

Comments
 (0)