VTK/Examples/IO/ReadUnstructuredGrid

From KitwarePublic

< VTK | Examples(Redirected from Read a VTU file)
Jump to: navigation, search

This examples demonstrates how to read an unstructured grid (VTU) file.

An example data file (.vtu) for testing can be found in VTKData/Data/quadraticTetra01.vtu.

ReadVTUFile.cxx

#include <vtkSmartPointer.h>
#include <vtkXMLUnstructuredGridReader.h>
#include <vtkUnstructuredGrid.h>
#include <vtkCell.h>
#include <vtkIdList.h>
#include <vtkUnsignedCharArray.h>
#include <vtkPointData.h>
 
int main(int argc, char *argv[])
{
  //parse command line arguments
  if(argc != 2)
    {
    vtkstd::cout << "Required arguments: Filename" << vtkstd::endl;
    return EXIT_FAILURE;
    }
 
  vtkstd::string filename = argv[1];
 
  //read all the data from the file
  vtkSmartPointer<vtkXMLUnstructuredGridReader> reader = vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
  reader->SetFileName(filename.c_str());
  reader->Update();
  vtkUnstructuredGrid* unstructuredGrid = reader->GetOutput();
 
  //get points
  vtkIdType numPoints = unstructuredGrid->GetNumberOfPoints();
  vtkstd::cout << "There are " << numPoints << " points." << vtkstd::endl;
 
  if(!(numPoints > 0) )
    {
    return EXIT_FAILURE;
    }
 
  double point[3];
  for(unsigned int i = 0; i < numPoints; i++)
  {
    unstructuredGrid->GetPoint(i, point);
    cout << "Point " << i << ": " << point[0] << " " << point[1] << " " << point[2] << endl;
  }
 
  //get triangles
  //if( (ug->GetCellType(0) == VTK_TRIANGLE) && (NumCells > 0) )//vtkCellType.h
  vtkIdType numCells = unstructuredGrid->GetNumberOfCells();
  vtkstd::cout << "There are " << numCells << " cells." << vtkstd::endl;
  for(vtkIdType tri = 0; tri < numCells; tri++)
    {
    vtkSmartPointer<vtkCell> cell = unstructuredGrid->GetCell(tri);
    vtkSmartPointer<vtkIdList> pts = cell->GetPointIds();
    std::vector<int> list(3);
    list[0] = pts->GetId(0);
    list[1] = pts->GetId(1);
    list[2] = pts->GetId(2);
    }
 
  //get colors
  vtkSmartPointer<vtkUnsignedCharArray> colorsData = vtkUnsignedCharArray::SafeDownCast(
      unstructuredGrid->GetPointData()->GetArray("Colors"));
 
  if(colorsData)
    { 
    unsigned char color[3];
 
    for(unsigned int i = 0; i < static_cast<unsigned int> (numPoints); i++)
      {
      colorsData->GetTupleValue(i, color);
      cout << "Color " << i << ": " << color[0] << " " << color[1] << " " << color[2] << endl;
      }
    }
 
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(ReadVTUFile)
 
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
 
ADD_EXECUTABLE(ReadVTUFile ReadVTUFile.cxx)
TARGET_LINK_LIBRARIES(ReadVTUFile vtkHybrid)
Personal tools