Skip to content

SeedWidget

vtk-examples/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 question about this example, please use the VTK Discourse Forum

Code

SeedWidget.cxx

#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointHandleRepresentation2D.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.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 <vtkSphereSource.h>

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

  vtkNew<vtkSphereSource> sphereSource;
  sphereSource->Update();

  // Create a mapper and actor.
  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(sphereSource->GetOutputPort());
  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetColor(colors->GetColor3d("MidnightBlue").GetData());

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

  vtkNew<vtkRenderWindow> window;
  window->AddRenderer(renderer);
  window->SetWindowName("SeedWidget");

  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(window);

  // Create the representation for the seed widget and for its handles.
  vtkNew<vtkPointHandleRepresentation2D> handleRep;
  handleRep->GetProperty()->SetColor(colors->GetColor3d("Red").GetData());
  vtkNew<vtkSeedRepresentation> widgetRep;
  widgetRep->SetHandleRepresentation(handleRep);

  // Create the seed widget.
  vtkNew<vtkSeedWidget> seedWidget;
  seedWidget->SetInteractor(interactor);
  seedWidget->SetRepresentation(widgetRep);

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

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(SeedWidget)

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

if (NOT VTK_FOUND)
  message(FATAL_ERROR "SeedWidget: 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(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}
)

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.