CellPicking
Repository source: 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), (PythonicAPI)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
CellPicking.cxx
#include <vtkActor.h>
#include <vtkCellPicker.h>
#include <vtkDataSetMapper.h>
#include <vtkExtractSelection.h>
#include <vtkIdTypeArray.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPlaneSource.h>
#include <vtkPoints.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());
const 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
CommonColor
CommonCore
CommonDataModel
FiltersCore
FiltersExtraction
FiltersSources
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "CellPicking: 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(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}
)
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.