Skip to content

Commit

Permalink
Allow for poly to work with empty polygons (#3146)
Browse files Browse the repository at this point in the history
* Fix empty polygon plotting

* fix error on Point2f[]

* fix error on Polygon[]

* add mor conversion tests for empty inputs

---------

Co-authored-by: ffreyer <[email protected]>
Co-authored-by: Simon <[email protected]>
  • Loading branch information
3 people authored Aug 14, 2023
1 parent f1fb6c0 commit c6c3a0e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
1 change: 1 addition & 0 deletions CairoMakie/src/overrides.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function draw_poly(scene::Scene, screen::Screen, poly, rects::Vector{<:Rect2})
end

function polypath(ctx, polygon)
isempty(polygon) && return nothing
ext = decompose(Point2f, polygon.exterior)
Cairo.set_fill_type(ctx, Cairo.CAIRO_FILL_RULE_EVEN_ODD)
Cairo.move_to(ctx, ext[1]...)
Expand Down
12 changes: 12 additions & 0 deletions ReferenceTests/src/tests/examples2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,18 @@ end
fig
end

@reference_test "Plotting empty polygons" begin
p = Makie.Polygon(Point2f[])
q = Makie.Polygon(Point2f[(-1.0, 0.0), (1.0, 0.0), (0.0, 1.0)])
fig, ax, sc = poly([p, q])
poly!(Axis(fig[1,2]), p, color = :black)
poly!(Axis(fig[2,1]), [p, q], color = [:red, :blue])
poly!(Axis(fig[2,2]), [p, q], color = :red)
poly!(Axis(fig[3,1]), Makie.MultiPolygon([p]), color = :green)
poly!(Axis(fig[3,2]), Makie.MultiPolygon([p, q]), color = [:black, :red])
fig
end

@reference_test "lines (some with NaNs) with array colors" begin
f = Figure()
ax = Axis(f[1, 1])
Expand Down
8 changes: 6 additions & 2 deletions src/basic_recipes/poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ function poly_convert(geometries)
return triangle_mesh.(geometries)
end
poly_convert(meshes::AbstractVector{<:AbstractMesh}) = meshes
poly_convert(polys::AbstractVector{<:Polygon}) = triangle_mesh.(polys)
function poly_convert(polys::AbstractVector{<:Polygon})
# GLPlainMesh2D is not concrete?
T = GeometryBasics.Mesh{2, Float32, GeometryBasics.Ngon{2, Float32, 3, Point2f}, SimpleFaceView{2, Float32, 3, GLIndex, Point2f, GLTriangleFace}}
return isempty(polys) ? T[] : triangle_mesh.(polys)
end
function poly_convert(multipolygons::AbstractVector{<:MultiPolygon})
return [merge(triangle_mesh.(multipoly.polygons)) for multipoly in multipolygons]
end
Expand Down Expand Up @@ -82,7 +86,7 @@ end

function to_lines(polygon::AbstractVector{<: VecTypes})
result = Point2f.(polygon)
push!(result, polygon[1])
isempty(result) || push!(result, polygon[1])
return result
end

Expand Down
17 changes: 16 additions & 1 deletion test/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,27 @@ end


@testset "empty poly" begin
# Geometry Primitive
f, ax, pl = poly(Rect2f[]);
pl[1] = [Rect2f(0, 0, 1, 1)];
@test pl.plots[1][1][] == [GeometryBasics.triangle_mesh(Rect2f(0, 0, 1, 1))]

f, ax, pl = poly(Vector{Point2f}[])
# Empty Polygon
f, ax, pl = poly(Polygon(Point2f[]));
pl[1] = Polygon(Point2f[(1,0), (1,1), (0,1)]);
@test pl.plots[1][1][] == GeometryBasics.triangle_mesh(pl[1][])

f, ax, pl = poly(Polygon[]);
pl[1] = [Polygon(Point2f[(1,0), (1,1), (0,1)])];
@test pl.plots[1][1][] == GeometryBasics.triangle_mesh.(pl[1][])

# PointBased inputs
f, ax, pl = poly(Point2f[])
points = decompose(Point2f, Circle(Point2f(0),1))
pl[1] = points
@test pl.plots[1][1][] == Makie.poly_convert(points)

f, ax, pl = poly(Vector{Point2f}[])
pl[1] = [points]
@test pl.plots[1][1][] == Makie.poly_convert(points)
end

0 comments on commit c6c3a0e

Please sign in to comment.