vtk中计算点到直线的距离有两个重载函数。
//x[3]表示点的x,y,z坐标, p1[3],p2[3] 分别为直线上的两个点
//函数都返回的为距离的平方
/*注意:重载1中计算点到直线的距离;重载2计算点到线段的最近距离,距离最近的点必定在线段上,p1[3],p2[3]分别为线段的端点*/
//重载1
static double vtkLine::DistanceToLine ( double x[3],
double p1[3],
double p2[3]
)
//重载2
static double vtkLine::DistanceToLine ( double x[3],
double p1[3],
double p2[3],
double & t,
double closestPoint[3]
)
本节实例代码:
#include <vtkSmartPointer.h>
#include <vtkLine.h>
#include <vtkPoints.h>
int main()
{
double lineP0[3] = { 0.0, 0.0, 0.0 };
double lineP1[3] = { 2.0, 0.0, 0.0 };
double p0[3] = { 3.0, 1.0, 0 };
double p1[3] = { 1.0, 2.0, 0 };
{
double dist0 = vtkLine::DistanceToLine(p0, lineP0, lineP1);
std::cout << "Dist0: " << dist0 << std::endl;
double dist1 = vtkLine::DistanceToLine(p1, lineP0, lineP1);
std::cout << "Dist1: " << dist1 << std::endl;
}
{
double t;
double closest[3];
double dist0 = vtkLine::DistanceToLine(p0, lineP0, lineP1, t, closest);
std::cout << "Dist0: " << dist0 << " closest point: " << closest[0] << " " << closest[1] << " " << closest[2] << std::endl;
double dist1 = vtkLine::DistanceToLine(p1, lineP0, lineP1, t, closest);
std::cout << "Dist1: " << dist1 << " closest point: " << closest[0] << " " << closest[1] << " " << closest[2] << std::endl;
}
return 0;
}