triangles
triwarp.triangles
Quantities carried by one triangle at a time: its normal, area, angles, shape and barycentre.
face_normals_and_areas,
face_angles,
face_centroids and
face_quality are the per-face fields the rest of the package
reduces over; face_nondegenerate_mask flags the faces
those quantities are meaningless on.
barycentric_to_points,
points_to_barycentric and
closest_point work one query point against one triangle, row
by row, without a BVH -- for a query against the whole surface see
triwarp.proximity.
CornerNormalWeighting
module-attribute
CornerNormalWeighting = Literal['angle', 'area']
Per-corner weight selected by corner_normals.
FaceQualityMetric
module-attribute
FaceQualityMetric = Literal[
"aspect_ratio",
"radius_ratio",
"area_max_side",
"mean_ratio",
"area",
]
Shape-quality measure selected by face_quality.
face_normals_and_areas
face_normals_and_areas(
vertices: array[vec3], faces: array[int32]
) -> tuple[wp.array[wp.vec3], wp.array[wp.float32]]
Compute the unit normal and area of each triangle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
Returns:
| Name | Type | Description |
|---|---|---|
normals |
array[vec3]
|
Length- |
areas |
array[float32]
|
Length- |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in triwarp/triangles.py
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 | |
face_angles
face_angles(
vertices: array[vec3], faces: array[int32]
) -> twt.Array2dFloat32
Interior angle at each triangle vertex, in radians.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
Returns:
| Type | Description |
|---|---|
Array2dFloat32
|
Shape |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
See Also
Source code in triwarp/triangles.py
71 72 73 74 75 76 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 | |
corner_normals
corner_normals(
vertices: array[vec3],
faces: array[int32],
crease_edges: Array2dInt32 | None = None,
*,
weighting: CornerNormalWeighting = "angle",
twins: array[int32] | None = None,
n_vertices: int | None = None,
face_normals: array[vec3] | None = None,
face_areas: array[float32] | None = None,
) -> twt.Array2dVec3
Per-corner normals: one normal for each (face, corner), averaged over its smooth group.
The third weighting triwarp lacked. A face normal is flat, a vertex normal is smooth everywhere, and neither can render a crease: a cube needs three different normals at each of its corners, one per incident face group. This gives each corner the angle-weighted average of the faces reachable from its own by rotating about its vertex without crossing a crease edge, which is what an OBJ export or a hard-edge renderer needs.
Two exact identities fall out of that definition and are worth knowing, because they are also how it is tested:
- with no creases, every corner of a vertex gets that vertex's normal under the matching
weighting --
vertex_normalsatweighting="angle"orvertex_normalsatweighting="area"; - with every edge a crease, every corner gets its own face's normal, whatever the weighting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
crease_edges
|
Array2dInt32 | None
|
|
None
|
weighting
|
CornerNormalWeighting
|
Per-corner weight for the average, named as in
|
'angle'
|
twins
|
array[int32] | None
|
Optional precomputed |
None
|
n_vertices
|
int | None
|
Total vertex count. When |
None
|
face_normals
|
array[vec3] | None
|
Optional length- |
None
|
face_areas
|
array[float32] | None
|
See |
None
|
Returns:
| Type | Description |
|---|---|
Array2dVec3
|
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
RuntimeError
|
If |
Examples:
creases = tw.seams.crease_edges(v, f, angle=0.5)
normals = tw.triangles.corner_normals(v, f, creases)
assert normals.shape == (f.shape[0] // 3, 3)
See Also
face_normals_and_areas
The flat weighting: one normal per face.
vertex_normals at weighting="angle"
The smooth weighting this reduces to when there are no creases.
crease_edges
Produces the hard-edge set.
Source code in triwarp/triangles.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
face_quality
face_quality(
vertices: array[vec3],
faces: array[int32],
metric: FaceQualityMetric = "aspect_ratio",
) -> wp.array[wp.float32]
Per-face shape-quality measure.
This is the quantity the library already uses internally to decide whether a triangle is worth
keeping — isotropic_remesh and
flip_to_delaunay gate on aspect_ratio, and the
hole-filling cost functions weight candidate triangles by it — exposed so a caller can inspect
or threshold it directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
metric
|
FaceQualityMetric
|
Which measure to compute. All but
|
'aspect_ratio'
|
Returns:
| Type | Description |
|---|---|
array[float32]
|
Length- |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
Source code in triwarp/triangles.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |
face_centroids
face_centroids(
vertices: array[vec3], faces: array[int32]
) -> wp.array[wp.vec3]
Barycentre of every face: the mean of its three corners.
Not to be confused with surface_centroid, which is the
mesh's single area-weighted centre. This is one point per triangle and no weighting is
involved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
Returns:
| Type | Description |
|---|---|
array[vec3]
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in triwarp/triangles.py
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 | |
face_signed_volumes
face_signed_volumes(
vertices: array[vec3] | array[vec3d],
faces: array[int32],
apex: vec3 | vec3d | None = None,
) -> wp.array[wp.float32] | wp.array[wp.float64]
Signed volume of the tetrahedron (apex, v0, v1, v2) for each face.
dot(v0 - apex, cross(v1 - apex, v2 - apex)) / 6, so the sign follows the face's winding:
positive where the face turns its front to the apex. Summed over a closed, consistently wound
surface this is the enclosed volume (volume) and is independent of
apex; per face it is not, which is why the argument exists —
sample_volume fans from the surface centroid so that every
tetrahedron is positive on a star-shaped mesh, and uses the array as a cumulative distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3] | array[vec3d]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
apex
|
vec3 | vec3d | None
|
Common apex of every tetrahedron, in |
None
|
Returns:
| Type | Description |
|---|---|
array[float32] | array[float64]
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
See Also
volume
The sum of these over the whole mesh.
is_volume
Whether that sum is a meaningful volume at all.
face_centroids
Source code in triwarp/triangles.py
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 | |
face_nondegenerate_mask
face_nondegenerate_mask(
vertices: array[vec3], faces: array[int32]
) -> wp.array[wp.bool]
Flag triangles with non-zero area.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
Returns:
| Type | Description |
|---|---|
array[bool]
|
Length- |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
See Also
Source code in triwarp/triangles.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | |
barycentric_to_points
barycentric_to_points(
vertices: array[vec3],
faces: array[int32],
barycentric: array[vec3],
) -> wp.array[wp.vec3]
Convert barycentric coordinates to Cartesian points, one triangle per row.
Operates on the "triangle soup" convention shared with trimesh.triangles: row i
of barycentric is evaluated against triangle i of faces (not every triangle
against every coordinate).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
barycentric
|
array[vec3]
|
Length- |
required |
Returns:
| Type | Description |
|---|---|
array[vec3]
|
Length- |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
See Also
points_to_barycentric
The inverse: recovers barycentric coordinates from Cartesian points.
trimesh.triangles.barycentric_to_points
Source code in triwarp/triangles.py
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 | |
points_to_barycentric
points_to_barycentric(
vertices: array[vec3],
faces: array[int32],
points: array[vec3],
) -> wp.array[wp.vec3]
Convert Cartesian points to barycentric coordinates, one triangle per row.
Operates on the "triangle soup" convention shared with trimesh.triangles: row i of
points is projected onto triangle i of faces (not every triangle against every
point).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
points
|
array[vec3]
|
Length- |
required |
Returns:
| Type | Description |
|---|---|
array[vec3]
|
Length- |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
See Also
barycentric_to_points
The inverse: recovers Cartesian points from barycentric coordinates.
trimesh.triangles.points_to_barycentric
Notes
trimesh.triangles.points_to_barycentric offers a method choice between a
cross-product ratio and Cramer's rule on the Gram system, and this function deliberately does
not: the two are algebraically equal, and the Cramer form is less accurate at every triangle
shape and scale measured and more accurate at none, returning wholly wrong finite values on
the slivers an ordinary decimated mesh carries. The two also cost the same, both being
launch-bound, so there was nothing to trade. The measured table is in
triwarp.kernels.triangles.point_barycentric.
Source code in triwarp/triangles.py
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 | |
closest_point
closest_point(
vertices: array[vec3],
faces: array[int32],
points: array[vec3],
) -> wp.array[wp.vec3]
Closest point on each triangle to a query point, one triangle per row.
Operates on the "triangle soup" convention shared with trimesh.triangles: row i of
points is projected onto triangle i of faces (not every triangle against every
point). For nearest-triangle queries over an entire mesh, see
closest_point_on_mesh instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
points
|
array[vec3]
|
Length- |
required |
Returns:
| Type | Description |
|---|---|
array[vec3]
|
Length- |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
See Also
Source code in triwarp/triangles.py
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 632 633 634 635 636 637 638 639 640 641 642 643 644 645 | |