repair
triwarp.repair
Removing what should not be in a mesh: redundant elements, bad triangles and inconsistent winding.
make_solid is the composite most callers want -- a broken digitised
surface in, a single watertight solid out -- and everything below is a stage of it that is also
useful on its own.
Five defects are visible in the index buffer alone, and each has a remover:
remove_unreferenced_vertices,
remove_duplicated_vertices,
resolve_duplicated_faces,
remove_degenerate_faces, and
collapse_small_triangles.
A sixth remover answers a different question -- not which elements are wrong but which parts of
the mesh are not the mesh: remove_small_components
drops face-connected components that are too small, by face count, area or bounding-box diameter.
It is the first step of every repair pipeline, and the debris it removes is not defective in
itself.
Two further defects need geometry rather than topology to detect, so they are found by a threshold
rather than a rule: validation.face_defective_mask flags
faces that are too thin, misoriented against their neighbourhood, or folded back over it -- it lives
with the other per-element detectors. remove_folded_faces
deletes the folded ones and flip_t_vertices flips away the
slivers a T-junction leaves behind.
The verb predicts the return shape, and that is a rule rather than a coincidence:
remove_*/collapse_*change the element count, so they return(vertices, faces)or more -- there is a new position buffer because vertices went away or moved.make_*preserve positions and counts and rewrite only the index buffer, so they returnfacesalone -- unless the name is a whole-mesh outcome rather than a property of the index buffer, which ismake_solidalone. It is the composite of most of this module and returns(vertices, faces)like the removers it runs; the three property-fixers beside it (make_winding_consistent,make_volume,make_normals_outward) returnfaces.reverse_windingis the one verb outside that scheme, and it obeys the same shape rule for the same reason: it rewrites only the index buffer, so it returnsfaces. It is not amake_*because it establishes no property -- it flips orientation unconditionally, where the threemake_*fixers decide face by face.- A verb that only moves vertices returns the positions alone, since neither buffer of indices
changes:
flatten_degree3_verticesis the only one, and it is here rather than intriwarp.smoothing-- whose every member has that same signature -- because it is the gentler half of a pair withremove_degree3_vertices: same defect, same test, one answering it by deleting the vertex and one by flattening the bump. Splitting the pair across two modules to satisfy the shape rule would cost more than the exception does. *_maskare detectors: they return awp.array[wp.bool]and mutate nothing. The one this module used to hold now lives intriwarp.validation.
Two names carry a verb of their own and follow one of the rules anyway, which is worth saying so the
list does not read as exhaustive. flip_t_vertices is named for
the make_* pattern rather than the remove_* one because it removes nothing: it flips the
long edge of each sliver a T-junction leaves, so the face count is unchanged and only faces
comes back. straighten_boundary is the same shape under a
geometric verb. And fix_self_intersections,
split_non_manifold_vertices,
collapse_small_triangles and
resolve_duplicated_faces all change the element count
and all return what the first rule says.
There is no eliminate_*. Two functions used to spell it that way and both opened their own
summary line with the word "Remove" -- one verb in the name and another in the sentence
mkdocstrings renders beside it -- so they are
remove_degree3_vertices and
remove_tunnels.
make_solid
make_solid(
vertices: array[vec3],
faces: array[int32],
*,
keep_largest: bool = True,
join_components: bool = False,
max_iter: int = 10,
inner_iter: int = 3,
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
Turn a broken digitised surface into a single watertight solid.
The composite this module's pieces exist to make possible, and the operation a caller reaching
for "repair" usually means: debris removed, holes closed, degeneracies collapsed and
self-intersections cut out and refilled, alternating until nothing is left to fix. Every stage
is a public function here or in triwarp.holes; what this adds is the order
and the loop, which is where the difficulty actually is -- closing a hole can create a
self-intersection, and cutting one out reopens a hole.
The stages, in order:
- The connectivity repair the reference implementation performs inside its loader, and which
therefore does not look like a stage at all until it is missing:
remove_unreferenced_vertices,make_winding_consistentandsplit_non_manifold_vertices. Without it a mesh whose defect is a non-manifold edge comes back with the right Euler characteristic and one component and is still not watertight, because no later stage looks at edge manifoldness. keep_largest->remove_small_components, so the scan debris goes before anything expensive runs on it.join_components->holes.join_closest_components, for an input whose pieces are meant to be one surface rather than a largest piece plus rubbish. Mutually useful withkeep_largestrather than exclusive: keep the big piece and weld what is left.holes.fill_min_weightif any boundary remains.- Up to
max_iterrounds ofremove_degenerate_facesandcollapse_small_triangles, thenfix_self_intersectionsatmax_iter=inner_iter. The loop exits as soon as a round changes nothing. - A final fill if stage 4 reopened a boundary -- which it routinely does, since cutting an intersecting region out is what opens one. Nothing geometric runs after it, and that is not an omission: filling a 3-vertex rim produces one sliver, a degeneracy pass deletes the sliver and reopens the rim, and the two trade the same faces indefinitely if run again after the fill.
Under keep_largest the component filter runs inside stage 4 as well as at the top, and
that is not belt and braces: cutting an intersecting band out can disconnect the surface, so the
extra piece does not exist yet when stage 1 looks. For example, on a torus whose inner wall
crosses itself, the intersection repair alone can leave two closed shells where the input was
one.
It returns the best it managed, not a guarantee
There is no success flag, deliberately. Convergence is not guaranteed for any input -- a
patch can intersect something, and its repair can open another hole -- so on a stubborn mesh
this returns a partly repaired surface rather than looping harder or raising. Ask
validation.is_watertight if the answer matters.
join_components is where that bites in practice, and the failure mode is worth knowing
because it is not a bug in any stage: welding several shells leaves one rim spanning all
of them, and if that rim is badly non-planar the minimum-weight patch across it
self-intersects, so stage 4 cuts the patch out and undoes the join. For example, three
hemispherical bowls joined at coplanar rims converge to one solid, but the same bowls
tilted 45 degrees relative to each other come back as three separate shells. Rims that
are far from coplanar want
holes.stitch_loops or a per-pair
holes.bridge_edges followed by a targeted fill, not this.
The reference implementation prints a diagnostic here and its own caller reports that diagnostic inverted, which is worth knowing only as a reason not to trust a boolean of this shape from anywhere: read the mesh.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
keep_largest
|
bool
|
Drop every connected component but the one with the most faces, first. |
True
|
join_components
|
bool
|
Bridge the remaining open components together instead of leaving them separate. |
False
|
max_iter
|
int
|
Cap on the alternating degeneracy / self-intersection rounds. |
10
|
inner_iter
|
int
|
Cap on the cut-and-refill passes inside each self-intersection repair. |
3
|
Returns:
| Name | Type | Description |
|---|---|---|
new_vertices |
array[vec3]
|
Positions of the repaired mesh, on |
new_faces |
array[int32]
|
Flat face buffer of the repaired mesh. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
See Also
validation.is_watertight
The question this does not answer for you.
remove_small_components
fix_self_intersections
holes.fill_min_weight
make_normals_outward
What to run afterwards if the winding has to face outward as well as be consistent.
Source code in triwarp/repair.py
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 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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
remove_unreferenced_vertices
remove_unreferenced_vertices(
vertices: array[vec3],
faces: array[int32],
*,
return_inverse: bool = False,
) -> (
tuple[
wp.array[wp.vec3],
wp.array[wp.int32],
wp.array[wp.int32],
]
| tuple[
wp.array[wp.vec3],
wp.array[wp.int32],
wp.array[wp.int32],
wp.array[wp.int32],
]
)
Remove vertices not referenced by any face and remap face indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Flat triangle index buffer. |
required |
return_inverse
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
new_vertices |
array[vec3]
|
Referenced vertices only, compacted from index zero. |
new_faces |
array[int32]
|
Face buffer with indices remapped into |
remap |
array[int32]
|
Length |
inverse |
(array[int32], optional)
|
Present when |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in triwarp/repair.py
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 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 | |
remove_duplicated_vertices
remove_duplicated_vertices(
vertices: array[vec3],
faces: array[int32],
epsilon: float = 0.0,
) -> tuple[
wp.array[wp.vec3],
wp.array[wp.int32],
wp.array[wp.int32],
wp.array[wp.int32],
]
Merge duplicate vertex positions up to a coordinate tolerance and remap face indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Flat triangle index buffer. |
required |
epsilon
|
float
|
Uniqueness tolerance. Positive values snap coordinates to |
0.0
|
Returns:
| Name | Type | Description |
|---|---|---|
unique_vertices |
array[vec3]
|
Deduplicated vertex positions (first occurrence per equivalence class). |
unique_indices |
array[int32]
|
Length |
inverse |
array[int32]
|
Length |
unique_faces |
array[int32]
|
Face buffer with indices remapped into |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Notes
Both tolerance modes quantize positions and group by cell, so both separate a pair straddling a
cell boundary however close it is. That is worth knowing about epsilon=0 in particular,
which is not an exact-equality test despite requiring no tolerance: it buckets by the high
bits of each coordinate's float32 representation, giving a relative cell about 2.4e-4
wide. So 1.0 and 1.000244 merge, while two adjacent float32 values on either side of
a bucket edge do not. Prefer an explicit epsilon whenever the tolerance matters; use 0
only to collapse positions that are already bitwise equal, which it does reliably (including
across +0.0 / -0.0).
Duplicates that are known from construction rather than measured are better collapsed directly:
see revolve, which derives them from its profile instead of
hashing positions.
See Also
duplicate_vertex_inverse
hash_vector_rows
triwarp.seams.cut_along_edges
The inverse operation: welding coincident positions back together closes every cut it makes.
Source code in triwarp/repair.py
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 | |
duplicate_vertex_inverse
duplicate_vertex_inverse(
vertices: array[vec3], epsilon: float
) -> wp.array[wp.int32]
Map each vertex to the slot of its coincident-vertex equivalence class.
The inverse map produced by welding vertices at epsilon tolerance, without also
computing the deduplicated vertex/face buffers — useful for remapping per-vertex
attributes (colors, UVs, ...) to match a remove_duplicated_vertices call made with the same epsilon.
This is the shared equivalence map the dedup remaps by, not a remover -- which is why it lives
here rather than in triwarp.grouping beside
first_occurrence_indices, whose shape it has. Its
key is a tolerance-quantized position, and choosing that tolerance is mesh-repair policy;
grouping is deliberately dtype-generic and geometry-free, so an epsilon there would be
the first crack in that contract.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
epsilon
|
float
|
Uniqueness tolerance, with the same meaning as in
|
required |
Returns:
| Type | Description |
|---|---|
array[int32]
|
Length |
See Also
remove_duplicated_vertices
The remover this map is the shared half of.
hash_vector_rows
grouping.first_occurrence_indices
The geometry-free counterpart: the same class-to-representative reduction over any key.
Source code in triwarp/repair.py
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 | |
resolve_duplicated_faces
resolve_duplicated_faces(
faces: array[int32],
) -> tuple[wp.array[wp.int32], wp.array[wp.int32]]
Resolve duplicated triangles by orientation-aware cancellation rules.
For each geometric duplicate:
- equal positive and negative counts: remove all copies;
- one extra positive copy: keep one positively oriented face;
- one extra negative copy: keep one negatively oriented face;
- otherwise raise ValueError when counts are not orientable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
faces
|
array[int32]
|
Flat triangle index buffer. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
resolved_faces |
array[int32]
|
Flat buffer of kept faces. |
kept_indices |
array[int32]
|
Original face indices into the input |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a duplicate group's signed count is not orientable, i.e. its positive and negative copies differ by more than one. |
Source code in triwarp/repair.py
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 541 542 543 544 545 546 547 548 549 | |
remove_degenerate_faces
remove_degenerate_faces(
vertices: array[vec3], faces: array[int32]
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
Drop degenerate (zero-area) triangles and reindex, keeping vertex positions unchanged.
Mirrors trimesh.Trimesh.nondegenerate_faces + update_faces: a face is degenerate when
two of its vertices coincide or its three vertices are collinear, detected by
face_nondegenerate_mask (both triangle altitudes
exceed the merge tolerance). Surviving faces are unchanged; vertices left unreferenced after the
drop are removed by the reindexing in
submesh_from_face_mask.
Unlike collapse_small_triangles, no vertices are
merged and no edges are collapsed: this only removes faces already degenerate in the input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
Returns:
| Name | Type | Description |
|---|---|---|
new_vertices |
array[vec3]
|
Vertices still referenced by a non-degenerate face, compacted from index zero. |
new_faces |
array[int32]
|
Flat buffer of the non-degenerate faces, remapped into |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in triwarp/repair.py
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 | |
remove_non_manifold_faces
remove_non_manifold_faces(
vertices: array[vec3],
faces: array[int32],
max_iter: int = 3,
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
Remove faces touching a non-manifold (>2-incident) edge, iterating until edge-manifold.
Each pass keeps only faces whose three edges are each used by at most two faces
(edge_manifold_mask); dropping a face can make a
neighbour manifold, so it repeats up to max_iter times.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
max_iter
|
int
|
Maximum number of removal passes. |
3
|
Returns:
| Name | Type | Description |
|---|---|---|
new_vertices |
array[vec3]
|
Vertices still referenced after non-manifold faces are dropped, compacted from index zero. |
new_faces |
array[int32]
|
Flat buffer of the surviving (edge-manifold, up to |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in triwarp/repair.py
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 | |
remove_small_components
remove_small_components(
vertices: array[vec3],
faces: array[int32],
*,
keep_largest: bool = False,
min_faces: int | None = None,
min_area: float | None = None,
min_diameter: float | None = None,
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
Drop face-connected components that are too small, by one of four measures of "small".
Debris -- a stray shell from a scan, a sliver left by a boolean, a component a decimation
stranded -- is what every repair pipeline removes first, and until now
combine.split handed back every component and left the caller to
write the argmax. This is that step, and it stays on the device: the per-component statistic is
accumulated by one scatter and thresholded by one kernel, so nothing is read back and no
component is ever materialized as its own mesh.
Exactly one criterion may be given, because they are four different questions rather than four spellings of one and combining them would hide which one rejected a component:
keep_largestkeeps the single component with the most faces and drops every other, so the result always has exactly one component. Face count, not area or diameter: a small dense shell outranks a large coarse one. A tie goes to the component whose lowest face index is smallest, which makes the choice deterministic rather than dependent on thread order.min_faceskeeps components with at least that many faces -- scale-free, and the measure that tracks how much data a component carries.min_areakeeps components whose summed triangle area reaches it.min_diameterkeeps components whose axis-aligned bounding-box diagonal reaches it, which is the measure that survives a component being a thin sheet of many tiny triangles.
All three min_* bounds are inclusive, matching the reference implementations: a
component of exactly the threshold face count, or of exactly the threshold diagonal, survives.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
keep_largest
|
bool
|
Keep only the component with the most faces. |
False
|
min_faces
|
int | None
|
Minimum face count for a component to be kept. |
None
|
min_area
|
float | None
|
Minimum summed triangle area for a component to be kept. |
None
|
min_diameter
|
float | None
|
Minimum bounding-box diagonal for a component to be kept. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
new_vertices |
array[vec3]
|
Vertices of the surviving components, compacted from index zero. |
new_faces |
array[int32]
|
Flat buffer of the surviving faces, in their input order. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no criterion is given, or if more than one is. |
RuntimeError
|
If |
See Also
combine.split
The whole decomposition, when every component is wanted rather than a subset.
holes.join_closest_components
The other answer to a multi-component mesh -- weld the pieces together instead of
discarding them.
adjacency.face_connected_component_labels
The labelling this thresholds.
remove_non_manifold_faces
Notes
A component's label is a representative face index rather than a dense 0..k-1 id, so the
per-component statistic is an n_faces-long array of which only k slots are ever written.
That is deliberate: densifying the labels first would cost a sort and a search per face to save
an allocation, and it is the allocation that is cheap.
Source code in triwarp/repair.py
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 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 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 | |
split_non_manifold_vertices
split_non_manifold_vertices(
vertices: array[vec3], faces: array[int32]
) -> tuple[
wp.array[wp.vec3],
wp.array[wp.int32],
wp.array[wp.int32],
]
Make a mesh manifold and orientable by duplicating vertices, keeping every face.
The non-lossy counterpart of
remove_non_manifold_faces, which deletes geometry
to reach the same property: this changes no position and drops no triangle, it only splits
vertices apart, so the surface is unchanged and the face count is exactly preserved. Use it to
feed a mesh to code that requires manifold input -- several reference libraries (e.g.
potpourri3d) reject non-manifold meshes outright, and so do triwarp's own halfedge consumers.
Two corners are kept together only across an edge that is manifold and consistently
oriented: exactly one half-edge each way. Every other edge -- a boundary edge, one shared by
three or more faces, or one whose two faces traverse it the same way -- separates the copies. So
a bowtie vertex splits in two, an edge with three faces splits into three boundary edges, and a
flipped face is cut free of its neighbours rather than reoriented (that is
make_winding_consistent's job, and running it first
leaves less to split).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
Returns:
| Name | Type | Description |
|---|---|---|
new_vertices |
array[vec3]
|
|
new_faces |
array[int32]
|
Flat buffer of the same |
source |
array[int32]
|
Length |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Notes
Vertices unreferenced by any face are dropped, since a new vertex only exists as some face's
corner -- the same convention as
remove_unreferenced_vertices, and the reason
this function cannot simply return an n_vertices-length remap.
Remove degenerate faces first, with
remove_degenerate_faces: a triangle with a repeated
index has two corners at one vertex that no edge can join, so it survives as two copies of
that vertex and the face stays degenerate.
The implementation is a connected-components pass over a graph of 3 * n_faces corner nodes
rather than a per-vertex star walk: one kernel counts each edge's half-edges by direction, a
second emits two links per mergeable edge, and
connected_component_labels_from_edges
does the merging. igl::split_nonmanifold instead explodes the mesh to 3 * n_faces
singleton vertices and greedily re-merges pairs, re-testing manifoldness after each candidate --
order-dependent and sequential by construction. The two agree exactly on a manifold mesh, a
bowtie vertex, a consistently-wound fan of three faces on one edge (both split it into three),
a flipped face and an open boundary.
They differ on one input class, by design. Where an edge carries one half-edge in one
direction and several in the other -- which is what a duplicated face produces -- igl keeps
one arbitrarily chosen pair joined, while this splits every copy: 18 vertices against igl's 15
on an icosahedron with one face duplicated, and 8 527 against 8 320 on bunny_decimated,
whose 87 duplicated faces are its only non-manifoldness. Both results are edge- and
vertex-manifold with the input's face count; this one is order-independent and makes no
arbitrary choice. Note that
resolve_duplicated_faces is not a way around the
difference: libigl's cancellation rules it implements cover a +1/-1 imbalance, so a face
duplicated in the same orientation makes it raise rather than dropping the copy.
See Also
remove_non_manifold_faces
make_winding_consistent
remove_degenerate_faces
is_edge_manifold
igl.split_nonmanifold
Source code in triwarp/repair.py
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 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 942 943 944 945 946 947 948 949 950 | |
collapse_small_triangles
collapse_small_triangles(
vertices: array[vec3],
faces: array[int32],
epsilon: float = 1e-06,
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
Collapse triangles smaller than a bounding-box-relative area threshold.
Mirrors igl::collapse_small_triangles. A triangle is small when its doubled area is below
2 * epsilon * bbd ** 2, where bbd is the diagonal of the axis-aligned bounding box of
vertices. Each small triangle has its shortest edge collapsed by merging that edge's two
endpoints; the merged face (now carrying a repeated vertex) is discarded. The process repeats to
a fixpoint, so triangles that only become small after a neighbouring collapse are also removed.
This subsumes degenerate-triangle removal: an exactly degenerate face (zero area) is always
below the threshold, so passing a small epsilon removes it. To drop only degenerate faces
without any bounding-box-relative collapsing, use
remove_degenerate_faces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
epsilon
|
float
|
Relative area tolerance. The doubled-area threshold is |
1e-06
|
Returns:
| Name | Type | Description |
|---|---|---|
new_vertices |
array[vec3]
|
Vertices surviving the collapse, compacted from index zero. |
new_faces |
array[int32]
|
Flat buffer of the surviving faces, remapped into |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Notes
Where igl::collapse_small_triangles merges vertices by a sequentially updated index map and
recurses until no edge collapses, this resolves all shortest-edge merges of one pass at once via
the connected-components closure of
connected_component_labels_from_edges,
then loops over the shrinking mesh. Both converge to a mesh with no sub-threshold triangle. The
surviving vertex of a collapsed edge keeps the position of the component representative (the
lowest original index in its class) rather than libigl's longest-edge-preserving endpoint; for
sub-threshold triangles the two endpoints are close enough that the difference is negligible.
The bounding-box diagonal is measured once on the input vertices so the threshold is fixed
across iterations.
Source code in triwarp/repair.py
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 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 | |
straighten_boundary
straighten_boundary(
vertices: array[vec3],
faces: array[int32],
*,
min_normal_dot: float = 0.9,
max_aspect_ratio: float = 10.0,
iterations: int = 1,
return_count: bool = False,
) -> wp.array[wp.int32] | tuple[wp.array[wp.int32], int]
Close concave notches in the mesh's rim, one triangle at a time.
A rim that came out of a clip, a decimation or a scan is ragged: it zig-zags by one triangle even where the surface is smooth. Each pass adds the single triangle that spans a rim vertex's two neighbours, wherever that triangle faces the same way as the surface it joins and is not a sliver. No vertex moves and none is added -- the mesh only gains faces, so the interior is untouched and every existing index stays valid.
Two gates, and both matter. min_normal_dot is what distinguishes a notch from a corner: a
convex rim corner would be closed by a triangle facing away from the surface, and filling it
folds the mesh over. max_aspect_ratio stops the pass trading a ragged rim for a fan of
slivers. Neither has a natural default, which is why both are keywords rather than constants.
Adjacent notches share a rim edge, so one pass closes a maximal independent set of them --
lowest index wins, deterministically -- and iterations passes go round again on what is
left. A pass that closes nothing ends the loop early.
Edge-manifoldness is not enough to make the rim a set of simple loops: at a bowtie rim vertex two loops are pinched at one point, so that vertex has two outgoing boundary halfedges and no single successor. The rim is therefore followed by halfedge rather than by vertex, which gives each loop its own links: both notches at such a vertex are closed, neither is attached across the pinch, and the answer is the same on every device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
min_normal_dot
|
float
|
Minimum cosine between the new triangle's normal and each of the two rim faces it will
border. |
0.9
|
max_aspect_ratio
|
float
|
Largest circum-radius over twice the in-radius the new triangle may have, as
|
10.0
|
iterations
|
int
|
Number of independent-set passes. |
1
|
return_count
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
faces |
array[int32]
|
Flat triangle index buffer with the new triangles appended. The vertex buffer is unchanged and is not returned. |
added |
(int, optional)
|
Present when |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
See Also
fill_min_weight
Closes a rim completely; this only tidies its shape and leaves it open.
ears
Finds the faces with two rim edges, which is the dual situation -- an ear sticks out where a
notch cuts in.
boundary_loops
Source code in triwarp/repair.py
1057 1058 1059 1060 1061 1062 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 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 | |
remove_degree3_vertices
remove_degree3_vertices(
vertices: array[vec3],
faces: array[int32],
*,
max_iter: int = 8,
return_count: bool = False,
) -> (
tuple[wp.array[wp.vec3], wp.array[wp.int32]]
| tuple[wp.array[wp.vec3], wp.array[wp.int32], int]
)
Remove interior vertices with exactly three incident faces, collapsing each fan to one triangle.
A valence-3 interior vertex carries no information the surface needs: its three faces tile the triangle formed by its three neighbours, so deleting it and keeping that triangle changes the connectivity and nothing else. They are a standard residue of subdivision, of decimation and of hole filling, and they make every downstream valence statistic worse.
Adjacent candidates share faces, so a pass removes a maximal independent set -- the
lowest-indexed of any two neighbouring candidates wins, deterministically -- and the loop
repeats until none is left or max_iter passes have run. Removing one vertex can create
another, which is why return_count reports what happened rather than promising it.
The other half of a double-face pass already exists
A pair of triangles on the same three vertices is
resolve_duplicated_faces' job, and this
function deliberately does not repeat it. The two together are what a "remove double faces"
pass means elsewhere; they are kept apart because they have different orientation rules and
different answers on a non-orientable mesh.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
max_iter
|
int
|
Cap on the number of passes. Each pass removes an independent set, so a chain of adjacent candidates needs one pass per link; the default covers any chain length likely in practice. |
8
|
return_count
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
vertices |
array[vec3]
|
Positions of the result, with the removed vertices compacted away. No position moves. |
faces |
array[int32]
|
Flat triangle index buffer, three faces shorter per removed vertex plus one longer. |
removed |
(int, optional)
|
Present when |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
See Also
flatten_degree3_vertices
The geometric answer to the same defect: move the vertex instead of deleting it, which
leaves the connectivity and every per-vertex attribute intact.
resolve_duplicated_faces
The other half: triangles repeated on the same three vertices.
remove_degenerate_faces
Removes faces by geometry; this one removes a vertex by its connectivity alone.
vertex_one_rings
The fan this walks.
Source code in triwarp/repair.py
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 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 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 | |
flatten_degree3_vertices
flatten_degree3_vertices(
vertices: array[vec3],
faces: array[int32],
region: array[bool] | None = None,
*,
max_iter: int = 8,
rings: tuple[array[int32], array[int32], array[bool]]
| None = None,
) -> wp.array[wp.vec3]
Flatten each interior valence-3 vertex into the plane of its three neighbours.
A valence-3 interior vertex sits on a little tetrahedral bump: three triangles meeting at a point over the triangle its neighbours form. Moving it to their centroid puts it in that triangle's plane, so the bump disappears and the three faces become coplanar -- which is what makes a subdivision, a decimation or a hole fill stop leaving visible pimples.
The gentler half of a pair.
remove_degree3_vertices
answers the same defect by deleting the vertex and keeping one triangle, which changes the
connectivity; this keeps every vertex and every face and only moves positions, so a caller
holding per-vertex attributes or a face selection can use it and the other one would invalidate
both.
Two interior valence-3 vertices can be neighbours -- a tetrahedron is four of them, each
adjacent to the other three -- and moving both at once puts neither in the other's new plane.
So a pass flattens a maximal independent set of them, lowest index wins, exactly as
remove_degree3_vertices does, and max_iter
passes go round again on the candidates left over. Every vertex lands in its neighbours' plane
as it is moved, which is the guarantee the name makes; where two candidates are adjacent the
later one sees the earlier one's new position, so the order the passes impose is part of the
answer rather than an implementation detail. The loop stops as soon as no candidate is left, so
the usual mesh -- where no two candidates touch -- takes one pass and one check.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
region
|
array[bool] | None
|
|
None
|
max_iter
|
int
|
Cap on the number of passes. Each pass flattens an independent set, so a chain of adjacent candidates needs one pass per link; the default covers any chain length likely in practice -- a tetrahedron, the densest case there is, needs four. |
8
|
rings
|
tuple[array[int32], array[int32], array[bool]] | None
|
Optional precomputed |
None
|
Returns:
| Type | Description |
|---|---|
array[vec3]
|
Positions on |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
See Also
remove_degree3_vertices
The topological answer to the same defect: delete the vertex instead of moving it.
equalize_triangle_areas
Relaxes every vertex toward an area objective, where this hard-sets only the valence-3 ones.
Source code in triwarp/repair.py
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 | |
reverse_winding
reverse_winding(faces: array[int32]) -> wp.array[wp.int32]
Reverse every face's winding, flipping the surface's orientation.
Rewrites each triangle (a, b, c) as (c, b, a), which negates every face normal and the
enclosed signed volume. Unconditional: unlike
make_winding_consistent and
make_normals_outward, which decide per face, this
flips all of them, so a consistently wound mesh stays consistent and an inconsistent one stays
inconsistent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
faces
|
array[int32]
|
Length- |
required |
Returns:
| Type | Description |
|---|---|
array[int32]
|
New face buffer of the same length. |
Notes
An involution: applying it twice returns the original buffer exactly.
Each face's signed volume negates exactly -- reversing a triangle's corners swaps two arguments of a scalar triple product, which negates the same floating-point products rather than recomputing them -- so the total negates bit-for-bit wherever the reduction visits faces in a fixed order. The example below asserts only the sign, since that is what holds independently of the reduction.
The corner that stays first is a convention, and this one matches np.fliplr -- which is
what trimesh.Trimesh.invert applies, so the two agree elementwise rather than only up to a
rotation of each row. kernels.repair.flip_faces_masked, which the per-face flippers use,
keeps corner 0 instead; both reverse orientation and they differ by a cyclic rotation.
Examples:
flipped = tw.repair.reverse_winding(f)
assert tw.measures.volume(v, flipped) < 0.0 < tw.measures.volume(v, f)
See Also
triwarp.mesh.Trimesh.invert
The cached-mesh form, which carries what survives a flip.
make_winding_consistent
make_normals_outward
triwarp.validation.is_winding_consistent
Source code in triwarp/repair.py
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 | |
make_winding_consistent
make_winding_consistent(
faces: array[int32],
) -> wp.array[wp.int32]
Flip faces so every shared edge is traversed in opposite directions by its two faces.
Reuses the orientation flood-fill of
face_flip_mask (one arbitrary seed
face per connected component) and reverses the winding of every face whose orientation bit is
set. The result satisfies
is_winding_consistent whenever one exists,
which is to say whenever the mesh is
is_orientable; an already-consistent mesh is returned
unchanged. Mirrors trimesh.repair.fix_winding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
faces
|
array[int32]
|
Length- |
required |
Returns:
| Type | Description |
|---|---|
array[int32]
|
New flat face buffer with corrected winding, on |
Notes
The reference winding within each connected component is arbitrary (the seed face keeps its
orientation), matching trimesh.repair.fix_winding's BFS. Use
make_volume afterwards to also orient normals outward.
On a non-orientable mesh no consistent winding exists, so this cannot succeed and does not
fail either: the flood-fill orients everything it reaches and the contradiction is left on a
seam. The seam can even end up with more inconsistent edges than before the pass ran, not
merely the same ones — so treat the result as unrepaired rather than partly repaired, and test
with is_orientable first if that matters.
Source code in triwarp/repair.py
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 | |
make_volume
make_volume(
vertices: array[vec3],
faces: array[int32],
*,
multibody: bool = False,
) -> wp.array[wp.int32]
Orient faces so the mesh encloses a positive signed volume (normals point outward).
Mirrors trimesh.repair.fix_inversion. With multibody=False (default) the mesh is only
corrected when it is watertight (every undirected edge shared by exactly two faces) and its
total signed volume is negative, in which case every face is reversed. With multibody=True
each connected component is corrected independently by the sign of its own signed volume.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
multibody
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
array[int32]
|
New flat face buffer with outward-oriented normals, on |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
See Also
is_volume
Test whether this succeeded.
volume
Measure the result.
make_winding_consistent
make_normals_outward
Notes
The signed volume is sum(dot(v0, cross(v1, v2)) / 6) measured from the origin, as in
is_volume. Unlike trimesh.repair.fix_inversion's
multibody path, this does not skip components that are not watertight/consistently wound: an
open component's signed volume is ill-defined and may be flipped spuriously. Run
make_winding_consistent first (see
make_normals_outward) and reserve multibody
for meshes whose bodies are individually closed.
Source code in triwarp/repair.py
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 | |
make_normals_outward
make_normals_outward(
vertices: array[vec3],
faces: array[int32],
*,
multibody: bool = False,
) -> wp.array[wp.int32]
Make winding consistent and orient normals outward (winding fix followed by inversion fix).
Equivalent to trimesh.repair.fix_normals: first
make_winding_consistent gives every connected
component a coherent winding, then make_volume flips it (or each
body, with multibody=True) so normals point outward. On a watertight, orientable mesh the
result satisfies is_volume.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
multibody
|
bool
|
Forwarded to |
False
|
Returns:
| Type | Description |
|---|---|
array[int32]
|
New flat face buffer with consistent winding and outward normals, on |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
See Also
Source code in triwarp/repair.py
1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 | |
remove_folded_faces
remove_folded_faces(
vertices: array[vec3],
faces: array[int32],
*,
angle: float = 160.0,
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
Drop faces that fold back over their own ring, and reindex.
A folded face is one whose dihedral angle to a neighbour is near pi: the two triangles lie
almost on top of each other with opposite normals, which is what a badly reconstructed or
self-intersecting patch looks like locally. Such a face contributes no surface and breaks every
normal-based computation downstream, so removing it is a repair rather than a simplification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
angle
|
float
|
Dihedral threshold in degrees; a face with a neighbour above it is dropped. MeshLab's
|
160.0
|
Returns:
| Name | Type | Description |
|---|---|---|
new_vertices |
array[vec3]
|
Vertices still referenced by a kept face, compacted from index zero. |
new_faces |
array[int32]
|
Flat buffer of the kept faces, remapped into |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Notes
MeshLab's meshing_remove_folded_faces flips the offending edge instead of deleting the
face, which preserves the face count but can only help when the fold is a triangulation mistake
rather than genuinely folded geometry. Deletion is the choice the rest of this module makes (see
remove_degenerate_faces and
remove_non_manifold_faces), and it leaves a hole
that triwarp.holes can retriangulate properly.
Source code in triwarp/repair.py
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 | |
fix_self_intersections
fix_self_intersections(
vertices: array[vec3],
faces: array[int32],
*,
method: Literal["local", "voxel"] = "local",
max_expand: int = 1,
max_iter: int = 3,
voxel_size: float | None = None,
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
Remove a mesh's self-intersections, either locally or by rebuilding it.
triwarp could detect a self-intersection
(triwarp.validation.face_self_intersecting_mask)
and not repair one. This is the repair, in the two forms that exist:
"local"cuts the trouble out and rebuilds it. The intersecting faces are dilated bymax_expandrings, that region is deleted, and the rims it opens are refilled by the minimum-weight patch -- so the surface away from the intersection is untouched. Iterated, because a patch can intersect something itself."voxel"rebuilds the whole surface as the zero level set of its own signed distance field. A level set cannot self-intersect, so this always terminates and resamples everything -- including the parts that were fine. Read the qualification in the Notes: the level set is clean, its triangulation can still carry an artifact at an ambiguous marching-cubes cell.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
method
|
Literal['local', 'voxel']
|
|
'local'
|
max_expand
|
int
|
Rings of faces added around each intersecting face before deleting, in the |
1
|
max_iter
|
int
|
Cap on cut-and-refill passes. The loop also stops as soon as nothing intersects. Raising it does not help a deep interpenetration and inflates the mesh; see the Notes. |
3
|
voxel_size
|
float | None
|
Lattice spacing for the |
None
|
Returns:
| Type | Description |
|---|---|
tuple[array[vec3], array[int32]]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
Examples:
clean_v, clean_f = tw.repair.fix_self_intersections(v, f)
Notes
Success is not guaranteed by either method, and neither is asserted. For "local": a
region whose rim cannot be triangulated without crossing something, or one that grows to swallow
the mesh, leaves intersections behind; the loop stops at max_iter and returns what it has.
For "voxel": the level set is clean, but Warp's MarchingCubes can emit a touching or
non-manifold pair at an ambiguous cell, and that is resolution-dependent: a finer lattice can
introduce a handful of such faces where a coarser one has none. So check with
triwarp.validation.is_self_intersecting when it
matters. Stating this is better than a loop that cannot terminate, and better than a promise the
extraction does not keep.
What the "local" method is for. It clears a shallow self-intersection outright -- a
torus whose tube passes through itself can be fully cleared at either dilation budget -- and
only reduces a deep one, such as two icospheres overlapping by a third of their diameter. That
is the method's shape rather than a tuning failure. Cutting out a lens-shaped overlap leaves a
rim whose minimum-weight patch runs back through the other shell, so the pass converges only
where the damage is a band. Reach for "voxel" when two closed pieces genuinely
interpenetrate: a level set has no notion of two shells.
On that input class the result is nondeterministic and max_iter is not a quality knob,
which "only reduces" does not by itself tell you. Past a certain point, raising it does not
reduce the residual intersections further while the face count climbs past the input's -- each
pass is refilling a rim that the next one cuts out again. Repeated runs on the same input can
also produce a slightly different result, from the refill chain's own atomic-ordering
nondeterminism (as in
triwarp.remesh.isotropic_remesh), so that variation is not
a regression. Do not raise max_iter hoping for convergence on a deep interpenetration; the
answer is "voxel".
The two methods differ in what they preserve, not in quality. "local" keeps the input's
triangulation everywhere it did not cut, so a per-vertex attribute survives outside the patch;
"voxel" keeps nothing but the shape, and its accuracy is the lattice's.
See Also
triwarp.validation.face_self_intersecting_mask
The detector, and what to check the result with.
triwarp.holes.refill_region
The cut-and-refill step each "local" pass runs.
triwarp.levelset.offset_mesh
The same level-set machinery at a non-zero distance.
Source code in triwarp/repair.py
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 | |
remove_tunnels
remove_tunnels(
vertices: array[vec3],
faces: array[int32],
max_length: float,
*,
metric: str = "plane_normalized",
max_iter: int = 100,
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32], int]
Remove thin handles by cutting along their short tunnel loops and sealing the two rims.
A handle is a genus the surface did not need: a scanning artifact where two sheets fused, or a
reconstruction that bridged across a gap. Its signature is a short non-contractible loop --
short being the whole test, since every genus of the intended shape has loops the size of the
shape. So: take a homology basis, shorten each loop within its class
(shorten_loop), keep the ones that come in under
max_length, cut along those and fill the boundary loops the cut opens. Cutting a surface
along a non-separating cycle and sealing the two rims it creates drops the genus by exactly one,
so 2 * removed is the rise in
euler_characteristic -- verified rather than
reported, and the invariant to assert if you extend this.
Nothing is removed when no loop is short enough, and the input is returned unchanged -- so this
is safe to run on a mesh whose genus is intended, provided max_length is below the scale of
its real handles.
One disjoint pass per call
The loops kept are pairwise vertex-disjoint, shortest first. Cutting along two loops
that cross is not the same operation as cutting along each in turn -- the shared vertex is
split by both cuts at once -- and without the restriction the genus can stop dropping one
per loop, or the surface can shatter into extra pieces. The cost is that one call removes at
most one tunnel per disjoint family, so a mesh whose basis loops all overlap needs to be run
again. Call it in a loop until removed is 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
max_length
|
float
|
Loops at or under this length are removed. It is an absolute length in the mesh's own units, so scale it off something intrinsic -- the mean edge length times the number of triangles a real handle would take to go round. |
required |
metric
|
str
|
Triangulation metric for the two rims, as
|
'plane_normalized'
|
max_iter
|
int
|
Sweep cap handed to |
100
|
Returns:
| Name | Type | Description |
|---|---|---|
vertices |
array[vec3]
|
Positions of the result. Longer than the input's wherever the cut split a vertex; the existing positions are unchanged and no new position is invented, since the rims are filled over their own vertices. |
faces |
array[int32]
|
Flat |
removed |
int
|
How many loops were cut. Zero means nothing was short enough, and the buffers are the input's. Returned unconditionally, unlike the diagnostic counts on
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
See Also
shorten_loop
Makes the length test meaningful, and is where the loops come from.
homology_generators
fix_self_intersections
The other topological repair here: that one removes crossings, this one removes genus.
Source code in triwarp/repair.py
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 | |
flip_t_vertices
flip_t_vertices(
vertices: array[vec3],
faces: array[int32],
*,
threshold: float = 40.0,
max_iter: int = 10,
) -> wp.array[wp.int32]
Repair T-vertices by flipping the long edge of each sliver they create.
A T-vertex is a vertex that sits in the interior of a neighbouring triangle's edge rather than at one of its corners — the classic symptom of two patches stitched at different resolutions. The vertex is topologically fine, but the triangle opposite it is a sliver: its apex lies (nearly) on the far edge, which sends its circumradius-to-inradius ratio to infinity and makes every cotangent weight, normal and curvature estimate around it unusable.
The repair is a flip, not a deletion: flipping the sliver's long edge moves the diagonal off the T and leaves two well-shaped triangles, with the same vertices and the same face count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
threshold
|
float
|
Aspect ratio above which a triangle counts as a T-vertex sliver, in the
|
40.0
|
max_iter
|
int
|
Maximum number of parallel flip passes. Each pass commits a conflict-free independent set of
flips; MeshLab's |
10
|
Returns:
| Type | Description |
|---|---|
array[int32]
|
Flat face buffer with the slivers re-triangulated, on |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Notes
A flip cannot fix a T-vertex on the mesh boundary or on a non-manifold edge, because there
is no second triangle to flip against. MeshLab offers an edge collapse method for that case;
here the equivalent is collapse_small_triangles,
which removes the sliver by merging its short edge instead.
Source code in triwarp/repair.py
2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 | |