DistanceToCamera

VTKExamples/Cxx/Visualization/DistanceToCamera


Description

This example produces two arrows whose scale stays fixed with respect to the distance from the camera (i.e. as you zoom in and out). Standard spheres are drawn for comparison.

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

DistanceToCamera.cxx

#include <vtkActor.h>
#include <vtkArrowSource.h>
#include <vtkDistanceToCamera.h>
#include <vtkGlyph3D.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkPointSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>

int main(int, char*[])
{
  //---------------------------------------------------------------------------
  // Draw some arrows that maintain a fixed size during zooming.

  // Create a set of points.
  vtkSmartPointer<vtkPointSource> fixedPointSource = vtkSmartPointer<
    vtkPointSource>::New();
  fixedPointSource->SetNumberOfPoints(2);

  // Calculate the distance to the camera of each point.
  vtkSmartPointer<vtkDistanceToCamera> distanceToCamera = vtkSmartPointer<
    vtkDistanceToCamera>::New();
  distanceToCamera->SetInputConnection(fixedPointSource->GetOutputPort());
  distanceToCamera->SetScreenSize(100.0);

  // Glyph each point with an arrow.
  vtkSmartPointer<vtkArrowSource> arrow =
    vtkSmartPointer<vtkArrowSource>::New();
  vtkSmartPointer<vtkGlyph3D> fixedGlyph = vtkSmartPointer<vtkGlyph3D>::New();
  fixedGlyph->SetInputConnection(distanceToCamera->GetOutputPort());
  fixedGlyph->SetSourceConnection(arrow->GetOutputPort());

  // Scale each point.
  fixedGlyph->SetScaleModeToScaleByScalar();
  fixedGlyph->SetInputArrayToProcess(0, 0, 0,
    vtkDataObject::FIELD_ASSOCIATION_POINTS, "DistanceToCamera");

  // Create a mapper.
  vtkSmartPointer<vtkPolyDataMapper> fixedMapper = vtkSmartPointer<
    vtkPolyDataMapper>::New();
  fixedMapper->SetInputConnection(fixedGlyph->GetOutputPort());
  fixedMapper->SetScalarVisibility(false);

  // Create an actor.
  vtkSmartPointer<vtkActor> fixedActor = vtkSmartPointer<vtkActor>::New();
  fixedActor->SetMapper(fixedMapper);
  fixedActor->GetProperty()->SetColor(0, 1, 1);

  //---------------------------------------------------------------------------
  // Draw some spheres that get bigger when zooming in.
  // Create a set of points.
  vtkSmartPointer<vtkPointSource> pointSource =
    vtkSmartPointer<vtkPointSource>::New();
  pointSource->SetNumberOfPoints(4);

  // Glyph each point with a sphere.
  vtkSmartPointer<vtkSphereSource> sphere =
    vtkSmartPointer<vtkSphereSource>::New();
  vtkSmartPointer<vtkGlyph3D> glyph = vtkSmartPointer<vtkGlyph3D>::New();
  glyph->SetInputConnection(pointSource->GetOutputPort());
  glyph->SetSourceConnection(sphere->GetOutputPort());
  glyph->SetScaleFactor(0.1);

  // Create a mapper.
  vtkSmartPointer<vtkPolyDataMapper> mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputConnection(glyph->GetOutputPort());
  mapper->SetScalarVisibility(false);

  // Create an actor.
  vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);
  actor->GetProperty()->SetColor(1, 1, 0);

  //---------------------------------------------------------------------------

  // A renderer and render window.
  vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
  renderer->SetBackground(1, 1, 1); // Background color white
  vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<
    vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);

  // Give DistanceToCamera a pointer to the renderer.
  distanceToCamera->SetRenderer(renderer);

  // Add the actors to the scene.
  renderer->AddActor(fixedActor);
  renderer->AddActor(actor);

  // An interactor.
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<
    vtkInteractorStyleTrackballCamera>::New();
  renderWindowInteractor->SetInteractorStyle(style);
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Render an image (lights and cameras are created automatically).
  renderWindow->Render();

  // Begin mouse interaction.
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(DistanceToCamera)

find_package(VTK COMPONENTS 
  vtkCommonCore
  vtkFiltersCore
  vtkFiltersSources
  vtkInteractionStyle
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
  message("Skipping DistanceToCamera: ${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(DistanceToCamera MACOSX_BUNDLE DistanceToCamera.cxx )
  target_link_libraries(DistanceToCamera PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(DistanceToCamera MACOSX_BUNDLE DistanceToCamera.cxx )
  target_link_libraries(DistanceToCamera PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS DistanceToCamera
    MODULES ${VTK_LIBRARIES}
    )
endif () 

Download and Build DistanceToCamera

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

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

./DistanceToCamera

WINDOWS USERS

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