VTK/Tutorials/TriangleGeometryPolygon: Difference between revisions

From KitwarePublic
< VTK‎ | Tutorials
Jump to navigationJump to search
No edit summary
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==TrianglePolygon.cxx==
<source lang="cpp">
<source lang="cpp">
void TestWriteTriSquare(const string &OutputFile)
#include <vtkCellArray.h>
#include <vtkSmartPointer.h>
#include <vtkPoints.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkPolyData.h>
#include <vtkTriangle.h>
 
int main(int argc, char *argv[])
{
{
//setup points
  //setup points (geometry)
vector<Point> Points;
  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
Points.push_back(Point(-1, 1, 0));
  points->InsertNextPoint ( 1.0, 0.0, 0.0 );
Points.push_back(Point(1, 1, 0));
  points->InsertNextPoint ( 0.0, 0.0, 0.0 );
Points.push_back(Point(1, -1, 0));
  points->InsertNextPoint ( 0.0, 1.0, 0.0 );
Points.push_back(Point(-1, -1, 0));
 
  vtkSmartPointer<vtkCellArray> triangles = vtkSmartPointer<vtkCellArray>::New();
//setup triangles
 
vector<vector<int> > VertexList;
  //create a triangle on the three points in the polydata
  vtkSmartPointer<vtkTriangle> triangle = vtkSmartPointer<vtkTriangle>::New();
vector<int> Tri1;
 
vector<int> Tri2;
  //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.
Tri1.push_back(0);
  //The second 0 is the index into the point (geometry) array, so this can range from 0-(NumPoints-1)
Tri1.push_back(1);
  //i.e. a more general statement is triangle->GetPointIds()->SetId(0, PointId);
Tri1.push_back(2);
  triangle->GetPointIds()->SetId ( 0, 0 );
  triangle->GetPointIds()->SetId ( 1, 1 );
Tri2.push_back(0);
  triangle->GetPointIds()->SetId ( 2, 2 );
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 )
  //add the triangle to the list of triangles (in this case there is only 1)
{
  triangles->InsertNextCell ( triangle );
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
  //create a polydata object
vtkCellArray* triangles = vtkCellArray::New();
  vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::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);
  //add the geometry and topology to the polydata
polydata->SetVerts(Vertices);
  polydata->SetPoints ( points );
polydata->SetPolys(triangles);
  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();


  //write the polydata to a file
  vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  writer->SetFileName ( "Triangle.vtp" );
#if VTK_MAJOR_VERSION <= 5
  writer->SetInput ( polydata );
#else
  writer->SetInputData ( polydata );
#endif
  writer->Write();
  return EXIT_SUCCESS;
}
}
</source>
==CMakeLists.txt==
<source lang="cmake">
cmake_minimum_required(VERSION 2.6)
project(TrianglePolygon)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
add_executable(TrianglePolygon TrianglePolygon.cxx)
if(VTK_LIBRARIES)
  target_link_libraries(TrianglePolygon ${VTK_LIBRARIES})
else()
  target_link_libraries(TrianglePolygon vtkHybrid)
endif()
</source>
</source>

Latest revision as of 19:29, 25 April 2014

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(int argc, char *argv[]) {

 //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" );
  1. if VTK_MAJOR_VERSION <= 5
 writer->SetInput ( polydata );
  1. else
 writer->SetInputData ( polydata );
  1. endif
 writer->Write();
 return EXIT_SUCCESS;

}

</source>

CMakeLists.txt

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

project(TrianglePolygon)

find_package(VTK REQUIRED) include(${VTK_USE_FILE})

add_executable(TrianglePolygon TrianglePolygon.cxx) if(VTK_LIBRARIES)

 target_link_libraries(TrianglePolygon ${VTK_LIBRARIES})

else()

 target_link_libraries(TrianglePolygon vtkHybrid)

endif()

</source>