CameraActor

VTKExamples/Cxx/Visualization/CameraActor


Other Languages

See (Java)

Question

If you have a simple question about this example contact us at VTKExamplesProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

Code

CameraActor.cxx

#include <vtkCameraActor.h>
#include <vtkSmartPointer.h>

#include <vtkCamera.h>
#include <vtkMapper.h>
#include <vtkNamedColors.h>
#include <vtkPlanes.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>

int main(int, char*[])
{
  auto namedColors =
    vtkSmartPointer<vtkNamedColors>::New();

  // Sphere
  auto sphereSource =
    vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->SetRadius(400);
  sphereSource->Update();

  auto sphereMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
  auto sphereActor =
    vtkSmartPointer<vtkActor>::New();
  sphereActor->SetMapper(sphereMapper);
  sphereActor->GetProperty()->SetDiffuseColor(
      namedColors->GetColor3d("Tomato").GetData());

  // Camera
  auto camera =
    vtkSmartPointer<vtkCamera>::New();

  auto cameraActor =
    vtkSmartPointer<vtkCameraActor>::New();
  cameraActor->SetCamera(camera);
  cameraActor->GetProperty()->SetColor(0, 0, 0);

  // (Xmin,Xmax,Ymin,Ymax,Zmin,Zmax).
  auto bounds = cameraActor->GetBounds();
  std::cout << "bounds: " << bounds[0] << " " << bounds[1] << " " << bounds[2]
            << " " << bounds[3] << " " << bounds[4] << " " << bounds[5]
            << std::endl;

  // Visualize
  auto renderer =
    vtkSmartPointer<vtkRenderer>::New();
  auto renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);

  auto renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  renderer->AddActor(sphereActor);
  // Compute the active camera parameters
  renderer->ResetCamera();

  // Set the camera parameters for the camera actor
  camera->DeepCopy(renderer->GetActiveCamera());
  renderer->AddActor(cameraActor);

  // Position the camera so that we can see the camera actor
  renderer->GetActiveCamera()->SetPosition(1, 0, 0);
  renderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);
  renderer->GetActiveCamera()->SetViewUp(0, 1, 0);
  renderer->GetActiveCamera()->Azimuth(30);
  renderer->GetActiveCamera()->Elevation(30);

  renderer->ResetCamera();
  renderer->SetBackground(namedColors->GetColor3d("SlateGray").GetData());

  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(CameraActor)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkFiltersSources
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
  message("Skipping CameraActor: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(CameraActor MACOSX_BUNDLE CameraActor.cxx )
  target_link_libraries(CameraActor PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(CameraActor MACOSX_BUNDLE CameraActor.cxx )
  target_link_libraries(CameraActor PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS CameraActor
    MODULES ${VTK_LIBRARIES}
    )
endif () 

Download and Build CameraActor

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

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

./CameraActor

WINDOWS USERS

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