[vtkusers] question ? [Writing triangles]

agatte agatakrason at gmail.com
Tue Sep 4 12:46:49 EDT 2012


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

    // Polydata reader
    vtkPolyDataReader* reader = vtkPolyDataReader::New();
    reader->SetFileName("SimpleMesh.vtk");
    reader->Update();

    vtkPolyData*polydata = reader->GetOutput();
    vtkPoints* points = polydata->GetPoints();
    vtkCellArray* polys = polydata->GetPolys();
    polydata->SetPoints(points);
    polydata->SetPolys(polys);

    // Write to txt file
    const char* filename = "test.txt";

    std::fstream infile;
    infile.open(filename);

    vtkIdType number_of_points, number_of_triangles;
    number_of_points =  polydata->GetNumberOfPoints();
    infile<<number_of_points&lt;&lt;std::endl;
    number_of_triangles = polydata->GetNumberOfCells();
    infile<<number_of_triangles&lt;&lt;std::endl;

    // Read points
    for (vtkIdType i = 0; i &lt; number_of_points; i++)
    {
        double p[3];
        points->GetPoint(i,p);
        infile <<p[0]&lt;&lt;&quot; &quot;&lt;&lt; p[1]&lt;&lt;&quot;
&quot;&lt;&lt;p[2];
        infile&lt;&lt;&quot; &quot;&lt;&lt;std::endl;
    }


    vtkSmartPointer&lt;vtkTriangle> triangle =
vtkSmartPointer<vtkTriangle>::New();

// HERE ??
    for(vtkIdType i = 0; i < number_of_triangles; i++)
    {

//       vtkSmartPointer<vtkLine> line = vtkSmartPointer<vtkLine>::New();
//       line->GetPointIds()->SetId(0,i);
//       line->GetPointIds()->SetId(1,i+1);
//       line->GetPointIds()->SetId(2,i+2);
//       polys->InsertNextCell(line);

        vtkIdType a,b,c;
        a = i;
        b = i+1;
        c = i+2;

        triangle->GetPointIds()->SetId(0,a);
        triangle->GetPointIds()->SetId(1,b);
        triangle->GetPointIds()->SetId(2,c);
        polys->InsertNextCell(triangle);

//        double p[3];
//        triangle->GetPoints()->GetPoint(i,p);
//        infile <<p[0]&lt;&lt;&quot; &quot;&lt;&lt; p[1]&lt;&lt;&quot;
&quot;&lt;&lt;p[2];
//        infile&lt;&lt;&quot; &quot;&lt;&lt;std::endl;



    }

    polydata->SetPoints(points);
    polydata->SetPolys(polys);

    for(vtkIdType i = 0; i < number_of_triangles;i++)
    {
            vtkCell* cell = polydata->GetCell(i);

           vtkTriangle* triangle = dynamic_cast<vtkTriangle*> (cell);


        double p0[3];
        double p1[3];
        double p2[3];
        triangle->GetPoints()->GetPoint(0,p0);
        std::cout<<"p0: "<< p0[0]<<" "<<p0[1]&lt;&lt;&quot;
&quot;&lt;&lt;p0[2]&lt;&lt;std::endl;
        infile&lt;&lt;p0[0]&lt;&lt;&quot; &quot;&lt;&lt;p0[1]&lt;&lt;&quot;
&quot;&lt;&lt;p0[2]&lt;&lt;std::endl;
/*
        triangle->GetPoints()->GetPoint(1,p1);
        std::cout<<"p1: "<<p1[0]&lt;&lt;&quot; &quot;&lt;&lt;p1[1] &lt;&lt;
&quot; &quot; &lt;&lt;p1[2] &lt;&lt; std::endl;
        infile&lt;&lt;p1[0]&lt;&lt;&quot; &quot;&lt;&lt;p1[1]&lt;&lt;&quot;
&quot;&lt;&lt;p1[2]&lt;&lt;std::endl;

        triangle->GetPoints()->GetPoint(2,p2);
        std::cout<<" "<<p2[0]<<" "<<p2[1]<<" "<<p2[2]<< std::endl;
        infile<<p2[0]<<" "<<p2[1]<<" "<<p2[2]<<std::endl*/;


    double area = vtkTriangle::TriangleArea(p0,p1,p2);

    }


    infile.close();


    return EXIT_SUCCESS;

}

I would appreciate for any help.

agatte



--
View this message in context: http://vtk.1045678.n5.nabble.com/question-Writing-triangles-tp5715836.html
Sent from the VTK - Users mailing list archive at Nabble.com.



More information about the vtkusers mailing list