Read a VTP file: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
(New page: <source lang="cpp"> bool VtpRead(const string &filename, ModelClass &Model) { //get all data from the file vtkSmartPointer<vtkXMLPolyDataReader> reader = vtkSmartPointer<vtkXMLPolyDat...)
 
No edit summary
 
Line 1: Line 1:
As VTP is a very flexible format, there is no way to make a generic reader. You must read the data that you are interested in. A generic reader can only look for points and triangles.


<source lang="cpp">
<source lang="cpp">
bool VtpRead(const string &filename, ModelClass &Model)
#include <iostream>
#include <vector>
 
#include "vtkCellArray.h"
#include "vtkPoints.h"
#include "vtkXMLPolyDataReader.h"
#include "vtkPolyData.h"
 
struct Point
{
{
double x,y,z;
Point(const double xin, const double yin, const double zin) : x(xin), y(yin), z(zin) {}
};
 
int main()
{
//get all data from the file
//get all data from the file
vtkSmartPointer<vtkXMLPolyDataReader> reader = vtkSmartPointer<vtkXMLPolyDataReader>::New();
vtkXMLPolyDataReader* reader = vtkXMLPolyDataReader::New();
reader->SetFileName(filename.c_str());
reader->SetFileName("Triangle.vtp");
reader->Update();
reader->Update();
vtkSmartPointer<vtkPolyData> polydata = reader->GetOutput();
vtkPolyData* polydata = reader->GetOutput();
   
 
//get points
//get the number of points the file contains
vtkIdType NumPoints = polydata->GetNumberOfPoints();
vtkIdType NumPoints = polydata->GetNumberOfPoints();
//vtkIdType NumCells = polydata->GetNumberOfCells();
//if there are no points, quit
if(!(NumPoints > 0) )
if(!(NumPoints > 0) )
{
{
return false;
exit(-1);
}
}
 
//read in all of the points
std::vector<Point> Points;
double point[3];
double point[3];
for(vtkIdType i = 0; i < NumPoints; i++)
for(vtkIdType i = 0; i < NumPoints; i++)
{
{
polydata->GetPoint(i, point);
polydata->GetPoint(i, point);
Model.Points.push_back(Point(point[0], point[1], point[2]));
Points.push_back(Point(point[0], point[1], point[2]));
}
}
//get triangles
//get the triangles (if there are any)
std::vector<std::vector<int> > VertexLists;
vtkIdType NumPolys = polydata->GetNumberOfPolys();
vtkIdType NumPolys = polydata->GetNumberOfPolys();
if(NumPolys > 0)
if(NumPolys > 0)
{
{
vtkSmartPointer<vtkCellArray> TriangleCells = polydata->GetPolys();
vtkCellArray* TriangleCells = polydata->GetPolys();
vtkIdType npts;
vtkIdType npts;
vtkIdType *pts;
vtkIdType *pts;


Model.VertexList.clear();
while(TriangleCells->GetNextCell(npts, pts))
while(TriangleCells->GetNextCell(npts, pts))
{
{
vector<int> List(3);
std::vector<int> List(3);
List[0] = pts[0];
List[0] = pts[0];
List[1] = pts[1];
List[1] = pts[1];
List[2] = pts[2];
List[2] = pts[2];
Model.VertexList.push_back(List);
VertexLists.push_back(List);
}
}
}
}
std::cout << "Points: " << Points.size() << std::endl;
for(unsigned int i = 0; i < Points.size(); i++)
{
std::cout << Points[i].x << " " << Points[i].y << " " << Points[i].z << std::endl;
}
//get colors
std::cout << std::endl;
vtkSmartPointer<vtkUnsignedCharArray> ColorsData = vtkUnsignedCharArray::SafeDownCast(polydata->GetPointData()->GetArray("Colors"));
if(ColorsData)
std::cout << "Triangles: " << VertexLists.size() << std::endl;
{
for(unsigned int i = 0; i < VertexLists.size(); i++)
unsigned char color[3];
for(unsigned int i = 0; i < static_cast<unsigned int>(NumPoints); i++)
{
ColorsData->GetTupleValue(i, color);
Color c(color[0], color[1], color[2]);
Model.Colors.push_back(c);
}
}
else //if there are no colors, make all the points red
{
{
for(unsigned int i = 0; i < static_cast<unsigned int> (NumPoints); i++)
std::cout << VertexLists[i][0] << " " << VertexLists[i][1] << " " << VertexLists[i][2] << std::endl;
Model.Colors.push_back(Red());
}
}
 
return 0;
cout << "Finished reading vtp file " << filename << "." << endl;
cout << "NumPoints: " << Model.Points.size() << endl;
cout << "NumColors: " << Model.Colors.size() << endl;
 
return true; //file read successfully
}
}
</source>
</source>

Latest revision as of 15:10, 18 June 2009

As VTP is a very flexible format, there is no way to make a generic reader. You must read the data that you are interested in. A generic reader can only look for points and triangles.

<source lang="cpp">

  1. include <iostream>
  2. include <vector>
  1. include "vtkCellArray.h"
  2. include "vtkPoints.h"
  3. include "vtkXMLPolyDataReader.h"
  4. include "vtkPolyData.h"

struct Point { double x,y,z; Point(const double xin, const double yin, const double zin) : x(xin), y(yin), z(zin) {} };

int main() { //get all data from the file vtkXMLPolyDataReader* reader = vtkXMLPolyDataReader::New(); reader->SetFileName("Triangle.vtp"); reader->Update(); vtkPolyData* polydata = reader->GetOutput();

//get the number of points the file contains vtkIdType NumPoints = polydata->GetNumberOfPoints();

//if there are no points, quit if(!(NumPoints > 0) ) { exit(-1); }

//read in all of the points std::vector<Point> Points; double point[3]; for(vtkIdType i = 0; i < NumPoints; i++) { polydata->GetPoint(i, point); Points.push_back(Point(point[0], point[1], point[2])); }

//get the triangles (if there are any) std::vector<std::vector<int> > VertexLists; vtkIdType NumPolys = polydata->GetNumberOfPolys(); if(NumPolys > 0) { vtkCellArray* TriangleCells = polydata->GetPolys(); vtkIdType npts; vtkIdType *pts;

while(TriangleCells->GetNextCell(npts, pts)) { std::vector<int> List(3); List[0] = pts[0]; List[1] = pts[1]; List[2] = pts[2];

VertexLists.push_back(List); } }

std::cout << "Points: " << Points.size() << std::endl; for(unsigned int i = 0; i < Points.size(); i++) { std::cout << Points[i].x << " " << Points[i].y << " " << Points[i].z << std::endl; }

std::cout << std::endl;

std::cout << "Triangles: " << VertexLists.size() << std::endl; for(unsigned int i = 0; i < VertexLists.size(); i++) { std::cout << VertexLists[i][0] << " " << VertexLists[i][1] << " " << VertexLists[i][2] << std::endl; } return 0; } </source>