Skip to content

Mesh utilities

Mesh extraction and cleaning, and the point-coordinate utilities (set_pixel_size, centring, scaling).

Decoupling the surface vertex set from the tesselation point set

dw3d's extraction is primal: a mesh triangle is a face of the tetrahedralisation, so every mesh vertex is a seed point. The boundary layer exploits that the other way round — it adds offset points a distance delta into each adjacent material precisely so that near-interface tetrahedra stop having all four vertices on the interface, which it achieves spectacularly (all-surface tets 40 % -> 0.8 %). But the offsets are then surface vertices too, and they sit delta voxels off the surface by design.

Measurement showed what that costs: the offsets are 30-35 % of mesh vertices, carry epsilon_rms = 2.51 voxels against the other families' 0.58-0.60, and account for 90 % of epsilon^2 — where epsilon is the vertex-to-true-surface distance that sets the junction-angle error through the calibrated error = C * epsilon / ell, C = 33.2 +- 1.2 deg.

exclude_offsets_from_surface removes them from the surface while leaving the tesselation, the watershed and the labelling untouched: each offset is merged onto its parent — the interface sample it is a delta-displacement of, which lies on the interface — and the triangles that collapse as a result are dropped. The surface vertex set becomes a subset of the interface samples and junction samples, i.e. of the points that were placed on the geometry, and the tesselation keeps every point it needs.

Two measurements decided this construction over the more literal alternative (move the vertex to "the EDT zero-set" along a tet edge):

  • The EDT has no zero-set at the interface. compute_edt_classical builds a valley, not a signed distance: interface samples sit at EDT 0.0-1.24 (median 0.82) with the level set by the local boundary thickness, and cell interiors are distance + max(edt_2). The interface is a local minimum set, so "project to the zero level set" is ill-posed; the well-posed operation is a 1-D minimisation across the sheet.
  • Doing that minimisation lands each offset 0.026 voxel (median) from an interface sample that is already a mesh vertex. The offsets carry no independent surface geometry: they are duplicates of the samples they were displaced from. Projecting them therefore produced 1234 triangles under 1 deg of minimum angle, 92 of exactly zero area and 409 normal flips on 3.tif alone — while merging them is exact, needs no interpolation and no threshold.

Sacha Ichbiah 2021. Matthieu Perez 2024.

labeled_mesh_from_labeled_graph

labeled_mesh_from_labeled_graph(tesselation_graph: TesselationGraph, map_label_to_nodes_ids: dict[int, list[int]]) -> tuple[NDArray[np.float64], NDArray[np.uint], NDArray[np.uint]]

Extract a labeled mesh from a labeled tesselation graph.

Winding convention. Every returned triangle has labels[i, 0] < labels[i, 1], and its right-hand-rule normal (p1 - p0) x (p2 - p0) points out of the region labels[i, 0] and into the region labels[i, 1]. This is the convention foambryo's cell volumes rely on (+det to the first label, -det to the second, by the divergence theorem). A triangle on the tesselation's hull (a border triangle, whose one tetrahedron carries a cell label) has labels (0, cell), so its normal points into the cell, i.e. into the hull.

Parameters:

Name Type Description Default
tesselation_graph TesselationGraph

TesselationGraph object

required
map_label_to_nodes_ids dict[int, list[int]]

Labels on this graph.

required

Returns:

Type Description
tuple[NDArray[float64], NDArray[uint], NDArray[uint]]

tuple[NDArray[np.float64], NDArray[np.uint], NDArray[np.uint]]: points, triangles, and labels (materials)

exclude_offsets_from_surface

exclude_offsets_from_surface(points: NDArray[float64], triangles: NDArray[uint], labels: NDArray[uint], surface_merge_target: NDArray[int64], guard_duplicate_faces: bool = True, collapse_rule: str = WELD) -> tuple[NDArray[np.uint], NDArray[np.uint], dict]

Merge every boundary-layer offset vertex onto its parent interface sample.

See the module docstring for why this is a merge and not a displacement. Each merge is a half-edge collapse, and they are independent: surface_merge_target is identity except on the offset families and no offset is ever another offset's target, so the merge itself is one vectorised re-indexing with no ordering to choose.

Geometry is not touched — no point moves — so points is returned to the caller unchanged and the surviving vertices keep the exact coordinates the tesselation gave them. What changes is which points the triangles reference.

Which merges are allowed is set by collapse_rule, and that is the whole difference between the unconditional weld and the link-condition-checked collapse:

  • "weld" (the historical, unconditional-merge behaviour) merges every offset it can, with only the duplicate-face guard below standing in the way. It is not topology preserving: welding two distinct vertices makes formerly distinct edges the same edge, so their triangle incidences add. Measured over the 51-case benchmark this took valence->=4 edges 115 -> 184 and abnormal non-manifold edges 7 -> 76.
  • "link_condition" (the topology-preserving rule) accepts a merge only when contracting the edge (offset, parent) cannot change the topology of the surface. See _refuse_by_link_condition for the three tests and why each is needed. It is the recommended rule; "weld" is kept reachable so the original offset-exclusion measurements stay reproducible.

