Skip to content

Commit 0c950ad

Browse files
committed
Merge branch 'master' into ff/spaces
2 parents 4d75594 + bd315b4 commit 0c950ad

File tree

20 files changed

+751
-564
lines changed

20 files changed

+751
-564
lines changed

CairoMakie/src/infrastructure.jl

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function cairo_draw(screen::Screen, scene::Scene)
1111
Cairo.save(screen.context)
1212
draw_background(screen, scene)
1313

14-
allplots = get_all_plots(scene)
14+
allplots = Makie.collect_atomic_plots(scene; is_atomic_plot = is_cairomakie_atomic_plot)
1515
zvals = Makie.zvalue2d.(allplots)
1616
permute!(allplots, sortperm(zvals))
1717

@@ -23,10 +23,12 @@ function cairo_draw(screen::Screen, scene::Scene)
2323

2424
Cairo.save(screen.context)
2525
for p in allplots
26-
to_value(get(p, :visible, true)) || continue
26+
check_parent_plots(p) do plot
27+
to_value(get(plot, :visible, true))
28+
end || continue
2729
# only prepare for scene when it changes
2830
# this should reduce the number of unnecessary clipping masks etc.
29-
pparent = p.parent::Scene
31+
pparent = Makie.parent_scene(p)
3032
pparent.visible[] || continue
3133
if pparent != last_scene
3234
Cairo.restore(screen.context)
@@ -36,8 +38,8 @@ function cairo_draw(screen::Screen, scene::Scene)
3638
end
3739
Cairo.save(screen.context)
3840

39-
# When a plot is too large to save with a reasonable file size on a vector backend,
40-
# the user can choose to rasterize it when plotting to vector backends, by using the
41+
# When a plot is too large to save with a reasonable file size on a vector backend,
42+
# the user can choose to rasterize it when plotting to vector backends, by using the
4143
# `rasterize` keyword argument. This can be set to a Bool or an Int which describes
4244
# the density of rasterization (in terms of a direct scaling factor.)
4345
# TODO: In future, this can also be set to a Tuple{Module, Int} which describes
@@ -54,12 +56,33 @@ function cairo_draw(screen::Screen, scene::Scene)
5456
return
5557
end
5658

57-
function get_all_plots(scene, plots = AbstractPlot[])
58-
append!(plots, scene.plots)
59-
for c in scene.children
60-
get_all_plots(c, plots)
59+
"""
60+
is_cairomakie_atomic_plot(plot::Combined)::Bool
61+
62+
Returns whether the plot is considered atomic for the CairoMakie backend.
63+
This is overridden for `Poly`, `Band`, and `Tricontourf` so we can apply
64+
CairoMakie can treat them as atomic plots and render them directly.
65+
66+
Plots with children are by default recursed into. This can be overridden
67+
by defining specific dispatches for `is_cairomakie_atomic_plot` for a given plot type.
68+
"""
69+
is_cairomakie_atomic_plot(plot::Combined) = isempty(plot.plots) || to_value(get(plot, :rasterize, false)) != false
70+
71+
"""
72+
check_parent_plots(f, plot::Combined)::Bool
73+
Returns whether the plot's parent tree satisfies the predicate `f`.
74+
`f` must return a `Bool` and take a plot as its only argument.
75+
"""
76+
function check_parent_plots(f, plot::Combined)
77+
if f(plot)
78+
check_parent_plots(f, parent(plot))
79+
else
80+
return false
6181
end
62-
plots
82+
end
83+
84+
function check_parent_plots(f, scene::Scene)
85+
return true
6386
end
6487

6588
function prepare_for_scene(screen::Screen, scene::Scene)

CairoMakie/src/overrides.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ function draw_plot(scene::Scene, screen::Screen, @nospecialize(poly::Poly))
1212
draw_poly(scene, screen, poly, to_value.(poly.input_args)...)
1313
end
1414

