Skip to content


# BoxWidget2

VTKEx/Cxx/Widgets/BoxWidget2

### Description

This example uses a vtkBoxWidget2 to manipulate an actor. The widget only contains the interaction logic; the actual box is drawn by the accompanying vtkBoxRepresentation. Contrary to the older vtkBoxWidget, this widget doesn't provide functionality to assign it to one or more actors, so that has to be implemented manually. The box is dimensioned and positioned by passing a bounding box to `PlaceWidget` method, with the `SetPlaceFactor` method providing a scaling factor in relation to that bounding box. The transformations applied to the box can be used to manipulate any number of object(s), via a custom callback class, which is passed to the box widget through the `AddObserver` method.

The older implementation vtkBoxWidget provides functionality to receive a vtkProp3D for the initial positioning and sizing, but the transformation synchronization still needs to be done manually. See BoxWidget for a simple example of how to use it.

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

BoxWidget2.cxx

    #include <vtkSmartPointer.h>
    // For the rendering pipeline setup:
    #include <vtkConeSource.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkActor.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkInteractorStyleTrackballCamera.h>
    // For vtkBoxWidget2:
    #include <vtkBoxWidget2.h>
    #include <vtkBoxRepresentation.h>
    #include <vtkCommand.h>
    #include <vtkTransform.h>

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

      vtkSmartPointer<vtkActor> m_actor;

      void SetActor( vtkSmartPointer<vtkActor> actor )
      {
        m_actor = actor;
      }

      virtual void Execute( vtkObject *caller, unsigned long, void* )
      {
        vtkSmartPointer<vtkBoxWidget2> boxWidget =
          dynamic_cast<vtkBoxWidget2*>(caller);

        vtkSmartPointer<vtkTransform> t =
          vtkSmartPointer<vtkTransform>::New();

        dynamic_cast<vtkBoxRepresentation*>( boxWidget->GetRepresentation() )->GetTransform( t );
        this->m_actor->SetUserTransform( t );
      }

      vtkBoxCallback(){}
    };

    int main( int vtkNotUsed( argc ), char* vtkNotUsed( argv )[] )
    {
      vtkSmartPointer<vtkConeSource> coneSource =
        vtkSmartPointer<vtkConeSource>::New();
      coneSource->SetHeight( 1.5 );

      vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      mapper->SetInputConnection( coneSource->GetOutputPort() );

      vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
      actor->SetMapper( mapper );

      vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
      renderer->AddActor(actor);
      renderer->ResetCamera(); // Reposition camera so the whole scene is visible

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

      vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      renderWindowInteractor->SetRenderWindow( renderWindow );

      // Use the "trackball camera" interactor style, rather than the default "joystick camera"
      vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
        vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
      renderWindowInteractor->SetInteractorStyle( style );

      vtkSmartPointer<vtkBoxWidget2> boxWidget =
        vtkSmartPointer<vtkBoxWidget2>::New();
      boxWidget->SetInteractor( renderWindowInteractor );
      boxWidget->GetRepresentation()->SetPlaceFactor( 1 ); // Default is 0.5
      boxWidget->GetRepresentation()->PlaceWidget(actor->GetBounds());

      // Set up a callback for the interactor to call so we can manipulate the actor
      vtkSmartPointer<vtkBoxCallback> boxCallback =
        vtkSmartPointer<vtkBoxCallback>::New();
      boxCallback->SetActor(actor);
      boxWidget->AddObserver( vtkCommand::InteractionEvent, boxCallback );

      boxWidget->On();

      renderWindow->Render();
      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(BoxWidget2)

    find_package(VTK COMPONENTS
      vtkCommonCore
      vtkCommonTransforms
      vtkFiltersSources
      vtkInteractionStyle
      vtkInteractionWidgets
      vtkRenderingContextOpenGL2
      vtkRenderingCore
      vtkRenderingFreeType
      vtkRenderingGL2PSOpenGL2
      vtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping BoxWidget2: ${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(BoxWidget2 MACOSX_BUNDLE BoxWidget2.cxx )
      target_link_libraries(BoxWidget2 PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(BoxWidget2 MACOSX_BUNDLE BoxWidget2.cxx )
      target_link_libraries(BoxWidget2 PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS BoxWidget2
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build BoxWidget2

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

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

    ./BoxWidget2

WINDOWS USERS

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