Skip to content

SeedWidgetWithCustomCallback

vtk-examples/Cxx/Widgets/SeedWidgetWithCustomCallback


Descriptionn

This example demonstrates how to place seed points in the scene. The seed points can be used for operations like connectivity, segmentation, and region growing.

Question

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

Code

SeedWidgetWithCustomCallback.cxx

#include <vtkActor.h>
#include <vtkCommand.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointHandleRepresentation2D.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkProperty2D.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSeedRepresentation.h>
#include <vtkSeedWidget.h>
#include <vtkSphereSource.h>

namespace {
class vtkSeedCallback : public vtkCommand
{
public:
  static vtkSeedCallback* New()
  {
    return new vtkSeedCallback;
  }

  vtkSeedCallback()
  {
  }

  virtual void Execute(vtkObject*, unsigned long event, void* calldata)
  {
    if (event == vtkCommand::PlacePointEvent)
    {
      std::cout << "Point placed, total of: "
                << this->SeedRepresentation->GetNumberOfSeeds() << std::endl;
    }
    if (event == vtkCommand::InteractionEvent)
    {
      if (calldata)
      {
        std::cout << "Interacting with seed : "
                  << *(static_cast<int*>(calldata)) << std::endl;
      }
    }

    std::cout << "List of seeds (Display coordinates):" << std::endl;
    for (vtkIdType i = 0; i < this->SeedRepresentation->GetNumberOfSeeds(); i++)
    {
      double pos[3];
      this->SeedRepresentation->GetSeedDisplayPosition(i, pos);
      std::cout << "(" << pos[0] << " " << pos[1] << " " << pos[2] << ")"
                << std::endl;
    }
  }

  void SetRepresentation(vtkSeedRepresentation* rep)
  {
    this->SeedRepresentation = rep;
  }

private:
  vtkSeedRepresentation* SeedRepresentation;
};
} // namespace

int main(int, char*[])
{
  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());

  // A renderer and render window
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("SeedWidgetWithCustomCallback");

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

  // An interactor
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Create the representation
  vtkNew<vtkPointHandleRepresentation2D> handle;
  handle->GetProperty()->SetColor(colors->GetColor3d("Red").GetData());
  vtkNew<vtkSeedRepresentation> rep;
  rep->SetHandleRepresentation(handle);

  // Seed widget
  vtkNew<vtkSeedWidget> seedWidget;
  seedWidget->SetInteractor(renderWindowInteractor);
  seedWidget->SetRepresentation(rep);

  vtkNew<vtkSeedCallback> seedCallback;
  seedCallback->SetRepresentation(rep);
  seedWidget->AddObserver(vtkCommand::PlacePointEvent, seedCallback);
  seedWidget->AddObserver(vtkCommand::InteractionEvent, seedCallback);

  renderWindow->Render();

  renderWindowInteractor->Initialize();
  renderWindow->Render();
  seedWidget->On();

  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(SeedWidgetWithCustomCallback)

find_package(VTK COMPONENTS 
)

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

Download and Build SeedWidgetWithCustomCallback

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

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

./SeedWidgetWithCustomCallback

WINDOWS USERS

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