Skip to content

validation

triwarp.validation

Mesh diagnostics: topological/geometric validity predicates and their per-element masks.

is_edge_manifold

is_edge_manifold(
    faces: array[int32],
    allow_boundary_edges: bool = True,
    *,
    edges_sorted: Array2dInt32 | None = None,
    n_vertices: int | None = None,
) -> bool

Whether every undirected mesh edge is shared by a manifold number of faces.

Counts how many faces share each undirected edge. With allow_boundary_edges=True an edge is manifold when it belongs to one or two faces; with allow_boundary_edges=False it must belong to exactly two faces (no boundary or non-manifold edges), matching Open3D's IsEdgeManifold.

Parameters:

Name Type Description Default
faces array[int32]

Length-3 * n_faces wp.int32 flat triangle index buffer.

required
allow_boundary_edges bool

When True (default) boundary edges (used by a single face) are allowed. When False every edge must be shared by exactly two faces.

True
edges_sorted Array2dInt32 | None

Optional precomputed (n_faces * 3, 2) sorted edges in faces_to_edges row order. When None, built from faces.

None
n_vertices int | None

Optional vertex count (the edge-hash base). When None, inferred from faces.

None

Returns:

Type Description
bool

True when all edges satisfy the manifold condition. Vacuously True for an empty mesh.

Raises:

Type Description
RuntimeError

If faces and edges_sorted are not all on one device.

See Also

is_vertex_manifold is_watertight

Notes

Equivalent to open3d.geometry.TriangleMesh.is_edge_manifold; libigl is_edge_manifold corresponds to the allow_boundary_edges=True case.

Deliberately not all(edge_manifold_mask(faces)), unlike the other is_* / *_mask pairs here: this reduces the per-edge counts directly, where the mask additionally needs unique_1d's inverse and a per-face gather pass. Delegating would add both to the cheap path.

Source code in triwarp/validation.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def is_edge_manifold(
    faces: wp.array[wp.int32],
    allow_boundary_edges: bool = True,
    *,
    edges_sorted: twt.Array2dInt32 | None = None,
    n_vertices: int | None = None,
) -> bool:
    """
    Whether every undirected mesh edge is shared by a manifold number of faces.

    Counts how many faces share each undirected edge. With ``allow_boundary_edges=True`` an edge is
    manifold when it belongs to one or two faces; with ``allow_boundary_edges=False`` it must belong
    to exactly two faces (no boundary or non-manifold edges), matching Open3D's ``IsEdgeManifold``.

    Parameters
    ----------
    faces
        Length-``3 * n_faces`` ``wp.int32`` flat triangle index buffer.
    allow_boundary_edges
        When ``True`` (default) boundary edges (used by a single face) are allowed. When ``False``
        every edge must be shared by exactly two faces.
    edges_sorted
        Optional precomputed ``(n_faces * 3, 2)`` sorted edges in
        [`faces_to_edges`][triwarp.edges.faces_to_edges] row order. When ``None``, built from
        ``faces``.
    n_vertices
        Optional vertex count (the edge-hash base). When ``None``, inferred from ``faces``.

    Returns
    -------
    bool
        ``True`` when all edges satisfy the manifold condition. Vacuously ``True`` for an empty
        mesh.

    Raises
    ------
    RuntimeError
        If ``faces`` and ``edges_sorted`` are not all on one device.

    See Also
    --------
    [`is_vertex_manifold`][triwarp.validation.is_vertex_manifold]
    [`is_watertight`][triwarp.validation.is_watertight]

    Notes
    -----
    Equivalent to ``open3d.geometry.TriangleMesh.is_edge_manifold``; libigl ``is_edge_manifold``
    corresponds to the ``allow_boundary_edges=True`` case.

    Deliberately **not** ``all(edge_manifold_mask(faces))``, unlike the other ``is_*`` / ``*_mask``
    pairs here: this reduces the per-edge counts directly, where the mask additionally needs
    ``unique_1d``'s inverse and a per-face gather pass. Delegating would add both to the cheap path.
    """
    require_same_device(faces=faces, edges_sorted=edges_sorted)
    n_faces = int(faces.shape[0]) // 3
    if n_faces == 0:
        return True

    if edges_sorted is None:
        edges_sorted = tw.edges.faces_to_edges(faces, sorted=True)
    if n_vertices is None:
        n_vertices = tw.array.index_bound(faces)
    keys = tw.grouping.hash_indices_rows(edges_sorted, max_index=n_vertices)
    _, counts = tw.grouping.unique_1d(keys, return_counts=True)

    min_count, max_count = tw.reduce.minmax(cast(twt.Array1dInt32, counts))
    if allow_boundary_edges:
        return max_count <= 2
    return min_count == 2 and max_count == 2

edge_manifold_mask

edge_manifold_mask(
    faces: array[int32],
    allow_boundary_edges: bool = True,
    *,
    edges_sorted: Array2dInt32 | None = None,
) -> wp.array[wp.bool]

Per-face flag: whether all three of each face's undirected edges are edge-manifold.

An edge is manifold when shared by one or two faces (allow_boundary_edges=True) or by exactly two faces (allow_boundary_edges=False); a face is flagged True only when all three of its edges qualify. This is a per-face collapse of libigl's BF (its per-corner is_edge_manifold matrix), and is_edge_manifold is True iff every entry of this mask is True.

Parameters:

Name Type Description Default
faces array[int32]

Length-3 * n_faces wp.int32 flat triangle index buffer.

required
allow_boundary_edges bool

When True (default) boundary edges (used by a single face) count as manifold. When False every edge of a face must be shared by exactly two faces.

True
edges_sorted Array2dInt32 | None

Optional precomputed (n_faces * 3, 2) sorted edges in faces_to_edges row order (each row min-first). When None, built from faces.

None

Returns:

Type Description
array[bool]

Length n_faces on faces.device. Empty for an empty mesh.

Raises:

Type Description
RuntimeError

If faces and edges_sorted are not all on one device.

See Also

is_edge_manifold vertex_manifold_mask

Source code in triwarp/validation.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
def edge_manifold_mask(
    faces: wp.array[wp.int32],
    allow_boundary_edges: bool = True,
    *,
    edges_sorted: twt.Array2dInt32 | None = None,
) -> wp.array[wp.bool]:
    """
    Per-face flag: whether all three of each face's undirected edges are edge-manifold.

    An edge is manifold when shared by one or two faces (``allow_boundary_edges=True``) or by
    exactly two faces (``allow_boundary_edges=False``); a face is flagged ``True`` only when all
    three of its edges qualify. This is a per-face collapse of libigl's ``BF`` (its per-corner
    ``is_edge_manifold`` matrix), and
    [`is_edge_manifold`][triwarp.validation.is_edge_manifold] is ``True`` iff every entry of
    this mask is ``True``.

    Parameters
    ----------
    faces
        Length-``3 * n_faces`` ``wp.int32`` flat triangle index buffer.
    allow_boundary_edges
        When ``True`` (default) boundary edges (used by a single face) count as manifold. When
        ``False`` every edge of a face must be shared by exactly two faces.
    edges_sorted
        Optional precomputed ``(n_faces * 3, 2)`` sorted edges in
        [`faces_to_edges`][triwarp.edges.faces_to_edges] row order (each row min-first). When
        ``None``, built from ``faces``.

    Returns
    -------
    wp.array[wp.bool]
        Length ``n_faces`` on ``faces.device``. Empty for an empty mesh.

    Raises
    ------
    RuntimeError
        If ``faces`` and ``edges_sorted`` are not all on one device.

    See Also
    --------
    [`is_edge_manifold`][triwarp.validation.is_edge_manifold]
    [`vertex_manifold_mask`][triwarp.validation.vertex_manifold_mask]
    """
    require_same_device(faces=faces, edges_sorted=edges_sorted)
    n_faces = int(faces.shape[0]) // 3
    device = faces.device
    if n_faces == 0:
        return wp.empty(0, dtype=wp.bool, device=device)

    if edges_sorted is None:
        edges_sorted = tw.edges.faces_to_edges(faces, sorted=True)
    keys = tw.grouping.hash_indices_rows(edges_sorted, max_index=tw.array.index_bound(faces))
    _, inverse, counts = tw.grouping.unique_1d(keys, return_inverse=True, return_counts=True)

    n_unique = int(counts.shape[0])
    edge_ok = wp.empty(n_unique, dtype=wp.bool, device=device)
    wp.map(kernel_validation.edge_manifold, counts, wp.bool(allow_boundary_edges), out=edge_ok)

    out_mask = wp.empty(n_faces, dtype=wp.bool, device=device)
    wp.launch(
        kernel_validation.face_edge_manifold_mask,
        dim=n_faces,
        inputs=[inverse, edge_ok, out_mask],
        device=device,
    )
    return out_mask

