Skip to content

SelectionSource

web-test/Cxx/Filtering/SelectionSource

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

SelectionSource.cxx

#include <vtkExtractSelection.h>
#include <vtkNew.h>
#include <vtkPointSource.h>
#include <vtkSelectionNode.h> // for POINT and INDICES enum values
#include <vtkSelectionSource.h>

int main(int, char*[])
{
  // Note - this generates 50 points and a single poly-vertex cell.
  vtkNew<vtkPointSource> pointSource;
  pointSource->SetNumberOfPoints(50);
  pointSource->Update();

  std::cout << "There are " << pointSource->GetOutput()->GetNumberOfPoints()
            << " input points." << std::endl;

  vtkNew<vtkSelectionSource> selectionSource;
  selectionSource->SetFieldType(vtkSelectionNode::POINT);
  selectionSource->SetContentType(vtkSelectionNode::INDICES);

  // Without this line, all points are passed through because the 11 points
  // we will select below are some of the points of the poly-vertex created
  // by the PointSource, so the cell (by default) gets passed through since
  // it contains some selected points, so therefore all of the points
  // (the 50 belonging to the poly-vertex) also get passed through, which
  // is not what we are trying to demonstrate.
  selectionSource->SetContainingCells(false);

  for (vtkIdType i = 10; i <= 20; i++)
  {
    selectionSource->AddID(0, i);
  }

  selectionSource->Update();

  vtkNew<vtkExtractSelection> extractSelection;
  extractSelection->SetInputConnection(0, pointSource->GetOutputPort());
  extractSelection->SetInputConnection(1, selectionSource->GetOutputPort());
  extractSelection->Update();

  vtkDataSet* ds = dynamic_cast<vtkDataSet*>(extractSelection->GetOutput());

  std::cout << "There are " << ds->GetNumberOfPoints() << " output points."
            << std::endl;

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(SelectionSource)

find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
  FiltersExtraction
  FiltersSources
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "SelectionSource: 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(SelectionSource MACOSX_BUNDLE SelectionSource.cxx )
  target_link_libraries(SelectionSource PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS SelectionSource
  MODULES ${VTK_LIBRARIES}
)

Download and Build SelectionSource

Click here to download SelectionSource and its CMakeLists.txt file. Once the tarball SelectionSource.tar has been downloaded and extracted,

cd SelectionSource/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:

./SelectionSource

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.