Skip to content

VisualDebugging

vtk-examples/Cxx/Demos/VisualDebugging

Question

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

Code

VisualDebugging.cxx

#include <vtkActor.h>
#include <vtkCommand.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkObjectFactory.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>

#include <vtkTestFilter.h>

class CustomStyle : public vtkInteractorStyleTrackballCamera
{
public:
  static CustomStyle* New();
  vtkTypeMacro(CustomStyle, vtkInteractorStyleTrackballCamera);

  CustomStyle()
  {
    this->Actor = vtkSmartPointer<vtkActor>::New();
    this->Mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    this->Filter = vtkSmartPointer<vtkTestFilter>::New();
  }

  void CallbackFunction(vtkObject* caller, long unsigned int eventId,
                        void* callData)
  {
    std::cout << "CustomStyle::CallbackFunction called." << std::endl;
    vtkPolyData* intermediate = this->Filter->GetIntermediateOutput();
    intermediate->Modified();
    this->Mapper->SetInput(intermediate);
    this->Renderer->ResetCamera();
    this->Renderer->Render();
    this->Interactor->GetRenderWindow()->Render();
  }

  void OnKeyPress()
  {
    // Get the keypress
    std::string key = this->Interactor->GetKeySym();

    if (key == "s") // 'S'tart
    {
      this->Renderer->AddActor(this->Actor);
      this->Filter->AddObserver(this->Filter->RefreshEvent, this,
                                &CustomStyle::CallbackFunction);
      this->Filter->Update();

      this->Actor->SetMapper(this->Mapper);
    }

    // Forward events
    vtkInteractorStyleTrackballCamera::OnKeyPress();
  }

  vtkRenderer* Renderer;
  vtkSmartPointer<vtkActor> Actor;
  vtkSmartPointer<vtkPolyDataMapper> Mapper;
  vtkSmartPointer<vtkTestFilter> Filter;
};
vtkStandardNewMacro(CustomStyle);

int main(int argc, char* argv[])
{
  // Create a renderer, render window, and interactor
  vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow =
      vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
      vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  renderer->SetBackground(1, 1, 1); // White

  vtkSmartPointer<CustomStyle> style = vtkSmartPointer<CustomStyle>::New();
  style->Renderer = renderer;

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

  return EXIT_SUCCESS;
}

vtkTestFilter.h

#ifndef __vtkTestFilter_h
#define __vtkTestFilter_h

#include <vtkPolyDataAlgorithm.h>
#include <vtkSmartPointer.h>

class vtkTestFilter : public vtkPolyDataAlgorithm
{
public:
  vtkTypeMacro(vtkTestFilter, vtkPolyDataAlgorithm);
  static vtkTestFilter* New();

  vtkPolyData* GetIntermediateOutput()
  {
    return this->Output;
  }

  int RefreshEvent;

protected:
  vtkTestFilter();

  int RequestData(vtkInformation*, vtkInformationVector**,
                  vtkInformationVector*);

  void Iterate(unsigned int iteration);

  vtkSmartPointer<vtkPolyData> Output;

private:
  vtkTestFilter(const vtkTestFilter&);  // Not implemented.
  void operator=(const vtkTestFilter&); // Not implemented.
};

#endif

vtkTestFilter.cxx

#include <vtkTestFilter.h>

#include <vtkAppendPolyData.h>
#include <vtkCommand.h>
#include <vtkDataObject.h>
#include <vtkInformation.h>
#include <vtkInformationVector.h>
#include <vtkObjectFactory.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkStreamingDemandDrivenPipeline.h>

vtkStandardNewMacro(vtkTestFilter);

vtkTestFilter::vtkTestFilter()
{
  this->SetNumberOfInputPorts(0);
  this->Output = vtkSmartPointer<vtkPolyData>::New();

  this->RefreshEvent = vtkCommand::UserEvent + 1;
}

int vtkTestFilter::RequestData(vtkInformation* vtkNotUsed(request),
                               vtkInformationVector** inputVector,
                               vtkInformationVector* outputVector)
{
  // Get the info object
  vtkInformation* outInfo = outputVector->GetInformationObject(0);

  vtkPolyData* output =
      dynamic_cast<vtkPolyData*>(outInfo->Get(vtkDataObject::DATA_OBJECT()));

  this->Output->DeepCopy(output);

  for (unsigned int i = 0; i < 10; i++)
  {
    Iterate(i);
    this->InvokeEvent(this->RefreshEvent, NULL);
  }

  output->ShallowCopy(this->Output);

  return 1;
}

void vtkTestFilter::Iterate(unsigned int iteration)
{
  vtkSmartPointer<vtkSphereSource> sphereSource =
      vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->SetCenter(iteration, 0, 0);
  sphereSource->SetRadius(0.2);
  sphereSource->Update();

  vtkSmartPointer<vtkAppendPolyData> appendFilter =
      vtkSmartPointer<vtkAppendPolyData>::New();
  appendFilter->AddInputConnection(this->Output->GetProducerPort());
  appendFilter->AddInputConnection(sphereSource->GetOutputPort());
  appendFilter->Update();

  this->Output->DeepCopy(appendFilter->GetOutput());
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(VisualDebugging)

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

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

Download and Build VisualDebugging

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

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

./VisualDebugging

WINDOWS USERS

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