Skip to content

SelectWindowRegion

Repository source: SelectWindowRegion


Question

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

Code

SelectWindowRegion.cxx

#include <vtkCallbackCommand.h>
#include <vtkCommand.h>
#include <vtkImageActor.h>
#include <vtkImageMapper3D.h>
#include <vtkInteractorStyleRubberBand2D.h>
#include <vtkJPEGReader.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

#include <iostream>
#include <string>

namespace {

void SelectionChangedCallbackFunction(vtkObject* caller,
                                      long unsigned int eventId,
                                      void* clientData, void* callData);
}

int main(int argc, char* argv[])
{
  // Parse input arguments.
  if (argc != 2)
  {
    std::cout << "Required parameters: Filename e.g. Ox.jpg" << std::endl;
    return EXIT_FAILURE;
  }

  vtkNew<vtkNamedColors> colors;

  std::string inputFilename = argv[1];

  // Read the image.
  vtkNew<vtkJPEGReader> jPEGReader;
  jPEGReader->SetFileName(inputFilename.c_str());
  jPEGReader->Update();

  // Create an actor.
  vtkNew<vtkImageActor> actor;
  actor->GetMapper()->SetInputConnection(jPEGReader->GetOutputPort());

  // Setup the SelectionChangedEvent callback.
  vtkNew<vtkCallbackCommand> selectionChangedCallback;
  selectionChangedCallback->SetCallback(SelectionChangedCallbackFunction);

  // Setup renderer.
  vtkNew<vtkRenderer> renderer;
  renderer->AddActor(actor);
  renderer->ResetCamera();
  renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());

  // Setup render window.
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("SelectWindowRegion");

  // Setup 2D interaction style.
  vtkNew<vtkInteractorStyleRubberBand2D> style;
  style->AddObserver(vtkCommand::SelectionChangedEvent,
                     selectionChangedCallback);

  // Setup render window interactor.
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetInteractorStyle(style);

  // Render and start interaction.
  renderWindowInteractor->SetRenderWindow(renderWindow);
  renderWindow->Render();
  renderWindowInteractor->Initialize();

  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

namespace {
void SelectionChangedCallbackFunction(vtkObject* vtkNotUsed(caller),
                                      long unsigned int vtkNotUsed(eventId),
                                      void* vtkNotUsed(clientData),
                                      void* callData)
{
  std::cout << "Selection changed callback" << std::endl;

  const unsigned int* rect = reinterpret_cast<unsigned int*>(callData);
  unsigned int pos1X = rect[0];
  unsigned int pos1Y = rect[1];
  unsigned int pos2X = rect[2];
  unsigned int pos2Y = rect[3];

  std::cout << "Start x: " << pos1X << " Start y: " << pos1Y
            << " End x: " << pos2X << " End y: " << pos2Y << std::endl;
}

/*
//You could override this, but then you have to reimplement the functionality.
//Instead, you should use an observer

void vtkInteractorStyleRubberBand2D::OnLeftButtonUp()
{
  std::cout << "LeftButtonUp!" << std::endl;

  std::cout << "Start: " << this->StartPosition[0] << " " <<
this->StartPosition[1] << std::endl; std::cout << "End: " <<
this->EndPosition[0] << " " << this->EndPosition[1] << std::endl;

  //this->Superclass.OnLeftButtonUp(); //doesn't work

  InvokeEvent(vtkCommand::EndPickEvent);
  InvokeEvent(vtkCommand::SelectionChangedEvent);

}
*/
} // namespace

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(SelectWindowRegion)

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

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

Download and Build SelectWindowRegion

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

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

./SelectWindowRegion

WINDOWS USERS

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