Difference between revisions of "VTK/Examples/Cxx/PolyData/CellLocator"

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to navigationJump to search
Line 10: Line 10:
 
{
 
{
 
   vtkSmartPointer<vtkSphereSource> sphereSource =  
 
   vtkSmartPointer<vtkSphereSource> sphereSource =  
      vtkSmartPointer<vtkSphereSource>::New();
+
    vtkSmartPointer<vtkSphereSource>::New();
 
   sphereSource->Update();
 
   sphereSource->Update();
 
    
 
    
   //Create the tree
+
   // Create the tree
 
   vtkSmartPointer<vtkCellLocator> cellLocator =  
 
   vtkSmartPointer<vtkCellLocator> cellLocator =  
      vtkSmartPointer<vtkCellLocator>::New();
+
    vtkSmartPointer<vtkCellLocator>::New();
 
   cellLocator->SetDataSet(sphereSource->GetOutput());
 
   cellLocator->SetDataSet(sphereSource->GetOutput());
 
   cellLocator->BuildLocator();
 
   cellLocator->BuildLocator();
Line 28: Line 28:
 
   cellLocator->FindClosestPoint(testPoint, closestPoint, cellId, subId, closestPointDist2);
 
   cellLocator->FindClosestPoint(testPoint, closestPoint, cellId, subId, closestPointDist2);
 
    
 
    
   cout << "Coordinates of closest point: " << closestPoint[0] << " " << closestPoint[1] << " " << closestPoint[2] << endl;
+
   std::cout << "Coordinates of closest point: " << closestPoint[0] << " " << closestPoint[1] << " " << closestPoint[2] << std::endl;
   cout << "Squared distance to closest point: " << closestPointDist2 << endl;
+
   std::cout << "Squared distance to closest point: " << closestPointDist2 << std::endl;
   cout << "CellId: " << cellId << endl;
+
   std::cout << "CellId: " << cellId << std::endl;
 
    
 
    
 
   return EXIT_SUCCESS;
 
   return EXIT_SUCCESS;
Line 50: Line 50:
  
 
</source>
 
</source>
 +
 +
Color query point and points found to be neighbors.
 +
[[Category:VTKAddVisualization]]

Revision as of 15:13, 1 September 2010

This example creates a sphere and then finds the closest point on the sphere to a test point.

CellLocator.cxx

#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkCellLocator.h>

int main(int, char *[])
{
  vtkSmartPointer<vtkSphereSource> sphereSource = 
    vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->Update();
  
  // Create the tree
  vtkSmartPointer<vtkCellLocator> cellLocator = 
    vtkSmartPointer<vtkCellLocator>::New();
  cellLocator->SetDataSet(sphereSource->GetOutput());
  cellLocator->BuildLocator();
  
  double testPoint[3] = {2.0, 0.0, 0.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;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

PROJECT(CellLocator)

FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})

ADD_EXECUTABLE(CellLocator CellLocator.cxx)
TARGET_LINK_LIBRARIES(CellLocator vtkHybrid)

Color query point and points found to be neighbors.