ExtractSelectionOriginalId
Repository source: ExtractSelectionOriginalId
Other languages
See (PythonicAPI)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
ExtractSelectionOriginalId.cxx
#include <vtkCamera.h>
#include <vtkDataSetMapper.h>
#include <vtkExtractSelection.h>
#include <vtkIdTypeArray.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPointSource.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSelection.h>
#include <vtkSelectionNode.h>
#include <vtkUnstructuredGrid.h>
#include <iostream>
#include <string>
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
vtkNew<vtkPointSource> pointSource;
pointSource->SetNumberOfPoints(50);
pointSource->Update();
std::cout << "There are " << pointSource->GetOutput()->GetNumberOfPoints()
<< " input points." << std::endl;
vtkNew<vtkIdTypeArray> ids;
ids->SetNumberOfComponents(1);
// set values
for (unsigned int i = 10; i < 20; i++)
{
ids->InsertNextValue(i);
}
vtkNew<vtkSelectionNode> selectionNode;
selectionNode->SetFieldType(vtkSelectionNode::POINT);
selectionNode->SetContentType(vtkSelectionNode::INDICES);
selectionNode->SetSelectionList(ids);
vtkNew<vtkSelection> selection;
selection->AddNode(selectionNode);
vtkNew<vtkExtractSelection> extractSelection;
extractSelection->SetInputConnection(0, pointSource->GetOutputPort());
extractSelection->SetInputData(1, selection);
extractSelection->Update();
// in selection
vtkNew<vtkUnstructuredGrid> selected;
selected->ShallowCopy(extractSelection->GetOutput());
std::cout << "There are " << selected->GetNumberOfPoints()
<< " points in the selection." << std::endl;
selected->Print(std::cout);
vtkIdTypeArray* originalIds = dynamic_cast<vtkIdTypeArray*>(
selected->GetPointData()->GetArray("vtkOriginalPointIds"));
for (vtkIdType i = 0; i < originalIds->GetNumberOfTuples(); i++)
{
std::cout << "Point " << i << " was originally point "
<< originalIds->GetValue(i) << std::endl;
}
for (vtkIdType i = 0; i < originalIds->GetNumberOfTuples(); i++)
{
vtkIdType query = originalIds->GetValue(i);
for (vtkIdType j = 0; j < originalIds->GetNumberOfTuples(); j++)
{
if (originalIds->GetValue(j) == query)
{
std::cout << "Original point " << query << " is now " << j << std::endl;
}
}
}
vtkNew<vtkDataSetMapper> inputMapper;
inputMapper->SetInputConnection(pointSource->GetOutputPort());
vtkNew<vtkActor> inputActor;
inputActor->SetMapper(inputMapper);
inputActor->GetProperty()->SetColor(
colors->GetColor3d("MidnightBlue").GetData());
inputActor->GetProperty()->SetPointSize(5);
vtkNew<vtkDataSetMapper> selectedMapper;
selectedMapper->SetInputData(selected);
vtkNew<vtkActor> selectedActor;
selectedActor->SetMapper(selectedMapper);
selectedActor->GetProperty()->SetColor(
colors->GetColor3d("MidnightBlue").GetData());
selectedActor->GetProperty()->SetPointSize(5);
// There will be one render window.
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetSize(600, 300);
renderWindow->SetWindowName("ExtractSelectionOriginalId");
// And one interactor.
vtkNew<vtkRenderWindowInteractor> interactor;
interactor->SetRenderWindow(renderWindow);
// Define viewport ranges.
// (xmin, ymin, xmax, ymax)
double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};
// Create a camera for all renderers.
vtkNew<vtkCamera> camera;
// Setup the renderers.
vtkNew<vtkRenderer> leftRenderer;
renderWindow->AddRenderer(leftRenderer);
leftRenderer->SetViewport(leftViewport);
leftRenderer->SetBackground(colors->GetColor3d("BurlyWood").GetData());
leftRenderer->SetActiveCamera(camera);
vtkNew<vtkRenderer> rightRenderer;
renderWindow->AddRenderer(rightRenderer);
rightRenderer->SetViewport(rightViewport);
rightRenderer->SetBackground(colors->GetColor3d("CornflowerBlue").GetData());
rightRenderer->SetActiveCamera(camera);
leftRenderer->AddActor(inputActor);
rightRenderer->AddActor(selectedActor);
leftRenderer->ResetCamera();
// rightRenderer->ResetCamera();
renderWindow->Render();
interactor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(ExtractSelectionOriginalId)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
FiltersExtraction
FiltersSources
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "ExtractSelectionOriginalId: 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(ExtractSelectionOriginalId MACOSX_BUNDLE ExtractSelectionOriginalId.cxx )
target_link_libraries(ExtractSelectionOriginalId PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS ExtractSelectionOriginalId
MODULES ${VTK_LIBRARIES}
)
Download and Build ExtractSelectionOriginalId¶
Click here to download ExtractSelectionOriginalId and its CMakeLists.txt file. Once the tarball ExtractSelectionOriginalId.tar has been downloaded and extracted,
cd ExtractSelectionOriginalId/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:
./ExtractSelectionOriginalId
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.