Skip to content

Commit

Permalink
enable levelalpha/planealpha for makie
Browse files Browse the repository at this point in the history
fix colorbarticks
  • Loading branch information
j-fu committed Jun 2, 2023
1 parent c370f01 commit ba8f6d1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 21 deletions.
11 changes: 6 additions & 5 deletions examples/plotting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ end

function plotting_func3d(;
Plotter = default_plotter(),
zplane = 0.49,
xplane = 0.49,
levels = [0.25],
xplanes = [0.49],
yplanes = [0.49],
zplanes = [0.49],
levels = 5,
kwargs...,
)
)
g, f = func3d()
scalarplot(g, f; Plotter = Plotter, kwargs...)
scalarplot(g, f; Plotter = Plotter, levels, xplanes, yplanes, zplanes, kwargs...)
end
# ![](plotting_func3d.svg)

Expand Down
4 changes: 2 additions & 2 deletions src/dispatch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ function default_plot_kwargs()
:color => Pair((0.0, 0.0, 0.0), "1D plot line color"),
:cellwise => Pair(false, "1D plots cellwise; unmaintained and can be slow)"),
:label => Pair("", "1D plot label"),
:levels => Pair(5, "array of isolevels or number of isolevels for comtour plots"),
:levels => Pair(5, "array of isolevels or number of isolevels for contour plots"),
:elevation => Pair(0.0, "2D plot height factor for elevation"),
:colorlevels => Pair(51, "2D/3D contour plot: number of color levels"),
:colormap => Pair(
Expand All @@ -337,7 +337,7 @@ function default_plot_kwargs()
),
:outlinealpha => Pair(0.05, "3D outline surface alpha value"),
:levelalpha => Pair(0.25, "3D isolevel alpha"),
:planealpha => Pair(1.0, "3D plane section alpha"),
:planealpha => Pair(0.25, "3D plane section alpha"),
:tetxplane_tol => Pair(0.0, "tolerance for tet-plane intersection in 3D"),
:spacing => Pair(:default, "Spacing of quiver points in vector plot"),
:offset => Pair(:default, "Offset of quiver grid"),
Expand Down
60 changes: 46 additions & 14 deletions src/makie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,8 @@ function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{1}}, grids, parentgrid
ymin = ylimits[1]
ymax = ylimits[2]
else
ext=extrema.(funcs)
(ymin, ymax)=(minimum(first.(ext)),maximum(last.(ext)))
ext = extrema.(funcs)
(ymin, ymax) = (minimum(first.(ext)), maximum(last.(ext)))
end

function update_lines(ctx, newrange)
Expand Down Expand Up @@ -875,7 +875,8 @@ function makescene3d(ctx)
ctx[:figure];
colormap = ctx[:colormap],
colorrange = ctx[:crange],
ticks = ctx[:levels],
ticks = map(d -> d.l, ctx[:data]),
tickformat = "{:.2e}",
width = 15,
ticklabelsize = 0.5 * ctx[:fontsize],
)
Expand All @@ -884,7 +885,8 @@ function makescene3d(ctx)
ctx[:figure];
colormap = ctx[:colormap],
colorrange = ctx[:crange],
ticks = ctx[:levels],
ticks = map(d -> d.l, ctx[:data]),
tickformat = "{:.2e}",
height = 15,
ticklabelsize = 0.5 * ctx[:fontsize],
vertical = false,
Expand Down Expand Up @@ -1091,19 +1093,21 @@ end

# 3d function
function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{3}}, grids, parentgrid, funcs)

levels, crange = isolevels(ctx, funcs)
ctx[:crange] = crange

nan_replacement = 0.5 * (crange[1] + crange[2])
make_mesh(pts, fcs) = Mesh(pts, fcs)

function make_mesh(pts, fcs, vals)
function make_mesh(pts, fcs, vals, alpha)
if length(fcs) > 0
colors = XMakie.Makie.interpolated_getindex.((cmap,), vals, (crange,))
if ctx[:levelalpha] > 0
if alpha < 1
colors = [
RGBA(colors[i].r, colors[i].g, colors[i].b, Float32(ctx[:levelalpha])) for i = 1:length(colors)
]
RGBA(colors[i].r, colors[i].g, colors[i].b, Float32(alpha)) for
i = 1:length(colors)
]
end
GeometryBasics.Mesh(meta(pts; color = colors, normals = normals(pts, fcs)), fcs)
else
Expand Down Expand Up @@ -1133,9 +1137,9 @@ function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{3}}, grids, parentgrid

# adjust_planes()

ctx[:xplanes]=collect(ctx[:xplanes])
ctx[:yplanes]=collect(ctx[:yplanes])
ctx[:zplanes]=collect(ctx[:zplanes])
ctx[:xplanes] = collect(ctx[:xplanes])
ctx[:yplanes] = collect(ctx[:yplanes])
ctx[:zplanes] = collect(ctx[:zplanes])

x = ctx[:xplanes]
y = ctx[:yplanes]
Expand Down Expand Up @@ -1201,12 +1205,29 @@ function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{3}}, grids, parentgrid
end
end

f =
make_planes =
d -> make_mesh(
marching_tetrahedra(
d.g,
d.f,
makeplanes(xyzmin, xyzmax, d.x, d.y, d.z),
[];
primepoints = hcat(xyzmin, xyzmax),
primevalues = crange,
tol = ctx[:tetxplane_tol],
Tp = Point3f,
Tf = GLTriangleFace,
Tv = Float32,
)...,
ctx[:planealpha],
)

make_levels =
d -> make_mesh(
marching_tetrahedra(
d.g,
d.f,
[],
d.l;
primepoints = hcat(xyzmin, xyzmax),
primevalues = crange,
Expand All @@ -1215,15 +1236,26 @@ function scalarplot!(ctx, TP::Type{MakieType}, ::Type{Val{3}}, grids, parentgrid
Tf = GLTriangleFace,
Tv = Float32,
)...,
ctx[:levelalpha],
)



#### Plane sections and isosurfaces
ctx[:mesh] = XMakie.mesh!(
ctx[:planesections] = XMakie.mesh!(
ctx[:scene],
map(f, ctx[:data]);
map(make_planes, ctx[:data]);
backlight = 1.0f0,
transparency = ctx[:planealpha] < 1.0,
)

ctx[:isosurfaces] = XMakie.mesh!(
ctx[:scene],
map(make_levels, ctx[:data]);
backlight = 1.0f0,
transparency = ctx[:levelalpha] < 1.0,
)

#### Interactions
scene_interaction(ctx[:scene].scene, XMakie, [:z, :y, :x, :l, :q]) do delta, key
if key == :x
Expand Down

0 comments on commit ba8f6d1

Please sign in to comment.