[vtkusers] question ? [Writing triangles]
Hal Canary
hal at cs.unc.edu
Tue Sep 4 14:19:13 EDT 2012
On 09/04/2012 12:46 PM, agatte wrote:
> Hi All VTK ;)
>
> I am trying to write triangle to txt file,
> Format of file like this :
>
> number_of_points
> number_of_triangles
> point[0]X point[0]Y point[0]Z
> point[1]X point[1]Y point[1]Z
> ...
> point[N]X point[N]Y point[N]Z
> triangle[0]A triangle[0]B triangle[0]C
> triangle[1]A triangle[1]B triangle[1]C
> ...
> triangle[M]A triangle[M]B triangle[M]C
>
>
> I followed this example :
> http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/TriangleArea
>
> I can not write triangle corectly.
> Could anyone help me please ?
> I don't know what is wrong with my code ?
>
> Here my code :
>
>[...]
>
> I would appreciate for any help.
>
> agatte
#include <iostream>
#include <cassert>
#include "vtkCellArray.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataReader.h"
#include "vtkSmartPointer.h"
int main(int argc, char *argv[])
{
// Polydata reader
vtkSmartPointer<vtkPolyDataReader> reader =
vtkSmartPointer<vtkPolyDataReader>::New();
reader->SetFileName("SimpleMesh.vtk");
reader->Update();
vtkPolyData * polydata = reader->GetOutput();
vtkPoints* points = polydata->GetPoints();
vtkCellArray* polys = polydata->GetPolys();
// Write to txt file
const char* filename = "test.txt";
std::ofstream infile(filename);
vtkIdType number_of_points, number_of_triangles;
number_of_points = polydata->GetNumberOfPoints();
number_of_triangles = polydata->GetNumberOfCells();
infile << number_of_points << std::endl
<< number_of_triangles << std::endl;
// Read points
for (vtkIdType i = 0; i < number_of_points; i++)
{
double p[3];
points->GetPoint(i,p);
infile << p[0] << " " << p[1] << " " << p[2] << std::endl;
}
// traverse cell array to get triangles
polys->InitTraversal();
vtkIdType * triangle;
vtkIdType numberOfCellPoints;
while( polys->GetNextCell( numberOfCellPoints, triangle ) )
{
if (numberOfCellPoints == 3) // traingeles
infile << triangle[0] << " " << triangle[1] << " "
<< triangle[2] << std::endl;
else
std::cerr << "non-triangle.\n";
}
infile.close();
return EXIT_SUCCESS;
}
More information about the vtkusers
mailing list