Skip to content

Commit 76f6aad

Browse files
committed
add more view types + constructors
1 parent 64fdc5f commit 76f6aad

File tree

6 files changed

+929
-228
lines changed

6 files changed

+929
-228
lines changed

src/GeometryBasics.jl

Lines changed: 4 additions & 216 deletions
Original file line numberDiff line numberDiff line change
@@ -2,224 +2,12 @@ module GeometryBasics
22

33
using StaticArrays, Tables, StructArrays
44
import GeometryTypes
5-
import Base: convert
6-
"""
7-
Geometry made of N connected points. Connected as one flat geometry, it makes a Ngon / Polygon.
8-
Connected as volume it will be a Simplex / Tri / Cube.
9-
Note That `Polytype{N} where N == 3` denotes a Triangle both as a Simplex or Ngon.
10-
"""
11-
abstract type Polytope{Dimension, T <: Real} end
12-
abstract type AbstractPolygon{Dimension, T} <: Polytope{Dimension, T} end
5+
using Base: @propagate_inbounds
136

14-
abstract type AbstractPoint{Dimension, T} <: StaticVector{Dimension, T} end
15-
abstract type AbstractFace{Dimension, T} <: StaticVector{Dimension, T} end
16-
abstract type AbstractSimplex{Dimension, N, T} <: StaticVector{Dimension, T} end
7+
include("basic_types.jl")
8+
include("metadata.jl")
9+
include("viewtypes.jl")
1710

18-
GeometryTypes.@fixed_vector Point AbstractPoint
1911

