Read a VTP file

From KitwarePublic
Revision as of 10:09, 8 April 2009 by Daviddoria (talk | contribs) (New page: <source lang="cpp"> bool VtpRead(const string &filename, ModelClass &Model) { //get all data from the file vtkSmartPointer<vtkXMLPolyDataReader> reader = vtkSmartPointer<vtkXMLPolyDat...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
bool VtpRead(const string &filename, ModelClass &Model)
{
	
	//get all data from the file
	vtkSmartPointer<vtkXMLPolyDataReader> reader = vtkSmartPointer<vtkXMLPolyDataReader>::New();
	reader->SetFileName(filename.c_str());
	reader->Update();
	vtkSmartPointer<vtkPolyData> polydata = reader->GetOutput();
    
	//get points
	vtkIdType NumPoints = polydata->GetNumberOfPoints();
	//vtkIdType NumCells = polydata->GetNumberOfCells();
	
	if(!(NumPoints > 0) )
	{
		return false;
	}
	
	double point[3];
	for(vtkIdType i = 0; i < NumPoints; i++)
	{
		polydata->GetPoint(i, point);
		Model.Points.push_back(Point(point[0], point[1], point[2]));
	}
	
	//get triangles
	vtkIdType NumPolys = polydata->GetNumberOfPolys();
	if(NumPolys > 0)
	{
		vtkSmartPointer<vtkCellArray> TriangleCells = polydata->GetPolys();
		vtkIdType npts;
		vtkIdType *pts;

		Model.VertexList.clear();
		while(TriangleCells->GetNextCell(npts, pts))
		{
			vector<int> List(3);
			List[0] = pts[0];
			List[1] = pts[1];
			List[2] = pts[2];
			
			Model.VertexList.push_back(List);
		}	
	}
	
	
	//get colors
	vtkSmartPointer<vtkUnsignedCharArray> ColorsData = vtkUnsignedCharArray::SafeDownCast(polydata->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);
			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++)
			Model.Colors.push_back(Red());
	}

	cout << "Finished reading vtp file " << filename << "." << endl;
	cout << "NumPoints: " << Model.Points.size() << endl;
	cout << "NumColors: " << Model.Colors.size() << endl;

	return true; //file read successfully
}