Skip to content

Commit ae50d5a

Browse files
committed
add plotting convenience
1 parent ed66276 commit ae50d5a

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

src/GeometryBasics.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ using .FixedSizeArrays
1010
include("basic_types.jl")
1111
include("metadata.jl")
1212
include("viewtypes.jl")
13+
include("abstractplotting.jl")
14+
1315

1416
end # module

src/abstractplotting.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,10 @@ function AbstractPlotting.convert_arguments(::Type{<: AbstractPlotting.Mesh}, me
4747
GeometryTypes.GLTriangle.(faces(mesh))
4848
),)
4949
end
50+
51+
52+
function to_mesh(mesh::GeometryTypes.AbstractMesh)
53+
points = GeometryBasics.Point{3, Float64}.(GeometryTypes.vertices(mesh))
54+
facets = GeometryBasics.TriangleFace{Cint}.(GeometryTypes.faces(mesh))
55+
Mesh(points, facets)
56+
end

src/basic_types.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Base.length(::NNgon{N}) where N = N
8282
"""
8383
The Ngon Polytope element type when indexing an array of points with a SimplexFace
8484
"""
85-
function Polytope(P::Type{<: AbstractPoint{Dim, T}}, ::Type{<: AbstractNgonFace{N}}) where {N, Dim, T}
85+
function Polytope(P::Type{<: AbstractPoint{Dim, T}}, ::Type{<: AbstractNgonFace{N, IT}}) where {N, Dim, T, IT}
8686
Ngon{Dim, T, N, P}
8787
end
8888

src/metadata.jl

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,21 @@ E.g:
4848
MetaFree(PointMeta) == Point
4949
"""
5050
MetaFree(::Type{T}) where T = error("No meta free Type for $T")
51+
52+
"""
53+
meta(x::MetaObject)
54+
Returns the metadata of `x`
55+
"""
56+
meta(x::T) where T = error("$T has no meta!")
57+
58+
metafree(x::T) where T = x
59+
60+
61+
"""
62+
meta(x::MetaObject; meta_data...)
63+
Attaches metadata to `x`
64+
"""
5165
meta(x::T) where T = error("$T has no meta!")
52-
metafree(x::T) where T = error("$T has no meta free representation!")
5366

5467

5568
macro meta_type(name, mainfield, supertype, params...)
@@ -104,9 +117,20 @@ macro meta_type(name, mainfield, supertype, params...)
104117
end
105118

106119
function GeometryBasics.meta(elements::AbstractVector{T}; meta...) where T <: $supertype
120+
n = length(elements)
121+
for (k, v) in meta
122+
if v isa AbstractVector
123+
mn = length(v)
124+
mn != n && error("Metadata array needs to have same length as data.
125+
Found $(n) data items, and $mn metadata items")
126+
else
127+
error("Metadata needs to be an array with the same length as data items. Found: $(typeof(v))")
128+
end
129+
end
107130
nt = values(meta)
108131
# get the first element to get the per element named tuple type
109132
ElementNT = typeof(map(first, nt))
133+
110134
StructArray{MetaType(T, ElementNT)}(($(mainfield) = elements, nt...))
111135
end
112136

@@ -125,3 +149,23 @@ Base.getindex(x::NgonFaceMeta, idx::Int) = getindex(metafree(x), idx)
125149
Base.getindex(x::SimplexFaceMeta, idx::Int) = getindex(metafree(x), idx)
126150

127151
@meta_type(Polygon, polygon, AbstractPolygon, N, T)
152+
153+
154+
"""
155+
pointmeta(mesh::Mesh; meta_data...)
156+
157+
Attaches metadata to the coordinates of a mesh
158+
"""
159+
function pointmeta(mesh::Mesh; meta_data...)
160+
Mesh(meta(coordinates(mesh); meta_data...), faces(mesh))
161+
end
162+
163+
164+
"""
165+
facemeta(mesh::Mesh; meta_data...)
166+
167+
Attaches metadata to the faces of a mesh
168+
"""
169+
function facemeta(mesh::Mesh; meta_data...)
170+
Mesh(coordinates(mesh), meta(faces(mesh); meta_data...))
171+
end

0 commit comments

Comments
 (0)