Skip to content

ReverseAccess

web-test/Cxx/Visualization/ReverseAccess

Question

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

Code

ReverseAccess.cxx

//
// This example demonstrates how to access the source object
// (e.g. vtkSphereSource) from the actor reversely.
//
// Some standard vtk headers.
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkMath.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>

// Additionally needed vtk header for this example.
#include <vtkAlgorithmOutput.h>

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

  // Source
  vtkNew<vtkSphereSource> sphere;
  sphere->SetRadius(0.5);
  // Mapper
  vtkNew<vtkPolyDataMapper> sphereMapper;
  sphereMapper->SetInputConnection(sphere->GetOutputPort());
  // Actor
  vtkNew<vtkActor> sphereActor;
  sphereActor->SetMapper(sphereMapper);
  sphereActor->GetProperty()->SetColor(
      colors->GetColor3d("MistyRose").GetData());

  // Renderer
  vtkNew<vtkRenderer> ren1;
  ren1->SetBackground(colors->GetColor3d("CadetBlue").GetData());

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

  // Add actor to the renderer.
  ren1->AddActor(sphereActor);

  //
  // Now we retrieve the source object from vtkActor reversely,
  // meaning we don't use the spheresource object we instantiated
  // above directly,
  // instead we retrieve a reference to the spheresource through the
  // actor.
  // An advantage of this concept might be that we don't need to
  // maintain the source object anymore
  // in a more complex application.
  // To demonstrate that we can modify properties of the spheresource
  // through this reference
  // beside changing some properties of the actor (in this example we
  // change actor's x-position),
  // we change the radius of the spheresource as well.
  //
  // The next two lines are the core lines for reverse access.
  //
  vtkSmartPointer<vtkAlgorithm> algorithm =
      sphereActor->GetMapper()->GetInputConnection(0, 0)->GetProducer();
  auto srcReference = dynamic_cast<vtkSphereSource*>(algorithm.GetPointer());

  float origRadius = srcReference->GetRadius();
  for (int i = 0; i < 360; ++i)
  {
    // Change radius of the sphere source.
    srcReference->SetRadius(origRadius *
                            (1 + sin((float)i / 180.0 * vtkMath::Pi())));
    // Change the x-position of the actor.
    sphereActor->SetPosition(sin((float)i / 45.0 * vtkMath::Pi()) * 0.5, 0, 0);
    renWin->Render();
  }

  //
  // Thanks to the usage of vtkSmartPointer there is no explicit need
  // to free any objects at this point.
  //
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(ReverseAccess)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonExecutionModel
  FiltersSources
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "ReverseAccess: 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(ReverseAccess MACOSX_BUNDLE ReverseAccess.cxx )
  target_link_libraries(ReverseAccess PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS ReverseAccess
  MODULES ${VTK_LIBRARIES}
)

Download and Build ReverseAccess

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

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

./ReverseAccess

WINDOWS USERS

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