|
1 | 1 |
|
2 |
| -[](https://travis-ci.com/SimonDanisch/GeometryBasics.jl) |
3 |
| -[](https://ci.appveyor.com/project/SimonDanisch/GeometryBasics-jl) |
4 |
| -[](https://codecov.io/gh/SimonDanisch/GeometryBasics.jl) |
| 2 | +[](https://travis-ci.com/JuliaGeometry/GeometryBasics.jl) |
| 3 | +[](https://codecov.io/gh/JuliaGeometry/GeometryBasics.jl) |
5 | 4 |
|
6 |
| -**Documentation**: [![][docs-stable-img]][docs-stable-url] [![][docs-master-img]][docs-master-url] |
7 |
| - |
8 |
| -[docs-stable-img]: https://img.shields.io/badge/docs-stable-lightgrey.svg |
9 |
| -[docs-stable-url]: http://juliageometry.github.io/GeometryBasics.jl/stable/ |
10 |
| -[docs-master-img]: https://img.shields.io/badge/docs-dev-blue.svg |
11 |
| -[docs-master-url]: http://juliageometry.github.io/GeometryBasics.jl |
| 5 | +**Documentation**: |
| 6 | +[](http://juliageometry.github.io/GeometryBasics.jl/stable/) |
| 7 | +[](http://juliageometry.github.io/GeometryBasics.jl/dev) |
12 | 8 |
|
13 | 9 | # GeometryBasics.jl
|
14 | 10 |
|
15 | 11 | Basic Geometry Types.
|
16 | 12 | This package aimes to offer a standard set of Geometry types, which easily work with metadata, query frameworks on geometries and different memory layouts.
|
17 | 13 | The aim is to create a solid basis for Graphics/Plotting, finite elements analysis, Geo applications, and general geometry manipulations - while offering a julian API, that still allows performant C-interop.
|
18 | 14 |
|
| 15 | +This package is a replacement for the discontinued [GeometryTypes](https://github.com/JuliaGeometry/GeometryTypes.jl/). |
19 | 16 |
|
20 |
| -It is planned to merge this package with [GeometryTypes](https://github.com/JuliaGeometry/GeometryTypes.jl/) once it's fully mature, or simply replace it! |
21 |
| - |
22 |
| -To get an idea, look at the runtests: |
| 17 | +## Quick start |
23 | 18 |
|
24 | 19 | ```julia
|
25 | 20 | using GeometryBasics
|
26 |
| -using GeometryBasics: Polygon, MultiPolygon, Point, LineFace, Polytope, Line |
27 |
| -using GeometryBasics: Simplex, connect, Triangle, NSimplex, Tetrahedron |
28 |
| -using GeometryBasics: QuadFace, hascolumn, getcolumn, metafree, coordinates, TetrahedronFace |
29 |
| -using GeometryBasics: TupleView, TriangleFace, SimplexFace, LineString, Mesh, meta |
30 |
| -using Test, Random, Query, StructArrays, Tables |
31 |
| -using StaticArrays |
32 |
| - |
33 |
| - |
34 |
| -@testset "embedding metadata" begin |
35 |
| - @testset "Meshes" begin |
36 |
| - |
37 |
| - @testset "per vertex attributes" begin |
38 |
| - points = rand(Point{3, Float64}, 8) |
39 |
| - tfaces = TetrahedronFace{Int}[(1, 2, 3, 4), (5, 6, 7, 8)] |
40 |
| - normals = rand(SVector{3, Float64}, 8) |
41 |
| - stress = LinRange(0, 1, 8) |
42 |
| - mesh = Mesh(meta(points, normals = normals, stress = stress), tfaces) |
43 |
| - |
44 |
| - @test hascolumn(coordinates(mesh), :stress) |
45 |
| - @test hascolumn(coordinates(mesh), :normals) |
46 |
| - @test coordinates(mesh).stress === stress |
47 |
| - @test coordinates(mesh).normals === normals |
48 |
| - @test coordinates(mesh).normals === normals |
49 |
| - @test GeometryBasics.faces(mesh) === tfaces |
50 |
| - |
51 |
| - end |
52 |
| - |
53 |
| - @testset "per face attributes" begin |
54 |
| - |
55 |
| - # Construct a cube out of Quads |
56 |
| - points = Point{3, Float64}[ |
57 |
| - (0.0, 0.0, 0.0), (2.0, 0.0, 0.0), |
58 |
| - (2.0, 2.0, 0.0), (0.0, 2.0, 0.0), |
59 |
| - (0.0, 0.0, 12.0), (2.0, 0.0, 12.0), |
60 |
| - (2.0, 2.0, 12.0), (0.0, 2.0, 12.0) |
61 |
| - ] |
62 |
| - |
63 |
| - facets = QuadFace{Cint}[ |
64 |
| - 1:4, |
65 |
| - 5:8, |
66 |
| - [1,5,6,2], |
67 |
| - [2,6,7,3], |
68 |
| - [3, 7, 8, 4], |
69 |
| - [4, 8, 5, 1] |
70 |
| - ] |
71 |
| - |
72 |
| - markers = Cint[-1, -2, 0, 0, 0, 0] |
73 |
| - # attach some additional information to our faces! |
74 |
| - mesh = Mesh(points, meta(facets, markers = markers)) |
75 |
| - @test hascolumn(GeometryBasics.faces(mesh), :markers) |
76 |
| - # test with === to assert we're not doing any copies |
77 |
| - @test getcolumn(GeometryBasics.faces(mesh), :markers) === markers |
78 |
| - @test coordinates(mesh) === points |
79 |
| - @test metafree(GeometryBasics.faces(mesh)) === facets |
80 |
| - |
81 |
| - end |
82 |
| - |
83 |
| - end |
84 |
| - @testset "polygon with metadata" begin |
85 |
| - polys = [Polygon(rand(Point{2, Float32}, 20)) for i in 1:10] |
86 |
| - pnames = [randstring(4) for i in 1:10] |
87 |
| - numbers = LinRange(0.0, 1.0, 10) |
88 |
| - bin = rand(Bool, 10) |
89 |
| - # create just an array |
90 |
| - plain = meta(polys, name = pnames, value = numbers, category = bin) |
91 |
| - # create a MultiPolygon with the right type & meta information! |
92 |
| - multipoly = MultiPolygon(polys; name = pnames, value = numbers, category = bin) |
93 |
| - for x in (plain, multipoly) |
94 |
| - for (mp, p, n, num, b) in zip(x, polys, pnames, numbers, bin) |
95 |
| - @test mp.polygon == p |
96 |
| - @test mp.name == n |
97 |
| - @test mp.value == num |
98 |
| - @test mp.category == b |
99 |
| - end |
100 |
| - |
101 |
| - filtered = @from i in x begin |
102 |
| - @where i.value < 0.7 |
103 |
| - @select i |
104 |
| - @collect |
105 |
| - end |
106 |
| - @test length(filtered) == 7 |
107 |
| - end |
108 |
| - end |
109 |
| -end |
110 |
| - |
111 |
| -@testset "view" begin |
112 |
| - @testset "TupleView" begin |
113 |
| - x = [1, 2, 3, 4, 5, 6] |
114 |
| - y = TupleView{2, 1}(x) |
115 |
| - @test y == [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)] |
116 |
| - |
117 |
| - y = TupleView{2}(x) |
118 |
| - @test y == [(1, 2), (3, 4), (5, 6)] |
119 |
| - |
120 |
| - y = TupleView{2, 3}(x) |
121 |
| - @test y == [(1, 2), (4, 5)] |
122 |
| - |
123 |
| - y = TupleView{3, 1}(x) |
124 |
| - @test y == [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6)] |
125 |
| - |
126 |
| - y = TupleView{2, 1}(x, connect = true) |
127 |
| - @test y == [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 1)] |
128 |
| - |
129 |
| - end |
130 |
| - @testset "connected views" begin |
131 |
| - numbers = [1, 2, 3, 4, 5, 6] |
132 |
| - x = connect(numbers, Point{2}) |
133 |
| - |
134 |
| - @test x == Point[(1, 2), (3, 4), (5, 6)] |
135 |
| - |
136 |
| - line = connect(x, Line, 1) |
137 |
| - @test line == [Line(Point(1, 2), Point(3, 4)), Line(Point(3, 4), Point(5, 6))] |
138 |
| - |
139 |
| - triangles = connect(x, Triangle) |
140 |
| - @test triangles == [Triangle(Point(1, 2), Point(3, 4), Point(5, 6))] |
141 |
| - x = connect([1, 2, 3, 4, 5, 6, 7, 8], Point{2}) |
142 |
| - tetrahedra = connect(x, NSimplex{4}) |
143 |
| - @test tetrahedra == [Tetrahedron(x[1], x[2], x[3], x[4])] |
144 |
| - |
145 |
| - @testset "matrix non-copy point views" begin |
146 |
| - # point in row |
147 |
| - points = [1 2; 1 4; 66 77] |
148 |
| - comparison = [Point(1, 2), Point(1, 4), Point(66, 77)] |
149 |
| - @test connect(points, Point{2}) == comparison |
150 |
| - # point in column |
151 |
| - points = [1 1 66; 2 4 77] |
152 |
| - # huh, reinterpret array doesn't seem to like `==` |
153 |
| - @test all(((a,b),)-> a==b, zip(connect(points, Point{2}), comparison)) |
154 |
| - end |
155 |
| - end |
156 |
| - |
157 |
| - @testset "face views" begin |
158 |
| - numbers = [1, 2, 3, 4, 5, 6] |
159 |
| - points = connect(numbers, Point{2}) |
160 |
| - faces = connect([1, 2, 3], TriangleFace) |
161 |
| - triangles = connect(points, faces) |
162 |
| - @test triangles == [Triangle(Point(1, 2), Point(3, 4), Point(5, 6))] |
163 |
| - x = Point{3}(1.0) |
164 |
| - triangles = connect([x], [TriangleFace(1, 1, 1)]) |
165 |
| - @test triangles == [Triangle(x, x, x)] |
166 |
| - points = connect([1, 2, 3, 4, 5, 6, 7, 8], Point{2}) |
167 |
| - faces = connect([1, 2, 3, 4], SimplexFace{4}) |
168 |
| - triangles = connect(points, faces) |
169 |
| - @test triangles == [Tetrahedron(points...)] |
170 |
| - end |
171 |
| - |
172 |
| -end |
173 |
| - |
174 |
| - |
175 |
| -@testset "constructors" begin |
176 |
| - @testset "LineFace" begin |
177 |
| - |
178 |
| - points = connect([1, 2, 3, 4, 5, 6], Point{2}) |
179 |
| - linestring = LineString(points) |
180 |
| - @test linestring == [Line(points[1], points[2]), Line(points[2], points[3])] |
181 |
| - |
182 |
| - points = rand(Point{2, Float64}, 4) |
183 |
| - linestring = LineString(points, 2) |
184 |
| - @test linestring == [Line(points[1], points[2]), Line(points[3], points[4])] |
185 |
| - |
186 |
| - linestring = LineString([points[1] => points[2], points[2] => points[3]]) |
187 |
| - @test linestring == [Line(points[1], points[2]), Line(points[2], points[3])] |
188 |
| - |
189 |
| - faces = [1, 2, 3] |
190 |
| - linestring = LineString(points, faces) |
191 |
| - @test linestring == LineString([points[1] => points[2], points[2] => points[3]]) |
192 |
| - a, b, c, d = Point(1, 2), Point(3, 4), Point(5, 6), Point(7, 8) |
193 |
| - points = [a, b, c, d]; faces = [1, 2, 3, 4] |
194 |
| - linestring = LineString(points, faces, 2) |
195 |
| - @test linestring == LineString([a => b, c => d]) |
196 |
| - |
197 |
| - faces = [LineFace(1, 2), LineFace(3, 4)] |
198 |
| - linestring = LineString(points, faces) |
199 |
| - @test linestring == LineString([a => b, c => d]) |
200 |
| - end |
201 |
| - |
202 |
| - @testset "Polygon" begin |
203 |
| - |
204 |
| - points = connect([1, 2, 3, 4, 5, 6], Point{2}) |
205 |
| - polygon = Polygon(points) |
206 |
| - @test polygon == Polygon(LineString(points)) |
207 |
| - |
208 |
| - points = rand(Point{2, Float64}, 4) |
209 |
| - linestring = LineString(points, 2) |
210 |
| - @test Polygon(points, 2) == Polygon(linestring) |
211 |
| - |
212 |
| - faces = [1, 2, 3] |
213 |
| - polygon = Polygon(points, faces) |
214 |
| - @test polygon == Polygon(LineString(points, faces)) |
215 |
| - |
216 |
| - a, b, c, d = Point(1, 2), Point(3, 4), Point(5, 6), Point(7, 8) |
217 |
| - points = [a, b, c, d]; faces = [1, 2, 3, 4] |
218 |
| - polygon = Polygon(points, faces, 2) |
219 |
| - @test polygon == Polygon(LineString(points, faces, 2)) |
220 |
| - |
221 |
| - faces = [LineFace(1, 2), LineFace(3, 4)] |
222 |
| - polygon = Polygon(points, faces) |
223 |
| - @test polygon == Polygon(LineString(points, faces)) |
224 |
| - end |
225 |
| - |
226 |
| - @testset "Mesh" begin |
227 |
| - |
228 |
| - numbers = [1, 2, 3, 4, 5, 6] |
229 |
| - points = connect(numbers, Point{2}) |
230 |
| - |
231 |
| - mesh = Mesh(points, [1,2,3]) |
232 |
| - @test mesh == [Triangle(points...)] |
233 |
| - |
234 |
| - x = Point{3}(1.0) |
235 |
| - mesh = Mesh([x], [TriangleFace(1, 1, 1)]) |
236 |
| - @test mesh == [Triangle(x, x, x)] |
237 |
| - |
238 |
| - points = connect([1, 2, 3, 4, 5, 6, 7, 8], Point{2}) |
239 |
| - faces = connect([1, 2, 3, 4], SimplexFace{4}) |
240 |
| - mesh = Mesh(points, faces) |
241 |
| - @test mesh == [Tetrahedron(points...)] |
242 |
| - |
243 |
| - end |
244 |
| - |
245 |
| -end |
| 21 | + |
| 22 | +# create some points |
| 23 | +julia> p1 = Point(3, 1) |
| 24 | +2-element Point{2,Int64} with indices SOneTo(2): |
| 25 | + 3 |
| 26 | + 1 |
| 27 | + |
| 28 | +julia> p2 = Point(1, 3); |
| 29 | + |
| 30 | +julia> p3 = Point(4, 4); |
| 31 | + |
| 32 | +# geometries can carry metadata |
| 33 | +julia> poi = meta(p1, city="Abuja", rainfall=1221.2) |
| 34 | +2-element PointMeta{2,Int64,Point{2,Int64},(:city, :rainfall),Tuple{String,Float64}} with indices SOneTo(2): |
| 35 | + 3 |
| 36 | + 1 |
| 37 | + |
| 38 | +# metadata is stored in a NamedTuple and can be retrieved as such |
| 39 | +julia> meta(poi) |
| 40 | +(city = "Abuja", rainfall = 1221.2) |
| 41 | + |
| 42 | +# specific metadata attributes can be directly retrieved |
| 43 | +julia> poi.rainfall |
| 44 | +1221.2 |
| 45 | + |
| 46 | +# to remove the metadata and keep only the geometry, use metafree |
| 47 | +julia> metafree(poi) |
| 48 | +2-element Point{2,Int64} with indices SOneTo(2): |
| 49 | + 3 |
| 50 | + 1 |
| 51 | + |
| 52 | +# connect the points with lines |
| 53 | +julia> l1 = Line(p1, p2) |
| 54 | +Line([3, 1] => [1, 3]) |
| 55 | + |
| 56 | +julia> l2 = Line(p2, p3); |
| 57 | + |
| 58 | +# connect the lines in a linestring |
| 59 | +julia> LineString([l1, l2]) |
| 60 | +2-element LineString{2,Int64,Point{2,Int64},Array{GeometryBasics.Ngon{2,Int64,2,Point{2,Int64}},1}}: |
| 61 | + Line([3, 1] => [1, 3]) |
| 62 | + Line([1, 3] => [4, 4]) |
| 63 | + |
| 64 | +# linestrings can also be constructed directly from points |
| 65 | +julia> LineString([p1, p2, p3]) |
| 66 | +2-element LineString{2,Int64,Point{2,Int64},Base.ReinterpretArray{GeometryBasics.Ngon{2,Int64,2,Point{2,Int64}},1,Tuple{Point{2,Int64},Point{2,Int64}},TupleView{Tuple{Point{2,Int64},Point{2,Int64}}, 1}}}: |
| 67 | + Line([3, 1] => [1, 3]) |
| 68 | + Line([1, 3] => [4, 4]) |
| 69 | + |
| 70 | +# the same goes for polygons |
| 71 | +julia> Polygon(Point{2, Int}[(3, 1), (4, 4), (2, 4), (1, 2), (3, 1)]) |
| 72 | +Polygon{2,Int64,Point{2,Int64},LineString{2,Int64,Point{2,Int64},Base.ReinterpretArray{GeometryBasics.Ngon{2,Int64,2,Point{2,Int64}},1,Tuple{Point{2,Int64},Point{2,Int64}},TupleView{Tuple{Point{2,Int64},Point{2,Int64}}, 1}}},Array{LineString{2,Int64,Point{2,Int64},Base.ReinterpretArray{GeometryBasics.Ngon{2,Int64,2,Point{2,Int64}},1,Tuple{Point{2,Int64},Point{2,Int64}},TupleView{Tuple{Point{2,Int64},Point{2,Int64}}, 1}}},1}}(GeometryBasics.Ngon{2,Int64,2,Point{2,Int64}}[Line([3, 1] => [4, 4]), Line([4, 4] => [2, 4]), Line([2, 4] => [1, 2]), Line([1, 2] => [3, 1])], LineString{2,Int64,Point{2,Int64},Base.ReinterpretArray{GeometryBasics.Ngon{2,Int64,2,Point{2,Int64}},1,Tuple{Point{2,Int64},Point{2,Int64}},TupleView{Tuple{Point{2,Int64},Point{2,Int64}}, 1}}}[]) |
| 73 | + |
| 74 | +# create a rectangle placed at the origin with unit widths |
| 75 | +julia> rect = Rect(Vec(0.0, 0.0), Vec(1.0, 1.0)) |
| 76 | +GeometryBasics.HyperRectangle{2,Float64}([0.0, 0.0], [1.0, 1.0]) |
| 77 | + |
| 78 | +# decompose the rectangle into two triangular faces |
| 79 | +julia> rect_faces = decompose(TriangleFace{Int}, rect) |
| 80 | +2-element Array{NgonFace{3,Int64},1}: |
| 81 | + TriangleFace(1, 2, 4) |
| 82 | + TriangleFace(1, 4, 3) |
| 83 | + |
| 84 | +# decompose the rectangle into four vertices |
| 85 | +julia> rect_vertices = decompose(Point{2, Float64}, rect) |
| 86 | +4-element Array{Point{2,Float64},1}: |
| 87 | + [0.0, 0.0] |
| 88 | + [1.0, 0.0] |
| 89 | + [0.0, 1.0] |
| 90 | + [1.0, 1.0] |
| 91 | + |
| 92 | +# combine the vertices and faces into a triangle mesh |
| 93 | +julia> mesh = Mesh(rect_vertices, rect_faces) |
| 94 | +Mesh{2, Float64, Triangle}: |
| 95 | + Triangle([0.0, 0.0], [1.0, 0.0], [1.0, 1.0]) |
| 96 | + Triangle([0.0, 0.0], [1.0, 1.0], [0.0, 1.0]) |
246 | 97 | ```
|
0 commit comments