Skip to content

Self-intersections

Exact nonlocal triangle-triangle intersection for a surface mesh.

Two triangles that share a vertex index are adjacent and are never tested: adjacent triangles touch by construction, and in a correct multimaterial mesh three triangles meet along every trijunction edge, so counting those would report the intended non-manifold structure as a defect. Every other pair is nonlocal and is tested exactly.

Why the library carries this and not only the benchmarks

Every other validity property a mesh consumer can check cheaply is local or combinatorial: watertightness, non-manifold edges, degenerate and duplicate faces, interface identity. A mesh whose two distant sheets pass through each other satisfies all of them. Measured on this package's own benchmark cohort: all twelve local predicates stayed bit-identical on 40 of 40 cases while 23 nonlocal crossings existed on 5 of them, introduced by a post-process whose guard checked only the triangles incident to the vertex it moved.

A local guard cannot certify a nonlocal property. :mod:dw3d.junction_relocation therefore uses this predicate as its own acceptance test, which is why the predicate belongs to the library rather than to a benchmark package the library must not depend on.

The test

For each candidate pair, the exact Moller triangle-triangle predicate:

  1. Signed distances of triangle B's vertices to triangle A's plane. If all three carry the same strict sign, that plane separates them and they cannot meet.
  2. The same with the roles exchanged.
  3. If either triangle is coplanar with the other's plane, fall back to a 2-D test in that plane: any edge-edge crossing, or either triangle's vertex inside the other.
  4. Otherwise both triangles cross the line where the two planes meet. Each cuts an interval on that line, and the triangles meet iff the two intervals overlap.

Steps 1, 2 and 4 are the standard construction and are exact up to floating point. The predicate is checked against an independent Moller-Trumbore segment-triangle oracle that shares no arithmetic with it, over random pairs; that differential test lives beside the benchmark evaluator.

Complexity

Candidate pairs come from a cKDTree on triangle centroids queried at twice the largest circumradius about the centroid. That bound is sound: two triangles whose centroids are further apart than the sum of their circumradii cannot meet, and twice the largest circumradius is at least that sum. The exact predicate then runs vectorised over the candidate set.

Units, tolerances and failure modes

Coordinates are the mesh's own units -- mask voxels for a reconstruction read in the mask frame -- and all arithmetic is float64. :data:CONTACT_TOLERANCE is an absolute distance in those units: a pair whose contact is within it is reported as touching rather than counted as an intersection, and both numbers are emitted so a borderline pair cannot hide inside a single total. A degenerate triangle has no well-defined plane; such triangles are excluded from the test and counted separately, because silently skipping one would let a sliver hide a crossing. A mesh with fewer than two triangles returns zero counts.

A self-intersection is not affine-invariant in general, so a mesh and its registered copy are two different meshes for this predicate and must be reported as such.

The count is a count, never a score, and no threshold is proposed here or anywhere else.

No new dependency: numpy and scipy only, both already required.

count_self_intersections

count_self_intersections(points: NDArray[float64], triangles: NDArray[integer]) -> dict

The nonlocal self-intersection counts, without the pair arrays.

A thin, JSON-serialisable projection of :func:find_self_intersections carrying exactly :data:SELF_INTERSECTION_KEYS and no array.

Parameters:

Name Type Description Default
points NDArray[float64]

vertex coordinates, the mesh's own units.

required
triangles NDArray[integer]

triangle vertex indices.

required

Returns:

Name Type Description
dict dict

the six keys of :data:SELF_INTERSECTION_KEYS. A count, never a score.

triangle_centroids_and_circumradii

triangle_centroids_and_circumradii(vertices: NDArray[float64]) -> tuple[NDArray[np.float64], NDArray[np.float64]]

Each triangle's centroid and the radius of the smallest ball about it containing it.

Parameters:

Name Type Description Default
vertices NDArray[float64]

triangle corner coordinates, (m, 3, 3).

required

Returns:

Name Type Description
tuple NDArray[float64]

(centroids, radii), shapes (m, 3) and (m,). Two triangles whose centroids are

NDArray[float64]

further apart than the sum of their radii cannot intersect, which is what makes a

tuple[NDArray[float64], NDArray[float64]]

centroid-radius neighbour search a sound filter rather than a heuristic.

twice_areas

twice_areas(vertices: NDArray[float64]) -> NDArray[np.float64]

Twice each triangle's area, from the cross product of two of its edges.

Parameters:

Name Type Description Default
vertices NDArray[float64]

triangle corner coordinates, (m, 3, 3).

required

Returns:

Type Description
NDArray[float64]

NDArray[np.float64]: one value per triangle; below :data:DEGENERATE_TWICE_AREA the

NDArray[float64]

triangle has no usable plane.

drop_adjacent_pairs

drop_adjacent_pairs(pairs: NDArray[int64], triangles: NDArray[int64]) -> NDArray[np.int64]

Remove the pairs that share at least one vertex index.

Parameters:

Name Type Description Default
pairs NDArray[int64]

candidate index pairs, (k, 2), into triangles.

required
triangles NDArray[int64]

triangle vertex indices.

required

Returns:

Type Description
NDArray[int64]

NDArray[np.int64]: the nonlocal subset, (k', 2).

pair_intersection_flags

pair_intersection_flags(vertices: NDArray[float64], pairs: NDArray[int64]) -> tuple[NDArray[np.bool_], NDArray[np.bool_]]

Run the exact predicate on an explicit list of triangle pairs.

This is the kernel both the whole-mesh certificate and any incremental guard must share, so that a guard and the certificate judging it cannot disagree about what an intersection is. Adjacency is not filtered here: the caller decides which pairs are nonlocal.

Parameters:

Name Type Description Default
vertices NDArray[float64]

triangle corner coordinates, (m, 3, 3), float64.

required
pairs NDArray[int64]

index pairs into vertices, (k, 2).

required

Returns:

Name Type Description
tuple NDArray[bool_]

(intersects, touching), boolean arrays of length k. A pair is touching when

NDArray[bool_]

its contact lies within :data:CONTACT_TOLERANCE; it is then not counted as an

tuple[NDArray[bool_], NDArray[bool_]]

intersection, and both flags are false for a separated pair.

find_self_intersections

find_self_intersections(points: NDArray[float64], triangles: NDArray[integer]) -> dict

Every nonlocal triangle-triangle intersection of one surface mesh, and the pairs themselves.

Parameters:

Name Type Description Default
points NDArray[float64]

vertex coordinates, the mesh's own units.

required
triangles NDArray[integer]

triangle vertex indices, (m, 3).

required

Returns:

Name Type Description
dict dict

the six counts of :data:SELF_INTERSECTION_KEYS plus intersecting_pairs and touching_pairs, each an (k, 2) array of indices into the input triangles, so a caller can draw or diagnose a specific crossing. Degenerate triangles are excluded from the test and counted.