Skip to content

ordito.adjacency

Mesh face-adjacency graph: which faces share an edge, and what each adjacent pair looks like.

face_adjacency is the table every other function here reads: one row per edge-adjacent face pair. The rest are per-adjacency-row quantities over that table, all row-aligned with it, so a caller derives the pairs once and passes them in -- require_paired_adjacency states the one rule those keywords carry, and is public because the functions taking the pair span three modules.

face_connected_component_labels is the one whole-graph answer: face-level connected components over the same adjacency.

Functions:

Name Description
face_adjacency

Face index pairs that share an undirected mesh edge.

require_paired_adjacency

Raise unless a precomputed face-adjacency pair is either wholly given or wholly omitted.

vertex_face_adjacency

Incidence CSR of the faces touching each vertex, as (vertex_faces, offsets).

vertex_face_adjacency_from_counts

vertex_face_adjacency from counts already taken.

face_adjacency_unshared

Vertex on each adjacent face that is not on their shared edge.

face_adjacency_angles

Unsigned angle in radians between each pair of adjacent faces.

face_adjacency_projections

Project each adjacent face pair's non-shared vertex onto the first face plane.

face_adjacency_convex

Whether each adjacent face pair is locally convex, one flag per adjacency row.

face_connected_component_labels

Connected-component label per face (face-adjacency graph).

sorted_face_edge_keys

Every halfedge's undirected edge key, sorted, and the permutation that sorted them.

face_adjacency

face_adjacency(
    faces: array[int32],
    *,
    return_edges: Literal[False] = False,
    n_vertices: int | None = None,
    edges_paired: bool = False,
) -> odt.Array2dInt32
face_adjacency(
    faces: array[int32],
    *,
    return_edges: Literal[True],
    n_vertices: int | None = None,
    edges_paired: bool = False,
) -> tuple[odt.Array2dInt32, odt.Array2dInt32]
face_adjacency(
    faces: array[int32],
    *,
    return_edges: bool = False,
    n_vertices: int | None = None,
    edges_paired: bool = False,
) -> (
    odt.Array2dInt32
    | tuple[odt.Array2dInt32, odt.Array2dInt32]
)

Face index pairs that share an undirected mesh edge.

Each output row lists two face indices whose triangles share an edge (vertex pair). On a closed manifold mesh every interior edge appears exactly twice in the edge list, so only edges with duplicate sorted rows are kept—boundary edges that appear once are omitted.

Parameters:

Name Type Description Default
faces array[int32]

(3 * n_faces,) buffer of triangle vertex indices, the same flat layout as ordito.triangles and faces_to_edges.

required
return_edges bool

If True, also return the shared vertex indices for each adjacency row.

False
n_vertices int | None

Optional vertex count, used as the row-hashing radix. It does not change the answer: when None a fixed radix above every int32 index packs the edges instead, in the same order, so no reduction or host readback is needed either way. When given it must be greater than every index in faces; see the warning on hash_indices_rows.

None
edges_paired bool

Promise that every undirected edge is shared by exactly two faces -- what is_edge_manifold with allow_boundary_edges=False establishes. The adjacency then has exactly 3 * n_faces / 2 rows and they are consecutive in the sorted edge-key order, so the run detection, its scan and the host read of the pair count are skipped. The answer is byte-identical to the default path on such a mesh. The promise is not checked beyond the face count's parity: on a mesh with a boundary or a non-manifold edge the rows are still valid face indices but pair faces that need not share an edge.

False

Returns:

Type Description
odt.Array2dInt32 or tuple of two such arrays

(m, 2) adjacency, m the number of face pairs sharing an edge, on faces.device. Row k gives face indices (f0, f1) with f0 <= f1 (rows sorted in-place). Faces faces[3*f0:3*f0+3] and faces[3*f1:3*f1+3] share an edge.

When return_edges is True, also returns (m, 2) adjacency_edges with the sorted vertex pair for that shared edge (one row per adjacency pair, taken from the first matching edge row).

Raises:

Type Description
ValueError

If edges_paired is given for an odd face count, where no pairing exists.

Notes

Duplicate-edge grouping uses group_int_rows with length=2, equivalent to trimesh.grouping.group_rows with require_count=2. An empty mesh yields shape (0, 2).

See Also

faces_to_edges connected_component_labels_from_edges face_connected_component_labels trimesh.graph.face_adjacency

Examples:

Two triangles sharing the edge (1, 2) give one row; on the closed cube every one of the 18 edges joins two faces, so there are 18 rows:

import warp as wp

import ordito as od

faces = wp.array([0, 1, 2, 2, 1, 3], dtype=wp.int32)
adjacency, shared_edges = od.adjacency.face_adjacency(faces, return_edges=True)
print(adjacency.numpy().tolist(), shared_edges.numpy().tolist())  # [[0, 1]] [[1, 2]]
vertices, faces = od.creation.box()
print(od.adjacency.face_adjacency(faces).shape)  # (18, 2)
Source code in ordito/adjacency.py
 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
 88
 89
 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
def face_adjacency(
    faces: wp.array[wp.int32],
    *,
    return_edges: bool = False,
    n_vertices: int | None = None,
    edges_paired: bool = False,
) -> odt.Array2dInt32 | tuple[odt.Array2dInt32, odt.Array2dInt32]:
    """
    Face index pairs that share an undirected mesh edge.

    Each output row lists two face indices whose triangles share an edge (vertex
    pair). On a closed manifold mesh every interior edge appears exactly twice in
    the edge list, so only edges with duplicate sorted rows are kept—boundary edges
    that appear once are omitted.

    Parameters
    ----------
    faces
        ``(3 * n_faces,)`` buffer of triangle vertex indices, the same flat layout as
        [`ordito.triangles`][ordito.triangles] and
        [`faces_to_edges`][ordito.edges.faces_to_edges].
    return_edges
        If ``True``, also return the shared vertex indices for each adjacency row.
    n_vertices
        Optional vertex count, used as the row-hashing radix. It does not change the answer:
        when ``None`` a fixed radix above every ``int32`` index packs the edges instead, in the
        same order, so no reduction or host readback is needed either way. When given it must be
        greater than every index in ``faces``; see the warning on
        [`hash_indices_rows`][ordito.grouping.hash_indices_rows].
    edges_paired
        Promise that every undirected edge is shared by exactly two faces -- what
        [`is_edge_manifold`][ordito.validation.is_edge_manifold] with
        ``allow_boundary_edges=False`` establishes. The adjacency then has exactly
        ``3 * n_faces / 2`` rows and they are consecutive in the sorted edge-key order, so the
        run detection, its scan and the host read of the pair count are skipped. The answer is
        byte-identical to the default path on such a mesh. The promise is **not checked** beyond
        the face count's parity: on a mesh with a boundary or a non-manifold edge the rows are
        still valid face indices but pair faces that need not share an edge.

    Returns
    -------
    odt.Array2dInt32 or tuple of two such arrays
        ``(m, 2)`` **adjacency**, ``m`` the number of face pairs sharing an edge, on
        ``faces.device``. Row ``k`` gives face indices ``(f0, f1)`` with ``f0 <= f1`` (rows sorted
        in-place). Faces ``faces[3*f0:3*f0+3]`` and ``faces[3*f1:3*f1+3]`` share an edge.

        When ``return_edges`` is ``True``, also returns ``(m, 2)`` **adjacency_edges**
        with the sorted vertex pair for that shared edge (one row
        per adjacency pair, taken from the first matching edge row).

    Raises
    ------
    ValueError
        If ``edges_paired`` is given for an odd face count, where no pairing exists.

    Notes
    -----
    Duplicate-edge grouping uses
    [`group_int_rows`][ordito.grouping.group_int_rows] with
    ``length=2``, equivalent to [`trimesh.grouping.group_rows`][] with
    ``require_count=2``. An empty mesh yields shape ``(0, 2)``.

    See Also
    --------
    [`faces_to_edges`][ordito.edges.faces_to_edges]
    [`connected_component_labels_from_edges`][ordito.graph.connected_component_labels_from_edges]
    [`face_connected_component_labels`][ordito.adjacency.face_connected_component_labels]
    [`trimesh.graph.face_adjacency`][]

    Examples
    --------
    Two triangles sharing the edge ``(1, 2)`` give one row; on the closed cube every one of the 18
    edges joins two faces, so there are 18 rows:

    ```python
    import warp as wp

    import ordito as od

    faces = wp.array([0, 1, 2, 2, 1, 3], dtype=wp.int32)
    adjacency, shared_edges = od.adjacency.face_adjacency(faces, return_edges=True)
    print(adjacency.numpy().tolist(), shared_edges.numpy().tolist())  # [[0, 1]] [[1, 2]]
    vertices, faces = od.creation.box()
    print(od.adjacency.face_adjacency(faces).shape)  # (18, 2)
    ```
    """
    device = faces.device
    n_faces = faces.size // 3
    if n_faces == 0:
        empty_array = odt.empty_2d((0, 2), wp.int32, device=device)
        if return_edges:
            return empty_array, odt.empty_2d((0, 2), wp.int32, device=device)
        return empty_array

    if edges_paired and n_faces % 2 != 0:
        raise ValueError(f"edges_paired needs an even face count, got {n_faces} faces")
    # Edge ``e`` belongs to face ``e // 3``, so the owning faces need no ``edges_face`` table, no
    # gather through it, and no row sort — one kernel does the division and orders the pair. With
    # ``return_edges`` the same kernel writes each pair's shared edge too, straight off ``faces``,
    # so no edge table is built just to be gathered from. Unpaired, that kernel also compacts the
    # pairs off the sorted keys, so no intermediate group table is written.
    if edges_paired:
        edge_groups = _paired_edge_groups(faces, n_vertices)
        n_pairs = int(edge_groups.shape[0])
        kernel, dim, sources = kernel_adjacency.edge_pairs_to_face_pairs, n_pairs, [edge_groups]
    else:
        order, inclusive, n_pairs = _sorted_pair_scan(faces, n_vertices)
        kernel, dim = kernel_adjacency.emit_sorted_face_pairs, inclusive.size
        sources = [inclusive, order]
    adjacency = odt.empty_2d((n_pairs, 2), wp.int32, device=device)
    adjacency_edges = odt.empty_2d((n_pairs, 2), wp.int32, device=device) if return_edges else None
    if n_pairs > 0:
        _launch.launch(
            kernel,
            dim=dim,
            inputs=[faces, None, *sources, adjacency, adjacency_edges],
            device=device,
        )
    if adjacency_edges is None:
        return odt.as_array2d(adjacency, wp.int32)
    return odt.as_array2d(adjacency, wp.int32), odt.as_array2d(adjacency_edges, wp.int32)

