Skip to content

Commit 81488f9

Browse files
committed
update normal gen tests + fixes
- fix normal gen for varying face types - fix normalization of face_normals
1 parent 18ad6f0 commit 81488f9

File tree

4 files changed

+33
-26
lines changed

4 files changed

+33
-26
lines changed

src/geometry_primitives.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,15 @@ function normals(vertices::AbstractVector{Point{3,T}}, faces::AbstractVector{F};
160160
return normals(vertices, faces, normaltype)
161161
end
162162

163-
function normals(vertices::AbstractVector{<:Point{3}}, faces::AbstractVector{F},
164-
::Type{NormalType}) where {F<:NgonFace,NormalType}
163+
function normals(vertices::AbstractVector{<:Point{3}}, faces::AbstractVector{<: NgonFace},
164+
::Type{NormalType}) where {NormalType}
165165

166166
normals_result = zeros(NormalType, length(vertices))
167167
for face in faces
168168
v = vertices[face]
169169
# we can get away with two edges since faces are planar.
170170
n = orthogonal_vector(v[1], v[2], v[3])
171-
for i in 1:length(F)
171+
for i in 1:length(face)
172172
fi = face[i]
173173
normals_result[fi] = normals_result[fi] .+ n
174174
end
@@ -198,14 +198,14 @@ end
198198
FT = ifelse(isconcretetype(F), :($F), :(typeof(f)))
199199

200200
quote
201-
normals = sizehint!(Vec3f[], length(fs))
202-
faces = sizehint!(F[], length(fs))
201+
normals = resize!(NormalType[], length(fs))
202+
faces = resize!(F[], length(fs))
203203

204-
for f in fs
204+
for (i, f) in enumerate(fs)
205205
ps = positions[f]
206206
n = GeometryBasics.orthogonal_vector(ps[1], ps[2], ps[3])
207-
push!(normals, n)
208-
push!(faces, $(FT)(length(normals)))
207+
normals[i] = normalize(n)
208+
faces[i] = $(FT)(i)
209209
end
210210

211211
return FaceView(normals, faces)

test/geometrytypes.jl

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,31 @@ NFace = NgonFace
138138
end
139139

140140
@testset "Normals" begin
141-
# Needs to be reworked
142-
@test_broken false
143-
144-
# r = centered(Rect3{Float64})
145-
# n64 = [n for n in normals(r) for _ in 1:4]
146-
# n32 = map(Vec{3,Float32}, n64)
147-
# m32 = normal_mesh(r)
148-
# m64 = normal_mesh(r, pointtype = Point3d)
149-
# @test normals(coordinates(m32), GeometryBasics.faces(m32)) == n32
150-
# @test normals(coordinates(m64), GeometryBasics.faces(m64)) == n64
141+
# per face normals
142+
r = Rect3f(Point3f(0), Vec3f(1))
143+
ns = face_normals(coordinates(r), faces(r))
144+
ux = unit(Vec3f, 1); uy = unit(Vec3f, 2); uz = unit(Vec3f, 3)
145+
@test ns == normals(r)
146+
@test values(ns) == [-ux, ux, -uy, uy, -uz, uz]
147+
148+
# typing
149+
ux = unit(Vec3d, 1); uy = unit(Vec3d, 2); uz = unit(Vec3d, 3)
150+
ns = face_normals(decompose(Point3d, r), faces(r))
151+
@test ns isa FaceView{Vec3d}
152+
@test values(ns) == [-ux, ux, -uy, uy, -uz, uz]
153+
154+
# Mixed
155+
c = Cylinder(Point3f(0), Point3f(0,0,1), 0.5f0)
156+
ns = normals(c)
157+
# caps without mantle
158+
f_ns = face_normals(coordinates(c), filter!(f -> f isa TriangleFace, faces(c)))
159+
@test all(n -> n == values(ns)[end-1], values(f_ns)[1:15])
160+
@test all(n -> n == values(ns)[end], values(f_ns)[16:end])
161+
# Mantle without caps
162+
v_ns = normals(coordinates(c), filter!(f -> f isa QuadFace, faces(c)))[1:end-2]
163+
@test values(ns)[1:15] v_ns[1:15]
164+
@test values(ns)[1:15] v_ns[16:30] # repeated via FaceView in ns
165+
151166
end
152167

153168
@testset "HyperSphere" begin

test/meshes.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ end
3535

3636
@testset "Vertex Index Remapping" begin
3737
# Sanity Check
38-
# TODO: extend
3938
m = Mesh(
4039
position = GeometryBasics.FaceView(Point2f[(0, 0), (1, 0), (1, 1), (0, 1)], [QuadFace(1,2,3,4)]),
4140
normal = GeometryBasics.FaceView([Vec3f(0,0,1)], [QuadFace(1)])

test/runtests.jl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ end
4444
@test mesh.normal === normals
4545
@test mesh.position === points
4646
@test GeometryBasics.faces(mesh) === tfaces
47-
# TODO: Is the order variable with Dict?
4847
@test propertynames(mesh) == (:vertex_attributes, :faces, :views, :position, :normal, :stress)
4948
end
5049
end
@@ -295,12 +294,6 @@ end
295294
@test <(x, x1)
296295
end
297296

298-
@testset "Face normals" begin
299-
r = Rect3f(Point3f(0), Vec3f(1))
300-
ns = normals(r)
301-
@test GeometryBasics.face_normals(coordinates(r), faces(r)) == ns
302-
end
303-
304297
@testset "Tests from GeometryTypes" begin
305298
include("geometrytypes.jl")
306299
end

0 commit comments

Comments
 (0)