SeedWidget

VTKEx/Cxx/Widgets/SeedWidget


Description

This example demonstrates how to use vtkSeedWidget, which generates (seeds) points to be placed in the scene in the locations where the user clicks.

The points can then be used for operations like connectivity, segmentation, and region growing. For an example using a custom callback where such operations can be assembled, see SeedWidgetWithCustomCallback.

Other languages

See (Java)

Question

If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

Code

SeedWidget.cxx

#include <vtkPointHandleRepresentation2D.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty2D.h> // For setting the color in the handles
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSeedRepresentation.h>
#include <vtkSeedWidget.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>

int main(int vtkNotUsed(argc), char* vtkNotUsed(argv)[])
{
  auto sphereSource =
    vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->Update();

  // Create a mapper and actor
  auto mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputConnection(sphereSource->GetOutputPort());
  auto actor =
    vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);

  auto renderer =
    vtkSmartPointer<vtkRenderer>::New();
  renderer->AddActor(actor);

  auto window =
    vtkSmartPointer<vtkRenderWindow>::New();
  window->AddRenderer(renderer);

  auto interactor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  interactor->SetRenderWindow(window);

  // Create the representation for the seed widget and for its handles
  auto handleRep =
    vtkSmartPointer<vtkPointHandleRepresentation2D>::New();
  handleRep->GetProperty()->SetColor(1, 1, 0); // Make the handles yellow
  auto widgetRep =
    vtkSmartPointer<vtkSeedRepresentation>::New();
  widgetRep->SetHandleRepresentation(handleRep);

  // Create the seed widget
  auto seedWidget =
    vtkSmartPointer<vtkSeedWidget>::New();
  seedWidget->SetInteractor(interactor);
  seedWidget->SetRepresentation(widgetRep);

  seedWidget->On();
  window->Render();
  interactor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(SeedWidget)

find_package(VTK COMPONENTS 
  vtkvtkCommonCore
  vtkvtkFiltersSources
  vtkvtkInteractionStyle
  vtkvtkInteractionWidgets
  vtkvtkRenderingContextOpenGL2
  vtkvtkRenderingCore
  vtkvtkRenderingFreeType
  vtkvtkRenderingGL2PSOpenGL2
  vtkvtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
  message("Skipping SeedWidget: ${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(SeedWidget MACOSX_BUNDLE SeedWidget.cxx )
  target_link_libraries(SeedWidget PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(SeedWidget MACOSX_BUNDLE SeedWidget.cxx )
  target_link_libraries(SeedWidget PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS SeedWidget
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build SeedWidget

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

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

./SeedWidget

WINDOWS USERS

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