require_paired_adjacency

require_paired_adjacency(
    face_adjacency: Array2dInt32 | None,
    face_adjacency_edges: Array2dInt32 | None,
) -> None

Raise unless a precomputed face-adjacency pair is either wholly given or wholly omitted.

The contract behind every face_adjacency= / face_adjacency_edges= keyword in the package: the two tables are row-aligned halves of one answer and are useless apart, so a function that accepts them accepts both or neither. Public because the functions that take the pair live in three modules -- here, ordito.curvature and ordito.validation -- and they must all reject the same call with the same message; a caller that validates its own arguments before dispatching between them can use it for that too.

Call it before an empty-mesh guard, not after. A half-supplied pair is a caller bug whatever the mesh is, and the four wrappers that take the pair once disagreed about this, so the same wrong call raised or returned an empty answer depending on the input. The derivation goes the other way round -- below the guard -- because on an empty mesh face_adjacency would build two empty tables nothing reads.

Parameters:

Name Type Description Default
face_adjacency Array2dInt32 | None

(m, 2) candidate face pairs, or None.

required
face_adjacency_edges Array2dInt32 | None

(m, 2) candidate shared-edge endpoints, or None.

required

Raises:

Type Description
ValueError

If exactly one of the two is given.

RuntimeError

If face_adjacency and face_adjacency_edges are not all on one device.

See Also

face_adjacency Produces the pair, with return_edges=True. face_adjacency_unshared face_adjacency_projections face_adjacency_convex

Examples:

import warp as wp

import ordito as od

faces = wp.array([0, 1, 2, 2, 1, 3], dtype=wp.int32)
adjacency, shared_edges = od.adjacency.face_adjacency(faces, return_edges=True)
od.adjacency.require_paired_adjacency(adjacency, shared_edges)
od.adjacency.require_paired_adjacency(None, None)
try:
    od.adjacency.require_paired_adjacency(adjacency, None)
    rejected = False
except ValueError:
    rejected = True
print(rejected)  # True
Source code in ordito/adjacency.py
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
233
234
235
236
237
238
239
240
241
242
243
244
245
def require_paired_adjacency(
    face_adjacency: odt.Array2dInt32 | None, face_adjacency_edges: odt.Array2dInt32 | None
) -> None:
    """
    Raise unless a precomputed face-adjacency pair is either wholly given or wholly omitted.

    The contract behind every ``face_adjacency=`` / ``face_adjacency_edges=`` keyword in the
    package: the two tables are row-aligned halves of one answer and are useless apart, so a
    function that accepts them accepts both or neither. Public because the functions that take the
    pair live in three modules -- here, [`ordito.curvature`][ordito.curvature] and
    [`ordito.validation`][ordito.validation] -- and they must all reject the same call with the
    same message; a caller that validates its own arguments before dispatching between them can
    use it for that too.

    **Call it before an empty-mesh guard, not after.** A half-supplied pair is a caller bug
    whatever the mesh is, and the four wrappers that take the pair once disagreed about this, so
    the same wrong call raised or returned an empty answer depending on the input. The *derivation*
    goes the other way round -- below the guard -- because on an empty mesh
    [`face_adjacency`][ordito.adjacency.face_adjacency] would build two empty tables nothing reads.

    Parameters
    ----------
    face_adjacency
        ``(m, 2)`` candidate face pairs, or ``None``.
    face_adjacency_edges
        ``(m, 2)`` candidate shared-edge endpoints, or ``None``.

    Raises
    ------
    ValueError
        If exactly one of the two is given.
    RuntimeError
        If ``face_adjacency`` and ``face_adjacency_edges`` are not all on one device.

    See Also
    --------
    [`face_adjacency`][ordito.adjacency.face_adjacency]
        Produces the pair, with ``return_edges=True``.
    [`face_adjacency_unshared`][ordito.adjacency.face_adjacency_unshared]
    [`face_adjacency_projections`][ordito.adjacency.face_adjacency_projections]
    [`face_adjacency_convex`][ordito.adjacency.face_adjacency_convex]

    Examples
    --------
    ```python
    import warp as wp

    import ordito as od

    faces = wp.array([0, 1, 2, 2, 1, 3], dtype=wp.int32)
    adjacency, shared_edges = od.adjacency.face_adjacency(faces, return_edges=True)
    od.adjacency.require_paired_adjacency(adjacency, shared_edges)
    od.adjacency.require_paired_adjacency(None, None)
    try:
        od.adjacency.require_paired_adjacency(adjacency, None)
        rejected = False
    except ValueError:
        rejected = True
    print(rejected)  # True
    ```
    """
    require_same_device(face_adjacency=face_adjacency, face_adjacency_edges=face_adjacency_edges)
    if (face_adjacency is None) != (face_adjacency_edges is None):
        raise ValueError(
            "face_adjacency and face_adjacency_edges must both be provided or both omitted"
        )

vertex_face_adjacency

vertex_face_adjacency(
    faces: array[int32], *, n_vertices: int | None = None
) -> tuple[wp.array[wp.int32], wp.array[wp.int32]]

Incidence CSR of the faces touching each vertex, as (vertex_faces, offsets).

Row v is vertex_faces[offsets[v] : offsets[v + 1]] and lists every face that references vertex v, once per reference. offsets has length n_vertices + 1, so its last entry is the total 3 * n_faces and no caller needs a sentinel appended. Values first and offsets second is the package's packed-buffer convention -- see array.pack_1d_arrays, which states it.

Built by counting sort rather than from halfedge twins, and that is a deliberate limitation rather than an omission: each row is a set, not a rotation. Ordering a row would require the halfedge structure, which does not exist at a vertex-non-manifold vertex — and the decimator and normal-flip guards that consume this must run on exactly such meshes. Use vertex_one_rings when the rotational order is what is wanted; it requires an edge-manifold mesh in exchange.

Parameters:

Name Type Description Default
faces array[int32]

(3 * n_faces,) flat triangle index buffer.

required
n_vertices int | None