The guard, and why it is on by default. ("weld" only — the link condition subsumes it, see condition 3 in _refuse_by_link_condition.) A few merges land two triangles on the same vertex triple: a flap flattened onto the film it hangs from. Measured three ways on 3.tif, keeping one copy of each such pair opens 4 boundary edges where the flap was what closed the surface; keeping both copies leaves 55 valence-4 edges (the coincident pairs) against the default's 0; dropping both opens 42. Refusing the handful of merges that would create a duplicate instead leaves the edge valence histogram identical to the default's (0 boundary edges, 227 valence-3, 0 valence-≥4) at the cost of leaving 38 of 650 offsets in the surface — 5.8 %, whose residual contribution to epsilon was measured separately. That is the trade taken, and guard_duplicate_faces=False reproduces the unguarded construction so it stays measurable rather than merely asserted.

The guard iterates because un-merging one offset can leave another duplicate behind; it converges in one round on every case measured, and GUARD_MAX_ROUNDS bounds it. Same-label duplicates that survive the guard are still deduplicated (one copy kept) so the return value is well defined either way.

Parameters:

Name Type Description Default
points NDArray[float64]

tesselation vertices (unchanged; read only to report the surface area before and after).

required
triangles NDArray[uint]

extracted surface triangles, indexing points.

required
labels NDArray[uint]

the (material, material) pair of each triangle.

required
surface_merge_target NDArray[int64]

per tesselation point, the point the surface should use in its place; identity for every non-offset point. From the point-placing scheme's point_metadata (dw3d.points_on_edt).

required
guard_duplicate_faces bool

refuse a merge that would put two triangles on one vertex triple. Defaults to True; see above. Ignored — and reported as ignored — when collapse_rule is "link_condition", which forbids the same thing on stronger grounds.

True
collapse_rule str

"weld" (the unconditional-merge default, unchanged) or "link_condition" (the topology-preserving rule). See above.

WELD

Returns:

Type Description
tuple[NDArray[uint], NDArray[uint], dict]

tuple[NDArray[np.uint], NDArray[np.uint], dict]: the re-indexed triangles, their labels, and a report: collapse_rule — the rule that was applied; n_triangles_before / n_triangles_after; n_triangles_collapsed — dropped for having a repeated vertex after the merge; n_merges_refused — offsets left in the surface by the guard or the link condition; n_guard_rounds — iterations the guard / link-condition sweep needed (0 when it is off or when there was nothing to refuse); n_refused_no_edge / n_refused_link_vertices / n_refused_link_edges / n_refused_valence — the link condition's refusal breakdown, all 0 under "weld"; see _refuse_by_link_condition; n_duplicate_triples — distinct vertex-triples still carrying more than one surviving triangle; n_duplicates_dropped — triangles dropped as same-label duplicates; n_duplicate_conflicts — duplicate triples whose triangles disagree on the label pair. Reported, not repaired: a conflict would mean the merge has fused two different interfaces, so it must be visible rather than silently deduplicated. n_surface_vertices_before / n_surface_vertices_after; n_offset_vertices_removed — surface vertices that were offsets and are gone; area_before / area_after.

set_points_min_max

set_points_min_max(points: NDArray[float64], global_min: float, global_max: float) -> NDArray[np.float64]

Return a homogeneously scaled points array such that its min & max values are global_min and global_max.

The minimum and maximum are taken over all coordinates together, not per axis, so one factor scales every axis and the shape is kept.

set_pixel_size

set_pixel_size(points: NDArray[float64], xy_pixel_size: float, z_pixel_size: float) -> NDArray[np.float64]

Return a scaled points array from voxel coordinates to physical coordinates.

Given the microscope's xy and z pixel sizes. The points are in the mask's index order: column i is the voxel index along the mask's axis i. For a stack stored as (z, y, x), the usual order of a microscope stack and the one dw3d assumes, column 0 is scaled by z_pixel_size and columns 1 and 2 by xy_pixel_size.

Changed in 0.7.0: before, columns 0 and 1 were scaled by xy_pixel_size and column 2 by z_pixel_size, which multiplied z by the xy size and x by the z size. Results differ only when the two sizes differ.

For a mask whose voxels are longer along z, prefer MeshReconstructionAlgorithm.construct_mesh_in_physical_units: it resamples the mask to cubic voxels before meshing, which is more accurate than meshing the native grid and scaling the mesh.

Parameters:

Name Type Description Default
points NDArray[float64]

(n, 3) points in voxel units, (z, y, x) order.

required
xy_pixel_size float

the pixel size in the image plane, which multiplies columns 1 and 2.

required
z_pixel_size float

the z step, which multiplies column 0.

required

Returns:

Type Description
NDArray[float64]

NDArray[np.float64]: a new (n, 3) array; points is not modified.

center_around_origin

center_around_origin(points: NDArray[float64]) -> NDArray[np.float64]

Return a copy of the points translated so that the centre of their bounding box is the origin.

filter_unused_points

filter_unused_points(points: NDArray[float64], triangles: NDArray[ulonglong]) -> tuple[NDArray[np.float64], NDArray[np.ulonglong]]

Take a mesh made from points and triangles and remove points not indexed in triangles. Re-index triangles.

Return the filtered points and reindexed triangles.