diff --git a/src/basic_recipes/contourf.jl b/src/basic_recipes/contourf.jl index e20ec2307..7bf7273fd 100644 --- a/src/basic_recipes/contourf.jl +++ b/src/basic_recipes/contourf.jl @@ -46,8 +46,14 @@ function AbstractPlotting.plot!(c::Contourf{<:Tuple{Any, Any, Any}}) _get_isoband_levels(levels, extrema_nan(zs)...) end + PolyType = typeof(Polygon(Point2f0[], [Point2f0[]])) - poly_and_colors = lift(xs, ys, zs, levels) do xs, ys, zs, levels + polys = Observable(PolyType[]) + colors = Observable(Float32[]) + + function calculate_polys(xs, ys, zs, levels) + empty!(polys[]) + empty!(colors[]) @assert levels isa Tuple lows, highs = levels isos = Isoband.isobands(xs, ys, zs, lows, highs) @@ -55,47 +61,33 @@ function AbstractPlotting.plot!(c::Contourf{<:Tuple{Any, Any, Any}}) allvertices = Point2f0[] allfaces = NgonFace{3,OffsetInteger{-1,UInt32}}[] allids = Int[] - - # TODO: this is ugly - polys = Vector{typeof(Polygon(rand(Point2f0, 3), [rand(Point2f0, 3)]))}() - colors = Float32[] - levelcenters = (highs .+ lows) ./ 2 - - foreach(zip(levelcenters, isos)) do (center, group) - + for (center, group) in zip(levelcenters, isos) points = Point2f0.(group.x, group.y) polygroups = _group_polys(points, group.id) - for polygroup in polygroups - outline = polygroup[1] holes = polygroup[2:end] - - poly = GeometryBasics.Polygon(outline, holes) - - push!(polys, poly) + push!(polys[], GeometryBasics.Polygon(outline, holes)) # use contour level center value as color - push!(colors, center) + push!(colors[], center) end - end - - polys, colors + polys[] = polys[] + return end - polys = @lift($poly_and_colors[1]) - colors = @lift($poly_and_colors[2]) + onany(calculate_polys, xs, ys, zs, levels) + # onany doesn't get called without a push, so we call + # it on a first run! + calculate_polys(xs[], ys[], zs[], levels[]) - poly!(c, + mesh!(c, polys, colormap = c.colormap, colorrange = c.colorrange, - strokewidth = 0, - strokecolor = :transparent, - color = colors) - - c + color = colors, + shading=false) end """ @@ -166,9 +158,9 @@ function _group_polys(points, ids) to_keep[ii] = false end end - + unclassified_polyindices = unclassified_polyindices[to_keep] containment_matrix = containment_matrix[to_keep, to_keep] end groups -end \ No newline at end of file +end diff --git a/src/basic_recipes/poly.jl b/src/basic_recipes/poly.jl index 9fbbf6887..cb9a888e4 100644 --- a/src/basic_recipes/poly.jl +++ b/src/basic_recipes/poly.jl @@ -138,8 +138,6 @@ function plot!(plot::Mesh{<: Tuple{<: AbstractVector{P}}}) where P <: Union{Abst bigmesh = if color_node[] isa AbstractVector && length(color_node[]) == length(meshes[]) # One color per mesh - real_colors = Observable(RGBAf0[]) - attributes[:color] = real_colors lift(meshes, color_node, attributes.colormap, attributes.colorrange) do meshes, colors, cmap, crange # Color are reals, so we need to transform it to colors first single_colors = if colors isa AbstractVector{<:Number} @@ -147,17 +145,16 @@ function plot!(plot::Mesh{<: Tuple{<: AbstractVector{P}}}) where P <: Union{Abst else to_color.(colors) end - empty!(real_colors[]) - + real_colors = RGBAf0[] # Map one single color per mesh to each vertex for (mesh, color) in zip(meshes, single_colors) - append!(real_colors[], Iterators.repeated(RGBAf0(color), length(coordinates(mesh)))) + append!(real_colors, Iterators.repeated(RGBAf0(color), length(coordinates(mesh)))) end - real_colors[] = real_colors[] + # real_colors[] = real_colors[] if P <: AbstractPolygon meshes = triangle_mesh.(meshes) end - return merge(meshes) + return pointmeta(merge(meshes), color=real_colors) end else attributes[:color] = color_node diff --git a/test/ReferenceTests/src/ReferenceTests.jl b/test/ReferenceTests/src/ReferenceTests.jl index 05344fac9..ae20e0d0a 100644 --- a/test/ReferenceTests/src/ReferenceTests.jl +++ b/test/ReferenceTests/src/ReferenceTests.jl @@ -15,6 +15,7 @@ using ghr_jll using Tar using Downloads using Pkg.TOML +using Statistics basedir(files...) = normpath(joinpath(@__DIR__, "..", files...)) @@ -29,5 +30,6 @@ include("database.jl") include("stable_rng.jl") include("runtests.jl") include("image_download.jl") +include("html_rendering.jl") end diff --git a/test/ReferenceTests/src/html_rendering.jl b/test/ReferenceTests/src/html_rendering.jl index ffa1fbef3..38c2f920e 100644 --- a/test/ReferenceTests/src/html_rendering.jl +++ b/test/ReferenceTests/src/html_rendering.jl @@ -1,19 +1,42 @@ """ Embedds all produced media in one big html file """ -function generate_preview(media_root, path=joinpath(@__DIR__, "preview.html")) +function generate_preview(path=joinpath(@__DIR__, "preview.html"); media_root=basedir("recorded")) open(path, "w") do io - for folder in readdir(media_root) - media = joinpath(media_root, folder, "media") - if !isfile(media) && ispath(media) + for file in readdir(media_root) + media = joinpath(media_root, file) + println(io, "
diff --git a/test/ReferenceTests/src/runtests.jl b/test/ReferenceTests/src/runtests.jl index a88149aef..f13df8c23 100644 --- a/test/ReferenceTests/src/runtests.jl +++ b/test/ReferenceTests/src/runtests.jl @@ -69,6 +69,7 @@ function reference_tests(recorded; ref_images = ReferenceTests.download_refimage @testset "$name" for (name, score) in scores @test score < difference end + return recorded, ref_images, scores end end @@ -100,7 +101,7 @@ function record_tests(db=load_database(); recording_dir=joinpath(@__DIR__, "..", return recorded_files, recording_dir end -function run_tests(db=load_database(); ref_images = ReferenceTests.download_refimages(), +function run_tests(db=load_database(); ref_images = ReferenceTests.download_refimages(), recording_dir=joinpath(@__DIR__, "..", "recorded"), difference=0.03) files, dir = record_tests(db, recording_dir=recording_dir) reference_tests(recording_dir, ref_images=ref_images, difference=difference) diff --git a/test/ReferenceTests/src/tests/examples2d.jl b/test/ReferenceTests/src/tests/examples2d.jl index b75c51f68..ea9ec2645 100644 --- a/test/ReferenceTests/src/tests/examples2d.jl +++ b/test/ReferenceTests/src/tests/examples2d.jl @@ -225,13 +225,13 @@ end end end -let +let struct FitzhughNagumo2 end (()-> FitzhughNagumo2())() end -let +let struct FitzhughNagumo{T} ϵ::T s::T @@ -343,3 +343,8 @@ end pie!(ax, 0.1:0.1:1.0, normalize=false) scene end + +@cell "intersecting polygon" begin + x = LinRange(0, 2pi, 100) + poly(Point2f0.(zip(sin.(x), sin.(2x))), color = :white, strokecolor = :blue, strokewidth = 10) +end