next up previous contents
Next: Display a Triangulation with Up: LESSON on freefem and Previous: Triangulation file

Read a Triangulation file

In C++ we use the "class" structure to define a vertex and a triangle

// file readT.cpp

#include <iostream.h>
#include <fstream.h>

//DECLARATIONS
class Vertex { public: float x,y; int where;};
class Triangle { public: int v[3]; int where;};

class Triangulation { public:
               int nv,nt;
               Vertex* v;
               Triangle* t;
      Triangulation(const char* path);
};

//IMPLEMENTATION
Triangulation::Triangulation (const char* path)
{
       int i,j,k,i1;
       ifstream file(path); 
      
       file >> nv >>nt;
       v = new Vertex[nv];
       t = new Triangle[nt];
       for( i=0; i<nv; i++) file >> v[i].x >> v[i].y >> v[i].where;
       for( j=0; j<nt; j++) 
        {  for( k=0; k<3;k++){ file >> i1; t[j].v[k] = --i1;}
           file >> t[j].where;
        }
       file.close();
}

void main()
{  
  Triangulation g("micro.msh");
}


remarque318
In FORTRAN all arrays begin at 1 (unless specified otherwise) but in C they begin at 0. When a file format is defined it is better to adjust to the FORTRAN convention because C programmers know about it but others don't. So a one is subtracted from t[j].v[k] in the instruction "- -i1".



Pironneau Olivier
Jeudi 12 mars 1998 16:06:41