Skip to content

DeletePoint

vtk-examples/Cxx/PolyData/DeletePoint

Question

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

Code

DeletePoint.cxx

#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>

namespace {
void OutputPoints(vtkSmartPointer<vtkPoints> points);
void ReallyDeletePoint(vtkSmartPointer<vtkPoints> points, vtkIdType id);
} // namespace

int main(int, char*[])
{
  // Create a set of points
  vtkNew<vtkPoints> points;
  points->InsertNextPoint(1.0, 0.0, 0.0);
  points->InsertNextPoint(0.0, 1.0, 0.0);
  points->InsertNextPoint(0.0, 0.0, 1.0);

  std::cout << "Number of points: " << points->GetNumberOfPoints() << std::endl;
  OutputPoints(points);

  ReallyDeletePoint(points, 1);

  std::cout << "Number of points: " << points->GetNumberOfPoints() << std::endl;
  OutputPoints(points);

  return EXIT_SUCCESS;
}

namespace {
void OutputPoints(vtkSmartPointer<vtkPoints> points)
{
  for (vtkIdType i = 0; i < points->GetNumberOfPoints(); i++)
  {
    double p[3];
    points->GetPoint(i, p);
    cout << p[0] << " " << p[1] << " " << p[2] << endl;
  }
}

void ReallyDeletePoint(vtkSmartPointer<vtkPoints> points, vtkIdType id)
{
  vtkNew<vtkPoints> newPoints;

  for (vtkIdType i = 0; i < points->GetNumberOfPoints(); i++)
  {
    if (i != id)
    {
      double p[3];
      points->GetPoint(i, p);
      newPoints->InsertNextPoint(p);
    }
  }

  points->ShallowCopy(newPoints);
}
} // namespace

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(DeletePoint)

find_package(VTK COMPONENTS 
  vtkCommonCore
  vtkCommonDataModel
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping DeletePoint: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(DeletePoint MACOSX_BUNDLE DeletePoint.cxx )
  target_link_libraries(DeletePoint PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(DeletePoint MACOSX_BUNDLE DeletePoint.cxx )
  target_link_libraries(DeletePoint PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS DeletePoint
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build DeletePoint

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

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

./DeletePoint

WINDOWS USERS

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