]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_GibiMeshDriver.hxx
Salome HOME
update after merging trhe branches CEA_V3_0_x, OCC_V3_1_0_a1_x, and the main
[modules/med.git] / src / MEDMEM / MEDMEM_GibiMeshDriver.hxx
1 #ifndef GIBI_MESH_DRIVER_HXX
2 #define GIBI_MESH_DRIVER_HXX
3
4 #include <string>
5 #include <vector>
6 #include <map>
7 #include <fstream>
8 #include <list>
9
10 #include "MEDMEM_define.hxx"
11 #include "MEDMEM_GenDriver.hxx"
12
13 #include "MEDMEM_STRING.hxx"
14 #include "MEDMEM_Exception.hxx"
15 #include "MEDMEM_Utilities.hxx"
16
17 /////
18 #include <sstream>
19 #include <iomanip>
20 /////
21
22 /*!
23
24   Driver GIBI for MESH.
25
26   Generic part : implement the readopen and close methods.
27   
28 */
29
30 namespace MEDMEM {
31 class MED;
32 class MESH;
33 class FIELD_;
34 class FAMILY;
35 class GROUP;
36 class SUPPORT;
37 class CONNECTIVITY;
38 class _intermediateMED;
39 class GIBI_MESH_DRIVER : public GENDRIVER
40 {
41 protected:
42   
43   MESH *          _ptrMesh;
44   // A VOIR FILE DESCRIPTEUR ? MED_FR::med_idt _medIdt;
45   string          _meshName;
46   /////
47
48   // tableau de correspondance des types géométriques de CASTEM -> MED
49   static const size_t nb_geometrie_gibi=47;
50   static const MED_EN::medGeometryElement geomGIBItoMED[nb_geometrie_gibi];
51   /////
52
53 public :
54
55   /*!
56     Constructor.
57   */
58   GIBI_MESH_DRIVER() ;
59   /*!
60     Constructor.
61   */
62   GIBI_MESH_DRIVER(const string & fileName,  
63                   MESH * ptrMesh, 
64                   MED_EN::med_mode_acces accessMode) ;
65   /*!
66     Copy constructor.
67   */
68   GIBI_MESH_DRIVER(const GIBI_MESH_DRIVER & driver) ;
69
70   /*!
71     Destructor.
72   */
73   virtual ~GIBI_MESH_DRIVER() ;
74
75   virtual void write( void ) const = 0 ;
76   virtual void read ( void ) = 0 ;
77
78   /*!
79     Set the name of the MESH asked in file.
80
81     It could be different than the name of the MESH object.
82   */
83   void   setMeshName(const string & meshName) ;
84   /*!
85     Get the name of the MESH asked in file.
86   */
87   string getMeshName() const ;
88
89   static MED_EN::medGeometryElement gibi2medGeom( size_t gibiTypeNb );
90   static int med2gibiGeom( MED_EN::medGeometryElement medGeomType );
91
92  private:
93   virtual GENDRIVER * copy ( void ) const = 0 ;
94
95 };
96
97
98 class GIBI_MESH_RDONLY_DRIVER : public virtual GIBI_MESH_DRIVER
99 {
100 public :
101   
102   /*!
103     Constructor.
104   */
105   GIBI_MESH_RDONLY_DRIVER() ;
106   /*!
107     Constructor.
108   */
109   GIBI_MESH_RDONLY_DRIVER(const string & fileName, MESH * ptrMesh) ;
110   /*!
111     Copy constructor.
112   */
113   GIBI_MESH_RDONLY_DRIVER(const GIBI_MESH_RDONLY_DRIVER & driver) ;
114
115   /*!
116     Destructor.
117   */
118   virtual ~GIBI_MESH_RDONLY_DRIVER() ;
119   
120   /*!
121     Return a MEDEXCEPTION : it is the read-only driver.
122   */
123   void write( void ) const throw (MEDEXCEPTION);
124   /*!
125     Read MESH in the specified file.
126   */
127   void read ( void ) throw (MEDEXCEPTION);
128
129   void open();
130   void close();
131
132 protected:
133
134   bool readFile(_intermediateMED*  medi, bool readFields );
135
136   void fillMesh(_intermediateMED* medi);
137
138 private:
139  
140   GENDRIVER * copy ( void ) const ;
141
142   // fields and methods for file reading
143
144   bool getLine(char* & line);
145   bool getNextLine (char* & line, bool raiseIfNot = true ) throw (MEDEXCEPTION)
146   {
147     if ( getLine( line )) return true;
148     if ( raiseIfNot ) throw MEDEXCEPTION(LOCALIZED(STRING("Unexpected EOF on ln ")<<_lineNb));
149     return false;
150   }
151   void initNameReading(int nbValues, int width = 8)  // FORMAT(8(1X,A8))
152   { init( nbValues, 72 / ( width + 1 ), width, 1 ); }
153   void initIntReading(int nbValues) //  FORMAT(10I8)
154   { init( nbValues, 10, 8 ); }
155   void initDoubleReading(int nbValues) //  FORMAT(1P,3E22.14)
156   { init( nbValues, 3, 22 ); }
157   void init( int nbToRead, int nbPosInLine, int width, int shift = 0 );
158   bool more() const { return ( _iRead < _nbToRead && _curPos ); }
159   void next();
160   char* str() const { return _curPos; }
161   int index() const { return _iRead; }
162   int getInt() const { return atoi( str() ); }
163   double getDouble() const { return atof( str() ); }
164   string getName() const;
165
166   // line getting
167   int   _File;
168   char* _start;
169   char* _ptr;
170   char* _eptr;
171   int   _lineNb;
172
173   // string reading
174   int _iPos, _nbPosInLine, _width, _shift;
175   int _iRead, _nbToRead;
176   char* _curPos;
177 };
178
179 /*!
180
181   Driver Med for MESH : Write only.
182
183   Implement write method.
184
185 */
186
187 class GIBI_MESH_WRONLY_DRIVER : public virtual GIBI_MESH_DRIVER {
188   
189 public :
190   
191   /*!
192     Constructor.
193   */
194   GIBI_MESH_WRONLY_DRIVER() ;
195   /*!
196     Constructor.
197   */
198   GIBI_MESH_WRONLY_DRIVER(const string & fileName, MESH * ptrMesh) ;
199   /*!
200     Copy constructor.
201   */
202   GIBI_MESH_WRONLY_DRIVER(const GIBI_MESH_WRONLY_DRIVER & driver) ;
203
204   /*!
205     Destructor.
206   */
207   virtual ~GIBI_MESH_WRONLY_DRIVER() ;
208
209   /*!
210     Write MESH in the specified file.
211   */
212   void write( void ) const throw (MEDEXCEPTION);
213   /*!
214     Return a MEDEXCEPTION : it is the write-only driver.
215   */
216   void read ( void ) throw (MEDEXCEPTION);
217
218   void open();
219   void close();
220
221  protected:
222
223   fstream _gibi;
224
225   /*!
226     Add a support to write. Return true if a support will be written
227   */
228   bool addSupport( const SUPPORT * support );
229   /*!
230     return submesh id and size for a support. Call it after writeSupportsAndMesh()
231   */
232   int getSubMeshIdAndSize(const SUPPORT *        support,
233                           list<pair<int,int> > & idsAndSizes ) const;
234   /*!
235     Write MESH and _supports.
236   */
237   void writeSupportsAndMesh();
238   /*!
239     Write the record closing file
240   */
241   void writeLastRecord();
242
243   static void addName( map<string,int>& nameMap, string& name, int index, string prefix );
244
245   void writeNames( map<string,int>& nameMap );
246
247  private:
248
249   struct typeData
250   {
251     int         _nbElems;
252     const int * _ptrElemIDs; // elem ids or 
253     int         _elemID1;    // first elem id if isOnAllElements()
254     typeData(): _nbElems(0),  _ptrElemIDs(NULL), _elemID1(0) {}
255     typeData( int nbElems, const int * ptrElemIDs, int elemID1 )
256       : _nbElems(nbElems),  _ptrElemIDs(ptrElemIDs), _elemID1(elemID1) {}
257   };
258   struct supportData
259   {
260     typedef map< MED_EN::medGeometryElement, list< typeData > >::iterator typeIterator;
261     int                                         _id;
262     string                                      _cleanName;
263     map< MED_EN::medGeometryElement, list< typeData > > _types;
264     supportData(): _id(0) {}
265     int getNumberOfTypes() const { return _types.size(); }
266     int getNumberObjects() const
267     { return _types.size() < 2 ? _types.size() : _types.size() + !_cleanName.empty(); }
268     void addTypeData(MED_EN::medGeometryElement type, int nbElems,
269                      const int * ptrElemIDs,  int elemID1 )
270     { _types[type].push_back( typeData( nbElems, ptrElemIDs, elemID1 )); }
271   };
272
273   void writeElements (MED_EN::medGeometryElement geomType,
274                       list< typeData >&  typeDataList,
275                       const int *        nodalConnect,
276                       const int *        nodalConnectIndex);
277
278   map<const SUPPORT*, supportData> _supports;
279
280   GENDRIVER * copy ( void ) const ;
281 };
282
283
284 /*!
285
286   Driver GIBI for MESH : Read write.
287   - Use read method from GIBI_MESH_RDONLY_DRIVER
288   - Use write method from GIBI_MESH_WRONLY_DRIVER
289
290 */
291
292 class GIBI_MESH_RDWR_DRIVER : public GIBI_MESH_RDONLY_DRIVER, public GIBI_MESH_WRONLY_DRIVER {
293
294 public :
295
296   /*!
297     Constructor.
298   */
299   GIBI_MESH_RDWR_DRIVER() ;
300   /*!
301     Constructor.
302   */
303   GIBI_MESH_RDWR_DRIVER(const string & fileName, MESH * ptrMesh) ;
304   /*!
305     Copy constructor.
306   */
307   GIBI_MESH_RDWR_DRIVER(const GIBI_MESH_RDWR_DRIVER & driver) ;
308
309   /*!
310     Destructor.
311   */
312   ~GIBI_MESH_RDWR_DRIVER() ;
313
314   /*!
315     Write MESH in the specified file.
316   */
317   void write(void) const throw (MEDEXCEPTION);
318   /*!
319     Read MESH in the specified file.
320   */
321   void read (void) throw (MEDEXCEPTION);
322
323   void open();
324   void close();
325
326  private:
327   GENDRIVER * copy(void) const ;
328
329 };
330
331 class GIBI_MED_RDONLY_DRIVER : public GIBI_MESH_RDONLY_DRIVER {
332
333   MED * _med;
334
335 public:
336   
337   /*!
338     Constructor.
339   */
340   GIBI_MED_RDONLY_DRIVER() ;
341   /*!
342     Constructor.
343   */
344   GIBI_MED_RDONLY_DRIVER(const string & fileName, MED * ptrMed) ;
345   /*!
346     Copy constructor.
347   */
348   GIBI_MED_RDONLY_DRIVER(const GIBI_MED_RDONLY_DRIVER & driver) ;
349
350   /*!
351     Destructor.
352   */
353   virtual ~GIBI_MED_RDONLY_DRIVER() ;
354   
355   /*!
356     Read MESH in the specified file.
357   */
358   void read ( void ) throw (MEDEXCEPTION);
359
360 private:
361  
362   GENDRIVER * copy ( void ) const ;
363
364 };
365
366 /*!
367
368   Driver Med for MESH : Write only.
369
370   Implement write method.
371
372 */
373
374 class GIBI_MED_WRONLY_DRIVER : public GIBI_MESH_WRONLY_DRIVER {
375
376   MED * _med;
377
378 public :
379   
380   /*!
381     Constructor.
382   */
383   GIBI_MED_WRONLY_DRIVER() ;
384   /*!
385     Constructor. To write a mesh and all fields on it
386   */
387   GIBI_MED_WRONLY_DRIVER(const string & fileName, MED * ptrMed, MESH * ptrMesh) ;
388   /*!
389     Copy constructor.
390   */
391   GIBI_MED_WRONLY_DRIVER(const GIBI_MED_WRONLY_DRIVER & driver) ;
392
393   /*!
394     Destructor.
395   */
396   virtual ~GIBI_MED_WRONLY_DRIVER() ;
397
398   /*!
399     Write MESH in the specified file.
400   */
401   void write( void ) const throw (MEDEXCEPTION);
402
403   //int getSupports(const FIELD_* field, list<const SUPPORT*>& supList) const;
404
405 private:
406
407   GENDRIVER * copy ( void ) const ;
408 };
409
410
411 /*!
412
413   Driver GIBI for MESH : Read write.
414   - Use read method from GIBI_MED_RDONLY_DRIVER
415   - Use write method from GIBI_MED_WRONLY_DRIVER
416
417 */
418
419 // class GIBI_MED_RDWR_DRIVER : public GIBI_MED_RDONLY_DRIVER, public GIBI_MED_WRONLY_DRIVER {
420
421 // public :
422
423 //   /*!
424 //     Constructor.
425 //   */
426 //   GIBI_MED_RDWR_DRIVER() ;
427 //   /*!
428 //     Constructor.
429 //   */
430 //   GIBI_MED_RDWR_DRIVER(const string & fileName, MESH * ptrMesh) ;
431 //   /*!
432 //     Copy constructor.
433 //   */
434 //   GIBI_MED_RDWR_DRIVER(const GIBI_MED_RDWR_DRIVER & driver) ;
435
436 //   /*!
437 //     Destructor.
438 //   */
439 //   ~GIBI_MED_RDWR_DRIVER() ;
440
441 //   /*!
442 //     Write MESH in the specified file.
443 //   */
444 //   void write(void) const throw (MEDEXCEPTION);
445 //   /*!
446 //     Read MESH in the specified file.
447 //   */
448 //   void read (void) throw (MEDEXCEPTION);
449
450 // private:
451 //   GENDRIVER * copy(void) const ;
452
453 // };
454 };
455
456
457 #endif /* GIBI_MESH_DRIVER_HXX */