AreaPicking
Repository source: AreaPicking
Description¶
This example creates 3 points+vertices. Currently, the address of the picked prop is 0 (this is not correct). A red bounding box is drawn around every picked prop.
-
For
[vtkInteractorStyleTrackballCamera](https://www.vtk.org/doc/nightly/html/classvtkInteractorStyleTrackballCamera.html)
- use 'p' to pick at the current mouse position -
For
[vtkInteractorStyleRubberBandPick](https://www.vtk.org/doc/nightly/html/classvtkInteractorStyleRubberBandPick.html)
- use 'r' and left-mouse to draw a selection box used to pick
Other languages
See (PythonicAPI)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
AreaPicking.cxx
#include <vtkActor.h>
#include <vtkAreaPicker.h>
#include <vtkCallbackCommand.h>
#include <vtkCellArray.h>
#include <vtkInteractorStyleRubberBandPick.h>
#include <vtkInteractorStyleTrackball.h>
// #include <vtkInteractorStyleTrackballCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProp3DCollection.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
namespace {
void PickCallbackFunction(vtkObject* caller, long unsigned int eventId,
void* clientData, void* callData);
}
int main(int, char*[])
{
// Create a set of points.
vtkNew<vtkPoints> points;
vtkNew<vtkCellArray> vertices;
vtkIdType pid[1];
pid[0] = points->InsertNextPoint(1.0, 0.0, 0.0);
vertices->InsertNextCell(1, pid);
pid[0] = points->InsertNextPoint(0.0, 0.0, 0.0);
vertices->InsertNextCell(1, pid);
pid[0] = points->InsertNextPoint(0.0, 1.0, 0.0);
vertices->InsertNextCell(1, pid);
// Create a polydata
vtkNew<vtkPolyData> polydata;
polydata->SetPoints(points);
polydata->SetVerts(vertices);
// Visualize
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputData(polydata);
vtkNew<vtkNamedColors> colors;
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
actor->GetProperty()->SetPointSize(8);
actor->GetProperty()->SetColor(colors->GetColor3d("Gold").GetData());
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("AreaPicking");
vtkNew<vtkAreaPicker> areaPicker;
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->SetPicker(areaPicker);
renderer->AddActor(actor);
renderer->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());
renderWindow->Render();
// For vtkInteractorStyleRubberBandPick - use 'r' and left-mouse to draw a
// selection box used to pick.
vtkNew<vtkInteractorStyleRubberBandPick> style;
// For vtkInteractorStyleTrackballCamera - use 'p' to pick at the current
// mouse position.
// vtkNew<vtkInteractorStyleTrackballCamera> style;
// paraview
style->SetCurrentRenderer(renderer);
renderWindowInteractor->SetInteractorStyle(style);
vtkNew<vtkCallbackCommand> pickCallback;
pickCallback->SetCallback(PickCallbackFunction);
areaPicker->AddObserver(vtkCommand::EndPickEvent, pickCallback);
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
namespace {
void PickCallbackFunction(vtkObject* caller,
long unsigned int vtkNotUsed(eventId),
void* vtkNotUsed(clientData),
void* vtkNotUsed(callData))
{
std::cout << "Pick." << std::endl;
vtkAreaPicker* areaPicker = static_cast<vtkAreaPicker*>(caller);
vtkProp3DCollection* props = areaPicker->GetProp3Ds();
props->InitTraversal();
for (vtkIdType i = 0; i < props->GetNumberOfItems(); i++)
{
vtkProp3D const* prop = props->GetNextProp3D();
std::cout << "Picked prop: " << prop << std::endl;
}
}
} // namespace
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(AreaPicking)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "AreaPicking: 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(AreaPicking MACOSX_BUNDLE AreaPicking.cxx )
target_link_libraries(AreaPicking PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS AreaPicking
MODULES ${VTK_LIBRARIES}
)
Download and Build AreaPicking¶
Click here to download AreaPicking and its CMakeLists.txt file. Once the tarball AreaPicking.tar has been downloaded and extracted,
cd AreaPicking/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:
./AreaPicking
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.