Junction relocation and interface projection (2D)¶
Move a finished 2D mesh's junction vertices onto the junctions the mask itself gives (opt-in).
Opt-in. It is the 2D port of dw3d's junction_relocation + junction_curves: the connectivity is frozen
(the edge array, hence every interface and junction identity, is never changed); only the coordinates
of junction vertices move, and only when four guards accept the move.
Why¶
dw2d's mesh vertices are Delaunay points chosen on the pixel lattice (integer row, column), so every
junction vertex sits on a pixel centre, while a junction of the label image lies on the dual lattice:
the corner shared by four pixels. On the equilibrium-foam benchmark (exact rasterisations of
certified circular-arc foams with known tensions; held-out split, 159 images, min_dist 3) moving
the junctions onto the mask's own junctions lowered the case angle error (per case the median over
junctions of the mean absolute error of the three material-attributed angles, then the median over
cases) by 1.76 deg [1.51, 1.94] from 9.15 deg, the junction position error by 0.21 px, and the
gauge-matched tension error by 0.025 [0.011, 0.029] (per-junction Young-Dupre rows), on every scale
tier (cell radius 10, 20, 40 px). With smooth boundary noise of 0.5 px RMS it still helps
(-1.49 deg, -0.019); at 1 px it helps the angles only (-0.66 deg); at 2 px it is neutral on angles.
These are measurements on synthetic masks, not a guarantee on real masks.
What it does¶
- Mask junctions. A dual-lattice corner at continuous (row, col) = (i + 0.5, j + 0.5) is shared by pixels (i, j), (i, j+1), (i+1, j), (i+1, j+1). It is a junction corner of the triple (a, b, c) when its four pixels hold exactly those three labels; a corner whose four pixels hold four labels counts for each of the four triples it contains (dw3d's rule, which carries a junction into a four-fold point instead of stopping it short). Per triple, 8-connected components of junction corners are one mask junction each, located at the component's centroid.
- Identity. Mesh junctions (vertices whose incident edges involve exactly three materials) are
paired with mask junctions of the same triple by the Hungarian assignment on Euclidean distance,
pairs farther than
gate_px(3 px) rejected, and only for triples whose two counts agree ("matched"). A vertex of four or more materials is never moved. - Guards. Moves are applied one vertex at a time in ascending vertex index against the
configuration as it stands. A move is accepted only if (1) no incident edge becomes shorter than
MIN_EDGE_PX; (2) the number of proper crossings between non-adjacent edges does not increase — moving v changes only the edges incident to v, so the change of the whole-mesh count equals the change restricted to pairs involving those edges (an exact identity, evaluated against a candidate set that provably contains every edge that can meet them); (3) no edge incident to the moved vertex comes into a contact it was not in before the move (see "Edge contacts" below); (4) no material's signed area (dw2dDcel.compute_areasover the oriented edges) changes sign or reaches zero. A refused move is dropped, never repaired: the vertex stays where it was. Whole-mesh recounts of crossings and of contacts before and after certify (2) and (3) globally; a rise raises.
Edge contacts¶
Guard (2) counts proper crossings in floating point, so it lets through a move that makes two edges
touch: on the equilibrium-foam benchmark at a cell radius of 40 px, the projection of
:mod:dw2d.locus_projection flattened 141 Z-shaped turns of plain interface chains so that a vertex
landed exactly on a non-adjacent edge (an endpoint inside another edge) and the edge before it folded
back by exactly 180 degrees. Guard (3) refuses every new contact of either kind, decided exactly:
- two edges sharing no vertex are in contact when the closed segments meet: a proper crossing, a T-touch (an endpoint of one lies on the other), coincident vertices (two distinct vertices with identical coordinates, endpoints of both), or a collinear overlap (four collinear endpoints sharing one point or a sub-segment);
- two edges sharing one vertex s are in contact when their other endpoints lie on the same ray from s: one edge runs back along the other (an adjacent overlap, a turn of exactly 180 degrees).
A pair counts as new when it is not in contact before the move, or in contact of another class.
Every coordinate is a binary64 float, hence an exact rational. Orientation signs use Shewchuk's static
filter for orient2d and, when the filter cannot certify the sign, the determinant in
fractions.Fraction; comparisons of coordinates are float comparisons, which are exact. On that
benchmark the guard refuses 143 projection moves in 38 of the 53 meshes at 40 px and leaves 0
contacts; at 10 and 20 px, where the projection creates none, it refuses nothing and the vertices are
bitwise unchanged. It keeps the Z turns of the plain mesh (two turns of opposite signs) rather than
removing them.
Units and conventions: coordinates in pixels, (row, col), the frame of geometry_reconstruction_2d's
vertices; the label image is the one the mesh was built from (after expansion_labels). Nothing
reads ground truth. Deterministic.
Failure modes: a mesh with no matched triple is returned unchanged (planned = 0); a triple whose
mesh and mask junction counts differ is left untouched and counted (unmatched_triples); the
thresholds are fixed and are not tuning parameters.
GuardedVertexMover ¶
GuardedVertexMover(verts, edges)
Vertex positions over a frozen edge array, moved one vertex at a time under the four guards.
Inputs: verts (V, 2) float (copied), edges (E, 4) int [v1, v2, m1, m2]. try_move(v, p)
applies the move and returns "" or the name of the refusing guard, checked in this order:
"short_edge", "crossing", "contact", "area_sign". The candidate search for the
crossing and contact guards is a KD-tree on the initial edge midpoints; its radius adds twice
the largest displacement applied so far, which bounds both how far any midpoint has moved and how
much any half-length has grown, so the candidate set always contains every edge the moved edges
can meet.
whole_mesh_contacts ¶
whole_mesh_contacts(verts, edges)
Number of pairs of edges of a 2D mesh in contact, of any class (see "Edge contacts" above).
verts (V, 2) float, edges (E, >= 2) int (first two columns = vertex indices). Pairs of
edges sharing no vertex count when the closed segments meet (proper crossing, T-touch, coincident
vertices, collinear overlap); pairs sharing one vertex count when one runs back along the other.
Zero-length edges are skipped. Candidate pairs come from a KD-tree on edge midpoints with a radius
covering the two longest half-edges, then an exact bounding-box test; the classes are exact.
whole_mesh_crossings ¶
whole_mesh_crossings(verts, edges)
Number of proper crossings between non-adjacent edges of a 2D mesh.
verts (V, 2) float, edges (E, >= 2) int (first two columns = vertex indices). Candidate
pairs come from a KD-tree on edge midpoints with a radius covering the two longest half-edges, so
no crossing can be missed.
signed_areas ¶
signed_areas(verts, edges)
{material: signed area} from dw2d's oriented edge array (the formula of Dcel.compute_areas).
On dw2d meshes every cell's value has the same sign (about its pixel count, positive since 0.8.0, when each
edge became the positively oriented boundary of its first material; negative before); the area guard relies
only on that consistency, which tests/test_junction_relocation.py pins.
mask_junctions ¶
mask_junctions(labels)
Junctions of a label image on the dual lattice.
Returns a list of dict(materials: sorted triple, position: [row, col] (the component centroid, on half-integer coordinates for a single corner), n_corners).
mesh_junction_vertices ¶
mesh_junction_vertices(edges)
{vertex index: sorted material triple} for vertices whose incident edges involve exactly three materials.
relocate_junction_vertices ¶
relocate_junction_vertices(mask: ndarray, points: ndarray, segments: ndarray, labels: ndarray, *, gate: float = GATE_PX, fixed_vertices: Iterable[int] = ()) -> tuple[np.ndarray, JunctionRelocationReport]
Move the matched junction vertices of a 2D mesh onto the mask's own junctions (connectivity frozen).
The 2D counterpart of dw3d.relocate_junction_vertices, with the same argument order: the mask first, then the
mesh. This module's docstring says what it does, under which guards, and what it was measured to buy.
dw2d.MeshReconstructionAlgorithm runs it by default (set_junction_relocation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
ndarray
|
the 2-D label image the mesh was built from (after any label expansion). |
required |
points
|
ndarray
|
|
required |
segments
|
ndarray
|
|
required |
labels
|
ndarray
|
|
required |
gate
|
float
|
largest distance, in pixels, between a mesh junction and the mask junction it is paired with. |
GATE_PX
|
fixed_vertices
|
Iterable[int]
|
vertices never moved (the algorithm passes its |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[ndarray, JunctionRelocationReport]
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if the whole-mesh crossing or contact count rose, which the guards make impossible. |
Move a 2D mesh's interface vertices onto the mask's own interface locus (opt-in, after the junction relocation).
The 2D port of dw3d's locus projection. Connectivity is frozen: only the
coordinates of interface vertices move, under the guards of :mod:dw2d.junction_relocation.
Why, and why only after the junction relocation¶
dw2d's vertices are pixel centres, while the label image's interface between labels a and b lies
between pixel centres. On the equilibrium-foam benchmark (exact rasterisations of certified
circular-arc foams with known tensions; held-out split, 159 images, min_dist 3), the projection alone made the case angle error
worse (+0.46 deg [+0.19, +0.56]) and the tension error worse (+0.014), but applied after the
junction relocation it lowered the angle error by 2.45 deg [2.07, 2.68] from 9.15 deg and the
gauge-matched tension error by 0.029, and it stayed better than the legacy mesh with smooth boundary
noise of 0.5 and 1 px RMS (-1.96 deg and -1.29 deg; tensions -0.023 and -0.021). The interaction
(harmful alone, helpful composed) is measured, its mechanism is not. Hence
geometry_reconstruction_2d(..., project_interface_vertices=True) requires
relocate_junctions=True.
What it does¶
For every vertex of exactly one interface (a, b) — degree 2, both incident edges carrying the same
pair, not a junction, not on the image border — the target is the zero of
psi_ab(x) = (D_a(x) - D_b(x)) / 2, D_x the Euclidean distance to the nearest pixel centre of label x
(scipy.ndimage.distance_transform_edt), interpolated bilinearly. Newton steps along grad psi, at most
MAX_ITERATIONS, stop at |psi| < TOLERANCE_PX; a vertex whose iteration does not converge, or
would move farther than MAX_DISPLACEMENT_PX, is left in place. Moves are applied in ascending
vertex index under the four guards (short edge, crossing count, edge contact, signed area); whole-mesh
recounts of crossings and of contacts before and after certify the crossing and contact guards
globally (a rise raises). The contact guard is the one that matters here: on the equilibrium-foam
benchmark at 40 px, the projection without it flattened 141 Z-shaped turns of plain interface chains
into an edge touching another and a turn of exactly 180 degrees; with it those moves are refused
and the vertex stays where it was (:mod:dw2d.junction_relocation, "Edge contacts").
Units and conventions: pixels, (row, col), the frame of geometry_reconstruction_2d's vertices; the
label image the mesh was built from (after expansion_labels). Nothing reads ground truth;
deterministic. Cost: one distance transform per label present.
project_interface_vertices ¶
project_interface_vertices(mask: ndarray, points: ndarray, segments: ndarray, labels: ndarray, *, fixed_vertices: Iterable[int] = ()) -> tuple[np.ndarray, InterfaceProjectionReport]
Move every eligible interface vertex of a 2D mesh onto its pair's mask locus (connectivity frozen).
This module's docstring says what it does, under which guards, and why it is offered only after the junction
relocation. dw2d.MeshReconstructionAlgorithm runs it when asked (set_interface_projection).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
ndarray
|
the 2-D label image the mesh was built from (after any label expansion). |
required |
points
|
ndarray
|
|
required |
segments
|
ndarray
|
|
required |
labels
|
ndarray
|
|
required |
fixed_vertices
|
Iterable[int]
|
vertices never moved (the algorithm passes its |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[ndarray, InterfaceProjectionReport]
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if the whole-mesh crossing or contact count rose. |