VTK/Examples/Cxx/Widgets/BoxWidget2

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Revision as of 07:47, 8 February 2016 by Waldyrious (talk | contribs) (→‎BoxWidget2.cxx: overhaul example so it actually displays and manipulates an object)
Jump to navigationJump to search
VTK Examples Baseline Widgets TestBoxWidget2.png

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 vtkBoxCallback::SetActor( vtkSmartPointer<vtkActor> actor )
  {
    m_actor = actor;
  }

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

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

    vtkBoxRepresentation::SafeDownCast( 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();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(BoxWidget2)

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

add_executable(BoxWidget2 MACOSX_BUNDLE BoxWidget2.cxx)

if(VTK_LIBRARIES)
  target_link_libraries(BoxWidget2 ${VTK_LIBRARIES})
else()
  target_link_libraries(BoxWidget2 vtkHybrid vtkWidgets)
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 PLEASE NOTE: Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.

Background color - show the box relative to something else?