CellPicking
vtk-examples/Cxx/Picking/CellPicking
Description¶
This example demonstrates how to get the coordinates of the point on an actor that is clicked with the left mouse button. It also indicates which cell the selected point belongs to by highlighting the edges of that cell.
Other languages
See (Python)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
CellPicking.cxx
#include <vtkActor.h>
#include <vtkCellArray.h>
#include <vtkCellPicker.h>
#include <vtkCommand.h>
#include <vtkDataSetMapper.h>
#include <vtkExtractSelection.h>
#include <vtkIdTypeArray.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkObjectFactory.h>
#include <vtkPlaneSource.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkRendererCollection.h>
#include <vtkSelection.h>
#include <vtkSelectionNode.h>
#include <vtkSmartPointer.h>
#include <vtkTriangleFilter.h>
#include <vtkUnstructuredGrid.h>
namespace {
// Catch mouse events
class MouseInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
static MouseInteractorStyle* New();
MouseInteractorStyle()
{
selectedMapper = vtkSmartPointer<vtkDataSetMapper>::New();
selectedActor = vtkSmartPointer<vtkActor>::New();
}
virtual void OnLeftButtonDown() override
{
vtkNew<vtkNamedColors> colors;
// Get the location of the click (in window coordinates)
int* pos = this->GetInteractor()->GetEventPosition();
vtkNew<vtkCellPicker> picker;
picker->SetTolerance(0.0005);
// Pick from this location.
picker->Pick(pos[0], pos[1], 0, this->GetDefaultRenderer());
double* worldPosition = picker->GetPickPosition();
std::cout << "Cell id is: " << picker->GetCellId() << std::endl;
if (picker->GetCellId() != -1)
{
std::cout << "Pick position is: (" << worldPosition[0] << ", "
<< worldPosition[1] << ", " << worldPosition[2] << ")" << endl;
vtkNew<vtkIdTypeArray> ids;
ids->SetNumberOfComponents(1);
ids->InsertNextValue(picker->GetCellId());
vtkNew<vtkSelectionNode> selectionNode;
selectionNode->SetFieldType(vtkSelectionNode::CELL);
selectionNode->SetContentType(vtkSelectionNode::INDICES);
selectionNode->SetSelectionList(ids);
vtkNew<vtkSelection> selection;
selection->AddNode(selectionNode);
vtkNew<vtkExtractSelection> extractSelection;
extractSelection->SetInputData(0, this->Data);
extractSelection->SetInputData(1, selection);
extractSelection->Update();
// In selection
vtkNew<vtkUnstructuredGrid> selected;
selected->ShallowCopy(extractSelection->GetOutput());
std::cout << "Number of points in the selection: "
<< selected->GetNumberOfPoints() << std::endl;
std::cout << "Number of cells in the selection : "
<< selected->GetNumberOfCells() << std::endl;
selectedMapper->SetInputData(selected);
selectedActor->SetMapper(selectedMapper);
selectedActor->GetProperty()->EdgeVisibilityOn();
selectedActor->GetProperty()->SetColor(
colors->GetColor3d("Tomato").GetData());
selectedActor->GetProperty()->SetLineWidth(3);
this->Interactor->GetRenderWindow()
->GetRenderers()
->GetFirstRenderer()
->AddActor(selectedActor);
}
// Forward events
vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}
vtkSmartPointer<vtkPolyData> Data;
vtkSmartPointer<vtkDataSetMapper> selectedMapper;
vtkSmartPointer<vtkActor> selectedActor;
};
vtkStandardNewMacro(MouseInteractorStyle);
} // namespace
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
vtkNew<vtkPlaneSource> planeSource;
planeSource->Update();
vtkNew<vtkTriangleFilter> triangleFilter;
triangleFilter->SetInputConnection(planeSource->GetOutputPort());
triangleFilter->Update();
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(triangleFilter->GetOutputPort());
vtkNew<vtkActor> actor;
actor->GetProperty()->SetColor(colors->GetColor3d("SeaGreen").GetData());
actor->SetMapper(mapper);
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("CellPicking");
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
// Set the custom stype to use for interaction.
vtkNew<MouseInteractorStyle> style;
style->SetDefaultRenderer(renderer);
style->Data = triangleFilter->GetOutput();
renderWindowInteractor->SetInteractorStyle(style);
renderer->AddActor(actor);
renderer->ResetCamera();
renderer->SetBackground(colors->GetColor3d("PaleTurquoise").GetData());
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(CellPicking)
find_package(VTK COMPONENTS
vtkCommonColor
vtkCommonCore
vtkCommonDataModel
vtkFiltersCore
vtkFiltersExtraction
vtkFiltersSources
vtkInteractionStyle
vtkRenderingContextOpenGL2
vtkRenderingCore
vtkRenderingFreeType
vtkRenderingGL2PSOpenGL2
vtkRenderingOpenGL2
QUIET
)
if (NOT VTK_FOUND)
message("Skipping CellPicking: ${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(CellPicking MACOSX_BUNDLE CellPicking.cxx )
target_link_libraries(CellPicking PRIVATE ${VTK_LIBRARIES})
else()
# 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(CellPicking MACOSX_BUNDLE CellPicking.cxx )
target_link_libraries(CellPicking PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS CellPicking
MODULES ${VTK_LIBRARIES}
)
endif()
Download and Build CellPicking¶
Click here to download CellPicking and its CMakeLists.txt file. Once the tarball CellPicking.tar has been downloaded and extracted,
cd CellPicking/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:
./CellPicking
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.