Skip to content

Commit 071771a

Browse files
committed
result.nearest_point and result.barycentric both available for backwards compatibility
1 parent 5e7b17f commit 071771a

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ tmd::Result result = mesh_distance.signed_distance({ x, y, z });
2323
std::cout << "Signed distance: " << result.distance << std::endl;
2424
std::cout << "Nearest entity: " << result.nearest_entity << std::endl;
2525
std::cout << "Nearest triangle index: " << result.triangle_id << std::endl;
26+
std::cout << "Nearest point: " << result.nearest_point << std::endl;
2627
std::cout << "Nearest point barycentric coordinates: " << result.barycentric << std::endl;
2728
```
2829

TriangleMeshDistance/include/tmd/TriangleMeshDistance.h

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,17 @@ namespace tmd
7373
/**
7474
* Computes the squared distance, the nearest entity (vertex, edge or face) and the nearest point from a point to a triangle.
7575
*/
76-
static void point_triangle_sq_unsigned(double& distance_sq, NearestEntity& nearest_entity, Vec3d& barycentric, const Vec3d& point, const Vec3d& v0, const Vec3d& v1, const Vec3d& v2);
76+
static void point_triangle_sq_unsigned(double& distance_sq, NearestEntity& nearest_entity, Vec3d& barycentric, Vec3d& nearest_point, const Vec3d& point, const Vec3d& v0, const Vec3d& v1, const Vec3d& v2);
7777
// -----------------------------------
7878

7979
// Struct that contains the result of a distance query
8080
struct Result
8181
{
8282
double distance = std::numeric_limits<double>::max();
83-
Vec3d barycentric;
83+
Vec3d nearest_point;
8484
tmd::NearestEntity nearest_entity;
8585
int triangle_id = -1;
86+
Vec3d barycentric;
8687
};
8788
// -----------------------------------
8889

@@ -515,10 +516,12 @@ inline void tmd::TriangleMeshDistance::_query(Result& result, const Node& node,
515516
double distance_sq;
516517
tmd::NearestEntity nearest_entity;
517518
Vec3d barycentric;
518-
tmd::point_triangle_sq_unsigned(distance_sq, nearest_entity, barycentric, point, v0, v1, v2);
519+
Vec3d nearest_point;
520+
tmd::point_triangle_sq_unsigned(distance_sq, nearest_entity, barycentric, nearest_point, point, v0, v1, v2);
519521

520522
if (distance_sq < result.distance * result.distance) {
521523
result.nearest_entity = nearest_entity;
524+
result.nearest_point = nearest_point;
522525
result.barycentric = barycentric;
523526
result.distance = std::sqrt(distance_sq);
524527
result.triangle_id = triangle_id;
@@ -553,7 +556,7 @@ inline void tmd::TriangleMeshDistance::_query(Result& result, const Node& node,
553556
}
554557
}
555558

556-
static void tmd::point_triangle_sq_unsigned(double& distance_sq, NearestEntity& nearest_entity, Vec3d& barycentric, const Vec3d& p, const Vec3d& a, const Vec3d& b, const Vec3d& c)
559+
static void tmd::point_triangle_sq_unsigned(double& distance_sq, NearestEntity& nearest_entity, Vec3d& barycentric, Vec3d& nearest_point, const Vec3d& p, const Vec3d& a, const Vec3d& b, const Vec3d& c)
557560
{
558561
// This function is a modified version of the one found in the Real-Time Collision Detection book by Ericson.
559562
Vec3d ab = b - a;
@@ -567,7 +570,8 @@ static void tmd::point_triangle_sq_unsigned(double& distance_sq, NearestEntity&
567570
if (snom <= 0.0 && tnom <= 0.0) {
568571
nearest_entity = NearestEntity::V0;
569572
barycentric = { 1.0, 0.0, 0.0 };
570-
distance_sq = (p - a).squaredNorm();
573+
nearest_point = a;
574+
distance_sq = (p - nearest_point).squaredNorm();
571575
return;
572576
}
573577

@@ -576,13 +580,15 @@ static void tmd::point_triangle_sq_unsigned(double& distance_sq, NearestEntity&
576580
if (sdenom <= 0.0 && unom <= 0.0) {
577581
nearest_entity = NearestEntity::V1;
578582
barycentric = { 0.0, 1.0, 0.0 };
579-
distance_sq = (p - b).squaredNorm();
583+
nearest_point = b;
584+
distance_sq = (p - nearest_point).squaredNorm();
580585
return;
581586
}
582587
if (tdenom <= 0.0 && udenom <= 0.0) {
583588
nearest_entity = NearestEntity::V2;
584589
barycentric = { 0.0, 0.0, 1.0 };
585-
distance_sq = (p - c).squaredNorm();
590+
nearest_point = c;
591+
distance_sq = (p - nearest_point).squaredNorm();
586592
return;
587593
}
588594

@@ -595,7 +601,8 @@ static void tmd::point_triangle_sq_unsigned(double& distance_sq, NearestEntity&
595601
double arc = snom / (snom + sdenom);
596602
nearest_entity = NearestEntity::E01;
597603
barycentric = { 1.0 - arc, arc, 0.0 };
598-
distance_sq = (p - (barycentric[0]*a + barycentric[1]*b)).squaredNorm();
604+
nearest_point = barycentric[0] * a + barycentric[1] * b;
605+
distance_sq = (p - nearest_point).squaredNorm();
599606
return;
600607
}
601608

@@ -605,7 +612,8 @@ static void tmd::point_triangle_sq_unsigned(double& distance_sq, NearestEntity&
605612
double arc = unom / (unom + udenom);
606613
nearest_entity = NearestEntity::E12;
607614
barycentric = { 0.0, 1.0 - arc, arc };
608-
distance_sq = (p - (barycentric[1]*b + barycentric[2]*c)).squaredNorm();
615+
nearest_point = barycentric[1] * b + barycentric[2] * c;
616+
distance_sq = (p - nearest_point).squaredNorm();
609617
return;
610618
}
611619

@@ -615,7 +623,8 @@ static void tmd::point_triangle_sq_unsigned(double& distance_sq, NearestEntity&
615623
double arc = tnom / (tnom + tdenom);
616624
nearest_entity = NearestEntity::E02;
617625
barycentric = { 1.0 - arc, 0.0, arc };
618-
distance_sq = (p - (barycentric[0]*a + barycentric[2]*c)).squaredNorm();
626+
nearest_point = barycentric[0] * a + barycentric[2] * c;
627+
distance_sq = (p - nearest_point).squaredNorm();
619628
return;
620629
}
621630

@@ -625,6 +634,7 @@ static void tmd::point_triangle_sq_unsigned(double& distance_sq, NearestEntity&
625634
double w = 1.0 - u - v; // = vc / (va + vb + vc)
626635
nearest_entity = NearestEntity::F;
627636
barycentric = { u, v, w };
628-
distance_sq = (p - (u*a + v*b + w*c)).squaredNorm();
637+
nearest_point = u*a + v*b + w*c;
638+
distance_sq = (p - nearest_point).squaredNorm();
629639
return;
630640
}

0 commit comments

Comments
 (0)