From 59e0094c15d23b748e3a0122fc2db2c899588c7d Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Thu, 20 Jun 2024 15:43:27 -0400 Subject: [PATCH] Update the meshimage recipe to the new syntax --- src/GeoMakie.jl | 1 + src/mesh_image.jl | 27 +++++++++++++++------------ test/meshimage.jl | 12 ++++++++++++ test/runtests.jl | 41 ++++------------------------------------- 4 files changed, 32 insertions(+), 49 deletions(-) create mode 100644 test/meshimage.jl diff --git a/src/GeoMakie.jl b/src/GeoMakie.jl index 2999fc1c..52d42e7c 100644 --- a/src/GeoMakie.jl +++ b/src/GeoMakie.jl @@ -14,6 +14,7 @@ using Reexport using GeometryBasics, Colors, ImageIO using Makie +using Makie.MakieCore import Makie: convert_arguments, convert_attribute, to_value, automatic using Makie, Makie.FileIO, Makie.GridLayoutBase, Makie.DocStringExtensions diff --git a/src/mesh_image.jl b/src/mesh_image.jl index 4cb1faf1..9bb4dd69 100644 --- a/src/mesh_image.jl +++ b/src/mesh_image.jl @@ -22,19 +22,20 @@ the provided image. Its conversion trait is `Image` (TODO: change this to Discr For now, it only accepts RGB images. This could be changed in the future. -## Attributes -$(Makie.ATTRIBUTES) """ -@recipe(MeshImage) do scene - Attributes( - npoints = 100, - space = :data, - ) +@recipe MeshImage (x, y, img) begin + """ + The number of points the mesh should have per side. + Can be an Integer or a 2-tuple of integers representing number of points per side. + """ + npoints = 100 + MakieCore.mixin_generic_plot_attributes()... + MakieCore.mixin_colormap_attributes()... end # this inherits all conversions for `Image`, # if no specialized conversion for `MeshImage` is found. -Makie.conversion_trait(::Type{<: MeshImage}) = ImageLike() +Makie.conversion_trait(::Type{<: MeshImage}) = Makie.ImageLike() # There really is no difference between this and Image, # except the implementation under the hood. @@ -58,9 +59,9 @@ function Makie.plot!(plot::MeshImage) if npoints != old_npoints[] # We need a new StructArray to hold all the points. # TODO: resize the old structarray instead! - points_observable.val = Vector{Point2f}(undef, npoints^2) + points_observable.val = Vector{Point2f}(undef, first(npoints) * last(npoints)) # This constructs an efficient triangulation of a rectangle (all images are rectangles). - rect = GeometryBasics.Tesselation(Rect2f(0, 0, 1, 1), (npoints, npoints)) + rect = GeometryBasics.Tesselation(Rect2f(0, 0, 1, 1), (first(npoints), last(npoints))) # This decomposes that Tesselation to actual triangles, with integer index values. faces_observable.val = GeometryBasics.decompose(Makie.GLTriangleFace, rect) # This holds the UVs for the mesh. These are reversed, so that the image is plotted correctly. @@ -68,8 +69,9 @@ function Makie.plot!(plot::MeshImage) end # These are the ranges for the points. - xs = LinRange(extrema(x_in)..., npoints) - ys = LinRange(extrema(y_in)..., npoints) + # `first` and `last` are used to get the number of points per side, if that's provided as a tuple. + xs = LinRange(extrema(x_in)..., first(npoints)) + ys = LinRange(extrema(y_in)..., last(npoints)) poval = points_observable[] # The array is in a grid, so we have to update them on a grid as well. for (linear_ind, cartesian_ind) in enumerate(CartesianIndices((npoints, npoints))) @@ -100,6 +102,7 @@ function Makie.plot!(plot::MeshImage) plot, final_mesh; color = plot[3], + MakieCore.colormap_attributes(plot)..., shading = NoShading, transformation = Transformation() # since the points are pre transformed, we don't need to transform them again ) diff --git a/test/meshimage.jl b/test/meshimage.jl new file mode 100644 index 00000000..837e9cdd --- /dev/null +++ b/test/meshimage.jl @@ -0,0 +1,12 @@ +using Test +using Makie, GeoMakie + +@testset "Meshimage color attribute passthrough" begin + img = rand(10, 10) + f, a, p = meshimage(0..1, 0..1, img) + @test p.colormap[] == :viridis + @test p.plots[1].colormap[] == :viridis + p.colormap[] = :jet + @test p.colormap[] == :jet + @test p.plots[1].colormap[] == :jet +end diff --git a/test/runtests.jl b/test/runtests.jl index 3a68e8ac..1decfa3d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -7,6 +7,10 @@ Makie.set_theme!(Theme( )) @testset "GeoMakie" begin + @testset "MeshImage" begin + include("meshimage.jl") + end + @testset "Basics" begin lons = -180:180 lats = -90:90 @@ -30,41 +34,4 @@ Makie.set_theme!(Theme( @test GeoMakie.coastlines(ga) isa Observable @test GeoMakie.coastlines(ga)[][1] isa GeometryBasics.LineString end - - # @testset "Examples" begin - # geomakie_path = dirname(dirname(pathof(GeoMakie))) - # examples = readdir(joinpath(geomakie_path, "examples"); join = true) - # filenames = filter(x-> isfile(x) && endswith(x, ".jl"), examples) - - # test_path = mkpath(joinpath(geomakie_path, "test_images")) - # cd(test_path) do - # for filename in filenames - # example_name = splitext(splitdir(filename)[2])[1] - # printstyled("Running ", bold = true, color = :cyan) - # println(example_name) - - # @testset "$example_name" begin - # @test begin - # print(rpad("Include: ", 9)) - # @time include(filename) - # true - # end - # @test begin - # savepath = "$example_name.png" - # print(rpad("PNG: ", 9)) - # @time CairoMakie.save(savepath, Makie.current_figure(); px_per_unit=2); - # isfile(savepath) && filesize(savepath) > 1000 - - # end - # @test begin - # savepath = "$example_name.pdf" - # print(rpad("PDF: ", 9)) - # @time CairoMakie.save(savepath, Makie.current_figure()); - # isfile(savepath) && filesize(savepath) > 1000 - # end - # haskey(ENV, "CI") && rm("$example_name.pdf") - # end - # end - # end - # end end