VTK寻找最近点

目录

1.vtkCellLocator

2.vtkKdTree


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();//关闭文件,保存文件
	}

找到的最近点(红色)如图所示,均为目标数据的顶点。

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值