bounds
triwarp.bounds
Bounding boxes: building them, and selecting or cropping with them.
Two halves. The constructors -- aabb,
aabb_union, enclosing_diagonal
and oriented_bounding_box -- reduce a cloud to a box. The
queries -- points_in_aabb,
points_in_obb and the
crop_points / crop_mesh pair -- run
the other way, testing points against a box someone already has. The two halves compose directly:
every constructor's return is a query's argument, in that layout, which is why an oriented box is a
(rotation, min_bound, max_bound) triple here rather than an object.
enclosing_diagonal is the one entry point here that
exists for another module's sake rather than its own: it is the default search radius every
mesh query in the package derives from its input's extent.
ITEMS_PER_CANDIDATE_SLICE
module-attribute
ITEMS_PER_CANDIDATE_SLICE = 256
CONVEX_PREFILTER_MIN_POINTS
module-attribute
CONVEX_PREFILTER_MIN_POINTS = 100000
aabb
aabb(points: array[vec3]) -> tuple[wp.vec3, wp.vec3]
Axis-aligned bounding box of points (component-wise min / max).
The reduction is minmax's wp.vec3 path: one chunked kernel
writes both corners into a single six-element buffer, read back once. This wrapper contributes
only the empty-input (+inf, -inf) convention, where minmax raises.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
array[vec3]
|
|
required |
Returns:
| Type | Description |
|---|---|
tuple[vec3, vec3]
|
|
See Also
aabb_union
enclosing_diagonal
points_in_aabb
The other direction: which points a box already in hand contains.
triwarp.reduce.minmax
Source code in triwarp/bounds.py
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 | |
aabb_union
aabb_union(
a_min: vec3, a_max: vec3, b_min: vec3, b_max: vec3
) -> tuple[wp.vec3, wp.vec3]
Smallest axis-aligned bounding box enclosing two given boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a_min
|
vec3
|
Min/max corners of the first box. |
required |
a_max
|
vec3
|
Min/max corners of the first box. |
required |
b_min
|
vec3
|
Min/max corners of the second box. |
required |
b_max
|
vec3
|
Min/max corners of the second box. |
required |
Returns:
| Type | Description |
|---|---|
tuple[vec3, vec3]
|
|
See Also
Source code in triwarp/bounds.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | |
enclosing_diagonal
enclosing_diagonal(
points: array[vec3], other: array[vec3] | None = None
) -> float
Diagonal of the axis-aligned box enclosing one or two point sets.
The default search radius for every mesh query in the package: no closest point, ray hit or
tangent sphere can be further away than the diagonal of a box containing both the surface and
the query points, so it is the smallest bound that is guaranteed not to cut an answer off. Pass
other whenever the queries may lie outside the mesh's own box, which is the usual case --
with points alone a query far outside would get a radius too short to reach the surface.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
array[vec3]
|
|
required |
other
|
array[vec3] | None
|
Optional second |
None
|
Returns:
| Type | Description |
|---|---|
float
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
See Also
Source code in triwarp/bounds.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 | |
points_in_aabb
points_in_aabb(
points: array[vec3], min_bound: vec3, max_bound: vec3
) -> wp.array[wp.int32]
Return the indices of the points inside an axis-aligned box, boundary included.
The bounded selection primitive, and the one that needs no spatial index at all: every point is
tested independently against six planes, so the cost is one pass over the cloud whatever the
box holds. A tree would only help if the box were the thing being searched for -- see
triwarp.neighbors.query_bvh_box for that direction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
array[vec3]
|
|
required |
min_bound
|
vec3
|
Opposite corners of the box, as |
required |
max_bound
|
vec3
|
Opposite corners of the box, as |
required |
Returns:
| Type | Description |
|---|---|
array[int32]
|
Ascending indices into |
Examples:
inside = tw.bounds.points_in_aabb(pts, wp.vec3(-1.0, -1.0, -1.0), wp.vec3(1.0, 1.0, 1.0))
Notes
Containment is inclusive: a point exactly on a face, edge or corner of the box is selected.
A point with a nan coordinate is not selected, and neither is one at an infinity. Both
conventions differ from half_space_mask, whose test is
strict.
See Also
points_in_aabb_mask
The same selection as a boolean mask, for a caller that wants to combine it.
points_in_obb
The oriented counterpart.
crop_points
This plus the gather, when the points themselves are wanted.
aabb
Source code in triwarp/bounds.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 | |
points_in_aabb_mask
points_in_aabb_mask(
points: array[vec3], min_bound: vec3, max_bound: vec3
) -> wp.array[wp.bool]
Flag the points inside an axis-aligned box, boundary included.
The mask form of points_in_aabb, for a caller that wants to
intersect the selection with another predicate before compacting it -- combining masks costs
one wp.map where combining index lists costs a set operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
array[vec3]
|
|
required |
min_bound
|
vec3
|
Opposite corners of the box, as |
required |
max_bound
|
vec3
|
Opposite corners of the box, as |
required |
Returns:
| Type | Description |
|---|---|
array[bool]
|
Length- |
See Also
points_in_aabb
The index form, and where the boundary and nan conventions are documented.
points_in_obb_mask
triwarp.array.flatnonzero
Source code in triwarp/bounds.py
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 | |
points_in_obb
points_in_obb(
points: array[vec3],
rotation: mat33,
min_bound: vec3,
max_bound: vec3,
) -> wp.array[wp.int32]
Return the indices of the points inside an oriented box, boundary included.
Takes the box exactly as oriented_bounding_box
returns it, so the two compose without a transpose or a corner rebuild.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
array[vec3]
|
|
required |
rotation
|
mat33
|
World-to-box frame as |
required |
min_bound
|
vec3
|
Extent of the box along its own axes, in box coordinates. |
required |
max_bound
|
vec3
|
Extent of the box along its own axes, in box coordinates. |
required |
Returns:
| Type | Description |
|---|---|
array[int32]
|
Ascending indices into |
Examples:
rotation, lower, upper = tw.bounds.oriented_bounding_box(v, 256)
inside = tw.bounds.points_in_obb(v, rotation, lower, upper)
Notes
One rotated point per thread, then the identical six comparisons
points_in_aabb makes -- the two share one predicate, so the
inclusive boundary and the nan exclusion documented there hold here too, and the two
functions cannot drift apart on either. Passing the identity as rotation reproduces the
axis-aligned answer exactly.
Being a proper rotation, the frame's inverse is its transpose, so nothing here inverts a matrix; a general affine box would, and is not what this takes.
See Also
points_in_obb_mask
points_in_aabb
The axis-aligned counterpart, and the shared conventions.
oriented_bounding_box
Where the (rotation, min_bound, max_bound) triple comes from.
Source code in triwarp/bounds.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
points_in_obb_mask
points_in_obb_mask(
points: array[vec3],
rotation: mat33,
min_bound: vec3,
max_bound: vec3,
) -> wp.array[wp.bool]
Flag the points inside an oriented box, boundary included.
The mask form of points_in_obb; see
points_in_aabb_mask for why the mask form exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
array[vec3]
|
|
required |
rotation
|
mat33
|
World-to-box frame as |
required |
min_bound
|
vec3
|
Extent of the box along its own axes, in box coordinates. |
required |
max_bound
|
vec3
|
Extent of the box along its own axes, in box coordinates. |
required |
Returns:
| Type | Description |
|---|---|
array[bool]
|
Length- |
See Also
points_in_obb
The index form, and where the conventions are documented.
points_in_aabb_mask
Source code in triwarp/bounds.py
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 | |
crop_points
crop_points(
points: array[vec3],
min_bound: vec3,
max_bound: vec3,
*,
rotation: mat33 | None = None,
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
Keep the points inside a box, returning the survivors and where they came from.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
array[vec3]
|
|
required |
min_bound
|
vec3
|
Opposite corners of the box: in world coordinates when |
required |
max_bound
|
vec3
|
Opposite corners of the box: in world coordinates when |
required |
rotation
|
mat33 | None
|
World-to-box frame as |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
kept |
array[vec3]
|
The points inside the box, in ascending index order, on |
indices |
array[int32]
|
Their indices in |
Examples:
kept, indices = tw.bounds.crop_points(pts, wp.vec3(-1.0, -1.0, -1.0), wp.vec3(1.0, 1.0, 1.0))
See Also
crop_mesh
The mesh counterpart, which must also decide what to do with a straddling face.
points_in_aabb
The selection alone, when the points are not needed.
triwarp.array.gather
Source code in triwarp/bounds.py
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | |
crop_mesh
crop_mesh(
vertices: array[vec3],
faces: array[int32],
min_bound: vec3,
max_bound: vec3,
*,
rotation: mat33 | None = None,
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
Keep the faces whose three vertices all lie inside a box, reindexed from zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
min_bound
|
vec3
|
Opposite corners of the box: in world coordinates when |
required |
max_bound
|
vec3
|
Opposite corners of the box: in world coordinates when |
required |
rotation
|
mat33 | None
|
World-to-box frame as |
None
|
Returns:
| Type | Description |
|---|---|
tuple[array[vec3], array[int32]]
|
Compact |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Examples:
rotation, lower, upper = tw.bounds.oriented_bounding_box(v, 256)
sub_v, sub_f = tw.bounds.crop_mesh(v, f, lower, upper, rotation=rotation)
Notes
A face straddling the box boundary is dropped, not clipped: the result is a subset of the input's triangles, so the crop never introduces a vertex the input did not have and the cut edge is ragged at the triangle scale. That is what makes the operation a selection rather than a boolean -- for a flush cut, intersect the mesh with the box as a solid instead.
To keep the straddling faces instead, take the mask and pass it on directly, which is the
face_mode="any" rule:
mask = tw.bounds.points_in_aabb_mask(v, wp.vec3(-1.0, -1.0, 0.0), wp.vec3(1.0, 1.0, 3.0))
sub_v, sub_f = tw.selection.submesh_from_vertex_mask(v, f, mask, face_mode="any")
See Also
crop_points
triwarp.selection.submesh_from_vertex_mask
The face selection this delegates to, where face_mode is exposed.
Source code in triwarp/bounds.py
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 | |
oriented_bounding_box
oriented_bounding_box(
points: array[vec3],
rotations: int = 4096,
objective: Literal[
"volume", "surface_area", "diagonal"
] = "volume",
refine_iterations: int = 8,
) -> tuple[wp.mat33, wp.vec3, wp.vec3]
Smallest bounding box over sampled orientations, sharpened by local refinement, and its frame.
The box is searched, not solved, in two phases. The global phase scores rotations
candidate orientations from the Super-Fibonacci spiral [Alexa 2022], a low-discrepancy
sampling of SO(3), with the identity appended as the last candidate, scored in parallel.
The refinement phase then takes the best-scoring candidates from up to four mutually distant
basins and runs refine_iterations trust-region rounds around each: every round scores a
low-discrepancy ball of perturbed frames whose angular radius starts at the global grid's
covering radius and shrinks by a factor of 0.4 per round, keeping each chain's best. Refinement
is monotone (each
chain re-scores its own base), so the answer is never worse than the global phase's and never
worse than aabb; with refine_iterations=0 and rotations=1 it
reproduces the axis-aligned box exactly.
Cost is rotations * len(points) point transforms for the global phase plus
refine_iterations * 512 * len(points) for refinement — but past
CONVEX_PREFILTER_MIN_POINTS a
convex_superset_mask prefilter first drops every
point that provably cannot touch the box, so both phases run on the few hull-candidate
survivors and the cost stops growing with the cloud (the box is identical: the mask keeps
every hull vertex and the extents are order-independent reductions over them).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
array[vec3]
|
|
required |
rotations
|
int
|
Number of global candidates to score, |
4096
|
objective
|
Literal['volume', 'surface_area', 'diagonal']
|
Which box quantity to minimize: |
'volume'
|
refine_iterations
|
int
|
Trust-region rounds after the global phase, |
8
|
Returns:
| Name | Type | Description |
|---|---|---|
rotation |
mat33
|
World-to-box frame, i.e. its rows are the box axes in world coordinates. A world point
|
(min_bound, max_bound)
|
Extent of |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Notes
Host traffic: the (rotations, 6) extent table (its objective and argmin are
O(rotations) host arithmetic over a buffer far too small to be worth a device pass), the
36 bytes of the winning frame, and one (512, 6) table per refinement round. The
refinement frames are generated and composed on the device, so the chains live on
the device and the extent tables are the only per-round traffic.
The result is a converged local minimum, not a certified global one. Certifying the true
minimum-volume box requires the exact-arithmetic search over the convex hull's face and edge
events (O'Rourke's rotating-calipers family), and triwarp deliberately has no exact convex hull.
What the sampling-plus-refinement search gives up is the certificate, not the volume: the
global phase lands in the optimum's basin whenever the grid's covering radius resolves it,
refinement converges within the basin, and the refined default ties or beats hull-based
reference methods on volume across a range of test shapes (tilted/stretched icosahedron, cube
shell, hemisphere, half torus).
A pathological cloud whose optimum basin is narrower than the covering radius of rotations
samples can still hide its box from this search; raise rotations if the input is a
near-symmetric polyhedron far from any sampled orientation.
Neither hull-based reference is an oracle for the minimum either: on a stretched icosahedron the refined search returns less volume than both, because the hull-face-flush restriction misses optima whose box touches only edges and vertices.
See Also
aabb
points_in_obb
Takes this triple as returned, to select the points a box contains.
trimesh.bounds.oriented_bounds
igl.oriented_bounding_box
Source code in triwarp/bounds.py
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 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 | |