VTK/Tutorials/TriangleGeometryPolygon

From KitwarePublic
< VTK‎ | Tutorials
Revision as of 15:11, 18 June 2009 by Daviddoria (talk | contribs)
Jump to navigationJump to search

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