Skip to content

P1 part 1: cvc::tet_complex, an exact incremental Delaunay (and the Qhull decision) - #1157

Merged
transfix merged 6 commits into
masterfrom
p1/tet-complex
Nov 13, 2026
Merged

transfix merged 6 commits into
masterfrom
p1/tet-complex

Conversation

@transfix

@transfix transfix commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

Decided 2026-11-12: Joe chose cvc::tet_complex ("Yes, tet_complex"). The roadmap's §1.2, §1.9, §2, T16 row, P1 row and §9 now say so (646b45c). Qhull stays only as the general-position cross-check in tet_complex_test and cgal_ab_test. Merging still needs Joe's OK once CI is green; parts 2 and 3 build on this.

Part 1 of P1, the skeletonization core (docs/roadmap/VOLROVER-LIBRARY-PORTS-ROADMAP.md §2, §7). The roadmap asks P1's first PR to prove the incremental build against the batch build, and to settle what to do about Qhull on degenerate input before the rest of the port is built on it. This PR does both.

The decision: Qhull with its flat tetrahedra dropped, or an exact incremental Delaunay

The spike (libcvc workstream, "Spike (P1)") found that incremental Qhull matches the batch build in general position, but Qt leaves flat tetrahedra on exactly cospherical points, and nearly cospherical points get merged into a fan that isn't exactly Delaunay. The two ways out were to drop the flats and accept approximate Delaunay, or to write our own incremental Delaunay on Shewchuk's predicates. I built the second one, so both could be measured on the same inputs (cgal_ab_test, Windows, MSVC 19.44, CGAL 6.0.1):

Input CGAL DT3 Qhull batch (d Qbb Qt Qz) Qhull, flats dropped cvc::tet_complex (this PR)
lattice 10^3 (cospherical) 4,374 tets, 14 ms 5,423 tets, 1,049 flat, 50 ms 3,444 facets with no neighbour inside the hull identical to CGAL, no flats, every circumsphere exactly empty, 20 ms
lattice 20^3 41,154 tets, 123 ms 50,471 tets, 9,317 flat, 649 ms 33,772 holes identical to CGAL, 196 ms
sphere, 2,000 points rounded onto it (nearly cospherical) 6,013 tets, 59 ms 3,991 tets, a fan: all 3,991 non-empty by exact predicates, 439 ms (no flats; still not Delaunay) identical to CGAL, all circumspheres exactly empty, 160 ms
integer points on a sphere r=25, and its centre (exactly cospherical) 296 tets, 1.9 ms 440 tets, 144 flat, 4.9 ms 0 holes (the flats lie in hull faces) identical to CGAL, 1.0 ms
cylinder, 12 rings of 24 (cocircular) 726 tets, 18 ms 801 tets, 75 flat, 705 of 801 non-empty, 10 ms 132 holes identical to CGAL, 17 ms
cube corners and centre 12 tets 12 tets 0 identical to CGAL
uniform 2k / 20k / 100k (general position) 12 / 106 / 560 ms identical to CGAL; 29 / 510 / 2,332 ms — identical to CGAL; 6 / 85 / 575 ms
Gaussian clump 20k 133 ms identical; 546 ms — identical; 99 ms

Why dropping the flats doesn't work: each flat tetrahedron sits between two different splits of the same planar quadrilateral, so once it is gone the tetrahedra on either side have facets with no neighbour. The cocone code walks across facets and around edges, so those holes are exactly what it can't take. Fixing them means flipping, which is most of an exact Delaunay anyway, and the nearly cospherical case would still not be Delaunay.

What cvc::tet_complex does. It is an incremental Bowyer–Watson of libcvc's own, about 1,200 lines of code:

  • insertion order: BRIO rounds of Morton keys, put in order by a sliced LSD radix sort;
  • point location: a visibility walk from the last new cell;
  • each new point re-stars its conflict region;
  • predicates: Shewchuk's adaptive orient3d / insphere (cvcpkg shewchuk-predicates);
  • ties: cospherical and coplanar-on-the-hull ties are broken by CGAL's own symbolic perturbation (Devillers–Teillaud). Coplanar in-circle tests are made exact through insphere with a point off the plane.

The perturbation makes the triangulation unique, so the result is CGAL's Delaunay_triangulation_3, cell for cell, on every input, degenerate ones included. That holds whatever the insertion order or seed (tested), and wherever cells are compared, so T1 (legacy parity) and T16 (A/B) can be exact on cospherical samples too, not just in general position. At 100k points it runs at CGAL's speed, 4× faster than Qhull's batch build and about 11× faster than incremental Qhull. Stepping in 4,096-unit slices costs 1–3% over a single step.

Recommendation: build P1 on cvc::tet_complex, the exact incremental Delaunay in this PR. It is exact, it has no flats, it matches CGAL even on degenerate input, it is faster, and it is a stepper by construction. P1 then needs no Qhull at all. Qhull stays a cvcpkg package and a libcvc option; nothing else uses it yet.

