Skip to content

cvc::segment_seeded: GenSeg vendored, sliced, matching the legacy library voxel for voxel (P5 part 1) - #1248

Merged
3 commits merged into
masterfrom
p5/genseg-core
Nov 15, 2026
Merged

3 commits merged into
masterfrom
p5/genseg-core

Conversation

@transfix

Copy link
Copy Markdown
Owner

P5 part 1 of docs/roadmap/VOLROVER-LIBRARY-PORTS-ROADMAP.md (§5 step 1): VolumeRover's GenSeg seeded segmentation, vendored into src/cvc/volume/segmentation/ and matching the legacy library voxel for voxel.

What lands

  • cvc::segment_seeded (inc/cvc/volume/segmentation.h): a volume and cvc::point_classes (P0d) in, a seeded_segmentation out: the labels as a UChar volume, a volume per class (the legacy _subunit_<n>.rawiv, now in the input's type and data units), the border voxels (the legacy _indices.txt), class names and counts, the seed counts and warnings. No files, no current directory.
  • cvc::segment_stepper with every phase sliced (§1.9): checking, normalising, seeding, classifying, marching, writing. A unit is a voxel visited, a seed placed, a voxel popped from a heap or a step of a walk. segment_seeded() is the stepper at budget 0; segment_seeded_job() wraps it in cvc::result_job (P0b).
  • seeded_segmentation_params with its cvc::param_table (inc/cvc/volume/segmentation_params.h): t_low, t_high (data units, or the legacy 0..255 scale with normalized_thresholds), gamma (the paper's speed, -0.12 as in GenSeg), automatic (none / local_maxima, the default per Joe's decision), variable / timestep (which classes seed the run, as VolumeGridRover sent them), class_volumes.
  • The vendored code (genseg.cpp, genseg.h): segment.cpp's ~25 globals are genseg_context members (§1.3); its functions keep their bodies, the hand-unrolled neighbour updates included. Segment(), GetGradient(), GetSeeds() and SearchIndex() are coroutines that yield a unit at a time. Every other change is listed at the top of genseg.cpp: no files or printf, exp() in double on every platform, size_t indices, and three fixes of undefined behaviour (below). -ffp-contract=off / /fp:precise like the other deterministic sources.

Parity (T1)

segmentation_test's MatchesTheLegacyLibrary compares labels and border lists with tools/volrover-goldens on six cases: identical on all six, natively and under node.

The three committed cases never ran GenSeg's threshold sweep (the code that gives each automatic seed a class): every automatic seed sat under a user seed. So this PR adds three cases to make_inputs.py (the existing inputs regenerate byte for byte) and their goldens, made with the legacy build (MSVC 2022, as the first three were):

Case What it covers
auto_seeds_sweep (28³) 5 automatic seeds away from the user seeds, 4 classified by the sweep; an isolated blob, a seed on the volume's face, a class with no seed
blob_touching_shell (24³) the roadmap's blob touching a shell
noisy_many_seeds (24³) 316 automatic seeds, 302 classified one by one as the sweep comes down

volrover goldens CI checks them on Linux with GCC.

The legacy library crashes on some inputs. With noise 0.5 instead of 0.45, noisy_many_seeds makes the legacy executable crash with an access violation. A seed the sweep classifies has nowhere to go, and the legacy march then pops an empty heap and reads before its arrays. The port ends that march (RunsWhereTheLegacyLibraryCrashed). It also grows a heap that would overflow, where the legacy code wrote past the end, and it stops the sweep once the threshold is below 0, where every later search would find nothing again.

A legacy behaviour worth knowing for P6 and P9: a step costs 0.01 exp(0.12 |∇I|), with |∇I| rescaled so that the steepest voxel is 255. Where |∇I| is above about 172 the cost passes MAX_TIME, and the front stops. So on smooth data GenSeg labels each feature's core and stops at its steepest flank. The parity tests pin this; the paper mode (P6) is where it could change.

Tests (§1.7 rows)

  • T1: the six cases above.
  • T2: the rescale and thresholds in data units, class volumes in the input's type and bounding box, classes chosen by variable and timestep.
  • T3: two blobs give two mirror-image labels, one seed of each class on a two-peak blob splits at the saddle, a shell around a blob gives two nested labels.
  • T4: over seeded noise volumes, with and without automatic seeds:
    • every border voxel stops next to a voxel of another class or below t_low;
    • fronts spread only through voxels whose neighbours are all at or above t_low;
    • every labelled voxel connects to a seed of its class;
    • labels never cross a wall below t_low.
  • T5:
    • no seeds, a seed outside the volume (seed (3, 16, 2) of class 'c1' is outside the 16x16x16 volume), more than 254 classes, a constant volume, NaN and infinite voxels, and bad parameters throw segmentation_error;
    • a seed below t_low, a class with no points and a voxel in two classes are warnings;
    • every voxel type gives the same labels.
  • T6: A then B equals B then A equals each alone; two threads with different parameters; two cvc::apps at once, each result belonging to its input's app.
  • T7: the resumable harness (budgets 1, 64, random, 0; cancel in each phase; cooperative frames) on auto_seeds_sweep. The slowest step at budget 4096 is 0.75 ms natively, and stepping costs 3% over budget 0. A step of 64 units stays bounded at 48³ as at 16³.
  • T8: the parameter table through the state tree: defaults seeded, properties written, bad values reported.

segmentation_test is in CVC_PORT_TESTS, so the ports (wasm-mt, node) lane builds and runs it, and in SAN_CXX_TESTS for the tsan and asan-ubsan lanes. No CI job is added or renamed.

The port is as fast as the legacy executable: 2.2 s against 2.3 s on noisy_many_seeds.

Rules

  • No singletons: nothing at namespace or class scope that writes; the objects have no writable data symbols but Boost's own exception statics (checked with GCC's nm). No allowlist line.
  • GenSeg is CVC's own LGPL code, kept with its notice and its request to cite the paper. No third-party code.
  • git-clang-format on the changed lines; ci.yml keeps its line endings (one line changed).

Next (P5 parts 2 and 3)

  • Part 2, §5 step 2: the clean-up, guarded by these parity tests. It compacts the unrolled updates, computes each voxel's step cost once, and stores linear indices in the heaps.
  • Part 3: cvc segment, pycvc, and segment.seeded with its -async and -start forms.

…rary voxel for voxel (P5 part 1)

VolumeRover's GenSeg seeded segmentation (docs/roadmap/VOLROVER-LIBRARY-PORTS-ROADMAP.md §5 step 1)
in src/cvc/volume/segmentation/: segment.cpp's globals become genseg_context's members, its
functions keep their bodies (the hand-unrolled neighbour updates included), and Segment(),
GetGradient(), GetSeeds() and SearchIndex() are coroutines that yield a unit at a time.
In-memory input and output: no files, no current directory.

- inc/cvc/volume/segmentation.h: segment_seeded(volume, point_classes, params), segment_stepper
  (checking, normalising, seeding, classifying, marching, writing), segment_seeded_job
  (cvc::result_job), seeded_segmentation (labels, per-class volumes, border, counts, warnings).
- inc/cvc/volume/segmentation_params.h: the param_table (thresholds in data units or the legacy
  0..255 scale, gamma, automatic seeding, variable and timestep, class volumes).
- Three fixes of the legacy code's undefined behaviour: an empty heap pop (the legacy executable
  crashes on such an input), a heap overflow, and a threshold sweep that ran on below 0.
- tools/volrover-goldens: three more GenSeg cases that run the threshold sweep (the first three
  never did), with their goldens from the legacy build.
- segmentation_test: T1 on all six goldens, T2-T8; in CVC_PORT_TESTS (the wasm-mt node lane)
  and SAN_CXX_TESTS.
…on says

genseg.cpp and segmentation.cpp get -ffp-contract=off (/fp:precise on MSVC) like the other
deterministic sources: the marches compare float arrival times, and a contracted step can flip
which front wins a tie on targets with FMA (aarch64 macOS).
@transfix
transfix disabled auto-merge November 15, 2026 04:54
@transfix
transfix enabled auto-merge November 15, 2026 04:54
@transfix transfix closed this pull request by merging all changes into master in f36e986 Nov 15, 2026
@transfix
transfix deleted the p5/genseg-core branch November 15, 2026 12:08
transfix added a commit that referenced this pull request Nov 15, 2026
Master now has P5 parts 1 and 2 (#1248 and #1250, the GenSeg core and its clean-up), the
4.0.0 bump (#1221), live textures (#1236) and Ewave's timeout (#1261). No conflicts: this
branch's CLI, pycvc and DSL sit on the cleaned-up genseg.cpp unchanged.
transfix added a commit that referenced this pull request Nov 15, 2026
No conflicts. Master brings 26 commits (#1171, #1191, #1221, #1236,
#1245, #1248, #1250 and others); none adds a writable global, a function
static or a signal handler to the hosts or examples, and none touches
the allowlist.
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