Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CairoMakie heatmap cell sizes #3625

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions CairoMakie/src/primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -771,27 +771,35 @@ function draw_atomic(scene::Scene, screen::Screen, @nospecialize(primitive::Unio
end
end

function _draw_rect_heatmap(ctx, xys, ni, nj, colors)
function _draw_rect_heatmap(ctx, xys, ni, nj, colors, margin_factor=0.05f0)
@inbounds for i in 1:ni, j in 1:nj
p1 = xys[i, j]
p2 = xys[i+1, j]
p3 = xys[i+1, j+1]
p4 = xys[i, j+1]

# Rectangles and polygons that are directly adjacent usually show
# white lines between them due to anti aliasing. To avoid this we
# increase their size slightly.

if alpha(colors[i, j]) == 1 && margin_factor > 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if using a transparent colormap here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem mostly goes away, but when you set alpha=0.99 for example, there are still some cases where artifacts occur. For example thin lines that shouldn't be there.

# dx and dy are the margin widths that are added to the
# heatmap cell sizes further down
# adding 5% seems like a good compromise between avoiding artifacts
# and not changing pixel sizes too much
# how much is added can be controlled via margin_factor
dx = abs(p2[1] - p1[1]) * 0.5f0 * margin_factor
dy = abs(p4[2] - p1[2]) * 0.5f0 * margin_factor

if alpha(colors[i, j]) == 1
# sign.(p - center) gives the direction in which we need to
# extend the polygon. (Which may change due to rotations in the
# model matrix.) (i!=1) etc is used to avoid increasing the
# outer extent of the heatmap.
center = 0.25f0 * (p1 + p2 + p3 + p4)
p1 += sign.(p1 - center) .* Point2f(0.5f0 * (i!=1), 0.5f0 * (j!=1))
p2 += sign.(p2 - center) .* Point2f(0.5f0 * (i!=ni), 0.5f0 * (j!=1))
p3 += sign.(p3 - center) .* Point2f(0.5f0 * (i!=ni), 0.5f0 * (j!=nj))
p4 += sign.(p4 - center) .* Point2f(0.5f0 * (i!=1), 0.5f0 * (j!=nj))
p1 += sign.(p1 - center) .* Point2f(dx * (i!=1), dy * (j!=1))
p2 += sign.(p2 - center) .* Point2f(dx * (i!=ni), dy * (j!=1))
p3 += sign.(p3 - center) .* Point2f(dx * (i!=ni), dy * (j!=nj))
p4 += sign.(p4 - center) .* Point2f(dx * (i!=1), dy * (j!=nj))
end

Cairo.set_line_width(ctx, 0)
Expand Down
Loading