SelectAnActor
Repository source: SelectAnActor
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
SelectAnActor.cxx
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCubeSource.h>
#include <vtkInteractorStyleTrackballActor.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
namespace {
// Handle mouse events.
class MouseInteractorStyle5 : public vtkInteractorStyleTrackballActor
{
public:
static MouseInteractorStyle5* New();
vtkTypeMacro(MouseInteractorStyle5, vtkInteractorStyleTrackballActor);
virtual void OnLeftButtonDown() override
{
// Forward events.
vtkInteractorStyleTrackballActor::OnLeftButtonDown();
if (this->InteractionProp == this->Cube)
{
std::cout << "Picked cube." << std::endl;
}
else if (this->InteractionProp == this->Sphere)
{
std::cout << "Picked sphere." << std::endl;
}
}
vtkActor* Cube;
vtkActor* Sphere;
};
vtkStandardNewMacro(MouseInteractorStyle5);
} // namespace
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
// Create a cube.
vtkNew<vtkCubeSource> cubeSource;
cubeSource->Update();
vtkNew<vtkPolyDataMapper> cubeMapper;
cubeMapper->SetInputConnection(cubeSource->GetOutputPort());
vtkNew<vtkActor> cubeActor;
cubeActor->SetMapper(cubeMapper);
cubeActor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());
// Create a sphere.
vtkNew<vtkSphereSource> sphereSource;
sphereSource->SetCenter(2, 0, 0);
sphereSource->Update();
// Create a mapper.
vtkNew<vtkPolyDataMapper> sphereMapper;
sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
// Create an actor.
vtkNew<vtkActor> sphereActor;
sphereActor->SetMapper(sphereMapper);
sphereActor->GetProperty()->SetColor(
colors->GetColor3d("LightGoldenrodYellow").GetData());
// A renderer and render window.
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("SelectAnActor");
// An interactor.
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
// Set the custom stype to use for interaction.
vtkNew<MouseInteractorStyle5> style;
style->SetDefaultRenderer(renderer);
style->Cube = cubeActor;
style->Sphere = sphereActor;
renderWindowInteractor->SetInteractorStyle(style);
renderer->AddActor(cubeActor);
renderer->AddActor(sphereActor);
renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
renderer->ResetCamera();
renderer->GetActiveCamera()->Zoom(0.9);
// Render and interact.
renderWindow->Render();
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(SelectAnActor)
find_package(VTK COMPONENTS
CommonColor
CommonCore
FiltersSources
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "SelectAnActor: 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(SelectAnActor MACOSX_BUNDLE SelectAnActor.cxx )
target_link_libraries(SelectAnActor PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS SelectAnActor
MODULES ${VTK_LIBRARIES}
)
Download and Build SelectAnActor¶
Click here to download SelectAnActor and its CMakeLists.txt file. Once the tarball SelectAnActor.tar has been downloaded and extracted,
cd SelectAnActor/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:
./SelectAnActor
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.