Number of vertices, i.e. the number of CSR rows. When None it is inferred from faces with array.index_bound, which costs one host readback; pass it when the caller already knows it. Rows for vertices no face references come out empty.

None

Returns:

Name Type Description
vertex_faces array[int32]

(3 * n_faces,) face indices, grouped by vertex. Under is_deterministic each row is in ascending face order, the same on every run; otherwise the order within a row is arbitrary.

offsets array[int32]

(n_vertices + 1,) row offsets on faces.device.

See Also

vertex_face_adjacency_from_counts The same CSR from per-vertex corner counts the caller already holds. face_adjacency vertex_one_rings igl.vertex_triangle_adjacency

Examples:

Vertices 1 and 2 lie on the shared edge of two triangles, so they touch both faces:

import warp as wp

import ordito as od

faces = wp.array([0, 1, 2, 2, 1, 3], dtype=wp.int32)
vertex_faces, offsets = od.adjacency.vertex_face_adjacency(faces, n_vertices=4)
print(offsets.numpy().tolist())  # [0, 1, 3, 5, 6]
print(sorted(vertex_faces.numpy()[offsets.numpy()[1] : offsets.numpy()[2]].tolist()))  # [0, 1]
Source code in ordito/adjacency.py
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
def vertex_face_adjacency(
    faces: wp.array[wp.int32], *, n_vertices: int | None = None
) -> tuple[wp.array[wp.int32], wp.array[wp.int32]]:
    """
    Incidence CSR of the faces touching each vertex, as ``(vertex_faces, offsets)``.

    Row ``v`` is ``vertex_faces[offsets[v] : offsets[v + 1]]`` and lists every face that references
    vertex ``v``, once per reference. ``offsets`` has length ``n_vertices + 1``, so its last entry
    is the total ``3 * n_faces`` and no caller needs a sentinel appended. Values first and offsets
    second is the package's packed-buffer convention -- see
    [`array.pack_1d_arrays`][ordito.array.pack_1d_arrays], which states it.

    Built by counting sort rather than from halfedge twins, and that is a deliberate limitation
    rather than an omission: **each row is a set, not a rotation**. Ordering a row would require the
    halfedge structure, which does not exist at a vertex-non-manifold vertex — and the decimator and
    normal-flip guards that consume this must run on exactly such meshes. Use
    [`vertex_one_rings`][ordito.halfedge.vertex_one_rings] when the rotational order is what is
    wanted; it requires an edge-manifold mesh in exchange.

    Parameters
    ----------
    faces
        ``(3 * n_faces,)`` flat triangle index buffer.
    n_vertices
        Number of vertices, i.e. the number of CSR rows. When ``None`` it is inferred from
        ``faces`` with [`array.index_bound`][ordito.array.index_bound], which costs one host
        readback; pass it when the caller already knows it. Rows for vertices no face references
        come out empty.

    Returns
    -------
    vertex_faces : wp.array[wp.int32]
        ``(3 * n_faces,)`` face indices, grouped by vertex. Under
        [`is_deterministic`][ordito.config.is_deterministic] each row is in ascending face order,
        the same on every run; otherwise the order within a row is arbitrary.
    offsets : wp.array[wp.int32]
        ``(n_vertices + 1,)`` row offsets on ``faces.device``.

    See Also
    --------
    [`vertex_face_adjacency_from_counts`][ordito.adjacency.vertex_face_adjacency_from_counts]
        The same CSR from per-vertex corner counts the caller already holds.
    [`face_adjacency`][ordito.adjacency.face_adjacency]
    [`vertex_one_rings`][ordito.halfedge.vertex_one_rings]
    ``igl.vertex_triangle_adjacency``

    Examples
    --------
    Vertices 1 and 2 lie on the shared edge of two triangles, so they touch both faces:

    ```python
    import warp as wp

    import ordito as od

    faces = wp.array([0, 1, 2, 2, 1, 3], dtype=wp.int32)
    vertex_faces, offsets = od.adjacency.vertex_face_adjacency(faces, n_vertices=4)
    print(offsets.numpy().tolist())  # [0, 1, 3, 5, 6]
    print(sorted(vertex_faces.numpy()[offsets.numpy()[1] : offsets.numpy()[2]].tolist()))  # [0, 1]
    ```
    """
    device = faces.device
    n_faces = faces.size // 3
    row_count = od.array.index_bound(faces) if n_vertices is None else int(n_vertices)

    if n_faces == 0 or row_count == 0:
        # ``row_count == 0`` with faces present (reachable only via an explicit ``n_vertices=0``)
        # would otherwise hand back an unwritten ``3 * n_faces`` buffer of allocator garbage.
        return (
            _launch.zeros(3 * n_faces, dtype=wp.int32, device=device),
            _launch.zeros(row_count + 1, dtype=wp.int32, device=device),
        )

    deterministic = is_deterministic()
    if deterministic and not wp.get_device(device).is_cpu:
        # The rows and their offsets come off one stable sort of the face buffer: no corner counts
        # and no scan. Both share one allocation (a CSR pair is held and dropped together), written
        # whole by the emit.
        offsets, vertex_faces = _launch.carve(wp.int32, (row_count + 1, 3 * n_faces), device)
        _fill_sorted_vertex_faces(faces, row_count, vertex_faces, offsets)
        return vertex_faces, offsets
    # A flat face buffer *is* the corner -> vertex map, so one launch over all 3 * n_faces corners
    # gives each vertex its incident-face count. The counts are taken in the offsets' own buffer
    # and scanned there in place: behind the leading zero for the sorted fill, one slot further
    # on for the scatter, whose atomics advance each row's start to its end.
    lead = 1 if deterministic else 2
    # The rows share the offsets' allocation (a CSR pair is held and dropped together); only the
    # counts are zeroed, as the rows are written whole before they are read.
    buffer, vertex_faces = _launch.carve(wp.int32, (row_count + lead, 3 * n_faces), device)
    _launch.zero_(buffer)
    counts = _launch.view(buffer, lead, row_count + lead)
    _launch.launch(
        kernel_scatter.count_occurrences, dim=3 * n_faces, inputs=[faces, counts], device=device
    )
    _launch.array_scan(counts, out_array=counts, inclusive=True)
    offsets = _launch.view(buffer, 0, row_count + 1)
    if deterministic:
        _fill_sorted_vertex_faces(faces, row_count, vertex_faces)
    else:
        _launch.launch(
            kernel_adjacency.scatter_vertex_faces,
            dim=n_faces,
            inputs=[faces],
            outputs=[buffer, vertex_faces],
            device=device,
        )
    return vertex_faces, offsets

vertex_face_adjacency_from_counts

vertex_face_adjacency_from_counts(
    faces: array[int32], counts: array[int32]
) -> tuple[wp.array[wp.int32], wp.array[wp.int32]]

vertex_face_adjacency from counts already taken.

The second half of that function, for a caller whose own kernel visits every corner in a pass it makes anyway and counts each vertex's corners there: the corner histogram is the one launch this skips. counts is trusted, not checked: it must be exactly each vertex's number of corners in faces.

Parameters:

Name Type Description Default
faces array[int32]

(3 * n_faces,) flat triangle index buffer, every index in [0, n_vertices).

required
counts array[int32]

(n_vertices,) int32 number of corners of faces naming each vertex, on faces.device. Not modified.

required

Returns:

Name Type Description
vertex_faces array[int32]

(3 * n_faces,) face indices grouped by vertex, ordered as in vertex_face_adjacency.

offsets array[int32]

(n_vertices + 1,) row offsets on faces.device.

Raises:

Type Description
RuntimeError

If faces and counts are not on one device.

See Also

vertex_face_adjacency ordito.graph.edges_to_neighbor_lists_from_counts The same split for an edge list's neighbour lists.

Examples:

import warp as wp

import ordito as od

