开发环境:
- Windows 11 家庭中文版
- Microsoft Visual Studio Community 2019
- VTK-9.3.0.rc0
- vtk-example
demo解决问题:使用 VTK 中的单元定位器来查找最近的点
关键点:
- 创建了一个球体数据源,并使用它构建了一个单元定位器(cell locator)。
- 通过 FindClosestPoint 方法查找了测试点的最近点,并输出了最近点的坐标、到最近点的距离的平方以及包含最近点的单元格的 ID。
prj name: CellLocator
#include <vtkCellLocator.h>
#include <vtkNew.h>
#include <vtkSphereSource.h>int main(int, char*[])
{vtkNew<vtkSphereSource> sphereSource;sphereSource->Update();// Create the treevtkNew<vtkCellLocator> cellLocator;cellLocator->SetDataSet(sphereSource->GetOutput());cellLocator->BuildLocator();double testPoint[3] = {2.0, 0.0, 0.0};// Find the closest points to TestPointdouble closestPoint[3]; // the coordinates of the closest point will be// returned heredouble closestPointDist2; // the squared distance to the closest point will be// returned herevtkIdType cellId; // the cell id of the cell containing the closest point will// be returned hereint subId; // this is rarely used (in triangle strips only, I believe)cellLocator->FindClosestPoint(testPoint, closestPoint, cellId, subId,closestPointDist2);std::cout << "Coordinates of closest point: " << closestPoint[0] << " "<< closestPoint[1] << " " << closestPoint[2] << std::endl;std::cout << "Squared distance to closest point: " << closestPointDist2<< std::endl;std::cout << "CellId: " << cellId << std::endl;return EXIT_SUCCESS;
}