VTK/Tutorials/TriangleGeometryPolygon: Difference between revisions

From KitwarePublic
< VTK‎ | Tutorials
Jump to navigationJump to search
No edit summary
Line 1: Line 1:
<source lang="cpp">
<source lang="cpp">
void TestWriteTriSquare(const string &OutputFile, const char FileType)
void TestWriteTriSquare(const string &OutputFile)
{
{
//setup points
//setup points
Line 27: Line 26:
VertexList.push_back(Tri2);
VertexList.push_back(Tri2);
//setup model
        //create VTK objects of the points (and vertices). Without vertices, you cannot see the points in Paraview
ModelClass Model;
vtkPoints* points3D = vtkPoints::New();
Model.Points = Points;
vtkCellArray* Vertices = vtkCellArray::New();
Model.VertexList = VertexList;
 
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 model
        //write the data to a vtp file
if(FileType == 'u')
vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
VtuWrite(Model, OutputFile);
writer->SetFileName(OutputFile.c_str());
else if(FileType == 'p')
writer->SetInput(polydata);
VtpWrite(Model, OutputFile);
writer->Write();


}
}
</source>
</source>

Revision as of 10:52, 30 April 2009

<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>