Intersection and area of non-convex polygons

I am trying to use Julia for visualising instances of the moving sofa problem. I would like to be able to calculate the intersection between non-convex shapes and to also calculate the area of the resulting shapes. However, most of the packages I’ve tried so far (e.g., GeometryBasics.jl, Meshes.jl, Polyhedra.jl) seem to support convex shapes only — either that or I am failing to make proper use of them.

The most promising package so far has been PolygonArea.jl. It seems to handle intersections well, but the computed area turns into NaN after a few cuts.

Here is a minimum “working” example:

using Plots
using PolygonArea

function plot_cuts(rads_start, rads_stop, num_cuts)
    plot(aspect_ratio=:equal)

    # Define the initial (large) sofa
    sofa = rectangle((-10.0, -10.0), (10.0, 10.0))

    # Define the corridor (which we'll use as a stencil)
    corridor = rectangle((-5.0, 0.0), (1.0, 1.0)) ∪
               rectangle((0.0, -5.0), (1.0, 1.0))

    # Define the range of rotations to perform
    rads_range = range(rads_start, rads_stop, length=num_cuts)

    # Apply cuts at each angle
    for (i, angle) in enumerate(rads_range)
        # Rotate the corridor by the specified angle
        rotated_corridor = PolygonArea.rotate(corridor, angle)

        # Plot the rotated corridor
        plot!(rotated_corridor, color=:lightgrey)

        # Perform a single cut by intersecting
        # the sofa with the rotated corridor
        sofa = sofa ∩ rotated_corridor

        # FLAKY! Print the area of the resulting shape
        # println("Area after $i cuts: $(area(sofa))")
    end

    # Plot the final shape of the sofa (after the cuts)
    plot!(sofa, color=:blue)
end

Calling plot_cuts(0, π/2, 30) produces this:

Calling plot_cuts(0, π/6, 10) produces this:

These actually look good! But when I uncomment the line for printing the area of the sofa, some NaN start to appear:

julia> plot_cuts(0, π/2, 4)
Area after 1 cuts: 11.0
Area after 2 cuts: 2.8546748471377024
Area after 3 cuts: 0.5566243270259363
Area after 4 cuts: NaN

julia> plot_cuts(0, π/3, 4)
Area after 1 cuts: 11.0
Area after 2 cuts: 3.8145239870160523
Area after 3 cuts: NaN
Area after 4 cuts: NaN

julia> plot_cuts(0, π/4, 4)
Area after 1 cuts: 11.0
Area after 2 cuts: NaN
Area after 3 cuts: 2.836512241476747
Area after 4 cuts: NaN

And, for example, if I call plot_cuts(0, π/2, 5), the code just seems to hang at the call to area.

What is the recommended package for calculating the intersection of non-convex shapes and their resulting area/volume? Thanks in advance!

1 Like

It would be great to get Clipper updated - Clipper1 is already available, but Clipper2 is still on someone’s to-do list…