Skip to content

PointCellIds

vtk-examples/Cxx/PolyData/PointCellIds

Description

This example demonstrates how to get the number of Ids in points and cells.

Question

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

Code

PointCellIds.cxx

#include <vtkCellData.h>
#include <vtkIdFilter.h>
#include <vtkIdTypeArray.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkSphereSource.h>
#include <vtkVersion.h>

#if VTK_VERSION_NUMBER >= 89000000000ULL
#define VTK890 1
#endif

#include <iostream>
#include <string>

int main(int, char*[])
{
  vtkNew<vtkSphereSource> sphereSource;
  sphereSource->Update();

  std::cout << "There are " << sphereSource->GetOutput()->GetNumberOfPoints()
            << " points." << std::endl;
  std::cout << "There are " << sphereSource->GetOutput()->GetNumberOfCells()
            << " cells." << std::endl;

  vtkNew<vtkIdFilter> idFilter;
  idFilter->SetInputConnection(sphereSource->GetOutputPort());
#if VTK890
  idFilter->SetPointIdsArrayName("ids");
  idFilter->SetCellIdsArrayName("ids");
#else
  idFilter->SetIdsArrayName("ids");
#endif
  idFilter->Update();

  std::cout << "point arrays: " << std::endl;
  for (vtkIdType i = 0;
       i < idFilter->GetOutput()->GetPointData()->GetNumberOfArrays(); i++)
  {
    std::cout << idFilter->GetOutput()->GetPointData()->GetArrayName(i)
              << std::endl;
  }

  std::cout << "cell arrays: " << std::endl;
  for (vtkIdType i = 0;
       i < idFilter->GetOutput()->GetCellData()->GetNumberOfArrays(); i++)
  {
    std::cout << idFilter->GetOutput()->GetCellData()->GetArrayName(i)
              << std::endl;
  }

  vtkIdTypeArray* pointIds = dynamic_cast<vtkIdTypeArray*>(
      idFilter->GetOutput()->GetPointData()->GetArray("ids"));
  std::cout << "There are " << pointIds->GetNumberOfTuples() << " point ids"
            << std::endl;

  vtkIdTypeArray* cellIds = dynamic_cast<vtkIdTypeArray*>(
      idFilter->GetOutput()->GetCellData()->GetArray("ids"));
  std::cout << "There are " << cellIds->GetNumberOfTuples() << " cell ids"
            << std::endl;

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(PointCellIds)

find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
  FiltersCore
  FiltersSources
)

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

Download and Build PointCellIds

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

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

./PointCellIds

WINDOWS USERS

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