is_vertex_manifold

is_vertex_manifold(
    faces: array[int32],
    *,
    face_adjacency: Array2dInt32 | None = None,
    face_adjacency_edges: Array2dInt32 | None = None,
) -> bool

Whether every referenced vertex has a single edge-connected fan of faces.

A vertex is manifold when the faces incident to it form one connected group under "shares an edge through this vertex" adjacency; two fans meeting only at the vertex (a bow-tie) are non-manifold. Unreferenced vertices in [0, max(faces)] are treated as non-manifold, matching libigl's is_vertex_manifold and Open3D's IsVertexManifold.

The check builds the corner graph (node 3 * f + k per face corner), links the corresponding corners of edge-adjacent faces, and requires all corners of each vertex to fall in one connected component.

Parameters:

Name Type Description Default
faces array[int32]

Length-3 * n_faces wp.int32 flat triangle index buffer.

required
face_adjacency Array2dInt32 | None

Optional precomputed (m, 2) face-adjacency pairs from face_adjacency. Must be given together with face_adjacency_edges.

None
face_adjacency_edges Array2dInt32 | None

Optional (m, 2) shared-edge vertex pairs aligned with face_adjacency (return_edges=True). Must be given together with face_adjacency.

None

Returns:

Type Description
bool

True when every referenced vertex is manifold. Vacuously True for an empty mesh.

Raises:

Type Description
ValueError

If exactly one of face_adjacency / face_adjacency_edges is provided.

RuntimeError

If faces, face_adjacency and face_adjacency_edges are not all on one device.

See Also

vertex_manifold_mask is_edge_manifold is_watertight

Notes

Corner adjacency is derived from face_adjacency, which pairs faces across edges shared by exactly two faces; on edge-non-manifold meshes (an edge shared by three or more faces) read the result together with is_edge_manifold. Equivalent to libigl is_vertex_manifold. The output is sized to max(faces) + 1 so unreferenced vertices in that range count as non-manifold; use vertex_manifold_mask for a per-vertex flag sized to a caller-provided vertex buffer.

Source code in triwarp/validation.py
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
def is_vertex_manifold(
    faces: wp.array[wp.int32],
    *,
    face_adjacency: twt.Array2dInt32 | None = None,
    face_adjacency_edges: twt.Array2dInt32 | None = None,
) -> bool:
    """
    Whether every referenced vertex has a single edge-connected fan of faces.

    A vertex is manifold when the faces incident to it form one connected group under
    "shares an edge through this vertex" adjacency; two fans meeting only at the vertex (a bow-tie)
    are non-manifold. Unreferenced vertices in ``[0, max(faces)]`` are treated as non-manifold,
    matching libigl's ``is_vertex_manifold`` and Open3D's ``IsVertexManifold``.

    The check builds the corner graph (node ``3 * f + k`` per face corner), links the corresponding
    corners of edge-adjacent faces, and requires all corners of each vertex to fall in one connected
    component.

    Parameters
    ----------
    faces
        Length-``3 * n_faces`` ``wp.int32`` flat triangle index buffer.
    face_adjacency
        Optional precomputed ``(m, 2)`` face-adjacency pairs from
        [`face_adjacency`][triwarp.adjacency.face_adjacency]. Must be given together with
        ``face_adjacency_edges``.
    face_adjacency_edges
        Optional ``(m, 2)`` shared-edge vertex pairs aligned with ``face_adjacency``
        (``return_edges=True``). Must be given together with ``face_adjacency``.

    Returns
    -------
    bool
        ``True`` when every referenced vertex is manifold. Vacuously ``True`` for an empty mesh.

    Raises
    ------
    ValueError
        If exactly one of ``face_adjacency`` / ``face_adjacency_edges`` is provided.
    RuntimeError
        If ``faces``, ``face_adjacency`` and ``face_adjacency_edges`` are not all on one device.

    See Also
    --------
    [`vertex_manifold_mask`][triwarp.validation.vertex_manifold_mask]
    [`is_edge_manifold`][triwarp.validation.is_edge_manifold]
    [`is_watertight`][triwarp.validation.is_watertight]

    Notes
    -----
    Corner adjacency is derived from
    [`face_adjacency`][triwarp.adjacency.face_adjacency], which pairs faces across edges shared by
    exactly two faces; on edge-non-manifold meshes (an edge shared by three or more faces) read the
    result together with [`is_edge_manifold`][triwarp.validation.is_edge_manifold]. Equivalent
    to libigl ``is_vertex_manifold``. The output is sized to ``max(faces) + 1`` so unreferenced
    vertices in that range count as non-manifold; use
    [`vertex_manifold_mask`][triwarp.validation.vertex_manifold_mask] for a per-vertex flag
    sized to a caller-provided vertex buffer.
    """
    require_same_device(
        faces=faces, face_adjacency=face_adjacency, face_adjacency_edges=face_adjacency_edges
    )
    # Checked before the empty-mesh guard so a caller passing only one half of the pair is told
    # about it whatever the mesh is; resolved after it, because on an empty buffer the resolve is
    # two empty tables nothing reads. Every wrapper taking this pair does it in this order.
    tw.adjacency.require_paired_adjacency(face_adjacency, face_adjacency_edges)
    if int(faces.shape[0]) // 3 == 0:
        return True
    if face_adjacency is None:
        face_adjacency, face_adjacency_edges = tw.adjacency.face_adjacency(faces, return_edges=True)
    assert face_adjacency_edges is not None
    manifold = _vertex_manifold_flags(
        faces, tw.array.index_bound(faces), face_adjacency, face_adjacency_edges
    )
    return bool(tw.reduce.all(manifold))

vertex_manifold_mask

vertex_manifold_mask(
    vertices: array[vec3], faces: array[int32]
) -> wp.array[wp.bool]

Per-vertex flag: whether each vertex has a single edge-connected fan of faces.

A vertex is manifold when the faces incident to it form one connected group under "shares an edge through this vertex" adjacency; two fans meeting only at the vertex (a bow-tie) are non-manifold. Unreferenced vertices are flagged False, matching libigl's is_vertex_manifold per-vertex output B and Open3D's GetNonManifoldVertices.

Parameters:

Name Type Description Default
vertices array[vec3]

(n_vertices,) vertex positions. Only the length is used; it sets the output size so that trailing unreferenced vertices are reported (False).

required
faces array[int32]

Length-3 * n_faces wp.int32 flat triangle index buffer.

required

Returns:

Type Description
array[bool]

Length n_vertices on faces.device.

Raises:

Type Description
RuntimeError

If vertices and faces are not all on one device.

See Also

is_vertex_manifold edge_manifold_mask

Source code in triwarp/validation.py
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
def vertex_manifold_mask(
    vertices: wp.array[wp.vec3], faces: wp.array[wp.int32]
) -> wp.array[wp.bool]:
    """
    Per-vertex flag: whether each vertex has a single edge-connected fan of faces.

    A vertex is manifold when the faces incident to it form one connected group under
    "shares an edge through this vertex" adjacency; two fans meeting only at the vertex (a bow-tie)
    are non-manifold. Unreferenced vertices are flagged ``False``, matching libigl's
    ``is_vertex_manifold`` per-vertex output ``B`` and Open3D's ``GetNonManifoldVertices``.

    Parameters
    ----------
    vertices
        ``(n_vertices,)`` vertex positions. Only the length is used; it sets the output size so that
        trailing unreferenced vertices are reported (``False``).
    faces
        Length-``3 * n_faces`` ``wp.int32`` flat triangle index buffer.

    Returns
    -------
    wp.array[wp.bool]
        Length ``n_vertices`` on ``faces.device``.

    Raises
    ------
    RuntimeError
        If ``vertices`` and ``faces`` are not all on one device.

    See Also
    --------
    [`is_vertex_manifold`][triwarp.validation.is_vertex_manifold]
    [`edge_manifold_mask`][triwarp.validation.edge_manifold_mask]
    """
    require_same_device(vertices=vertices, faces=faces)
    n_vertices = int(vertices.shape[0])
    if int(faces.shape[0]) // 3 == 0:
        return wp.zeros(n_vertices, dtype=wp.bool, device=faces.device)
    adjacency, adjacency_edges = tw.adjacency.face_adjacency(
        faces, return_edges=True, n_vertices=n_vertices
    )
    return _vertex_manifold_flags(faces, n_vertices, adjacency, adjacency_edges)

