VTK/Tutorials/TriangleGeometryPolygon: Difference between revisions

From KitwarePublic
< VTK‎ | Tutorials
Jump to navigationJump to search
No edit summary
No edit summary
Line 1: Line 1:
<source lang="cpp">
<source lang="cpp">
void TestWriteTriSquare(const string &OutputFile)
#include <iostream>
#include <vector>
 
#include "vtkCellArray.h"
#include "vtkPoints.h"
#include "vtkXMLPolyDataWriter.h"
#include "vtkPolyData.h"
#include "vtkTriangle.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()
{
//setup points
//setup points
vector<Point> Points;
std::vector<Point> Coords;
Points.push_back(Point(-1, 1, 0));
Coords.push_back(Point(1.0, 0.0, 0.0));
Points.push_back(Point(1, 1, 0));
Coords.push_back(Point(0.0, 0.0, 0.0));
Points.push_back(Point(1, -1, 0));
Coords.push_back(Point(0.0, 1.0, 0.0));
Points.push_back(Point(-1, -1, 0));
vtkPoints* Points = vtkPoints::New();
//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);
for ( unsigned int i = 0; i < Coords.size(); ++i )
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 = Coords[i];
Point P = Model.Points[i];
Points->InsertNextPoint(P.x, P.y, P.z);
pid[0] = points3D->InsertNextPoint(P.x, P.y, P.z);
Vertices->InsertNextCell(1,pid);
}
}
 
        //create VTK triangle objects
std::vector<int> VertexList;
vtkCellArray* triangles = vtkCellArray::New();
vtkCellArray* triangles = vtkCellArray::New();
for(unsigned int i = 0; i < Model.VertexList.size(); i++)
vtkTriangle* triangle = vtkTriangle::New();
{
triangle->GetPointIds()->SetId(0, 0);
vector<int> vlist = Model.VertexList[i];
triangle->GetPointIds()->SetId(1, 1);
vtkSmartPointer<vtkTriangle> triangle = vtkSmartPointer<vtkTriangle>::New();
triangle->GetPointIds()->SetId(2, 2);
triangle->GetPointIds()->SetId(0,vlist[0]);
triangles->InsertNextCell(triangle);
triangle->GetPointIds()->SetId(1,vlist[1]);
triangle->GetPointIds()->SetId(2,vlist[2]);
vtkPolyData* polydata = vtkPolyData::New();
triangles->InsertNextCell(triangle);
}
        //assemble the points and triangles into a PolyData
vtkSmartPointer<vtkPolyData> polydata = vtkPolyData::New();


polydata->SetPoints(points3D);
polydata->SetPoints(Points);
polydata->SetVerts(Vertices);
polydata->SetPolys(triangles);
polydata->SetPolys(triangles);
 
        //write the data to a vtp file
vtkXMLPolyDataWriter* writer = vtkXMLPolyDataWriter::New();
vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
writer->SetFileName("Triangle.vtp");
writer->SetFileName(OutputFile.c_str());
writer->SetInput(polydata);
writer->SetInput(polydata);
writer->Write();
writer->Write();

Revision as of 15:11, 18 June 2009

<source lang="cpp">

  1. include <iostream>
  2. include <vector>
  1. include "vtkCellArray.h"
  2. include "vtkPoints.h"
  3. include "vtkXMLPolyDataWriter.h"
  4. include "vtkPolyData.h"
  5. include "vtkTriangle.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() {

//setup points std::vector<Point> Coords; Coords.push_back(Point(1.0, 0.0, 0.0)); Coords.push_back(Point(0.0, 0.0, 0.0)); Coords.push_back(Point(0.0, 1.0, 0.0));

vtkPoints* Points = vtkPoints::New();

for ( unsigned int i = 0; i < Coords.size(); ++i ) { Point P = Coords[i]; Points->InsertNextPoint(P.x, P.y, P.z); }

std::vector<int> VertexList;

vtkCellArray* triangles = vtkCellArray::New(); vtkTriangle* triangle = vtkTriangle::New(); triangle->GetPointIds()->SetId(0, 0); triangle->GetPointIds()->SetId(1, 1); triangle->GetPointIds()->SetId(2, 2); triangles->InsertNextCell(triangle);

vtkPolyData* polydata = vtkPolyData::New();

polydata->SetPoints(Points); polydata->SetPolys(triangles);

vtkXMLPolyDataWriter* writer = vtkXMLPolyDataWriter::New(); writer->SetFileName("Triangle.vtp"); writer->SetInput(polydata); writer->Write();

} </source>