Skip to content

Commit 1ec04f9

Browse files
committed
add tests for missed functions
1 parent e073fa8 commit 1ec04f9

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/geometry_primitives.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ end
145145
Calculates an orthogonal vector to a polygon defined by a vector of ordered
146146
`points`. Note that the orthogonal vector to a collection of 2D points needs to
147147
expand to 3D space.
148+
149+
Note that this vector is not normalized.
148150
"""
149151
function orthogonal_vector(::Type{VT}, vertices) where {VT <: VecTypes{3}}
150152
c = zeros(VT) # Inherit vector type from input
@@ -157,6 +159,17 @@ function orthogonal_vector(::Type{VT}, vertices) where {VT <: VecTypes{3}}
157159
return c
158160
end
159161

162+
function orthogonal_vector(::Type{VT}, vertices::Tuple) where {VT <: VecTypes{3}}
163+
c = zeros(VT) # Inherit vector type from input
164+
prev = to_ndim(VT, last(vertices), 0)
165+
@inbounds for p in vertices # Use shoelace approach
166+
v = to_ndim(VT, p, 0)
167+
c += cross(prev, v) # Add each edge contribution
168+
prev = v
169+
end
170+
return c
171+
end
172+
160173
# Not sure how useful this fast path is, but it's simple to keep
161174
function orthogonal_vector(::Type{VT}, triangle::Triangle) where {VT <: VecTypes{3}}
162175
a, b, c = triangle

test/geometrytypes.jl

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,28 @@ end
334334

335335
@testset "orthogonal_vector" begin
336336
tri = Triangle(Point3d(0,0,0), Point3d(1,0,0), Point3d(0,1,0))
337-
@test GeometryBasics.orthogonal_vector(tri) == Vec3d(0,0,1)
338-
@test GeometryBasics.orthogonal_vector(collect(coordinates(tri))) == Vec3d(0,0,1)
337+
@test GeometryBasics.orthogonal_vector(tri) === Vec3d(0,0,1)
338+
@test GeometryBasics.orthogonal_vector(collect(coordinates(tri))) === Vec3d(0,0,1)
339+
@test GeometryBasics.orthogonal_vector(Vec3f, tri) === Vec3f(0,0,1)
340+
@test GeometryBasics.orthogonal_vector(Vec3f, collect(coordinates(tri))) === Vec3f(0,0,1)
339341

340342
quad = GeometryBasics.Quadrilateral(Point2i(0,0), Point2i(1,0), Point2i(1,1), Point2i(0,1))
341-
@test GeometryBasics.orthogonal_vector(quad) == Vec3i(0,0,2)
342-
@test GeometryBasics.orthogonal_vector(collect(coordinates(quad))) == Vec3i(0,0,2)
343+
@test GeometryBasics.orthogonal_vector(quad) === Vec3i(0,0,2)
344+
@test GeometryBasics.orthogonal_vector(collect(coordinates(quad))) === Vec3i(0,0,2)
345+
@test GeometryBasics.orthogonal_vector(Vec3d, quad) === Vec3d(0,0,2)
346+
@test GeometryBasics.orthogonal_vector(Vec3d, collect(coordinates(quad))) === Vec3d(0,0,2)
347+
348+
t = (Point3f(0), Point3f(1,0,1), Point3f(0,1,0))
349+
@test GeometryBasics.orthogonal_vector(t) == Vec3f(-1,0,1)
350+
@test GeometryBasics.orthogonal_vector(Vec3i, t) == Vec3i(-1,0,1)#
351+
352+
# Maybe the ::Any fallback is too generic...?
353+
struct TestType
354+
data::Vector{Vec3f}
355+
end
356+
GeometryBasics.coordinates(x::TestType) = x.data
357+
x = TestType([Point3f(1,1,1), Point3f(0,0,0), Point3f(0.5,0,0)])
358+
@test GeometryBasics.orthogonal_vector(x) == Vec3f(0, -0.5, 0.5)
343359
end
344360

345361
@testset "Normals" begin

0 commit comments

Comments
 (0)