15+
# Override `is_cairomakie_atomic_plot` to allow `poly` to remain a unit,
16+
# instead of auto-decomposing in lines and mesh.
17+
is_cairomakie_atomic_plot(plot::Poly) = true
18+
1519
"""
1620
Fallback method for args without special treatment.
1721
"""
@@ -177,6 +181,12 @@ function draw_plot(scene::Scene, screen::Screen,
177181
nothing
178182
end
179183

184+
# Override `is_cairomakie_atomic_plot` to allow this dispatch of `band` to remain a unit,
185+
# instead of auto-decomposing in lines and mesh.
186+
function is_cairomakie_atomic_plot(plot::Band{<:Tuple{<:AbstractVector{<:Point2},<:AbstractVector{<:Point2}}})
187+
return true
188+
end
189+
180190
#################################################################################
181191
# Tricontourf #
182192
# Tricontourf creates many disjoint polygons that are adjacent and form contour #
@@ -207,3 +217,9 @@ function draw_plot(scene::Scene, screen::Screen, tric::Tricontourf)
207217

208218
return
209219
end
220+
221+
# Override `is_cairomakie_atomic_plot` to allow `Tricontourf` to remain a unit,
222+
# instead of auto-decomposing in lines and mesh.
223+
function is_cairomakie_atomic_plot(plot::Tricontourf)
224+
return true
225+
end

GLMakie/assets/shader/lines.geom

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,21 @@ vec3 screen_space(vec4 vertex)
4949
// Manual uv calculation
5050
// - position in screen space (double resolution as generally used)
5151
// - uv with uv.u normalized (0..1), uv.v unnormalized (0..pattern_length)
52-
void emit_vertex(vec3 position, vec2 uv, int index)
52+
void emit_vertex(vec3 position, vec2 uv, int index, float thickness)
5353
{
5454
f_uv = uv;
5555
f_color = g_color[index];
5656
gl_Position = vec4((position.xy / resolution), position.z, 1.0);
5757
f_id = g_id[index];
5858
// linewidth scaling may shrink the effective linewidth
59-
f_thickness = abs(uv.y) - AA_THICKNESS;
59+
f_thickness = thickness;
6060
EmitVertex();
6161
}
62+
// default for miter joins
63+
void emit_vertex(vec3 position, vec2 uv, int index)
64+
{
65+
emit_vertex(position, uv, index, g_thickness[index]);
66+
}
6267

6368
// For center point
6469
void emit_vertex(vec3 position, vec2 uv)
@@ -72,15 +77,20 @@ void emit_vertex(vec3 position, vec2 uv)
7277
}
7378

7479
// Debug
75-
void emit_vertex(vec3 position, vec2 uv, int index, vec4 color)
80+
void emit_vertex(vec3 position, vec2 uv, int index, vec4 color, float thickness)
7681
{
7782
f_uv = uv;
7883
f_color = color;
7984
gl_Position = vec4((position.xy / resolution), position.z, 1.0);
8085
f_id = g_id[index];
81-
f_thickness = abs(uv.y) - AA_THICKNESS;
86+
f_thickness = thickness;
8287
EmitVertex();
8388
}
89+
// default for miter joins
90+
void emit_vertex(vec3 position, vec2 uv , int index, vec4 color)
91+
{
92+
emit_vertex(position, uv , index, color, g_thickness[index]);
93+
}
8494
void emit_vertex(vec3 position, vec2 uv, vec4 color)
8595
{
8696
f_uv = uv;
@@ -98,8 +108,11 @@ void emit_vertex(vec3 position, vec2 offset, vec2 line_dir, vec2 uv, int index)
98108
emit_vertex(
99109
position + vec3(offset, 0),
100110
vec2(uv.x + px2uv * dot(line_dir, offset), uv.y),
101-
index
111+
index,
112+
abs(uv.y) - AA_THICKNESS
102113
);
114+
// `abs(uv.y) - AA_THICKNESS` corrects for enlarged AA padding between
115+
// segments of different linewidth, see #2953
103116
}
104117

105118
void emit_vertex(vec3 position, vec2 offset, vec2 line_dir, vec2 uv)

GLMakie/src/screen.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ mutable struct ScreenConfig
106106
end
107107
end
108108

109-
const LAST_INLINE = Ref{Union{Makie.Automatic, Bool}}(Makie.automatic)
109+
const LAST_INLINE = Ref{Union{Makie.Automatic, Bool}}(false)
110110

