transform
triwarp.transform
Affine transforms: build a 4x4 matrix, apply it to a buffer, and classify what it preserves.
Three layers, and which one a caller wants depends on what they hold. The buffer ops
(transform_points and its vector / normal / mesh siblings)
take a matrix and an array and support in-place operation through out=. The builders
(translation_matrix,
rotation_matrix,
scale_matrix,
reflection_matrix) compose the matrix on the host, each
about an optional center. And classify_transform
answers what a matrix preserves -- lengths, angles, orientation -- which is what
Trimesh.transform reads to decide how much of its cache
survives.
Points, vectors and normals are three different maps and picking the wrong one is a silent error rather than a loud one:
| quantity | maps by | translation |
|---|---|---|
| position | M |
applied |
| displacement, tangent | linear block of M |
ignored |
| normal | inverse transpose of the linear block | ignored |
The first two agree on everything but the translation, so a normal pushed through
transform_vectors is correct under any isometry and
tilts off the surface under a shear or a non-uniform scale -- measured 37 degrees off on a
diag(2, 1, 1) scale of the plane x + y = 0. Only unit scale hides it, which is why the
distinction gets its own entry point.
See Also
triwarp.registration
Fits a transform (procrustes,
icp); this module applies one.
ORTHOGONALITY_RTOL
module-attribute
ORTHOGONALITY_RTOL = 1e-05
TransformKind
Bases: StrEnum
What an affine transform preserves, ordered from most structure to least.
Each member's guarantees include every later member's: a TRANSLATION is a RIGID motion
whose rotation is the identity, and a RIGID motion is a SIMILARITY of scale 1. The
ordering is what makes this useful as a cache key -- see
Trimesh.transform.
Attributes:
| Name | Type | Description |
|---|---|---|
IDENTITY |
Changes nothing. |
|
TRANSLATION |
Identity linear block. Preserves lengths, angles, orientation and every direction, so normals and tangent frames are unchanged rather than merely rotated. |
|
RIGID |
Orthogonal linear block with positive determinant: a rotation, optionally with a translation. Preserves lengths, angles and orientation; rotates directions. |
|
REFLECTION |
Orthogonal linear block with negative determinant and unit scale. Preserves lengths and
unsigned angles but reverses orientation, so face winding must be flipped to keep
normals outward. A mirroring transform that also scales is |
|
SIMILARITY |
Orthogonal up to a single factor: a uniform scale, optionally rotated, mirrored and
translated. Preserves angles and therefore cotangent weights; scales lengths by
|
|
AFFINE |
An invertible map that is none of the above -- a non-uniform scale or a shear. Being a bijection it still preserves connectivity, incidence and self-intersection; nothing metric. |
|
SINGULAR |
Not an invertible affine map: a rank-deficient linear block, or a bottom row that makes the matrix projective rather than affine. Flattens the mesh, so even the predicates a bijection would preserve are gone and only connectivity survives. |
IDENTITY
class-attribute
instance-attribute
IDENTITY = 'identity'
TRANSLATION
class-attribute
instance-attribute
TRANSLATION = 'translation'
RIGID
class-attribute
instance-attribute
RIGID = 'rigid'
REFLECTION
class-attribute
instance-attribute
REFLECTION = 'reflection'
SIMILARITY
class-attribute
instance-attribute
SIMILARITY = 'similarity'
AFFINE
class-attribute
instance-attribute
AFFINE = 'affine'
SINGULAR
class-attribute
instance-attribute
SINGULAR = 'singular'
transform_points
transform_points(
points: array[vec3],
matrix: mat44 | array[mat44],
*,
out: array[vec3] | None = None,
) -> wp.array[wp.vec3]
Apply an affine transform to a buffer of positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
array[vec3]
|
|
required |
matrix
|
mat44 | array[mat44]
|
|
required |
out
|
array[vec3] | None
|
Destination, allocated when |
None
|
Returns:
| Type | Description |
|---|---|
array[vec3]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
Notes
Safe in place: each thread reads exactly the element it writes.
Source code in triwarp/transform.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 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 | |
transform_vectors
transform_vectors(
vectors: array[vec3],
matrix: mat44 | array[mat44],
*,
out: array[vec3] | None = None,
) -> wp.array[wp.vec3]
Apply the linear block of an affine transform to a buffer of directions, ignoring translation.
For a displacement, a velocity or a tangent vector. Not for a normal unless the transform
is an isometry -- see transform_normals and this
module's docstring. The result is not renormalized: a scale in matrix scales the output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vectors
|
array[vec3]
|
|
required |
matrix
|
mat44 | array[mat44]
|
|
required |
out
|
array[vec3] | None
|
Destination, allocated when |
None
|
Returns:
| Type | Description |
|---|---|
array[vec3]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
See Also
Source code in triwarp/transform.py
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 | |
transform_normals
transform_normals(
normals: array[vec3],
matrix: mat44 | array[mat44],
*,
out: array[vec3] | None = None,
) -> wp.array[wp.vec3]
Map unit normals through an affine transform by its inverse transpose, and renormalize.
A normal is a covector: it maps by the inverse transpose of the linear block, which is the
only map keeping it perpendicular to the transformed surface under a shear or a non-uniform
scale. Zero normals stay zero, the degenerate-face convention
face_normals_and_areas writes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
normals
|
array[vec3]
|
|
required |
matrix
|
mat44 | array[mat44]
|
|
required |
out
|
array[vec3] | None
|
Destination, allocated when |
None
|
Returns:
| Type | Description |
|---|---|
array[vec3]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the linear block of |
RuntimeError
|
If |
Notes
Orientation is not corrected: a mirroring matrix leaves these pointing inward until the
face winding is reversed too. transform_mesh is the
entry point that handles both.
See Also
Source code in triwarp/transform.py
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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
transform_mesh
transform_mesh(
vertices: array[vec3],
faces: array[int32],
matrix: mat44 | array[mat44],
*,
out_vertices: array[vec3] | None = None,
out_faces: array[int32] | None = None,
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
Transform a mesh's vertices, reversing face winding when the transform mirrors.
The winding flip is what keeps normals outward: a transform with negative determinant maps the
face's corner cross product to -M n, so reversing the corner order restores +M n.
Without it a mirrored mesh has inward normals and a negative
volume.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
matrix
|
mat44 | array[mat44]
|
|
required |
out_vertices
|
array[vec3] | None
|
Destinations, allocated when |
None
|
out_faces
|
array[vec3] | None
|
Destinations, allocated when |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
vertices |
array[vec3]
|
Transformed positions. |
faces |
array[int32]
|
Face buffer, corner-reversed if |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If |
See Also
transform_points
Trimesh.transform
The cached-mesh entry point, which carries derived quantities forward.
Source code in triwarp/transform.py
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 | |
translation_matrix
translation_matrix(
offset: vec3 | Sequence[float],
) -> wp.mat44
Homogeneous matrix translating by offset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
offset
|
vec3 | Sequence[float]
|
Length-3 translation. |
required |
Returns:
| Type | Description |
|---|---|
mat44
|
A |
Examples:
up = tw.transform.translation_matrix((0.0, 0.0, 1.0))
moved = tw.transform.transform_points(v, up)
See Also
Source code in triwarp/transform.py
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 | |
rotation_matrix
rotation_matrix(
axis: vec3 | Sequence[float],
angle: float,
center: vec3 | Sequence[float] | None = None,
) -> wp.mat44
Homogeneous matrix rotating by angle radians about axis, through center.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axis
|
vec3 | Sequence[float]
|
Length-3 rotation axis; normalized internally, so its magnitude is ignored. |
required |
angle
|
float
|
Rotation angle in radians, counter-clockwise about |
required |
center
|
vec3 | Sequence[float] | None
|
Fixed point of the rotation, the origin when |
None
|
Returns:
| Type | Description |
|---|---|
mat44
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
See Also
Source code in triwarp/transform.py
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 | |
scale_matrix
scale_matrix(
factor: float | Sequence[float],
center: vec3 | Sequence[float] | None = None,
) -> wp.mat44
Homogeneous matrix scaling about center, uniformly or per axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factor
|
float | Sequence[float]
|
A single scale factor, or a length-3 per-axis factor. A per-axis factor whose components
differ is a |
required |
center
|
vec3 | Sequence[float] | None
|
Fixed point of the scaling, the origin when |
None
|
Returns:
| Type | Description |
|---|---|
mat44
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
See Also
Source code in triwarp/transform.py
450 451 452 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 | |
reflection_matrix
reflection_matrix(
normal: vec3 | Sequence[float],
center: vec3 | Sequence[float] | None = None,
) -> wp.mat44
Homogeneous matrix reflecting through the plane with the given normal and point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
normal
|
vec3 | Sequence[float]
|
Length-3 plane normal; normalized internally. |
required |
center
|
vec3 | Sequence[float] | None
|
A point on the mirror plane, the origin when |
None
|
Returns:
| Type | Description |
|---|---|
mat44
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in triwarp/transform.py
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 | |
normal_matrix
normal_matrix(matrix: mat44 | array[mat44]) -> wp.mat33
Inverse transpose of a transform's linear block: the map that carries normals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
mat44 | array[mat44]
|
|
required |
Returns:
| Type | Description |
|---|---|
mat33
|
The |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the linear block is singular to within
|
See Also
Source code in triwarp/transform.py
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 | |
classify_transform
classify_transform(
matrix: mat44 | array[mat44],
*,
rtol: float = ORTHOGONALITY_RTOL,
) -> TransformKind
Decide what an affine transform preserves: lengths, angles, orientation, or only connectivity.
Reads the 3x3 linear block's Gram matrix R Rt. When it is a positive multiple of the
identity the transform is a similarity, and the multiple is the squared scale; the sign of
det R then separates rotations from reflections, and an identity block separates a pure
translation. Everything else is TransformKind.AFFINE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
mat44 | array[mat44]
|
|
required |
rtol
|
float
|
Relative tolerance on the Gram matrix, defaulting to
|
ORTHOGONALITY_RTOL
|
Returns:
| Type | Description |
|---|---|
TransformKind
|
The strongest class the matrix satisfies. |
Notes
Singularity is decided from the linear block's singular values, not from its determinant: a
block is rank-deficient to within rtol when its smallest singular value is at most an
rtol fraction of its largest -- the reciprocal condition number, which stays correct
however anisotropic the block is. A determinant-vs-scale test would not: a non-uniform scale
can have an arbitrarily small determinant relative to its trace while remaining perfectly
invertible (diag(1000, 1, 1) is TransformKind.AFFINE, not singular, despite a determinant
500x below its isotropic-scale expectation).
Orientation is not part of the answer except at TransformKind.REFLECTION, which is the
unit-scale mirror. A mirroring scale is TransformKind.SIMILARITY, so a caller that needs to
know whether winding flips must read det of the linear block rather than switching on this
result.
Examples:
kind = tw.transform.classify_transform(tw.transform.rotation_matrix((0.0, 0.0, 1.0), 0.5))
See Also
Source code in triwarp/transform.py
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 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 | |
transform_scale
transform_scale(matrix: mat44 | array[mat44]) -> float
Uniform scale factor of a similarity transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
mat44 | array[mat44]
|
|
required |
Returns:
| Type | Description |
|---|---|
float
|
The positive factor by which lengths scale. Meaningful only when
|
See Also
Source code in triwarp/transform.py
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 | |
as_mat44
as_mat44(matrix: mat44 | array[mat44]) -> wp.mat44
Read a transform parameter as a scalar wp.mat44, whichever form it arrives in.
Every entry point here accepts a (1,) device array. For
transform_points and
transform_mesh the array's pointer reaches the kernel
directly, so a fitted transform never leaves the device; every other entry point -- the
orientation flip, the classification, the normal map, transform_vectors -- needs the value on
the host regardless of which form it arrives in, and this is the one place it crosses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
mat44 | array[mat44]
|
|
required |
Returns:
| Type | Description |
|---|---|
mat44
|
The matrix as a host value, returned unchanged when it already is one. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Notes
A device array costs one host readback. Every caller here is making a host branch over a whole
launch, so the matrix has to cross either way. Calling this more than once on the same
unchanged matrix within one operation (e.g. once for classification, once for the normal
map) pays the readback again each time -- resolve it to a host value once and pass that value
down instead, the way Trimesh.transform does.
See Also
Source code in triwarp/transform.py
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 | |
matrix_to_numpy
matrix_to_numpy(matrix: mat44 | array[mat44]) -> np.ndarray
Read a transform parameter back as a host (4, 4) float64 array.
For host matrix arithmetic that has no wp.mat44 equivalent -- composing with a NumPy
matrix built elsewhere, or reading a row/column out directly. Every decision this module makes
on a transform (classify_transform,
reverses_orientation,
normal_matrix) goes through this.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
mat44 | array[mat44]
|
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
See Also
Source code in triwarp/transform.py
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 | |
reverses_orientation
reverses_orientation(matrix: mat44 | array[mat44]) -> bool
Whether a transform mirrors, so that face winding must be reversed to keep normals outward.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
mat44 | array[mat44]
|
|
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Notes
Deliberately separate from classify_transform:
orientation is an axis of its own, and only TransformKind.REFLECTION implies it. A mirroring
scale classifies as TransformKind.SIMILARITY and still reverses winding, so a caller that
switches on the kind alone gets it wrong.
See Also
Source code in triwarp/transform.py
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 | |