VTK/Examples/Cxx/PolyData/PointInsideObject

From KitwarePublic

Jump to: navigation, search

This example creates a sphere at the origin with radius 1. Then it checks if the point (0,0,0) is inside the sphere. The result should be yes. Then it checks if the point (10,0,0) is inside the sphere. The result should be no.

PointInsideObject.cxx

#include <vtkVersion.h>
#include <vtkPolyData.h>
#include <vtkPointData.h>
#include <vtkSphereSource.h>
#include <vtkSmartPointer.h>
#include <vtkSelectEnclosedPoints.h>
#include <vtkIntArray.h>
#include <vtkDataArray.h>
 
int main(int, char *[])
{ 
  vtkSmartPointer<vtkSphereSource> sphereSource = 
    vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->SetCenter(0.0, 0.0, 0.0);
  sphereSource->SetRadius(1.0);
  sphereSource->Update();
 
  vtkPolyData* sphere = sphereSource->GetOutput();
 
  double testInside[3] = {0.0, 0.0, 0.0};
  double testOutside[3] = {10.0, 0.0, 0.0};
  vtkSmartPointer<vtkPoints> points = 
    vtkSmartPointer<vtkPoints>::New();
  points->InsertNextPoint(testInside);
  points->InsertNextPoint(testOutside);
 
  vtkSmartPointer<vtkPolyData> pointsPolydata = 
    vtkSmartPointer<vtkPolyData>::New();
  pointsPolydata->SetPoints(points);
 
  vtkSmartPointer<vtkSelectEnclosedPoints> selectEnclosedPoints = 
    vtkSmartPointer<vtkSelectEnclosedPoints>::New();
#if VTK_MAJOR_VERSION <= 5
  selectEnclosedPoints->SetInput(pointsPolydata);
#else
  selectEnclosedPoints->SetInputData(pointsPolydata);
#endif
#if VTK_MAJOR_VERSION <= 5
  selectEnclosedPoints->SetSurface(sphere);
#else
  selectEnclosedPoints->SetSurfaceData(sphere);
#endif
  selectEnclosedPoints->Update();
 
  for(unsigned int i = 0; i < 2; i++)
    {
    std::cout << "Point " << i << ": " << selectEnclosedPoints->IsInside(i) << std::endl;
    }
 
  vtkDataArray* insideArray = vtkDataArray::SafeDownCast(selectEnclosedPoints->GetOutput()->GetPointData()->GetArray("SelectedPoints"));
 
  for(vtkIdType i = 0; i < insideArray->GetNumberOfTuples(); i++)
    { 
    std::cout << i << " : " << insideArray->GetComponent(i,0) << std::endl;
    }
 
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
 
PROJECT(PointInsideObject)
 
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
 
if (APPLE)
  add_executable(PointInsideObject MACOSX_BUNDLE PointInsideObject.cxx)
else()
  add_executable(PointInsideObject PointInsideObject.cxx)
endif()
 
if(VTK_LIBRARIES)
  target_link_libraries(PointInsideObject ${VTK_LIBRARIES})
else()
  target_link_libraries(PointInsideObject vtkHybrid )
endif()

Maybe color points inside differently than points outside?

Personal tools