vtk中使用查找点集中离目标点最近的k个点(vtkKdTree,vtkKdTreePointLocator,vtkOctreePointLocator)

通俗的说,就是有一个点集{(0,0),(1,0),(0,1),(1,1)},你有一个点A(0.9,0),你想
在点集中找k个(k=2)离点A最近的点,就可以使用此方法

1.使用vtkKdTree
关键代码:

    // Create the tree
    vtkSmartPointer<vtkKdTree> pointTree =
      vtkSmartPointer<vtkKdTree>::New();
    pointTree->BuildLocatorFromPoints(points);

    // Find the 2 closest points to (0.5,0,0)
    vtkIdType k = 2;
    double testPoint[3] = {0.5, 0.0, 0.0};
    vtkSmartPointer<vtkIdList> result =
      vtkSmartPointer<vtkIdList>::New();

    pointTree->FindClosestNPoints(k, testPoint, result);

官方demo:
https://lorensen.github.io/VTKExamples/site/Cxx/DataStructures/BuildLocatorFromKClosestPoints/
使用vtkOctreePointLocator查找最临近的k个点:
https://lorensen.github.io/VTKExamples/site/Cxx/DataStructures/OctreeKClosestPoints/

2.使用vtkKdTreePointLocator
关键代码:

    //Create the tree
    vtkSmartPointer<vtkKdTreePointLocator> pointTree =
        vtkSmartPointer<vtkKdTreePointLocator>::New();
    pointTree->SetDataSet(pointSource->GetOutput());
    pointTree->BuildLocator();

    // Find the k closest points to (0,0,0)
    unsigned int k = 3;
    double testPoint[3] = {0.0, 0.0, 0.0};
    vtkSmartPointer<vtkIdList> result =
        vtkSmartPointer<vtkIdList>::New();

    pointTree->FindClosestNPoints(k, testPoint, result);

官方demo:
https://lorensen.github.io/VTKExamples/site/Cxx/DataStructures/KdTreePointLocator/ClosestNPoints/

3.使用kDTree查找最近的点
关键代码:

  //Create the tree
  vtkSmartPointer<vtkKdTree> kDTree = 
    vtkSmartPointer<vtkKdTree>::New();
  kDTree->BuildLocatorFromPoints(points);

  double testPoint[3] = {2.0, 0.0, 0.0};

  //Find the closest points to TestPoint
  double closestPointDist;
  vtkIdType id = kDTree->FindClosestPoint(testPoint, closestPointDist); //vtkKdTree::FindClosestPoint: must build locator first
  std::cout << "The closest point is point " << id << std::endl;

官方demo:
https://lorensen.github.io/VTKExamples/site/Cxx/DataStructures/KdTree/
使用vtkKdTreePointLocator查找最近点demo:
https://lorensen.github.io/VTKExamples/site/Cxx/DataStructures/KdTreePointLocatorClosestPoint/
使用vtkIncrementalOctreePointLocator查找最近点及插入点demo:
https://lorensen.github.io/VTKExamples/site/Cxx/DataStructures/IncrementalOctreePointLocator/
vtkOctreePointLocator查找最近点及插入点demo:
https://lorensen.github.io/VTKExamples/site/Cxx/DataStructures/OctreeClosestPoint/

4.使用vtkKdTreePointLocator在圆范围内查找

pointTree->FindPointsWithinRadius(1.0, testPoint, result);

官方demo:
https://lorensen.github.io/VTKExamples/site/Cxx/DataStructures/KDTreeFindPointsWithinRadius/
使用vtkOctreePointLocator在圆范围内查找:
https://lorensen.github.io/VTKExamples/site/Cxx/DataStructures/OctreeFindPointsWithinRadius/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力减肥的小胖子5

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值