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

[WIP] Make array handling in converts as generic as possible #2756

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion src/Makie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ using UnicodeFun
using LinearAlgebra
using Statistics
using MakieCore
using OffsetArrays
using Downloads
using ShaderAbstractions

Expand Down
58 changes: 37 additions & 21 deletions src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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] = Point3f(xyz)
end
return (points,)
end

"""
convert_arguments(P, y)::Vector
Expand Down Expand Up @@ -308,12 +328,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
Expand Down Expand Up @@ -366,13 +386,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, 0f0 .. n, 0f0 .. m, el32convert(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)
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(data))
end

function convert_arguments(SL::SurfaceLike, x::AbstractVector{<:Number}, y::AbstractVector{<:Number}, z::AbstractVector{<:Number})
Expand Down Expand Up @@ -431,8 +451,14 @@ 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))
start = Float32.(firstindex.((data,), 1:3) .- 1)
stop = Float32.(lastindex.((data,), 1:3))
return (
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
Expand Down Expand Up @@ -645,16 +671,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 #
################################################################################
Expand All @@ -672,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)

Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
19 changes: 19 additions & 0 deletions test/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using Makie.PlotUtils
using Makie.FileIO
using Makie.IntervalSets
using GeometryBasics: Pyramid
using OffsetArrays

using Makie: volume

Expand Down
Loading