111111
"""
112112
GLMakie.activate!(; screen_config...)
@@ -518,7 +518,7 @@ end
518518
function Base.delete!(screen::Screen, scene::Scene, plot::AbstractPlot)
519519
if !isempty(plot.plots)
520520
# this plot consists of children, so we flatten it and delete the children instead
521-
for cplot in Makie.flatten_plots(plot)
521+
for cplot in Makie.collect_atomic_plots(plot)
522522
delete!(screen, scene, cplot)
523523
end
524524
else
@@ -919,7 +919,7 @@ function renderloop(screen)
919919
end
920920

921921
function plot2robjs(screen::Screen, plot)
922-
plots = Makie.flatten_plots(plot)
922+
plots = Makie.collect_atomic_plots(plot)
923923
return map(x-> screen.cache[objectid(x)], plots)
924924
end
925925

NEWS.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
## master
44

5+
- Deprecated `flatten_plots` in favor of `collect_atomic_plots`. Using the new `collect_atomic_plots` fixed a bug in CairoMakie where the z-level of plots within recipes was not respected. [#2793](https://github.com/MakieOrg/Makie.jl/pull/2793)
6+
- Fixed incorrect line depth in GLMakie [#2843](https://github.com/MakieOrg/Makie.jl/pull/2843)
7+
- Fixed incorrect line alpha in dense lines in GLMakie [#2843](https://github.com/MakieOrg/Makie.jl/pull/2843)
58
- Fixed DataInspector interaction with transformations [#3002](https://github.com/MakieOrg/Makie.jl/pull/3002)
9+
- Added option `WGLMakie.activate!(resize_to_body=true)`, to make plots resize to the VSCode plotpane. Resizes to the HTML body element, so may work outside VSCode [#3044](https://github.com/MakieOrg/Makie.jl/pull/3044), [#3042](https://github.com/MakieOrg/Makie.jl/pull/3042).
10+
- Fixed DataInspector interaction with transformations [#3002](https://github.com/MakieOrg/Makie.jl/pull/3002).
611
- Fix incomplete stroke with some Bezier markers in CairoMakie and blurry strokes in GLMakie [#2961](https://github.com/MakieOrg/Makie.jl/pull/2961)
712
- Added the ability to use custom triangulations from DelaunayTriangulation.jl [#2896](https://github.com/MakieOrg/Makie.jl/pull/2896).
813
- Adjusted scaling of scatter/text stroke, glow and anti-aliasing width under non-uniform 2D scaling (Vec2f markersize/fontsize) in GLMakie [#2950](https://github.com/MakieOrg/Makie.jl/pull/2950).
914
- Scaled `errorbar` whiskers and `bracket` correctly with transformations [#3012](https://github.com/MakieOrg/Makie.jl/pull/3012).
1015
- Updated `bracket` when the screen is resized or transformations change [#3012](https://github.com/MakieOrg/Makie.jl/pull/3012).
11-
16+
- Added auto-resizing functionality to WGLMakie plot figures [#3042](https://github.com/MakieOrg/Makie.jl/pull/3042)
1217

1318
## v0.19.6
1419

ReferenceTests/src/tests/examples2d.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,3 +1019,28 @@ end
10191019
end
10201020
f
10211021
end
1022+
1023+
@reference_test "Z-translation within a recipe" begin
1024+
# This is testing whether backends respect the
1025+
# z-level of plots within recipes in 2d.
1026+
# Ideally, the output of this test
1027+
# would be a blue line with red scatter markers.
1028+
# However, if a backend does not correctly pick up on translations,
1029+
# then this will be drawn in the drawing order, and blue
1030+
# will completely obscure red.
1031+
1032+
# It seems like we can't define recipes in `@reference_test` yet,
1033+
# so we'll have to fake a recipe's structure.
1034+
1035+
fig = Figure(resolution = (600, 600))
1036+
# Create a recipe plot
1037+
ax, plot_top = heatmap(fig[1, 1], randn(10, 10))
1038+
# Plot some recipes at the level below the contour
1039+
scatterlineplot_1 = scatterlines!(plot_top, 1:10, 1:10; linewidth = 20, markersize = 20, color = :red)
1040+
scatterlineplot_2 = scatterlines!(plot_top, 1:10, 1:10; linewidth = 20, markersize = 30, color = :blue)
1041+
# Translate the lowest level plots (scatters)
1042+
translate!(scatterlineplot_1.plots[2], 0, 0, 1)
1043+
translate!(scatterlineplot_2.plots[2], 0, 0, -1)
1044+
# Display
1045+
fig
1046+
end

WGLMakie/src/Camera.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,23 @@ export function attach_3d_camera(canvas, makie_camera, cam3d, scene) {
5555
camera.up = new THREE.Vector3(...cam3d.upvector);
5656
camera.position.set(...cam3d.eyeposition);
5757
camera.lookAt(center);
58-
5958
function update() {
60-
camera.updateProjectionMatrix();
61-
camera.updateWorldMatrix();
6259
const view = camera.matrixWorldInverse;
6360
const projection = camera.projectionMatrix;
64-
const [width, height] = makie_camera.resolution.value;
61+
const [width, height] = cam3d.resolution.value;
6562
const [x, y, z] = camera.position;
63+
camera.aspect = width / height;
64+
camera.updateProjectionMatrix();
65+
camera.updateWorldMatrix();
66+
6667
makie_camera.update_matrices(
6768
view.elements,
6869
projection.elements,
6970
[width, height],
7071
[x, y, z]
71-
);
72+
);
7273
}
74+
cam3d.resolution.on(update);
7375

7476
function addMouseHandler(domObject, drag, zoomIn, zoomOut) {
7577
let startDragX = null;

WGLMakie/src/display.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const WEB_MIMES = (
4646
"""
4747
struct ScreenConfig
4848
framerate::Float64 # =30.0
49+
resize_to_body::Bool # false
4950
end
5051

