Skip to content

DetermineArrayDataTypes

Repository source: DetermineArrayDataTypes

Description

This example shows how to determine the type of data stored in a named array in a vtp file.

Question

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

Code

DetermineArrayDataTypes.cxx

#include <string>
#include <vector>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkXMLPolyDataReader.h>

namespace {
void FindAllData(const std::string& inputFilename);
}

int main(int argc, char* argv[])
{
  if (argc != 2)
  {
    std::cout << "Usage: " << argv[0]
              << " InputFilename e.g. filledContours.vtp" << std::endl;
    return EXIT_FAILURE;
  }

  std::string inputFilename = argv[1];
  FindAllData(inputFilename);

  return EXIT_SUCCESS;
}

namespace {
void FindAllData(const std::string& InputFilename)
{
  vtkNew<vtkXMLPolyDataReader> reader;
  reader->SetFileName(InputFilename.c_str());
  reader->Update();
  vtkPolyData* polydata = reader->GetOutput();

  unsigned int numberOfArrays = polydata->GetPointData()->GetNumberOfArrays();
  std::cout << "NumArrays: " << numberOfArrays << std::endl;

  std::cout << "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;

  std::vector<std::string> arrayNames;
  for (unsigned int i = 0; i < numberOfArrays; 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 << ": " << arrayNames[i]
              << " (type: " << dataTypeID << ")" << std::endl;
  }
}
} // namespace

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(DetermineArrayDataTypes)

find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
  IOXML
)

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

Download and Build DetermineArrayDataTypes

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

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

./DetermineArrayDataTypes

WINDOWS USERS

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