Skip to content

Commit d7fc448

Browse files
authored
Merge pull request #125 from knuesel/fix-area
area and MetaFree fixes
2 parents b769ca5 + 302fdb0 commit d7fc448

File tree

6 files changed

+46
-23
lines changed

6 files changed

+46
-23
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
version:
13+
- '1.3'
1314
- '1'
1415
- 'nightly'
1516
os:

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ Tables = "0.2, 1"
1919
julia = "1.3"
2020

2121
[extras]
22+
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
2223
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2324
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2425

2526
[targets]
26-
test = ["Test", "Random"]
27+
test = ["Test", "Random", "OffsetArrays"]

src/metadata.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ macro meta_type(name, mainfield, supertype, params...)
102102
return $MetaName{$(params_sym...),ST,Names,Types}
103103
end
104104

105-
GeometryBasics.MetaFree(::Type{<:$MetaName{Typ}}) where {Typ} = Typ
105+
GeometryBasics.MetaFree(::Type{<:$MetaName{$(params_sym...),Typ}}) where {$(params_sym...), Typ<:$supertype{$(params_sym...)} } = Typ
106106
GeometryBasics.MetaFree(::Type{<:$MetaName}) = $name
107107
GeometryBasics.metafree(x::$MetaName) = getfield(x, :main)
108108
GeometryBasics.metafree(x::AbstractVector{<:$MetaName}) = getproperty(x, $field)

src/triangulation.jl

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,28 @@ function area(vertices::AbstractVector{<:AbstractPoint{3,VT}},
3333
return sum(x -> area(vertices, x), faces)
3434
end
3535

36-
function area(contour::AbstractVector{<:AbstractPoint{N,T}}) where {N,T}
37-
n = length(contour)
38-
n < 3 && return zero(T)
36+
"""
37+
area(contour::AbstractVector{AbstractPoint}})
38+
39+
Calculate the area of a polygon.
40+
41+
For 2D points, the oriented area is returned (negative when the points are
42+
oriented clockwise).
43+
"""
44+
function area(contour::AbstractVector{<:AbstractPoint{2,T}}) where {T}
45+
length(contour) < 3 && return zero(T)
3946
A = zero(T)
4047
p = lastindex(contour)
41-
q = firstindex(contour)
42-
while q <= n
48+
for q in eachindex(contour)
4349
A += cross(contour[p], contour[q])
4450
p = q
45-
q += 1
4651
end
4752
return A * T(0.5)
4853
end
4954

50-
function area(contour::AbstractVector{Point{3,T}}) where {T}
55+
function area(contour::AbstractVector{<:AbstractPoint{3,T}}) where {T}
5156
A = zero(eltype(contour))
52-
o = contour[1]
57+
o = first(contour)
5358
for i in (firstindex(contour) + 1):(lastindex(contour) - 1)
5459
A += cross(contour[i] - o, contour[i + 1] - o)
5560
end
@@ -59,8 +64,7 @@ end
5964
"""
6065
in(point, triangle)
6166
62-
InsideTriangle decides if a point P is Inside of the triangle
63-
defined by A, B, C.
67+
Determine if a point is inside of a triangle.
6468
"""
6569
function Base.in(P::T, triangle::Triangle) where {T<:AbstractPoint}
6670
A, B, C = coordinates(triangle)

test/geometrytypes.jl

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
using Test, GeometryBasics
22

3-
@testset "algorithms.jl" begin
4-
cube = Rect(Vec3f0(-0.5), Vec3f0(1))
5-
cube_faces = decompose(TriangleFace{Int}, faces(cube))
6-
cube_vertices = decompose(Point{3,Float32}, cube)
7-
@test area(cube_vertices, cube_faces) == 6
8-
mesh = Mesh(cube_vertices, cube_faces)
9-
@test GeometryBasics.volume(mesh) 1
10-
end
11-
123
@testset "Cylinder" begin
134
@testset "constructors" begin
145
o, extr, r = Point2f0(1, 2), Point2f0(3, 4), 5.0f0

test/runtests.jl

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
1-
using Test, Random, StructArrays, Tables, StaticArrays
1+
using Test, Random, StructArrays, Tables, StaticArrays, OffsetArrays
22
using GeometryBasics
33
using LinearAlgebra
44
using GeometryBasics: attributes
55

66
@testset "GeometryBasics" begin
77

8+
@testset "algorithms" begin
9+
cube = Rect(Vec3f0(-0.5), Vec3f0(1))
10+
cube_faces = decompose(TriangleFace{Int}, faces(cube))
11+
cube_vertices = decompose(Point{3,Float32}, cube)
12+
@test area(cube_vertices, cube_faces) == 6
13+
mesh = Mesh(cube_vertices, cube_faces)
14+
@test GeometryBasics.volume(mesh) 1
15+
16+
points_cwise = Point2f0[(0,0), (0,1), (1,1)]
17+
points_ccwise = Point2f0[(0,0), (1,0), (1,1)]
18+
@test area(points_cwise) -0.5
19+
@test area(points_ccwise) 0.5
20+
@test area(OffsetArray(points_cwise, -2)) -0.5
21+
22+
points3d = Point3f0[(0,0,0), (0,0,1), (0,1,1)]
23+
@test area(OffsetArray(points3d, -2)) 0.5
24+
25+
pm2d = [PointMeta(0.0, 0.0, a=:d), PointMeta(0.0, 1.0, a=:e), PointMeta(1.0, 0.0, a=:f)]
26+
@test area(pm2d) -0.5
27+
28+
pm3d = [PointMeta(0.0, 0.0, 0.0, a=:d), PointMeta(0.0, 1.0, 0.0, a=:e), PointMeta(1.0, 0.0, 0.0, a=:f)]
29+
@test_broken area(pm3d) 0.5 # Currently broken as zero(PointMeta(0.0, 0.0, 0.0, a=:d)) fails
30+
end
31+
832
@testset "embedding metadata" begin
933
@testset "Meshes" begin
1034

@@ -84,13 +108,15 @@ using GeometryBasics: attributes
84108
@testset "point with metadata" begin
85109
p = Point(1.1, 2.2)
86110
@test p isa AbstractVector{Float64}
87-
pm = GeometryBasics.PointMeta(1.1, 2.2; a=1, b=2)
111+
pm = PointMeta(1.1, 2.2; a=1, b=2)
88112
p1 = Point(2.2, 3.6)
89113
p2 = [p, p1]
90114
@test coordinates(p2) == p2
91115
@test meta(pm) === (a=1, b=2)
92116
@test metafree(pm) === p
93117
@test propertynames(pm) == (:position, :a, :b)
118+
@test GeometryBasics.MetaFree(typeof(pm)) == Point{2,Float64}
119+
@test_broken zero(pm) == [0, 0]
94120
end
95121

96122
@testset "MultiPoint with metadata" begin

0 commit comments

Comments
 (0)