VTK/Tutorials/TriangleGeometryPolygon: Difference between revisions

From KitwarePublic
< VTK‎ | Tutorials
Jump to navigationJump to search
m (Write a file of a triangle moved to Triangle - Geometry + Polygon: Changed structure of polydata examples)
No edit summary
Line 1: Line 1:
==TrianglePolygon.cxx==
<source lang="cpp">
<source lang="cpp">
#include <iostream>
#include <vtkCellArray.h>
#include <vector>
#include <vtkSmartPointer.h>
#include <vtkPoints.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkPolyData.h>
#include <vtkTriangle.h>
 
int main()
{
  //setup points (geometry)
  vtkSmartPointer<vtkPoints> Points = vtkSmartPointer<vtkPoints>::New();
  Points->InsertNextPoint ( 1.0, 0.0, 0.0 );
  Points->InsertNextPoint ( 0.0, 0.0, 0.0 );
  Points->InsertNextPoint ( 0.0, 1.0, 0.0 );
 
  vtkSmartPointer<vtkCellArray> triangles = vtkSmartPointer<vtkCellArray>::New();
 
  //create a triangle on the three points in the polydata
  vtkSmartPointer<vtkTriangle> triangle = vtkSmartPointer<vtkTriangle>::New();


#include "vtkCellArray.h"
  //Unfortunately in this simple example the following lines are ambiguous.
#include "vtkPoints.h"
  //The first 0 is the index of the triangle vertex which is ALWAYS 0-2.
#include "vtkXMLPolyDataWriter.h"
  //The second 0 is the index into the point (geometry) array, so this can range from 0-(NumPoints-1)
#include "vtkPolyData.h"
  //i.e. a more general statement is triangle->GetPointIds()->SetId(0, PointId);
#include "vtkTriangle.h"
  triangle->GetPointIds()->SetId ( 0, 0 );
  triangle->GetPointIds()->SetId ( 1, 1 );
  triangle->GetPointIds()->SetId ( 2, 2 );


struct Point
  //add the triangle to the list of triangles (in this case there is only 1)
{
  triangles->InsertNextCell ( triangle );
double x,y,z;
Point(const double xin, const double yin, const double zin) : x(xin), y(yin), z(zin) {}
};


int main()
  //create a polydata object
{
  vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
//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);
  //add the geometry and topology to the polydata
polydata->SetPolys(triangles);
  polydata->SetPoints ( Points );
  polydata->SetPolys ( triangles );


vtkXMLPolyDataWriter* writer = vtkXMLPolyDataWriter::New();
  //write the polydata to a file
writer->SetFileName("Triangle.vtp");
  vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
writer->SetInput(polydata);
  writer->SetFileName ( "Triangle.vtp" );
writer->Write();
  writer->SetInput ( polydata );
  writer->Write();


}
}
</source>
==CMakeLists.txt==
<source lang="text">
cmake_minimum_required(VERSION 2.6)
PROJECT(TrianglePolygon)
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
ADD_EXECUTABLE(TrianglePolygon TrianglePolygon.cxx)
TARGET_LINK_LIBRARIES(TrianglePolygon vtkHybrid)
</source>
</source>

Revision as of 18:32, 22 October 2009

TrianglePolygon.cxx

<source lang="cpp">

  1. include <vtkCellArray.h>
  2. include <vtkSmartPointer.h>
  3. include <vtkPoints.h>
  4. include <vtkXMLPolyDataWriter.h>
  5. include <vtkPolyData.h>
  6. include <vtkTriangle.h>

int main() {

 //setup points (geometry)
 vtkSmartPointer<vtkPoints> Points = vtkSmartPointer<vtkPoints>::New();
 Points->InsertNextPoint ( 1.0, 0.0, 0.0 );
 Points->InsertNextPoint ( 0.0, 0.0, 0.0 );
 Points->InsertNextPoint ( 0.0, 1.0, 0.0 );
 vtkSmartPointer<vtkCellArray> triangles = vtkSmartPointer<vtkCellArray>::New();
 //create a triangle on the three points in the polydata
 vtkSmartPointer<vtkTriangle> triangle = vtkSmartPointer<vtkTriangle>::New();
 //Unfortunately in this simple example the following lines are ambiguous.
 //The first 0 is the index of the triangle vertex which is ALWAYS 0-2.
 //The second 0 is the index into the point (geometry) array, so this can range from 0-(NumPoints-1)
 //i.e. a more general statement is triangle->GetPointIds()->SetId(0, PointId);
 triangle->GetPointIds()->SetId ( 0, 0 );
 triangle->GetPointIds()->SetId ( 1, 1 );
 triangle->GetPointIds()->SetId ( 2, 2 );
 //add the triangle to the list of triangles (in this case there is only 1)
 triangles->InsertNextCell ( triangle );
 //create a polydata object
 vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
 //add the geometry and topology to the polydata
 polydata->SetPoints ( Points );
 polydata->SetPolys ( triangles );
 //write the polydata to a file
 vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
 writer->SetFileName ( "Triangle.vtp" );
 writer->SetInput ( polydata );
 writer->Write();

}

</source>

CMakeLists.txt

<source lang="text"> cmake_minimum_required(VERSION 2.6)

PROJECT(TrianglePolygon)

FIND_PACKAGE(VTK REQUIRED) INCLUDE(${VTK_USE_FILE})

ADD_EXECUTABLE(TrianglePolygon TrianglePolygon.cxx) TARGET_LINK_LIBRARIES(TrianglePolygon vtkHybrid)

</source>