faces = wp.array([0, 1, 2, 2, 1, 3], dtype=wp.int32)
counts = wp.array([1, 2, 2, 1], dtype=wp.int32)
vertex_faces, offsets = od.adjacency.vertex_face_adjacency_from_counts(faces, counts)
print(offsets.numpy().tolist())  # [0, 1, 3, 5, 6]
print(sorted(vertex_faces.numpy()[offsets.numpy()[1] : offsets.numpy()[2]].tolist()))  # [0, 1]
Source code in ordito/adjacency.py
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 vertex_face_adjacency_from_counts(
    faces: wp.array[wp.int32], counts: wp.array[wp.int32]
) -> tuple[wp.array[wp.int32], wp.array[wp.int32]]:
    """
    [`vertex_face_adjacency`][ordito.adjacency.vertex_face_adjacency] from counts already taken.

    The second half of that function, for a caller whose own kernel visits every corner in a pass
    it makes anyway and counts each vertex's corners there: the corner histogram is the one launch
    this skips. ``counts`` is trusted, not checked: it must be exactly each vertex's number of
    corners in ``faces``.

    Parameters
    ----------
    faces
        ``(3 * n_faces,)`` flat triangle index buffer, every index in ``[0, n_vertices)``.
    counts
        ``(n_vertices,)`` ``int32`` number of corners of ``faces`` naming each vertex, on
        ``faces.device``. Not modified.

    Returns
    -------
    vertex_faces : wp.array[wp.int32]
        ``(3 * n_faces,)`` face indices grouped by vertex, ordered as in
        [`vertex_face_adjacency`][ordito.adjacency.vertex_face_adjacency].
    offsets : wp.array[wp.int32]
        ``(n_vertices + 1,)`` row offsets on ``faces.device``.

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

    See Also
    --------
    [`vertex_face_adjacency`][ordito.adjacency.vertex_face_adjacency]
    [`ordito.graph.edges_to_neighbor_lists_from_counts`][ordito.graph.edges_to_neighbor_lists_from_counts]
        The same split for an edge list's neighbour lists.

    Examples
    --------
    ```python
    import warp as wp

    import ordito as od

    faces = wp.array([0, 1, 2, 2, 1, 3], dtype=wp.int32)
    counts = wp.array([1, 2, 2, 1], dtype=wp.int32)
    vertex_faces, offsets = od.adjacency.vertex_face_adjacency_from_counts(faces, counts)
    print(offsets.numpy().tolist())  # [0, 1, 3, 5, 6]
    print(sorted(vertex_faces.numpy()[offsets.numpy()[1] : offsets.numpy()[2]].tolist()))  # [0, 1]
    ```
    """
    require_same_device(faces=faces, counts=counts)
    device = faces.device
    n_faces = faces.size // 3
    row_count = counts.size
    if n_faces == 0 or row_count == 0:
        return (
            _launch.zeros(3 * n_faces, dtype=wp.int32, device=device),
            _launch.zeros(row_count + 1, dtype=wp.int32, device=device),
        )
    deterministic = is_deterministic()
    if deterministic and not wp.get_device(device).is_cpu:
        # The sort gives the offsets as well, so the counts go unread.
        offsets, vertex_faces = _launch.carve(wp.int32, (row_count + 1, 3 * n_faces), device)
        _fill_sorted_vertex_faces(faces, row_count, vertex_faces, offsets)
        return vertex_faces, offsets
    # Deliberately NOT od.array.counts_to_offsets: that helper always reads the total back, and
    # this function never needs it (it is 3 * n_faces, known on the host). Converting for
    # symmetry would add a device synchronization where there is currently none. The rows share
    # the offsets' allocation (a CSR pair is held and dropped together).
    offsets, vertex_faces = _launch.carve(wp.int32, (row_count + 1, 3 * n_faces), device)
    if deterministic:
        _launch.zero_(_launch.view(offsets, 0, 1))
        _launch.array_scan(
            counts, out_array=_launch.view(offsets, 1, row_count + 1), inclusive=True
        )
        _fill_sorted_vertex_faces(faces, row_count, vertex_faces)
        return vertex_faces, offsets
    # Each row's start, scanned in behind entry 0, which the scatter writes: its claims advance the
    # starts to the rows' ends (``kernels/scatter.claim_row_slot``), so nothing is zeroed and the
    # counts are only read.
    _launch.array_scan(counts, out_array=_launch.view(offsets, 1, row_count + 1), inclusive=False)
    _launch.launch(
        kernel_adjacency.scatter_vertex_faces,
        dim=n_faces,
        inputs=[faces],
        outputs=[offsets, vertex_faces],
        device=device,
    )
    return vertex_faces, offsets

face_adjacency_unshared

face_adjacency_unshared(
    faces: array[int32],
    face_adjacency: Array2dInt32 | None = None,
    face_adjacency_edges: Array2dInt32 | None = None,
    *,
    n_vertices: int | None = None,
) -> odt.Array2dInt32

Vertex on each adjacent face that is not on their shared edge.

For each row of face_adjacency, column 0 is the unshared vertex index on the first face and column 1 on the second face. When a face does not have exactly one vertex off the shared edge (degenerate case), that entry is -1.

The answer is defined by the recorded shared edge of each adjacency row, matching trimesh.graph.face_adjacency_unshared exactly. This matters only for duplicate faces: two coincident triangles meet along all three of their edges, so they produce three adjacency rows and each one reports the corner off its own edge ([[2, 2], [1, 1], [0, 0]] for two copies of (0, 1, 2)). A cheaper "vertex of one face absent from the other" rule would return -1 for all three, and would not be trimesh.

Parameters:

Name Type Description Default
faces array[int32]

(3 * n_faces,) buffer of triangle vertex indices, the same flat layout as face_adjacency.

required
face_adjacency Array2dInt32 | None

(m, 2) face index pairs from face_adjacency. When None, adjacency and shared edges are derived from faces directly and neither table is materialized.

None
face_adjacency_edges Array2dInt32 | None

(m, 2) sorted shared vertex pairs (as from face_adjacency with return_edges=True), or None. Must be supplied together with face_adjacency or omitted with it.

None
n_vertices int | None

Optional vertex count used as the row-hashing radix, forwarded to face_adjacency. Ignored when the adjacency tables are supplied. It does not change the answer; see that function's note.

None

Returns:

Type Description
Array2dInt32

(m, 2) unshared vertex indices per face pair, on faces.device. Row k gives vertex indices into faces for the corners not on face_adjacency_edges[k], or -1 when degenerate. Rows are in the same order face_adjacency returns for the same faces, so the two line up row-for-row whether or not the tables were passed in.

Raises:

Type Description
ValueError

If only one of face_adjacency and face_adjacency_edges is provided, or if their row counts differ.

RuntimeError

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

See Also

face_adjacency trimesh.graph.face_adjacency_unshared

Examples:

The two triangles share the edge (1, 2); vertex 0 is off it on the first and vertex 3 on the second:

import warp as wp

import ordito as od

