FindAllArrayNames

VTKExamples/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 simple question about this example contact us at VTKExamplesProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

Code

FindAllArrayNames.cxx

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

#include <string>
#include <vector>

void FindAllData(vtkPolyData* polydata);

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

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

  FindAllData(polydata);

  return EXIT_SUCCESS;
}

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/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;
  }
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(FindAllArrayNames)

find_package(VTK COMPONENTS 
  vtkCommonCore
  vtkCommonDataModel
  vtkFiltersSources
  vtkIOXML QUIET)
if (NOT VTK_FOUND)
  message("Skipping FindAllArrayNames: ${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(FindAllArrayNames MACOSX_BUNDLE FindAllArrayNames.cxx )
  target_link_libraries(FindAllArrayNames PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  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}
    )
endif () 

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.