If you'd rather keep Qhull, I'll swap the builder behind tet_complex to the spike's incremental Qhull in this PR, with flat tetrahedra kept as flagged zero-volume cells rather than dropped, since dropping leaves the holes above. The tet_complex API stays the same either way.

The roadmap's §1.2, §2 and the P1 row still say Qhull. I'll update them in this PR once you've decided.

What lands

  • inc/cvc/geometry/tet_complex.h, src/cvc/geometry/tet_complex.cpp.
    • cvc::tet_complex follows Triangulation_data_structure_3's conventions: an infinite vertex closes the hull; neighbor(c, i) is opposite vertex(c, i); mirror indices; finite cells positively oriented as CGAL counts it; CGAL's next_around_edge order for cells_around_edge. It caches circumcentres with CGAL's formula, term for term, so Voronoi vertices round as the legacy triang.dual(c) did. It also has validate(), with an exact Delaunay check, and finite_tetrahedra() in canonical form. The skeleton port's CGAL-handle adapter for the vendored cocone code comes in part 2, on top of this.
    • cvc::tet_complex_builder is a resumable stepper (§1.9). Its phases are checking, ordering, inserting, compacting and circumcentres, every one sliced. A unit is a point checked, a key moved in one radix pass, a cell touched by an insertion, a cell compacted, or a circumcentre. cancel() ends the build cleanly, so a result_job around it ends "cancelled". Duplicates map to their first point.
    • cvc::delaunay_tet_complex() is the blocking build: the stepper run at budget 0.
    • No mutable statics: two constexpr tables, an RNG seeded from the options, and every insertion's state on the builder.
  • tet_complex_test is a googletest binary in CVC_PORT_TESTS, so it also runs as wasm-mt under node in the ports lane, and it joins SAN_CXX_TESTS. Rows:
    • T2: valid and exactly Delaunay on uniform, clump, lattice, rounded-sphere and cube-and-centre clouds; the lattice's tetrahedra have exact volumes filling the cubes; the traversals (incident cells, cells around an edge, mirrors) match brute force.
    • Incremental against batch: identical to Qhull's batch d Qbb Qt Qz in general position (uniform 300 and 5,000, clump 5,000).
    • T4: the same triangulation under shuffled input and other seeds, the lattice and spheres included.
    • T5: errors that say what's wrong for 0 or 3 points, all-equal, collinear and coplanar points, and NaN or infinite coordinates. Duplicates are handled.
    • T7: the resumable harness (budgets 1, 64, random and 0 are bit-identical; cancel in each phase; the cooperative frame run). The slowest 4,096-unit step was 0.7 ms on clump 3,000, and 0.4–0.8 ms at 20k–100k points (cgal_ab's stepped rows); no phase change does O(n) work outside the unit count.
  • tools/cgal_ab: tet_complex rows against DT3 on every delaunay3 input, plus three new degenerate inputs (lattice 20^3, the integer sphere, the cylinder); tet_complex stepped rows; and INFO rows for what dropping Qhull's flats leaves. The README records the findings.
  • CI:
    • the ports lane installs qhull and shewchuk-predicates for wasm-mt;
    • the cgal-ab job's path filter watches the new files;
    • ci.yml keeps its CRLF line endings.

macOS

CVC_WITH_SHEWCHUK is OFF on macOS in this PR. The builder then throws a clear error, tet_complex_available() returns false, and the test skips itself. The skeleton is optional behind CVC_WITH_SHEWCHUK in the same way.

Update 2026-11-12: the macOS bundles of shewchuk-predicates (and of clipper2, cdt and nanoflann) are published on cvcpkg.org now. #1165 (open) adds macOS to the recipe's scopes, so once it merges CVC_WITH_SHEWCHUK is on there and these tests run on macOS too.

Update 2026-11-12 (late): #1165 has merged, and master is merged into this branch (72b0d1f). So this CI run is the first with CVC_WITH_SHEWCHUK on macOS, and the tests run on Apple clang instead of skipping.

Checked locally

  • Windows, MSVC 19.44: tet_complex_test passes 10/10, and cgal_ab_test passes with 0 failures (CGAL 6.0.1).
  • wasm-mt (emsdk, node 22): tet_complex_test passes 10/10 under node 22 as built by the ports lane's configuration. On this Windows host, googletest needs a POSIX process.cwd() under NODERAWFS; Linux CI doesn't. The slowest 4,096-unit step was 0.55 ms; the cooperative frame run's slowest frame was 13 ms with a 4 ms slice, unbounded for now.
  • Timing caveat: the machine is shared with other builds, so times vary 1.5–2× between runs. The ratios above are from one run; in a second run tet_complex was 1.3–1.7× faster than CGAL in general position.
  • clang-format 18 is clean on the changed lines.

Next (parts 2 and 3, after your decision)

  • Part 2. The legacy skeletonization (transfix/volrover src/Skeletonization, UT LGPL-2.1) ported into src/cvc/geometry/skeleton/, with the shared cvc::cocone core, skeleton_context and the adapter onto tet_complex, plus §2's fixes:

    • the global bounding_box, srand/rand and the debug files go;
    • the helix window becomes a parameter;
    • the sheets are kept.

    It also brings skeleton_stepper with every phase sliced, cvc::skeletonize and its result_job, the parameter table, T1–T8, and cgal_ab rows comparing the skeletons built on each triangulation.

  • Part 3. cvc skeleton, pycvc, the skeleton.compute intrinsic (with -start / -async and job.cancel), and T10–T12.

…, part 1)

