Picking
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¶
Picking.cxx
#include <vtkSmartPointer.h>
#include <vtkActor.h>
#include <vtkSphereSource.h>
#include <vtkRendererCollection.h>
#include <vtkCellArray.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkObjectFactory.h>
#include <vtkPlaneSource.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkPropPicker.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
// Handle mouse events
class MouseInteractorStyle2 : public vtkInteractorStyleTrackballCamera
{
public:
static MouseInteractorStyle2* New();
vtkTypeMacro(MouseInteractorStyle2, vtkInteractorStyleTrackballCamera);
virtual void OnLeftButtonDown() override
{
int* clickPos = this->GetInteractor()->GetEventPosition();
// Pick from this location.
vtkSmartPointer<vtkPropPicker> picker =
vtkSmartPointer<vtkPropPicker>::New();
picker->Pick(clickPos[0], clickPos[1], 0, this->GetDefaultRenderer());
double* pos = picker->GetPickPosition();
std::cout << "Pick position (world coordinates) is: "
<< pos[0] << " " << pos[1]
<< " " << pos[2] << std::endl;
std::cout << "Picked actor: " << picker->GetActor() << std::endl;
//Create a sphere
vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();
sphereSource->SetCenter(pos[0], pos[1], pos[2]);
sphereSource->SetRadius(0.1);
//Create a mapper and actor
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(sphereSource->GetOutputPort());
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
this->GetDefaultRenderer()->AddActor(actor);
// Forward events
vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}
private:
};
vtkStandardNewMacro(MouseInteractorStyle2);
// Execute application.
int main(int, char *[])
{
vtkSmartPointer<vtkPlaneSource> planeSource =
vtkSmartPointer<vtkPlaneSource>::New();
planeSource->Update();
// Create a polydata object
vtkPolyData* polydata = planeSource->GetOutput();
// Create a mapper
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData ( polydata );
// Create an actor
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper ( mapper );
std::cout << "Actor address: " << actor << std::endl;
// A renderer and render window
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer ( renderer );
// An interactor
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow ( renderWindow );
// Set the custom stype to use for interaction.
vtkSmartPointer<MouseInteractorStyle2> style =
vtkSmartPointer<MouseInteractorStyle2>::New();
style->SetDefaultRenderer(renderer);
renderWindowInteractor->SetInteractorStyle( style );
// Add the actors to the scene
renderer->AddActor ( actor );
renderer->SetBackground ( 0,0,1 );
// Render and interact
renderWindow->Render();
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(Picking)
find_package(VTK COMPONENTS
vtkvtkCommonCore
vtkvtkCommonDataModel
vtkvtkFiltersSources
vtkvtkInteractionStyle
vtkvtkRenderingContextOpenGL2
vtkvtkRenderingCore
vtkvtkRenderingFreeType
vtkvtkRenderingGL2PSOpenGL2
vtkvtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
message("Skipping Picking: ${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(Picking MACOSX_BUNDLE Picking.cxx )
target_link_libraries(Picking PRIVATE ${VTK_LIBRARIES})
else ()
# include all components
add_executable(Picking MACOSX_BUNDLE Picking.cxx )
target_link_libraries(Picking PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS Picking
MODULES ${VTK_LIBRARIES}
)
endif ()
Download and Build Picking¶
Click here to download Picking and its CMakeLists.txt file. Once the tarball Picking.tar has been downloaded and extracted,
cd Picking/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:
./Picking
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.