selection
triwarp.selection
Face-subset extraction and vertex-selection morphology.
region_boundary_edges
region_boundary_edges(
faces: array[int32],
face_mask: array[bool],
n_vertices: int | None = None,
*,
oriented: bool = False,
) -> twt.Array2dInt32
Interior edges on the boundary of a face region.
Returns the edges that have exactly two incident faces, exactly one of which is in
face_mask — i.e. the interior seam separating the region from the rest of the mesh (mesh
boundary edges, with a single incident face, are excluded).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
faces
|
array[int32]
|
Length- |
required |
face_mask
|
array[bool]
|
Length- |
required |
n_vertices
|
int | None
|
Optional vertex count; inferred from |
None
|
oriented
|
bool
|
Return each row as the directed pair belonging to its region face, instead of the
ascending pair. That puts the region on the left of the contour, which is the orientation
|
False
|
Returns:
| Type | Description |
|---|---|
Array2dInt32
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
See Also
faces_left_of_contour
The inverse: turns this seam back into the region it bounds, given oriented=True.
expand_vertex_mask
Source code in triwarp/selection.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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
faces_left_of_contour
faces_left_of_contour(
faces: array[int32],
contour_edges: Array2dInt32,
*,
n_vertices: int | None = None,
twins: array[int32] | None = None,
) -> wp.array[wp.bool]
Flood-fill the faces on the left of a directed contour of mesh edges.
The dual of region_boundary_edges: that one turns a
face region into the seam around it, this one turns a seam back into a region. Which of the two
sides comes back is decided only by the contour's direction: the seeds are the faces that
own the halfedges a -> b, and a face's corners run counter-clockwise, so reversing the rows
returns the complement. Nothing else about the contour matters, and a closed contour on a closed
mesh therefore partitions it exactly.
The fill is a connected-component labelling of the face-adjacency graph with the contour's dual edges removed, so a contour that does not separate the mesh returns everything reachable -- which is the whole surface. That is the honest answer rather than a failure, but it means the result is worth checking against the input's face count when the contour is meant to close.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
faces
|
array[int32]
|
Length- |
required |
contour_edges
|
Array2dInt32
|
|
required |
n_vertices
|
int | None
|
Total vertex count, used as the key radix. When |
None
|
twins
|
array[int32] | None
|
Optional precomputed |
None
|
Returns:
| Type | Description |
|---|---|
array[bool]
|
Length- |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
RuntimeError
|
If |
Examples:
seam = tw.selection.region_boundary_edges(f, face_mask, oriented=True)
left = tw.selection.faces_left_of_contour(f, seam)
assert np.array_equal(left.numpy(), face_mask.numpy())
See Also
region_boundary_edges
The dual: the seam around a region, which this turns back into a region.
cut_along_edges
Splits the mesh along the same kind of edge set instead of labelling its sides.
submesh_from_face_mask
Turns the returned mask into a mesh.
Source code in triwarp/selection.py
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 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 233 | |
exclude_fully_selected_components
exclude_fully_selected_components(
faces: array[int32],
mask: array[bool],
n_vertices: int,
unique_edges: Array2dInt32 | None = None,
) -> wp.array[wp.bool]
Drop selected vertices whose entire connected component is selected.
A vertex-connected component that is wholly inside mask would make a region-smoothing
Dirichlet system singular (no fixed anchor), so those components are removed from the
selection; components with at least one unselected vertex are kept intact.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
faces
|
array[int32]
|
Length- |
required |
mask
|
array[bool]
|
Length- |
required |
n_vertices
|
int
|
Vertex count (component labels span |
required |
unique_edges
|
Array2dInt32 | None
|
Optional precomputed |
None
|
Returns:
| Type | Description |
|---|---|
array[bool]
|
Selection with fully-selected components removed, on |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
Source code in triwarp/selection.py
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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | |
submesh_from_face_indices
submesh_from_face_indices(
vertices: array[vec3],
faces: array[int32],
face_indices: array[int32],
*,
unique_indices: bool = ...,
return_index: Literal[False] = False,
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
submesh_from_face_indices(
vertices: array[vec3],
faces: array[int32],
face_indices: array[int32],
*,
unique_indices: bool = ...,
return_index: Literal[True],
) -> tuple[
wp.array[wp.vec3],
wp.array[wp.int32],
wp.array[wp.int32],
]
submesh_from_face_indices(
vertices: array[vec3],
faces: array[int32],
face_indices: array[int32],
*,
unique_indices: bool = False,
return_index: bool = False,
) -> (
tuple[wp.array[wp.vec3], wp.array[wp.int32]]
| tuple[
wp.array[wp.vec3],
wp.array[wp.int32],
wp.array[wp.int32],
]
)
Extract a face subset by index and reindex vertices from zero.
Gathers the selected face triplets, compacts referenced vertices, and remaps
face indices into the compact vertex buffer. Matches the core geometry step of
trimesh.util.submesh (without visuals, repair, watertight filtering, or
append).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
face_indices
|
array[int32]
|
1D |
required |
unique_indices
|
bool
|
If |
False
|
return_index
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
sub_vertices |
array[vec3]
|
Compact positions on |
sub_faces |
array[int32]
|
Flat triangle index buffer into |
vertex_index |
array[int32]
|
Only when |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in triwarp/selection.py
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 399 400 401 402 403 404 | |
submeshes_from_face_groups
submeshes_from_face_groups(
vertices: array[vec3],
faces: array[int32],
group_face_indices: array[int32],
group_offsets: array[int32],
) -> tuple[
wp.array[wp.vec3],
wp.array[wp.int32],
wp.array[wp.int32],
]
Extract many face subsets at once: CSR groups in, CSR submeshes out.
The batched form of
submesh_from_face_indices. Every launch count
is O(1) in the number of groups, so extracting ten thousand components costs the same
number of kernel launches and host synchronisations as extracting one — which is the whole
point, since the per-group loop it replaces pays a sort, a scan and a readback each.
How it stays batched: each corner of each selected face is packed into the single int64 key
group * n_vertices + vertex, and one global
unique_1d deduplicates all groups together. Because the key is
group * radix + vertex, ascending key order is exactly (group, vertex) lexicographic
order, so the unique slots come out already partitioned by group and ascending by original
vertex index within each group — bit-identical to running unique_1d per group.
A vertex shared by two groups is duplicated into both, matching
trimesh.util.submesh and the per-group loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
group_face_indices
|
array[int32]
|
Concatenated face indices of every group, as |
required |
group_offsets
|
array[int32]
|
Length- |
required |
Returns:
| Name | Type | Description |
|---|---|---|
vertices_all |
array[vec3]
|
Every group's compacted vertices, concatenated. |
vertex_offsets |
array[int32]
|
Length- |
faces_all |
array[int32]
|
Every group's reindexed flat faces, concatenated in the same group order. Group |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
Source code in triwarp/selection.py
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 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 | |
submesh_from_face_mask
submesh_from_face_mask(
vertices: array[vec3],
faces: array[int32],
face_mask: array[bool],
*,
return_index: Literal[False] = False,
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
submesh_from_face_mask(
vertices: array[vec3],
faces: array[int32],
face_mask: array[bool],
*,
return_index: Literal[True],
) -> tuple[
wp.array[wp.vec3],
wp.array[wp.int32],
wp.array[wp.int32],
]
submesh_from_face_mask(
vertices: array[vec3],
faces: array[int32],
face_mask: array[bool],
*,
return_index: bool = False,
) -> (
tuple[wp.array[wp.vec3], wp.array[wp.int32]]
| tuple[
wp.array[wp.vec3],
wp.array[wp.int32],
wp.array[wp.int32],
]
)
Extract a face subset selected by a per-face boolean mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
face_mask
|
array[bool]
|
Length- |
required |
return_index
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
tuple[array[vec3], array[int32]] | tuple[..., array[int32]]
|
Compact |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
See Also
submesh_from_face_indices
delete_region_keep_boundary
The complement: keep everything outside a region, and report the rims that opens.
Source code in triwarp/selection.py
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 | |
delete_region_keep_boundary
delete_region_keep_boundary(
vertices: array[vec3],
faces: array[int32],
face_mask: array[bool],
) -> tuple[
wp.array[wp.vec3],
wp.array[wp.int32],
list[wp.array[wp.int32]],
]
Remove a face region and report the boundary loops the removal opened.
The complement of
submesh_from_face_mask: it keeps everything the
mask does not select, and it answers the question that makes the result usable -- which rims
are new. That is the distinction a caller needs and cannot recover afterwards: the surviving
mesh's boundary loops are the rims the deletion made plus whatever rims the input already had,
and only the former should be filled. It is the natural input to
triwarp.holes.fill_min_weight, and the first half of
triwarp.holes.refill_region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
face_mask
|
array[bool]
|
Length- |
required |
Returns:
| Name | Type | Description |
|---|---|---|
kept_vertices |
array[vec3]
|
Compact positions of the surviving submesh. |
kept_faces |
array[int32]
|
Flat triangle index buffer into |
new_loops |
list[array[int32]]
|
The boundary loops the deletion opened, as vertex-index cycles into |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
Examples:
doomed = tw.validation.face_self_intersecting_mask(v, f)
kept_v, kept_f, rims = tw.selection.delete_region_keep_boundary(v, f, doomed)
Notes
A loop counts as pre-existing when every one of its edges was already a boundary edge of the input. That is the right test rather than "any": deleting a face that touches an existing rim extends that rim rather than opening a new one, and the extended loop has to be reported -- otherwise a caller filling only the new loops would leave the extension open.
The loop classification is host-side, over the loops themselves rather than over the mesh: a
boundary loop is short next to the surface it bounds, and the edge sets involved are already
materialized by triwarp.boundary.boundary_loops. The call's
cost is dominated by the loop trace and the submesh extraction, so anything spent optimizing
this function further belongs in
triwarp.boundary.boundary_loops rather than here.
See Also
submesh_from_face_mask
The same extraction with the mask's polarity reversed and no loop report.
triwarp.holes.refill_region
Delete and immediately fill, which is what this is usually the first half of.
Source code in triwarp/selection.py
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 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 | |
submesh_from_vertex_indices
submesh_from_vertex_indices(
vertices: array[vec3],
faces: array[int32],
vertex_indices: array[int32],
*,
face_mode: Literal["all", "any"] = "all",
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
Extract faces incident on the given vertices and reindex from zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
vertex_indices
|
array[int32]
|
1D |
required |
face_mode
|
Literal['all', 'any']
|
|
'all'
|
Returns:
| Type | Description |
|---|---|
tuple[array[vec3], array[int32]]
|
Compact |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in triwarp/selection.py
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 | |
submesh_from_vertex_mask
submesh_from_vertex_mask(
vertices: array[vec3],
faces: array[int32],
vertex_mask: array[bool],
*,
face_mode: Literal["all", "any"] = "all",
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
Extract faces incident on vertices selected by a boolean mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
vertex_mask
|
array[bool]
|
Length- |
required |
face_mode
|
Literal['all', 'any']
|
|
'all'
|
Returns:
| Type | Description |
|---|---|
tuple[array[vec3], array[int32]]
|
Compact |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
Notes
A mask is a cheaper input than the equivalent index list, not merely a more convenient one:
the face reduction reads it directly, where the index form has to rebuild membership through
isin first. The index form still pays that cost, because it starts from
indices and has nothing else to go on.
See Also
submesh_from_vertex_indices
The index form, for a caller that holds a list rather than a mask.
submesh_from_face_mask
The per-face selection this reduces onto.
Source code in triwarp/selection.py
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 | |
expand_vertex_mask
expand_vertex_mask(
faces: array[int32],
mask: array[bool],
hops: int,
unique_edges: Array2dInt32 | None = None,
) -> wp.array[wp.bool]
Grow a vertex selection by hops one-ring layers.
Each round adds every vertex sharing a mesh edge with a currently-selected vertex, so after
hops rounds the mask covers all vertices within graph distance hops of the input
selection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
faces
|
array[int32]
|
Length- |
required |
mask
|
array[bool]
|
Length- |
required |
hops
|
int
|
Number of one-ring dilation rounds ( |
required |
unique_edges
|
Array2dInt32 | None
|
Optional precomputed |
None
|
Returns:
| Type | Description |
|---|---|
array[bool]
|
Dilated mask on |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
See Also
Notes
Dilation is vertex-based: a vertex enters the mask when any 1-ring neighbour is in it.
The per-round wp.clone is not worth removing. The kernel only sets bits, so each round
must start from a copy of the previous mask; ping-ponging two preallocated buffers would keep
the copy and drop only the allocation, which is a small fraction of the round's cost.
Source code in triwarp/selection.py
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 | |
shrink_vertex_mask
shrink_vertex_mask(
faces: array[int32],
mask: array[bool],
hops: int,
unique_edges: Array2dInt32 | None = None,
) -> wp.array[wp.bool]
Erode a vertex selection by hops one-ring layers.
Implemented as the complement of an expand_vertex_mask
of the complement: a vertex is removed if any vertex within hops hops is unselected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
faces
|
array[int32]
|
Length- |
required |
mask
|
array[bool]
|
Length- |
required |
hops
|
int
|
Number of one-ring erosion rounds. |
required |
unique_edges
|
Array2dInt32 | None
|
Optional precomputed |
None
|
Returns:
| Type | Description |
|---|---|
array[bool]
|
Eroded mask on |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
See Also
Notes
Erosion here is vertex-based: a vertex survives when every 1-ring neighbour is also in the mask. MeshLab's Erode Selection is a face-based operation and gives a different answer.
Source code in triwarp/selection.py
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 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 | |
face_indices_from_vertex_indices
face_indices_from_vertex_indices(
faces: array[int32],
vertex_indices: array[int32],
*,
face_mode: Literal["all", "any"] = "all",
n_vertices: int | None = None,
) -> wp.array[wp.int32]
Face indices whose vertex indices match a set under an all/any rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
faces
|
array[int32]
|
Length- |
required |
vertex_indices
|
array[int32]
|
1D |
required |
face_mode
|
Literal['all', 'any']
|
|
'all'
|
n_vertices
|
int | None
|
Optional vertex count, forwarded to |
None
|
Returns:
| Type | Description |
|---|---|
array[int32]
|
Selected face indices on |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
Source code in triwarp/selection.py
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 | |