The skeletonization port's tetrahedral complex (VOLROVER-LIBRARY-PORTS-ROADMAP.md §2), with
TDS_3's conventions (an infinite vertex closing the hull, neighbours opposite vertices, mirror
indices, CGAL's next_around_edge order), built by tet_complex_builder, a resumable stepper
(§1.9): checking, ordering (BRIO rounds of Morton keys, a sliced LSD radix sort), inserting
(Bowyer-Watson: a visibility walk from the last new cell, the conflict region re-starred from
the new point), compacting, circumcentres.

The predicates are Shewchuk's (cvcpkg shewchuk-predicates), with CGAL's symbolic perturbation
for cospherical and coplanar ties, so the triangulation is unique and is CGAL's
Delaunay_triangulation_3 cell for cell: cgal_ab_test's new tet_complex rows find it identical on
all 11 of its inputs, the 10^3 and 20^3 lattices, an exactly cospherical integer sphere, the
rounded sphere and a cocircular cylinder included, at 0.97-2x CGAL's speed in general position
and 4-6x Qhull's batch build. The INFO rows show what dropping Qhull's flat tetrahedra would
leave instead: 3,444 facets with no neighbour on the 10^3 lattice.

tet_complex_test (a port test, so also wasm-mt under node, and in the sanitizer lanes): exact
Delaunay and validity on clouds and degenerate input, equality with Qhull's batch build in
general position, independence of input order and seed, traversals against brute force,
duplicates, bad input, and the T7 harness. Without Shewchuk's predicates (macOS today) the
builder throws and the test skips. The ports lane installs qhull and shewchuk-predicates; the
cgal-ab job watches the new files.
The radix sort's arrays are allocated uninitialised, the point, duplicate and vertex-cell
arrays are filled while keying, the remap and the circumcentres are appended in counted loops,
and compaction moves the finite cells and then the infinite ones in two appending passes. The
slowest 4096-unit step is now 0.4-0.8 ms at 20k-100k points (cgal_ab's stepped rows report it).
§1.2's replacement table, §1.9's third-party calls, §2's triangulation and resumable phases,
the T16 row, the P1 row and §9 now say cvc::tet_complex, the exact incremental Delaunay on
Shewchuk's predicates measured in this PR, instead of Qhull. Qhull stays only as the
general-position cross-check in tet_complex_test and cgal_ab_test.
The asan-ubsan job failed in TetComplex.DuplicatesMapToTheirFirstPoint with
a stack-buffer-overflow in shewchuk::detail::fast_expansion_sum_zeroelim,
called from orient3dadapt. Shewchuk's expansion sums read one element past an
input expansion on their last pass (`fnow = f[++findex]`); the value is never
used, so the predicates' results are right, but the read is out of bounds.

exact_predicates.cpp is now the only file that includes
<shewchuk/predicates.hpp>, and tet_complex.cpp calls cvc::exact::orient3d,
insphere and orient2d. The ASan builds compile that one file without ASan, as
the cvcpkg deps are, so no instrumented copy of the header's inline functions
is linked. The header itself should guard the read in cvcpkg's recipe.
# Conflicts:
#	.github/workflows/ci.yml
#	src/cvc/tests/port_tests.cmake
@transfix
transfix merged commit fb634c5 into master Nov 13, 2026
29 of 35 checks passed
@transfix
transfix deleted the p1/tet-complex branch November 13, 2026 10:06
transfix added a commit that referenced this pull request Nov 13, 2026
transfix added a commit that referenced this pull request Nov 13, 2026
transfix added a commit that referenced this pull request Nov 13, 2026
The one conflict was arigl_web_smoke.mjs. Both sides are kept:
- the /apps/up endpoint, with #1200's ISOLATION headers on its replies as on every response;
- the settle probe reads out and isolated/sab;
- each case runs its before/inject and #1200's isolation check.
The headless-Chrome smoke passes 6/6 here, the picked-file upload included.
Master since then: #1179, #1174, #1196, #1198, #1178, #1157, #1172, #1200.
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