Skip to content

Commit 88f5d79

Browse files
gkioxarifacebook-github-bot
authored andcommitted
fix small face issue for ptmeshdist
Summary: Fix small face issue for point_mesh distance computation. The issue lies in the computation of `IsInsideTriangle` which is unstable and non-symmetrical when faces with small areas are given as input. This diff fixes the issue by returning `False` for `IsInsideTriangle` when small faces are given as input. Reviewed By: bottler Differential Revision: D29163052 fbshipit-source-id: be297002f26b5e6eded9394fde00553a37406bee
1 parent a343cf5 commit 88f5d79

File tree

3 files changed

+93
-11
lines changed

3 files changed

+93
-11
lines changed

pytorch3d/csrc/utils/geometry_utils.cuh

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,27 @@ PointTriangleDistanceBackward(
461461
// vec3 utils //
462462
// ************************************************************* //
463463

464+
// Computes the area of a triangle (v0, v1, v2).
465+
//
466+
// Args:
467+
// v0, v1, v2: vec3 coordinates of the triangle vertices
468+
//
469+
// Returns
470+
// area: float: The area of the triangle
471+
//
472+
__device__ inline float
473+
AreaOfTriangle(const float3& v0, const float3& v1, const float3& v2) {
474+
float3 p0 = v1 - v0;
475+
float3 p1 = v2 - v0;
476+
477+
// compute the hypotenus of the scross product (p0 x p1)
478+
float dd = hypot(
479+
p0.y * p1.z - p0.z * p1.y,
480+
hypot(p0.z * p1.x - p0.x * p1.z, p0.x * p1.y - p0.y * p1.x));
481+
482+
return dd / 2.0;
483+
}
484+
464485
// Computes the barycentric coordinates of a point p relative
465486
// to a triangle (v0, v1, v2), i.e. p = w0 * v0 + w1 * v1 + w2 * v2
466487
// s.t. w0 + w1 + w2 = 1.0
@@ -503,6 +524,7 @@ __device__ inline float3 BarycentricCoords3Forward(
503524
// Checks whether the point p is inside the triangle (v0, v1, v2).
504525
// A point is inside the triangle, if all barycentric coordinates
505526
// wrt the triangle are >= 0 & <= 1.
527+
// If the triangle is degenerate, aka line or point, then return False.
506528
//
507529
// NOTE that this function assumes that p lives on the space spanned
508530
// by (v0, v1, v2).
@@ -521,11 +543,16 @@ __device__ inline bool IsInsideTriangle(
521543
const float3& v0,
522544
const float3& v1,
523545
const float3& v2) {
524-
float3 bary = BarycentricCoords3Forward(p, v0, v1, v2);
525-
bool x_in = 0.0f <= bary.x && bary.x <= 1.0f;
526-
bool y_in = 0.0f <= bary.y && bary.y <= 1.0f;
527-
bool z_in = 0.0f <= bary.z && bary.z <= 1.0f;
528-
bool inside = x_in && y_in && z_in;
546+
bool inside;
547+
if (AreaOfTriangle(v0, v1, v2) < 1e-5) {
548+
inside = 0;
549+
} else {
550+
float3 bary = BarycentricCoords3Forward(p, v0, v1, v2);
551+
bool x_in = 0.0f <= bary.x && bary.x <= 1.0f;
552+
bool y_in = 0.0f <= bary.y && bary.y <= 1.0f;
553+
bool z_in = 0.0f <= bary.z && bary.z <= 1.0f;
554+
inside = x_in && y_in && z_in;
555+
}
529556
return inside;
530557
}
531558

pytorch3d/csrc/utils/geometry_utils.h

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,26 @@ PointTriangleDistanceBackward(
560560
return std::make_tuple(grad_p, grad_v0, grad_v1, grad_v2);
561561
}
562562

563+
// Computes the area of a triangle (v0, v1, v2).
564+
// Args:
565+
// v0, v1, v2: vec3 coordinates of the triangle vertices
566+
//
567+
// Returns:
568+
// area: float: the area of the triangle
569+
//
570+
template <typename T>
571+
T AreaOfTriangle(const vec3<T>& v0, const vec3<T>& v1, const vec3<T>& v2) {
572+
vec3<T> p0 = v1 - v0;
573+
vec3<T> p1 = v2 - v0;
574+
575+
// compute the hypotenus of the scross product (p0 x p1)
576+
float dd = std::hypot(
577+
p0.y * p1.z - p0.z * p1.y,
578+
std::hypot(p0.z * p1.x - p0.x * p1.z, p0.x * p1.y - p0.y * p1.x));
579+
580+
return dd / 2.0;
581+
}
582+
563583
// Computes the squared distance of a point p relative to a triangle (v0, v1,
564584
// v2). If the point's projection p0 on the plane spanned by (v0, v1, v2) is
565585
// inside the triangle with vertices (v0, v1, v2), then the returned value is
@@ -604,6 +624,7 @@ vec3<T> BarycentricCoords3Forward(
604624
// Checks whether the point p is inside the triangle (v0, v1, v2).
605625
// A point is inside the triangle, if all barycentric coordinates
606626
// wrt the triangle are >= 0 & <= 1.
627+
// If the triangle is degenerate, aka line or point, then return False.
607628
//
608629
// NOTE that this function assumes that p lives on the space spanned
609630
// by (v0, v1, v2).
@@ -623,11 +644,16 @@ static bool IsInsideTriangle(
623644
const vec3<T>& v0,
624645
const vec3<T>& v1,
625646
const vec3<T>& v2) {
626-
vec3<T> bary = BarycentricCoords3Forward(p, v0, v1, v2);
627-
bool x_in = 0.0f <= bary.x && bary.x <= 1.0f;
628-
bool y_in = 0.0f <= bary.y && bary.y <= 1.0f;
629-
bool z_in = 0.0f <= bary.z && bary.z <= 1.0f;
630-
bool inside = x_in && y_in && z_in;
647+
bool inside;
648+
if (AreaOfTriangle(v0, v1, v2) < 1e-5) {
649+
inside = 0;
650+
} else {
651+
vec3<T> bary = BarycentricCoords3Forward(p, v0, v1, v2);
652+
bool x_in = 0.0f <= bary.x && bary.x <= 1.0f;
653+
bool y_in = 0.0f <= bary.y && bary.y <= 1.0f;
654+
bool z_in = 0.0f <= bary.z && bary.z <= 1.0f;
655+
inside = x_in && y_in && z_in;
656+
}
631657
return inside;
632658
}
633659

tests/test_point_mesh_distance.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def _point_to_bary(point: torch.Tensor, tri: torch.Tensor) -> torch.Tensor:
9696
d20 = v2.dot(v0)
9797
d21 = v2.dot(v1)
9898

99-
denom = d00 * d11 - d01 * d01
99+
denom = d00 * d11 - d01 * d01 + TestPointMeshDistance.eps()
100100
s2 = (d11 * d20 - d01 * d21) / denom
101101
s3 = (d00 * d21 - d01 * d20) / denom
102102
s1 = 1.0 - s2 - s3
@@ -117,6 +117,13 @@ def _is_inside_triangle(point: torch.Tensor, tri: torch.Tensor) -> torch.Tensor:
117117
Returns:
118118
inside: BoolTensor of shape (1)
119119
"""
120+
v0 = tri[1] - tri[0]
121+
v1 = tri[2] - tri[0]
122+
area = torch.cross(v0, v1).norm() / 2.0
123+
124+
# check if triangle is a line or a point. In that case, return False
125+
if area < 1e-5:
126+
return False
120127
bary = TestPointMeshDistance._point_to_bary(point, tri)
121128
inside = ((bary >= 0.0) * (bary <= 1.0)).all()
122129
return inside
@@ -836,6 +843,28 @@ def test_point_mesh_face_distance(self):
836843
)
837844
self.assertClose(pcls.points_list()[i].grad, pcls_op.points_list()[i].grad)
838845

846+
def test_small_faces_case(self):
847+
for device in [torch.device("cpu"), torch.device("cuda:0")]:
848+
mesh_vertices = torch.tensor(
849+
[
850+
[-0.0021, -0.3769, 0.7146],
851+
[-0.0161, -0.3771, 0.7146],
852+
[-0.0021, -0.3771, 0.7147],
853+
],
854+
dtype=torch.float32,
855+
device=device,
856+
)
857+
mesh1_faces = torch.tensor([[0, 2, 1]], device=device)
858+
mesh2_faces = torch.tensor([[2, 0, 1]], device=device)
859+
pcd_points = torch.tensor([[-0.3623, -0.5340, 0.7727]], device=device)
860+
mesh1 = Meshes(verts=[mesh_vertices], faces=[mesh1_faces])
861+
mesh2 = Meshes(verts=[mesh_vertices], faces=[mesh2_faces])
862+
pcd = Pointclouds(points=[pcd_points])
863+
864+
loss1 = point_mesh_face_distance(mesh1, pcd)
865+
loss2 = point_mesh_face_distance(mesh2, pcd)
866+
self.assertClose(loss1, loss2)
867+
839868
@staticmethod
840869
def point_mesh_edge(N: int, V: int, F: int, P: int, device: str):
841870
device = torch.device(device)

0 commit comments

Comments
 (0)