faces = wp.array([0, 1, 2, 2, 1, 3], dtype=wp.int32)
print(od.adjacency.face_adjacency_unshared(faces).numpy().tolist())  # [[0, 3]]
Source code in ordito/adjacency.py
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
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
599
600
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
def face_adjacency_unshared(
    faces: wp.array[wp.int32],
    face_adjacency: odt.Array2dInt32 | None = None,
    face_adjacency_edges: odt.Array2dInt32 | None = None,
    *,
    n_vertices: int | None = None,
) -> odt.Array2dInt32:
    """
    Vertex on each adjacent face that is not on their shared edge.

    For each row of ``face_adjacency``, column 0 is the unshared vertex index on
    the first face and column 1 on the second face. When a face does not have
    exactly one vertex off the shared edge (degenerate case), that entry is ``-1``.

    The answer is defined by the **recorded shared edge** of each adjacency row, matching
    [`trimesh.graph.face_adjacency_unshared`][] exactly. This matters only for duplicate faces:
    two coincident triangles meet along all three of their edges, so they produce three adjacency
    rows and each one reports the corner off *its own* edge (``[[2, 2], [1, 1], [0, 0]]`` for two
    copies of ``(0, 1, 2)``). A cheaper "vertex of one face absent from the other" rule would
    return ``-1`` for all three, and would not be trimesh.

    Parameters
    ----------
    faces
        ``(3 * n_faces,)`` buffer of triangle vertex indices, the same flat layout as
        [`face_adjacency`][ordito.adjacency.face_adjacency].
    face_adjacency
        ``(m, 2)`` face index pairs from
        [`face_adjacency`][ordito.adjacency.face_adjacency]. When
        ``None``, adjacency and shared edges are derived from ``faces`` directly and neither table
        is materialized.
    face_adjacency_edges
        ``(m, 2)`` sorted shared vertex pairs (as from
        [`face_adjacency`][ordito.adjacency.face_adjacency] with ``return_edges=True``), or
        ``None``. Must be supplied together with ``face_adjacency`` or omitted with it.
    n_vertices
        Optional vertex count used as the row-hashing radix, forwarded to
        [`face_adjacency`][ordito.adjacency.face_adjacency]. Ignored when the adjacency tables are
        supplied. It does not change the answer; see that function's note.

    Returns
    -------
    odt.Array2dInt32
        ``(m, 2)`` unshared vertex indices per face pair, on ``faces.device``. Row ``k`` gives
        vertex indices into
        ``faces`` for the corners not on ``face_adjacency_edges[k]``, or ``-1``
        when degenerate. Rows are in the same order
        [`face_adjacency`][ordito.adjacency.face_adjacency] returns for the same ``faces``, so the
        two line up row-for-row whether or not the tables were passed in.

    Raises
    ------
    ValueError
        If only one of ``face_adjacency`` and ``face_adjacency_edges`` is provided,
        or if their row counts differ.
    RuntimeError
        If ``faces``, ``face_adjacency`` and ``face_adjacency_edges`` are not all on one device.

    See Also
    --------
    [`face_adjacency`][ordito.adjacency.face_adjacency]
    [`trimesh.graph.face_adjacency_unshared`][]

    Examples
    --------
    The two triangles share the edge ``(1, 2)``; vertex 0 is off it on the first and vertex 3 on the
    second:

    ```python
    import warp as wp

    import ordito as od

    faces = wp.array([0, 1, 2, 2, 1, 3], dtype=wp.int32)
    print(od.adjacency.face_adjacency_unshared(faces).numpy().tolist())  # [[0, 3]]
    ```
    """
    require_same_device(
        faces=faces, face_adjacency=face_adjacency, face_adjacency_edges=face_adjacency_edges
    )
    # The derive branch below deliberately does *not* call face_adjacency, recovering both
    # owning faces and the shared edge from the sorted halfedge pairs instead. Only the pairing
    # rule is shared with the other wrappers that take this pair.
    require_paired_adjacency(face_adjacency, face_adjacency_edges)
    device = faces.device
    # Both branches end in one launch over ``m`` adjacency rows writing one ``(m, 2)`` buffer, so
    # the two differ only in the kernel and the table it reads. Resolving that first, and letting a
    # single ``m == 0`` return cover an empty mesh, an empty adjacency and an empty supplied table
    # alike, is what keeps the empty case to *one* allocation rather than building an empty
    # ``edge_groups`` only to size an empty output off it.
    if face_adjacency is None:
        m, dim, tables = 0, 0, ()
        if faces.size >= 3:
            order, inclusive, m = _sorted_pair_scan(faces, n_vertices)
            dim, tables = inclusive.size, (inclusive, order)
        kernel = kernel_adjacency.emit_sorted_unshared
    else:
        assert face_adjacency_edges is not None
        if face_adjacency.shape[0] != face_adjacency_edges.shape[0]:
            raise ValueError(
                "face_adjacency and face_adjacency_edges row counts must match, "
                f"got {face_adjacency.shape[0]} and {face_adjacency_edges.shape[0]}"
            )
        m = dim = int(face_adjacency.shape[0])
        kernel, tables = (
            kernel_adjacency.face_adjacency_unshared,
            (face_adjacency, face_adjacency_edges),
        )

    unshared = odt.empty_2d((m, 2), wp.int32, device=device)
    if m > 0:
        _launch.launch(kernel, dim=dim, inputs=[faces, *tables, unshared], device=device)
    return odt.as_array2d(unshared, wp.int32)

face_adjacency_angles

face_adjacency_angles(
    vertices: array[vec3],
    faces: array[int32],
    face_adjacency: Array2dInt32 | None = None,
    face_normals: array[vec3] | None = None,
) -> wp.array[wp.float32]

Unsigned angle in radians between each pair of adjacent faces.

For each row of face_adjacency, the angle is computed from the two corresponding face normals (unit vectors). Pair it with face_adjacency_convex for the sign: that function reports which side of each shared edge the pair folds towards, which is exactly the sign this unsigned magnitude is missing.

Parameters:

Name Type Description Default
vertices array[vec3]

(n_vertices,) mesh vertex positions on the target device.

required
faces array[int32]

(3 * n_faces,) flat triangle index buffer (same layout as face_adjacency).

required
face_adjacency Array2dInt32 | None

(m, 2) face index pairs from face_adjacency. When None, adjacency is computed from faces.

None
face_normals array[vec3] | None

(n_faces,) unit face normals. When None, normals are computed from vertices and faces via face_normals_and_areas.

None

Returns:

Type Description
array[float32]

(m,) unsigned angles in radians on faces.device, one per face_adjacency row. Empty when there are no faces or no adjacency pairs.

Raises:

Type Description
RuntimeError

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

See Also

face_adjacency face_adjacency_convex The sign this magnitude omits: convex or concave, per adjacency row. vector_angle trimesh.Trimesh.face_adjacency_angles

Examples:

Of the cube's 18 adjacent face pairs, the 6 split by a face diagonal are flat and the 12 across a side meet at a right angle:

import numpy as np

import ordito as od

vertices, faces = od.creation.box()
angles = od.adjacency.face_adjacency_angles(vertices, faces)
print(angles.shape)  # (18,)
degrees = np.degrees(angles.numpy()).round(1)
print(int((degrees == 90.0).sum()), int((degrees == 0.0).sum()))  # 12 6
Source code in ordito/adjacency.py
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
666
667
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
def face_adjacency_angles(
    vertices: wp.array[wp.vec3],
    faces: wp.array[wp.int32],
    face_adjacency: odt.Array2dInt32 | None = None,
    face_normals: wp.array[wp.vec3] | None = None,
) -> wp.array[wp.float32]:
    """
    Unsigned angle in radians between each pair of adjacent faces.

    For each row of ``face_adjacency``, the angle is computed from the two
    corresponding face normals (unit vectors). Pair it with
    [`face_adjacency_convex`][ordito.adjacency.face_adjacency_convex] for the sign: that function
    reports which side of each shared edge the pair folds towards, which is exactly the sign this
    unsigned magnitude is missing.

    Parameters
    ----------
    vertices
        ``(n_vertices,)`` mesh vertex positions on the target device.
    faces
        ``(3 * n_faces,)`` flat triangle index buffer (same layout as
        [`face_adjacency`][ordito.adjacency.face_adjacency]).
    face_adjacency
        ``(m, 2)`` face index pairs from
        [`face_adjacency`][ordito.adjacency.face_adjacency]. When
        ``None``, adjacency is computed from ``faces``.
    face_normals
        ``(n_faces,)`` unit face normals. When ``None``, normals
        are computed from ``vertices`` and ``faces`` via
        [`face_normals_and_areas`][ordito.triangles.face_normals_and_areas].

    Returns
    -------
    wp.array[wp.float32]
        ``(m,)`` unsigned angles in radians on ``faces.device``, one per
        ``face_adjacency`` row. Empty when there are no faces or no adjacency pairs.

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

    See Also
    --------
    [`face_adjacency`][ordito.adjacency.face_adjacency]
    [`face_adjacency_convex`][ordito.adjacency.face_adjacency_convex]
        The sign this magnitude omits: convex or concave, per adjacency row.
    [`vector_angle`][ordito.points.vector_angle]
    [`trimesh.Trimesh.face_adjacency_angles`][]

    Examples
    --------
    Of the cube's 18 adjacent face pairs, the 6 split by a face diagonal are flat and the 12 across
    a side meet at a right angle:

    ```python
    import numpy as np

    import ordito as od

    vertices, faces = od.creation.box()
    angles = od.adjacency.face_adjacency_angles(vertices, faces)
    print(angles.shape)  # (18,)
    degrees = np.degrees(angles.numpy()).round(1)
    print(int((degrees == 90.0).sum()), int((degrees == 0.0).sum()))  # 12 6
    ```
    """
    require_same_device(
        vertices=vertices, faces=faces, face_adjacency=face_adjacency, face_normals=face_normals
    )
    device = faces.device
    n_faces = faces.size // 3
    if n_faces == 0:
        return _launch.empty(0, dtype=wp.float32, device=device)

    if face_adjacency is None:
        # The pairs come off the sorted halfedge keys, so no pair table is written; the normals
        # are read from ``face_normals`` or formed from the positions where they are read.
        if face_normals is None:
            kernel, table = kernel_adjacency.emit_sorted_angles, vertices
        else:
            kernel, table = kernel_adjacency.emit_sorted_normal_angles, face_normals
        return _sorted_pair_values(kernel, table, faces, vertices.size, wp.float32)

    m = int(face_adjacency.shape[0])
    if m == 0:
        return _launch.empty(0, dtype=wp.float32, device=device)

    out_angles = _launch.empty(m, dtype=wp.float32, device=device)
    if face_normals is None:
        # The pair's normals are formed where they are read, the values a normal table would hold.
        _launch.launch(
            kernel_adjacency.face_adjacency_angles_from_positions,
            dim=m,
            inputs=[vertices, faces, face_adjacency, out_angles],
            device=device,
        )
        return out_angles
    _launch.launch(
        kernel_adjacency.face_adjacency_angles,
        dim=m,
        inputs=[face_normals, face_adjacency, out_angles],
        device=device,
    )
    return out_angles

