From f3bb1c8f7cd8f94688303ac6e797478865994b4e Mon Sep 17 00:00:00 2001 From: Frederic Freyer Date: Mon, 31 Jul 2023 15:06:39 +0200 Subject: [PATCH] fix contour label - transform interaction (#3083) * fix contour label - transform interaction * add NEWS entry * add refimg test --------- Co-authored-by: Simon --- NEWS.md | 1 + ReferenceTests/src/tests/examples2d.jl | 10 ++++++++++ src/basic_recipes/contours.jl | 11 +++++++---- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index ba55a509260..a78869301dc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,7 @@ ## master +- Fixed incorrect placement of contourlabels with transform functions [#3083](https://github.com/MakieOrg/Makie.jl/pull/3083) - Fix automatic normal generation for meshes with shading and no normals [#3041](https://github.com/MakieOrg/Makie.jl/pull/3041). ## v0.19.7 diff --git a/ReferenceTests/src/tests/examples2d.jl b/ReferenceTests/src/tests/examples2d.jl index f79c4416ae7..81709161878 100644 --- a/ReferenceTests/src/tests/examples2d.jl +++ b/ReferenceTests/src/tests/examples2d.jl @@ -807,6 +807,16 @@ end fig end +@reference_test "contour labels with transform_func" begin + f = Figure(resolution = (400, 400)) + a = Axis(f[1, 1], xscale = log10) + xs = 10 .^ range(0, 3, length=101) + ys = range(1, 4, length=101) + zs = [sqrt(x*x + y*y) for x in -50:50, y in -50:50] + contour!(a, xs, ys, zs, labels = true, labelsize = 20) + f +end + @reference_test "contour labels 3D" begin fig = Figure() Axis3(fig[1, 1]) diff --git a/src/basic_recipes/contours.jl b/src/basic_recipes/contours.jl index adab837bd3b..ed02afc1e69 100644 --- a/src/basic_recipes/contours.jl +++ b/src/basic_recipes/contours.jl @@ -257,7 +257,9 @@ function plot!(plot::T) where T <: Union{Contour, Contour3d} col = texts.color.val; empty!(col) lbl = texts.text.val; empty!(lbl) for (lev, (p1, p2, p3), color) in lev_pos_col - rot_from_horz::Float32 = angle(project(scene, p1), project(scene, p3)) + px_pos1 = project(scene, apply_transform(transform_func(plot), p1, space)) + px_pos3 = project(scene, apply_transform(transform_func(plot), p3, space)) + rot_from_horz::Float32 = angle(px_pos1, px_pos3) # transition from an angle from horizontal axis in [-π; π] # to a readable text with a rotation from vertical axis in [-π / 2; π / 2] rot_from_vert::Float32 = if abs(rot_from_horz) > 0.5f0 * π @@ -278,7 +280,8 @@ function plot!(plot::T) where T <: Union{Contour, Contour3d} labels || return return broadcast(texts.plots[1][1].val, texts.positions.val, texts.rotation.val) do gc, pt, rot # drop the depth component of the bounding box for 3D - Rect2f(boundingbox(gc, project(scene.camera, space, :pixel, pt), to_rotation(rot))) + px_pos = project(scene, apply_transform(transform_func(plot), pt, space)) + Rect2f(boundingbox(gc, to_ndim(Point3f, px_pos, 0f0), to_rotation(rot))) end end @@ -293,14 +296,14 @@ function plot!(plot::T) where T <: Union{Contour, Contour3d} if isnan(p) && n < nlab bb = bboxes[n += 1] # next segment is materialized by a NaN, thus consider next label # wireframe!(plot, bb, space = :pixel) # toggle to debug labels - elseif project(scene.camera, space, :pixel, p) in bb + elseif project(scene, apply_transform(transform_func(plot), p, space)) in bb masked[i] = nan for dir in (-1, +1) j = i while true j += dir checkbounds(Bool, segments, j) || break - project(scene.camera, space, :pixel, segments[j]) in bb || break + project(scene, apply_transform(transform_func(plot), segments[j], space)) in bb || break masked[j] = nan end end