Skip to content

cvc::planar: grid, exact predicates, polygons, Clipper2 Booleans and offsets (P3a, part 1) - #1158

Closed
transfix wants to merge 9 commits into
masterfrom
p3a/planar-core
Closed

transfix wants to merge 9 commits into
masterfrom
p3a/planar-core

Conversation

@transfix

@transfix transfix commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

Phase P3a of the VolumeRover ports plan (docs/roadmap/VOLROVER-LIBRARY-PORTS-ROADMAP.md §3, §7), part 1 of 3: the cvc::planar module that the ContourTiler port (P3b) sits on. It replaces the GPL CGAL packages ContourTiler uses with permissive ones, all from cvcpkg (§1.2).

What lands

  • inc/cvc/geometry/planar.h, src/cvc/geometry/planar/

    • The snapping grid. Integer points within ±2^31 (Boost.Polygon's Voronoi range, which part 3 needs). grid::fit picks a power-of-two unit and an origin from a bounding box, so every vertex snaps once and every grid point's world position is exact.
    • Exact predicates. Shewchuk's orient2d and incircle, segment intersection, point on segment.
    • Rings and polygons with holes. Area, orientation, simplicity, exact point location, clean-up, and nest (ContourTiler's arrange_polygons: rings arranged into polygons with holes by depth).
    • Clipper2. unite, intersect, subtract, exclusive_or, resolve (fill rules), one-pass offset with any join, and minkowski_sum.
    • wavefront_offset: the straight skeleton's offset. This is what offset_polygon.cpp took from CGAL. It runs mitred Clipper2 passes in steps, each ending at the wavefront's next event (an edge collapsing, or a vertex reaching another edge). The steps run on a grid 2^20 times finer, and the result rounds to the caller's grid once at the end.
    • Pure functions: no statics, safe on any thread, planar_error on bad input.
  • Build. Built only when Clipper2, CDT and Shewchuk's predicates are all found. cvc then exports CVC_WITH_PLANAR.

    • macOS builds it too. Master's recipe gained those three packages on macOS in libcvc recipe: Clipper2, CDT and Shewchuk's predicates on macOS too #1165 (their macOS bundles were published on 2026-11-12), and this branch has merged it. Where a build lacks any of them, planar_test is one skipped test.
    • Boost.Polygon (part 3) is header-only and comes with Boost, which is already a dependency.
  • Tests (T2, T6). planar_test, against brute-force references:

    • the predicates against exact integer arithmetic, including at the edge of the range where doubles are not exact;
    • point location against summed winding angles;
    • Booleans: every sample point classified as the operation says, and exact areas on orthogonal polygons;
    • offsets: convex mitred offsets against half-plane clipping, rounded offsets against point distances, and the wavefront offset against the skeleton's known shape past an edge event;
    • Minkowski sums against the convex hull of vertex sums;
    • two threads give what each gives alone.

    It is in CVC_PORT_TESTS and the sanitizer lanes' SAN_CXX_TESTS. The ports lane now installs qhull, clipper2, cdt and shewchuk-predicates, and fails if the configure did not enable cvc::planar.

  • A/B against CGAL (T16). tools/cgal_ab/cgal_ab_planar.cpp runs cvc::planar's own functions against ContourTiler's CGAL calls: Polygon_set_2, create_{interior,exterior}_skeleton_and_offset_polygons_with_holes_2 and minkowski_sum_2. Each case compares areas, ring counts and the symmetric difference, within one grid unit per unit of boundary. The cgal-ab job's path filter now covers src/cvc/geometry/planar and planar.h.

The P0c finding: one-pass offsets vs the straight skeleton

The libcvc workstream recorded that Clipper2's mitred offsets differ from straight-skeleton offsets past an edge event. The A/B narrows that down:

  • A single mitred pass agrees with the skeleton past an edge event whose collapse leaves a convex join.
  • It differs past one that leaves a reflex join. There the skeleton mitres the vanished edge's neighbours, while a single pass bounds the region by the vanished edge's line.
  • Locally that happened in 2 of the 50 offset cases, both deep inward offsets of stars, by 0.4% and 7.4% of the area.

How cvc::planar handles it: wavefront_offset steps through those events and agrees with CGAL in all 50 cases, those two included (symmetric difference 0 after rounding). When every step rounded to the caller's grid instead, the error reached about two grid units per unit of boundary on dense contours, which is why the steps run on the finer grid. offset stays available as the plain one-pass operation; its header documents where it differs.

Checked locally (Windows, MSVC 19.44, CGAL 6.0.1; emsdk, node 22)

  • planar_test: 28/28 natively, and 28/28 as wasm-mt under node. Running it here meant relinking without -sNODERAWFS, because gtest can't read the working directory through NODERAWFS on Windows; the Linux ports lane doesn't need that.
  • cgal_ab_test: 0 failures.
    • Booleans: all 32 cases agree, exactly on the orthogonal ones, at 20–60× CGAL's speed.
    • Offsets: wavefront_offset agrees in all 50 cases, about 27× faster than CGAL.
    • Minkowski sums: all agree, exactly for convex pairs.
  • The writable-data scan: compiling Clipper2's headers with GCC 16 at -O2 emits no writable data (its static const sentinels and message pointers are folded away), so the allowlist doesn't grow. cvc::planar itself has no statics.

Parts 2 and 3

  • Part 2: CDT triangulations, the Hertel–Mehlhorn convex partition and Sibson interpolation.
  • Part 3: the Boost.Polygon medial axis.

Each follows as its own PR stacked on this one.

…offsets (P3a, part 1)

The planar-geometry module the ContourTiler port sits on (VOLROVER-LIBRARY-PORTS-ROADMAP.md §3,
P3a), replacing the GPL CGAL packages ContourTiler used with permissive ones:

- inc/cvc/geometry/planar.h, src/cvc/geometry/planar/: integer points on a snapping grid
  (grid::fit picks a power-of-two unit and an origin from a box, so a grid point's world
  position is exact), Shewchuk's orient2d and incircle, rings and polygons with holes (area,
  orientation, simplicity, point location, clean-up, nesting as arrange_polygons did), Clipper2
  Booleans and resolve(), one-pass offsets, and minkowski_sum.
- wavefront_offset: the straight skeleton's offset (what offset_polygon.cpp took from CGAL),
  as mitred Clipper2 passes stepped from one wavefront event to the next on a grid 2^20 times
  finer, rounded once at the end.
- Built only with Clipper2, CDT and Shewchuk's predicates (then cvc exports CVC_WITH_PLANAR);
  macOS has no bundles of those yet, so it is off there.
- planar_test (T2 against brute force, T6 on two threads), in CVC_PORT_TESTS and the sanitizer
  lanes; the ports lane installs the geometry packages and checks the configure enabled it.
- cgal_ab_planar.cpp (T16): cvc::planar against Polygon_set_2, the straight-skeleton offsets
  and minkowski_sum_2. A single mitred pass differs from the skeleton in 2 of 50 offset cases
  (0.4% and 7.4% of the area, past an edge event that leaves a reflex join);
  wavefront_offset agrees in all 50, and the Booleans in all 32.
transfix added a commit that referenced this pull request Nov 15, 2026
…and the Boost.Polygon medial axis (P3a, #1158, #1162) (#1164)

Squash of #1164, which is stacked on #1162 and #1158 and carries all of both:

- #1158 (P3a, part 1): the grid, exact predicates, polygons, Clipper2 Booleans and offsets.
- #1162 (P3a, part 2): CDT triangulations, Hertel-Mehlhorn partitions, Sibson coordinates.
- #1164 (P3a, part 3): the Boost.Polygon medial axis.

#1164's required checks passed on the combined tree.
@transfix

Copy link
Copy Markdown
Owner Author

Landed in #1164's squash commit 187f701. #1164 was stacked on this PR and carried all of it, and #1164's required checks passed on the combined tree. Master now has this PR's changes, in the form #1164 left them.

@transfix transfix closed this Nov 15, 2026
@transfix
transfix deleted the p3a/planar-core branch November 15, 2026 04:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant