SDF segmentation
What it does
Synthesizes per-facet semantic labels from geometry alone, partitioning the surface into regions of similar local thickness. CAD- exported STL almost never carries patch / surface-group information — every facet is in one anonymous bag. This TBP gives you a per-facet integer in [0, k] that downstream code (BC tagging, CFD setup) can use as a substitute for the .cgns patch hierarchy that's missing.
Three-stage pipeline (Shapira, Shamir & Cohen-Or 2008):
- Per-facet Shape Diameter Function — for each facet, cast a cone of rays inward (along
-normal), measure first-hit distance on the opposite surface, take the median. The shape diameter is approximately the local thickness of the solid at that facet. - Dual-graph Laplacian smoothing — average each facet's SDF with its 3 neighbours via
facet%fcon_edge. Two passes by default (Shapira's value). - 1D Gaussian-mixture clustering — fit a
k-component GMM to the smoothed SDF values via EM with k-means++ initialization. Per-facet hard label = argmax posterior, with clusters re-ordered ascending by mean so label 1 = thinnest features, labelk= thickest.
Pipeline detail
For step 1: each facet emits num_rays rays (default 30) in a cone of half-aperture cone_angle_deg/2 (default 60° = 120° full aperture) around the inward normal, distributed via Fibonacci-spiral sampling (deterministic, low-discrepancy, no clumping at pole or rim). Rays start at centroid - ε·normal (offset trick) to skip the self-hit. If fewer than 50 % of rays hit the opposite surface, the facet is flagged with sentinel SDF (-1) and excluded from clustering.
For step 2: sdf_new[f] = (1-λ)·sdf[f] + λ·mean(sdf[neighbours]). Sentinels propagate (a sentinel facet stays sentinel) AND are excluded from neighbour averages (sentinel neighbours don't contaminate the mean).
For step 3: variance-floored EM (var ≥ 1e-12 · total_var prevents empty-cluster collapse), log-sum-exp stable E step, 50-iteration cap with log-likelihood Δ < 1e-9 convergence. K-means++ init is deterministic (first center = median, subsequent = max-d² greedy) for reproducibility.
The smoothed shape diameter is the only input of the clustering: the labels above are this field cut into four intervals by the mixture (on the dragon, label 1 is 0.000 to 0.837, label 4 is 3.783 to 4.916; see the example).
API
call surface%segment_sdf(facet_labels, sdf, num_clusters, smoothing_lambda, &
smoothing_iterations, num_rays, cone_angle_deg, status)| Argument | Intent | Type | Default | Notes |
|---|---|---|---|---|
facet_labels | out | integer(I4P), allocatable | — | Per-facet labels in [0, num_clusters]. |
sdf | out, opt. | real(R8P), allocatable | — | Smoothed per-facet SDF for inspection. |
num_clusters | in, opt. | integer(I4P) | 4 | GMM component count. Mechanical-CAD-friendly; CGAL uses 5. |
smoothing_lambda | in, opt. | real(R8P) | 0.5 | Laplacian blend weight ∈ [0, 1]. |
smoothing_iterations | in, opt. | integer(I4P) | 2 | Smoothing pass count. Shapira's default. |
num_rays | in, opt. | integer(I4P) | 30 | Cone rays per facet. Shapira's default. |
cone_angle_deg | in, opt. | real(R8P) | 120.0 | Full cone aperture, degrees. |
status | out, opt. | integer(I4P) | — | SDF_STATUS_*. |
facet_labels(i) == 0 (SDF_LABEL_UNASSIGNED) means the facet had sentinel SDF (degenerate shell, isolated patch). Downstream code should treat unassigned facets as their own implicit "unknown" group.
Example
A plate 4 × 1 whose thickness steps from 0.1 to 0.5 at x = 2, built in code (watertight, outward), split into two labels; then the dragon with the defaults. For the per-facet thickness as a measurement rather than a feature, see thickness and gaps.
use fossil, only : surface_stl_object, SDF_STATUS_OK, SDF_LABEL_UNASSIGNED
use fossil_facet_object, only : facet_object
use penf, only : I4P, R8P
use vecfor, only : vector_R8P
implicit none
integer(I4P), parameter :: NX = 16, NY = 4 ! cells of the plate's top and bottom faces
type(surface_stl_object) :: plate, dragon
type(facet_object), allocatable :: f(:)
type(facet_object), pointer :: fp
integer(I4P), allocatable :: labels(:)
real(R8P), allocatable :: sdf(:), xc(:)
integer(I4P) :: k, m, status, u
call build_plate(f) ! [0, 4] x [0, 1], thickness 0.1 for x < 2, 0.5 for x > 2.25
call plate%adopt_facets(facets=f)
call plate%analyze
call plate%segment_sdf(facet_labels=labels, sdf=sdf, num_clusters=2_I4P, status=status)
if (status /= SDF_STATUS_OK) error stop 'segment_sdf failed'
allocate(xc(plate%get_facets_number()))
do k = 1, plate%get_facets_number() ! facet centroids along the plate
fp => plate%facet_at(k)
xc(k) = (fp%vertex(1)%x + fp%vertex(2)%x + fp%vertex(3)%x) / 3._R8P
enddo
print '(A,I0,A,L1,A,I0)', 'plate: ', plate%get_facets_number(), ' facets, watertight ', plate%is_watertight(), &
', unassigned ', count(labels == SDF_LABEL_UNASSIGNED)
do k = 1, 2
print '(A,I0,A,I3,A,F6.3,A,F6.3,A,F6.3,A,F6.3)', ' label ', k, ': ', count(labels == k), ' facets, sdf ', &
minval(sdf, labels == k), ' to ', maxval(sdf, labels == k), ', centroids x ', minval(xc, labels == k), &
' to ', maxval(xc, labels == k)
enddocall dragon%load_from_file(file_name='dragon.stl', guess_format=.true.)
call dragon%segment_sdf(facet_labels=labels, sdf=sdf, status=status) ! defaults: 4 clusters, 30 rays, 120 degrees
if (status /= SDF_STATUS_OK) error stop 'segment_sdf failed'
print '(A,I0,A,I0)', 'dragon: ', size(labels), ' facets, unassigned ', count(labels == SDF_LABEL_UNASSIGNED)
do k = 1, 4
print '(A,I0,A,I5,A,F6.3,A,F6.3)', ' label ', k, ': ', count(labels == k), ' facets, sdf ', &
minval(sdf, labels == k), ' to ', maxval(sdf, labels == k)
enddo$ ref_sdf_segmentation
plate: 336 facets, watertight T, unassigned 0
label 1: 148 facets, sdf 0.123 to 0.135, centroids x 0.000 to 1.833
label 2: 188 facets, sdf 0.120 to 0.628, centroids x 0.000 to 4.000
dragon: 6588 facets, unassigned 41
label 1: 2192 facets, sdf 0.000 to 0.837
label 2: 1859 facets, sdf 0.838 to 1.916
label 3: 2089 facets, sdf 1.919 to 3.779
label 4: 407 facets, sdf 3.783 to 4.916segment_sdf(facet_labels=, sdf=, num_clusters=2)on the plate: label 1 holds 148 facets, all with a shape diameter between 0.123 and 0.135, 1.23 to 1.35 times the thickness 0.1: the median of a 120° cone overestimates a plate by about a third (see the bias of the cone).- Label 2 holds the 188 others, from 0.120 to 0.628. It takes the thick half and the thin facets near the ramp, whose cone reaches the thick part: the label-1 centroids stop at x = 1.833.
- Two facets of the thin end (shape diameter 0.120 and 0.121) are in label 2 although their value is the thin one. They lie below the narrow component's range, where its density falls faster than the broad component's: the argmax of the posterior is not monotone in the shape diameter. No graph cut repairs it (see below).
- The dragon (6588 facets) is open: 41 facets get
SDF_LABEL_UNASSIGNED, fewer than half of their rays hit something. The four labels are disjoint intervals of the smoothed shape diameter.
Known limitations
- GMM is a mode-finder, not a mode-merger. With
klarger than the data supports, GMM will split a tightly-clustered distribution into two near-identical components — and the argmax labelling treats those as distinct labels. On a uniform input (sphere withk = 2), the label distribution is not trivially "all label 1"; it'll split ~62 / 38 between two labels of essentially the same SDF cluster. This is intended GMM behaviour, not a bug. - Cube +
k = 2is a misleading test fixture. The cube's 12-facet diagonal-split tessellation produces a genuinely bimodal SDF (some facets see longer cone-ray paths to the opposite face), so GMM correctly splits — even though intuitively "the cube has only one feature scale." Use sphere withk = 1to test the trivial cluster path. - No alpha-expansion graph cut (Shapira §5). The full Shapira pipeline runs a graph-cut post-pass over the dual graph to enforce label coherence across low-dihedral-angle edges (segmentation respects sharp edges). FOSSIL's MVP omits this — labels can flip across edges that smoothing didn't already align. Deferred to §1.9b follow-up. For typical AMR-CFD STL the cone-ray + smoothing signal is dominant and the labels are usable as-is.
- Sentinel SDF facets are excluded from clustering. They get label 0 (
SDF_LABEL_UNASSIGNED). On clean closed meshes this is rare (< 0.1 % of facets); on dirty meshes with isolated patches it can be substantial. - Reproducibility: k-means++ init is deterministic but not parallel-safe. Don't multi-thread
segment_sdf.
See also
- ray queries — the
intersect_ray_firstTBP that the cone-of-rays SDF query uses internally. get_facets_number— to size yourlabelsallocation if pre-allocating.- Constants —
SDF_STATUS_*,SDF_LABEL_UNASSIGNED,SDF_SENTINEL.
References
- Shapira, Shamir & Cohen-Or, Consistent Mesh Partitioning and Skeletonisation Using the Shape Diameter Function, The Visual Computer 24(4), 2008. The reference algorithm.
- CGAL
Surface_mesh_segmentationpackage documentation. The reference implementation; FOSSIL matches its API shape but omits the alpha-expansion post-pass. - Boykov & Kolmogorov, An Experimental Comparison of Min-Cut/Max- Flow Algorithms for Energy Minimization in Vision, PAMI 2004. The graph-cut algorithm that §1.9b would adopt.