From 5c0eb89c89f6724f1985455dc44b16f3507eac7c Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Tue, 14 Mar 2023 07:45:28 +0530 Subject: [PATCH 1/5] Make array handling in converts as generic as possible --- src/conversions.jl | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/conversions.jl b/src/conversions.jl index 33597627f56..2bb06b1ad7c 100644 --- a/src/conversions.jl +++ b/src/conversions.jl @@ -300,12 +300,12 @@ end function edges(v::AbstractVector) l = length(v) if l == 1 - return [v[1] - 0.5, v[1] + 0.5] + return [v[begin] - 0.5, v[begin] + 0.5] else # Equivalent to # mids = 0.5 .* (v[1:end-1] .+ v[2:end]) # borders = [2v[1] - mids[1]; mids; 2v[end] - mids[end]] - borders = [0.5 * (v[max(1, i)] + v[min(end, i+1)]) for i in 0:length(v)] + borders = [0.5 * (v[max(begin, i)] + v[min(end, i+1)]) for i in (firstindex(v) - 1):lastindex(v)] borders[1] = 2borders[1] - borders[2] borders[end] = 2borders[end] - borders[end-1] return borders @@ -359,12 +359,11 @@ and stores the `ClosedInterval` to `n` and `m`, plus the original matrix in a Tu """ function convert_arguments(sl::SurfaceLike, data::AbstractMatrix) n, m = Float32.(size(data)) - convert_arguments(sl, 0f0 .. n, 0f0 .. m, el32convert(data)) + convert_arguments(sl, firstindex(data, 1) .. lastindex(data, 1), firstindex(data, 2) .. lastindex(data, 2), el32convert(collect(data))) end function convert_arguments(ds::DiscreteSurface, data::AbstractMatrix) - n, m = Float32.(size(data)) - convert_arguments(ds, edges(1:n), edges(1:m), el32convert(data)) + convert_arguments(ds, edges(axes(data, 1)), edges(axes(data, 2)), el32convert(collect(data))) end function convert_arguments(SL::SurfaceLike, x::AbstractVector{<:Number}, y::AbstractVector{<:Number}, z::AbstractVector{<:Number}) @@ -423,12 +422,16 @@ and stores the `ClosedInterval` to `n`, `m` and `k`, plus the original array in `P` is the plot Type (it is optional). """ function convert_arguments(::VolumeLike, data::AbstractArray{T, 3}) where T - n, m, k = Float32.(size(data)) - return (0f0 .. n, 0f0 .. m, 0f0 .. k, el32convert(data)) + return ( + Float32(firstindex(data, 1)) .. Float32(lastindex(data, 1)), + Float32(firstindex(data, 2)) .. Float32(lastindex(data, 2)), + Float32(firstindex(data, 3)) .. Float32(lastindex(data, 3)), + el32convert(collect(data)) + ) end function convert_arguments(::VolumeLike, x::RangeLike, y::RangeLike, z::RangeLike, data::AbstractArray{T, 3}) where T - return (x, y, z, el32convert(data)) + return (x, y, z, el32convert(collect(data))) end """ convert_arguments(P, x, y, z, i)::(Vector, Vector, Vector, Matrix) @@ -631,16 +634,6 @@ function tryrange(F, vec) error("$F is not a Function, or is not defined at any of the values $vec") end -# OffsetArrays conversions -function convert_arguments(sl::SurfaceLike, wm::OffsetArray) - x1, y1 = wm.offsets .+ 1 - nx, ny = size(wm) - x = range(x1, length = nx) - y = range(y1, length = ny) - v = parent(wm) - return convert_arguments(sl, x, y, v) -end - ################################################################################ # Helper Functions # ################################################################################ From 77a80bc82db4b0b3d1b8c8cf4792f549d88fa71e Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Tue, 14 Mar 2023 21:00:35 +0530 Subject: [PATCH 2/5] Don't convert intervals in volume to Float32 --- src/conversions.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/conversions.jl b/src/conversions.jl index 2bb06b1ad7c..96846ed8b83 100644 --- a/src/conversions.jl +++ b/src/conversions.jl @@ -423,9 +423,9 @@ and stores the `ClosedInterval` to `n`, `m` and `k`, plus the original array in """ function convert_arguments(::VolumeLike, data::AbstractArray{T, 3}) where T return ( - Float32(firstindex(data, 1)) .. Float32(lastindex(data, 1)), - Float32(firstindex(data, 2)) .. Float32(lastindex(data, 2)), - Float32(firstindex(data, 3)) .. Float32(lastindex(data, 3)), + firstindex(data, 1) .. lastindex(data, 1), + firstindex(data, 2) .. lastindex(data, 2), + firstindex(data, 3) .. lastindex(data, 3), el32convert(collect(data)) ) end From b836c3636561d3e8b623f4fad39112ee3ad77703 Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 20 Jul 2023 12:30:09 +0200 Subject: [PATCH 3/5] Update conversions.jl --- src/conversions.jl | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/conversions.jl b/src/conversions.jl index 96846ed8b83..6747617888c 100644 --- a/src/conversions.jl +++ b/src/conversions.jl @@ -358,12 +358,13 @@ and stores the `ClosedInterval` to `n` and `m`, plus the original matrix in a Tu `P` is the plot Type (it is optional). """ function convert_arguments(sl::SurfaceLike, data::AbstractMatrix) - n, m = Float32.(size(data)) - convert_arguments(sl, firstindex(data, 1) .. lastindex(data, 1), firstindex(data, 2) .. lastindex(data, 2), el32convert(collect(data))) + start = Float32.(firstindex.((data,), 1:2) .- 1) + stop = Float32.(lastindex.((data,), 1:2)) + convert_arguments(sl, start[1] .. stop[1], start[2] .. stop[2], el32convert(data)) end function convert_arguments(ds::DiscreteSurface, data::AbstractMatrix) - convert_arguments(ds, edges(axes(data, 1)), edges(axes(data, 2)), el32convert(collect(data))) + convert_arguments(ds, edges(axes(data, 1)), edges(axes(data, 2)), el32convert(data)) end function convert_arguments(SL::SurfaceLike, x::AbstractVector{<:Number}, y::AbstractVector{<:Number}, z::AbstractVector{<:Number}) @@ -422,16 +423,18 @@ and stores the `ClosedInterval` to `n`, `m` and `k`, plus the original array in `P` is the plot Type (it is optional). """ function convert_arguments(::VolumeLike, data::AbstractArray{T, 3}) where T + start = Float32.(firstindex.((data,), 1:3) .- 1) + stop = Float32.(lastindex.((data,), 1:3)) return ( - firstindex(data, 1) .. lastindex(data, 1), - firstindex(data, 2) .. lastindex(data, 2), - firstindex(data, 3) .. lastindex(data, 3), - el32convert(collect(data)) + start[1] .. stop[1], + start[2] .. stop[2], + start[3] .. stop[3], + el32convert(data) ) end function convert_arguments(::VolumeLike, x::RangeLike, y::RangeLike, z::RangeLike, data::AbstractArray{T, 3}) where T - return (x, y, z, el32convert(collect(data))) + return (x, y, z, el32convert(data)) end """ convert_arguments(P, x, y, z, i)::(Vector, Vector, Vector, Matrix) From 5d94788df529a8a8898e9da0a7f7c5c5c39c335d Mon Sep 17 00:00:00 2001 From: SimonDanisch Date: Fri, 21 Jul 2023 15:34:55 +0200 Subject: [PATCH 4/5] clean up + tests --- Project.toml | 2 -- src/Makie.jl | 1 - src/conversions.jl | 26 +++++++++++++++++++++++--- test/Project.toml | 1 + test/conversions.jl | 19 +++++++++++++++++++ test/runtests.jl | 1 + 6 files changed, 44 insertions(+), 6 deletions(-) diff --git a/Project.toml b/Project.toml index e6d2419da51..bbd843e66c9 100644 --- a/Project.toml +++ b/Project.toml @@ -36,7 +36,6 @@ Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a" Match = "7eb4fadd-790c-5f42-8a69-bfa0b872bfbf" MathTeXEngine = "0a4f8689-d25c-4efe-a92b-7142dfc1aa53" Observables = "510215fc-4207-5dde-b226-833fc4488ee2" -OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" Packing = "19eb6ba3-879d-56ad-ad62-d5c202156566" PlotUtils = "995b91a9-d308-5afd-9ec6-746e21dbc043" PolygonOps = "647866c9-e3ac-4575-94e7-e3d426903924" @@ -86,7 +85,6 @@ MakieCore = "=0.6.3" Match = "1.1" MathTeXEngine = "0.5" Observables = "0.5.3" -OffsetArrays = "1" Packing = "0.5" PlotUtils = "1" PolygonOps = "0.1.1" diff --git a/src/Makie.jl b/src/Makie.jl index ff0cc254311..c99255506dd 100644 --- a/src/Makie.jl +++ b/src/Makie.jl @@ -35,7 +35,6 @@ using UnicodeFun using LinearAlgebra using Statistics using MakieCore -using OffsetArrays using Downloads using ShaderAbstractions diff --git a/src/conversions.jl b/src/conversions.jl index fc01fc607e5..af0f68b9046 100644 --- a/src/conversions.jl +++ b/src/conversions.jl @@ -133,9 +133,29 @@ function convert_arguments(::PointBased, pos::AbstractMatrix{<: Number}) (to_vertices(pos),) end -convert_arguments(P::PointBased, x::AbstractVector{<:Real}, y::AbstractVector{<:Real}) = (Point2f.(x, y),) +function convert_arguments(P::PointBased, x::AbstractVector{<:Real}, y::AbstractVector{<:Real}) + if length(x) != length(y) + error("Length of x & y need to be the same. Found x: $(length(x)), y: $(length(y)))") + end + # Manually iterate, since for comprehension / map / broadcast all preserve input array types, which we dont want here! + points = Vector{Point2f}(undef, length(x)) + @inbounds for (i, xy) in enumerate(zip(x, y)) + points[i] = Point2f(xy) + end + return (points,) +end -convert_arguments(P::PointBased, x::AbstractVector{<:Real}, y::AbstractVector{<:Real}, z::AbstractVector{<:Real}) = (Point3f.(x, y, z),) +function convert_arguments(P::PointBased, x::AbstractVector{<:Real}, y::AbstractVector{<:Real}, z::AbstractVector{<:Real}) + if length(x) != length(y) && length(z) != length(y) + error("Length of x & y & z need to be the same. Found x: $(length(x)), y: $(length(y))), z: $(length(z)))") + end + # Manually iterate, since for comprehension / map / broadcast all preserve input array types, which we dont want here! + points = Vector{Point3f}(undef, length(x)) + @inbounds for (i, xyz) in enumerate(zip(x, y, z)) + points[i] = Point2f(xyz) + end + return (points,) +end """ convert_arguments(P, y)::Vector @@ -668,7 +688,7 @@ float32type(::Type{<: Colorant}) = RGBA{Float32} float32type(x::AbstractArray{T}) where T = float32type(T) float32type(x::T) where T = float32type(T) el32convert(x::AbstractArray) = elconvert(float32type(x), x) -el32convert(x::AbstractArray{Float32}) = x +el32convert(x::AbstractArray{Float32, N}) where {N} = collect(x) el32convert(x::Observable) = lift(el32convert, x) el32convert(x) = convert(float32type(x), x) diff --git a/test/Project.toml b/test/Project.toml index 42c77e8b94a..d51af965d5c 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -8,6 +8,7 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" MeshIO = "7269a6da-0436-5bbc-96c2-40638cbb6118" Observables = "510215fc-4207-5dde-b226-833fc4488ee2" +OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" diff --git a/test/conversions.jl b/test/conversions.jl index 881062c02c3..158fc58789c 100644 --- a/test/conversions.jl +++ b/test/conversions.jl @@ -287,3 +287,22 @@ end pl[1] = [points] @test pl.plots[1][1][] == Makie.poly_convert(points) end + +@testset "offsetarrays" begin + x = -5:5 + y = -3:3 + data_2d = rand(Float32, length.((x, y))) + offsets_2d = OffsetArray(data_2d, x, y) + _x, _y, z = convert_arguments(Heatmap, offsets_2d) + @test z == data_2d + + points = OffsetArray(1:length(x), x) + xy = convert_arguments(Makie.PointBased(), points) + @test xy[1] == Point2f.(x, 1:length(x)) + + z = -10:10 + data_3d = rand(Float32, length.((x, y, z))) + offset_3d = OffsetArray(data_3d, x, y, z) + x, y, z, vol = convert_arguments(Volume, offset_3d) + @test vol == data_3d +end diff --git a/test/runtests.jl b/test/runtests.jl index 1e2e257d552..4b156f71dd4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -6,6 +6,7 @@ using Makie.PlotUtils using Makie.FileIO using Makie.IntervalSets using GeometryBasics: Pyramid +using OffsetArrays using Makie: volume From 11d2bcc7fdd1e37497057e8db5df25bbb7ea5e14 Mon Sep 17 00:00:00 2001 From: SimonDanisch Date: Fri, 21 Jul 2023 15:48:51 +0200 Subject: [PATCH 5/5] fix 3d conversion --- src/conversions.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/conversions.jl b/src/conversions.jl index af0f68b9046..6fad1235642 100644 --- a/src/conversions.jl +++ b/src/conversions.jl @@ -152,7 +152,7 @@ function convert_arguments(P::PointBased, x::AbstractVector{<:Real}, y::Abstract # Manually iterate, since for comprehension / map / broadcast all preserve input array types, which we dont want here! points = Vector{Point3f}(undef, length(x)) @inbounds for (i, xyz) in enumerate(zip(x, y, z)) - points[i] = Point2f(xyz) + points[i] = Point3f(xyz) end return (points,) end