Skip to content

ModifiedBSPTreeIntersectWithLine

vtk-examples/Cxx/DataStructures/ModifiedBSPTreeIntersectWithLine

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

ModifiedBSPTreeIntersectWithLine.cxx

#include <vtkModifiedBSPTree.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkSphereSource.h>

int main(int, char*[])
{
  vtkNew<vtkSphereSource> sphereSource;
  sphereSource->Update();

  // Create the tree
  vtkNew<vtkModifiedBSPTree> bspTree;
  bspTree->SetDataSet(sphereSource->GetOutput());
  bspTree->BuildLocator();

  // Inputs
  double p1[3] = {-2, 0, 0};
  double p2[3] = {2, 0, 0};
  double tolerance = .001;

  // Outputs
  double t; // Parametric coordinate of intersection (0 (corresponding to p1) to
            // 1 (corresponding to p2)).
  double x[3]; // The coordinate of the intersection.
  double pcoords[3];
  int subId;

  // Note: For a typical use case (ray-triangle intersection), pcoords and subId
  // will not be used.
  vtkIdType iD =
      bspTree->IntersectWithLine(p1, p2, tolerance, t, x, pcoords, subId);

  std::cout << "iD: " << iD << std::endl;
  std::cout << "t: " << t << std::endl;
  std::cout << "x: " << x[0] << " " << x[1] << " " << x[2] << std::endl;

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(ModifiedBSPTreeIntersectWithLine)

find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
  FiltersFlowPaths
  FiltersSources
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "ModifiedBSPTreeIntersectWithLine: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(ModifiedBSPTreeIntersectWithLine MACOSX_BUNDLE ModifiedBSPTreeIntersectWithLine.cxx )
  target_link_libraries(ModifiedBSPTreeIntersectWithLine PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS ModifiedBSPTreeIntersectWithLine
  MODULES ${VTK_LIBRARIES}
)

Download and Build ModifiedBSPTreeIntersectWithLine

Click here to download ModifiedBSPTreeIntersectWithLine and its CMakeLists.txt file. Once the tarball ModifiedBSPTreeIntersectWithLine.tar has been downloaded and extracted,

cd ModifiedBSPTreeIntersectWithLine/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:

./ModifiedBSPTreeIntersectWithLine

WINDOWS USERS

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