is_self_intersecting

is_self_intersecting(
    mesh: Mesh, *, max_triangle_collisions: int = 32
) -> bool

Whether any two non-adjacent triangles of the mesh intersect.

Broad phase queries each triangle's AABB against mesh's BVH for candidate overlaps; narrow phase runs Moller's interval test on each candidate pair, skipping pairs that share a vertex. Mirrors Open3D's IsSelfIntersecting (AABB pre-test followed by a triangle-triangle test on non-neighbouring faces). Coplanar and merely touching pairs are not intersections -- contact alone does not count.

Parameters:

Name Type Description Default
mesh Mesh

Mesh to test; mesh.points and mesh.indices are used directly, so its BVH is always in sync with the triangles being tested (a separately-passed vertex/face pair could otherwise be misaligned with a caller's mesh).

required
max_triangle_collisions int

Broad-phase candidate cap per query triangle. Raise this for meshes with many triangles packed into overlapping bounding boxes.

32

Returns:

Type Description
bool

True when at least one intersecting non-adjacent triangle pair exists. False for meshes with fewer than two faces.

See Also

face_self_intersecting_mask mesh_with_mesh is_watertight

Notes

Equivalent to open3d.geometry.TriangleMesh.is_self_intersecting.

Source code in triwarp/validation.py
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
def is_self_intersecting(mesh: wp.Mesh, *, max_triangle_collisions: int = 32) -> bool:
    """
    Whether any two non-adjacent triangles of the mesh intersect.

    Broad phase queries each triangle's AABB against ``mesh``'s BVH for candidate overlaps;
    narrow phase runs Moller's interval test on each candidate pair, skipping pairs that share a
    vertex. Mirrors Open3D's ``IsSelfIntersecting`` (AABB pre-test followed by a triangle-triangle
    test on non-neighbouring faces). Coplanar and merely touching pairs are **not** intersections
    -- contact alone does not count.

    Parameters
    ----------
    mesh
        Mesh to test; ``mesh.points`` and ``mesh.indices`` are used directly, so its BVH is
        always in sync with the triangles being tested (a separately-passed vertex/face pair
        could otherwise be misaligned with a caller's ``mesh``).
    max_triangle_collisions
        Broad-phase candidate cap per query triangle. Raise this for meshes with many triangles
        packed into overlapping bounding boxes.

    Returns
    -------
    bool
        ``True`` when at least one intersecting non-adjacent triangle pair exists. ``False`` for
        meshes with fewer than two faces.

    See Also
    --------
    [`face_self_intersecting_mask`][triwarp.validation.face_self_intersecting_mask]
    [`mesh_with_mesh`][triwarp.intersection.mesh_with_mesh]
    [`is_watertight`][triwarp.validation.is_watertight]

    Notes
    -----
    Equivalent to ``open3d.geometry.TriangleMesh.is_self_intersecting``.
    """
    found = _intersecting_pairs(mesh, mesh.points, mesh.indices, max_triangle_collisions)
    if found is None:
        return False
    _pairs, valid = found
    return bool(tw.reduce.any(valid))

face_self_intersecting_mask

face_self_intersecting_mask(
    vertices: array[vec3],
    faces: array[int32],
    *,
    max_triangle_collisions: int = 32,
    mesh: Mesh | None = None,
) -> wp.array[wp.bool]

Per-face flag: whether each triangle intersects some non-adjacent triangle.

Broad phase builds a warp.Mesh and queries each triangle's AABB for candidate overlaps; narrow phase runs Moller's interval test on each candidate pair (skipping pairs that share a vertex), and both faces of every intersecting pair are flagged. is_self_intersecting is True iff any entry of this mask is True.

A pair that is coplanar, or that touches without crossing, is not an intersection.

The narrow phase runs in float64 on the float32 vertices, and the reason is worth knowing rather than taking as caution: widening is lossless, so the geometry is unchanged, and what the precision buys is the decisions -- the sign of a plane distance and the overlap of two intervals. Audited face for face against an independent float64 Moller implementation over the six self-intersecting parametric surfaces plus a self-intersecting torus, at a scale-relative tolerance of 1e-9:

input reference this second library pymeshfix
boy 177 177 177 436 (cut)
klein 203 203 203 203
bohemian_dome 161 161 161, 1 wrong either way 168
figure8_klein 174 174 190, 16 wrong 336 (cut)
torus (16x16) 64 64 64 64
cross_cap 73 74 74 336 (cut)
roman 154 158 158 514 (cut)

Exact on five of seven, and identical to the second library on the other two -- where both differ from the reference by 4 faces or fewer, all of them a vertex lying on the other triangle's plane, which is a tolerance choice rather than a fact. In float32 the same audit read 20 false positives and 88 false negatives on roman alone, 2 and 4 on the torus, and 1 false negative on the dome. The cost is ~4 % of this call (the narrow phase is 6-8 % of it and doubles).

The pymeshfix column is the newest and it reads two ways at once. Where it analyses the mesh it was handed, it agrees exactly -- 203 on klein, 64 on the torus -- and on the tangency class it lands 7 faces away, the same order as every other implementation. The four rows marked (cut) are not disagreements: its loader repairs connectivity before anything else and cuts the orientation-reversing seam of a non-orientable surface, taking boy from 1 483 to 1 559 vertices at an unchanged 2 964 faces. That leaves two coincident sheets where the surface had one, and every face along the cut then genuinely does intersect. So a comparison against it has to establish that the load changed nothing before it means anything.

Where a comparison against another library is wanted on the singular-curve class (roman, cross_cap), compare counts, not sets: which faces sit exactly on a tangency is the part no two implementations agree on.

Parameters:

Name Type Description Default
vertices array[vec3]

(n_vertices,) vertex positions.

required
faces array[int32]

Length-3 * n_faces wp.int32 flat triangle index buffer.

required
max_triangle_collisions int

Broad-phase candidate cap per query triangle. Raise this for meshes with many triangles packed into overlapping bounding boxes.

32
mesh Mesh | None

A wp.Mesh already built over vertices and faces, to spare the BVH build the broad phase otherwise pays on every call. Purely an optimization, and not checked against vertices / faces.

None

Returns:

Type Description
array[bool]

Length n_faces on faces.device. All-False for meshes with fewer than two faces.

Raises:

Type Description
RuntimeError

If vertices, faces and mesh are not all on one device.

See Also

is_self_intersecting

Source code in triwarp/validation.py
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
def face_self_intersecting_mask(
    vertices: wp.array[wp.vec3],
    faces: wp.array[wp.int32],
    *,
    max_triangle_collisions: int = 32,
    mesh: wp.Mesh | None = None,
) -> wp.array[wp.bool]:
    """
    Per-face flag: whether each triangle intersects some non-adjacent triangle.

    Broad phase builds a ``warp.Mesh`` and queries each triangle's AABB for candidate overlaps;
    narrow phase runs Moller's interval test on each candidate pair (skipping pairs that share a
    vertex), and both faces of every intersecting pair are flagged.
    [`is_self_intersecting`][triwarp.validation.is_self_intersecting] is ``True`` iff any entry
    of this mask is ``True``.

    A pair that is coplanar, or that touches without crossing, is not an intersection.

    **The narrow phase runs in ``float64`` on the ``float32`` vertices**, and the reason is worth
    knowing rather than taking as caution: widening is lossless, so the geometry is unchanged, and
    what the precision buys is the decisions -- the sign of a plane distance and the overlap of two
    intervals. Audited face for face against an independent ``float64`` Moller implementation over
    the six self-intersecting parametric surfaces plus a self-intersecting torus, at a
    scale-relative tolerance of ``1e-9``:

    | input | reference | this | second library | pymeshfix |
    |---|---|---|---|---|
    | ``boy`` | 177 | **177** | 177 | 436 (cut) |
    | ``klein`` | 203 | **203** | 203 | **203** |
    | ``bohemian_dome`` | 161 | **161** | 161, 1 wrong either way | 168 |
    | ``figure8_klein`` | 174 | **174** | 190, 16 wrong | 336 (cut) |
    | ``torus`` (16x16) | 64 | **64** | 64 | **64** |
    | ``cross_cap`` | 73 | 74 | 74 | 336 (cut) |
    | ``roman`` | 154 | 158 | 158 | 514 (cut) |

    Exact on five of seven, and identical to the second library on the other two -- where both
    differ from the reference by 4 faces or fewer, all of them a vertex lying *on* the other
    triangle's plane, which is a tolerance choice rather than a fact. In ``float32`` the same audit
    read 20 false positives and 88 false negatives on ``roman`` alone, 2 and 4 on the torus, and 1
    false negative on the dome. The cost is ~4 % of this call (the narrow phase is 6-8 % of it and
    doubles).

    The pymeshfix column is the newest and it reads two ways at once. Where it analyses the mesh it
    was handed, it agrees **exactly** -- 203 on ``klein``, 64 on the torus -- and on the tangency
    class it lands 7 faces away, the same order as every other implementation. The four rows marked
    *(cut)* are not disagreements: its loader repairs connectivity before anything else and cuts the
    orientation-reversing seam of a non-orientable surface, taking ``boy`` from 1 483 to 1 559
    vertices at an unchanged 2 964 faces. That leaves two coincident sheets where the surface had
    one, and every face along the cut then genuinely does intersect. So a comparison against it has
    to establish that the load changed nothing before it means anything.

    Where a comparison against another library is wanted on the singular-curve class (``roman``,
    ``cross_cap``), compare **counts, not sets**: which faces sit exactly on a tangency is the part
    no two implementations agree on.

    Parameters
    ----------
    vertices
        ``(n_vertices,)`` vertex positions.
    faces
        Length-``3 * n_faces`` ``wp.int32`` flat triangle index buffer.
    max_triangle_collisions
        Broad-phase candidate cap per query triangle. Raise this for meshes with many triangles
        packed into overlapping bounding boxes.
    mesh
        A ``wp.Mesh`` already built over ``vertices`` and ``faces``, to spare the BVH build the
        broad phase otherwise pays on every call. Purely an optimization, and not checked against
        ``vertices`` / ``faces``.

    Returns
    -------
    wp.array[wp.bool]
        Length ``n_faces`` on ``faces.device``. All-``False`` for meshes with fewer than two faces.

    Raises
    ------
    RuntimeError
        If ``vertices``, ``faces`` and ``mesh`` are not all on one device.

    See Also
    --------
    [`is_self_intersecting`][triwarp.validation.is_self_intersecting]
    """
    require_same_device(vertices=vertices, faces=faces, mesh=mesh)
    device = vertices.device
    n_faces = int(faces.shape[0]) // 3
    mask = wp.zeros(n_faces, dtype=wp.bool, device=device)
    if n_faces < 2:
        return mask

    if mesh is None:
        require_nonempty_mesh(faces, "face_self_intersecting_mask")
        mesh = wp.Mesh(points=vertices, indices=faces)

    found = _intersecting_pairs(mesh, vertices, faces, max_triangle_collisions)
    if found is None:
        return mask
    pairs, valid = found
    wp.launch(
        kernel_validation.mark_intersecting_faces,
        dim=int(pairs.shape[0]),
        inputs=[pairs, valid, mask],
        device=device,
    )
    return mask

is_winding_consistent

is_winding_consistent(faces: array[int32]) -> bool

Whether every shared edge is traversed in opposite directions by its two faces.

A mesh has consistent winding when, for each edge shared by two faces, the two faces list the edge's endpoints in opposite order (so their normals agree locally). This is a property of the current winding, unlike is_orientable, which allows faces to be flipped. Matches trimesh.Trimesh.is_winding_consistent.

Parameters:

Name Type Description Default
faces array[int32]

Length-3 * n_faces wp.int32 flat triangle index buffer.

required

Returns:

Type Description
bool

True when winding is consistent across all shared edges. Vacuously True for an empty mesh or a mesh with no shared edges.

See Also

edge_winding_consistent_mask is_orientable is_volume trimesh.Trimesh.is_winding_consistent

Source code in triwarp/validation.py
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
def is_winding_consistent(faces: wp.array[wp.int32]) -> bool:
    """
    Whether every shared edge is traversed in opposite directions by its two faces.

    A mesh has consistent winding when, for each edge shared by two faces, the two faces list the
    edge's endpoints in opposite order (so their normals agree locally). This is a property of the
    current winding, unlike [`is_orientable`][triwarp.validation.is_orientable], which allows
    faces to be flipped. Matches [`trimesh.Trimesh.is_winding_consistent`][].

    Parameters
    ----------
    faces
        Length-``3 * n_faces`` ``wp.int32`` flat triangle index buffer.

    Returns
    -------
    bool
        ``True`` when winding is consistent across all shared edges. Vacuously ``True`` for an empty
        mesh or a mesh with no shared edges.

    See Also
    --------
    [`edge_winding_consistent_mask`][triwarp.validation.edge_winding_consistent_mask]
    [`is_orientable`][triwarp.validation.is_orientable]
    [`is_volume`][triwarp.validation.is_volume]
    [`trimesh.Trimesh.is_winding_consistent`][]
    """
    n_faces = int(faces.shape[0]) // 3
    if n_faces == 0:
        return True

    mask = edge_winding_consistent_mask(faces)
    if int(mask.shape[0]) == 0:
        return True
    return bool(tw.reduce.all(mask))

edge_winding_consistent_mask

edge_winding_consistent_mask(
    faces: array[int32],
    edges: Array2dInt32 | None = None,
    edges_sorted: Array2dInt32 | None = None,
) -> wp.array[wp.bool]

Per shared-edge flag: whether an undirected edge's two faces traverse it in opposite directions.

Built over the undirected edges shared by exactly two faces (edge groups of length two). An entry is True when the two directed half-edges are reversed (locally consistent normals); boundary and non-manifold edges have no entry. is_winding_consistent is True iff every entry is True.

Parameters:

Name Type Description Default
faces array[int32]

Length-3 * n_faces wp.int32 flat triangle index buffer.

required
edges Array2dInt32 | None

Optional precomputed (n_faces * 3, 2) directed edges in faces_to_edges row order. Built from faces when None.

None
edges_sorted Array2dInt32 | None

Optional precomputed sorted (min-first) edges in the same row order. Built from faces when None.

None

Returns:

Type Description
array[bool]

Length n_shared_edges on faces.device. Empty when the mesh has no shared edges.

Raises:

Type Description
RuntimeError

If faces, edges and edges_sorted are not all on one device.

See Also

is_winding_consistent face_flip_mask

Source code in triwarp/validation.py
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
def edge_winding_consistent_mask(
    faces: wp.array[wp.int32],
    edges: twt.Array2dInt32 | None = None,
    edges_sorted: twt.Array2dInt32 | None = None,
) -> wp.array[wp.bool]:
    """
    Per shared-edge flag: whether an undirected edge's two faces traverse it in opposite directions.

    Built over the undirected edges shared by exactly two faces (edge groups of length two). An
    entry is ``True`` when the two directed half-edges are reversed (locally consistent normals);
    boundary and non-manifold edges have no entry.
    [`is_winding_consistent`][triwarp.validation.is_winding_consistent] is ``True`` iff every
    entry is ``True``.

    Parameters
    ----------
    faces
        Length-``3 * n_faces`` ``wp.int32`` flat triangle index buffer.
    edges
        Optional precomputed ``(n_faces * 3, 2)`` directed edges in
        [`faces_to_edges`][triwarp.edges.faces_to_edges] row order. Built from ``faces`` when
        ``None``.
    edges_sorted
        Optional precomputed sorted (min-first) edges in the same row order. Built from ``faces``
        when ``None``.

    Returns
    -------
    wp.array[wp.bool]
        Length ``n_shared_edges`` on ``faces.device``. Empty when the mesh has no shared edges.

    Raises
    ------
    RuntimeError
        If ``faces``, ``edges`` and ``edges_sorted`` are not all on one device.

    See Also
    --------
    [`is_winding_consistent`][triwarp.validation.is_winding_consistent]
    [`face_flip_mask`][triwarp.validation.face_flip_mask]
    """
    require_same_device(faces=faces, edges=edges, edges_sorted=edges_sorted)
    n_faces = int(faces.shape[0]) // 3
    device = faces.device
    if n_faces == 0:
        return wp.empty(0, dtype=wp.bool, device=device)

    if edges is None:
        edges = tw.edges.faces_to_edges(faces)
    if edges_sorted is None:
        edges_sorted = tw.edges.faces_to_edges(faces, sorted=True)

    edge_groups = tw.grouping.group_int_rows(edges_sorted, length=2)
    n_groups = int(edge_groups.shape[0])
    if n_groups == 0:
        return wp.empty(0, dtype=wp.bool, device=device)

    consistent = wp.empty(n_groups, dtype=wp.bool, device=device)
    wp.launch(
        kernel_validation.edge_pair_winding_mask,
        dim=n_groups,
        inputs=[edges, edge_groups, consistent],
        device=device,
    )
    return consistent

face_orientation_bits

face_orientation_bits(
    faces: array[int32],
) -> tuple[
    wp.array[wp.int32],
    twt.Array2dInt32,
    wp.array[wp.int32],
    int,
]

Per-face Z2 orientation bits (relative to each component seed) plus signed face-adjacency edges.

orient[f] is 0 for a face that agrees with its connected component's seed and 1 for a face that must be flipped to agree — the flip mask consumed by face_flip_mask and make_winding_consistent. m is the number of face-adjacency rows; when m == 0 the returned signed_edges / signs are empty. Assumes n_faces > 0 (callers guard).

This is a lower-level primitive (the underlying orientation engine behind is_orientable and face_flip_mask) exposed for callers that need the raw flip bits together with the propagation edges, such as triwarp.repair.make_winding_consistent.

The bits are solved, not propagated: orient is the Z2 potential of the signed face-adjacency graph, computed by a parity-carrying union-find (connected_component_parity_from_edges) in a fixed three launches. Cost therefore depends on the mesh's size, not on the diameter of its face-adjacency graph.

Parameters:

Name Type Description Default
faces array[int32]

Length-3 * n_faces wp.int32 flat triangle index buffer. n_faces must be positive.

required

Returns:

Name Type Description
orient array[int32]

Length n_faces flip bits (0 or 1) on faces.device.

signed_edges Array2dInt32

(m, 2) face-adjacency pairs, one row per face-adjacency edge.

signs array[int32]

Length m Z2 sign per adjacency edge.

m int

Number of face-adjacency rows.

See Also

is_orientable face_flip_mask

Source code in triwarp/validation.py
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
def face_orientation_bits(
    faces: wp.array[wp.int32],
) -> tuple[wp.array[wp.int32], twt.Array2dInt32, wp.array[wp.int32], int]:
    """
    Per-face Z2 orientation bits (relative to each component seed) plus signed face-adjacency edges.

    ``orient[f]`` is ``0`` for a face that agrees with its connected component's seed and ``1`` for
    a face that must be flipped to agree — the flip mask consumed by
    [`face_flip_mask`][triwarp.validation.face_flip_mask] and
    [`make_winding_consistent`][triwarp.repair.make_winding_consistent]. ``m`` is the number of
    face-adjacency rows; when ``m == 0`` the returned ``signed_edges`` / ``signs`` are empty.
    Assumes ``n_faces > 0`` (callers guard).

    This is a lower-level primitive (the underlying orientation engine behind
    [`is_orientable`][triwarp.validation.is_orientable] and
    [`face_flip_mask`][triwarp.validation.face_flip_mask]) exposed for callers that
    need the raw flip bits together with the propagation edges, such as
    [`triwarp.repair.make_winding_consistent`][triwarp.repair.make_winding_consistent].

    The bits are solved, not propagated: ``orient`` is the Z2 potential of the signed
    face-adjacency graph, computed by a parity-carrying union-find
    ([`connected_component_parity_from_edges`]
    [triwarp.graph.connected_component_parity_from_edges]) in a fixed three launches. Cost
    therefore depends on the mesh's *size*, not on the diameter of its face-adjacency graph.

    Parameters
    ----------
    faces
        Length-``3 * n_faces`` ``wp.int32`` flat triangle index buffer. ``n_faces`` must be
        positive.

    Returns
    -------
    orient : wp.array[wp.int32]
        Length ``n_faces`` flip bits (``0`` or ``1``) on ``faces.device``.
    signed_edges : twt.Array2dInt32
        ``(m, 2)`` face-adjacency pairs, one row per face-adjacency edge.
    signs : wp.array[wp.int32]
        Length ``m`` Z2 sign per adjacency edge.
    m : int
        Number of face-adjacency rows.

    See Also
    --------
    [`is_orientable`][triwarp.validation.is_orientable]
    [`face_flip_mask`][triwarp.validation.face_flip_mask]
    """
    device = faces.device
    n_faces = int(faces.shape[0]) // 3
    adjacency, adjacency_edges = tw.adjacency.face_adjacency(faces, return_edges=True)
    m = int(adjacency.shape[0])

    if m == 0:
        signed_edges = twt.empty_2d((0, 2), wp.int32, device=device)
        signs = wp.empty(0, dtype=wp.int32, device=device)
        return wp.zeros(n_faces, dtype=wp.int32, device=device), signed_edges, signs, m

    signed_edges = twt.empty_2d((m, 2), wp.int32, device=device)
    signs = wp.empty(m, dtype=wp.int32, device=device)
    wp.launch(
        kernel_validation.build_signed_face_edges,
        dim=m,
        inputs=[faces, adjacency, adjacency_edges, signed_edges, signs],
        device=device,
    )

    # The flip bits *are* a Z2 potential over the signed face-adjacency graph, so a parity-carrying
    # union-find produces them directly in three launches with no host synchronization
    # ([`connected_component_parity_from_edges`]
    # [triwarp.graph.connected_component_parity_from_edges]). The bits are unchanged from the
    # edge-by-edge flood fill this replaces: that seeded ``0`` at each component's smallest face id
    # and XOR-ed signs outward, and the union-find's root is that same smallest id at that same
    # parity 0. What changes is the cost — the fill ran one launch per graph level, so a mesh whose
    # face adjacency is a long path (a ribbon) paid tens of thousands of launches and thousands of
    # readbacks for work bounded by a few milliseconds of bandwidth.
    # ``validate=False``: ``signed_edges`` holds face ids this function just built from
    # ``adjacency`` (itself bounded by ``n_faces`` by construction), so the range check would only
    # re-derive a bound already guaranteed -- at the cost of a device synchronization.
    _labels, orient = tw.graph.connected_component_parity_from_edges(
        signed_edges, signs, n_faces, validate=False
    )
    return orient, signed_edges, signs, m

is_orientable

is_orientable(faces: array[int32]) -> bool

Whether the faces admit a consistent orientation (allowing per-face flips).

A mesh is orientable when each face can be assigned a flip bit so that every pair of edge-adjacent faces traverses their shared edge in opposite directions. This is a topological property independent of the current winding, matching Open3D's IsOrientable.

Each face-adjacency edge carries a Z2 flip bit; the check seeds one bit per connected component, propagates bits across adjacencies, then verifies that no adjacency violates its constraint (a contradiction means non-orientable).

Parameters:

Name Type Description Default
faces array[int32]

Length-3 * n_faces wp.int32 flat triangle index buffer.

required

Returns:

Type Description
bool

True when a consistent orientation exists. Vacuously True for an empty mesh.

See Also

face_flip_mask is_watertight is_winding_consistent

Notes

Equivalent to open3d.geometry.TriangleMesh.is_orientable. Unlike is_winding_consistent, orientability allows individual faces to be flipped, so a consistently-orientable mesh with inconsistent winding still returns True.

Source code in triwarp/validation.py
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
def is_orientable(faces: wp.array[wp.int32]) -> bool:
    """
    Whether the faces admit a consistent orientation (allowing per-face flips).

    A mesh is orientable when each face can be assigned a flip bit so that every pair of
    edge-adjacent faces traverses their shared edge in opposite directions. This is a topological
    property independent of the current winding, matching Open3D's ``IsOrientable``.

    Each face-adjacency edge carries a Z2 flip bit; the check seeds one bit per connected component,
    propagates bits across adjacencies, then verifies that no adjacency violates its constraint (a
    contradiction means non-orientable).

    Parameters
    ----------
    faces
        Length-``3 * n_faces`` ``wp.int32`` flat triangle index buffer.

    Returns
    -------
    bool
        ``True`` when a consistent orientation exists. Vacuously ``True`` for an empty mesh.

    See Also
    --------
    [`face_flip_mask`][triwarp.validation.face_flip_mask]
    [`is_watertight`][triwarp.validation.is_watertight]
    [`is_winding_consistent`][triwarp.validation.is_winding_consistent]

    Notes
    -----
    Equivalent to ``open3d.geometry.TriangleMesh.is_orientable``. Unlike
    [`is_winding_consistent`][triwarp.validation.is_winding_consistent], orientability allows
    individual faces to be flipped, so a consistently-orientable mesh with inconsistent winding
    still returns ``True``.
    """
    n_faces = int(faces.shape[0]) // 3
    if n_faces == 0:
        return True

    orient, signed_edges, signs, m = face_orientation_bits(faces)
    if m == 0:
        return True

    device = faces.device
    conflict = wp.zeros(1, dtype=wp.int32, device=device)
    wp.launch(
        kernel_validation.verify_orientation,
        dim=m,
        inputs=[signed_edges, signs, orient, conflict],
        device=device,
    )
    return int(read_scalar(conflict, 0)) == 0

face_flip_mask

face_flip_mask(faces: array[int32]) -> wp.array[wp.bool]

Per-face flag: whether a face must be flipped to make winding consistent within its patch.

Runs the Z2 orientation propagation over the face-adjacency graph (seeding one arbitrary reference face per connected component) and returns True for every face whose winding disagrees with its component's seed. Applying these flips yields a consistently wound mesh, so this is the per-face flip mask consumed by make_winding_consistent. A mesh that is already consistently wound yields an all-False mask.

Parameters:

Name Type Description Default
faces array[int32]

Length-3 * n_faces wp.int32 flat triangle index buffer.

required

Returns:

Type Description
array[bool]

Length n_faces on faces.device. Empty for an empty mesh.

See Also

is_orientable is_winding_consistent make_winding_consistent face_flipped_mask

Notes

The reference orientation is arbitrary per connected component, so on a non-orientable patch the mask is still a best-effort flood-fill (matching trimesh.repair.fix_winding).

This is a different flip from face_flipped_mask, which is about the UV domain: this mask flags a triangle whose index order must be reversed to agree with its patch, a property of the 3D connectivity that ignores geometry entirely, while that one flags a triangle whose UV image has negative signed area. Neither implies the other.

Source code in triwarp/validation.py
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
def face_flip_mask(faces: wp.array[wp.int32]) -> wp.array[wp.bool]:
    """
    Per-face flag: whether a face must be flipped to make winding consistent within its patch.

    Runs the Z2 orientation propagation over the face-adjacency graph (seeding one arbitrary
    reference face per connected component) and returns ``True`` for every face whose winding
    disagrees with its component's seed. Applying these flips yields a consistently wound mesh, so
    this is the per-face flip mask consumed by
    [`make_winding_consistent`][triwarp.repair.make_winding_consistent]. A mesh that is already
    consistently wound yields an all-``False`` mask.

    Parameters
    ----------
    faces
        Length-``3 * n_faces`` ``wp.int32`` flat triangle index buffer.

    Returns
    -------
    wp.array[wp.bool]
        Length ``n_faces`` on ``faces.device``. Empty for an empty mesh.

    See Also
    --------
    [`is_orientable`][triwarp.validation.is_orientable]
    [`is_winding_consistent`][triwarp.validation.is_winding_consistent]
    [`make_winding_consistent`][triwarp.repair.make_winding_consistent]
    [`face_flipped_mask`][triwarp.parametrization.face_flipped_mask]

    Notes
    -----
    The reference orientation is arbitrary per connected component, so on a non-orientable patch the
    mask is still a best-effort flood-fill (matching ``trimesh.repair.fix_winding``).

    This is a different flip from
    [`face_flipped_mask`][triwarp.parametrization.face_flipped_mask], which is about the *UV
    domain*: this mask flags a triangle whose index order must be reversed to agree with its patch,
    a property of the 3D connectivity that ignores geometry entirely, while that one flags a
    triangle whose UV image has negative signed area. Neither implies the other.
    """
    n_faces = int(faces.shape[0]) // 3
    device = faces.device
    if n_faces == 0:
        return wp.empty(0, dtype=wp.bool, device=device)

    orient, _, _, _ = face_orientation_bits(faces)
    mask = wp.empty(n_faces, dtype=wp.bool, device=device)
    wp.map(kernel_array.greater, orient, wp.int32(0), out=mask)
    return mask

is_watertight

is_watertight(
    vertices: array[vec3],
    faces: array[int32],
    *,
    edges_sorted: Array2dInt32 | None = None,
    mesh: Mesh | None = None,
) -> bool

Whether the mesh bounds a closed volume with no self-intersections.

Follows Open3D's IsWatertight: the mesh must be edge-manifold with no boundary edges, vertex-manifold, and free of self-intersections.

Parameters:

Name Type Description Default
vertices array[vec3]

(n_vertices,) vertex positions.

required
faces array[int32]

Length-3 * n_faces wp.int32 flat triangle index buffer.

required
edges_sorted Array2dInt32 | None

Optional precomputed (n_faces * 3, 2) sorted edges in faces_to_edges row order. When None, built once and shared by the edge- and vertex-manifold checks.

None
mesh Mesh | None

A wp.Mesh already built over vertices and faces, forwarded to the self-intersection broad phase so its BVH is not rebuilt. Purely an optimization, and not checked against vertices / faces.

None

Returns:

Type Description
bool

True when the mesh is edge-manifold (no boundary), vertex-manifold, and not self-intersecting.

Raises:

Type Description
RuntimeError

If vertices, faces, edges_sorted and mesh are not all on one device.

See Also

face_watertight_mask is_edge_manifold is_vertex_manifold is_self_intersecting is_volume

Notes

Equivalent to open3d.geometry.TriangleMesh.is_watertight. For the cheaper "every edge shared by exactly two faces" test (trimesh semantics) use is_edge_manifold with allow_boundary_edges=False.

This is not the predicate form of face_watertight_mask, despite the names: that mask is trimesh's per-face edge test, while this is Open3D's three-part composition, so all(face_watertight_mask(faces)) is the first of the three conditions and not this function. Every other is_* / *_mask pair in this module does relate that way.

Source code in triwarp/validation.py
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
def is_watertight(
    vertices: wp.array[wp.vec3],
    faces: wp.array[wp.int32],
    *,
    edges_sorted: twt.Array2dInt32 | None = None,
    mesh: wp.Mesh | None = None,
) -> bool:
    """
    Whether the mesh bounds a closed volume with no self-intersections.

    Follows Open3D's ``IsWatertight``: the mesh must be edge-manifold with no boundary edges,
    vertex-manifold, and free of self-intersections.

    Parameters
    ----------
    vertices
        ``(n_vertices,)`` vertex positions.
    faces
        Length-``3 * n_faces`` ``wp.int32`` flat triangle index buffer.
    edges_sorted
        Optional precomputed ``(n_faces * 3, 2)`` sorted edges in
        [`faces_to_edges`][triwarp.edges.faces_to_edges] row order. When ``None``, built once
        and shared by the edge- and vertex-manifold checks.
    mesh
        A ``wp.Mesh`` already built over ``vertices`` and ``faces``, forwarded to the
        self-intersection broad phase so its BVH is not rebuilt. Purely an optimization, and not
        checked against ``vertices`` / ``faces``.

    Returns
    -------
    bool
        ``True`` when the mesh is edge-manifold (no boundary), vertex-manifold, and not
        self-intersecting.

    Raises
    ------
    RuntimeError
        If ``vertices``, ``faces``, ``edges_sorted`` and ``mesh`` are not all on one device.

    See Also
    --------
    [`face_watertight_mask`][triwarp.validation.face_watertight_mask]
    [`is_edge_manifold`][triwarp.validation.is_edge_manifold]
    [`is_vertex_manifold`][triwarp.validation.is_vertex_manifold]
    [`is_self_intersecting`][triwarp.validation.is_self_intersecting]
    [`is_volume`][triwarp.validation.is_volume]

    Notes
    -----
    Equivalent to ``open3d.geometry.TriangleMesh.is_watertight``. For the cheaper "every edge shared
    by exactly two faces" test (trimesh semantics) use
    [`is_edge_manifold`][triwarp.validation.is_edge_manifold] with
    ``allow_boundary_edges=False``.

    **This is not the predicate form of
    [`face_watertight_mask`][triwarp.validation.face_watertight_mask]**, despite the names: that
    mask is trimesh's per-face edge test, while this is Open3D's three-part composition, so
    ``all(face_watertight_mask(faces))`` is the *first* of the three conditions and not this
    function. Every other ``is_*`` / ``*_mask`` pair in this module does relate that way.
    """
    require_same_device(vertices=vertices, faces=faces, edges_sorted=edges_sorted, mesh=mesh)
    n_faces = int(faces.shape[0]) // 3
    if n_faces == 0:
        return True

    # The three checks share their most expensive intermediates: sorted edges feed both
    # manifold tests, and the face adjacency (built lazily only after the cheap edge test
    # passes, preserving the short-circuit) feeds the vertex-manifold test.
    if edges_sorted is None:
        edges_sorted = tw.edges.faces_to_edges(faces, sorted=True)
    n_vertices = int(vertices.shape[0])
    if not is_edge_manifold(
        faces, allow_boundary_edges=False, edges_sorted=edges_sorted, n_vertices=n_vertices
    ):
        return False
    adjacency, adjacency_edges = tw.adjacency.face_adjacency(
        faces, edges_sorted=edges_sorted, return_edges=True, n_vertices=n_vertices
    )
    if not is_vertex_manifold(
        faces, face_adjacency=adjacency, face_adjacency_edges=adjacency_edges
    ):
        return False
    if mesh is None:
        require_nonempty_mesh(faces, "is_watertight")
        mesh = wp.Mesh(points=vertices, indices=faces)
    return not is_self_intersecting(mesh)

face_watertight_mask

face_watertight_mask(
    faces: array[int32],
    edges_sorted: Array2dInt32 | None = None,
) -> wp.array[wp.bool]

Per-face flag: whether all three of a face's undirected edges are shared by exactly two faces.

This is edge_manifold_mask with allow_boundary_edges=False: a face is True only when none of its edges is a boundary edge (used once) or a non-manifold edge (used three or more times). The faces that break watertightness (trimesh's broken_faces) are flatnonzero(~face_watertight_mask(faces)).

Parameters:

Name Type Description Default
faces array[int32]

Length-3 * n_faces wp.int32 flat triangle index buffer.

required
edges_sorted Array2dInt32 | None

Optional precomputed (n_faces * 3, 2) sorted edges forwarded to edge_manifold_mask.

None

Returns:

Type Description
array[bool]

Length n_faces on faces.device. Empty for an empty mesh.

Raises:

Type Description
RuntimeError

If faces and edges_sorted are not all on one device.

See Also

is_watertight edge_manifold_mask

Source code in triwarp/validation.py
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
def face_watertight_mask(
    faces: wp.array[wp.int32], edges_sorted: twt.Array2dInt32 | None = None
) -> wp.array[wp.bool]:
    """
    Per-face flag: whether all three of a face's undirected edges are shared by exactly two faces.

    This is [`edge_manifold_mask`][triwarp.validation.edge_manifold_mask] with
    ``allow_boundary_edges=False``: a face is ``True`` only when none of its edges is a boundary
    edge (used once) or a non-manifold edge (used three or more times). The faces that break
    watertightness (trimesh's ``broken_faces``) are ``flatnonzero(~face_watertight_mask(faces))``.

    Parameters
    ----------
    faces
        Length-``3 * n_faces`` ``wp.int32`` flat triangle index buffer.
    edges_sorted
        Optional precomputed ``(n_faces * 3, 2)`` sorted edges forwarded to
        [`edge_manifold_mask`][triwarp.validation.edge_manifold_mask].

    Returns
    -------
    wp.array[wp.bool]
        Length ``n_faces`` on ``faces.device``. Empty for an empty mesh.

    Raises
    ------
    RuntimeError
        If ``faces`` and ``edges_sorted`` are not all on one device.

    See Also
    --------
    [`is_watertight`][triwarp.validation.is_watertight]
    [`edge_manifold_mask`][triwarp.validation.edge_manifold_mask]
    """
    require_same_device(faces=faces, edges_sorted=edges_sorted)
    return edge_manifold_mask(faces, edges_sorted=edges_sorted, allow_boundary_edges=False)

is_volume

is_volume(
    vertices: array[vec3],
    faces: array[int32],
    *,
    edges: Array2dInt32 | None = None,
    edges_sorted: Array2dInt32 | None = None,
) -> bool

Whether the mesh is a valid closed volume with outward-facing normals.

Follows trimesh.Trimesh.is_volume: the mesh must be watertight (every undirected edge shared by exactly two faces), winding-consistent, and enclose a positive signed volume (normals point outward). A mesh with inward-facing normals encloses a negative signed volume and is reported False.

Parameters:

Name Type Description Default
vertices array[vec3]

(n_vertices,) vertex positions.

required
faces array[int32]

Length-3 * n_faces wp.int32 flat triangle index buffer.

required
edges Array2dInt32 | None

Optional precomputed (n_faces * 3, 2) directed edges in faces_to_edges row order. When None, built from faces.

None
edges_sorted Array2dInt32 | None

Optional precomputed (n_faces * 3, 2) sorted edges (same row order). When None, built from faces.

None

Returns:

Type Description
bool

True when the mesh is watertight, winding-consistent, and has outward normals (positive signed volume). False for an empty mesh.

Raises:

Type Description
RuntimeError

If vertices, faces, edges and edges_sorted are not all on one device.

See Also

make_volume Produce one: flip whatever fails this test. volume Measure one, once this returns True. is_winding_consistent is_watertight is_orientable trimesh.Trimesh.is_volume

Notes

Watertightness here is trimesh's "every undirected edge shared by exactly two faces" (each sorted edge forms a group of two), and winding consistency requires the two directed copies of every shared edge to be reversed. The signed volume is the sum of per-face signed tetrahedron volumes dot(v0, cross(v1, v2)) / 6 measured from the origin; for a closed surface this is independent of the reference point and its sign encodes the normal orientation.

Source code in triwarp/validation.py
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
def is_volume(
    vertices: wp.array[wp.vec3],
    faces: wp.array[wp.int32],
    *,
    edges: twt.Array2dInt32 | None = None,
    edges_sorted: twt.Array2dInt32 | None = None,
) -> bool:
    """
    Whether the mesh is a valid closed volume with outward-facing normals.

    Follows [`trimesh.Trimesh.is_volume`][]: the mesh must be watertight (every undirected edge
    shared by exactly two faces), winding-consistent, and enclose a positive signed volume (normals
    point outward). A mesh with inward-facing normals encloses a negative signed volume and is
    reported ``False``.

    Parameters
    ----------
    vertices
        ``(n_vertices,)`` vertex positions.
    faces
        Length-``3 * n_faces`` ``wp.int32`` flat triangle index buffer.
    edges
        Optional precomputed ``(n_faces * 3, 2)`` directed edges in
        [`faces_to_edges`][triwarp.edges.faces_to_edges] row order. When ``None``, built from
        ``faces``.
    edges_sorted
        Optional precomputed ``(n_faces * 3, 2)`` sorted edges (same row order). When ``None``,
        built from ``faces``.

    Returns
    -------
    bool
        ``True`` when the mesh is watertight, winding-consistent, and has outward normals (positive
        signed volume). ``False`` for an empty mesh.

    Raises
    ------
    RuntimeError
        If ``vertices``, ``faces``, ``edges`` and ``edges_sorted`` are not all on one device.

    See Also
    --------
    [`make_volume`][triwarp.repair.make_volume]
        Produce one: flip whatever fails this test.
    [`volume`][triwarp.measures.volume]
        Measure one, once this returns ``True``.
    [`is_winding_consistent`][triwarp.validation.is_winding_consistent]
    [`is_watertight`][triwarp.validation.is_watertight]
    [`is_orientable`][triwarp.validation.is_orientable]
    [`trimesh.Trimesh.is_volume`][]

    Notes
    -----
    Watertightness here is trimesh's "every undirected edge shared by exactly two faces" (each
    sorted edge forms a group of two), and winding consistency requires the two directed copies of
    every shared edge to be reversed. The signed volume is the sum of per-face signed tetrahedron
    volumes ``dot(v0, cross(v1, v2)) / 6`` measured from the origin; for a closed surface this is
    independent of the reference point and its sign encodes the normal orientation.
    """
    require_same_device(vertices=vertices, faces=faces, edges=edges, edges_sorted=edges_sorted)
    n_faces = int(faces.shape[0]) // 3
    if n_faces == 0:
        return False

    if edges is None:
        edges = tw.edges.faces_to_edges(faces)

    # The winding mask is built over edge groups of exactly two faces, so its *length* is the
    # watertightness test: every directed edge must belong to one such group.
    consistent = edge_winding_consistent_mask(faces, edges, edges_sorted)
    n_groups = int(consistent.shape[0])
    if n_groups * 2 != int(edges.shape[0]):
        return False  # not watertight: some undirected edge is not shared by exactly two faces
    # Unreachable for n_faces > 0 -- the length test above already rejects a mesh with no shared
    # edges -- but ``reduce.all`` raises on an empty array, so the guard stays rather than relying
    # on that.
    if n_groups == 0:
        return False
    if not bool(tw.reduce.all(consistent)):
        return False

    return tw.reduce.sum(tw.triangles.face_signed_volumes(vertices, faces)) > 0.0

face_defective_mask

face_defective_mask(
    vertices: array[vec3],
    faces: array[int32],
    *,
    min_quality: float | None = 0.02,
    max_normal_angle: float | None = None,
    max_fold_angle: float | None = None,
    face_normals: array[vec3] | None = None,
) -> wp.array[wp.bool]

Flag faces that are thin, misoriented relative to their neighbourhood, or folded over it.

The three criteria are independent and a face is flagged if any enabled one fires; each is disabled by passing None. This is the detector behind repair.remove_folded_faces -- unlike the rest of this module it needs geometry rather than topology, which is why it takes vertices and has no is_* counterpart: "defective" is a threshold question, not a property of the mesh.

Parameters:

Name Type Description Default
vertices array[vec3]

(n_vertices,) mesh vertex positions.

required
faces array[int32]

Length-3 * n_faces wp.int32 flat triangle index buffer.

required
min_quality float | None

Flag a face whose radius_ratio (face_quality) is below this. 0 is fully degenerate and 1 is equilateral, so this is a thinness gate; MeshLab's aratio, whose default of 0.02 is this one. None disables it.

0.02
max_normal_angle float | None

Flag a face whose normal is more than this many degrees from the direction of the sum of its edge-neighbours' normals — the local consensus. This catches a single face inserted the wrong way round in an otherwise consistent patch. MeshLab's nfratio (default 60, off by default). None disables it.

None
max_fold_angle float | None

Flag a face that meets some neighbour at more than this many degrees — a fold, where the two triangles lie almost on top of each other with opposing normals. Of the two faces at such an edge only the one facing against its own wider neighbourhood is flagged, since only one of them is the mistake; a face whose neighbours give it no consensus (an isolated face, or a strip of exactly three) is therefore never flagged on this criterion alone. MeshLab's folded_faces_angle_threshold (default 160, off by default). Must be in (0, 180]. None disables it.

None
face_normals array[vec3] | None

Optional length-n_faces unit face normals from face_normals_and_areas; recomputed when None. Only the two angle criteria read them, so this is the one geometry pass a caller already holding normals can skip.

None

Returns:

Type Description
array[bool]

Length-n_faces mask on faces.device; True marks a defective face. All-False when every criterion is disabled.

Raises:

Type Description
ValueError

If max_normal_angle or max_fold_angle is outside (0, 180].

RuntimeError

If vertices, faces and face_normals are not all on one device.

Notes

Matches MeshLab's compute_selection_bad_faces; the three parameters are its aratio, nfratio and folded_faces_angle_threshold.

See Also

repair.remove_folded_faces Deletes the folded ones. Trimesh.face_normals Caches what face_normals wants. repair.remove_degenerate_faces Deletes the fully degenerate ones, on an exact test rather than a threshold. triwarp.triangles.face_quality The thinness measure min_quality gates on.

Source code in triwarp/validation.py
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
def face_defective_mask(
    vertices: wp.array[wp.vec3],
    faces: wp.array[wp.int32],
    *,
    min_quality: float | None = 0.02,
    max_normal_angle: float | None = None,
    max_fold_angle: float | None = None,
    face_normals: wp.array[wp.vec3] | None = None,
) -> wp.array[wp.bool]:
    """
    Flag faces that are thin, misoriented relative to their neighbourhood, or folded over it.

    The three criteria are independent and a face is flagged if *any* enabled one fires; each is
    disabled by passing ``None``. This is the detector behind
    [`repair.remove_folded_faces`][triwarp.repair.remove_folded_faces] -- unlike the rest of this
    module it needs geometry rather than topology, which is why it takes ``vertices`` and has no
    ``is_*`` counterpart: "defective" is a threshold question, not a property of the mesh.

    Parameters
    ----------
    vertices
        ``(n_vertices,)`` mesh vertex positions.
    faces
        Length-``3 * n_faces`` ``wp.int32`` flat triangle index buffer.
    min_quality
        Flag a face whose ``radius_ratio``
        ([`face_quality`][triwarp.triangles.face_quality]) is below this. ``0`` is fully degenerate
        and ``1`` is equilateral, so this is a *thinness* gate; MeshLab's ``aratio``, whose default
        of ``0.02`` is this one. ``None`` disables it.
    max_normal_angle
        Flag a face whose normal is more than this many **degrees** from the direction of the sum of
        its edge-neighbours' normals — the local consensus. This catches a single face inserted the
        wrong way round in an otherwise consistent patch. MeshLab's ``nfratio`` (default ``60``,
        off by default). ``None`` disables it.
    max_fold_angle
        Flag a face that meets *some* neighbour at more than this many **degrees** — a fold, where
        the two triangles lie almost on top of each other with opposing normals. Of the two faces at
        such an edge only the one facing *against* its own wider neighbourhood is flagged, since
        only one of them is the mistake; a face whose neighbours give it no consensus (an isolated
        face, or a strip of exactly three) is therefore never flagged on this criterion alone.
        MeshLab's ``folded_faces_angle_threshold`` (default ``160``, off by default). Must be in
        ``(0, 180]``. ``None`` disables it.
    face_normals
        Optional length-``n_faces`` unit face normals from
        [`face_normals_and_areas`][triwarp.triangles.face_normals_and_areas]; recomputed when
        ``None``. Only the two angle criteria read them, so this is the one geometry pass a caller
        already holding normals can skip.

    Returns
    -------
    wp.array[wp.bool]
        Length-``n_faces`` mask on ``faces.device``; ``True`` marks a defective face. All-``False``
        when every criterion is disabled.

    Raises
    ------
    ValueError
        If ``max_normal_angle`` or ``max_fold_angle`` is outside ``(0, 180]``.
    RuntimeError
        If ``vertices``, ``faces`` and ``face_normals`` are not all on one device.

    Notes
    -----
    Matches MeshLab's ``compute_selection_bad_faces``; the three parameters are its ``aratio``,
    ``nfratio`` and ``folded_faces_angle_threshold``.

    See Also
    --------
    [`repair.remove_folded_faces`][triwarp.repair.remove_folded_faces]
        Deletes the folded ones.
    [`Trimesh.face_normals`][triwarp.mesh.Trimesh.face_normals]
        Caches what ``face_normals`` wants.
    [`repair.remove_degenerate_faces`][triwarp.repair.remove_degenerate_faces]
        Deletes the fully degenerate ones, on an exact test rather than a threshold.
    [`triwarp.triangles.face_quality`][triwarp.triangles.face_quality]
        The thinness measure ``min_quality`` gates on.
    """
    require_same_device(vertices=vertices, faces=faces, face_normals=face_normals)
    for name, angle in (("max_normal_angle", max_normal_angle), ("max_fold_angle", max_fold_angle)):
        if angle is not None and not 0.0 < angle <= 180.0:
            raise ValueError(f"{name} must be in (0, 180] degrees, got {angle}")

    device = faces.device
    n_faces = int(faces.shape[0]) // 3
    out_bad = wp.zeros(n_faces, dtype=wp.bool, device=device)
    if n_faces == 0:
        return out_bad

    quality = (
        tw.triangles.face_quality(vertices, faces, metric="radius_ratio")
        if min_quality is not None
        else wp.full(n_faces, 1.0, dtype=wp.float32, device=device)
    )
    if face_normals is None:
        face_normals, _areas = tw.triangles.face_normals_and_areas(vertices, faces)
    neighbor_sum = wp.zeros(n_faces, dtype=wp.vec3, device=device)
    max_angle = wp.zeros(n_faces, dtype=wp.float32, device=device)
    if max_normal_angle is not None or max_fold_angle is not None:
        adjacency = tw.adjacency.face_adjacency(faces, n_vertices=int(vertices.shape[0]))
        if int(adjacency.shape[0]) > 0:
            angles = tw.adjacency.face_adjacency_angles(
                vertices, faces, face_adjacency=adjacency, face_normals=face_normals
            )
            wp.launch(
                kernel_validation.accumulate_neighbor_normals,
                dim=int(adjacency.shape[0]),
                inputs=[face_normals, adjacency, angles, neighbor_sum, max_angle],
                device=device,
            )

    # -2 is unreachable for a cosine and -1 for the normalized quality, so a disabled criterion
    # simply never fires and the kernel needs no per-criterion flag.
    wp.launch(
        kernel_validation.face_defective_mask,
        dim=n_faces,
        inputs=[
            quality,
            face_normals,
            neighbor_sum,
            max_angle,
            wp.float32(min_quality if min_quality is not None else -1.0),
            wp.float32(
                math.cos(math.radians(max_normal_angle)) if max_normal_angle is not None else -2.0
            ),
            wp.float32(
                math.cos(math.radians(max_fold_angle)) if max_fold_angle is not None else -2.0
            ),
            out_bad,
        ],
        device=device,
    )
    return out_bad