Hello,
I am using the method vtkCellLocator::FindClosestPoint and get incorrect results for certain input point coordinates and polydata.
The example code loads a polydata file and uses a vtkCellLocator to find the closest point to the input point with coordinates (110000, 110000, 0). See here:
#include <vtkCellLocator.h>
#include <vtkNew.h>
#include <vtkXMLPolyDataReader.h>
int main(int argc, char *argv[]) {
// Parse command line arguments
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " Filename(.vtp) e.g. Torso.vtp"
<< std::endl;
return EXIT_FAILURE;
}
std::string filename = argv[1];
// Read all the data from the file
vtkNew<vtkXMLPolyDataReader> reader;
reader->SetFileName(filename.c_str());
reader->Update();
// Create the tree
vtkNew<vtkCellLocator> cellLocator;
cellLocator->SetDataSet(reader->GetOutputAsDataSet());
cellLocator->BuildLocator();
double testPoint[3] = {110000, 110000, 0};
// Find the closest points to TestPoint
double closestPoint[3]; // the coordinates of the closest point will be
// returned here
double closestPointDist2; // the squared distance to the closest point will be
// returned here
vtkIdType cellId; // the cell id of the cell containing the closest point will
// be returned here
int 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;
}
The input polydata was created with the plane source in paraview.
For a plane with 4 cells
plane_4cells.vtp (4.6 KB)
the expected result is returned:
Coordinates of closest point: 10.5 10.5 0
Squared distance to closest point: 2.41954e+10
CellId: 3
However for a plane with the same bounds but finer mesh
plane_56cells.vtp (8.7 KB)
the result is incorrect:
Coordinates of closest point: 2.122e-314 2.54639e-313 0
Squared distance to closest point: -1
CellId: 20
Note that switching to vtkStaticCellLocator also does not work for plane_56cells.vtp. The results are:
Coordinates of closest point: 7.90505e-323 1.35808e-312 9.88131e-324
Squared distance to closest point: inf
CellId: 80
I am using VTK9.2.6 on Ubuntu 22.04.
Are there any limitations on FindClosestPoint that need to be considered or is this a bug?