Skip to content

Upgrade to Clipper2 #199

@gpeairs

Description

@gpeairs

We can get a 2x to 4x speedup on clipping and offsetting operations by switching to Clipper2. I've done some experiments locally, and for the existing benchmarks I get:

Benchmark Clipper2 main Speedup
difference2d 100_square 165 μs 672 μs 4.1x
difference2d 961_square 2.40 ms 7.53 ms 3.1x
difference2d 3600_square 9.65 ms 30.7 ms 3.2x
difference2d 8000_skew 23.5 ms 71.7 ms 3.1x
difference2d 10000_square 30.6 ms 92.3 ms 3.0x
difference2d 19881_square 66.3 ms 193 ms 2.9x
difference2d_offset 88.2 μs 230 μs 2.6x

Offsetting small polygons is ~4x, large polygons is ~2x.

This was done with a minimal ad-hoc approach to enable the tests, making a drop-in replacement despite breaking changes to the Clipper2 interface itself:

  • wrapping Clipper2 with a custom C wrapper matching the existing custom C wrapper for Clipper1 used by Clipper.jl
  • modifying Clipper.jl to use that wrapper

The test suite mostly still passes, with some exceptions where behavior seems to have changed:

  • point order changes (fine, some tests fail but can be changed)
  • intermediate points on straight edges are not removed (likewise)
  • under some circumstances shapes with exactly shared edges are not combined (union2d(Rectangle(1, 1), Rectangle(1, 1) + Point(1, 0)) returns the same 2 contours, although a float or Unitful version gives 1 as expected) -- needs investigation (does this affect our DotMatrix fonts, which need to be combined for rounding?) but likely fine

So I suspect there are no fundamentally breaking changes, but will want to do more testing.

There was an earlier effort to upgrade Clipper.jl to Clipper2 (JuliaGeometry/Clipper.jl#50) but it seems to have stalled. If we want to pursue this, I would wrap Clipper2 faithfully and put any compatibility shim in DeviceLayout.jl instead of the wrapper/Clipper.jl.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions