SphereWidgetEvents
VTKEx/Cxx/Widgets/SphereWidgetEvents
Description¶
This example shows how to subclass a widget so that events can be further customized.
Contributed by: Alex Malyushytskyy.
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¶
SphereWidgetEvents.cxx
#include <vtkCallbackCommand.h>
#include <vtkCommand.h>
#include <vtkObjectFactory.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkSphereWidget.h>
#include <vtkSphereRepresentation.h>
class MySphereWidget : public vtkSphereWidget
{
public:
static MySphereWidget* New();
vtkTypeMacro(MySphereWidget, vtkSphereWidget);
// Handles the events
static void ProcessEvents(vtkObject* object,
unsigned long event,
void* clientdata,
void* calldata);
protected:
MySphereWidget();
// Create a new callback command rather than using the one defined in
// vtkSphereWidget. This prevents problems with unexpected behavior due
// to the AbortFlag being manipulated.
vtkSmartPointer<vtkCallbackCommand> MyEventCallbackCommand;
};
MySphereWidget::MySphereWidget()
{
this->MyEventCallbackCommand = vtkSmartPointer<vtkCallbackCommand>::New();
// Connect our own callback command to our own ProcessEvents function.
// This way we do not have to deal with side effects of SphereWidget::ProcessEvents
this->MyEventCallbackCommand->SetCallback( MySphereWidget::ProcessEvents );
// Connect our callback function to a few events.
this->AddObserver(vtkCommand::StartInteractionEvent,
this->MyEventCallbackCommand);
this->AddObserver(vtkCommand::InteractionEvent,
this->MyEventCallbackCommand);
this->AddObserver(vtkCommand::EndInteractionEvent,
this->MyEventCallbackCommand);
}
void MySphereWidget::ProcessEvents( vtkObject *vtkNotUsed(object),
unsigned long event,
void *vtkNotUsed(clientdata),
void *vtkNotUsed(calldata) )
{
switch(event)
{
case vtkCommand::StartInteractionEvent:
std::cout << "StartInteractionEvent" << std::endl;
break;
case vtkCommand::EndInteractionEvent:
std::cout << "EndInteractionEvent" << std::endl;
break;
case vtkCommand::InteractionEvent:
std::cout << "InteractionEvent" << std::endl;
break;
}
}
vtkStandardNewMacro(MySphereWidget);
int main(int, char *[])
{
// Create a renderer and render window
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
// Create an interactor
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
vtkSmartPointer<MySphereWidget> sphereWidget =
vtkSmartPointer<MySphereWidget>::New();
sphereWidget->SetInteractor(renderWindowInteractor);
sphereWidget->SetRepresentationToSurface();
sphereWidget->HandleVisibilityOn();
renderWindow->Render();
renderWindowInteractor->Initialize();
renderWindow->Render();
sphereWidget->On();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(SphereWidgetEvents)
find_package(VTK COMPONENTS
vtkvtkCommonCore
vtkvtkInteractionStyle
vtkvtkInteractionWidgets
vtkvtkRenderingContextOpenGL2
vtkvtkRenderingCore
vtkvtkRenderingFreeType
vtkvtkRenderingGL2PSOpenGL2
vtkvtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
message("Skipping SphereWidgetEvents: ${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(SphereWidgetEvents MACOSX_BUNDLE SphereWidgetEvents.cxx )
target_link_libraries(SphereWidgetEvents PRIVATE ${VTK_LIBRARIES})
else ()
# include all components
add_executable(SphereWidgetEvents MACOSX_BUNDLE SphereWidgetEvents.cxx )
target_link_libraries(SphereWidgetEvents PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS SphereWidgetEvents
MODULES ${VTK_LIBRARIES}
)
endif ()
Download and Build SphereWidgetEvents¶
Click here to download SphereWidgetEvents and its CMakeLists.txt file. Once the tarball SphereWidgetEvents.tar has been downloaded and extracted,
cd SphereWidgetEvents/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:
./SphereWidgetEvents
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.