voxels
triwarp.voxels
Sparse voxel grids: voxelize a mesh or a cloud, test membership, reshape, and mesh the result.
The grid is a warp.Volume -- a NanoVDB index grid -- rather than a triwarp value type, and
every function here either produces one or consumes one. Four properties of that grid shape the
rest of the module:
- The builder is the dedup.
Volume.allocate_by_voxelscollapses repeated cells as it builds, so no function here ever callstriwarp.grouping.unique_rowsfirst. - The volume's linear index is the row index of its cell array.
wp.volume_lookup_indexreturns exactly the rowcellsputs that voxel in, so a per-voxel payload is a plainwp.arrayof lengthn_voxelsand membership costs oneO(1)probe. - NanoVDB centres voxels on integers, so the volume's translation is
origin + 0.5 * swhereoriginis the world position of the lower corner of cell(0, 0, 0). Under that shift cellccovers world[origin + c * s, origin + (c + 1) * s)exactly, which is Open3D's cell and the anchortriwarp.remesh.cluster_decimatealso uses.grid_transformundoes the shift so callers never see it. - The cell order is leaf-major, not lexicographic:
get_voxelswalks NanoVDB's 8-cubed leaves and the cells within each. It is deterministic and identical on CPU and CUDA, but it is not a C-orderreshape, socellscarries anorderflag for callers who need the ascending-key ordertriwarp.grouping.unique_rowsproduces.
Because the grid is a warp.Volume, a caller can sample it (wp.volume_sample_f), probe it
(wp.volume_lookup_index) inside their own kernels, save it with grid.save_to_nvdb(path), and
hand it straight to warp.fem's nanogrid geometries. There is deliberately no to_volume pair.
Three functions here are about a dense corner lattice rather than the sparse grid, and share
nothing with it but the (lower, upper) bounds convention
triwarp.levelset.marching_cubes takes:
grid_points produces the sample positions,
splat_onto_grid accumulates a scattered field onto them and
sample_grid_trilinear reads one back. The last two are
transposes of each other and are kept together: they share the world-to-lattice map, and
splitting the pair across modules -- the gather reads like one of
triwarp.interpolation's transfer verbs -- would put that map behind a
private import and let the two halves drift a half-cell apart.
interpolate_from_points carries the
cross-reference from the other side.
Recipes this module does not wrap
- Implicit CSG:
grid_points→triwarp.proximity.signed_distance_on_meshper solid →wp.map(wp.min, ...)for a union →triwarp.levelset.marching_cubes. Wrapping it would put a four-module pipeline behind one name and duplicatemarching_cubes's own arguments; the voxel answer to the same question isunionand its two siblings. - A full dense box:
from_dense(wp.full(shape, True), s, o), which is Open3D'sVoxelGrid.create_dense. Two calls with no gotcha between them, and a wrapper would allocate the identical dense lattice.
The set algebra, morphological closing and opening, and resampling that this note used to list
are wrapped now -- union,
intersection, difference,
closing, opening and
revoxelize.
DType
module-attribute
DType = TypeVar('DType')
voxelize_mesh
voxelize_mesh(
vertices: array[vec3],
faces: array[int32],
voxel_size: float | None = None,
*,
origin: vec3 | None = None,
mode: Literal["surface", "solid"] = "surface",
max_candidates: int = 1 << 28,
) -> wp.Volume
Occupancy grid of a triangle mesh, by an exact triangle-box overlap test per candidate cell.
A cell is occupied when the closed triangle actually meets the closed cell, decided by the
13-axis separating-axis test (three box normals, the triangle plane, nine edge cross-products).
No sampling, no subdivision heuristic, no tolerance: the accept set is the same one Open3D's
CreateFromTriangleMesh produces, and it is what makes mode="solid" sound.
The work is flattened into one thread per (triangle, candidate cell) pair rather than one per triangle, because per-triangle window sizes span orders of magnitude on any real mesh and a thread-per-triangle launch is load-imbalanced by that same factor. Each triangle's window is its exact inclusive AABB span, whose sizes are prefix-summed into the work-item offsets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
voxel_size
|
float | None
|
Cell width. Defaults to |
None
|
origin
|
vec3 | None
|
World position of the lower corner of cell |
None
|
mode
|
Literal['surface', 'solid']
|
|
'surface'
|
max_candidates
|
int
|
Guard on the total number of (triangle, cell) pairs, which grows cubically as
|
1 << 28
|
Returns:
| Type | Description |
|---|---|
Volume
|
A NanoVDB index grid on |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
Notes
Open3D runs the same test in float64 and this one runs in float32, so a triangle exactly
tangent to a cell plane can be resolved differently by the two. Nothing else differs: Open3D's
round((max - min) / voxel_size) + 2 candidate window is a superset of the exact AABB window
used here, and a cell outside a triangle's AABB cannot overlap the triangle.
trimesh's voxelize_subdivide is a different algorithm -- subdivide until every edge is
under half a pitch, then round each vertex to a cell -- and its result neither contains nor is
contained in this one.
Source code in triwarp/voxels.py
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 | |
voxelize_points
voxelize_points(
points: array[vec3],
voxel_size: float | None = None,
*,
origin: vec3 | None = None,
) -> wp.Volume
Occupancy grid of a point cloud: one active voxel per cell that contains at least one point.
Open3D's VoxelGrid.create_from_point_cloud. There is no dedup step to write — the builder
collapses repeated cells itself.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
array[vec3]
|
|
required |
voxel_size
|
float | None
|
Cell width. Defaults to |
None
|
origin
|
vec3 | None
|
World position of the lower corner of cell |
None
|
Returns:
| Type | Description |
|---|---|
Volume
|
A NanoVDB index grid on |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
See Also
Source code in triwarp/voxels.py
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 | |
voxel_down_sample
voxel_down_sample(
points: array[vec3],
voxel_size: float | None = None,
*,
origin: vec3 | None = None,
pooling: Literal["mean", "min", "max", "sum"] = "mean",
return_inverse: bool = False,
) -> (
wp.array[wp.vec3]
| tuple[wp.array[wp.vec3], wp.array[wp.int32]]
)
Reduce a cloud to one point per occupied voxel.
Open3D's PointCloud.voxel_down_sample: the standard uniform decimation for a scan, and the
cheapest way to put two clouds on a common sampling density before registration. The default
"mean" is Open3D's AccumulatedPoint average and does not renormalize, so the same
call pools per-point normals or colours through
pool_by_voxel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
array[vec3]
|
|
required |
voxel_size
|
float | None
|
Cell width. Defaults to |
None
|
origin
|
vec3 | None
|
World position of the lower corner of cell |
None
|
pooling
|
Literal['mean', 'min', 'max', 'sum']
|
How each voxel reduces the points inside it: |
'mean'
|
return_inverse
|
bool
|
Also return, for each input point, the row of the output it was pooled into. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
points |
array[vec3]
|
|
inverse |
(array[int32], optional)
|
Present when |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Notes
"mean" and "sum" are bitwise reproducible run to run: they sort the points by voxel and
reduce each segment in index order rather than accumulating with float atomics, whose ordering
varies between launches. That costs one radix sort over the point count. "min" and "max"
use atomics directly, since those are order-independent for floats.
Source code in triwarp/voxels.py
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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
pool_by_voxel
pool_by_voxel(
grid: Volume,
points: array[vec3],
values: array[vec3],
*,
pooling: Literal["mean", "min", "max", "sum"] = "mean",
) -> wp.array[wp.vec3]
Reduce a per-point wp.vec3 payload onto the voxels of grid.
The general form of voxel_down_sample: positions,
normals, colours or velocities, pooled onto whichever grid the caller already has. It takes the
grid rather than a precomputed inverse map because the grid is the inverse map — one
O(1) probe per point recovers its output row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
Volume
|
Index grid whose voxels are the output rows. |
required |
points
|
array[vec3]
|
|
required |
values
|
array[vec3]
|
|
required |
pooling
|
Literal['mean', 'min', 'max', 'sum']
|
|
'mean'
|
Returns:
| Type | Description |
|---|---|
array[vec3]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If |
RuntimeError
|
If |
See Also
Source code in triwarp/voxels.py
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 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | |
cells
cells(
grid: Volume,
*,
order: Literal["grid", "sorted"] = "grid",
) -> twt.Array2dInt32
Integer cell coordinates of every active voxel, one row per voxel.
The array-facing half of the grid, and the only place its row order is observable: everything
else here returns an opaque warp.Volume.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
Volume
|
Index grid to read. |
required |
order
|
Literal['grid', 'sorted']
|
Row order of the result:
|
'grid'
|
Returns:
| Type | Description |
|---|---|
Array2dInt32
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If |
See Also
Notes
The length comes from Volume.get_active_stats, never from get_voxel_count: on a
rebuildable grid the latter reports the reserved capacity, and the trailing rows of
get_voxels are padding at [0, 0, 0] that would read as real voxels.
Source code in triwarp/voxels.py
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 | |
from_cells
from_cells(
cells: Array2dInt32, voxel_size: float, origin: vec3
) -> wp.Volume
Build an index grid from integer cell coordinates.
The inverse of cells, and the constructor every other producer here
ends in. Repeated rows are collapsed by the builder, so the input need not be unique.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cells
|
Array2dInt32
|
|
required |
voxel_size
|
float
|
Cell width. |
required |
origin
|
vec3
|
World position of the lower corner of cell |
required |
Returns:
| Type | Description |
|---|---|
Volume
|
A NanoVDB index grid on |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If |
See Also
Source code in triwarp/voxels.py
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | |
grid_transform
grid_transform(grid: Volume) -> tuple[float, wp.vec3]
Cell width and world origin of an index grid.
Undoes the half-voxel shift NanoVDB's integer-centred voxels need, so origin here is the
world position of the lower corner of cell (0, 0, 0) — the same anchor
from_cells takes, not the volume's own translation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
Volume
|
Index grid to read. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
voxel_size |
float
|
Cell width. |
origin |
vec3
|
World position of the lower corner of cell |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
See Also
Source code in triwarp/voxels.py
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 | |
resolve_voxel_grid
resolve_voxel_grid(
points: array[vec3],
voxel_size: float | None = None,
origin: vec3 | None = None,
*,
caller: str = "resolve_voxel_grid",
) -> tuple[float, wp.vec3]
Voxel size and grid origin for a point set, filling in either default from its bounding box.
The package's one definition of what an unspecified voxel grid means: a cell of 1 % of the
bounding-box diagonal, anchored half a cell below the box. That anchor is Open3D's, and keeping
it in one place is what lets voxelize_points,
voxel_down_sample and
cluster_decimate agree cell for cell -- they used to say so
in two comments instead.
Public because cluster_decimate lives in another module and
needs the same convention; it is pure host arithmetic over one
aabb.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
array[vec3]
|
|
required |
voxel_size
|
float | None
|
Edge length of a cell. |
None
|
origin
|
vec3 | None
|
Lower corner of cell |
None
|
caller
|
str
|
Name used in the error message, so a caller's own name appears rather than this one. |
'resolve_voxel_grid'
|
Returns:
| Type | Description |
|---|---|
tuple[float, vec3]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Notes
The resolve_* pattern
Both of these -- this and sample.resolve_seed /
voxels.resolve_voxel_grid -- turn an optional
argument into the concrete value the wrapper would have derived, so a caller who wants two
functions to share the derived thing can resolve it once and pass it to both. Each default
is domain knowledge, so they cannot share a module. The optional face-adjacency pair
deliberately has no such resolver: deriving it is one
face_adjacency call with return_edges=True, so
only the pairing rule is worth sharing and
adjacency.require_paired_adjacency is that
rule on its own.
Source code in triwarp/voxels.py
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 | |
cell_indices
cell_indices(
points: array[vec3], voxel_size: float, *, origin: vec3
) -> twt.Array2dInt32
Cell each point falls in, one row per point and no deduplication.
trimesh's points_to_indices and Open3D's VoxelGrid.get_voxel. It takes the transform
explicitly rather than a grid because its job is to be used before a grid exists — it is what
voxelize_points feeds to the builder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
array[vec3]
|
|
required |
voxel_size
|
float
|
Cell width. |
required |
origin
|
vec3
|
World position of the lower corner of cell |
required |
Returns:
| Type | Description |
|---|---|
Array2dInt32
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
See Also
Source code in triwarp/voxels.py
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 | |
cell_centers
cell_centers(grid: Volume) -> wp.array[wp.vec3]
World position of the centre of every active voxel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
Volume
|
Index grid to read. |
required |
Returns:
| Type | Description |
|---|---|
array[vec3]
|
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in triwarp/voxels.py
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 | |
occupancy_at_points
occupancy_at_points(
grid: Volume, points: array[vec3]
) -> wp.array[wp.bool]
Occupancy mask of the voxel each query point falls in.
Open3D's VoxelGrid.check_if_included and trimesh's VoxelGrid.is_filled, which are one
hash lookup per query on one core; here it is one O(1) probe per query in a single kernel,
with no host work.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
Volume
|
Index grid to test against. |
required |
points
|
array[vec3]
|
|
required |
Returns:
| Type | Description |
|---|---|
array[bool]
|
Length- |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
RuntimeError
|
If |
See Also
Source code in triwarp/voxels.py
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 | |
occupancy_at_cells
occupancy_at_cells(
grid: Volume, cells: Array2dInt32
) -> wp.array[wp.bool]
Occupancy mask of a list of integer cells.
The index-space form of occupancy_at_points, and the
primitive the set algebra is built on: intersection keeps the
voxels this mask accepts and difference keeps the rest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
Volume
|
Index grid to test against. |
required |
cells
|
Array2dInt32
|
|
required |
Returns:
| Type | Description |
|---|---|
array[bool]
|
Length- |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If |
RuntimeError
|
If |
See Also
Source code in triwarp/voxels.py
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 | |
splat_onto_grid
splat_onto_grid(
points: array[vec3],
values: array[DType],
shape: tuple[int, int, int],
*,
bounds: tuple[vec3, vec3] | None = None,
min_weight: float = 0.0001,
) -> tuple[wp.array[DType, Literal[3]], twt.Array3dFloat32]
Accumulate a scattered field onto a dense lattice with trilinear weights, and average it.
pytorch3d's add_points_features_to_volume_densities_features, and the scatter half of the
lattice pair: each point contributes to the eight lattice corners around it, weighted by
the trilinear fractions, and the same eight weights accumulate into a density lattice whose
entry is the number of points that corner saw. The field is then divided by that density, so
the result is a weighted mean rather than a sum and is independent of how many points
happened to land in a cell.
This is a corner lattice addressed by bounds, the same convention
grid_points and
triwarp.levelset.marching_cubes take -- not a
wp.Volume and not the voxel-centre addressing the rest of this module uses. That is
deliberate: the field it produces is meant to be marched, sampled or reshaped, and those three
all speak bounds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
array[vec3]
|
|
required |
values
|
array[DType]
|
Length- |
required |
shape
|
tuple[int, int, int]
|
|
required |
bounds
|
tuple[vec3, vec3] | None
|
|
None
|
min_weight
|
float
|
Density floor: the division is by |
0.0001
|
Returns:
| Name | Type | Description |
|---|---|---|
field |
array[DType, Literal[3]]
|
|
density |
Array3dFloat32
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
See Also
sample_grid_trilinear
The inverse: read a dense lattice back at arbitrary positions, with the same eight weights.
grid_points
The positions of the lattice this writes into.
pool_by_voxel
The nearest-corner counterpart over a sparse wp.Volume: one cell per point rather than
eight, and a min / max / sum choice this has no analogue for.
interpolate_from_points
The gather-side alternative when the target is an arbitrary sample set rather than a
lattice, and the source weighting should be Gaussian rather than trilinear.
Notes
Departs from pytorch3d in two ways, both forced. It takes bounds where pytorch3d takes a
normalized [-1, 1] local space plus an align_corners switch, because every dense
lattice in triwarp is addressed by bounds; and it returns the pair where pytorch3d
accumulates into caller-supplied volume_densities / volume_features in place. The
min_weight floor is pytorch3d's, default and all.
This averages, so it is not the inverse of a sample except on the lattice itself. A few properties hold exactly and are worth knowing before reading a round trip as a check:
density.sum()is the point count exactly -- the eight weights per point sum to 1.- Points on the lattice corners give
density == 1everywhere and an exact splat-then-sample round trip, because each point's stencil degenerates to its own corner. - A constant field comes back as that constant everywhere some point was reached. Off the lattice the round trip is a smoothing rather than the identity, and the residual comes from the corners no point reached: a query whose stencil touches one averages a zero in.
Source code in triwarp/voxels.py
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 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 | |
sample_grid_trilinear
sample_grid_trilinear(
field: array[DType, Literal[3]],
points: array[vec3],
*,
bounds: tuple[vec3, vec3] | None = None,
) -> wp.array[DType]
Read a dense lattice at arbitrary positions by trilinear interpolation.
The gather half of the lattice pair, and the exact transpose of
splat_onto_grid: the value at a position is the sum of the
eight surrounding corners weighted by the same trilinear fractions the splat distributes with.
Use it to read a signed-distance field, a density or a reconstructed vector field back at query
points; use interpolate_from_points instead
when the source is a scattered cloud rather than a lattice.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
array[DType, Literal[3]]
|
|
required |
points
|
array[vec3]
|
|
required |
bounds
|
tuple[vec3, vec3] | None
|
|
None
|
Returns:
| Type | Description |
|---|---|
array[DType]
|
Length- |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
See Also
splat_onto_grid
The scatter this transposes: accumulate a scattered field onto the lattice this reads. Its
Notes records exactly which round trips through the pair are exact and which are a
smoothing -- averaging makes the pair adjoint, not inverse.
interpolate_from_points
The same question with a scattered source and a Gaussian kernel.
occupancy_at_points
The nearest-cell boolean over a sparse wp.Volume, where this is a smooth read of a
dense one.
Notes
A position outside the lattice reads the nearest boundary cell's stencil rather than a null
value -- the field is extended by its boundary, which is what a distance or density lattice
wants and is the convention the Poisson sampler in
triwarp.reconstruction already had. Clamp or mask the query set
yourself if an outside position should be an error.
Source code in triwarp/voxels.py
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 | |
grid_points
grid_points(
shape: tuple[int, int, int],
*,
bounds: tuple[vec3, vec3] | None = None,
device: DeviceLike = None,
) -> wp.array[wp.vec3]
Regular lattice of sample positions spanning a box, flattened in C order.
igl.grid, and the producer side of the implicit-surface round trip: it takes the same
(lower, upper) corner-mapping tuple
triwarp.levelset.marching_cubes takes, so a
per-point scalar answer .reshape(shape)s straight back into a field that function accepts.
This is a corner lattice, not a voxel grid: it has nothing to do with the rest of the module
except that both speak the same bounds convention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple[int, int, int]
|
|
required |
bounds
|
tuple[vec3, vec3] | None
|
|
None
|
device
|
DeviceLike
|
Target Warp device. |
None
|
Returns:
| Type | Description |
|---|---|
array[vec3]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
See Also
Notes
igl.voxel_grid produces this same lattice up to its own transform: s cells along the
longest side, pad_count cells beyond the box, then an isotropic rescale re-centred on the
box centre.
Source code in triwarp/voxels.py
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 | |
union
union(a: Volume, b: Volume) -> wp.Volume
Cells occupied in either grid: a | b.
trimesh's ops.boolean_sparse under numpy.logical_or, and the cheapest of the three set
operations here -- the two cell arrays are concatenated and the builder collapses the overlap,
so there is no membership test and no unique pass to write.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Volume
|
Index grid. |
required |
b
|
Volume
|
Index grid on the same lattice and device as |
required |
Returns:
| Type | Description |
|---|---|
Volume
|
A new index grid on |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the two grids differ in cell width or in origin. |
TypeError
|
If either argument is not a NanoVDB index grid with isotropic voxels. |
RuntimeError
|
If |
See Also
Notes
A cell coordinate means nothing without a lattice, so the three set operations require one:
two grids share it only when they were built with the same voxel_size and origin.
resolve_voxel_grid is what a caller resolves once and
passes to both builds; revoxelize moves an existing grid onto
another lattice.
Source code in triwarp/voxels.py
1189 1190 1191 1192 1193 1194 1195 1196 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 | |
intersection
intersection(a: Volume, b: Volume) -> wp.Volume
Cells occupied in both grids: a & b.
trimesh's ops.boolean_sparse under numpy.logical_and. One O(1) probe of b per
voxel of a -- no dense lattice is materialized, so the cost is set by the smaller
argument when it is passed first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Volume
|
Index grid; its voxels are the ones tested, so pass the smaller set here. |
required |
b
|
Volume
|
Index grid on the same lattice and device as |
required |
Returns:
| Type | Description |
|---|---|
Volume
|
A new index grid on |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the two grids differ in cell width or in origin. |
TypeError
|
If either argument is not a NanoVDB index grid with isotropic voxels. |
RuntimeError
|
If |
See Also
Source code in triwarp/voxels.py
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 | |
difference
difference(a: Volume, b: Volume) -> wp.Volume
Cells occupied in a and not in b: a - b.
trimesh's ops.boolean_sparse under numpy.logical_and of a with the complement of
b, and intersection's pass with the membership mask
negated. Asymmetric: difference(a, b) and difference(b, a) are different sets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Volume
|
Index grid to keep voxels from. |
required |
b
|
Volume
|
Index grid on the same lattice and device as |
required |
Returns:
| Type | Description |
|---|---|
Volume
|
A new index grid on |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the two grids differ in cell width or in origin. |
TypeError
|
If either argument is not a NanoVDB index grid with isotropic voxels. |
RuntimeError
|
If |
See Also
union
intersection
surface_voxels
The shell, which is this operation against the grid's own erosion.
Source code in triwarp/voxels.py
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 | |
revoxelize
revoxelize(
grid: Volume,
voxel_size: float,
*,
origin: vec3 | None = None,
max_cells: int = 1 << 28,
) -> wp.Volume
Resample the occupied set onto a lattice of a different cell width.
trimesh's VoxelGrid.revoxelized, which asks the same question the same way: a cell of the
new lattice is occupied when its centre falls in an occupied cell of the old one. That rule
is what makes the operation exact at an unchanged voxel_size and safe when refining, where
voxelizing the old cell centres would instead leave holes between them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
Volume
|
Index grid to resample. |
required |
voxel_size
|
float
|
Cell width of the result. Smaller refines, larger coarsens. |
required |
origin
|
vec3 | None
|
World position of the lower corner of the new cell |
None
|
max_cells
|
int
|
Budget for the sampling lattice, which covers the occupied box and therefore grows as the
cube of |
1 << 28
|
Returns:
| Type | Description |
|---|---|
Volume
|
A new index grid on |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If |
See Also
voxelize_points
occupancy_at_points
union
Set algebra, which needs both grids on one lattice -- this is how one gets there.
Notes
Coarsening by centre sampling drops an old voxel whose cell contains no new centre, so it is
not the same as "any overlap": at 2 * voxel_size a lone voxel survives only if a new centre
lands inside it. Take dilate first when the result must contain the
input.
Only the occupied box is sampled, not the whole box between origin and the set, so a grid
whose voxels sit far from its own origin costs no more than a tight one. That is what the
origin_cell half of from_dense is for.
An even integer coarsening is the one ill-conditioned case: it places every new centre
exactly on a face of the old lattice, at origin + (2c + 1) * old_size, so which of the two
neighbouring cells answers is decided by float32 rounding. Pass an origin displaced by a
fraction of a cell where that matters; odd and fractional factors sample strictly inside an old
cell and are exact.
Source code in triwarp/voxels.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 | |
fill_cavities
fill_cavities(
grid: Volume, *, max_cells: int = 1 << 28
) -> wp.Volume
Fill every enclosed cavity: an empty cell is kept empty only if it reaches the outside.
trimesh's morphology.fill_holes (scipy.ndimage.binary_fill_holes) and the default of
VoxelGrid.fill. The one operation here the sparse grid cannot answer by itself, because it
is a statement about the empty complement, so this densifies over the occupied bounding box
plus one cell of padding and labels the empty cells' 6-connected components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
Volume
|
Index grid to fill. |
required |
max_cells
|
int
|
Budget for the dense box this labels over, which spans the occupied cells and is therefore set by how far apart the voxels are rather than by how many there are. Exceeding it raises rather than allocating. Five lattices are built over the box, so this bounds the footprint. |
1 << 28
|
Returns:
| Type | Description |
|---|---|
Volume
|
A new index grid containing |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If |
See Also
fill_orthographic
The blunter fill, with the same budget for the same reason.
voxelize_mesh
Notes
The 6-neighbour stencil is implicit: the union-find hooks three backward probes per cell
straight off the dense occupancy, so no edge list is ever built. An explicit one would cost
3 * n_empty rows, which is why the connected-component pass is written against the stencil
rather than against
triwarp.graph.connected_component_labels_from_edges.
Source code in triwarp/voxels.py
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 1471 1472 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 | |
fill_orthographic
fill_orthographic(
grid: Volume, *, max_cells: int = 1 << 28
) -> wp.Volume
Fill the intersection of the three axis-aligned solid shadows of the voxel set.
trimesh's ops.fill_orthographic: along every line parallel to an axis, fill from the first
occupied cell to the last, then keep only the cells all three axes agree on. Cheaper and blunter
than fill_cavities — it fills concavities that are open in
only one or two directions, and it cannot fill a cavity whose enclosing shell is not convex
along all three axes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
Volume
|
Index grid to fill. |
required |
max_cells
|
int
|
Budget for the dense box the three axis sweeps run over, which spans the occupied cells and is therefore set by how far apart the voxels are rather than by how many there are. Exceeding it raises rather than allocating. |
1 << 28
|
Returns:
| Type | Description |
|---|---|
Volume
|
A new index grid containing |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If |
See Also
fill_cavities
The exact fill, with the same budget for the same reason.
Source code in triwarp/voxels.py
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 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 | |
dilate
dilate(
grid: Volume,
*,
connectivity: Literal[6, 18, 26] = 6,
iterations: int = 1,
) -> wp.Volume
Grow the voxel set by one shell of neighbours per iteration.
trimesh's morphology.binary_dilation (scipy.ndimage.binary_dilation). Every voxel writes
its whole neighbourhood as candidate cells and the builder dedups them, so there is no unique
pass — but the candidate buffer is (connectivity + 1) * n_voxels cells, which grows quickly
at connectivity=26. That growth is why 6 is the default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
Volume
|
Index grid to dilate. |
required |
connectivity
|
Literal[6, 18, 26]
|
Neighbourhood: |
6
|
iterations
|
int
|
Number of dilation passes. Each is a separate grid build; a widened stencil is deliberately
not used, since a |
1
|
Returns:
| Type | Description |
|---|---|
Volume
|
A new index grid on |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If |
See Also
Notes
Binary closing is erode(dilate(g)) and opening dilate(erode(g)); both are wrapped, as
closing and opening, because the order
is the whole operation and each name is the other one's mistake.
Source code in triwarp/voxels.py
1592 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 | |
erode
erode(
grid: Volume,
*,
connectivity: Literal[6, 18, 26] = 6,
iterations: int = 1,
) -> wp.Volume
Shrink the voxel set to the voxels whose whole neighbourhood is occupied.
trimesh's morphology.binary_erosion. One thread per voxel and connectivity O(1)
probes, with no allocation and no sort: neighbours usually live in the same 8-cubed NanoVDB
leaf, so the probes are cache-local.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
Volume
|
Index grid to erode. |
required |
connectivity
|
Literal[6, 18, 26]
|
Neighbourhood: |
6
|
iterations
|
int
|
Number of erosion passes. |
1
|
Returns:
| Type | Description |
|---|---|
Volume
|
A new index grid on |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If |
See Also
Notes
Cells outside the grid are empty by definition, so a voxel on the boundary of the occupied
region always erodes away — the same convention scipy.ndimage.binary_erosion takes with
border_value=0.
Source code in triwarp/voxels.py
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 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 | |
closing
closing(
grid: Volume,
*,
connectivity: Literal[6, 18, 26] = 6,
iterations: int = 1,
) -> wp.Volume
Dilation followed by erosion: bridge gaps and cracks thinner than the structuring element.
trimesh's morphology.binary_closing (scipy.ndimage.binary_closing), and exactly
erode(dilate(grid)) at the same settings. It is a name rather than a recipe because the
order is the whole operation and the wrong one is the other function here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
Volume
|
Index grid to close. |
required |
connectivity
|
Literal[6, 18, 26]
|
Neighbourhood of the structuring element: |
6
|
iterations
|
int
|
Cells of reach: the dilation runs |
1
|
Returns:
| Type | Description |
|---|---|
Volume
|
A new index grid on |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If |
See Also
opening
dilate
erode
fill_cavities
The unbounded form: it closes an enclosed void of any size, where this one closes a gap of
at most iterations cells.
Notes
Nothing is clipped here, which is the one way this differs from the dense reference: the
intermediate dilation grows onto whatever cells it needs, where scipy.ndimage's runs inside
the array it was handed and erodes a shell off every face of it unless the caller padded first.
Source code in triwarp/voxels.py
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 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 | |
opening
opening(
grid: Volume,
*,
connectivity: Literal[6, 18, 26] = 6,
iterations: int = 1,
) -> wp.Volume
Erosion followed by dilation: drop specks and necks thinner than the structuring element.
scipy.ndimage.binary_opening, and exactly dilate(erode(grid)) at the same settings --
the dual of closing, and the one that removes rather than adds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
Volume
|
Index grid to open. |
required |
connectivity
|
Literal[6, 18, 26]
|
Neighbourhood of the structuring element: |
6
|
iterations
|
int
|
Cells of reach: a feature that survives |
1
|
Returns:
| Type | Description |
|---|---|
Volume
|
A new index grid on |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If |
Notes
A component narrower than the element vanishes entirely rather than shrinking, which is what makes this the speck filter of a noisy voxelization; the surviving components keep their extent but lose their corners.
Source code in triwarp/voxels.py
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 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 | |
surface_voxels
surface_voxels(
grid: Volume, *, connectivity: Literal[6, 18, 26] = 6
) -> wp.Volume
Keep the occupied voxels that touch an empty one: the boundary shell of the set.
trimesh's VoxelGrid.surface, i.e. grid minus erode(grid),
computed with the same probes and one launch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
Volume
|
Index grid to read. |
required |
connectivity
|
Literal[6, 18, 26]
|
Neighbourhood deciding adjacency: |
6
|
Returns:
| Type | Description |
|---|---|
Volume
|
A new index grid holding only the boundary voxels. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If |
Notes
A voxel on the edge of the grid's occupied region counts as surface, since everything outside the set is empty by definition.
Source code in triwarp/voxels.py
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 | |
to_dense
to_dense(
grid: Volume,
*,
origin_cell: tuple[int, int, int] | None = None,
shape: tuple[int, int, int] | None = None,
) -> tuple[twt.Array3dBool, tuple[int, int, int]]
Occupancy of a cell box as a dense boolean lattice.
trimesh's sparse_to_matrix, plus the cell offset that makes it invertible: unlike trimesh's
grids, cell coordinates here can be negative, so the lattice's own origin has to be returned
alongside it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
Volume
|
Index grid to read. |
required |
origin_cell
|
tuple[int, int, int] | None
|
Cell that becomes |
None
|
shape
|
tuple[int, int, int] | None
|
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
occupancy |
Array3dBool
|
|
origin_cell |
tuple[int, int, int]
|
The cell at |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If |
See Also
Source code in triwarp/voxels.py
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 | |
from_dense
from_dense(
occupancy: Array3dBool,
voxel_size: float,
origin: vec3,
*,
origin_cell: tuple[int, int, int] = (0, 0, 0),
) -> wp.Volume
Build an index grid from a dense boolean occupancy lattice.
trimesh's DenseEncoding, and the inverse of to_dense: pass back
the origin_cell that function returned and the round trip is exact, negative cells included.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
occupancy
|
Array3dBool
|
|
required |
voxel_size
|
float
|
Cell width. |
required |
origin
|
vec3
|
World position of the lower corner of cell |
required |
origin_cell
|
tuple[int, int, int]
|
Cell coordinate of |
(0, 0, 0)
|
Returns:
| Type | Description |
|---|---|
Volume
|
A NanoVDB index grid on |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If |
See Also
Source code in triwarp/voxels.py
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 | |
to_field
to_field(
grid: Volume, *, pad: int = 1
) -> tuple[twt.Array3dFloat32, tuple[wp.vec3, wp.vec3]]
Occupancy as a padded float32 lattice on the cell centres, with the box it spans.
The bridge to triwarp.levelset.marching_cubes:
marching_cubes(*to_field(grid), 0.5) reproduces trimesh's ops.matrix_to_marching_cubes,
VoxelGrid.marching_cubes and ops.points_to_marching_cubes, which are all the same recipe
(pad by one, threshold at 0.5). The padding is what closes the surface: without it a voxel
on the lattice boundary has no zero outside it to cross.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
Volume
|
Index grid to read. |
required |
pad
|
int
|
Empty cells of margin added on every side. |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
field |
Array3dFloat32
|
|
bounds |
tuple[vec3, vec3]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If |
Notes
Because the samples sit on cell centres, the extracted surface runs half a voxel inside the occupied cells' outer faces — the same half-voxel offset trimesh's marching-cubes path has.
Source code in triwarp/voxels.py
1997 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 | |
to_boxes
to_boxes(
grid: Volume, *, cull_internal: bool = True
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
Triangulate the voxel set as a mesh of axis-aligned cubes.
trimesh's ops.multibox / VoxelGrid.as_boxes, and the cube rendering Open3D draws a
VoxelGrid as. Corners are shared rather than duplicated — the deduplicated corner lattice
comes from voxel_corners — so no welding pass runs afterwards.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
Volume
|
Index grid to mesh. |
required |
cull_internal
|
bool
|
Drop the faces between two occupied voxels (the default), leaving only the visible shell.
|
True
|
Returns:
| Name | Type | Description |
|---|---|---|
vertices |
array[vec3]
|
Corner positions on |
faces |
array[int32]
|
Flat |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
See Also
Notes
With cull_internal=True on a solid set the result is closed and manifold; with False it
is not, since interior faces are duplicated back to back.
The corner lattice is shared rather than duplicated, so culling the interior faces leaves the
interior corners unreferenced -- they belong to the lattice but to no surviving face. Those
are compacted away before returning, which is a change worth knowing about if a caller was
relying on an index into the full lattice: it was not, since the lattice itself is
voxel_corners and that is where an index into it comes from.
Source code in triwarp/voxels.py
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 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 | |
voxel_corners
voxel_corners(
grid: Volume,
) -> tuple[twt.Array2dInt32, twt.Array2dInt32]
Build the deduplicated corner lattice of the voxel set, plus each voxel's eight corners.
igl.unique_sparse_voxel_corners. The lattice is not computed here: warp.fem's sparse
nanogrid geometry derives its own vertex grid from the cell grid, and that grid is the
deduplicated corner set — so this is eight O(1) probes per voxel and no 8 * n_voxels
candidate buffer, no unique pass, and no hand-built dual grid.
Corner (i, j, k) is the lower corner of cell (i, j, k), at world position
origin + (i, j, k) * voxel_size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
Volume
|
Index grid to read. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
corners |
Array2dInt32
|
|
cell_corners |
Array2dInt32
|
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Notes
igl numbers a cell's corners in yxz binary-counting order, a fixed permutation of the order
used here.
warp.fem is imported inside this function, not at module scope, since nothing else in this
module needs it.
Source code in triwarp/voxels.py
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 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 | |