Skip to content

GenericDataObjectReader

vtk-examples/Cxx/IO/GenericDataObjectReader

Question

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

Code

GenericDataObjectReader.cxx

#include <string>
#include <vtkGenericDataObjectReader.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkStructuredGrid.h>
#include <vtkUnstructuredGrid.h>

int main(int argc, char* argv[])
{
  // Ensure a filename was specified
  if (argc != 2)
  {
    std::cerr << "Usage: " << argv[0] << " InputFilename e.g. blow.vtk" << endl;
    return EXIT_FAILURE;
  }

  // Get the filename from the command line
  std::string inputFilename = argv[1];

  // Get all data from the file
  vtkNew<vtkGenericDataObjectReader> reader;
  reader->SetFileName(inputFilename.c_str());
  reader->Update();

  // All of the standard data types can be checked and obtained like this:
  if (reader->IsFilePolyData())
  {
    std::cout << "output is polydata," << std::endl;
    auto output = reader->GetPolyDataOutput();
    std::cout << "   output has " << output->GetNumberOfPoints() << " points."
              << std::endl;
  }

  if (reader->IsFileUnstructuredGrid())
  {
    std::cout << "output is unstructured grid," << std::endl;
    auto output = reader->GetUnstructuredGridOutput();
    std::cout << "   output has " << output->GetNumberOfPoints() << " points."
              << std::endl;
  }

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(GenericDataObjectReader)

find_package(VTK COMPONENTS 
  vtkCommonCore
  vtkCommonDataModel
  vtkIOLegacy
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping GenericDataObjectReader: ${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(GenericDataObjectReader MACOSX_BUNDLE GenericDataObjectReader.cxx )
  target_link_libraries(GenericDataObjectReader PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(GenericDataObjectReader MACOSX_BUNDLE GenericDataObjectReader.cxx )
  target_link_libraries(GenericDataObjectReader PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS GenericDataObjectReader
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build GenericDataObjectReader

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

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

./GenericDataObjectReader

WINDOWS USERS

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