目录
1.vtkCellLocator
使用octree构建,找到的点并不一定是mesh的顶点,而是由点到面作垂线的交点。
可以直接获得最近点的坐标。
// 通过vtkCellLocator查找最近对应点
void correspondencesInit()
{
// 创建树,找到最近的cell id和点 id
_cellLocator = vtkSmartPointer<vtkCellLocator>::New();
_cellLocator->SetDataSet(_target);
_cellLocator->BuildLocator();
//设置对应点的数量与源数据点的数量保持一致
if (_correspondences == NULL) _correspondences = Vertices::New();
_correspondences->SetNumberOfPoints(_vertices->GetNumberOfPoints());
ofstream outfile;
outfile.open("D:\\tooth_tata\\IOS_single_o\\correspondences.txt", ios::in);//打开文件
for (int i = 0; i < _vertices->GetNumberOfPoints(); i++)
{
double testPoint[3];
_vertices->GetPoint(i, testPoint);//获取源数据所有点的索引及坐标
auto assistCell = vtkSmartPointer<vtkGenericCell>::New();
double closestPoint[3];//返回最近点的坐标
double closestPointDist2;//返回到最近点的平方距离
vtkIdType cellId; //返回最近点的cell id
int subId;
_cellLocator->FindClosestPoint(testPoint, closestPoint, assistCell, cellId, subId, closestPointDist2);
_correspondences->SetPoint(i, closestPoint);//把找到的最近点坐标赋给_correspondences
outfile << closestPoint[0] << " " << closestPoint[1] << " " << closestPoint[2] << endl;
}
outfile.close();//关闭文件,保存文件
}
找到的最近点(红色)如图所示,并不为目标数据的顶点。
2.vtkKdTree
可以设置查找最近的k个点,可以获取点的ID,并通过GetId()、GetPoint()进一步获得点的坐标。
// 通过vtkKdTree查找最近对应点
void correspondencesInit()
{
//创建树
vtkSmartPointer<vtkKdTree> kDTree = vtkSmartPointer<vtkKdTree>::New();
kDTree->BuildLocatorFromPoints(_target);
//设置寻找k个最近点
vtkIdType k = 1;
vtkSmartPointer<vtkIdList> result = vtkSmartPointer<vtkIdList>::New();
/* vtkSmartPointer<vtkKdTreePointLocator> kDTree = vtkSmartPointer<vtkKdTreePointLocator>::New();
kDTree->SetDataSet(_target);
kDTree->BuildLocator();*/
//设置对应点的数量与源数据点的数量保持一致
if (_correspondences == NULL) _correspondences = Vertices::New();
_correspondences->SetNumberOfPoints(_vertices->GetNumberOfPoints());
ofstream outfile;
outfile.open("D:\\tooth_tata\\IOS_single_o\\correspondences.txt", ios::in);//打开文件
for (int i = 0; i < _vertices->GetNumberOfPoints(); i++)
{
double testPoint[3];
_vertices->GetPoint(i, testPoint);//获取源数据所有点的索引及坐标
double closestPoint[3];//返回最近点的坐标
double closestPointDist2;//返回到最近点的平方距离
kDTree->FindClosestNPoints(k, testPoint, result);
vtkIdType point_ind = result->GetId(0);
double p[3];
_target->GetPoint(point_ind, p);
_correspondences->SetPoint(i, p);
outfile << p[0] << " " << p[1] << " " << p[2] << endl;
}
outfile.close();//关闭文件,保存文件
}
找到的最近点(红色)如图所示,均为目标数据的顶点。