Skip to content

ThresholdPoints

vtk-examples/Cxx/PolyData/ThresholdPoints

Question

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

Code

ThresholdPoints.cxx

#include <vtkIntArray.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkThresholdPoints.h>

#include <iostream>
#include <string>

int main(int, char*[])
{
  vtkNew<vtkPoints> points;
  points->InsertNextPoint(0, 0, 0);
  points->InsertNextPoint(1, 1, 1);
  points->InsertNextPoint(2, 2, 2);
  points->InsertNextPoint(3, 3, 3);
  points->InsertNextPoint(4, 4, 4);

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

  // Setup position index.
  vtkNew<vtkIntArray> index;
  index->SetNumberOfComponents(1);
  index->SetName("index");
  index->InsertNextValue(0);
  index->InsertNextValue(1);
  index->InsertNextValue(2);
  index->InsertNextValue(3);
  index->InsertNextValue(4);

  vtkNew<vtkPolyData> polydata;
  polydata->SetPoints(points);
  polydata->GetPointData()->AddArray(index);

  vtkNew<vtkThresholdPoints> threshold;
  threshold->SetInputData(polydata);
  threshold->ThresholdByLower(2);
  threshold->SetInputArrayToProcess(
      0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_POINTS, "index");
  threshold->Update();

  // vtkPolyData* thresholdedPolydata = threshold->GetOutput();
  vtkPolyData* thresholdedPolydata = threshold->GetOutput();
  std::cout << "There are " << thresholdedPolydata->GetNumberOfPoints()
            << " points after thresholding." << std::endl;
  for (vtkIdType i = 0; i < thresholdedPolydata->GetNumberOfPoints(); i++)
  {
    double p[3];
    thresholdedPolydata->GetPoint(i, p);
    std::cout << "Point " << i << " : " << p[0] << " " << p[1] << " " << p[2]
              << std::endl;
  }

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(ThresholdPoints)

find_package(VTK COMPONENTS 
)

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

Download and Build ThresholdPoints

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

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

./ThresholdPoints

WINDOWS USERS

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