BoxWidget
Repository source: BoxWidget
Description¶
This example uses a vtkBoxWidget to manipulate an actor. The class includes geometry to draw a box around the object, which is bound to it via SetProp3D. The box is dimensioned using the SetPlaceFactor
method, and positioned with the PlaceWidget
method. After the initial setup, synchronizing the box with the object is done through a custom callback class, which is passed to the box widget through the AddObserver
method.
For a widget that isn't tied to a specific vtkProp3D and has its geometric representation separate from the interaction logic, see the example BoxWidget2.
Other languages
See (Python), (PythonicAPI)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
BoxWidget.cxx
#include <vtkActor.h>
#include <vtkBoxWidget.h>
#include <vtkCommand.h>
#include <vtkConeSource.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTransform.h>
namespace {
class vtkMyCallback : public vtkCommand
{
public:
static vtkMyCallback* New()
{
return new vtkMyCallback;
}
virtual void Execute(vtkObject* caller, unsigned long, void*)
{
// Here we use the vtkBoxWidget to transform the underlying coneActor
// (by manipulating its transformation matrix).
vtkNew<vtkTransform> t;
vtkBoxWidget* widget = reinterpret_cast<vtkBoxWidget*>(caller);
widget->GetTransform(t);
widget->GetProp3D()->SetUserTransform(t);
}
};
} // namespace
int main(int vtkNotUsed(argc), char* vtkNotUsed(argv)[])
{
vtkNew<vtkNamedColors> colors;
vtkNew<vtkConeSource> cone;
vtkNew<vtkPolyDataMapper> coneMapper;
coneMapper->SetInputConnection(cone->GetOutputPort());
vtkNew<vtkActor> coneActor;
coneActor->SetMapper(coneMapper);
coneActor->GetProperty()->SetColor(colors->GetColor3d("BurlyWood").GetData());
vtkNew<vtkRenderer> renderer;
renderer->AddActor(coneActor);
renderer->SetBackground(colors->GetColor3d("Blue").GetData());
vtkNew<vtkRenderWindow> window;
window->AddRenderer(renderer);
window->SetSize(300, 300);
window->SetWindowName("BoxWidget");
vtkNew<vtkRenderWindowInteractor> interactor;
interactor->SetRenderWindow(window);
vtkNew<vtkInteractorStyleTrackballCamera> style;
interactor->SetInteractorStyle(style);
vtkNew<vtkBoxWidget> boxWidget;
boxWidget->SetInteractor(interactor);
boxWidget->SetProp3D(coneActor);
boxWidget->SetPlaceFactor(1.25); // Make the box 1.25x larger than the actor
boxWidget->PlaceWidget();
vtkNew<vtkMyCallback> callback;
boxWidget->AddObserver(vtkCommand::InteractionEvent, callback);
boxWidget->On();
window->Render();
interactor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(BoxWidget)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonTransforms
FiltersSources
InteractionStyle
InteractionWidgets
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "BoxWidget: 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(BoxWidget MACOSX_BUNDLE BoxWidget.cxx )
target_link_libraries(BoxWidget PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS BoxWidget
MODULES ${VTK_LIBRARIES}
)
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
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.