face_adjacency_projections

face_adjacency_projections(
    vertices: array[vec3],
    faces: array[int32],
    face_adjacency: Array2dInt32 | None = None,
    face_adjacency_edges: Array2dInt32 | None = None,
    face_adjacency_unshared: Array2dInt32 | None = None,
    face_normals: array[vec3] | None = None,
) -> wp.array[wp.float32]

Project each adjacent face pair's non-shared vertex onto the first face plane.

For each row of face_adjacency, the dot product is taken between the normal of face face_adjacency[k, 0] and the vector from one endpoint of the shared edge to the unshared vertex on face_adjacency[k, 1].

Parameters:

Name Type Description Default
vertices array[vec3]

(n_vertices,) mesh vertex positions on the target device.

required
faces array[int32]

(3 * n_faces,) flat triangle index buffer (same layout as face_adjacency).

required
face_adjacency Array2dInt32 | None

(m, 2) face index pairs from face_adjacency. When None, adjacency and shared edges are computed from faces.

None
face_adjacency_edges Array2dInt32 | None

(m, 2) sorted shared vertex pairs (as from face_adjacency with return_edges=True), or None. Must be supplied together with face_adjacency or omitted with it.

None
face_adjacency_unshared Array2dInt32 | None

(m, 2) unshared vertex indices per face pair from face_adjacency_unshared. When None, computed from faces and the adjacency data.

None
face_normals array[vec3] | None

(n_faces,) unit face normals. When None, normals are computed from vertices and faces via face_normals_and_areas.

None

Returns:

Type Description
array[float32]

(m,) projections on faces.device, one per face_adjacency row. Empty when there are no faces or no adjacency pairs. A row whose second face is degenerate (its face_adjacency_unshared entry is -1) reads as +inf, so it never registers as convex.

Raises:

Type Description
ValueError

If only one of face_adjacency and face_adjacency_edges is provided, or if a supplied face_adjacency_unshared has a different row count from face_adjacency.

RuntimeError

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

See Also

face_adjacency_convex trimesh.Trimesh.face_adjacency_projections

Examples:

Folding the second triangle below the first face's plane gives a negative projection (a convex pair), folding it above a positive one:

import warp as wp

import ordito as od

faces = wp.array([0, 1, 2, 2, 1, 3], dtype=wp.int32)
flat = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
down = wp.array([*flat, [1.0, 1.0, -0.5]], dtype=wp.vec3)
up = wp.array([*flat, [1.0, 1.0, 0.5]], dtype=wp.vec3)
print(f"{od.adjacency.face_adjacency_projections(down, faces).numpy()[0]:.4f}")  # -0.5000
print(f"{od.adjacency.face_adjacency_projections(up, faces).numpy()[0]:.4f}")  # 0.5000
Source code in ordito/adjacency.py
748
749
750
751
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
804
805
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
854
855
856
857
858
def face_adjacency_projections(
    vertices: wp.array[wp.vec3],
    faces: wp.array[wp.int32],
    face_adjacency: odt.Array2dInt32 | None = None,
    face_adjacency_edges: odt.Array2dInt32 | None = None,
    face_adjacency_unshared: odt.Array2dInt32 | None = None,
    face_normals: wp.array[wp.vec3] | None = None,
) -> wp.array[wp.float32]:
    """
    Project each adjacent face pair's non-shared vertex onto the first face plane.

    For each row of ``face_adjacency``, the dot product is taken between the
    normal of face ``face_adjacency[k, 0]`` and the vector from one endpoint of
    the shared edge to the unshared vertex on ``face_adjacency[k, 1]``.

    Parameters
    ----------
    vertices
        ``(n_vertices,)`` mesh vertex positions on the target device.
    faces
        ``(3 * n_faces,)`` flat triangle index buffer (same layout as
        [`face_adjacency`][ordito.adjacency.face_adjacency]).
    face_adjacency
        ``(m, 2)`` face index pairs from
        [`face_adjacency`][ordito.adjacency.face_adjacency]. When ``None``, adjacency and
        shared edges are computed from ``faces``.
    face_adjacency_edges
        ``(m, 2)`` sorted shared vertex pairs (as from
        [`face_adjacency`][ordito.adjacency.face_adjacency] with ``return_edges=True``), or
        ``None``. Must be supplied together with ``face_adjacency`` or omitted with it.
    face_adjacency_unshared
        ``(m, 2)`` unshared vertex indices per face pair from
        [`face_adjacency_unshared`][ordito.adjacency.face_adjacency_unshared]. When ``None``,
        computed from ``faces`` and the adjacency data.
    face_normals
        ``(n_faces,)`` unit face normals. When ``None``, normals
        are computed from ``vertices`` and ``faces`` via
        [`face_normals_and_areas`][ordito.triangles.face_normals_and_areas].

    Returns
    -------
    wp.array[wp.float32]
        ``(m,)`` projections on ``faces.device``, one per ``face_adjacency``
        row. Empty when there are no faces or no adjacency pairs. A row whose second face is
        degenerate (its
        [`face_adjacency_unshared`][ordito.adjacency.face_adjacency_unshared] entry is ``-1``)
        reads as ``+inf``, so it never registers as convex.

    Raises
    ------
    ValueError
        If only one of ``face_adjacency`` and ``face_adjacency_edges`` is provided, or if a
        supplied ``face_adjacency_unshared`` has a different row count from ``face_adjacency``.
    RuntimeError
        If ``vertices``, ``faces``, ``face_adjacency``, ``face_adjacency_edges``,
        ``face_adjacency_unshared`` and ``face_normals`` are not all on one device.

    See Also
    --------
    [`face_adjacency_convex`][ordito.adjacency.face_adjacency_convex]
    [`trimesh.Trimesh.face_adjacency_projections`][]

    Examples
    --------
    Folding the second triangle below the first face's plane gives a negative projection (a
    convex pair), folding it above a positive one:

    ```python
    import warp as wp

    import ordito as od

    faces = wp.array([0, 1, 2, 2, 1, 3], dtype=wp.int32)
    flat = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
    down = wp.array([*flat, [1.0, 1.0, -0.5]], dtype=wp.vec3)
    up = wp.array([*flat, [1.0, 1.0, 0.5]], dtype=wp.vec3)
    print(f"{od.adjacency.face_adjacency_projections(down, faces).numpy()[0]:.4f}")  # -0.5000
    print(f"{od.adjacency.face_adjacency_projections(up, faces).numpy()[0]:.4f}")  # 0.5000
    ```
    """
    require_same_device(
        vertices=vertices,
        faces=faces,
        face_adjacency=face_adjacency,
        face_adjacency_edges=face_adjacency_edges,
        face_adjacency_unshared=face_adjacency_unshared,
        face_normals=face_normals,
    )
    if _no_tables(face_adjacency, face_adjacency_edges, face_adjacency_unshared, face_normals):
        return _sorted_pair_values(
            kernel_adjacency.emit_sorted_projections,
            vertices,
            faces,
            vertices.size,
            wp.float32,
            wp.float32(0.0),
            after=(None,),
        )
    tables = _projection_tables(
        vertices, faces, face_adjacency, face_adjacency_edges, face_adjacency_unshared, face_normals
    )
    if tables is None:
        return _launch.empty(0, dtype=wp.float32, device=faces.device)
    out_projections = _launch.empty(int(tables[1].shape[0]), dtype=wp.float32, device=faces.device)
    _launch.launch(
        kernel_adjacency.face_adjacency_projections,
        dim=out_projections.size,
        inputs=[vertices, *tables, wp.float32(0.0), out_projections, None],
        device=faces.device,
    )
    return out_projections

