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

meshcatter argument color doesn't work with views of Vector{Float32} #4428

Open
f-ij opened this issue Sep 30, 2024 · 2 comments
Open

meshcatter argument color doesn't work with views of Vector{Float32} #4428

f-ij opened this issue Sep 30, 2024 · 2 comments
Labels
Attributes Plot, Block and Scene Attributes bug conversions Mainly `convert_arguments` Makie Backend independent issues (Makie core) meshscatter

Comments

@f-ij
Copy link

f-ij commented Sep 30, 2024

using GLMakie

const state = zeros(Float32, 5^2+10^3)

state .= randn(5^2+10^3)

update(state)

const view = @view state[5^2+1:end]
function create_unsafe_vector(view_array)
    # Get the pointer to the view array
    ptr = pointer(view_array)
    # Wrap the pointer into a Julia array without copying
    unsafe_vector = unsafe_wrap(Vector{eltype(view_array)}, ptr, length(view_array))
    return unsafe_vector
end
const unsafe_view = create_unsafe_vector(view)
const unsafeobs = Observable(unsafe_view)
const viewobs = Observable(view)

fig = Figure()
ax = Axis3(fig[1, 1])
display(fig)

sz = (10,10,10)
allidxs = [1:1000;]
idx2ycoord(size::NTuple{3,T}, idx) where {T} = (T(idx)-T(1)) % size[1] + T(1)
idx2xcoord(size::NTuple{3,T}, idx) where {T} = (floor(T, (idx-T(1))/size[1])) % size[2] + T(1)
idx2zcoord(size::NTuple{3,T}, idx) where {T} = floor(T, (idx-T(1))/(size[1]*size[2])) + T(1)

xs = idx2xcoord.(Ref(sz), allidxs)
ys = idx2ycoord.(Ref(sz), allidxs)
zs = idx2zcoord.(Ref(sz), allidxs)
meshscatter!(ax, xs, ys, zs, markersize = 0.2, color = viewobs)

The above code only works with the unsafeobs, not the viewobs. When using Float64 for the original array there are no problems. I'm getting the following error:

ERROR: MethodError: no method matching GLMakie.GLAbstraction.TextureBuffer(::SubArray{Float32, 1, Vector{Float32}, Tuple{UnitRange{Int64}}, true})

Closest candidates are:
  GLMakie.GLAbstraction.TextureBuffer(::GLMakie.GLAbstraction.Texture{T, 1}, ::GLMakie.GLAbstraction.GLBuffer{T}) where T
   @ GLMakie ~/.julia/packages/GLMakie/VvhAo/src/GLAbstraction/GLTexture.jl:50
  GLMakie.GLAbstraction.TextureBuffer(::GLMakie.GLAbstraction.GLBuffer{T}) where T<:Union{Real, ColorTypes.Colorant, StaticArraysCore.StaticArray{Tuple{N}, T, 1} where {N, T}}
   @ GLMakie ~/.julia/packages/GLMakie/VvhAo/src/GLAbstraction/GLTexture.jl:191
  GLMakie.GLAbstraction.TextureBuffer(::Vector{T}) where T<:Union{Real, ColorTypes.Colorant, StaticArraysCore.StaticArray{Tuple{N}, T, 1} where {N, T}}
   @ GLMakie ~/.julia/packages/GLMakie/VvhAo/src/GLAbstraction/GLTexture.jl:204
@f-ij f-ij added the bug label Sep 30, 2024
@f-ij
Copy link
Author

f-ij commented Sep 30, 2024

Actually, I just found that even with the unsafeview, updating and then notifying the colors gives weird behavior, where the colors are not updated properly. When the original state has eltype Float64 everything seems to work fine.

@f-ij
Copy link
Author

f-ij commented Sep 30, 2024

Silly workaround:

wrap the unsafeview in the following

struct CastVec{In,Out} <: AbstractVector{Out}
    data::Vector{In}
end
Base.getindex(v::CastVec{In,Out}, i) where {In,Out} = Out(v.data[i])
Base.setindex!(v::CastVec{In,Out}, val, i) where {In,Out} = v.data[i] = In(val)
Base.length(v::CastVec) = length(v.data)
Base.size(v::CastVec) = size(v.data)
Base.eltype(v::CastVec) = eltype(v.data)
Base.IteratorSize(::Type{CastVec}) = Base.HasLength()
Base.iterate(v::CastVec, i=1) = i > length(v) ? nothing : (v[i], i+1)
CastVec(t::Type, data) = CastVec{eltype(data), t}(data)

castobs = Observable(CastVec(Float64,unsafe_view))

@ffreyer ffreyer added Makie Backend independent issues (Makie core) conversions Mainly `convert_arguments` Attributes Plot, Block and Scene Attributes meshscatter labels Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Attributes Plot, Block and Scene Attributes bug conversions Mainly `convert_arguments` Makie Backend independent issues (Makie core) meshscatter
Projects
None yet
Development

No branches or pull requests

2 participants