From fee21c7a346f41c68e9f84f9874d4e5c0aeb0ade Mon Sep 17 00:00:00 2001 From: papush! Date: Wed, 2 Mar 2022 14:46:01 +0100 Subject: [PATCH] fix point to triangle distance computation --- src/main.cc | 15 +++++++++---- src/point_tris_dist.cc | 49 +++++++++++++++++++++--------------------- src/point_tris_dist.h | 5 +++-- 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/src/main.cc b/src/main.cc index c7afaf9..3e0c2fb 100644 --- a/src/main.cc +++ b/src/main.cc @@ -121,16 +121,23 @@ int main(int argc, char **argv) { profile->SetPoints(points); profile->SetStrips(strips); - vtkTriangle *cell = vtkTriangle::SafeDownCast(profile->GetCell(0)); + vtkCell *cell = profile->GetCell(0); + // vtkTriangle *triangle = vtkTriangle::SafeDownCast(cell); + // vtkTriangle *triangle = (vtkTriangle *) cell; - double direction[3] = {0, 0, 0}; + double direction[3];// = {0, 0, 0}; double point[3] = {0, 1, 0}; double d = pointTriangleDistance(point, cell, direction); //double d = 5; - std::cout << "[" << point[0] << ", " << point[1] << ", " << point[2] << "] (" << d << ")" - << "[" << direction[0] << ", " << direction[1] << ", " << direction[2] << "]\n"; + std::cout << "[" << point[0] + << ", " << point[1] + << ", " << point[2] + << "] (" << d << ")" + << "[" << direction[0] + << ", " << direction[1] + << ", " << direction[2] << "]\n"; #ifdef USE_VIEWER /* Volume rendering properties */ diff --git a/src/point_tris_dist.cc b/src/point_tris_dist.cc index aaef56a..53bb241 100644 --- a/src/point_tris_dist.cc +++ b/src/point_tris_dist.cc @@ -1,19 +1,20 @@ #include "point_tris_dist.h" #include +#include #include -double pointSegmentDistance(double *p, double *a, double *b, double* normal) { +double pointSegmentDistance(double *p, double *a, double *b, double* direction) { double segSqrLength = vtkMath::Distance2BetweenPoints(a, b); double ap[3]; vtkMath::Subtract(p, a, ap); double ab[3]; vtkMath::Subtract(b, a, ab); if(segSqrLength <= 0) { - normal[0] = ap[0]; - normal[1] = ap[1]; - normal[2] = ap[2]; - vtkMath::Normalize(normal); + direction[0] = ap[0]; + direction[1] = ap[1]; + direction[2] = ap[2]; + vtkMath::Normalize(direction); return vtkMath::Norm(ap, 3); } @@ -22,14 +23,14 @@ double pointSegmentDistance(double *p, double *a, double *b, double* normal) { vtkMath::MultiplyScalar(ab, h); double v[3]; vtkMath::Subtract(ap, ab, v); - normal[0] = v[0]; - normal[1] = v[1]; - normal[2] = v[2]; - vtkMath::Normalize(normal); + direction[0] = v[0]; + direction[1] = v[1]; + direction[2] = v[2]; + vtkMath::Normalize(direction); return vtkMath::Norm(v); } -double pointTriangleDistance(double *p, vtkTriangle *triangle, double *normal) { +double pointTriangleDistance(double *p, vtkCell *triangle, double *direction) { double a[3]; triangle->GetPoints()->GetPoint(0, a); double b[3]; triangle->GetPoints()->GetPoint(1, b); double c[3]; triangle->GetPoints()->GetPoint(2, c); @@ -64,7 +65,7 @@ double pointTriangleDistance(double *p, vtkTriangle *triangle, double *normal) { n[2] * p[2], }; - double t[3]; vtkMath::Subtract(na, np, t); + double t[3]; vtkMath::Subtract(np, na, t); double nt[3] = { n[0] * t[0], @@ -73,10 +74,10 @@ double pointTriangleDistance(double *p, vtkTriangle *triangle, double *normal) { }; - normal[0] = nt[0]; - normal[1] = nt[1]; - normal[2] = nt[2]; - vtkMath::Normalize(normal); + direction[0] = nt[0]; + direction[1] = nt[1]; + direction[2] = nt[2]; + vtkMath::Normalize(direction); return vtkMath::Norm(nt); } else { @@ -91,17 +92,17 @@ double pointTriangleDistance(double *p, vtkTriangle *triangle, double *normal) { double min = vtkMath::Min(lab, vtkMath::Min(lbc, lca)); if(min == lab) { - normal[0] = normalA[0]; - normal[1] = normalA[1]; - normal[2] = normalA[2]; + direction[0] = normalA[0]; + direction[1] = normalA[1]; + direction[2] = normalA[2]; } else if(min == lbc) { - normal[0] = normalB[0]; - normal[1] = normalB[1]; - normal[2] = normalB[2]; + direction[0] = normalB[0]; + direction[1] = normalB[1]; + direction[2] = normalB[2]; } else { - normal[0] = normalC[0]; - normal[1] = normalC[1]; - normal[2] = normalC[2]; + direction[0] = normalC[0]; + direction[1] = normalC[1]; + direction[2] = normalC[2]; } return min; diff --git a/src/point_tris_dist.h b/src/point_tris_dist.h index e317b89..176e8cb 100644 --- a/src/point_tris_dist.h +++ b/src/point_tris_dist.h @@ -1,9 +1,10 @@ #ifndef POINT_TRIS_DIST_H #define POINT_TRIS_DIST_H -#include +/* #include */ +#include #include -double pointTriangleDistance(double *point, vtkTriangle *triangle, double *normal); +double pointTriangleDistance(double *point, vtkCell *triangle, double *direction); #endif