通俗的说,就是有一个点集{(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/