VTK/Examples/OBBTree
From KitwarePublic
For ray-triangle intersections, vtkOBBTree (oriented bounding box tree) should be used. It returns ALL intersections with a line and the dataset, so you must manually find the closest intersection if that is what you want. In this example, we create a sphere and intersect a single line with it.
#include <vtkSmartPointer.h> #include <vtkSphereSource.h> #include <vtkPoints.h> #include <vtkPolyData.h> #include <vtkPointData.h> #include <vtkLine.h> #include <vtkOBBTree.h> int main(int argc, char *argv[]) { 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); cout << "NumPoints: " << intersectPoints->GetNumberOfPoints() << endl; cout << "Intersection: " << intersection[0] << ", " << intersection[1] << ", " << intersection[2] << 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)

