VTK/Examples/Cxx/DataStructures/OBBTree IntersectWithLine

From KitwarePublic

Jump to: navigation, search

vtkOBBTree returns ALL intersections with a line and the dataset. If you want the closest intersection, you must manually find it. In this example, we create a sphere and intersect a single line with it.

OBBTree.cxx

#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPointData.h>
#include <vtkLine.h>
#include <vtkOBBTree.h>
 
int main(int, char *[])
{
  vtkSmartPointer<vtkSphereSource> sphereSource = 
    vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->Update();
 
  // Create the locator
  vtkSmartPointer<vtkOBBTree> tree = 
    vtkSmartPointer<vtkOBBTree>::New();
  tree->SetDataSet(sphereSource->GetOutput());
  tree->BuildLocator();
 
  // Intersect the locator with the line
  double lineP0[3] = {0.0, 0.0, 0.0};
  double lineP1[3] = {0.0, 0.0, 2.0};
  vtkSmartPointer<vtkPoints> intersectPoints = 
    vtkSmartPointer<vtkPoints>::New();
 
  tree->IntersectWithLine(lineP0, lineP1, intersectPoints, NULL);
 
  double intersection[3];
  intersectPoints->GetPoint(0, intersection);
  std::cout << "NumPoints: " << intersectPoints->GetNumberOfPoints()
            << std::endl;
  std::cout << "Intersection: " 
            << intersection[0] << ", " 
            << intersection[1] << ", "
            << intersection[2] << std::endl;
 
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(OBBTree)
 
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
 
ADD_EXECUTABLE(OBBTree OBBTree.cxx)
TARGET_LINK_LIBRARIES(OBBTree vtkHybrid)
Personal tools