Skip to content

Commit 523d039

Browse files
authored
Refactor viz of grids (#1055)
* Refactor viz of grids * Fix code
1 parent e5b5756 commit 523d039

File tree

4 files changed

+109
-85
lines changed

4 files changed

+109
-85
lines changed

ext/grid.jl

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function vizgrid!(plot, ::Type{<:🌐}, pdim::Val, edim::Val)
1414
vizgrid!(plot, 𝔼, pdim, edim)
1515
end
1616

17-
vizgrid!(plot, M::Type{<:𝔼}, pdim::Val, edim::Val) = vizmesh!(plot, M, pdim, edim)
17+
vizgrid!(plot, M::Type{<:𝔼}, pdim::Val, edim::Val) = vizgridfallback!(plot, M, pdim, edim)
1818

1919
# ----------------
2020
# SPECIALIZATIONS
@@ -25,6 +25,56 @@ include("grid/rectilinear.jl")
2525
include("grid/structured.jl")
2626
include("grid/transformed.jl")
2727

28+
# -----------------
29+
# HELPER FUNCTIONS
30+
# -----------------
31+
32+
function vizgridfallback!(plot, M, pdim, edim)
33+
grid = plot[:object]
34+
color = plot[:color]
35+
alpha = plot[:alpha]
36+
colormap = plot[:colormap]
37+
colorrange = plot[:colorrange]
38+
39+
if pdim == Val(2) # visualize quadrangle mesh with texture using uv coords
40+
# decide whether or not to reverse connectivity list
41+
rfunc = Makie.@lift _reverse(crs($grid))
42+
43+
verts = Makie.@lift map(asmakie, vertices($grid))
44+
quads = Makie.@lift [GB.QuadFace($rfunc(indices(e))) for e in elements(topology($grid))]
45+
46+
colorant = Makie.@lift process($color, $colormap, $colorrange, $alpha)
47+
48+
nverts = Makie.@lift length($verts)
49+
nquads = Makie.@lift length($quads)
50+
ncolor = Makie.@lift $colorant isa AbstractVector ? length($colorant) : 1
51+
52+
dims = Makie.@lift size($grid)
53+
texture = if ncolor[] == 1
54+
Makie.@lift fill($colorant, $dims)
55+
elseif ncolor[] == nquads[]
56+
Makie.@lift reshape($colorant, $dims)
57+
elseif ncolor[] == nverts[]
58+
Makie.@lift reshape($colorant, $dims .+ 1)
59+
else
60+
throw(ArgumentError("invalid number of colors"))
61+
end
62+
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)]
64+
65+
mesh = Makie.@lift GB.Mesh(Makie.meta($verts, uv=$uv), $quads)
66+
67+
shading = edim == Val(3) ? Makie.FastShading : Makie.NoShading
68+
69+
Makie.mesh!(plot, mesh, color=texture, shading=shading)
70+
else # fallback to triangle mesh visualization
71+
vizmesh!(plot, M, pdim, edim)
72+
end
73+
end
74+
75+
_reverse(::Type{<:CRS}) = identity
76+
_reverse(::Type{<:LatLon}) = reverse
77+
2878
# helper functions to create a minimum number
2979
# of line segments within Cartesian/Rectilinear grid
3080
function xysegments(xs, ys)

ext/grid/rectilinear.jl

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,43 @@ function vizgrid!(plot::Viz{<:Tuple{RectilinearGrid}}, M::Type{<:𝔼}, pdim::Va
1212
segmentcolor = plot[:segmentcolor]
1313
segmentsize = plot[:segmentsize]
1414

15-
# process color spec into colorant
16-
colorant = Makie.@lift process($color, $colormap, $colorrange, $alpha)
15+
if crs(grid[]) <: Cartesian
16+
# process color spec into colorant
17+
colorant = Makie.@lift process($color, $colormap, $colorrange, $alpha)
1718

18-
# number of vertices and colors
19-
nv = Makie.@lift nvertices($grid)
20-
nc = Makie.@lift $colorant isa AbstractVector ? length($colorant) : 1
19+
# number of vertices and colors
20+
nv = Makie.@lift nvertices($grid)
21+
nc = Makie.@lift $colorant isa AbstractVector ? length($colorant) : 1
2122

22-
# grid coordinates
23-
xyz = Makie.@lift map(x -> ustrip.(x), Meshes.xyz($grid))
24-
xs = Makie.@lift $xyz[1]
25-
ys = Makie.@lift $xyz[2]
23+
# grid coordinates
24+
xyz = Makie.@lift map(x -> ustrip.(x), Meshes.xyz($grid))
25+
xs = Makie.@lift $xyz[1]
26+
ys = Makie.@lift $xyz[2]
2627

27-
if nc[] == 1
28-
# visualize bounding box with a single
29-
# color for maximum performance
30-
bbox = Makie.@lift boundingbox($grid)
31-
viz!(plot, bbox, color=colorant)
32-
else
33-
if nc[] == nv[]
34-
# visualize as a simple mesh so that
35-
# colors can be specified at vertices
36-
vizmesh!(plot, M, pdim, edim)
28+
if nc[] == 1
29+
# visualize bounding box with a single
30+
# color for maximum performance
31+
bbox = Makie.@lift boundingbox($grid)
32+
viz!(plot, bbox, color=colorant)
3733
else
38-
# visualize as built-in heatmap
39-
sz = Makie.@lift size($grid)
40-
C = Makie.@lift reshape($colorant, $sz)
41-
Makie.heatmap!(plot, xs, ys, C)
34+
if nc[] == nv[]
35+
# visualize as a simple mesh so that
36+
# colors can be specified at vertices
37+
vizmesh!(plot, M, pdim, edim)
38+
else
39+
# visualize as built-in heatmap
40+
sz = Makie.@lift size($grid)
41+
C = Makie.@lift reshape($colorant, $sz)
42+
Makie.heatmap!(plot, xs, ys, C)
43+
end
4244
end
43-
end
4445

45-
if showsegments[]
46-
tup = Makie.@lift xysegments($xs, $ys)
47-
x, y = Makie.@lift($tup[1]), Makie.@lift($tup[2])
48-
Makie.lines!(plot, x, y, color=segmentcolor, linewidth=segmentsize)
46+
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)
50+
end
51+
else
52+
vizgridfallback!(plot, M, pdim, edim)
4953
end
5054
end

