Skip to content

FindAllArrayNames

vtk-examples/Cxx/IO/FindAllArrayNames

Description

This example shows how to find out the names of all of the data arrays stored in a vtkPolyData.

Other languages

See (CSharp)

Question

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

Code

FindAllArrayNames.cxx

#include <vtkCellData.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkXMLPolyDataWriter.h>

#include <string>
#include <vector>

namespace {
void FindAllData(vtkPolyData* polydata);
}

int main(int argc, char* argv[])
{
  vtkSmartPointer<vtkPolyData> polydata;
  if (argc < 2)
  {
    vtkNew<vtkSphereSource> sphereSource;
    sphereSource->Update();
    vtkNew<vtkXMLPolyDataWriter> writer;
    writer->SetFileName("test.vtp");
    writer->SetInputConnection(sphereSource->GetOutputPort());
    writer->Write();

    polydata = sphereSource->GetOutput();
  }
  else
  {
    vtkNew<vtkXMLPolyDataReader> reader;
    reader->SetFileName(argv[1]);
    reader->Update();
    polydata = reader->GetOutput();
  }

  FindAllData(polydata);

  return EXIT_SUCCESS;
}

namespace {
void FindAllData(vtkPolyData* polydata)
{
  std::cout << "Normals: " << polydata->GetPointData()->GetNormals()
            << std::endl;

  vtkIdType numberOfPointArrays = polydata->GetPointData()->GetNumberOfArrays();
  std::cout << "Number of PointData arrays: " << numberOfPointArrays
            << std::endl;

  vtkIdType numberOfCellArrays = polydata->GetCellData()->GetNumberOfArrays();
  std::cout << "Number of CellData arrays: " << numberOfCellArrays << std::endl;

  std::cout << "Type table/key: " << std::endl;
  ;
  // More values can be found in <VTK_DIR>/Common/Core/vtkSetGet.h
  std::cout << VTK_UNSIGNED_CHAR << " unsigned char" << std::endl;
  std::cout << VTK_UNSIGNED_INT << " unsigned int" << std::endl;
  std::cout << VTK_FLOAT << " float" << std::endl;
  std::cout << VTK_DOUBLE << " double" << std::endl;

  for (vtkIdType i = 0; i < numberOfPointArrays; i++)
  {
    // The following two lines are equivalent.
    // arrayNames.push_back(polydata->GetPointData()->GetArray(i)->GetName());
    // arrayNames.push_back(polydata->GetPointData()->GetArrayName(i));
    int dataTypeID = polydata->GetPointData()->GetArray(i)->GetDataType();
    std::cout << "Array " << i << ": "
              << polydata->GetPointData()->GetArrayName(i)
              << " (type: " << dataTypeID << ")" << std::endl;
  }

  for (vtkIdType i = 0; i < numberOfCellArrays; i++)
  {
    // The following two lines are equivalent.
    // polydata->GetPointData()->GetArray(i)->GetName();
    // polydata->GetPointData()->GetArrayName(i);
    int dataTypeID = polydata->GetCellData()->GetArray(i)->GetDataType();
    std::cout << "Array " << i << ": "
              << polydata->GetCellData()->GetArrayName(i)
              << " (type: " << dataTypeID << ")" << std::endl;
  }
}
} // namespace

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(FindAllArrayNames)

find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
  FiltersSources
  IOXML
)

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

Download and Build FindAllArrayNames

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

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

./FindAllArrayNames

WINDOWS USERS

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