|
1 | 1 | using GeometryBasics
|
2 |
| -using GeometryBasics: Polygon, MultiPolygon, Point, LineFace, Polytope, Line, Simplex, connect, Triangle, NSimplex, Tetrahedron, TupleView, TriangleFace, SimplexFace, LineString, Mesh |
3 |
| -using Test, Random |
4 |
| - |
5 |
| - |
6 |
| -@testset "polygon with metadata" begin |
7 |
| - polys = [Polygon(rand(Point{2, Float32}, 20)) for i in 1:10] |
8 |
| - pnames = [randstring(4) for i in 1:10] |
9 |
| - numbers = rand(10) |
10 |
| - bin = rand(Bool, 10) |
11 |
| - x = MultiPolygon(polys, name = pnames, value = numbers, category = bin) |
12 |
| - for (mp, p, n, num, b) in zip(x, polys, pnames, numbers, bin) |
13 |
| - @test mp.polygon == p |
14 |
| - @test mp.name == n |
15 |
| - @test mp.value == num |
16 |
| - @test mp.category == b |
| 2 | +using GeometryBasics: Polygon, MultiPolygon, Point, LineFace, Polytope, Line |
| 3 | +using GeometryBasics: Simplex, connect, Triangle, NSimplex, Tetrahedron |
| 4 | +using GeometryBasics: QuadFace, hascolumn, getcolumn, metafree, coordinates, TetrahedronFace |
| 5 | +using GeometryBasics: TupleView, TriangleFace, SimplexFace, LineString, Mesh, meta |
| 6 | +using Test, Random, Query, StructArrays, Tables |
| 7 | +using StaticArrays |
| 8 | + |
| 9 | + |
| 10 | +@testset "embedding metadata" begin |
| 11 | + @testset "Meshes" begin |
| 12 | + |
| 13 | + @testset "per vertex attributes" begin |
| 14 | + points = rand(Point{3, Float64}, 8) |
| 15 | + tfaces = TetrahedronFace{Int}[(1, 2, 3, 4), (5, 6, 7, 8)] |
| 16 | + normals = rand(SVector{3, Float64}, 8) |
| 17 | + stress = LinRange(0, 1, 8) |
| 18 | + mesh = Mesh(meta(points, normals = normals, stress = stress), tfaces) |
| 19 | + |
| 20 | + @test hascolumn(coordinates(mesh), :stress) |
| 21 | + @test hascolumn(coordinates(mesh), :normals) |
| 22 | + @test coordinates(mesh).stress === stress |
| 23 | + @test coordinates(mesh).normals === normals |
| 24 | + @test coordinates(mesh).normals === normals |
| 25 | + @test GeometryBasics.faces(mesh) === tfaces |
| 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 hascolumn(GeometryBasics.faces(mesh), :markers) |
| 52 | + # test with === to assert we're not doing any copies |
| 53 | + @test getcolumn(GeometryBasics.faces(mesh), :markers) === markers |
| 54 | + @test coordinates(mesh) === points |
| 55 | + @test metafree(GeometryBasics.faces(mesh)) === facets |
| 56 | + |
| 57 | + end |
| 58 | + |
| 59 | + end |
| 60 | + @testset "polygon with metadata" begin |
| 61 | + polys = [Polygon(rand(Point{2, Float32}, 20)) for i in 1:10] |
| 62 | + pnames = [randstring(4) for i in 1:10] |
| 63 | + numbers = LinRange(0.0, 1.0, 10) |
| 64 | + bin = rand(Bool, 10) |
| 65 | + # create just an array |
| 66 | + plain = meta(polys, name = pnames, value = numbers, category = bin) |
| 67 | + # create a MultiPolygon with the right type & meta information! |
| 68 | + multipoly = MultiPolygon(polys; name = pnames, value = numbers, category = bin) |
| 69 | + for x in (plain, multipoly) |
| 70 | + for (mp, p, n, num, b) in zip(x, polys, pnames, numbers, bin) |
| 71 | + @test mp.polygon == p |
| 72 | + @test mp.name == n |
| 73 | + @test mp.value == num |
| 74 | + @test mp.category == b |
| 75 | + end |
| 76 | + |
| 77 | + filtered = @from i in x begin |
| 78 | + @where i.value < 0.7 |
| 79 | + @select i |
| 80 | + @collect |
| 81 | + end |
| 82 | + @test length(filtered) == 7 |
| 83 | + end |
17 | 84 | end
|
18 | 85 | end
|
19 | 86 |
|
20 |
| - |
21 | 87 | @testset "view" begin
|
22 | 88 | @testset "TupleView" begin
|
23 | 89 | x = [1, 2, 3, 4, 5, 6]
|
|
48 | 114 |
|
49 | 115 | triangles = connect(x, Triangle)
|
50 | 116 | @test triangles == [Triangle(Point(1, 2), Point(3, 4), Point(5, 6))]
|
51 |
| - tetrahedra = connect(x, NSimplex{3}) |
52 |
| - @test tetrahedra == [Tetrahedron(x[1], x[2], x[3])] |
| 117 | + x = connect([1, 2, 3, 4, 5, 6, 7, 8], Point{2}) |
| 118 | + tetrahedra = connect(x, NSimplex{4}) |
| 119 | + @test tetrahedra == [Tetrahedron(x[1], x[2], x[3], x[4])] |
| 120 | + |
| 121 | + @testset "matrix non-copy point views" begin |
| 122 | + # point in row |
| 123 | + points = [1 2; 1 4; 66 77] |
| 124 | + comparison = [Point(1, 2), Point(1, 4), Point(66, 77)] |
| 125 | + @test connect(points, Point{2}) == comparison |
| 126 | + # point in column |
| 127 | + points = [1 1 66; 2 4 77] |
| 128 | + # huh, reinterpret array doesn't seem to like `==` |
| 129 | + @test all(((a,b),)-> a==b, zip(connect(points, Point{2}), comparison)) |
| 130 | + end |
53 | 131 | end
|
54 | 132 |
|
55 | 133 | @testset "face views" begin
|
|
61 | 139 | x = Point{3}(1.0)
|
62 | 140 | triangles = connect([x], [TriangleFace(1, 1, 1)])
|
63 | 141 | @test triangles == [Triangle(x, x, x)]
|
64 |
| - |
65 |
| - faces = connect([1, 2, 3], SimplexFace{3}) |
| 142 | + points = connect([1, 2, 3, 4, 5, 6, 7, 8], Point{2}) |
| 143 | + faces = connect([1, 2, 3, 4], SimplexFace{4}) |
66 | 144 | triangles = connect(points, faces)
|
67 | 145 | @test triangles == [Tetrahedron(points...)]
|
68 | 146 | end
|
|
133 | 211 | mesh = Mesh([x], [TriangleFace(1, 1, 1)])
|
134 | 212 | @test mesh == [Triangle(x, x, x)]
|
135 | 213 |
|
136 |
| - faces = connect([1, 2, 3], SimplexFace{3}) |
| 214 | + points = connect([1, 2, 3, 4, 5, 6, 7, 8], Point{2}) |
| 215 | + faces = connect([1, 2, 3, 4], SimplexFace{4}) |
137 | 216 | mesh = Mesh(points, faces)
|
138 | 217 | @test mesh == [Tetrahedron(points...)]
|
139 | 218 |
|
|
0 commit comments