Skip to content

Commit

Permalink
fix contour label - transform interaction (#3083)
Browse files Browse the repository at this point in the history
* fix contour label  - transform interaction

* add NEWS entry

* add refimg test

---------

Co-authored-by: Simon <sdanisch@protonmail.com>
  • Loading branch information
ffreyer and SimonDanisch authored Jul 31, 2023
1 parent cdbb746 commit f3bb1c8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions ReferenceTests/src/tests/examples2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
11 changes: 7 additions & 4 deletions src/basic_recipes/contours.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 * π
Expand All @@ -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

Expand All @@ -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
Expand Down

0 comments on commit f3bb1c8

Please sign in to comment.