grouping
triwarp.grouping
Index grouping by equal value/row, plus value and row deduplication for Warp arrays.
Scalar
module-attribute
Scalar = TypeVar('Scalar', bound=wp.Scalar)
group
group(values: array[Int], length: int) -> twt.Array2dInt32
Return index groups of exactly length entries that share the same value.
values are radix-sorted with their original indices; each output row lists
length indices whose corresponding entries are equal and form a run of
precisely that size (runs shorter or longer than length are omitted). This
matches trimesh.grouping.group with min_len == max_len == length and
trimesh.grouping.group_rows with require_count=length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
array[Int]
|
|
required |
length
|
int
|
Required run length; each returned group contains exactly this many indices. |
required |
Returns:
| Type | Description |
|---|---|
Array2dInt32
|
|
See Also
Source code in triwarp/grouping.py
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 | |
group_int_rows
group_int_rows(
data: Array2dInt,
length: int,
max_value: int | None = None,
*,
validate: bool = True,
) -> twt.Array2dInt32
Return index groups of exactly length rows that are identical.
Each row is hashed with hash_indices_rows, then
group finds runs of length equal keys in sorted order. For
example, [[1, 2], [3, 4], [1, 2]] with length=2 yields one group [[0, 2]] (same as
trimesh.grouping.group_rows with require_count=2).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Array2dInt
|
|
required |
length
|
int
|
Required number of duplicate rows per group. |
required |
max_value
|
int | None
|
Optional exclusive upper bound on entries and radix for row hashing; passed
through to |
None
|
validate
|
bool
|
Forwarded to |
True
|
Returns:
| Type | Description |
|---|---|
Array2dInt32
|
|
See Also
Source code in triwarp/grouping.py
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 | |
unique_1d
unique_1d(
data: array[Scalar],
*,
return_inverse: Literal[False] = False,
return_counts: Literal[False] = False,
) -> wp.array[Scalar]
unique_1d(
data: array[Scalar],
*,
return_inverse: Literal[True],
return_counts: Literal[False] = False,
) -> tuple[wp.array[Scalar], wp.array[wp.int32]]
unique_1d(
data: array[Scalar],
*,
return_inverse: Literal[False] = False,
return_counts: Literal[True],
) -> tuple[wp.array[Scalar], wp.array[wp.int32]]
unique_1d(
data: array[Scalar],
*,
return_inverse: Literal[True],
return_counts: Literal[True],
) -> tuple[
wp.array[Scalar], wp.array[wp.int32], wp.array[wp.int32]
]
unique_1d(
data: array[Scalar],
*,
return_inverse: bool = False,
return_counts: bool = False,
) -> (
wp.array[Scalar]
| tuple[wp.array[Scalar], wp.array[wp.int32]]
| tuple[
wp.array[Scalar],
wp.array[wp.int32],
wp.array[wp.int32],
]
)
Find sorted unique elements of a 1D Warp array (numpy.unique subset).
Uses an open-addressing hash table: O(n) inserts plus an O(n_unique log n_unique) sort of only the unique values.
return_inverse maps each input position to the index of its value in the sorted unique
output.
return_index is not supported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
array[Scalar]
|
Rank-1 |
required |
return_inverse
|
bool
|
If |
False
|
return_counts
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
unique |
array
|
Sorted unique values (same |
inverse |
(array[int32], optional)
|
Present if |
counts |
(array[int32], optional)
|
Present if |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in triwarp/grouping.py
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 | |
unique_rows
unique_rows(
data: array[vec3],
*,
return_inverse: Literal[False] = False,
return_counts: Literal[False] = False,
) -> wp.array[wp.vec3]
unique_rows(
data: array[vec3],
*,
return_inverse: Literal[True],
return_counts: Literal[False] = False,
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
unique_rows(
data: array[vec3],
*,
return_inverse: Literal[False] = False,
return_counts: Literal[True],
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
unique_rows(
data: array[vec3],
*,
return_inverse: Literal[True],
return_counts: Literal[True],
) -> tuple[
wp.array[wp.vec3],
wp.array[wp.int32],
wp.array[wp.int32],
]
unique_rows(
data: Array2dInt32,
*,
return_inverse: Literal[False] = False,
return_counts: Literal[False] = False,
) -> twt.Array2dInt32
unique_rows(
data: Array2dInt32,
*,
return_inverse: Literal[True],
return_counts: Literal[False] = False,
) -> tuple[twt.Array2dInt32, wp.array[wp.int32]]
unique_rows(
data: Array2dInt32,
*,
return_inverse: Literal[False] = False,
return_counts: Literal[True],
) -> tuple[twt.Array2dInt32, wp.array[wp.int32]]
unique_rows(
data: Array2dInt32,
*,
return_inverse: Literal[True],
return_counts: Literal[True],
) -> tuple[
twt.Array2dInt32, wp.array[wp.int32], wp.array[wp.int32]
]
unique_rows(
data: Array2dFloat32,
*,
return_inverse: Literal[False] = False,
return_counts: Literal[False] = False,
) -> twt.Array2dFloat32
unique_rows(
data: Array2dFloat32,
*,
return_inverse: Literal[True],
return_counts: Literal[False] = False,
) -> tuple[twt.Array2dFloat32, wp.array[wp.int32]]
unique_rows(
data: Array2dFloat32,
*,
return_inverse: Literal[False] = False,
return_counts: Literal[True],
) -> tuple[twt.Array2dFloat32, wp.array[wp.int32]]
unique_rows(
data: Array2dFloat32,
*,
return_inverse: Literal[True],
return_counts: Literal[True],
) -> tuple[
twt.Array2dFloat32,
wp.array[wp.int32],
wp.array[wp.int32],
]
unique_rows(
data: array,
*,
return_inverse: bool = False,
return_counts: bool = False,
) -> (
wp.array
| tuple[wp.array, wp.array[wp.int32]]
| tuple[
wp.array, wp.array[wp.int32], wp.array[wp.int32]
]
)
Find unique rows of a 2D Warp array (numpy.unique along axis 0).
Each row is hashed with hash_rows, then deduplicated via the
same open-addressing hash table as unique_1d. Unique rows are
returned in
sorted hash-key order; the representative row for each key is the first input
row in that equivalence class.
return_inverse maps each input row index to its slot in the sorted unique
output. return_index is not supported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
array
|
|
required |
return_inverse
|
bool
|
If |
False
|
return_counts
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
unique
|
|
|
inverse |
(array[int32], optional)
|
Present if |
counts |
(array[int32], optional)
|
Present if |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in triwarp/grouping.py
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 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 | |
unique_faces
unique_faces(
faces: array[int32],
*,
return_inverse: Literal[False] = False,
) -> wp.array[wp.int32]
unique_faces(
faces: array[int32], *, return_inverse: Literal[True]
) -> tuple[wp.array[wp.int32], wp.array[wp.int32]]
unique_faces(
faces: array[int32], *, return_inverse: bool = False
) -> (
wp.array[wp.int32]
| tuple[wp.array[wp.int32], wp.array[wp.int32]]
)
Find unique triangular faces up to vertex permutation (orientation-agnostic).
Two faces are equal when they share the same three vertices regardless of order. Each face's indices are sorted before deduplication; the representative returned for a class is the first occurring face, with its original vertex order preserved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
faces
|
array[int32]
|
Flat |
required |
return_inverse
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
unique_faces |
array[int32]
|
Flat buffer of the unique faces (length |
inverse |
(array[int32], optional)
|
Present if |
Source code in triwarp/grouping.py
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 | |
first_occurrence_indices
first_occurrence_indices(
inverse: array[int32], n_unique: int | None = None
) -> wp.array[wp.int32]
Index of the first element of each equivalence class in an inverse map.
The representative-picking half of every deduplication in this package: given the
inverse that unique_1d / unique_rows return, produce for each class the smallest
input index belonging to it. Gathering any per-element payload by the result yields one
representative per class, taken from its first occurrence -- which is what makes
unique_faces keep the original winding and edges_unique keep the first-seen edge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inverse
|
array[int32]
|
Length- |
required |
n_unique
|
int | None
|
Number of classes (the output length). Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
array[int32]
|
Length- |
See Also
unique_rows
unique_1d
gather
The companion step: gather the payload by these indices to get the representatives.
Source code in triwarp/grouping.py
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 | |
hash_rows
hash_rows(data: array[vec3]) -> wp.array[wp.uint64]
hash_rows(data: Array2dInt32) -> wp.array[wp.uint64]
hash_rows(data: Array2dFloat32) -> wp.array[wp.uint64]
hash_rows(
data: array[vec3] | Array2dInt32 | Array2dFloat32,
) -> wp.array[wp.uint64]
Pack each row of a 2D or wp.vec3 array into a single uint64 key.
Dispatches to hash_vector_rows for wp.vec3
(including float32 arrays with width 3) or
hash_indices_rows for int32 rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
array[vec3] | Array2dInt32 | Array2dFloat32
|
|
required |
Returns:
| Type | Description |
|---|---|
array[uint64]
|
Length- |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in triwarp/grouping.py
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 | |
hash_vector_rows
hash_vector_rows(
data: array[vec3], epsilon: float = 0.0
) -> wp.array[wp.uint64]
Pack each wp.vec3 row into a single uint64 key.
Two schemes, selected by epsilon:
epsilon > 0.0— an absolute tolerance. Each coordinate is snapped to a multiple ofepsilonmeasured from the data's own minimum corner, and the resulting integer row is packed viahash_indices_rows. Equal keys mean the rows landed in the same grid cell. Snapping relative to the minimum corner rather than to the coordinate origin is what lets negative coordinates work at all, and it keeps the scaled values at the size of the data's extent, wherefloat32still resolvesepsilon.epsilon == 0.0— a relative bucket. Each coordinate'sfloat32bits are right-shifted by 11, so a key names an interval roughly2 ** -12(about2.4e-4) wide relative to the coordinate's own magnitude. This is not an equality test in either direction, and callers that need one should pass an explicitepsilon: see Notes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
array[vec3]
|
|
required |
epsilon
|
float
|
Uniqueness tolerance. |
0.0
|
Returns:
| Type | Description |
|---|---|
array[uint64]
|
Length- |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Notes
Both schemes quantize, so both split a pair that straddles a cell boundary no matter how close
the two values are. For epsilon > 0 the packing is additionally injective only while
radix ** 3 fits a uint64, where radix is the widest per-axis extent in cells; beyond
that the row keys wrap and behave as a hash with a small collision probability rather than an
exact cell identity. The relative scheme additionally:
- collides distinct coordinates that share a bucket —
1.0and1.000244produce the same key, so a mesh whose vertex spacing is below2.4e-4relative is merged too aggressively; - has unbounded resolution approaching zero, so
+1e-6and-1e-6are thousands of buckets apart (correct — they are distinct points), but so are+1e-40and-1e-40, which for most purposes are the same point. Only+0.0and-0.0are folded together, because IEEE-754 defines them as equal.
Neither is a defect of the packing so much as the nature of a fixed-width key; pass an explicit
epsilon when the tolerance has to be one you chose.
See Also
Source code in triwarp/grouping.py
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 | |
hash_indices_rows
hash_indices_rows(
data: Array2dInt32,
max_index: int | None = None,
*,
validate: bool = True,
) -> wp.array[wp.uint64]
Pack each row of non-negative int32 values into a single uint64 key.
Each row is treated as digits in base max_index (or max(data)+1 when
max_index is omitted) and combined into one integer key per row. A global
min/max over data (via triwarp.reduce.minmax) validates the range and
supplies the radix when it is inferred.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Array2dInt32
|
|
required |
max_index
|
int | None
|
Optional exclusive upper bound on entries and radix for packing; must be
positive when provided. If |
None
|
validate
|
bool
|
When A caller that derived its |
True
|
Returns:
| Type | Description |
|---|---|
array[uint64]
|
Length- |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Warnings
validate=False with a max_index smaller than the true maximum silently produces
colliding keys, and therefore wrong groupings, rather than raising. Only use it where the bound
is structurally guaranteed.
The mixed-radix key is injective only while max_index ** w fits a uint64; past that the
positional sum wraps and equal keys no longer imply equal rows. Two 32-bit columns always fit,
but three columns need max_index <= 2 ** (64 / 3), about 2.6e6. Beyond that the keys
degrade from an exact row identity into a hash whose collision probability grows with the square
of the row count -- around 2e-5 for 28M rows at a radix of 1.4e7. This is deliberately
not validated: raising would reject meshes that group correctly in practice.
See Also
Source code in triwarp/grouping.py
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 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 | |
sorted_undirected_edge_keys
sorted_undirected_edge_keys(
edges: Array2dInt32, n_vertices: int
) -> wp.array[wp.uint64]
Sorted packed keys of an undirected edge set, for binary-search membership testing.
Each row packs to the same key regardless of which endpoint comes first, so a halfedge's own
(origin, destination) key -- built the same way from either order -- can be tested for
membership against the result with a binary search, without caring which order a caller's row
was given in.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edges
|
Array2dInt32
|
|
required |
n_vertices
|
int
|
Total vertex count, used as the packing radix. |
required |
Returns:
| Type | Description |
|---|---|
array[uint64]
|
Length- |
Source code in triwarp/grouping.py
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 | |