Skip to content

ImplicitPlaneWidget2

vtk-examples/Cxx/Widgets/ImplicitPlaneWidget2


Description

This example shows how to use the second generation ImplicitPlaneWidget2 to interactively define the clipping plane for a polydata. If no arguments are specified, a vtkSphereSource generates the polydata. By specifying a .vtp file, the example can operate on arbitrary polydata.

For example, try VTKData/Data/cow.vtp.

Question

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

Code

ImplicitPlaneWidget2.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkClipPolyData.h>
#include <vtkCommand.h>
#include <vtkImplicitPlaneRepresentation.h>
#include <vtkImplicitPlaneWidget2.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPlane.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkXMLPolyDataReader.h>

namespace {
// Callback for the interaction
// This does the actual work: updates the vtkPlane implicit function.
// This in turn causes the pipeline to update and clip the object.
class vtkIPWCallback : public vtkCommand
{
public:
  static vtkIPWCallback* New()
  {
    return new vtkIPWCallback;
  }
  virtual void Execute(vtkObject* caller, unsigned long, void*)
  {
    vtkImplicitPlaneWidget2* planeWidget =
        reinterpret_cast<vtkImplicitPlaneWidget2*>(caller);
    vtkImplicitPlaneRepresentation* rep =
        reinterpret_cast<vtkImplicitPlaneRepresentation*>(
            planeWidget->GetRepresentation());
    rep->GetPlane(this->Plane);
  }
  vtkIPWCallback() : Plane(0), Actor(0)
  {
  }
  vtkPlane* Plane;
  vtkActor* Actor;
};
} // namespace

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

  vtkNew<vtkSphereSource> sphereSource;
  sphereSource->SetRadius(10.0);

  vtkNew<vtkXMLPolyDataReader> reader;

  // Setup a visualization pipeline
  vtkNew<vtkPlane> plane;
  vtkNew<vtkClipPolyData> clipper;
  clipper->SetClipFunction(plane);
  clipper->InsideOutOn();
  if (argc < 2)
  {
    clipper->SetInputConnection(sphereSource->GetOutputPort());
  }
  else
  {
    reader->SetFileName(argv[1]);
    clipper->SetInputConnection(reader->GetOutputPort());
  }

  // Create a mapper and actor
  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(clipper->GetOutputPort());
  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);

  vtkNew<vtkProperty> backFaces;
  backFaces->SetDiffuseColor(colors->GetColor3d("Gold").GetData());

  actor->SetBackfaceProperty(backFaces);

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

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

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

  renderWindow->Render();

  renderer->GetActiveCamera()->Azimuth(-60);
  renderer->GetActiveCamera()->Elevation(30);
  renderer->ResetCamera();
  renderer->GetActiveCamera()->Zoom(0.75);

  // The callback will do the work
  vtkNew<vtkIPWCallback> myCallback;
  myCallback->Plane = plane;
  myCallback->Actor = actor;

  vtkNew<vtkImplicitPlaneRepresentation> rep;
  rep->SetPlaceFactor(1.25); // This must be set prior to placing the widget
  rep->PlaceWidget(actor->GetBounds());
  rep->SetNormal(plane->GetNormal());

  vtkNew<vtkImplicitPlaneWidget2> planeWidget;
  planeWidget->SetInteractor(renderWindowInteractor);
  planeWidget->SetRepresentation(rep);
  planeWidget->AddObserver(vtkCommand::InteractionEvent, myCallback);

  // Render

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

  // Begin mouse interaction
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(ImplicitPlaneWidget2)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkFiltersCore
  vtkFiltersSources
  vtkIOXML
  vtkInteractionStyle
  vtkInteractionWidgets
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping ImplicitPlaneWidget2: ${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(ImplicitPlaneWidget2 MACOSX_BUNDLE ImplicitPlaneWidget2.cxx )
  target_link_libraries(ImplicitPlaneWidget2 PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(ImplicitPlaneWidget2 MACOSX_BUNDLE ImplicitPlaneWidget2.cxx )
  target_link_libraries(ImplicitPlaneWidget2 PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS ImplicitPlaneWidget2
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build ImplicitPlaneWidget2

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

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

./ImplicitPlaneWidget2

WINDOWS USERS

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