Skip to content

Tutorial Step2

vtk-examples/Cxx/Tutorial/Tutorial_Step2


Description

This example shows how to add an observer to a C++ program. It extends the Tutorial_Step1 example (see that example for information on the basic setup).

VTK uses a command/observer design pattern. That is, observers watch for particular events that any vtkObject (or subclass) may invoke on itself. For example, the vtkRenderer invokes a "StartEvent" as it begins to render. Here we add an observer that invokes a command when this event is observed.

Other languages

See (Python)

Question

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

Code

Tutorial_Step2.cxx

/*=========================================================================

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

// First include the required header files for the VTK classes we are using.
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCommand.h>
#include <vtkConeSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>

#include <iostream>

//
// We put the callback in an anonymous namespace, i.e. a namespace with
// no name. This anonymous namespace is only accessible within the file
// that you have created it in. So it is a good way of declaring unique
// identifiers and avoiding making global static variables.
//
namespace {
// Callback for the interaction.
class vtkMyCallback : public vtkCommand
{
public:
  static vtkMyCallback* New()
  {
    return new vtkMyCallback;
  }
  void Execute(vtkObject* caller, unsigned long, void*) override
  {
    // Note the use of reinterpret_cast to cast the caller to the expected type.
    auto renderer = reinterpret_cast<vtkRenderer*>(caller);
    std::cout << renderer->GetActiveCamera()->GetPosition()[0] << " "
              << renderer->GetActiveCamera()->GetPosition()[1] << " "
              << renderer->GetActiveCamera()->GetPosition()[2] << std::endl;
  }
  vtkMyCallback() = default;
};
} // namespace

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

  //
  // The pipeline creation is documented in Tutorial_Step1.
  //
  vtkNew<vtkConeSource> cone;
  cone->SetHeight(3.0);
  cone->SetRadius(1.0);
  cone->SetResolution(10);

  vtkNew<vtkPolyDataMapper> coneMapper;
  coneMapper->SetInputConnection(cone->GetOutputPort());
  vtkNew<vtkActor> coneActor;
  coneActor->SetMapper(coneMapper);
  coneActor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());

  vtkNew<vtkRenderer> ren1;
  ren1->AddActor(coneActor);
  ren1->SetBackground(colors->GetColor3d("MidnightBlue").GetData());
  ren1->ResetCamera();

  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer(ren1);
  renWin->SetSize(300, 300);
  renWin->SetWindowName("Tutorial_Step2");

  // Here is where we setup the observer.
  vtkNew<vtkMyCallback> mo1;
  ren1->AddObserver(vtkCommand::StartEvent, mo1);

  //
  // Now we loop over 360 degrees and render the cone each time.
  //
  for (int i = 0; i < 360; ++i)
  {
    // Render the image.
    renWin->Render();
    // Rotate the active camera by one degree.
    ren1->GetActiveCamera()->Azimuth(1);
  }

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(Tutorial_Step2)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkFiltersSources
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping Tutorial_Step2: ${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(Tutorial_Step2 MACOSX_BUNDLE Tutorial_Step2.cxx )
  target_link_libraries(Tutorial_Step2 PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(Tutorial_Step2 MACOSX_BUNDLE Tutorial_Step2.cxx )
  target_link_libraries(Tutorial_Step2 PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS Tutorial_Step2
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build Tutorial_Step2

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

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

./Tutorial_Step2

WINDOWS USERS

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