Skip to content

Commit 53293e7

Browse files
Changed orthogonal_vector to use shoelace formula
1 parent e60d315 commit 53293e7

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/geometry_primitives.jl

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,23 @@ function collect_with_eltype!(result::AbstractVector{T}, iter) where {T}
140140
end
141141

142142
"""
143-
orthogonal_vector(p1, p2, p3)
143+
orthogonal_vector(vertices)
144144
145-
Calculates an orthogonal vector `cross(p2 - p1, p3 - p1)` to a plane described
146-
by 3 points p1, p2, p3.
145+
Calculates an orthogonal vector to a face or polygon defined by a set of
146+
ordered points contained in the point vector `vertices`.
147147
"""
148-
orthogonal_vector(p1, p2, p3) = cross(p2 - p1, p3 - p1)
149-
orthogonal_vector(::Type{VT}, p1, p2, p3) where {VT} = orthogonal_vector(VT(p1), VT(p2), VT(p3))
148+
function orthogonal_vector(vertices)
149+
N = length(vertices)
150+
c = zeros(eltype(vertices)) # Inherit vector type from input
151+
@inbounds for i in eachindex(vertices) # Use shoelace approach
152+
c += cross(vertices[i],vertices[mod1(i+1,N)]) # Add each edge contribution
153+
end
154+
return c
155+
end
156+
157+
function orthogonal_vector(VT,vertices)
158+
return orthogonal_vector(VT.(vertices))
159+
end
150160

151161
"""
152162
normals(positions::Vector{Point3{T}}, faces::Vector{<: NgonFace}[; normaltype = Vec3{T}])
@@ -165,12 +175,12 @@ end
165175

166176
function normals(vertices::AbstractVector{<:Point{3}}, faces::AbstractVector{<: NgonFace},
167177
::Type{NormalType}) where {NormalType}
168-
178+
println("WOOOHOOOO")
169179
normals_result = zeros(NormalType, length(vertices))
170180
for face in faces
171181
v = vertices[face]
172182
# we can get away with two edges since faces are planar.
173-
n = orthogonal_vector(NormalType, v[1], v[2], v[3])
183+
n = orthogonal_vector(NormalType, v)
174184
for i in 1:length(face)
175185
fi = face[i]
176186
normals_result[fi] = normals_result[fi] .+ n
@@ -206,7 +216,7 @@ end
206216

207217
for (i, f) in enumerate(fs)
208218
ps = positions[f]
209-
n = orthogonal_vector(NormalType, ps[1], ps[2], ps[3])
219+
n = orthogonal_vector(NormalType, ps)
210220
normals[i] = normalize(n)
211221
faces[i] = $(FT)(i)
212222
end

0 commit comments

Comments
 (0)