Skip to content

Commit

Permalink
Merge pull request #1100 from JuliaPlots/sd/cm-heatmap
Browse files Browse the repository at this point in the history
fix cairomakie reversed heatmap +clean up tests
  • Loading branch information
SimonDanisch authored Jul 9, 2021
2 parents ff728cc + 22dab9f commit 3d0be44
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 38 deletions.
13 changes: 8 additions & 5 deletions CairoMakie/src/primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -483,23 +483,21 @@ function draw_atomic(scene::Scene, screen::CairoScreen, primitive::Union{Heatmap
image = primitive[3][]
xs, ys = primitive[1][], primitive[2][]

if !(xs isa Vector)
if !(xs isa AbstractVector)
l, r = extrema(xs)
N = size(image, 1)
xs = range(l, r, length = N+1)
else
xs = regularly_spaced_array_to_range(xs)
end
if !(ys isa Vector)
if !(ys isa AbstractVector)
l, r = extrema(ys)
N = size(image, 2)
ys = range(l, r, length = N+1)
else
ys = regularly_spaced_array_to_range(ys)
end
model = primitive[:model][]
imsize = (extrema_nan(xs), extrema_nan(ys))

interp = to_value(get(primitive, :interpolate, true))
weird_cairo_limit = (2^15) - 23

Expand All @@ -508,7 +506,11 @@ function draw_atomic(scene::Scene, screen::CairoScreen, primitive::Union{Heatmap
fast_path = to_value(get(primitive, :fast_path, true))
# Vector backends don't support FILTER_NEAREST for interp == false, so in that case we also need to draw rects
is_vector = is_vector_backend(ctx)
if fast_path && xs isa AbstractRange && ys isa AbstractRange && !(is_vector && !interp)
t = Makie.transform_func_obs(primitive)[]
identity_transform = t === identity || t isa Tuple && all(x-> x === identity, t)
if fast_path && xs isa AbstractRange && ys isa AbstractRange && !(is_vector && !interp) && identity_transform
imsize = ((first(xs), last(xs)), (first(ys), last(ys)))

# find projected image corners
# this already takes care of flipping the image to correct cairo orientation
xy = project_position(scene, Point2f0(first.(imsize)), model)
Expand All @@ -522,6 +524,7 @@ function draw_atomic(scene::Scene, screen::CairoScreen, primitive::Union{Heatmap
if s.width > weird_cairo_limit || s.height > weird_cairo_limit
error("Cairo stops rendering images bigger than $(weird_cairo_limit), which is likely a bug in Cairo. Please resample your image/heatmap with e.g. `ImageTransformations.imresize`")
end

Cairo.rectangle(ctx, xy..., w, h)
Cairo.save(ctx)
Cairo.translate(ctx, xy...)
Expand Down
1 change: 1 addition & 0 deletions ReferenceTests/src/ReferenceTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ using Tar
using Downloads
using Pkg.TOML
using Statistics
using ImageShow

basedir(files...) = normpath(joinpath(@__DIR__, "..", files...))
loadasset(files...) = FileIO.load(assetpath(files...))
Expand Down
10 changes: 3 additions & 7 deletions ReferenceTests/src/html_rendering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,17 @@ function get_medias(path, result=[])
return result
end

using ImageShow

function generate_test_summary(path, recorded_root, refimages_root, scores)
open(path, "w") do io
scores_sorted = sort!(collect(scores), by=last, rev=true)
for (filename, score) in scores_sorted
media_ref = get_medias(joinpath(refimages_root, filename))
media_recorded = get_medias(joinpath(recorded_root, filename))
println(io, "<h1> $filename : $(round(score, digits=4)) </h1>")
println(io, "<h1> $filename [overlay, reference, recorded] : $(round(score, digits=4)) </h1>")
for (ref, rec) in zip(media_ref, media_recorded)
diff = (ref .- rec) ./ 2.0

diff2 = (ref .* 0.5) .+ (rec .* 0.5)
diff = (ref .* 0.5) .+ (rec .* 0.5)
ctx = IOContext(io, :thumbnailsize=> size(diff), :thumbnail => false)
show(ctx, "text/html", [diff, diff2])
show(ctx, "text/html", [diff, ref, rec])
end
end
end
Expand Down
16 changes: 0 additions & 16 deletions ReferenceTests/src/tests/examples3d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,6 @@ end
wireframe(r, r, z)
end

@cell "Surface" begin
N = 30
function xy_data(x, y)
r = sqrt(x^2 + y^2)
r == 0.0 ? 1f0 : (sin(r) / r)
end
lspace = range(-10, stop=10, length=N)
z = Float32[xy_data(x, y) for x in lspace, y in lspace]
r = range(0, stop=3, length=N)
surface(
r, r, z,
colormap=:Spectral
)
end

@cell "Surface with image" begin
N = 30
function xy_data(x, y)
Expand Down Expand Up @@ -189,7 +174,6 @@ end
contour3d(r, r, (x, y) -> xy_data(10x, 10y), levels=20, linewidth=3)
end


@cell "Arrows 3D" begin
function SphericalToCartesian(r::T, θ::T, ϕ::T) where T <: AbstractArray
x = @.r * sin(θ) * cos(ϕ)
Expand Down
12 changes: 8 additions & 4 deletions ReferenceTests/src/tests/short_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ end
fig
end


@cell meshscatter(RNG.rand(10), RNG.rand(10), RNG.rand(10), color=RNG.rand(10))
@cell meshscatter(RNG.rand(10), RNG.rand(10), RNG.rand(10), color=RNG.rand(RGBAf0, 10))


@cell begin
s1 = uv_mesh(Sphere(Point3f0(0), 1f0))
mesh(uv_mesh(Sphere(Point3f0(0), 1f0)), color=RNG.rand(50, 50))
Expand All @@ -81,7 +79,7 @@ end

@cell "Unequal x and y sizes in surface" begin
#
NL = 30
NL = 15
NR = 31
function xy_data(x, y)
r = sqrt(x^2 + y^2)
Expand Down Expand Up @@ -154,5 +152,11 @@ end
end

@cell "log10 heatmap" begin
heatmap(rand(10, 5), axis = (yscale = log10, xscale=log10))
heatmap(RNG.rand(10, 5), axis = (yscale = log10, xscale=log10))
end

@cell "reverse range heatmap" begin
x = [1 0
2 3]
heatmap(1:2, 1:-1:0, x)
end
17 changes: 11 additions & 6 deletions WGLMakie/src/imagelike.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,22 @@ function draw_mesh(mscene::Scene, mesh, plot; uniforms...)
return Program(WebGL(), lasset("mesh.vert"), lasset("mesh.frag"), mesh; uniforms...)
end

_length(x::Makie.IntervalSets.AbstractInterval) = 2
_length(x) = length(x)
xy_convert(x::AbstractArray{Float32}, n) = copy(x)
xy_convert(x::AbstractArray, n) = el32convert(x)
xy_convert(x, n) = Float32[LinRange(extrema(x)..., n + 1);]

function limits_to_uvmesh(plot)
px, py, pz = plot[1], plot[2], plot[3]
px = map((x, z)-> xy_convert(x, size(z, 1)), px, pz)
py = map((y, z)-> xy_convert(y, size(z, 2)), py, pz)
# Special path for ranges of length 2 wich
# can be displayed as a rectangle
if _length(px[]) == 2 && _length(py[]) == 2
t = Makie.transform_func_obs(plot)[]
identity_transform = t === identity || t isa Tuple && all(x-> x === identity, t)
if length(px[]) == 2 && length(py[]) == 2 && identity_transform
rect = lift(px, py) do x, y
xmin, xmax = extrema(x)
ymin, ymax = extrema(y)
xmin, xmax = x
ymin, ymax = y
return Rect2D(xmin, ymin, xmax - xmin, ymax - ymin)
end
positions = Buffer(lift(rect-> decompose(Point2f0, rect), rect))
Expand All @@ -55,7 +60,7 @@ function limits_to_uvmesh(plot)
return vec(g)
end
rect = lift(z -> Tesselation(Rect2D(0f0, 0f0, 1f0, 1f0), size(z) .+ 1), pz)
positions = Buffer(lift(grid, px, py, pz, transform_func_obs(plot)))
positions = Buffer(lift(grid, px, py, pz, t))
faces = Buffer(lift(r -> decompose(GLTriangleFace, r), rect))
uv = Buffer(lift(decompose_uv, rect))
end
Expand Down

0 comments on commit 3d0be44

Please sign in to comment.