Skip to content

Commit 47f0e23

Browse files
eliascarvjuliohm
andauthored
Refactor vizmesh! (#1008)
* Refactor 'vizmesh!' * Apply suggestions * Update 'asmakie' * Apply suggestions * Adjust comments --------- Co-authored-by: Júlio Hoffimann <[email protected]>
1 parent a0a1b30 commit 47f0e23

File tree

4 files changed

+44
-39
lines changed

4 files changed

+44
-39
lines changed

ext/MeshesMakieExt.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ using Colorfy
1515
using CoordRefSystems: Projected
1616

1717
import TransformsBase as TB
18+
import Makie.GeometryBasics as GB
1819

1920
import Meshes: viz, viz!
2021
import Makie

ext/geometryset.jl

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -128,22 +128,3 @@ function showfacets2D!(plot, geoms)
128128
viz!(plot, bset, color=segmentcolor, segmentsize=segmentsize)
129129
end
130130
end
131-
132-
asmakie(geoms::AbstractVector{<:Geometry}) = asmakie.(geoms)
133-
134-
asmakie(multis::AbstractVector{<:Multi}) = mapreduce(m -> asmakie.(parent(m)), vcat, multis)
135-
136-
function asmakie(poly::Polygon)
137-
rs = rings(poly)
138-
outer = [asmakie(p) for p in vertices(first(rs))]
139-
if hasholes(poly)
140-
inners = map(i -> [asmakie(p) for p in vertices(rs[i])], 2:length(rs))
141-
Makie.Polygon(outer, inners)
142-
else
143-
Makie.Polygon(outer)
144-
end
145-
end
146-
147-
asmakie(p::Point) = Makie.Point(ustrip.(Tuple(to(p))))
148-
149-
asmakie(v::Vec) = Makie.Vec(ustrip.(Tuple(v)))

ext/mesh.jl

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,36 +71,39 @@ function vizmesh!(plot, ::Type{<:𝔼}, ::Val{2}, ::Val)
7171
elems = elements(topo)
7272

7373
# coordinates of vertices
74-
coords = map(p -> ustrip.(to(p)), verts)
74+
coords = map(asmakie, verts)
7575

7676
# fan triangulation (assume convexity)
77-
tris4elem = map(elems) do elem
77+
ntris = sum(e -> nvertices(pltype(e)) - 2, elems)
78+
tris = Vector{GB.TriangleFace{Int}}(undef, ntris)
79+
tind = 0
80+
for elem in elems
7881
I = indices(elem)
79-
[[I[1], I[i], I[i + 1]] for i in 2:(length(I) - 1)]
82+
for i in 2:(length(I) - 1)
83+
tind += 1
84+
tris[tind] = GB.TriangleFace(I[1], I[i], I[i + 1])
85+
end
8086
end
8187

82-
# flatten vector of triangles
83-
tris = [tri for tris in tris4elem for tri in tris]
84-
8588
# element vs. vertex coloring
8689
if $colorant isa AbstractVector
8790
ncolor = length($colorant)
8891
if ncolor == nelem # element coloring
8992
# duplicate vertices and adjust
9093
# connectivities to avoid linear
9194
# interpolation of colors
92-
nt = 0
95+
tind = 0
9396
elem4tri = Dict{Int,Int}()
94-
for e in 1:nelem
95-
Δs = tris4elem[e]
96-
for _ in 1:length(Δs)
97-
nt += 1
98-
elem4tri[nt] = e
97+
sizehint!(elem4tri, ntris)
98+
for (eind, e) in enumerate(elems)
99+
for _ in 1:(nvertices(pltype(e)) - 2)
100+
tind += 1
101+
elem4tri[tind] = eind
99102
end
100103
end
101-
nv = 3nt
104+
nv = 3ntris
102105
tcoords = [coords[i] for tri in tris for i in tri]
103-
tconnec = [collect(I) for I in Iterators.partition(1:nv, 3)]
106+
tconnec = [GB.TriangleFace(i, i + 1, i + 2) for i in range(start=1, step=3, length=ntris)]
104107
tcolors = map(1:nv) do i
105108
t = ceil(Int, i / 3)
106109
e = elem4tri[t]
@@ -126,22 +129,23 @@ function vizmesh!(plot, ::Type{<:𝔼}, ::Val{2}, ::Val)
126129
tcolors = $colorant
127130
end
128131

129-
# convert connectivities to matrix format
130-
tmatrix = reduce(hcat, tconnec) |> transpose
131-
132132
# enable shading in 3D
133133
tshading = dim == 3 ? Makie.FastShading : Makie.NoShading
134134

135-
tcoords, tmatrix, tcolors, tshading
135+
tcoords, tconnec, tcolors, tshading
136136
end
137137

138138
# unpack observable of parameters
139139
tcoords = Makie.@lift $tparams[1]
140-
tmatrix = Makie.@lift $tparams[2]
140+
tconnec = Makie.@lift $tparams[2]
141141
tcolors = Makie.@lift $tparams[3]
142142
tshading = Makie.@lift $tparams[4]
143143

144-
Makie.mesh!(plot, tcoords, tmatrix, color=tcolors, shading=tshading)
144+
# Makie's triangle mesh
145+
mkemesh = Makie.@lift GB.Mesh($tcoords, $tconnec)
146+
147+
# main visualization
148+
Makie.mesh!(plot, mkemesh, color=tcolors, shading=tshading)
145149

146150
if showsegments[]
147151
# retrieve coordinates parameters

ext/utils.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,22 @@ function vizmany!(plot, objs, color)
2727

2828
viz!(plot, object, color=colors, alpha=alphas, colormap=colormap, pointsize=pointsize, segmentsize=segmentsize)
2929
end
30+
31+
asmakie(geoms::AbstractVector{<:Geometry}) = asmakie.(geoms)
32+
33+
asmakie(multis::AbstractVector{<:Multi}) = mapreduce(m -> asmakie.(parent(m)), vcat, multis)
34+
35+
function asmakie(poly::Polygon)
36+
rs = rings(poly)
37+
outer = [asmakie(p) for p in vertices(first(rs))]
38+
if hasholes(poly)
39+
inners = map(i -> [asmakie(p) for p in vertices(rs[i])], 2:length(rs))
40+
Makie.Polygon(outer, inners)
41+
else
42+
Makie.Polygon(outer)
43+
end
44+
end
45+
46+
asmakie(p::Point) = Makie.Point{embeddim(p),Float32}(ustrip.(Tuple(to(p))))
47+
48+
asmakie(v::Vec) = Makie.Vec{length(v),Float32}(ustrip.(Tuple(v)))

0 commit comments

Comments
 (0)