VTK/Examples/Cxx/Widgets/BoxWidget

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Revision as of 07:06, 8 February 2016 by Waldyrious (talk | contribs) (→‎BoxWidget.cxx: reorder includes; remove cone customization, which is superfluous for this example)
Jump to navigationJump to search
VTK Examples Baseline Widgets TestBoxWidget.png

BoxWidget.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 vtkBoxWidget:
#include <vtkBoxWidget.h>
#include <vtkCommand.h>
#include <vtkTransform.h>

class vtkMyCallback : public vtkCommand
{
public:
  static vtkMyCallback *New()
  {
    return new vtkMyCallback;
  }
  virtual void Execute( vtkObject *caller, unsigned long, void* )
  {
    vtkSmartPointer<vtkTransform> t =
      vtkSmartPointer<vtkTransform>::New();
    vtkBoxWidget *widget = reinterpret_cast<vtkBoxWidget*>(caller);
    widget->GetTransform( t );
    widget->GetProp3D()->SetUserTransform( t );
  }
};

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

  vtkSmartPointer<vtkPolyDataMapper> coneMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  coneMapper->SetInputConnection( cone->GetOutputPort() );

  vtkSmartPointer<vtkActor> coneActor =
    vtkSmartPointer<vtkActor>::New();
  coneActor->SetMapper( coneMapper );

  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  renderer->AddActor( coneActor );
  renderer->SetBackground( 0.1, 0.2, 0.4 );

  vtkSmartPointer<vtkRenderWindow> window =
    vtkSmartPointer<vtkRenderWindow>::New();
  window->AddRenderer( renderer );
  window->SetSize( 300, 300 );

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

  vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
    vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
  interactor->SetInteractorStyle( style );

  // Here we use a vtkBoxWidget to transform the underlying coneActor (by
  // manipulating its transformation matrix).

  // The place factor controls the initial size of the widget
  // with respect to the bounding box of the input to the widget.
  vtkSmartPointer<vtkBoxWidget> boxWidget =
    vtkSmartPointer<vtkBoxWidget>::New();
  boxWidget->SetInteractor( interactor );
  boxWidget->SetPlaceFactor( 1.25 );

  boxWidget->SetProp3D( coneActor );
  boxWidget->PlaceWidget();
  vtkSmartPointer<vtkMyCallback> callback =
    vtkSmartPointer<vtkMyCallback>::New();
  boxWidget->AddObserver( vtkCommand::InteractionEvent, callback );

  boxWidget->On();

  interactor->Initialize();
  interactor->Start();

  return EXIT_SUCCESS;
}

Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(BoxWidget)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

add_executable(BoxWidget MACOSX_BUNDLE BoxWidget.cxx)

if(VTK_LIBRARIES)
  target_link_libraries(BoxWidget ${VTK_LIBRARIES})
else()
  target_link_libraries(BoxWidget vtkHybrid vtkWidgets)
endif()

Download and Build BoxWidget

Click here to download BoxWidget. and its CMakeLists.txt file.

Once the tarball BoxWidget.tar has been downloaded and extracted,

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

./BoxWidget

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