Skip to content

DistanceToCamera

vtk-examples/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 question about this example, 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 <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>

int main(int, char*[])
{
  vtkNew<vtkNamedColors> colors;

  //---------------------------------------------------------------------------
  // Draw some arrows that maintain a fixed size during zooming.

  // Create a set of points.
  vtkNew<vtkPointSource> fixedPointSource;
  fixedPointSource->SetNumberOfPoints(2);

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

  // Glyph each point with an arrow.
  vtkNew<vtkArrowSource> arrow;
  vtkNew<vtkGlyph3D> fixedGlyph;
  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.
  vtkNew<vtkPolyDataMapper> fixedMapper;
  fixedMapper->SetInputConnection(fixedGlyph->GetOutputPort());
  fixedMapper->SetScalarVisibility(false);

  // Create an actor.
  vtkNew<vtkActor> fixedActor;
  fixedActor->SetMapper(fixedMapper);
  fixedActor->GetProperty()->SetColor(colors->GetColor3d("Cyan").GetData());

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

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

  // Create a mapper.
  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(glyph->GetOutputPort());
  mapper->SetScalarVisibility(false);

  // Create an actor.
  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetColor(colors->GetColor3d("Yellow").GetData());

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

  // A renderer and render window.
  vtkNew<vtkRenderer> renderer;
  renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("DistanceToCamera");

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

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

  // An interactor.
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  vtkNew<vtkInteractorStyleTrackballCamera> style;
  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.12 FATAL_ERROR)

project(DistanceToCamera)

find_package(VTK COMPONENTS 
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "DistanceToCamera: 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(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}
)

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.