face_adjacency_convex

face_adjacency_convex(
    vertices: array[vec3],
    faces: array[int32],
    face_adjacency: Array2dInt32 | None = None,
    face_adjacency_edges: Array2dInt32 | None = None,
    face_adjacency_unshared: Array2dInt32 | None = None,
    face_normals: array[vec3] | None = None,
) -> wp.array[wp.bool]

Whether each adjacent face pair is locally convex, one flag per adjacency row.

A pair is locally convex when the unshared vertex of the second face, projected onto the plane of the first face, has a projection less than TOLERANCE_MERGE.

Parameters:

Name Type Description Default
vertices array[vec3]

(n_vertices,) mesh vertex positions on the target device.

required
faces array[int32]

(3 * n_faces,) flat triangle index buffer (same layout as face_adjacency).

required
face_adjacency Array2dInt32 | None

(m, 2) face index pairs from face_adjacency. When None, adjacency and shared edges are computed from faces.

None
face_adjacency_edges Array2dInt32 | None

(m, 2) sorted shared vertex pairs, or None. Must be supplied together with face_adjacency or omitted with it.

None
face_adjacency_unshared Array2dInt32 | None

(m, 2) unshared vertex indices per face pair, or None.

None
face_normals array[vec3] | None

(n_faces,) unit face normals, or None.

None

Returns:

Type Description
array[bool]

(m,) mask on faces.device, one per face_adjacency row. Empty when there are no faces or no adjacency pairs.

Raises:

Type Description
ValueError

If only one of face_adjacency and face_adjacency_edges is provided, or if a supplied face_adjacency_unshared has a different row count from face_adjacency.

RuntimeError

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

See Also

face_adjacency_projections trimesh.Trimesh.face_adjacency_convex

Examples:

A pair folded below the first face's plane is convex; folded above it, concave:

import warp as wp

import ordito as od

faces = wp.array([0, 1, 2, 2, 1, 3], dtype=wp.int32)
flat = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
down = wp.array([*flat, [1.0, 1.0, -0.5]], dtype=wp.vec3)
up = wp.array([*flat, [1.0, 1.0, 0.5]], dtype=wp.vec3)
print(od.adjacency.face_adjacency_convex(down, faces).numpy().tolist())  # [True]
print(od.adjacency.face_adjacency_convex(up, faces).numpy().tolist())  # [False]
vertices, faces = od.creation.box()
print(bool(od.adjacency.face_adjacency_convex(vertices, faces).numpy().all()))  # True
Source code in ordito/adjacency.py
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
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
def face_adjacency_convex(
    vertices: wp.array[wp.vec3],
    faces: wp.array[wp.int32],
    face_adjacency: odt.Array2dInt32 | None = None,
    face_adjacency_edges: odt.Array2dInt32 | None = None,
    face_adjacency_unshared: odt.Array2dInt32 | None = None,
    face_normals: wp.array[wp.vec3] | None = None,
) -> wp.array[wp.bool]:
    """
    Whether each adjacent face pair is locally convex, one flag per adjacency row.

    A pair is locally convex when the unshared vertex of the second face,
    projected onto the plane of the first face, has a projection less than
    [`TOLERANCE_MERGE`][ordito.constants.TOLERANCE_MERGE].

    Parameters
    ----------
    vertices
        ``(n_vertices,)`` mesh vertex positions on the target device.
    faces
        ``(3 * n_faces,)`` flat triangle index buffer (same layout as
        [`face_adjacency`][ordito.adjacency.face_adjacency]).
    face_adjacency
        ``(m, 2)`` face index pairs from
        [`face_adjacency`][ordito.adjacency.face_adjacency]. When ``None``, adjacency and
        shared edges are computed from ``faces``.
    face_adjacency_edges
        ``(m, 2)`` sorted shared vertex pairs, or ``None``. Must be supplied together with
        ``face_adjacency`` or omitted with it.
    face_adjacency_unshared
        ``(m, 2)`` unshared vertex indices per face pair, or ``None``.
    face_normals
        ``(n_faces,)`` unit face normals, or ``None``.

    Returns
    -------
    wp.array[wp.bool]
        ``(m,)`` mask on ``faces.device``, one per ``face_adjacency`` row. Empty when there are
        no faces or no adjacency pairs.

    Raises
    ------
    ValueError
        If only one of ``face_adjacency`` and ``face_adjacency_edges`` is provided, or if a
        supplied ``face_adjacency_unshared`` has a different row count from ``face_adjacency``.
    RuntimeError
        If ``vertices``, ``faces``, ``face_adjacency``, ``face_adjacency_edges``,
        ``face_adjacency_unshared`` and ``face_normals`` are not all on one device.

    See Also
    --------
    [`face_adjacency_projections`][ordito.adjacency.face_adjacency_projections]
    [`trimesh.Trimesh.face_adjacency_convex`][]

    Examples
    --------
    A pair folded below the first face's plane is convex; folded above it, concave:

    ```python
    import warp as wp

    import ordito as od

    faces = wp.array([0, 1, 2, 2, 1, 3], dtype=wp.int32)
    flat = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
    down = wp.array([*flat, [1.0, 1.0, -0.5]], dtype=wp.vec3)
    up = wp.array([*flat, [1.0, 1.0, 0.5]], dtype=wp.vec3)
    print(od.adjacency.face_adjacency_convex(down, faces).numpy().tolist())  # [True]
    print(od.adjacency.face_adjacency_convex(up, faces).numpy().tolist())  # [False]
    vertices, faces = od.creation.box()
    print(bool(od.adjacency.face_adjacency_convex(vertices, faces).numpy().all()))  # True
    ```
    """
    require_same_device(
        vertices=vertices,
        faces=faces,
        face_adjacency=face_adjacency,
        face_adjacency_edges=face_adjacency_edges,
        face_adjacency_unshared=face_adjacency_unshared,
        face_normals=face_normals,
    )
    if _no_tables(face_adjacency, face_adjacency_edges, face_adjacency_unshared, face_normals):
        return _sorted_pair_values(
            kernel_adjacency.emit_sorted_projections,
            vertices,
            faces,
            vertices.size,
            wp.bool,
            TOLERANCE_MERGE_CONSTANT,
            None,
        )
    tables = _projection_tables(
        vertices, faces, face_adjacency, face_adjacency_edges, face_adjacency_unshared, face_normals
    )
    if tables is None:
        return _launch.empty(0, dtype=wp.bool, device=faces.device)
    # The projection and its threshold in one launch rather than the projections array plus a
    # ``wp.map`` comparison over it; the shared ``adjacency_projection`` keeps the two answers
    # row-for-row consistent.
    out_convex = _launch.empty(int(tables[1].shape[0]), dtype=wp.bool, device=faces.device)
    _launch.launch(
        kernel_adjacency.face_adjacency_projections,
        dim=out_convex.size,
        inputs=[vertices, *tables, TOLERANCE_MERGE_CONSTANT, None, out_convex],
        device=faces.device,
    )
    return out_convex

face_connected_component_labels

face_connected_component_labels(
    faces: array[int32],
    *,
    n_vertices: int | None = None,
    mates: array[int32] | None = None,
) -> wp.array[wp.int32]

Connected-component label per face (face-adjacency graph).

The labels are those connected_component_labels_from_edges gives over face_adjacency with node_count = n_faces -- each component named by its smallest face id -- and run through the same union-find, fed straight from halfedge_mates instead of a compacted pair table. Trimesh.face_connected_component_labels calls the edge-list form directly when it already holds a cached adjacency.

Parameters:

Name Type Description Default
faces array[int32]

(3 * n_faces,) flat triangle index buffer (same as face_adjacency).

required
n_vertices int | None

Optional exclusive bound on the vertex indices, forwarded to halfedge_mates, which pairs faster with it. It does not change the answer, and it is trusted, not checked: it must exceed every index.

None
mates array[int32] | None

(3 * n_faces,) halfedge mate codes of faces, from halfedge_mates, to spare building them here (and n_vertices is then unused). Not checked against faces beyond its length. Trimesh.halfedge_mates is the cached one to pass.

None

Returns:

Type Description
array[int32]

