sample
triwarp.sample
Generative sampling: new points on a mesh surface, inside its volume, or over a sphere.
Everything here creates points -- uniformly over the faces
(sample_surface), spaced at least radius apart
(sample_surface_poisson_disk,
sample_surface_blue_noise), inside a watertight solid
(sample_volume), or over a sphere, hemisphere or cone as a
low-discrepancy Fibonacci lattice (the sample_fibonacci_* family, which take a count and no
mesh at all).
Subsampling an existing cloud is the other question and lives with the data structures that
answer it: points.farthest_point_sample for an exact
count, and voxels.voxel_down_sample for one representative
per occupied cell. Neither belongs here -- each is a reduction of points a caller already has.
The one function here that reads like a subsampler and is not is
sample_surface_blue_noise: it draws its own dense pool
from the surface and thins that, so its input is a mesh and its output is new points, never a
subset of anything the caller passed.
See Also
points.farthest_point_sample
Subsample an existing cloud to an exact count.
voxels.voxel_down_sample
Subsample an existing cloud to one point per occupied voxel.
sample_fibonacci_sphere
sample_fibonacci_sphere(
count: int, device: DeviceLike = None
) -> wp.array[wp.vec3]
Generate near-uniform unit vectors on the sphere via the Fibonacci spiral.
Successive points are placed at multiples of the golden angle while their
height z descends uniformly through (-1, 1), producing the Fibonacci
lattice — a deterministic, low-discrepancy covering of the sphere that is far
more even than independent random sampling for the same count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Number of directions to generate. |
required |
device
|
DeviceLike
|
Warp device for the result. Defaults to the current device. |
None
|
Returns:
| Type | Description |
|---|---|
array[vec3]
|
|
See Also
Source code in triwarp/sample.py
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 | |
sample_fibonacci_hemisphere
sample_fibonacci_hemisphere(
count: int, device: DeviceLike = None
) -> wp.array[wp.vec3]
Generate near-uniform unit vectors on the positive-z hemisphere.
Same Fibonacci-spiral construction as
sample_fibonacci_sphere but with
z descending uniformly through (0, 1), so every direction has a
positive z component. Because the hemisphere and its reflection tile the
full sphere, pairing each direction n with its antipode -n (for
example via a min/max reduction) covers all orientations with half the
directions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Number of directions to generate. |
required |
device
|
DeviceLike
|
Warp device for the result. Defaults to the current device. |
None
|
Returns:
| Type | Description |
|---|---|
array[vec3]
|
|
See Also
Source code in triwarp/sample.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
sample_fibonacci_cone
sample_fibonacci_cone(
count: int, half_angle: float, device: DeviceLike = None
) -> wp.array[wp.vec3]
Generate near-uniform unit vectors inside a cone around +z.
The same Fibonacci-spiral construction as
sample_fibonacci_sphere, with z descending
uniformly through (cos(half_angle), 1) instead of the whole range — which is the correct
restriction, because a uniform z is a uniform solid angle (Archimedes) whether the band is
the full sphere or a cap. Rejection-sampling a sphere lattice down to the cone would not stay
low-discrepancy; this does.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Number of directions to generate. |
required |
half_angle
|
float
|
Half-angle of the cone in radians, measured from |
required |
device
|
DeviceLike
|
Warp device for the result. Defaults to the current device. |
None
|
Returns:
| Type | Description |
|---|---|
array[vec3]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in triwarp/sample.py
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 | |
sample_surface
sample_surface(
vertices: array[vec3],
faces: array[int32],
count: int,
face_weight: array[float32] | None = None,
seed: int | None = None,
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
Sample points uniformly on a triangle mesh surface (area-weighted faces).
Uses face_normals_and_areas for default triangle weights. Builds a CDF and
draws triangle indices with wp.sample_cdf, then uniform points with
wp.sample_triangle (same scheme as trimesh.sample.sample_surface).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
Vertex positions. |
required |
faces
|
array[int32]
|
Flat triangle indices |
required |
count
|
int
|
Number of samples. |
required |
face_weight
|
array[float32] | None
|
Optional per-face weights (length = number of triangles). If |
None
|
seed
|
int | None
|
RNG seed for |
None
|
Returns:
| Type | Description |
|---|---|
samples
|
|
face_index
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
Source code in triwarp/sample.py
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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
sample_surface_poisson_disk
sample_surface_poisson_disk(
vertices: array[vec3],
faces: array[int32],
count: int,
init_factor: float = 5.0,
face_weight: array[float32] | None = None,
seed: int | None = None,
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
Sample points on a triangle mesh surface with Poisson disk distribution.
Uses Weighted Sample Elimination (Öztireli & Gross 2012): generates
init_factor * count uniform surface samples, then iteratively removes
the most "crowded" points in parallel rounds until count remain.
Each round deletes all alive local weight-maxima simultaneously. Maximality is decided on
(weight, -index), so no two of them are ever within r_max of each other and deleting
the whole set at once cannot remove a point that a sequential elimination would have kept.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
Vertex positions. |
required |
faces
|
array[int32]
|
Flat triangle indices |
required |
count
|
int
|
Number of output samples. |
required |
init_factor
|
float
|
Over-sampling factor; initial pool has |
5.0
|
face_weight
|
array[float32] | None
|
Optional per-face weights passed to the initial uniform sampling. |
None
|
seed
|
int | None
|
RNG seed for the initial uniform sampling. If |
None
|
Returns:
| Type | Description |
|---|---|
samples
|
|
face_index
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
Source code in triwarp/sample.py
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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | |
sample_surface_blue_noise
sample_surface_blue_noise(
vertices: array[vec3],
faces: array[int32],
radius: float,
seed: int | None = None,
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
Sample points on a triangle mesh surface with a blue-noise (Poisson-disk) distribution.
Draws a dense uniform surface pool — 30x the expected output, the oversampling factor
igl::blue_noise uses — and reduces it to a maximal subset in which no two points are
within radius of each other, by randomized-priority parallel dart throwing. The result has
the distribution of sequential dart throwing over a uniformly random order of the pool; see
kernels/algorithms/blue_noise.py for why the parallelism costs nothing in distribution, and
for the measured spacing and coverage against MeshLab and Open3D.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
Vertex positions. |
required |
faces
|
array[int32]
|
Flat triangle indices |
required |
radius
|
float
|
Minimum Poisson disk radius (Euclidean distance in 3D). Enforced exactly: the closest pair in the output is never below it. |
required |
seed
|
int | None
|
RNG seed for the initial uniform sampling and the sampling order. If |
None
|
Returns:
| Type | Description |
|---|---|
samples
|
|
face_index
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
Notes
The count is derived, never requested: it is what a maximal radius-packing of the
surface comes to, so it lands near — not at — the hexagonal-packing estimate the radius is
usually chosen from. Ask for a count with
sample_surface_poisson_disk instead.
A mesh with no faces returns two empty arrays rather than raising.
Source code in triwarp/sample.py
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 | |
sample_volume
sample_volume(
vertices: array[vec3],
faces: array[int32],
count: int,
seed: int | None = None,
) -> wp.array[wp.vec3]
Sample points uniformly inside a watertight triangle mesh volume.
Fans tetrahedra from the mesh's area-weighted surface centroid, builds a CDF from signed tetrahedron volumes, and draws uniform points inside each selected one via the order-statistics barycentric method (zero rejection for meshes that are star-shaped with respect to their centroid).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
Vertex positions. |
required |
faces
|
array[int32]
|
Flat triangle indices |
required |
count
|
int
|
Number of samples. |
required |
seed
|
int | None
|
RNG seed for |
None
|
Returns:
| Type | Description |
|---|---|
samples
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the mesh is not watertight (open boundary edges detected). |
ValueError
|
If the mesh has zero total volume, some signed tetrahedron volumes are negative after
fanning from the centroid (the mesh is not star-shaped with respect to its own centroid,
e.g. a torus), or the mesh has no faces and |
RuntimeError
|
If |
Source code in triwarp/sample.py
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 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 | |
resolve_seed
resolve_seed(seed: int | None) -> int
Concrete non-negative int32-range RNG seed, drawn at random when none was given.
Every generator in the package takes seed: int | None and means the same thing by it, so
the draw lives here rather than at each entry point. Public because
random_soup needs the identical convention from another
module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int | None
|
User-provided seed, or |
required |
Returns:
| Type | Description |
|---|---|
int
|
|
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/sample.py
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 838 839 840 841 842 843 844 845 846 847 848 849 850 851 | |