Skip to content

Commit 3bf780f

Browse files
committed
fix plotting
1 parent b499f3d commit 3bf780f

File tree

5 files changed

+73
-40
lines changed

5 files changed

+73
-40
lines changed

src/geometry_primitives.jl

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,6 @@ function normals(vertices::AbstractVector{<: AbstractPoint{3, T}},
106106
return normals_result
107107
end
108108

109-
function normals(mesh::AbstractMesh)
110-
if hasproperty(mesh, :normals)
111-
return mesh.normals
112-
else
113-
return normals(coordinates(mesh), faces(mesh))
114-
end
115-
end
116-
117109
##
118110
# Some more primitive types
119111

@@ -210,8 +202,6 @@ function rotation(c::Cylinder{3, T}) where T
210202
return hcat(v, w, u)
211203
end
212204

213-
best_nvertices(x::Cylinder{2}) = (2, 2)
214-
215205
function coordinates(c::Cylinder{2, T}, nvertices=(2, 2)) where T
216206
r = Rect(c.origin[1] - c.r/2, c.origin[2], c.r, height(c))
217207
M = rotation(c)
@@ -224,8 +214,6 @@ function faces(sphere::Cylinder{2}, nvertices=(2, 2))
224214
return faces(Rect(0, 0, 1, 1), nvertices)
225215
end
226216

227-
best_nvertices(x::Cylinder{3}) = 30
228-
229217
function coordinates(c::Cylinder{3, T}, nvertices=30) where T
230218
if isodd(nvertices)
231219
nvertices = 2 * (nvertices ÷ 2)

src/interfaces.jl

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ function Tesselation(primitive::GeometryPrimitive{Dim, T}, nvertices::NTuple{N,
6060
return Tesselation{Dim, T, typeof(primitive), N}(primitive, Int.(nvertices))
6161
end
6262

63-
6463
Tesselation(primitive, nvertices::Integer) = Tesselation(primitive, (nvertices,))
6564

6665
# This is a bit lazy, I guess we should just refactor these methods
@@ -85,10 +84,13 @@ const Meshable{Dim, T} = Union{Tesselation{Dim, T}, Mesh{Dim, T},
8584

8685
struct UV{T} end
8786
UV(::Type{T}) where T = UV{T}()
87+
UV() = UV(Vec2f0)
8888
struct UVW{T} end
8989
UVW(::Type{T}) where T = UVW{T}()
90+
UVW() = UVW(Vec3f0)
9091
struct Normal{T} end
9192
Normal(::Type{T}) where T = Normal{T}()
93+
Normal() = Normal(Vec3f0)
9294

9395
function decompose(::Type{F}, primitive) where {F<:AbstractFace}
9496
f = faces(primitive)
@@ -108,16 +110,33 @@ function decompose(::Type{T}, primitive) where {T}
108110
return collect_with_eltype(T, primitive)
109111
end
110112

111-
decompose_uv(primitive) = decompose(UV(Vec2f0), primitive)
112-
decompose_uvw(primitive) = decompose(UVW(Vec3f0), primitive)
113-
decompose_normals(primitive) = decompose(Normal(Vec3f0), primitive)
113+
decompose_uv(primitive) = decompose(UV(), primitive)
114+
decompose_uvw(primitive) = decompose(UVW(), primitive)
115+
decompose_normals(primitive) = decompose(Normal(), primitive)
114116

115-
function decompose(::Normal{T}, primitive) where T
117+
function decompose(NT::Normal{T}, primitive) where T
116118
n = normals(primitive)
117-
n === nothing && return nothing
119+
if n === nothing
120+
return decompose(NT, normals(coordinates(primitive), faces(primitive)))
121+
end
118122
return collect_with_eltype(T, n)
119123
end
120124

121-
function decompose(::Union{UV{T}, UVW{T}}, primitive) where T
122-
return collect_with_eltype(T, texturecoordinates(primitive))
125+
function decompose(UVT::Union{UV{T}, UVW{T}}, primitive) where T
126+
uv = texturecoordinates(primitive)
127+
if uv === nothing
128+
return decompose(UVT, texturecoordinates(coordinates(primitive)))
129+
end
130+
return collect_with_eltype(T, uv)
131+
end
132+
133+
function texturecoordinates(positions::AbstractVector{<:VecTypes})
134+
bb = Rect(positions)
135+
return map(positions) do p
136+
return (p .- minimum(bb)) ./ widths(bb)
137+
end
123138
end
139+
140+
# Stay backward compatible:
141+
142+
decompose(::Type{T}, primitive::Meshable, nvertices) where T = decompose(T, Tesselation(primitive, nvertices))

src/meshes.jl

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@ const FaceMesh{Dim, T, Element} = Mesh{Dim, T, Element, <: FaceView{Element}}
22

33
coordinates(mesh::FaceMesh) = coordinates(getfield(mesh, :simplices))
44
faces(mesh::FaceMesh) = faces(getfield(mesh, :simplices))
5-
best_nvertices(mesh::Mesh) = nothing
5+
6+
function texturecoordinates(mesh::AbstractMesh)
7+
hasproperty(mesh, :uv) && return mesh.uv
8+
hasproperty(mesh, :uvw) && return mesh.uvw
9+
return nothing
10+
end
11+
12+
function normals(mesh::AbstractMesh)
13+
hasproperty(mesh, :normals) && return mesh.normals
14+
return nothing
15+
end
16+
617

718
const GLTriangleElement = Triangle{3, Float32}
819
const GLTriangleFace = TriangleFace{GLIndex}
@@ -74,7 +85,7 @@ const GLNormalUVWMesh{Dim} = NormalUVWMesh{Dim, Float32}
7485
const GLNormalUVWMesh2D = GLNormalUVWMesh{2}
7586
const GLNormalUVWMesh3D = GLNormalUVWMesh{3}
7687

77-
best_pointtype(::GeometryPrimitive{N, T}) where {N, T} = Point{N, T}
88+
best_pointtype(::Meshable{Dim, T}) where {Dim, T} = Point{Dim, T}
7889

7990
"""
8091
mesh(primitive::GeometryPrimitive;
@@ -106,9 +117,9 @@ function mesh(primitive::Meshable;
106117
end
107118

108119
if normaltype !== nothing
109-
primitive_normals = decompose(Normal(normaltype), primitive)
120+
primitive_normals = normals(primitive)
110121
if primitive_normals !== nothing
111-
attributes[:normals] = primitive_normals
122+
attributes[:normals] = decompose(normaltype, primitive_normals)
112123
else
113124
# Normals not implemented for primitive, so we calculate them!
114125
n = normals(positions, faces)
@@ -156,7 +167,7 @@ end
156167

157168
function normal_mesh(points::AbstractVector{<:AbstractPoint},
158169
faces::AbstractVector{<:AbstractFace})
159-
_points = convert(Vector{Point3f0}, points)
170+
_points = decompose(Point3f0, points)
160171
_faces = decompose(GLTriangleFace, faces)
161172
return Mesh(meta(_points; normals=normals(_points, _faces)), _faces)
162173
end
@@ -166,6 +177,15 @@ function normal_mesh(primitive::Meshable{N}) where {N}
166177
facetype=GLTriangleFace)
167178
end
168179

180+
## Backward compatibility
181+
function normal_mesh(primitive::GeometryPrimitive; nvertices=30)
182+
return normal_mesh(Tesselation(primitive, nvertices))
183+
end
184+
185+
function triangle_mesh(primitive::GeometryPrimitive; nvertices=30)
186+
return triangle_mesh(Tesselation(primitive, nvertices))
187+
end
188+
169189
"""
170190
volume(triangle)
171191
@@ -205,19 +225,6 @@ function Base.merge(meshes::AbstractVector{<: Mesh})
205225
end
206226
end
207227

208-
function decompose(::Normal{T}, mesh::Mesh) where {T}
209-
normal_vectors = normals(mesh)
210-
return decompose(T, normal_vectors)
211-
end
212-
213-
function decompose(::UV{T}, mesh::Mesh) where {T}
214-
if hasproperty(mesh, :uv)
215-
return decompose(T, mesh.uv)
216-
else
217-
error("Mesh doesn't have UV texture coordinates")
218-
end
219-
end
220-
221228
"""
222229
pointmeta(mesh::Mesh; meta_data...)
223230
@@ -232,6 +239,14 @@ function pointmeta(mesh::Mesh; meta_data...)
232239
return Mesh(meta(metafree(points); attr..., meta_data...), faces(mesh))
233240
end
234241

242+
function pointmeta(mesh::Mesh, uv::UV)
243+
return pointmeta(mesh; uv=decompose(uv, mesh))
244+
end
245+
246+
function pointmeta(mesh::Mesh, normal::Normal)
247+
return pointmeta(mesh; normal=decompose(normal, mesh))
248+
end
249+
235250
"""
236251
pop_pointmeta(mesh::Mesh, property::Symbol)
237252
Remove `property` from point metadata.

test/geometrytypes.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,6 @@ end
206206
@test decompose(Point2f0, mesh) decompose(Point2f0, tess_circle)
207207
end
208208

209-
210-
211209
@testset "Rectangles" begin
212210
rect = FRect2D(0, 7, 20, 3)
213211
@test (rect + 4) == FRect2D(4, 11, 20, 3)

test/runtests.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,19 @@ end
303303
points = decompose(Point2f0, Circle(Point2f0(0), 1))
304304
triangle_mesh(points)
305305
@test true # yay no errors so far!
306+
307+
m = GeometryBasics.mesh(Sphere(Point3f0(0), 1))
308+
@test normals(m) == nothing
309+
m_normals = pointmeta(m, Normal(Vec3f0))
310+
@test normals(m_normals) isa Vector{Vec3f0}
311+
312+
@test texturecoordinates(m) == nothing
313+
uv = decompose_uv(m)
314+
@test Rect(Point.(uv)) == Rect(0, 0, 1, 1)
315+
316+
points = decompose(Point2f0, Circle(Point2f0(0), 1))
317+
m = GeometryBasics.mesh(points)
318+
@test coordinates(m) === points
306319
end
307320

308321
@testset "Tests from GeometryTypes" begin

0 commit comments

Comments
 (0)