VTK/Tutorials/TriangleGeometryPolygon

From KitwarePublic
< VTK‎ | Tutorials
Revision as of 10:52, 30 April 2009 by Daviddoria (talk | contribs)
Jump to navigationJump to search

<source lang="cpp"> void TestWriteTriSquare(const string &OutputFile) { //setup points vector<Point> Points; Points.push_back(Point(-1, 1, 0)); Points.push_back(Point(1, 1, 0)); Points.push_back(Point(1, -1, 0)); Points.push_back(Point(-1, -1, 0));

//setup triangles vector<vector<int> > VertexList;

vector<int> Tri1; vector<int> Tri2;

Tri1.push_back(0); Tri1.push_back(1); Tri1.push_back(2);

Tri2.push_back(0); Tri2.push_back(2); Tri2.push_back(3);

VertexList.push_back(Tri1); VertexList.push_back(Tri2);

       //create VTK objects of the points (and vertices). Without vertices, you cannot see the points in Paraview

vtkPoints* points3D = vtkPoints::New(); vtkCellArray* Vertices = vtkCellArray::New();

for ( unsigned int i = 0; i < Model.Points.size(); ++i ) { vtkIdType pid[1]; Point P = Model.Points[i]; pid[0] = points3D->InsertNextPoint(P.x, P.y, P.z); Vertices->InsertNextCell(1,pid); }

       //create VTK triangle objects

vtkCellArray* triangles = vtkCellArray::New(); for(unsigned int i = 0; i < Model.VertexList.size(); i++) { 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); }

       //assemble the points and triangles into a PolyData

vtkSmartPointer<vtkPolyData> polydata = vtkPolyData::New();

polydata->SetPoints(points3D); polydata->SetVerts(Vertices); polydata->SetPolys(triangles);

       //write the data to a vtp file

vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New(); writer->SetFileName(OutputFile.c_str()); writer->SetInput(polydata); writer->Write();

} </source>