laplacian
triwarp.laplacian
Discrete Laplacians on a triangle mesh, and the intrinsic repairs that keep them well-behaved.
Every Laplacian this package builds lives here: the cotangent (stiffness) operator and its vector-valued sibling, the combinatorial and row-normalized 1-ring operators, and the lumped mass matrix that pairs with them.
The cotangent operator needs only edge lengths, not vertex positions — which is what makes it
repairable without moving anything. A sliver triangle produces a huge cotangent weight and solves
that are ill-conditioned; a degenerate one, whose three edge lengths fail the triangle inequality
outright (common after float32 rounding, decimation, or a boolean), has no finite weight at all.
The assembly refuses to divide by such a face's zero area, so it contributes nothing — the operator
stays finite, but that face's edge couplings are simply missing from it, which is a wrong
operator rather than an unusable one.
mollify_intrinsic (Sharp & Crane 2020) fixes both by adding
one global constant to every edge length — the smallest that restores the triangle inequality with a
margin. The perturbation is slight and uniform, which beats the alternatives: the operator stays
symmetric, no vertex moves, no connectivity changes, and a mesh that is already fine gets
delta = 0 and is untouched. Mollification makes the weights finite;
intrinsic_delaunay (in
triwarp.remesh, beside the extrinsic flipper) makes them non-negative.
robust_laplacian combines them.
The higher-order operators assembled from these -- the integrated k-harmonic form, the two
Hessian smoothness energies, the edge-based Crouzeix-Raviart pair and the LSCM Hessian -- live one
module along in triwarp.energies. The line is first order against second: this
module builds the operators, that one builds the quadratic forms a solver minimizes. A reader
arriving from libigl, where cotmatrix and crouzeix_raviart_cotmatrix sit together, wants
crouzeix_raviart_cotmatrix.
face_gradients
face_gradients(
vertices: array[vec3],
faces: array[int32],
values: array[float64],
*,
face_normals: array[vec3] | None = None,
face_areas: array[float32] | None = None,
) -> wp.array[wp.vec3d]
Gradient of a per-vertex scalar field inside each face, as a vector in that face's plane.
The piecewise-linear gradient is constant per triangle:
grad = 1/(2A) * sum_k values[k] * (n x e_k), with e_k the counter-clockwise edge
opposite corner k. It satisfies dot(grad, e) == values[end] - values[start] for every
edge of the face, which is the property that makes it the discrete gradient rather than a finite
difference.
Accumulated in float64 and returned as wp.vec3d: the fields this serves decay
exponentially (diffused heat, geodesic distance), and a float32 sum of the three cross
products loses the far field. A degenerate face gets the zero vector rather than a division by
its zero area.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
values
|
array[float64]
|
|
required |
face_normals
|
array[vec3] | None
|
Optional precomputed per-face unit normals and areas, as returned by
|
None
|
face_areas
|
array[vec3] | None
|
Optional precomputed per-face unit normals and areas, as returned by
|
None
|
Returns:
| Type | Description |
|---|---|
array[vec3d]
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Notes
igl.grad(V, F) is the same operator in matrix form, a sparse (3 * n_faces, n_vertices)
map whose product with the field stacks the gradients as [all x; all y; all z]. triwarp
returns the applied result instead of the matrix, because that is what every in-repo consumer
wants -- the heat method takes the normalized gradient face by face and never needs the operator
itself. tests/test_laplacian.py compares the two through that product.
See Also
cotmatrix
face_normals_and_areas
igl.grad
Source code in triwarp/laplacian.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 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 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 | |
cotmatrix_entries
cotmatrix_entries(
vertices: array[vec3],
faces: array[int32],
dtype: type = wp.float32,
) -> twt.Array2dFloat
Per-triangle half-cotangent weights.
For each triangle face, column e stores 1/2 * cot(angle at vertex e) for the
edge opposite that vertex. Columns follow igl edge order: opposite vertices 0, 1, 2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
dtype
|
type
|
Scalar type of the returned weights: |
float32
|
Returns:
| Type | Description |
|---|---|
Array2dFloat
|
Shape |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
See Also
Notes
Matches igl::cotmatrix_entries, column order included.
Source code in triwarp/laplacian.py
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 | |
cotmatrix_entries_intrinsic
cotmatrix_entries_intrinsic(
edge_lengths: Array2dFloat32, dtype: type = wp.float32
) -> twt.Array2dFloat
Per-triangle half-cotangent weights from edge lengths.
(igl::cotmatrix_entries intrinsic overload).
Each row gives the three edge lengths opposite vertices 0, 1, and 2 of the corresponding triangle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_lengths
|
Array2dFloat32
|
|
required |
dtype
|
type
|
Scalar type of the returned weights: |
float32
|
Returns:
| Type | Description |
|---|---|
Array2dFloat
|
Shape |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
See Also
Source code in triwarp/laplacian.py
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 | |
cotmatrix
cotmatrix(
vertices: array[vec3],
faces: array[int32],
cot_entries: Array2dFloat | None = None,
dtype: type = wp.float32,
) -> wps.BsrMatrix[wp.float32]
Cotangent stiffness matrix of the mesh: the discrete Laplace-Beltrami operator.
Builds the sparse (n_vertices, n_vertices) matrix from triangle geometry. Diagonal
entries are negative (each row sums to zero); -L is positive semi-definite on
closed meshes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
cot_entries
|
Array2dFloat | None
|
Optional precomputed |
None
|
dtype
|
type
|
Scalar block type of the assembled matrix: |
float32
|
Returns:
| Type | Description |
|---|---|
BsrMatrix
|
Square |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
ValueError
|
If |
See Also
cotmatrix_entries
crouzeix_raviart_cotmatrix
The nonconforming-FEM sibling, with the degrees of freedom on edge midpoints instead of
vertices. libigl keeps the two together; here it lives in
triwarp.energies with the operators it is assembled for.
mass_matrix
edges_to_csr
Notes
Matches igl::cotmatrix, sign convention included; asserted in tests/test_laplacian.py.
Source code in triwarp/laplacian.py
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 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 | |
robust_laplacian
robust_laplacian(
vertices: array[vec3],
faces: array[int32],
epsilon: float = TOLERANCE_MOLLIFY,
dtype: type = wp.float32,
*,
use_intrinsic_delaunay: bool = True,
) -> wps.BsrMatrix[wp.float32]
Cotangent Laplacian that a bad triangulation cannot poison, via mollification and flips.
Two independent repairs, both intrinsic — no vertex moves, so the surface is unchanged:
- mollification adds one constant to every edge length so that no triangle is degenerate,
which is what keeps the weights finite at all
(
mollify_intrinsic); - intrinsic Delaunay flips retriangulate toward non-negative cotangent weights, which is
what makes the operator satisfy a maximum principle
(
intrinsic_delaunay). Not until — that function keeps a simplicial output, so it declines a flip whose new edge already joins the same two vertices, and a strongly graded surface can leave a handful of negative weights behind that no number of rounds removes. See its Notes; check the result rather than assuming it.
With both on this is igl::intrinsic_delaunay_cotmatrix, and the operator
potpourri3d's use_robust=True solvers build. Turn the flips off for a drop-in
cotmatrix that keeps every edge coupling: the plain operator
drops the ones belonging to a degenerate face, because a zero-area triangle has no finite
cotangent to contribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
epsilon
|
float
|
Triangle-inequality margin, relative to the mean edge length. The default |
TOLERANCE_MOLLIFY
|
dtype
|
type
|
Scalar type of the matrix: |
float32
|
use_intrinsic_delaunay
|
bool
|
Flip to the intrinsic Delaunay triangulation first (default), the name and the default
|
True
|
Returns:
| Type | Description |
|---|---|
BsrMatrix
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in triwarp/laplacian.py
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 | |
mollify_intrinsic
mollify_intrinsic(
vertices: array[vec3],
faces: array[int32],
epsilon: float = TOLERANCE_MOLLIFY,
) -> tuple[twt.Array2dFloat32, float]
Add the smallest constant to every edge length that makes every triangle non-degenerate.
Returns the mollified (n_faces, 3) length table and the constant used. The constant is a
single global number, which is the point: it keeps the perturbation uniform, so the operators
built from these lengths stay symmetric and no triangle is treated as a special case.
delta is zero, and the lengths unchanged, whenever every triangle already satisfies the
triangle inequality with margin epsilon * mean_edge_length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
epsilon
|
float
|
Required margin, relative to the mean edge length. |
TOLERANCE_MOLLIFY
|
Returns:
| Name | Type | Description |
|---|---|---|
lengths |
Array2dFloat32
|
|
delta |
float
|
The constant added to every length. Reading it costs one host readback, and it is returned because it is the honest measure of how much the geometry had to be changed. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in triwarp/laplacian.py
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 460 461 462 463 464 465 466 467 468 | |
connection_laplacian
connection_laplacian(
vertices: array[vec3],
faces: array[int32],
cot_entries: Array2dFloat | None = None,
transport_angles: array[float32] | None = None,
) -> wps.BsrMatrix[wp.float64]
Vector (connection) Laplacian: the cotangent Laplacian for tangent vector fields.
Same cotangent weights and same sparsity as cotmatrix, but each
scalar becomes a 2 x 2 block and each off-diagonal weight is multiplied by the rotation that
re-expresses a tangent vector in the neighbouring vertex's frame
(halfedge_transport_angles). Without those
rotations a difference between vectors at two vertices would subtract components measured from
two unrelated reference directions.
Assembled float64 and positive semi-definite (positive diagonal) — the opposite sign to
cotmatrix's igl convention — because its consumers feed it to a conjugate-gradient solve. It
is symmetric, since transporting from i to j and back are inverse rotations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
cot_entries
|
Array2dFloat | None
|
Optional precomputed |
None
|
transport_angles
|
array[float32] | None
|
Optional precomputed per-halfedge
There is deliberately no |
None
|
Returns:
| Type | Description |
|---|---|
BsrMatrix
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
ValueError
|
If |
Source code in triwarp/laplacian.py
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 | |
laplacian_entries
laplacian_entries(
vertices: array[vec3],
faces: array[int32],
equal_weight: bool = True,
symmetric: bool | None = None,
dtype: type = wp.float32,
edges: Array2dInt32 | None = None,
*,
validate: bool = True,
) -> tuple[
wp.array[wp.int32], wp.array[wp.int32], twt.Array1dFloat
]
Per-edge weight triplets for the 1-ring Laplacian, before assembly.
The analogue of cotmatrix_entries for the umbrella
operator, assembled and row-normalized by laplacian.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
equal_weight
|
bool
|
If |
True
|
symmetric
|
bool | None
|
Adjacency shape. If |
None
|
dtype
|
type
|
Scalar type of the returned |
float32
|
edges
|
Array2dInt32 | None
|
Optional precomputed Worth passing whenever the caller already holds it, because deriving the edge set can
dominate the whole call on a large mesh. Ignored by the directed branch, which reads
Trusted to already be unique and undirected, the way |
None
|
validate
|
bool
|
When |
True
|
Returns:
| Type | Description |
|---|---|
tuple of wp.array
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
ValueError
|
If |
See Also
Source code in triwarp/laplacian.py
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 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 | |
laplacian
laplacian(
vertices: array[vec3],
faces: array[int32],
equal_weight: bool = True,
symmetric: bool | None = None,
dtype: type = wp.float32,
edges: Array2dInt32 | None = None,
*,
validate: bool = True,
) -> wps.BsrMatrix[wp.float32]
Row-normalized 1-ring averaging operator (uniform / umbrella Laplacian).
Builds the sparse (n_vertices, n_vertices) matrix whose row i holds the weights of
the neighbors of vertex i, normalized so each row sums to 1. Applying it to vertex
positions replaces each vertex by the weighted mean of its 1-ring, matching
trimesh.smoothing.laplacian_calculation with equal_weight.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
equal_weight
|
bool
|
If |
True
|
symmetric
|
bool | None
|
Adjacency shape (see |
None
|
dtype
|
type
|
Scalar block type of the assembled matrix: |
float32
|
edges
|
Array2dInt32 | None
|
Optional precomputed |
None
|
validate
|
bool
|
Forwarded to |
True
|
Returns:
| Type | Description |
|---|---|
BsrMatrix
|
Square |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
ValueError
|
If |
Source code in triwarp/laplacian.py
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 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 | |
graph_laplacian
graph_laplacian(
vertices: array[vec3],
faces: array[int32],
dtype: type = wp.float32,
) -> wps.BsrMatrix[wp.float32]
Combinatorial (graph) Laplacian L = A - diag(deg) from mesh connectivity.
Builds the sparse (n_vertices, n_vertices) matrix with unit off-diagonal weights on every
undirected edge and the negated vertex degree on the diagonal, ignoring geometry. Mirrors
libigl's uniform-weight igl::harmonic variant (L = A - diag(rowsum(A)) from
igl::adjacency_matrix). Diagonal entries are negative (each row sums to zero), so -L
is positive semi-definite — the same sign convention as
cotmatrix. This is the operator behind the Tutte embedding
tutte, whose interior block -L_uu = diag(deg) - A is a
diagonally dominant M-matrix (hence positive definite for a well-posed Dirichlet problem).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
dtype
|
type
|
Scalar block type of the assembled matrix: |
float32
|
Returns:
| Type | Description |
|---|---|
BsrMatrix
|
Square |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in triwarp/laplacian.py
790 791 792 793 794 795 796 797 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 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 | |
mass_matrix_entries
mass_matrix_entries(
vertices: array[vec3],
faces: array[int32],
dtype: type = wp.float32,
*,
face_areas: array[float32] | None = None,
) -> twt.Array1dFloat
Per-vertex barycentric lumped mass: a third of each incident triangle's area.
Each triangle donates a third of its area to each of its three vertices, so entry i is
the summed one-third incident-face area at vertex i. This is the
MASSMATRIX_TYPE_BARYCENTRIC lumping; it is the diagonal of
mass_matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
dtype
|
type
|
Scalar type of the returned diagonal: |
float32
|
face_areas
|
array[float32] | None
|
Optional length- |
None
|
Returns:
| Type | Description |
|---|---|
Array1dFloat
|
Length- |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
ValueError
|
If |
See Also
Notes
This is the diagonal of igl::massmatrix under MASSMATRIX_TYPE_BARYCENTRIC.
Source code in triwarp/laplacian.py
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 886 887 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 | |
mass_matrix
mass_matrix(
vertices: array[vec3],
faces: array[int32],
dtype: type = wp.float32,
*,
face_areas: array[float32] | None = None,
) -> wps.BsrMatrix[wp.float32]
Diagonal barycentric lumped mass matrix of the mesh.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
Length- |
required |
dtype
|
type
|
Scalar block type of the assembled matrix: |
float32
|
face_areas
|
array[float32] | None
|
Optional precomputed triangle areas, forwarded to
|
None
|
Returns:
| Type | Description |
|---|---|
BsrMatrix
|
Square |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
See Also
mass_matrix_entries
cotmatrix
crouzeix_raviart_massmatrix
The edge-based sibling, in triwarp.energies.
Notes
Matches igl::massmatrix under MASSMATRIX_TYPE_BARYCENTRIC.
Source code in triwarp/laplacian.py
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 | |