|
| 1 | +# GeometryBasics.jl |
| 2 | + |
| 3 | +Basic Geometry Types. |
| 4 | +This package aimes to offer a standard set of Geometry types, which easily work with metadata, query frameworks on geometries and different memory layouts. |
| 5 | +To get an idea, look at the runtests: |
| 6 | + |
| 7 | +```julia |
| 8 | +using GeometryBasics |
| 9 | +using GeometryBasics: Polygon, MultiPolygon, Point, LineFace, Polytope, Line |
| 10 | +using GeometryBasics: Simplex, connect, Triangle, NSimplex, Tetrahedron |
| 11 | +using GeometryBasics: QuadFace, hascolumn, getcolumn, metafree, coordinates, TetrahedronFace |
| 12 | +using GeometryBasics: TupleView, TriangleFace, SimplexFace, LineString, Mesh, meta |
| 13 | +using Test, Random, Query, StructArrays, Tables |
| 14 | +using StaticArrays |
| 15 | + |
| 16 | + |
| 17 | +@testset "embedding metadata" begin |
| 18 | + @testset "Meshes" begin |
| 19 | + |
| 20 | + @testset "per vertex attributes" begin |
| 21 | + points = rand(Point{3, Float64}, 8) |
| 22 | + tfaces = TetrahedronFace{Int}[(1, 2, 3, 4), (5, 6, 7, 8)] |
| 23 | + normals = rand(SVector{3, Float64}, 8) |
| 24 | + stress = LinRange(0, 1, 8) |
| 25 | + mesh = Mesh(meta(points, normals = normals, stress = stress), tfaces) |
| 26 | + |
| 27 | + @test hascolumn(coordinates(mesh), :stress) |
| 28 | + @test hascolumn(coordinates(mesh), :normals) |
| 29 | + @test coordinates(mesh).stress === stress |
| 30 | + @test coordinates(mesh).normals === normals |
| 31 | + @test coordinates(mesh).normals === normals |
| 32 | + @test GeometryBasics.faces(mesh) === tfaces |
| 33 | + |
| 34 | + end |
| 35 | + |
| 36 | + @testset "per face attributes" begin |
| 37 | + |
| 38 | + # Construct a cube out of Quads |
| 39 | + points = Point{3, Float64}[ |
| 40 | + (0.0, 0.0, 0.0), (2.0, 0.0, 0.0), |
| 41 | + (2.0, 2.0, 0.0), (0.0, 2.0, 0.0), |
| 42 | + (0.0, 0.0, 12.0), (2.0, 0.0, 12.0), |
| 43 | + (2.0, 2.0, 12.0), (0.0, 2.0, 12.0) |
| 44 | + ] |
| 45 | + |
| 46 | + facets = QuadFace{Cint}[ |
| 47 | + 1:4, |
| 48 | + 5:8, |
| 49 | + [1,5,6,2], |
| 50 | + [2,6,7,3], |
| 51 | + [3, 7, 8, 4], |
| 52 | + [4, 8, 5, 1] |
| 53 | + ] |
| 54 | + |
| 55 | + markers = Cint[-1, -2, 0, 0, 0, 0] |
| 56 | + # attach some additional information to our faces! |
| 57 | + mesh = Mesh(points, meta(facets, markers = markers)) |
| 58 | + @test hascolumn(GeometryBasics.faces(mesh), :markers) |
| 59 | + # test with === to assert we're not doing any copies |
| 60 | + @test getcolumn(GeometryBasics.faces(mesh), :markers) === markers |
| 61 | + @test coordinates(mesh) === points |
| 62 | + @test metafree(GeometryBasics.faces(mesh)) === facets |
| 63 | + |
| 64 | + end |
| 65 | + |
| 66 | + end |
| 67 | + @testset "polygon with metadata" begin |
| 68 | + polys = [Polygon(rand(Point{2, Float32}, 20)) for i in 1:10] |
| 69 | + pnames = [randstring(4) for i in 1:10] |
| 70 | + numbers = LinRange(0.0, 1.0, 10) |
| 71 | + bin = rand(Bool, 10) |
| 72 | + # create just an array |
| 73 | + plain = meta(polys, name = pnames, value = numbers, category = bin) |
| 74 | + # create a MultiPolygon with the right type & meta information! |
| 75 | + multipoly = MultiPolygon(polys; name = pnames, value = numbers, category = bin) |
| 76 | + for x in (plain, multipoly) |
| 77 | + for (mp, p, n, num, b) in zip(x, polys, pnames, numbers, bin) |
| 78 | + @test mp.polygon == p |
| 79 | + @test mp.name == n |
| 80 | + @test mp.value == num |
| 81 | + @test mp.category == b |
| 82 | + end |
| 83 | + |
| 84 | + filtered = @from i in x begin |
| 85 | + @where i.value < 0.7 |
| 86 | + @select i |
| 87 | + @collect |
| 88 | + end |
| 89 | + @test length(filtered) == 7 |
| 90 | + end |
| 91 | + end |
| 92 | +end |
| 93 | + |
| 94 | +@testset "view" begin |
| 95 | + @testset "TupleView" begin |
| 96 | + x = [1, 2, 3, 4, 5, 6] |
| 97 | + y = TupleView{2, 1}(x) |
| 98 | + @test y == [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)] |
| 99 | + |
| 100 | + y = TupleView{2}(x) |
| 101 | + @test y == [(1, 2), (3, 4), (5, 6)] |
| 102 | + |
| 103 | + y = TupleView{2, 3}(x) |
| 104 | + @test y == [(1, 2), (4, 5)] |
| 105 | + |
| 106 | + y = TupleView{3, 1}(x) |
| 107 | + @test y == [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6)] |
| 108 | + |
| 109 | + y = TupleView{2, 1}(x, connect = true) |
| 110 | + @test y == [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 1)] |
| 111 | + |
| 112 | + end |
| 113 | + @testset "connected views" begin |
| 114 | + numbers = [1, 2, 3, 4, 5, 6] |
| 115 | + x = connect(numbers, Point{2}) |
| 116 | + |
| 117 | + @test x == Point[(1, 2), (3, 4), (5, 6)] |
| 118 | + |
| 119 | + line = connect(x, Line, 1) |
| 120 | + @test line == [Line(Point(1, 2), Point(3, 4)), Line(Point(3, 4), Point(5, 6))] |
| 121 | + |
| 122 | + triangles = connect(x, Triangle) |
| 123 | + @test triangles == [Triangle(Point(1, 2), Point(3, 4), Point(5, 6))] |
| 124 | + x = connect([1, 2, 3, 4, 5, 6, 7, 8], Point{2}) |
| 125 | + tetrahedra = connect(x, NSimplex{4}) |
| 126 | + @test tetrahedra == [Tetrahedron(x[1], x[2], x[3], x[4])] |
| 127 | + |
| 128 | + @testset "matrix non-copy point views" begin |
| 129 | + # point in row |
| 130 | + points = [1 2; 1 4; 66 77] |
| 131 | + comparison = [Point(1, 2), Point(1, 4), Point(66, 77)] |
| 132 | + @test connect(points, Point{2}) == comparison |
| 133 | + # point in column |
| 134 | + points = [1 1 66; 2 4 77] |
| 135 | + # huh, reinterpret array doesn't seem to like `==` |
| 136 | + @test all(((a,b),)-> a==b, zip(connect(points, Point{2}), comparison)) |
| 137 | + end |
| 138 | + end |
| 139 | + |
| 140 | + @testset "face views" begin |
| 141 | + numbers = [1, 2, 3, 4, 5, 6] |
| 142 | + points = connect(numbers, Point{2}) |
| 143 | + faces = connect([1, 2, 3], TriangleFace) |
| 144 | + triangles = connect(points, faces) |
| 145 | + @test triangles == [Triangle(Point(1, 2), Point(3, 4), Point(5, 6))] |
| 146 | + x = Point{3}(1.0) |
| 147 | + triangles = connect([x], [TriangleFace(1, 1, 1)]) |
| 148 | + @test triangles == [Triangle(x, x, x)] |
| 149 | + points = connect([1, 2, 3, 4, 5, 6, 7, 8], Point{2}) |
| 150 | + faces = connect([1, 2, 3, 4], SimplexFace{4}) |
| 151 | + triangles = connect(points, faces) |
| 152 | + @test triangles == [Tetrahedron(points...)] |
| 153 | + end |
| 154 | + |
| 155 | +end |
| 156 | + |
| 157 | + |
| 158 | +@testset "constructors" begin |
| 159 | + @testset "LineFace" begin |
| 160 | + |
| 161 | + points = connect([1, 2, 3, 4, 5, 6], Point{2}) |
| 162 | + linestring = LineString(points) |
| 163 | + @test linestring == [Line(points[1], points[2]), Line(points[2], points[3])] |
| 164 | + |
| 165 | + points = rand(Point{2, Float64}, 4) |
| 166 | + linestring = LineString(points, 2) |
| 167 | + @test linestring == [Line(points[1], points[2]), Line(points[3], points[4])] |
| 168 | + |
| 169 | + linestring = LineString([points[1] => points[2], points[2] => points[3]]) |
| 170 | + @test linestring == [Line(points[1], points[2]), Line(points[2], points[3])] |
| 171 | + |
| 172 | + faces = [1, 2, 3] |
| 173 | + linestring = LineString(points, faces) |
| 174 | + @test linestring == LineString([points[1] => points[2], points[2] => points[3]]) |
| 175 | + a, b, c, d = Point(1, 2), Point(3, 4), Point(5, 6), Point(7, 8) |
| 176 | + points = [a, b, c, d]; faces = [1, 2, 3, 4] |
| 177 | + linestring = LineString(points, faces, 2) |
| 178 | + @test linestring == LineString([a => b, c => d]) |
| 179 | + |
| 180 | + faces = [LineFace(1, 2), LineFace(3, 4)] |
| 181 | + linestring = LineString(points, faces) |
| 182 | + @test linestring == LineString([a => b, c => d]) |
| 183 | + end |
| 184 | + |
| 185 | + @testset "Polygon" begin |
| 186 | + |
| 187 | + points = connect([1, 2, 3, 4, 5, 6], Point{2}) |
| 188 | + polygon = Polygon(points) |
| 189 | + @test polygon == Polygon(LineString(points)) |
| 190 | + |
| 191 | + points = rand(Point{2, Float64}, 4) |
| 192 | + linestring = LineString(points, 2) |
| 193 | + @test Polygon(points, 2) == Polygon(linestring) |
| 194 | + |
| 195 | + faces = [1, 2, 3] |
| 196 | + polygon = Polygon(points, faces) |
| 197 | + @test polygon == Polygon(LineString(points, faces)) |
| 198 | + |
| 199 | + a, b, c, d = Point(1, 2), Point(3, 4), Point(5, 6), Point(7, 8) |
| 200 | + points = [a, b, c, d]; faces = [1, 2, 3, 4] |
| 201 | + polygon = Polygon(points, faces, 2) |
| 202 | + @test polygon == Polygon(LineString(points, faces, 2)) |
| 203 | + |
| 204 | + faces = [LineFace(1, 2), LineFace(3, 4)] |
| 205 | + polygon = Polygon(points, faces) |
| 206 | + @test polygon == Polygon(LineString(points, faces)) |
| 207 | + end |
| 208 | + |
| 209 | + @testset "Mesh" begin |
| 210 | + |
| 211 | + numbers = [1, 2, 3, 4, 5, 6] |
| 212 | + points = connect(numbers, Point{2}) |
| 213 | + |
| 214 | + mesh = Mesh(points, [1,2,3]) |
| 215 | + @test mesh == [Triangle(points...)] |
| 216 | + |
| 217 | + x = Point{3}(1.0) |
| 218 | + mesh = Mesh([x], [TriangleFace(1, 1, 1)]) |
| 219 | + @test mesh == [Triangle(x, x, x)] |
| 220 | + |
| 221 | + points = connect([1, 2, 3, 4, 5, 6, 7, 8], Point{2}) |
| 222 | + faces = connect([1, 2, 3, 4], SimplexFace{4}) |
| 223 | + mesh = Mesh(points, faces) |
| 224 | + @test mesh == [Tetrahedron(points...)] |
| 225 | + |
| 226 | + end |
| 227 | + |
| 228 | +end |
| 229 | +``` |
| 230 | + |
| 231 | + |
0 commit comments