Skip to content

Commit d0ea0a0

Browse files
committed
Minor Refactor
1 parent 46220f8 commit d0ea0a0

File tree

2 files changed

+129
-123
lines changed

2 files changed

+129
-123
lines changed

src/metadata.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ end
66
Feature(x; kwargs...) = Feature(x, values(kwargs))
77

88
#can change names?
9-
GeometryBasics.metafree(F::Feature) = getproperty(F, :data)
10-
GeometryBasics.metafree(x::AbstractVector{<: Feature}) = [getproperty(i, :data) for i in x]
11-
GeometryBasics.meta(x::Feature) = getfield(x, :rest)
12-
GeometryBasics.meta(x::AbstractVector{<: Feature}) = [getproperty(i, :rest) for i in x]
9+
function metafree(F::Feature)
10+
getproperty(F, :data)
11+
end
12+
metafree(x::AbstractVector{<: Feature}) = [getproperty(i, :data) for i in x]
13+
14+
function meta(x::Feature)
15+
getfield(x, :rest)
16+
end
17+
meta(x::AbstractVector{<: Feature}) = [getproperty(i, :rest) for i in x]
1318

1419
Base.getproperty(f::Feature, s::Symbol) = s == :data ? getfield(f, 1) : s == :rest ? getfield(f, 2) : getproperty(getfield(f, 2), s)
1520
Base.propertynames(f::Feature) = (:data, propertynames(f.rest)...)

test/runtests.jl

Lines changed: 120 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -7,131 +7,132 @@ using GeometryBasics: attributes
77