5152
"""
@@ -205,7 +206,7 @@ function session2image(session::Session, scene::Scene)
205206
to_data = js"""function (){
206207
return $(scene).then(scene => {
207208
const {renderer} = scene.screen
208-
WGLMakie.render_scene(scene)
209+
WGL.render_scene(scene)
209210
const img = renderer.domElement.toDataURL()
210211
return img
211212
})
@@ -342,7 +343,7 @@ const DISABLE_JS_FINALZING = Base.RefValue(false)
342343
const DELETE_QUEUE = LockfreeQueue{Tuple{Screen,String,Vector{String}}}(delete_plot!)
343344

344345
function Base.delete!(screen::Screen, scene::Scene, plot::Combined)
345-
atomics = Makie.flatten_plots(plot) # delete all atomics
346+
atomics = Makie.collect_atomic_plots(plot) # delete all atomics
346347
# only queue atomics to actually delete on js
347348
if !DISABLE_JS_FINALZING[]
348349
push!(DELETE_QUEUE, (screen, js_uuid(scene), js_uuid.(atomics)))

WGLMakie/src/events.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ function connect_scene_events!(scene::Scene, comm::Observable)
102102
e.keyboardbutton[] = KeyEvent(code_to_keyboard(keyup), Keyboard.release)
103103
end
104104
end
105+
@handle msg.resize begin
106+
resize!(scene, tuple(resize...))
107+
end
105108
catch err
106109
@warn "Error in window event callback" exception=(err, Base.catch_backtrace())
107110
end

WGLMakie/src/picking.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function pick_native(screen::Screen, rect::Rect2i)
1717
if isempty(matrix)
1818
return empty
1919
else
20-
all_children = Makie.flatten_plots(scene)
20+
all_children = Makie.collect_atomic_plots(scene)
2121
lookup = Dict(Pair.(js_uuid.(all_children), all_children))
2222
return map(matrix) do (uuid, index)
2323
!haskey(lookup, uuid) && return (nothing, 0)
@@ -27,7 +27,7 @@ function pick_native(screen::Screen, rect::Rect2i)
2727
end
2828

2929
function plot_lookup(scene::Scene)
30-
all_plots = Makie.flatten_plots(scene)
30+
all_plots = Makie.collect_atomic_plots(scene)
3131
return Dict(Pair.(js_uuid.(all_plots), all_plots))
3232
end
3333

@@ -93,7 +93,7 @@ App() do session
9393
}
9494
\"\"\"
9595
96-
tooltip = WGLMakie.ToolTip(f, on_click_callback; plots=pl)
96+
tooltip = WGL.ToolTip(f, on_click_callback; plots=pl)
9797
return DOM.div(f, tooltip)
9898
end
9999
```
@@ -107,7 +107,7 @@ struct ToolTip
107107
if isnothing(plots)
108108
plots = scene.plots
109109
end
110-
all_plots = WGLMakie.js_uuid.(filter!(x-> x.inspectable[], Makie.flatten_plots(plots)))
110+
all_plots = js_uuid.(filter!(x-> x.inspectable[], Makie.collect_atomic_plots(plots)))
111111
new(scene, callback, all_plots)
112112
end
113113
end

0 commit comments

Comments
 (0)