metrics
triwarp.metrics
Chamfer and Hausdorff distances between point clouds and meshes.
All metrics are fully GPU-resident: they compose the nearest-neighbor and
point-to-surface primitives in proximity with the tiled reductions
in reduce, and never move per-element data to the host.
Chamfer distances follow the pytorch3d convention and are built on squared
Euclidean distances. Hausdorff distances follow libigl's igl::hausdorff and reduce the
(already Euclidean) per-element distances with a maximum, so no squaring or final square root is
needed.
One place the convention does not carry over, because the two libraries answer different
questions: pytorch3d.loss.point_mesh_face_distance sums point-to-triangle with
face-to-point, where
chamfer_points_to_mesh's backward direction is
mesh-vertex to nearest query. The forward halves agree, but the whole scalars differ by
construction and are not comparable.
Two families of geometry are supported and can be mixed:
points_to_points-- both sides are point clouds (nearest neighbor viaquery_nearest).points_to_mesh-- a point cloud versus a triangle mesh (point-to-surface viaclosest_point_on_meshone way, mesh-vertex nearest neighbor the other way).mesh_to_mesh-- both sides are triangle meshes (vertex-to-surface both directions).
chamfer_points_to_points
chamfer_points_to_points(
x: array[vec3],
y: array[vec3],
*,
point_reduction: _PointReduction = ...,
single_directional: bool = ...,
) -> float
chamfer_points_to_points(
x: array[vec3],
y: array[vec3],
*,
point_reduction: None,
single_directional: bool = ...,
) -> _UnreducedChamfer
chamfer_points_to_points(
x: array[vec3],
y: array[vec3],
*,
point_reduction: _PointReduction | None = "mean",
single_directional: bool = False,
) -> float | _UnreducedChamfer
Chamfer distance between two point clouds.
For each point in x the squared Euclidean distance to its nearest neighbor
in y is accumulated (and symmetrically for y into x unless
single_directional), following the pytorch3d convention, whose single_directional
keyword maps onto this one exactly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array[vec3]
|
|
required |
y
|
array[vec3]
|
|
required |
point_reduction
|
_PointReduction | None
|
How to reduce the per-point squared distances of each direction:
|
'mean'
|
single_directional
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
float or array[float32] or tuple
|
The reduced Chamfer distance as a Python |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
ValueError
|
If |
Source code in triwarp/metrics.py
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 134 | |
chamfer_points_to_mesh
chamfer_points_to_mesh(
points: array[vec3],
vertices: array[vec3],
faces: array[int32],
*,
point_reduction: _PointReduction = ...,
single_directional: bool = ...,
) -> float
chamfer_points_to_mesh(
points: array[vec3],
vertices: array[vec3],
faces: array[int32],
*,
point_reduction: None,
single_directional: bool = ...,
) -> _UnreducedChamfer
chamfer_points_to_mesh(
points: array[vec3],
vertices: array[vec3],
faces: array[int32],
*,
point_reduction: _PointReduction | None = "mean",
single_directional: bool = False,
) -> float | _UnreducedChamfer
Chamfer distance between a point cloud and a triangle mesh.
The forward term is the squared distance from each point to the mesh surface
(exact, via closest_point_on_mesh);
the backward term is the squared distance from each mesh vertex to its nearest
point in the cloud.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
array[vec3]
|
|
required |
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
|
required |
point_reduction
|
_PointReduction | None
|
|
'mean'
|
single_directional
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
float or array[float32] or tuple
|
Same layout as |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
ValueError
|
If |
Source code in triwarp/metrics.py
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 | |
chamfer_mesh_to_mesh
chamfer_mesh_to_mesh(
vertices_a: array[vec3],
faces_a: array[int32],
vertices_b: array[vec3],
faces_b: array[int32],
*,
point_reduction: _PointReduction = ...,
single_directional: bool = ...,
) -> float
chamfer_mesh_to_mesh(
vertices_a: array[vec3],
faces_a: array[int32],
vertices_b: array[vec3],
faces_b: array[int32],
*,
point_reduction: None,
single_directional: bool = ...,
) -> _UnreducedChamfer
chamfer_mesh_to_mesh(
vertices_a: array[vec3],
faces_a: array[int32],
vertices_b: array[vec3],
faces_b: array[int32],
*,
point_reduction: _PointReduction | None = "mean",
single_directional: bool = False,
) -> float | _UnreducedChamfer
Chamfer distance between two triangle meshes (vertex-to-surface).
The forward term is the squared distance from each vertex of mesh A to the
surface of mesh B; the backward term is the reverse. This mirrors the
vertex-based approach of libigl while accumulating (mean/sum) instead of maximizing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices_a
|
array[vec3]
|
Vertices |
required |
faces_a
|
array[vec3]
|
Vertices |
required |
vertices_b
|
array[vec3]
|
Vertices |
required |
faces_b
|
array[vec3]
|
Vertices |
required |
point_reduction
|
_PointReduction | None
|
|
'mean'
|
single_directional
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
float or array[float32] or tuple
|
Same layout as |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
ValueError
|
If |
Source code in triwarp/metrics.py
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 291 292 293 294 295 | |
chamfer_points_to_points_loss
chamfer_points_to_points_loss(
x: array[vec3],
y: array[vec3],
*,
tape: Tape | None = None,
point_reduction: _DiffReduction = "mean",
single_directional: bool = False,
) -> twt.Array1dFloat32
Differentiable Chamfer loss between two point clouds.
Squared-distance analogue of
chamfer_points_to_points that returns
a length-1 wp.float32 device array instead of a host float, so the loss can be
back-propagated to x and y through a caller-owned wp.Tape.
The nearest-neighbor assignment (via
query_nearest) is computed
outside tape and held constant during the backward pass, matching pytorch3d's
chamfer_distance gradient.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array[vec3]
|
|
required |
y
|
array[vec3]
|
|
required |
tape
|
Tape | None
|
Caller-owned |
None
|
point_reduction
|
_DiffReduction
|
|
'mean'
|
single_directional
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
array[float32]
|
Length-1 device array holding the Chamfer loss ( |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
ValueError
|
If |
Source code in triwarp/metrics.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 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 | |
chamfer_points_to_mesh_loss
chamfer_points_to_mesh_loss(
points: array[vec3],
vertices: array[vec3],
faces: array[int32],
*,
tape: Tape | None = None,
point_reduction: _DiffReduction = "mean",
single_directional: bool = False,
) -> twt.Array1dFloat32
Differentiable Chamfer loss between a point cloud and a triangle mesh.
Squared-distance analogue of
chamfer_points_to_mesh. The forward term
is the exact point-to-surface squared distance (closest triangle held constant during
backprop); the backward term is the squared distance from each mesh vertex to its
nearest point in the cloud. Gradients flow to points and vertices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
array[vec3]
|
|
required |
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
|
required |
tape
|
Tape | None
|
Caller-owned |
None
|
point_reduction
|
_DiffReduction
|
|
'mean'
|
single_directional
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
array[float32]
|
Length-1 device array holding the Chamfer loss ( |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
ValueError
|
If |
Source code in triwarp/metrics.py
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 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | |
chamfer_mesh_to_mesh_loss
chamfer_mesh_to_mesh_loss(
vertices_a: array[vec3],
faces_a: array[int32],
vertices_b: array[vec3],
faces_b: array[int32],
*,
tape: Tape | None = None,
point_reduction: _DiffReduction = "mean",
single_directional: bool = False,
) -> twt.Array1dFloat32
Differentiable Chamfer loss between two triangle meshes (vertex-to-surface).
Squared-distance analogue of
chamfer_mesh_to_mesh. The forward term is
the squared distance from each vertex of mesh A to the surface of mesh B (with
the closest triangle held constant during backprop); the backward term is the reverse.
Gradients flow to both meshes' vertices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices_a
|
array[vec3]
|
Vertices |
required |
faces_a
|
array[vec3]
|
Vertices |
required |
vertices_b
|
array[vec3]
|
Vertices |
required |
faces_b
|
array[vec3]
|
Vertices |
required |
tape
|
Tape | None
|
Caller-owned |
None
|
point_reduction
|
_DiffReduction
|
|
'mean'
|
single_directional
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
array[float32]
|
Length-1 device array holding the Chamfer loss ( |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
ValueError
|
If |
Source code in triwarp/metrics.py
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 | |
hausdorff_points_to_points
hausdorff_points_to_points(
x: array[vec3],
y: array[vec3],
*,
single_directional: bool = False,
) -> float
Hausdorff distance between two point clouds.
The directed distance d(x, y) = max_i min_j ||x_i - y_j|| is computed (and
symmetrized with d(y, x) unless single_directional). Equivalent to
symmetrizing scipy.spatial.distance.directed_hausdorff.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array[vec3]
|
|
required |
y
|
array[vec3]
|
|
required |
single_directional
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
float
|
The (symmetric or directed) Hausdorff distance. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in triwarp/metrics.py
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 | |
hausdorff_points_to_mesh
hausdorff_points_to_mesh(
points: array[vec3],
vertices: array[vec3],
faces: array[int32],
*,
single_directional: bool = False,
) -> float
Hausdorff distance between a point cloud and a triangle mesh.
The forward distance is the maximum over points of the distance to the mesh surface; the backward distance is the maximum over mesh vertices of the distance to the nearest point in the cloud.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
array[vec3]
|
|
required |
vertices
|
array[vec3]
|
|
required |
faces
|
array[int32]
|
|
required |
single_directional
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
float
|
The (symmetric or directed) Hausdorff distance. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in triwarp/metrics.py
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 | |
hausdorff_mesh_to_mesh
hausdorff_mesh_to_mesh(
vertices_a: array[vec3],
faces_a: array[int32],
vertices_b: array[vec3],
faces_b: array[int32],
*,
single_directional: bool = False,
) -> float
Hausdorff distance between two triangle meshes.
Direct port of libigl's igl::hausdorff (vertex-based overload): the distance
from every vertex of one mesh to the surface of the other is computed in both
directions and the overall maximum is returned. This equals
sqrt(max(d_ba, d_ab)) in libigl, where d_* are squared distances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices_a
|
array[vec3]
|
Vertices |
required |
faces_a
|
array[vec3]
|
Vertices |
required |
vertices_b
|
array[vec3]
|
Vertices |
required |
faces_b
|
array[vec3]
|
Vertices |
required |
single_directional
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
float
|
The (symmetric or directed) Hausdorff distance. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in triwarp/metrics.py
670 671 672 673 674 675 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 | |