88
#= This left till meta is removed completely
99
# @testset "embedding metadata" begin
10-
# @testset "Meshes" begin
11-
12-
# @testset "per vertex attributes" begin
13-
# points = rand(Point{3, Float64}, 8)
14-
# tfaces = TetrahedronFace{Int}[(1, 2, 3, 4), (5, 6, 7, 8)]
15-
# normals = rand(SVector{3, Float64}, 8)
16-
# stress = LinRange(0, 1, 8)
17-
# mesh = Mesh(meta(points, normals = normals, stress = stress), tfaces)
18-
19-
# @test hasproperty(coordinates(mesh), :stress)
20-
# @test hasproperty(coordinates(mesh), :normals)
21-
# @test coordinates(mesh).stress === stress
22-
# @test coordinates(mesh).normals === normals
23-
# @test coordinates(mesh).normals === normals
24-
# @test GeometryBasics.faces(mesh) === tfaces
25-
# @test propertynames(coordinates(mesh)) == (:position, :normals, :stress)
26-
27-
# end
28-
29-
# @testset "per face attributes" begin
30-
31-
# # Construct a cube out of Quads
32-
# points = Point{3, Float64}[
33-
# (0.0, 0.0, 0.0), (2.0, 0.0, 0.0),
34-
# (2.0, 2.0, 0.0), (0.0, 2.0, 0.0),
35-
# (0.0, 0.0, 12.0), (2.0, 0.0, 12.0),
36-
# (2.0, 2.0, 12.0), (0.0, 2.0, 12.0)
37-
# ]
38-
39-
# facets = QuadFace{Cint}[
40-
# 1:4,
41-
# 5:8,
42-
# [1,5,6,2],
43-
# [2,6,7,3],
44-
# [3, 7, 8, 4],
45-
# [4, 8, 5, 1]
46-
# ]
47-
48-
# markers = Cint[-1, -2, 0, 0, 0, 0]
49-
# # attach some additional information to our faces!
50-
# mesh = Mesh(points, meta(facets, markers = markers))
51-
# @test hasproperty(GeometryBasics.faces(mesh), :markers)
52-
# # test with === to assert we're not doing any copies
53-
# @test GeometryBasics.faces(mesh).markers === markers
54-
# @test coordinates(mesh) === points
55-
# @test metafree(GeometryBasics.faces(mesh)) === facets
56-
57-
# end
58-
59-
# end
10+
@testset "Meshes" begin
11+
12+
@testset "per vertex attributes" begin
13+
points = rand(Point{3, Float64}, 8)
14+
tfaces = TetrahedronFace{Int}[(1, 2, 3, 4), (5, 6, 7, 8)]
15+
normals = rand(SVector{3, Float64}, 8)
16+
stress = LinRange(0, 1, 8)
17+
mesh = Mesh(meta(points, normals = normals, stress = stress), tfaces)
18+
19+
@test hasproperty(coordinates(mesh), :stress)
20+
@test hasproperty(coordinates(mesh), :normals)
21+
@test coordinates(mesh).stress === stress
22+
@test coordinates(mesh).normals === normals
23+
@test coordinates(mesh).normals === normals
24+
@test GeometryBasics.faces(mesh) === tfaces
25+
@test propertynames(coordinates(mesh)) == (:position, :normals, :stress)
26+
27+
end
28+
29+
@testset "per face attributes" begin
30+
31+
# Construct a cube out of Quads
32+
points = Point{3, Float64}[
33+
(0.0, 0.0, 0.0), (2.0, 0.0, 0.0),
34+
(2.0, 2.0, 0.0), (0.0, 2.0, 0.0),
35+
(0.0, 0.0, 12.0), (2.0, 0.0, 12.0),
36+
(2.0, 2.0, 12.0), (0.0, 2.0, 12.0)
37+
]
38+
39+
facets = QuadFace{Cint}[
40+
1:4,
41+
5:8,
42+
[1,5,6,2],
43+
[2,6,7,3],
44+
[3, 7, 8, 4],
45+
[4, 8, 5, 1]
46+
]
47+
48+
markers = Cint[-1, -2, 0, 0, 0, 0]
49+
# attach some additional information to our faces!
50+
mesh = Mesh(points, meta(facets, markers = markers))
51+
@test hasproperty(GeometryBasics.faces(mesh), :markers)
52+
# test with === to assert we're not doing any copies
53+
@test GeometryBasics.faces(mesh).markers === markers
54+
@test coordinates(mesh) === points
55+
@test metafree(GeometryBasics.faces(mesh)) === facets
56+
57+
end
58+
59+
end
6060
61-
# @testset "polygon with metadata" begin
62-
# polys = [Polygon(rand(Point{2, Float32}, 20)) for i in 1:10]
63-
# pnames = [randstring(4) for i in 1:10]
64-
# numbers = LinRange(0.0, 1.0, 10)
65-
# bin = rand(Bool, 10)
66-
# # create a polygon
67-
# poly = PolygonMeta(polys[1], name = pnames[1], value = numbers[1], category = bin[1])
68-
# # create a MultiPolygon with the right type & meta information!
69-
# multipoly = MultiPolygonMeta(polys, name = pnames, value = numbers, category = bin)
70-
# @test multipoly isa AbstractVector
71-
# @test poly isa GeometryBasics.AbstractPolygon
61+
@testset "polygon with metadata" begin
62+
polys = [Polygon(rand(Point{2, Float32}, 20)) for i in 1:10]
63+
pnames = [randstring(4) for i in 1:10]
64+
numbers = LinRange(0.0, 1.0, 10)
65+
bin = rand(Bool, 10)
66+
# create a polygon
67+
poly = PolygonMeta(polys[1], name = pnames[1], value = numbers[1], category = bin[1])
68+
# create a MultiPolygon with the right type & meta information!
69+
multipoly = MultiPolygonMeta(polys, name = pnames, value = numbers, category = bin)
70+
@test multipoly isa AbstractVector
71+
@test poly isa GeometryBasics.AbstractPolygon
7272
73-
# @test GeometryBasics.getcolumn(poly, :name) == pnames[1]
74-
# @test GeometryBasics.MetaFree(PolygonMeta) == Polygon
75-
76-
# @test GeometryBasics.getcolumn(multipoly, :name) == pnames
77-
# @test GeometryBasics.MetaFree(MultiPolygonMeta) == MultiPolygon
78-
79-
# meta_p = meta(polys[1], boundingbox=Rect(0, 0, 2, 2))
80-
# @test meta_p.boundingbox === Rect(0, 0, 2, 2)
81-
# @test metafree(meta_p) === polys[1]
82-
# attributes(meta_p) == Dict{Symbol, Any}(:boundingbox => meta_p.boundingbox,
83-
# :polygon => polys[1])
84-
# end
85-
# @testset "point with metadata" begin
86-
# p = Point(1.1, 2.2)
87-
# @test p isa AbstractVector{Float64}
88-
# pm = GeometryBasics.PointMeta(1.1, 2.2; a=1, b=2)
89-
# p1 = Point(2.2, 3.6)
90-
# p2 = [p, p1]
91-
# @test coordinates(p2) == p2
92-
# @test meta(pm) === (a=1, b=2)
93-
# @test metafree(pm) === p
94-
# @test propertynames(pm) == (:position, :a, :b)
95-
# end
73+
@test GeometryBasics.getcolumn(poly, :name) == pnames[1]
74+
@test GeometryBasics.MetaFree(PolygonMeta) == Polygon
75+
76+
@test GeometryBasics.getcolumn(multipoly, :name) == pnames
77+
@test GeometryBasics.MetaFree(MultiPolygonMeta) == MultiPolygon
78+
79+
meta_p = meta(polys[1], boundingbox=Rect(0, 0, 2, 2))
80+
@test meta_p.boundingbox === Rect(0, 0, 2, 2)
81+
@test metafree(meta_p) === polys[1]
82+
attributes(meta_p) == Dict{Symbol, Any}(:boundingbox => meta_p.boundingbox,
83+
:polygon => polys[1])
84+
end
85+
@testset "point with metadata" begin
86+
p = Point(1.1, 2.2)
87+
@test p isa AbstractVector{Float64}
88+
pm = GeometryBasics.PointMeta(1.1, 2.2; a=1, b=2)
89+
p1 = Point(2.2, 3.6)
90+
p2 = [p, p1]
91+
@test coordinates(p2) == p2
92+
@test meta(pm) === (a=1, b=2)
93+
@test metafree(pm) === p
94+
@test propertynames(pm) == (:position, :a, :b)
95+
end
9696
97-
# @testset "MultiPoint with metadata" begin
98-
# p = collect(Point{2, Float64}(x, x+1) for x in 1:5)
99-
# @test p isa AbstractVector
100-
# mpm = MultiPointMeta(p, a=1, b=2)
101-
# @test coordinates(mpm) == mpm
102-
# @test meta(mpm) === (a=1, b=2)
103-
# @test metafree(mpm) == p
104-
# @test propertynames(mpm) == (:points, :a, :b)
105-
# end
106-
107-
# @testset "LineString with metadata" begin
108-
# linestring = LineStringMeta(Point{2, Int}[(10, 10), (20, 20), (10, 40)], a = 1, b = 2)
109-
# @test linestring isa AbstractVector
110-
# @test meta(linestring) === (a = 1, b = 2)
111-
# @test metafree(linestring) == linestring
112-
# @test propertynames(linestring) == (:lines, :a, :b)
113-
# end
114-
115-
# @testset "MultiLineString with metadata" begin
116-
# linestring1 = LineString(Point{2, Int}[(10, 10), (20, 20), (10, 40)])
117-
# linestring2 = LineString(Point{2, Int}[(40, 40), (30, 30), (40, 20), (30, 10)])
118-
# multilinestring = MultiLineString([linestring1, linestring2])
119-
# multilinestringmeta = MultiLineStringMeta([linestring1, linestring2]; boundingbox = Rect(1.0, 1.0, 2.0, 2.0))
120-
# @test multilinestringmeta isa AbstractVector
121-
# @test meta(multilinestringmeta) === (boundingbox = Rect(1.0, 1.0, 2.0, 2.0),)
122-
# @test metafree(multilinestringmeta) == multilinestring
123-
# @test propertynames(multilinestringmeta) == (:linestrings, :boundingbox)
124-
# end
97+
@testset "MultiPoint with metadata" begin
98+
p = collect(Point{2, Float64}(x, x+1) for x in 1:5)
99+
@test p isa AbstractVector
100+
mpm = MultiPointMeta(p, a=1, b=2)
101+
@test coordinates(mpm) == mpm
102+
@test meta(mpm) === (a=1, b=2)
103+
@test metafree(mpm) == p
104+
@test propertynames(mpm) == (:points, :a, :b)
105+
end
125106
126-
# @testset "Mesh with metadata" begin
127-
# m = triangle_mesh(Sphere(Point3f0(0), 1))
128-
# m_meta = MeshMeta(m; boundingbox=Rect(1.0, 1.0, 2.0, 2.0))
129-
# @test meta(m_meta) === (boundingbox = Rect(1.0, 1.0, 2.0, 2.0),)
130-
# @test metafree(m_meta) === m
131-
# @test propertynames(m_meta) == (:mesh, :boundingbox)
132-
# end
107+
@testset "LineString with metadata" begin
108+
linestring = LineStringMeta(Point{2, Int}[(10, 10), (20, 20), (10, 40)], a = 1, b = 2)
109+
@test linestring isa AbstractVector
110+
@test meta(linestring) === (a = 1, b = 2)
111+
@test metafree(linestring) == linestring
112+
@test propertynames(linestring) == (:lines, :a, :b)
113+
end
114+
115+
@testset "MultiLineString with metadata" begin
116+
linestring1 = LineString(Point{2, Int}[(10, 10), (20, 20), (10, 40)])
117+
linestring2 = LineString(Point{2, Int}[(40, 40), (30, 30), (40, 20), (30, 10)])
118+
multilinestring = MultiLineString([linestring1, linestring2])
119+
multilinestringmeta = MultiLineStringMeta([linestring1, linestring2]; boundingbox = Rect(1.0, 1.0, 2.0, 2.0))
120+
@test multilinestringmeta isa AbstractVector
121+
@test meta(multilinestringmeta) === (boundingbox = Rect(1.0, 1.0, 2.0, 2.0),)
122+
@test metafree(multilinestringmeta) == multilinestring
123+
@test propertynames(multilinestringmeta) == (:linestrings, :boundingbox)
124+
end
125+
126+
@testset "Mesh with metadata" begin
127+
m = triangle_mesh(Sphere(Point3f0(0), 1))
128+
m_meta = MeshMeta(m; boundingbox=Rect(1.0, 1.0, 2.0, 2.0))
129+
@test meta(m_meta) === (boundingbox = Rect(1.0, 1.0, 2.0, 2.0),)
130+
@test metafree(m_meta) === m
131+
@test propertynames(m_meta) == (:mesh, :boundingbox)
132+
end
133133
# end
134134
=#
135+
135136
@testset "embedding metadata(new)" begin
136137
# @testset "Meshes" begin
137138

0 commit comments

Comments
 (0)