Skip to content

Commit 691f734

Browse files
Further attempt to fix use of new orthogonal_vector
1 parent 0de3ee3 commit 691f734

File tree

3 files changed

+30
-39
lines changed

3 files changed

+30
-39
lines changed

src/geometry_primitives.jl

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,22 @@ end
143143
orthogonal_vector(vertices)
144144
145145
Calculates an orthogonal vector to a face or polygon defined by a set of
146-
ordered points contained in the point vector `vertices`.
146+
ordered points contained in the point vector `vertices`. Note that for 2D points
147+
a scalar is returned (the dot product of the orthogonal vector with the
148+
positive Z-direction).
147149
"""
148-
function orthogonal_vector(vertices)
149-
N = length(vertices)
150-
c = zeros(eltype(vertices)) # Inherit vector type from input
150+
function orthogonal_vector(::Type{VT},vertices) where VT
151+
c = zeros(VT) # Inherit vector type from input
152+
j = length(vertices)
151153
@inbounds for i in eachindex(vertices) # Use shoelace approach
152-
c += cross(vertices[i],vertices[mod1(i+1,N)]) # Add each edge contribution
154+
c += cross(vertices[j],vertices[i]) # Add each edge contribution
155+
j = i
153156
end
154157
return c
155158
end
156159

157-
function orthogonal_vector(VT,vertices)
158-
return orthogonal_vector(VT.(vertices))
160+
function orthogonal_vector(vertices)
161+
return orthogonal_vector(eltype(vertices),vertices)
159162
end
160163

161164
"""
@@ -178,7 +181,7 @@ function normals(vertices::AbstractVector{<:Point{3}}, faces::AbstractVector{<:
178181

179182
normals_result = zeros(NormalType, length(vertices))
180183
for face in faces
181-
v = vertices[face]
184+
v = coordinates(vertices[face])
182185
# we can get away with two edges since faces are planar.
183186
n = orthogonal_vector(NormalType, v)
184187
for i in 1:length(face)
@@ -215,7 +218,7 @@ end
215218
faces = resize!(F[], length(fs))
216219

217220
for (i, f) in enumerate(fs)
218-
ps = positions[f]
221+
ps = coordinates(positions[f])
219222
n = orthogonal_vector(NormalType, ps)
220223
normals[i] = normalize(n)
221224
faces[i] = $(FT)(i)

src/meshes.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,8 @@ end
213213
Calculate the signed volume of one tetrahedron. Be sure the orientation of your
214214
surface is right.
215215
"""
216-
function volume(triangle::Triangle)
217-
v1, v2, v3 = triangle
218-
sig = sign(orthogonal_vector(v1, v2, v3) v1)
216+
function volume(triangle::Triangle)
217+
sig = sign(orthogonal_vector(triangle.points) v1)
219218
return sig * abs(v1 (v2 × v3)) / 6
220219
end
221220

src/triangulation.jl

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,54 +13,43 @@ The above copyright notice and this permission notice shall be included in all c
1313
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1414
=#
1515
"""
16-
area(vertices::AbstractVector{Point{3}}, face::TriangleFace)
16+
area(vertices::AbstractVector{Point{3}}, face::NgonFace)
1717
18-
Calculate the area of one triangle.
18+
Calculate the area of one face.
1919
"""
20-
function area(vertices::AbstractVector{<:Point{3,VT}},
21-
face::TriangleFace{FT}) where {VT,FT}
22-
v1, v2, v3 = vertices[face]
23-
return 0.5 * norm(orthogonal_vector(v1, v2, v3))
20+
function area(vertices::AbstractVector{<:Point},
21+
face::NgonFace)
22+
return 0.5 * norm(orthogonal_vector(vertices[face]))
2423
end
2524

2625
"""
27-
area(vertices::AbstractVector{Point{3}}, faces::AbstractVector{TriangleFace})
26+
area(vertices::AbstractVector{Point}, faces::AbstractVector{NgonFace})
2827
29-
Calculate the area of all triangles.
28+
Calculate the area of all faces.
3029
"""
31-
function area(vertices::AbstractVector{Point{3,VT}},
32-
faces::AbstractVector{TriangleFace{FT}}) where {VT,FT}
30+
function area(vertices::AbstractVector{<:Point},
31+
faces::AbstractVector{<:NgonFace})
3332
return sum(x -> area(vertices, x), faces)
3433
end
3534

3635
"""
37-
area(contour::AbstractVector{Point}})
36+
area(vertices::AbstractVector{Point}})
3837
3938
Calculate the area of a polygon.
4039
4140
For 2D points, the oriented area is returned (negative when the points are
4241
oriented clockwise).
4342
"""
44-
function area(contour::AbstractVector{Point{2,T}}) where {T}
45-
length(contour) < 3 && return zero(T)
46-
A = zero(T)
47-
p = lastindex(contour)
48-
for q in eachindex(contour)
49-
A += cross(contour[p], contour[q])
50-
p = q
51-
end
52-
return A * T(0.5)
43+
function area(vertices::AbstractVector{Point{2,T}}) where {T}
44+
length(vertices) < 3 && return zero(T)
45+
return T(0.5) * orthogonal_vector(vertices)
5346
end
5447

55-
function area(contour::AbstractVector{Point{3,T}}) where {T}
56-
A = zero(eltype(contour))
57-
o = first(contour)
58-
for i in (firstindex(contour) + 1):(lastindex(contour) - 1)
59-
A += cross(contour[i] - o, contour[i + 1] - o)
60-
end
61-
return norm(A) * T(0.5)
48+
function area(vertices::AbstractVector{Point{3,T}}) where {T}
49+
return T(0.5) * norm(orthogonal_vector(vertices))
6250
end
6351

52+
6453
"""
6554
in(point, triangle)
6655

0 commit comments

Comments
 (0)