20-
"""
21-
Face index, connecting points to form a simplex
22-
"""
23-
GeometryTypes.@fixed_vector SimplexFace AbstractFace
24-
25-
"""
26-
Face index, connecting points to form an Ngon
27-
"""
28-
GeometryTypes.@fixed_vector NgonFace AbstractFace
29-
const TriangleFace{T} = NgonFace{3, T}
30-
function element_type(P::Type{<: AbstractPoint{Dim, T}}, ::Type{<: NgonFace{N}}) where {N, Dim, T}
31-
NGon{Dim, T, N, P}
32-
end
33-
34-
"""
35-
Fixed Size Polygon, e.g.
36-
N 1-2 : Illegal!
37-
N = 3 : Triangle
38-
N = 4 : Quadrilateral (or Quad, Or tetragon)
39-
N = 5 : Pentagon
40-
...
41-
"""
42-
struct Ngon{
43-
Dimension, T <: Real,
44-
N,
45-
Point <: AbstractPoint{Dimension, T}
46-
} <: AbstractPolygon{Dimension, T}
47-
48-
points::SVector{N, Point}
49-
end
50-
# Simplex{D, T, 3} & Ngon{D, T, 3} are both representing a triangle.
51-
# Since Ngon is supposed to be flat and a triangle is flat, lets prefer Ngon
52-
# for triangle:
53-
const Triangle{Dimension, T} = Ngon{Dimension, T, 3, P} where P <: AbstractPoint{Dimension, T}
54-
const Quadrilateral{Dimension, T} = Ngon{Dimension, T, 4, P} where P <: AbstractPoint{Dimension, T}
55-
56-
"""
57-
A `Simplex` is a generalization of an N-dimensional tetrahedra and can be thought
58-
of as a minimal convex set containing the specified points.
59-
60-
* A 0-simplex is a point.
61-
* A 1-simplex is a line segment.
62-
* A 2-simplex is a triangle.
63-
* A 3-simplex is a tetrahedron.
64-
65-
Note that this datatype is offset by one compared to the traditional
66-
mathematical terminology. So a one-simplex is represented as `Simplex{2,T}`.
67-
This is for a simpler implementation.
68-
69-
It applies to infinite dimensions. The structure of this type is designed
70-
to allow embedding in higher-order spaces by parameterizing on `T`.
71-
"""
72-
struct Simplex{
73-
Dimension, T <: Real,
74-
N,
75-
Point <: AbstractPoint{Dimension, T},
76-
} <: Polytope{Dimension, T}
77-
78-
points::SVector{N, Point}
79-
end
80-
const Line{Dimension, T, P <: AbstractPoint{Dimension, T}} = Simplex{Dimension, T, 2, P}
81-
const Tetrahedron{Dimension, T, P <: AbstractPoint{Dimension, T}} = Simplex{Dimension, T, 4, P}
82-
83-
84-
struct LineString{
85-
Dimension, T <: Real,
86-
V <: AbstractVector{<: AbstractPoint{Dimension, T}}
87-
} <: AbstractVector{Line{Dimension, T}}
88-
89-
points::V
90-
end
91-
92-
struct Polygon{
93-
Dimension, T <: Real,
94-
L <: AbstractVector{<: Line{Dimension, T}},
95-
V <: AbstractVector{L}
96-
} <: AbstractPolygon{Dimension, T}
97-
98-
exterior::L
99-
interiors::V
100-
end
101-
function Polygon(exterior::AbstractVector{Line{Dimension, T, P}}, interiors::V) where {Dimension, T, P, V <: AbstractVector{<: AbstractVector{Line{Dimension, T, P}}}}
102-
Polygon{Dimension, T, typeof(exterior), V}(exterior, interiors)
103-
end
104-
105-
Polygon(exterior::L) where L <: AbstractVector{<: Line} = Polygon(exterior, L[])
106-
function Polygon(exterior::AbstractVector{P}) where {Dim, T, P <: AbstractPoint{Dim, T}}
107-
Polygon(reinterpret(Line{Dim, T, P}, exterior))
108-
end
109-
110-
111-
struct MetaPolygon{
112-
Dimension, T <: Real,
113-
P <: AbstractPolygon{Dimension, T},
114-
Names, Types
115-
} <: AbstractPolygon{Dimension, T}
116-
117-
polygon::P
118-
meta::NamedTuple{Names, Types}
119-
end
120-
121-
MetaPolygon(polygon::AbstractPolygon; meta...) = MetaPolygon(polygon, values(meta))
122-
function MetaPolygon(polygon::AbstractVector; meta...)
123-
MetaPolygon(Polygon(polygon); meta...)
124-
end
125-
function MetaPolygon(exterior::L, interior::AbstractVector{L}; meta...) where L
126-
MetaPolygon(Polygon(exterior, interior); meta...)
127-
end
128-
129-
function Tables.schema(::AbstractVector{MetaPolygon{Dim, T, P, Names, Types}}) where {Dim, T, P, Names, Types}
130-
Tables.Schema((:polygon, Names...), (P, Types...))
131-
end
132-
function Base.getproperty(x::MetaPolygon{Dim, T, P, Names, Types}, field::Symbol) where {Dim, T, P, Names, Types}
133-
field === :polygon && return getfield(x, :polygon)
134-
Base.sym_in(field, Names) && return getfield(getfield(x, :meta), field)
135-
error("Field $field not part of Element")
136-
end
137-
138-
139-
struct MultiPolygon{
140-
Dimension, T <: Real,
141-
Element <: AbstractPolygon{Dimension, T},
142-
A <: AbstractVector{Element}
143-
} <: AbstractVector{Element}
144-
145-
polygons::A
146-
end
147-
Tables.rows(x::MultiPolygon) = x.polygons
148-
Tables.istable(::Type{<:MultiPolygon}) = true
149-
Tables.istable(::Type{<:MultiPolygon{<:Tuple}}) = false
150-
Tables.rowaccess(::Type{<:MultiPolygon}) = true
151-
# Tables.columnaccess(::Type{<:MultiPolygon}) = true
152-
153-
function MultiPolygon(polygons::AbstractVector{P}; meta...) where P <: AbstractPolygon{Dim, T} where {Dim, T}
154-
nt = values(meta)
155-
n = keys(nt)
156-
typs = Tuple{eltype.(values(nt))...}
157-
m = StructArray(nt)
158-
PType = MetaPolygon{Dim, T, P, n, typs}
159-
mpolys = StructArray{PType}((polygons, m))
160-
MultiPolygon{Dim, T, PType, typeof(mpolys)}(mpolys)
161-
end
162-
163-
Base.getindex(mp::MultiPolygon, i) = mp.polygons[i]
164-
Base.size(mp::MultiPolygon) = size(mp.polygons)
165-
166-
167-
struct MultiLineString{
168-
Dimension, T <: Real,
169-
Element <: LineString{Dimension, T},
170-
A <: AbstractVector{Element}
171-
} <: AbstractVector{Element}
172-
173-
polygons::A
174-
end
175-
176-
struct Mesh{
177-
Dimension, T <: Real,
178-
Element <: Polytope{Dimension, T},
179-
V <: AbstractVector{Element}
180-
} <: AbstractVector{Element}
181-
182-
simplices::V
183-
end
184-
185-
function Mesh(elements::AbstractVector{<: Polytope{Dim, T}}) where {Dim, T}
186-
Mesh{Dim, T, eltype(elements), typeof(elements)}(elements)
187-
end
188-
189-
"""
190-
FaceView enables to link one array of points via a face array, to generate one
191-
abstract array of elements.
192-
E.g., this becomes possible:
193-
```
194-
x = FaceView(rand(Point3f0, 10), TriangleFace[(1, 2, 3), (2,4, 5), ...])
195-
x[1] isa Triangle == true
196-
x isa AbstractVector{<: Triangle} == true
197-
# This means we can use it as a mesh:
198-
Mesh(x) # should just work!
199-
Can also be used for Points:
200-
201-
linestring = FaceView(points, LineFace[...])
202-
Polygon(linestring)
203-
```
204-
"""
205-
struct FaceView{
206-
Element,
207-
Point <: AbstractPoint,
208-
Face <: AbstractFace,
209-
P <: AbstractVector{Point},
210-
F <: AbstractVector{Face}
211-
} <: AbstractVector{Element}
212-
213-
points::P
214-
faces::F
215-
end
216-
function FaceView(points::AbstractVector{P}, faces::AbstractVector{F}) where {P <: AbstractPoint, F <: AbstractFace}
217-
Element = element_type(P, F)
218-
FaceView{Element, P, F, typeof(points), typeof(faces)}(points, faces)
219-
end
220-
221-
function Mesh(points::AbstractVector{<: AbstractPoint}, faces::AbstractVector{<: AbstractFace})
222-
Mesh(FaceView(points, faces))
223-
end
22412

22513
end # module

0 commit comments

Comments
 (0)