(n_faces,) component labels on faces.device.

Raises:

Type Description
ValueError

If mates is given and its length is not 3 * n_faces.

RuntimeError

If faces and mates are not on one device.

See Also

connected_component_labels connected_component_labels_from_edges face_adjacency

Examples:

The first two triangles share an edge and the third stands alone; each component is named by its smallest face index:

import warp as wp

import ordito as od

faces = wp.array([0, 1, 2, 2, 1, 3, 4, 5, 6], dtype=wp.int32)
print(od.adjacency.face_connected_component_labels(faces).numpy().tolist())  # [0, 0, 2]
Source code in ordito/adjacency.py
1063
1064
1065
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
def face_connected_component_labels(
    faces: wp.array[wp.int32],
    *,
    n_vertices: int | None = None,
    mates: wp.array[wp.int32] | None = None,
) -> wp.array[wp.int32]:
    """
    Connected-component label per face (face-adjacency graph).

    The labels are those
    [`connected_component_labels_from_edges`][ordito.graph.connected_component_labels_from_edges]
    gives over [`face_adjacency`][ordito.adjacency.face_adjacency] with
    ``node_count = n_faces`` -- each component named by its smallest face id -- and run through
    the same union-find, fed straight from
    [`halfedge_mates`][ordito.halfedge.halfedge_mates] instead of a compacted pair table.
    [`Trimesh.face_connected_component_labels`][ordito.mesh.Trimesh] calls the edge-list form
    directly when it already holds a cached adjacency.

    Parameters
    ----------
    faces
        ``(3 * n_faces,)`` flat triangle index buffer (same as
        [`face_adjacency`][ordito.adjacency.face_adjacency]).
    n_vertices
        Optional exclusive bound on the vertex indices, forwarded to
        [`halfedge_mates`][ordito.halfedge.halfedge_mates], which pairs faster with it. It does
        not change the answer, and it is trusted, not checked: it must exceed every index.
    mates
        ``(3 * n_faces,)`` halfedge mate codes of ``faces``, from
        [`halfedge_mates`][ordito.halfedge.halfedge_mates], to spare building them here (and
        ``n_vertices`` is then unused). Not checked against ``faces`` beyond its length.
        [`Trimesh.halfedge_mates`][ordito.mesh.Trimesh.halfedge_mates] is the cached one to pass.

    Returns
    -------
    wp.array[wp.int32]
        ``(n_faces,)`` component labels on ``faces.device``.

    Raises
    ------
    ValueError
        If ``mates`` is given and its length is not ``3 * n_faces``.
    RuntimeError
        If ``faces`` and ``mates`` are not on one device.

    See Also
    --------
    [`connected_component_labels`][ordito.graph.connected_component_labels]
    [`connected_component_labels_from_edges`][ordito.graph.connected_component_labels_from_edges]
    [`face_adjacency`][ordito.adjacency.face_adjacency]

    Examples
    --------
    The first two triangles share an edge and the third stands alone; each component is named by its
    smallest face index:

    ```python
    import warp as wp

    import ordito as od

    faces = wp.array([0, 1, 2, 2, 1, 3, 4, 5, 6], dtype=wp.int32)
    print(od.adjacency.face_connected_component_labels(faces).numpy().tolist())  # [0, 0, 2]
    ```
    """
    require_same_device(faces=faces, mates=mates)
    n_faces = faces.size // 3
    if mates is not None and mates.size != 3 * n_faces:
        raise ValueError(
            f"mates must have one entry per halfedge, got {mates.size} for {n_faces} faces"
        )
    if n_faces == 0:
        return _launch.empty(0, dtype=wp.int32, device=faces.device)
    device = faces.device
    parents = _face_component_forest(faces, n_vertices, mates)
    labels = _launch.empty(n_faces, dtype=wp.int32, device=device)
    _launch.launch(
        kernel_connected_components.ecl_flatten,
        dim=n_faces,
        inputs=[parents, labels, None],
        device=device,
    )
    return labels

sorted_face_edge_keys

sorted_face_edge_keys(
    faces: array[int32], *, n_vertices: int | None = None
) -> tuple[wp.array[wp.uint64], wp.array[wp.int32]]

Every halfedge's undirected edge key, sorted, and the permutation that sorted them.

Halfedge 3f + k is corner k of face f, the faces_to_edges row order, and its key packs the sorted endpoint pair (lo, hi) as lo + hi * radix -- the key hash_indices_rows gives those rows. The sort is stable, so equal keys keep halfedge order: an edge's halfedges are one run, the pairs face_adjacency groups are its runs of exactly two, and order[i] // 3 is the face owning sorted position i.

Parameters:

Name Type Description Default
faces array[int32]

(3 * n_faces,) flat triangle index buffer.

required
n_vertices int | None

Optional exclusive bound on the vertex indices, used as the packing radix. It lets the sort order only the low bits the keys can occupy, which is cheaper; without it the keys pack against constants.INDEX_RADIX_PAIR, which orders them identically and needs no bound. It is trusted, not checked.

None

Returns:

Name Type Description
sorted_keys array[uint64]

(3 * n_faces,) ascending keys.

order array[int32]

(3 * n_faces,) halfedge index of each sorted key.

Raises:

Type Description
ValueError

If n_vertices is not positive.

Examples:

With n_vertices=4 an edge (lo, hi) packs as lo + 4 * hi; the two halfedges of the shared edge (1, 2) (halfedges 1 and 3) sort next to each other:

import warp as wp

import ordito as od

faces = wp.array([0, 1, 2, 2, 1, 3], dtype=wp.int32)
sorted_keys, order = od.adjacency.sorted_face_edge_keys(faces, n_vertices=4)
print(sorted_keys.numpy().tolist())  # [4, 8, 9, 9, 13, 14]
print(order.numpy().tolist())  # [0, 2, 1, 3, 4, 5]
Source code in ordito/adjacency.py
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
def sorted_face_edge_keys(
    faces: wp.array[wp.int32], *, n_vertices: int | None = None
) -> tuple[wp.array[wp.uint64], wp.array[wp.int32]]:
    """
    Every halfedge's undirected edge key, sorted, and the permutation that sorted them.

    Halfedge ``3f + k`` is corner ``k`` of face ``f``, the
    [`faces_to_edges`][ordito.edges.faces_to_edges] row order, and its key packs the sorted
    endpoint pair ``(lo, hi)`` as ``lo + hi * radix`` -- the key
    [`hash_indices_rows`][ordito.grouping.hash_indices_rows] gives those rows. The sort is stable,
    so equal keys keep halfedge order: an edge's halfedges are one run, the pairs
    [`face_adjacency`][ordito.adjacency.face_adjacency] groups are its runs of exactly two, and
    ``order[i] // 3`` is the face owning sorted position ``i``.

    Parameters
    ----------
    faces
        ``(3 * n_faces,)`` flat triangle index buffer.
    n_vertices
        Optional exclusive bound on the vertex indices, used as the packing radix. It lets the sort
        order only the low bits the keys can occupy, which is cheaper; without it the keys pack
        against [`constants.INDEX_RADIX_PAIR`][ordito.constants.INDEX_RADIX_PAIR], which orders
        them identically and needs no bound. It is trusted, not checked.

    Returns
    -------
    sorted_keys : wp.array[wp.uint64]
        ``(3 * n_faces,)`` ascending keys.
    order : wp.array[wp.int32]
        ``(3 * n_faces,)`` halfedge index of each sorted key.

    Raises
    ------
    ValueError
        If ``n_vertices`` is not positive.

    Examples
    --------
    With ``n_vertices=4`` an edge ``(lo, hi)`` packs as ``lo + 4 * hi``; the two halfedges of the
    shared edge ``(1, 2)`` (halfedges 1 and 3) sort next to each other:

    ```python
    import warp as wp

    import ordito as od

    faces = wp.array([0, 1, 2, 2, 1, 3], dtype=wp.int32)
    sorted_keys, order = od.adjacency.sorted_face_edge_keys(faces, n_vertices=4)
    print(sorted_keys.numpy().tolist())  # [4, 8, 9, 9, 13, 14]
    print(order.numpy().tolist())  # [0, 2, 1, 3, 4, 5]
    ```
    """
    keys, order, n = _sorted_face_edge_buffers(faces, n_vertices)
    return _launch.view(keys, 0, n), _launch.view(order, 0, n)