Skip to content

Commit

Permalink
Fix CairoMakie not being able to plot exotic point arrays in lines (#…
Browse files Browse the repository at this point in the history
…4668)

* Make type annotation on `points` more abstract

* Implement the function barrier approach

* Fix typo

* Add a test to the CairoMakie tests

* Add a changelog entry

* Remove type annotation to enable 2D->3D

* mention lines in changelog

---------

Co-authored-by: Frederic Freyer <[email protected]>
  • Loading branch information
asinghvi17 and ffreyer authored Dec 16, 2024
1 parent 158fe66 commit 5c7aafa
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- Added `transform_marker` attribute to meshscatter and changed the default behavior to not transform marker/mesh vertices [#4606](https://github.com/MakieOrg/Makie.jl/pull/4606)
- Fixed some issues with meshscatter not correctly transforming with transform functions and float32 rescaling [#4606](https://github.com/MakieOrg/Makie.jl/pull/4606)
- Fixed `poly` pipeline for 3D and/or Float64 polygons that begin from an empty vector [#4615](https://github.com/MakieOrg/Makie.jl/pull/4615).
- Fixed an issue where `reinterpret`ed arrays of line points were not handled correctly in CairoMakie [#4668](https://github.com/MakieOrg/Makie.jl/pull/4668).

## [0.21.18] - 2024-12-12

Expand Down
15 changes: 12 additions & 3 deletions CairoMakie/src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,25 @@ end



function project_line_points(scene, plot::T, positions, colors, linewidths) where {T <: Union{Lines, LineSegments}}
function project_line_points(scene, plot::T, positions::AbstractArray{<: Makie.VecTypes{N, FT}}, colors, linewidths) where {T <: Union{Lines, LineSegments}, N, FT <: Real}

# Standard transform from input space to clip space
# Note that this is type unstable, so there is a function barrier in place.
space = (plot.space[])::Symbol
points = Makie.apply_transform(transform_func(plot), positions, space)

return project_transformed_line_points(scene, plot, points, colors, linewidths)
end

function project_transformed_line_points(scene, plot::T, points::AbstractArray{<: Makie.VecTypes{N, FT}}, colors, linewidths) where {T <: Union{Lines, LineSegments}, N, FT <: Real}
# Note that here, `points` has already had `transform_func` applied.
# If colors are defined per point they need to be interpolated like positions
# at clip planes
per_point_colors = colors isa AbstractArray
per_point_linewidths = (T <: Lines) && (linewidths isa AbstractArray)

space = (plot.space[])::Symbol
model = (plot.model[])::Mat4d
# Standard transform from input space to clip space
points = Makie.apply_transform(transform_func(plot), positions, space)::typeof(positions)
f32convert = Makie.f32_convert_matrix(scene.float32convert, space)
transform = Makie.space_to_clip(scene.camera, space) * f32convert * model
clip_points = map(points) do point
Expand Down
11 changes: 11 additions & 0 deletions CairoMakie/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,15 @@ end
ps, _, _ = CairoMakie.project_line_points(a.scene, ls, ls_points, nothing, nothing)
@test length(ps) >= 6 # at least 6 points: [2,3,3,4,4,5]
@test all(ref -> findfirst(p -> isapprox(p, ref, atol = 1e-4), ps) !== nothing, necessary_points)

# Check that `reinterpret`ed arrays of points are handled correctly
# ref. https://github.com/MakieOrg/Makie.jl/issues/4661

data = reinterpret(Point2f, rand(Point2f, 10) .=> rand(Point2f, 10))

f, a, p = lines(data)
Makie.update_state_before_display!(f)
ps, _, _ = @test_nowarn CairoMakie.project_line_points(a.scene, p, data, nothing, nothing)
@test length(ps) == length(data) # this should never clip!

end

0 comments on commit 5c7aafa

Please sign in to comment.