VTK/Examples/IO/WriteVTU

From KitwarePublic

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

This example writes a VTU file.

WriteVTUFile.cxx

#include <vtkSmartPointer.h>
#include <vtkXMLUnstructuredGridWriter.h>
#include <vtkUnstructuredGrid.h>
#include <vtkCell.h>
#include <vtkCellArray.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: OutputFilename" << vtkstd::endl;
    exit(-1);
    }
 
  vtkstd::string outputFilename = argv[1];
 
  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
  vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New();
 
  vtkIdType pid[1];
  pid[0] = points->InsertNextPoint(1, 0, 0);
  vertices->InsertNextCell(1,pid);
  pid[0] = points->InsertNextPoint(0, 1, 0);
  vertices->InsertNextCell(1,pid);
  pid[0] = points->InsertNextPoint(0, 0, 1);
  vertices->InsertNextCell(1,pid);
 
  vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New();
  colors->SetNumberOfComponents(3);
  colors->SetName("Colors");
 
  for ( unsigned int i = 0; i < points->GetNumberOfPoints(); i++ )
    {
    unsigned char colorArray[3] = {255, 0, 0}; //red
    colors->InsertNextTupleValue(colorArray);
    }
 
  vtkSmartPointer<vtkCellArray> triangles = vtkSmartPointer<vtkCellArray>::New();
 
  for(unsigned int i = 0; i < Model.VertexList.size(); i++)
    {
    vtkstd::vector<int> vlist = Model.VertexList[i];
    vtkSmartPointer<vtkTriangle> triangle = vtkSmartPointer<vtkTriangle>::New();
    triangle->GetPointIds()->SetId(0,vlist[0]);
    triangle->GetPointIds()->SetId(1,vlist[1]);
    triangle->GetPointIds()->SetId(2,vlist[2]);
    triangles->InsertNextCell(triangle);
    }
 
  vtkSmartPointer<vtkUnstructuredGrid> unstructuredGrid = 
    vtkSmartPointer<vtkUnstructuredGrid>::New();
  unstructuredGrid->SetPoints(points);
 
  //add triangles
  /*
  if(HasTriangles)
    {
    //unstructuredGrid->SetCells(VTK_TRIANGLE, triangles);
    }
  else
    {
    unstructuredGrid->SetCells(VTK_VERTEX, vertices);
    }
  */
  //add colors
  unstructuredGrid->GetPointData()->AddArray(Colors);
 
  vtkSmartPointer<vtkXMLUnstructuredGridWriter> writer = vtkSmartPointer<vtkXMLUnstructuredGridWriter>::New();
  writer->SetFileName(outputFilename.c_str());
  writer->SetInput(unstructuredGrid);
  writer->Write();	
 
  return 0;
}

CMakeLists.txt

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