VTK/Examples/Cxx/Boneyard/GeometricObjects/WriteFile/Frustum

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to navigationJump to search

This example gets the frustum of a camera and converts it to a PolyData.

Frustum.cxx

#include <vtkFrustumSource.h>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkCamera.h>
#include <vtkPlanes.h>

int main(int, char *[])
{
  //Create a camera.
  vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
  double planesArray[24];
  
  //Get the data from the camera describing the frustum planes.
  camera->GetFrustumPlanes(1, planesArray);
  
  //Create a set of planes from this data.
  vtkSmartPointer<vtkPlanes> planes = vtkSmartPointer<vtkPlanes>::New();
  planes->SetFrustumPlanes(planesArray);
  
  //Create a frustum.
  vtkSmartPointer<vtkFrustumSource> frustumSource = vtkSmartPointer<vtkFrustumSource>::New();
  frustumSource->SetPlanes(planes);
  frustumSource->Update();
  
  vtkPolyData* frustum = frustumSource->GetOutput();

  //Write the file.
  vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  writer->SetInput(frustum);
  writer->SetFileName("frustum.vtp");
  writer->Write();

  return EXIT_SUCCESS;
}

Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(Frustum)

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

add_executable(Frustum MACOSX_BUNDLE Frustum.cxx)

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

Download and Build Frustum

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

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

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

./Frustum

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.