Skip to content

Commit a021f49

Browse files
authored
Merge pull request #122 from knuesel/fix-polygon-faces
Fix mesh(::AbstractPolygon) in case faces returns nothing
2 parents 24d0ee5 + 12a820b commit a021f49

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/meshes.jl

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ const GLNormalUVWMesh{Dim} = NormalUVWMesh{Dim,Float32}
8686
const GLNormalUVWMesh2D = GLNormalUVWMesh{2}
8787
const GLNormalUVWMesh3D = GLNormalUVWMesh{3}
8888

89+
function decompose_triangulate_fallback(primitive::Meshable; pointtype, facetype)
90+
positions = decompose(pointtype, primitive)
91+
faces = decompose(facetype, primitive)
92+
# If faces returns nothing for primitive, we try to triangulate!
93+
if faces === nothing
94+
# triangulation.jl
95+
faces = decompose(facetype, positions)
96+
end
97+
return positions, faces
98+
end
99+
89100
"""
90101
mesh(primitive::GeometryPrimitive;
91102
pointtype=Point, facetype=GLTriangle,
@@ -101,13 +112,7 @@ It also only losely correlates to the number of vertices, depending on the algor
101112
function mesh(primitive::Meshable; pointtype=Point, facetype=GLTriangleFace, uv=nothing,
102113
normaltype=nothing)
103114

104-
positions = decompose(pointtype, primitive)
105-
faces = decompose(facetype, primitive)
106-
# If faces returns nothing for primitive, we try to triangulate!
107-
if faces === nothing
108-
# triangulation.jl
109-
faces = decompose(facetype, positions)
110-
end
115+
positions, faces = decompose_triangulate_fallback(primitive; pointtype, facetype)
111116

112117
# We want to preserve any existing attributes!
113118
attrs = attributes(primitive)
@@ -151,8 +156,7 @@ end
151156
function mesh(polygon::AbstractPolygon{Dim,T}; pointtype=Point{Dim,T},
152157
facetype=GLTriangleFace, normaltype=nothing) where {Dim,T}
153158

154-
faces = decompose(facetype, polygon)
155-
positions = decompose(pointtype, polygon)
159+
positions, faces = decompose_triangulate_fallback(polygon; pointtype, facetype)
156160

157161
if normaltype !== nothing
158162
n = normals(positions, faces; normaltype=normaltype)

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,12 @@ end
489489
@test decompose(Point{2, Int}, poly_ext_int) == [pts_ext..., pts_int1..., pts_int2...]
490490
end
491491

492+
@testset "mesh" begin
493+
primitive = Triangle(Point2f0(0), Point2f0(1), Point2f0(1,0))
494+
m = GeometryBasics.mesh(primitive)
495+
@test length(faces(m)) == 1
496+
end
497+
492498
@testset "convert mesh + meta" begin
493499
m = uv_normal_mesh(Circle(Point2f0(0), 1f0))
494500
# for 2D primitives we dont actually calculate normals

0 commit comments

Comments
 (0)