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

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to: navigation, search
Line 37: Line 37:
 
</source>
 
</source>
  
==CMakeLists.txt==
+
{{VTKCMakeLists|{{SUBPAGENAME}}}}
<source lang="cmake">
 
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)
 
 
 
</source>
 
  
 
Color query point and points found to be neighbors.
 
Color query point and points found to be neighbors.
 
[[Category:VTKAddVisualization]]
 
[[Category:VTKAddVisualization]]

Revision as of 15:50, 10 December 2012

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;
}

Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(CellLocator)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

add_executable(CellLocator MACOSX_BUNDLE CellLocator.cxx)

if(VTK_LIBRARIES)
  target_link_libraries(CellLocator ${VTK_LIBRARIES})
else()
  target_link_libraries(CellLocator vtkHybrid vtkWidgets)
endif()

Download and Build CellLocator

Click here to download CellLocator. and its CMakeLists.txt file.

Once the tarball CellLocator.tar has been downloaded and extracted,

cd CellLocator/build 
  • If VTK is installed:
cmake ..
  • If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:
cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./CellLocator

WINDOWS USERS PLEASE NOTE: Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.

Color query point and points found to be neighbors.