ext/grid/structured.jl

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,35 @@ function vizgrid!(plot::Viz{<:Tuple{StructuredGrid}}, M::Type{<:𝔼}, pdim::Val
1212
segmentcolor = plot[:segmentcolor]
1313
segmentsize = plot[:segmentsize]
1414

15-
# process color spec into colorant
16-
colorant = Makie.@lift process($color, $colormap, $colorrange, $alpha)
15+
if crs(grid[]) <: Cartesian
16+
# process color spec into colorant
17+
colorant = Makie.@lift process($color, $colormap, $colorrange, $alpha)
1718

18-
# number of vertices and colors
19-
nv = Makie.@lift nvertices($grid)
20-
nc = Makie.@lift $colorant isa AbstractVector ? length($colorant) : 1
19+
# number of vertices and colors
20+
nv = Makie.@lift nvertices($grid)
21+
nc = Makie.@lift $colorant isa AbstractVector ? length($colorant) : 1
2122

22-
if nc[] == nv[]
23-
# size and coordinates
24-
sz = Makie.@lift size($grid) .+ 1
25-
XYZ = Makie.@lift map(X -> ustrip.(X), Meshes.XYZ($grid))
26-
X = Makie.@lift $XYZ[1]
27-
Y = Makie.@lift $XYZ[2]
23+
if nc[] == nv[]
24+
# size and coordinates
25+
sz = Makie.@lift size($grid) .+ 1
26+
XYZ = Makie.@lift map(X -> ustrip.(X), Meshes.XYZ($grid))
27+
X = Makie.@lift $XYZ[1]
28+
Y = Makie.@lift $XYZ[2]
2829

29-
# visualize as built-in surface
30-
C = Makie.@lift reshape($colorant, $sz)
31-
Makie.surface!(plot, X, Y, color=C)
30+
# visualize as built-in surface
31+
C = Makie.@lift reshape($colorant, $sz)
32+
Makie.surface!(plot, X, Y, color=C)
3233

33-
if showsegments[]
34-
tup = Makie.@lift structuredsegments($grid)
35-
x, y = Makie.@lift($tup[1]), Makie.@lift($tup[2])
36-
Makie.lines!(plot, x, y, color=segmentcolor, linewidth=segmentsize)
34+
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)
38+
end
39+
else
40+
vizmesh!(plot, M, pdim, edim)
3741
end
3842
else
39-
vizmesh!(plot, M, pdim, edim)
43+
vizgridfallback!(plot, M, pdim, edim)
4044
end
4145
end
4246

ext/grid/transformed.jl

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,13 @@ function vizgrid!(plot::Viz{<:Tuple{TransformedGrid}}, M::Type{<:𝔼}, pdim::Va
1717

1818
if isoptimized(trans[]) # visualize parent grid and transform visualization
1919
grid = Makie.@lift parent($tgrid)
20-
viz!(plot, grid; color, alpha, colormap, showsegments, segmentcolor, segmentsize)
20+
viz!(plot, grid; color, alpha, colormap, colorrange, showsegments, segmentcolor, segmentsize)
2121
makietransform!(plot, trans)
22-
elseif pdim == Val(2) # visualize quadrangle mesh with texture using uv coords
23-
# decide whether or not to reverse connectivity list
24-
rfunc = Makie.@lift _reverse(crs($tgrid))
25-
26-
verts = Makie.@lift map(asmakie, vertices($tgrid))
27-
quads = Makie.@lift [GB.QuadFace($rfunc(indices(e))) for e in elements(topology($tgrid))]
28-
29-
colorant = Makie.@lift process($color, $colormap, $colorrange, $alpha)
30-
31-
nverts = Makie.@lift length($verts)
32-
nquads = Makie.@lift length($quads)
33-
ncolor = Makie.@lift $colorant isa AbstractVector ? length($colorant) : 1
34-
35-
dims = Makie.@lift size($tgrid)
36-
texture = if ncolor[] == 1
37-
Makie.@lift fill($colorant, $dims)
38-
elseif ncolor[] == nquads[]
39-
Makie.@lift reshape($colorant, $dims)
40-
elseif ncolor[] == nverts[]
41-
Makie.@lift reshape($colorant, $dims .+ 1)
42-
else
43-
throw(ArgumentError("invalid number of colors"))
44-
end
45-
46-
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)]
47-
48-
mesh = Makie.@lift GB.Mesh(Makie.meta($verts, uv=$uv), $quads)
49-
50-
shading = edim == Val(3) ? Makie.FastShading : Makie.NoShading
51-
52-
Makie.mesh!(plot, mesh, color=texture, shading=shading)
53-
else # fallback to triangle mesh visualization
54-
vizmesh!(plot, M, pdim, edim)
22+
else # fallback to full grid visualization
23+
vizgridfallback!(plot, M, pdim, edim)
5524
end
5625
end
5726

58-
_reverse(::Type{<:CRS}) = identity
59-
_reverse(::Type{<:LatLon}) = reverse
60-
6127
# --------------
6228
# OPTIMIZATIONS
6329
# --------------

0 commit comments

Comments
 (0)