Skip to content

Commit 6624d79

Browse files
authored
Add showsegments support for vizgridfallback! (#1056)
* Add 'showsegments' support for 'vizgridfallback!' * Fix 'vizgridfallback!' * Apply suggestions * Apply suggestions * Apply suggestions * Fix code * Apply suggestions * Apply suggestions * Fix typo
1 parent 523d039 commit 6624d79

File tree

6 files changed

+181
-107
lines changed

6 files changed

+181
-107
lines changed

ext/geometryset.jl

+66-44
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,7 @@
22
# Licensed under the MIT License. See LICENSE in the project root.
33
# ------------------------------------------------------------------
44

5-
function Makie.plot!(plot::Viz{<:Tuple{GeometrySet}})
6-
gset = plot[:object]
7-
color = plot[:color]
8-
alpha = plot[:alpha]
9-
colormap = plot[:colormap]
10-
colorrange = plot[:colorrange]
11-
12-
# process color spec into colorant
13-
colorant = Makie.@lift process($color, $colormap, $colorrange, $alpha)
14-
15-
# get geometries
16-
geoms = Makie.@lift parent($gset)
17-
18-
# get geometry types
19-
types = Makie.@lift unique(typeof.($geoms))
20-
21-
for G in types[]
22-
inds = Makie.@lift findall(g -> g isa G, $geoms)
23-
gvec = Makie.@lift collect(G, $geoms[$inds])
24-
colors = Makie.@lift $colorant isa AbstractVector ? $colorant[$inds] : $colorant
25-
M = Makie.@lift manifold(first($gvec))
26-
pdim = Makie.@lift paramdim(first($gvec))
27-
edim = Makie.@lift embeddim(first($gvec))
28-
vizgset!(plot, M[], Val(pdim[]), Val(edim[]), gvec, colors)
29-
end
30-
end
5+
Makie.plot!(plot::Viz{<:Tuple{GeometrySet}}) = vizgeoms!(plot)
316

327
const ObservableVector{T} = Makie.Observable{<:AbstractVector{T}}
338

@@ -52,14 +27,20 @@ function vizgset!(plot, ::Type{<:𝔼}, ::Val{0}, ::Val, geoms::ObservableVector
5227
end
5328

5429
function vizgset!(plot, ::Type{<:𝔼}, ::Val{1}, ::Val, geoms, colorant)
30+
showpoints = plot[:showpoints]
31+
5532
meshes = Makie.@lift discretize.($geoms)
5633
vizmany!(plot, meshes, colorant)
57-
showfacets1D!(plot, geoms)
34+
35+
if showpoints[]
36+
vizfacets!(plot, geoms)
37+
end
5838
end
5939

6040
function vizgset!(plot, ::Type{<:𝔼}, ::Val{1}, ::Val, geoms::ObservableVector{<:Ray}, colorant)
6141
rset = plot[:object]
6242
segmentsize = plot[:segmentsize]
43+
showpoints = plot[:showpoints]
6344

6445
Dim = embeddim(rset[])
6546

@@ -71,13 +52,20 @@ function vizgset!(plot, ::Type{<:𝔼}, ::Val{1}, ::Val, geoms::ObservableVector
7152
size = Makie.@lift 0.1 * $segmentsize
7253
Makie.arrows!(plot, orig, dirs, color=colorant, arrowsize=size)
7354

74-
showfacets1D!(plot, geoms)
55+
if showpoints[]
56+
vizfacets!(plot, geoms)
57+
end
7558
end
7659

7760
function vizgset!(plot, ::Type{<:𝔼}, ::Val{2}, ::Val, geoms, colorant)
61+
showsegments = plot[:showsegments]
62+
7863
meshes = Makie.@lift discretize.($geoms)
7964
vizmany!(plot, meshes, colorant)
80-
showfacets2D!(plot, geoms)
65+
66+
if showsegments[]
67+
vizfacets!(plot, geoms)
68+
end
8169
end
8270

8371
const PolygonLike = Union{Polygon,MultiPolygon}
@@ -104,29 +92,63 @@ function vizgset!(plot, ::Type{<:𝔼}, ::Val{3}, ::Val, geoms, colorant)
10492
vizmany!(plot, meshes, colorant)
10593
end
10694

107-
function showfacets1D!(plot, geoms)
108-
showpoints = plot[:showpoints]
95+
vizfacets!(plot::Viz{<:Tuple{GeometrySet}}) = vizgeoms!(plot, facets=false)
96+
97+
function vizfacets!(plot::Viz{<:Tuple{GeometrySet}}, geoms)
98+
M = Makie.@lift manifold(first($geoms))
99+
pdim = Makie.@lift paramdim(first($geoms))
100+
edim = Makie.@lift embeddim(first($geoms))
101+
vizgsetfacets!(plot, M[], Val(pdim[]), Val(edim[]), geoms)
102+
end
103+
104+
function vizgsetfacets!(plot, ::Type, ::Val{1}, ::Val, geoms)
109105
pointmarker = plot[:pointmarker]
110106
pointcolor = plot[:pointcolor]
111107
pointsize = plot[:pointsize]
112108

113-
if showpoints[]
114-
# all boundaries are points or multipoints
115-
points = Makie.@lift filter(!isnothing, boundary.($geoms))
116-
pset = Makie.@lift GeometrySet($points)
117-
viz!(plot, pset, color=pointcolor, pointmarker=pointmarker, pointsize=pointsize)
118-
end
109+
# all boundaries are points or multipoints
110+
points = Makie.@lift filter(!isnothing, boundary.($geoms))
111+
pset = Makie.@lift GeometrySet($points)
112+
viz!(plot, pset, color=pointcolor, pointmarker=pointmarker, pointsize=pointsize)
119113
end
120114

121-
function showfacets2D!(plot, geoms)
122-
showsegments = plot[:showsegments]
115+
function vizgsetfacets!(plot, ::Type, ::Val{2}, ::Val, geoms)
123116
segmentcolor = plot[:segmentcolor]
124117
segmentsize = plot[:segmentsize]
125118

126-
if showsegments[]
127-
# all boundaries are 1D geometries
128-
bounds = Makie.@lift filter(!isnothing, boundary.($geoms))
129-
bset = Makie.@lift GeometrySet($bounds)
130-
viz!(plot, bset, color=segmentcolor, segmentsize=segmentsize)
119+
# all boundaries are 1D geometries
120+
bounds = Makie.@lift filter(!isnothing, boundary.($geoms))
121+
bset = Makie.@lift GeometrySet($bounds)
122+
viz!(plot, bset, color=segmentcolor, segmentsize=segmentsize)
123+
end
124+
125+
function vizgeoms!(plot; facets=false)
126+
gset = plot[:object]
127+
color = plot[:color]
128+
alpha = plot[:alpha]
129+
colormap = plot[:colormap]
130+
colorrange = plot[:colorrange]
131+
132+
# process color spec into colorant
133+
colorant = facets ? nothing : Makie.@lift(process($color, $colormap, $colorrange, $alpha))
134+
135+
# get geometries
136+
geoms = Makie.@lift parent($gset)
137+
138+
# get geometry types
139+
types = Makie.@lift unique(typeof.($geoms))
140+
141+
for G in types[]
142+
inds = Makie.@lift findall(g -> g isa G, $geoms)
143+
gvec = Makie.@lift collect(G, $geoms[$inds])
144+
M = Makie.@lift manifold(first($gvec))
145+
pdim = Makie.@lift paramdim(first($gvec))
146+
edim = Makie.@lift embeddim(first($gvec))
147+
if facets
148+
vizgsetfacets!(plot, M[], Val(pdim[]), Val(edim[]), gvec)
149+
else
150+
cvec = Makie.@lift $colorant isa AbstractVector ? $colorant[$inds] : $colorant
151+
vizgset!(plot, M[], Val(pdim[]), Val(edim[]), gvec, cvec)
152+
end
131153
end
132154
end

ext/grid.jl

+18-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ end
1616

1717
vizgrid!(plot, M::Type{<:𝔼}, pdim::Val, edim::Val) = vizgridfallback!(plot, M, pdim, edim)
1818

19+
function vizfacets!(plot::Viz{<:Tuple{Grid}})
20+
grid = plot[:object]
21+
M = Makie.@lift manifold($grid)
22+
pdim = Makie.@lift paramdim($grid)
23+
edim = Makie.@lift embeddim($grid)
24+
vizgridfacets!(plot, M[], Val(pdim[]), Val(edim[]))
25+
end
26+
27+
vizgridfacets!(plot, M::Type, pdim::Val, edim::Val) = vizmeshfacets!(plot, M, pdim, edim)
28+
1929
# ----------------
2030
# SPECIALIZATIONS
2131
# ----------------
@@ -35,6 +45,7 @@ function vizgridfallback!(plot, M, pdim, edim)
3545
alpha = plot[:alpha]
3646
colormap = plot[:colormap]
3747
colorrange = plot[:colorrange]
48+
showsegments = plot[:showsegments]
3849

3950
if pdim == Val(2) # visualize quadrangle mesh with texture using uv coords
4051
# decide whether or not to reverse connectivity list
@@ -50,23 +61,28 @@ function vizgridfallback!(plot, M, pdim, edim)
5061
ncolor = Makie.@lift $colorant isa AbstractVector ? length($colorant) : 1
5162

5263
dims = Makie.@lift size($grid)
64+
vdims = Makie.@lift Meshes.vsize($grid)
5365
texture = if ncolor[] == 1
5466
Makie.@lift fill($colorant, $dims)
5567
elseif ncolor[] == nquads[]
5668
Makie.@lift reshape($colorant, $dims)
5769
elseif ncolor[] == nverts[]
58-
Makie.@lift reshape($colorant, $dims .+ 1)
70+
Makie.@lift reshape($colorant, $vdims)
5971
else
6072
throw(ArgumentError("invalid number of colors"))
6173
end
6274

63-
uv = Makie.@lift [Makie.Vec2f(v, 1 - u) for v in range(0, 1, $dims[2] + 1) for u in range(0, 1, $dims[1] + 1)]
75+
uv = Makie.@lift [Makie.Vec2f(v, 1 - u) for v in range(0, 1, $vdims[2]) for u in range(0, 1, $vdims[1])]
6476

6577
mesh = Makie.@lift GB.Mesh(Makie.meta($verts, uv=$uv), $quads)
6678

6779
shading = edim == Val(3) ? Makie.FastShading : Makie.NoShading
6880

6981
Makie.mesh!(plot, mesh, color=texture, shading=shading)
82+
83+
if showsegments[]
84+
vizfacets!(plot)
85+
end
7086
else # fallback to triangle mesh visualization
7187
vizmesh!(plot, M, pdim, edim)
7288
end

ext/grid/cartesian.jl

+25-12
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ function vizgrid!(plot::Viz{<:Tuple{CartesianGrid}}, ::Type{<:𝔼}, ::Val{2}, :
3131
viz!(plot, bbox, color=colorant)
3232

3333
if showsegments[]
34-
xyz = Makie.@lift map(x -> ustrip.(x), Meshes.xyz($grid))
35-
tup = Makie.@lift xysegments($xyz...)
36-
x, y = Makie.@lift($tup[1]), Makie.@lift($tup[2])
37-
Makie.lines!(plot, x, y, color=segmentcolor, linewidth=segmentsize)
34+
vizfacets!(plot)
3835
end
3936
else
4037
if nc[] == nv[]
@@ -48,9 +45,7 @@ function vizgrid!(plot::Viz{<:Tuple{CartesianGrid}}, ::Type{<:𝔼}, ::Val{2}, :
4845
end
4946

5047
if showsegments[]
51-
tup = Makie.@lift xysegments(0:$sz[1], 0:$sz[2])
52-
x, y = Makie.@lift($tup[1]), Makie.@lift($tup[2])
53-
Makie.lines!(plot, x, y, color=segmentcolor, linewidth=segmentsize)
48+
vizfacets!(plot)
5449
end
5550

5651
# adjust spacing and origin
@@ -69,8 +64,6 @@ function vizgrid!(plot::Viz{<:Tuple{CartesianGrid}}, ::Type{<:𝔼}, ::Val{3}, :
6964
colormap = plot[:colormap]
7065
colorrange = plot[:colorrange]
7166
showsegments = plot[:showsegments]
72-
segmentcolor = plot[:segmentcolor]
73-
segmentsize = plot[:segmentsize]
7467

7568
# process color spec into colorant
7669
colorant = Makie.@lift process($color, $colormap, $colorrange, $alpha)
@@ -103,8 +96,28 @@ function vizgrid!(plot::Viz{<:Tuple{CartesianGrid}}, ::Type{<:𝔼}, ::Val{3}, :
10396
end
10497

10598
if showsegments[]
106-
tup = Makie.@lift xyzsegments($xyz...)
107-
x, y, z = Makie.@lift($tup[1]), Makie.@lift($tup[2]), Makie.@lift($tup[3])
108-
Makie.lines!(plot, x, y, z, color=segmentcolor, linewidth=segmentsize)
99+
vizfacets!(plot)
109100
end
110101
end
102+
103+
function vizgridfacets!(plot::Viz{<:Tuple{CartesianGrid}}, ::Type{<:𝔼}, ::Val{2}, ::Val{2})
104+
grid = plot[:object]
105+
segmentcolor = plot[:segmentcolor]
106+
segmentsize = plot[:segmentsize]
107+
108+
xyz = Makie.@lift map(x -> ustrip.(x), Meshes.xyz($grid))
109+
tup = Makie.@lift xysegments($xyz...)
110+
x, y = Makie.@lift($tup[1]), Makie.@lift($tup[2])
111+
Makie.lines!(plot, x, y, color=segmentcolor, linewidth=segmentsize)
112+
end
113+
114+
function vizgridfacets!(plot::Viz{<:Tuple{CartesianGrid}}, ::Type{<:𝔼}, ::Val{3}, ::Val{3})
115+
grid = plot[:object]
116+
segmentcolor = plot[:segmentcolor]
117+
segmentsize = plot[:segmentsize]
118+
119+
xyz = Makie.@lift map(x -> ustrip.(x), Meshes.xyz($grid))
120+
tup = Makie.@lift xyzsegments($xyz...)
121+
x, y, z = Makie.@lift($tup[1]), Makie.@lift($tup[2]), Makie.@lift($tup[3])
122+
Makie.lines!(plot, x, y, z, color=segmentcolor, linewidth=segmentsize)
123+
end

ext/grid/rectilinear.jl

+12-3
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,20 @@ function vizgrid!(plot::Viz{<:Tuple{RectilinearGrid}}, M::Type{<:𝔼}, pdim::Va
4444
end
4545

4646
if showsegments[]
47-
tup = Makie.@lift xysegments($xs, $ys)
48-
x, y = Makie.@lift($tup[1]), Makie.@lift($tup[2])
49-
Makie.lines!(plot, x, y, color=segmentcolor, linewidth=segmentsize)
47+
vizfacets!(plot)
5048
end
5149
else
5250
vizgridfallback!(plot, M, pdim, edim)
5351
end
5452
end
53+
54+
function vizgridfacets!(plot::Viz{<:Tuple{RectilinearGrid}}, ::Type{<:𝔼}, ::Val{2}, ::Val{2})
55+
grid = plot[:object]
56+
segmentcolor = plot[:segmentcolor]
57+
segmentsize = plot[:segmentsize]
58+
59+
xyz = Makie.@lift map(x -> ustrip.(x), Meshes.xyz($grid))
60+
tup = Makie.@lift xysegments($xyz...)
61+
x, y = Makie.@lift($tup[1]), Makie.@lift($tup[2])
62+
Makie.lines!(plot, x, y, color=segmentcolor, linewidth=segmentsize)
63+
end

ext/grid/structured.jl

+11-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ function vizgrid!(plot::Viz{<:Tuple{StructuredGrid}}, M::Type{<:𝔼}, pdim::Val
3232
Makie.surface!(plot, X, Y, color=C)
3333

3434
if showsegments[]
35-
tup = Makie.@lift structuredsegments($grid)
36-
x, y = Makie.@lift($tup[1]), Makie.@lift($tup[2])
37-
Makie.lines!(plot, x, y, color=segmentcolor, linewidth=segmentsize)
35+
vizfacets!(plot)
3836
end
3937
else
4038
vizmesh!(plot, M, pdim, edim)
@@ -44,6 +42,16 @@ function vizgrid!(plot::Viz{<:Tuple{StructuredGrid}}, M::Type{<:𝔼}, pdim::Val
4442
end
4543
end
4644

45+
function vizgridfacets!(plot::Viz{<:Tuple{StructuredGrid}}, ::Type{<:𝔼}, ::Val{2}, ::Val{2})
46+
grid = plot[:object]
47+
segmentcolor = plot[:segmentcolor]
48+
segmentsize = plot[:segmentsize]
49+
50+
tup = Makie.@lift structuredsegments($grid)
51+
x, y = Makie.@lift($tup[1]), Makie.@lift($tup[2])
52+
Makie.lines!(plot, x, y, color=segmentcolor, linewidth=segmentsize)
53+
end
54+
4755
function structuredsegments(grid)
4856
cinds = CartesianIndices(size(grid) .+ 1)
4957
coords = []

0 commit comments

Comments
 (0)