]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_GibiMeshDriver.hxx
Salome HOME
Fix problem of make distcheck
[modules/med.git] / src / MEDMEM / MEDMEM_GibiMeshDriver.hxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #ifndef GIBI_MESH_DRIVER_HXX
24 #define GIBI_MESH_DRIVER_HXX
25
26 #include <MEDMEM.hxx>
27
28 #include <string>
29 #include <vector>
30 #include <map>
31 #include <fstream>
32 #include <list>
33
34 #include "MEDMEM_define.hxx"
35 #include "MEDMEM_GenDriver.hxx"
36
37 #include "MEDMEM_STRING.hxx"
38 #include "MEDMEM_Exception.hxx"
39 #include "MEDMEM_Utilities.hxx"
40
41 #include "MEDMEM_FieldForward.hxx"
42
43 /////
44 #include <sstream>
45 #include <iomanip>
46 /////
47
48 #ifdef WIN32
49 #else
50 #define HAS_XDR
51 #endif
52
53 /*!
54
55   Driver GIBI for MESH.
56
57   Generic part : implement the readopen and close methods.
58
59 */
60
61 namespace MEDMEM {
62 class GMESH;
63 class MESH;
64 class FAMILY;
65 class GROUP;
66 class SUPPORT;
67 class CONNECTIVITY;
68 class FIELD_;
69 struct _intermediateMED;
70
71 // IMP 0020434: mapping GIBI names to MED names
72 struct nameGIBItoMED {
73   // GIBI value
74   int gibi_pile;    // PILE_SOUS_MAILLAGE or PILE_FIELD/PILE_NODES_FIELD, or PILE_STRINGS(for components)
75   int gibi_id;
76   string gibi_name; // used only for components
77   // MED value
78   // med_pile = 27; // PILE_STRINGS
79   int med_id;       // used only on reading
80   string med_name;  // used only on writing
81 };
82
83 class MEDMEM_EXPORT GIBI_MESH_DRIVER : public GENDRIVER
84 {
85 protected:
86
87   GMESH *         _mesh;
88   std::string    _meshName;
89
90   // This enumeration is used to substitude static const
91   // memers data causing link errors on VC7 compiler.
92   enum
93   {
94     nb_geometrie_gibi = 47
95   };
96   // tableau de correspondance des types géométriques de CASTEM -> MED
97   //static const size_t nb_geometrie_gibi=47;
98   static const MED_EN::medGeometryElement geomGIBItoMED[nb_geometrie_gibi];
99   /////
100
101 public :
102
103   /*!
104     Constructor.
105   */
106   GIBI_MESH_DRIVER() ;
107   /*!
108     Constructor.
109   */
110   GIBI_MESH_DRIVER(const string &         fileName,
111                    GMESH *                ptrMesh,
112                    MED_EN::med_mode_acces accessMode) ;
113   /*!
114     Copy constructor.
115   */
116   GIBI_MESH_DRIVER(const GIBI_MESH_DRIVER & driver) ;
117
118   /*!
119     Destructor.
120   */
121   virtual ~GIBI_MESH_DRIVER() ;
122
123   virtual void write( void ) const = 0 ;
124   virtual void read ( void ) = 0 ;
125
126   /*!
127     Set the name of the MESH asked in file.
128
129     It could be different than the name of the MESH object.
130   */
131   void   setMeshName(const string & meshName) ;
132   /*!
133     Get the name of the MESH asked in file.
134   */
135   string getMeshName() const ;
136
137   static MED_EN::medGeometryElement gibi2medGeom( size_t gibiTypeNb );
138   static int med2gibiGeom( MED_EN::medGeometryElement medGeomType );
139
140  private:
141   virtual GENDRIVER * copy ( void ) const = 0 ;
142
143 };
144
145
146 class MEDMEM_EXPORT GIBI_MESH_RDONLY_DRIVER : public virtual GIBI_MESH_DRIVER
147 {
148 public :
149
150   /*!
151     Constructor.
152   */
153   GIBI_MESH_RDONLY_DRIVER() ;
154   /*!
155     Constructor.
156   */
157   GIBI_MESH_RDONLY_DRIVER(const string & fileName, MESH * ptrMesh) ;
158   /*!
159     Copy constructor.
160   */
161   GIBI_MESH_RDONLY_DRIVER(const GIBI_MESH_RDONLY_DRIVER & driver) ;
162
163   /*!
164     Destructor.
165   */
166   virtual ~GIBI_MESH_RDONLY_DRIVER() ;
167
168   /*!
169     Return a MEDEXCEPTION : it is the read-only driver.
170   */
171   void write( void ) const throw (MEDEXCEPTION);
172   /*!
173     Read MESH in the specified file.
174   */
175   void read ( void ) throw (MEDEXCEPTION);
176
177   void open();
178   void close();
179
180 protected:
181
182   bool readFile(_intermediateMED*  medi, bool readFields );
183
184   void fillMesh(_intermediateMED* medi);
185
186   void updateSupports();
187
188 private:
189
190   GENDRIVER * copy ( void ) const ;
191
192   // fields and methods for file reading
193
194   bool getLine(char* & line);
195   bool getNextLine (char* & line, bool raiseIfNot = true ) throw (MEDEXCEPTION)
196   {
197     if ( getLine( line )) return true;
198     if ( raiseIfNot ) throw MEDEXCEPTION(LOCALIZED(STRING("Unexpected EOF on ln ")<<_lineNb));
199     return false;
200   }
201   void initNameReading(int nbValues, int width = 8);  // FORMAT(8(1X,A8))
202   void initIntReading(int nbValues); //  FORMAT(10I8)
203   void initDoubleReading(int nbValues); //  FORMAT(1P,3E22.14)
204   void init( int nbToRead, int nbPosInLine, int width, int shift = 0 );
205   bool more() const;
206   void next();
207   char* str() const { return _curPos; }
208   int index() const { return _iRead; }
209   int getInt() const;
210   float getFloat() const;
211   double getDouble() const;
212   string getName() const;
213
214   // line getting
215   int   _File;
216   char* _start;
217   char* _ptr;
218   char* _eptr;
219   int   _lineNb;
220
221   // string reading
222   int _iPos, _nbPosInLine, _width, _shift;
223   int _iRead, _nbToRead;
224   char* _curPos;
225
226   // xdr
227 #ifdef HAS_XDR
228   bool _is_xdr;
229   FILE* _xdrs_file;
230   void* _xdrs;
231   char* _xdr_cvals;
232   int* _xdr_ivals;
233   double* _xdr_dvals;
234   int _xdr_kind;
235   enum
236     {
237       _xdr_kind_null,
238       _xdr_kind_char,
239       _xdr_kind_int,
240       _xdr_kind_double
241     };
242 #endif
243 };
244
245 /*!
246
247   Driver Med for MESH : Write only.
248
249   Implement write method.
250
251 */
252
253 class MEDMEM_EXPORT GIBI_MESH_WRONLY_DRIVER : public virtual GIBI_MESH_DRIVER {
254
255 public :
256
257   /*!
258     Constructor.
259   */
260   GIBI_MESH_WRONLY_DRIVER() ;
261   /*!
262     Constructor.
263   */
264   GIBI_MESH_WRONLY_DRIVER(const string & fileName, GMESH * ptrMesh) ;
265   /*!
266     Copy constructor.
267   */
268   GIBI_MESH_WRONLY_DRIVER(const GIBI_MESH_WRONLY_DRIVER & driver) ;
269
270   /*!
271     Destructor.
272   */
273   virtual ~GIBI_MESH_WRONLY_DRIVER() ;
274
275   /*!
276     Write MESH in the specified file.
277   */
278   void write( void ) const throw (MEDEXCEPTION);
279   /*!
280     Return a MEDEXCEPTION : it is the write-only driver.
281   */
282   void read ( void ) throw (MEDEXCEPTION);
283
284   void open();
285   void close();
286
287  protected:
288
289   fstream _gibi;
290
291   /*!
292     Add a support to write. Return true if a support will be written
293   */
294   bool addSupport( const SUPPORT * support );
295   /*!
296     return submesh id and size for a support. Call it after writeSupportsAndMesh()
297   */
298   int getSubMeshIdAndSize(const SUPPORT *                  support,
299                           std::list<std::pair<int,int> > & idsAndSizes ) const;
300   /*!
301     Write MESH and _supports.
302   */
303   void writeSupportsAndMesh(list<nameGIBItoMED>& listGIBItoMED_mail);
304   /*!
305    * Store MED names of supports and fields.
306    * All MED names are written in STRINGS PILE, and the correspondence
307    * between GIBI and MED names is written as TABLE "noms_med"
308   */
309   void writeMEDNames(const std::list<nameGIBItoMED>& listGIBItoMED_mail,
310                      const std::list<nameGIBItoMED>& listGIBItoMED_cham,
311                      const std::list<nameGIBItoMED>& listGIBItoMED_comp);
312   /*!
313     Write the record closing file
314   */
315   void writeLastRecord();
316
317   //static void addName( map<string,int>& nameMap, string& name, int index, string prefix );
318   static void addName( std::map<std::string,int>& nameMap,
319                        std::map<std::string,int>& namePrefixesMap,
320                        const std::string&         name,
321                        int                        index);
322
323   void writeNames( std::map<std::string,int>& nameMap );
324
325  private:
326
327   struct typeData
328   {
329     int         _nbElems;
330     const int * _ptrElemIDs; // elem ids or
331     int         _elemID1;    // first elem id if isOnAllElements()
332     typeData( int nbElems=0, const int * ptrElemIDs=NULL, int elemID1=0 )
333       : _nbElems(nbElems),  _ptrElemIDs(ptrElemIDs), _elemID1(elemID1) {}
334   };
335   struct supportData
336   {
337     typedef map< MED_EN::medGeometryElement, list< typeData > >::iterator typeIterator;
338     int                                         _id;
339     string                                      _cleanName;
340     map< MED_EN::medGeometryElement, list< typeData > > _types;
341     supportData(): _id(0) {}
342     int getNumberOfTypes() const { return _types.size(); }
343     int getNumberObjects() const
344     { return _types.size() < 2 ? _types.size() : _types.size() + !_cleanName.empty(); }
345     void addTypeData(MED_EN::medGeometryElement type, int nbElems,
346                      const int * ptrElemIDs,  int elemID1 )
347     { _types[type].push_back( typeData( nbElems, ptrElemIDs, elemID1 )); }
348   };
349
350   void writeElements (MED_EN::medGeometryElement geomType,
351                       list< typeData >&  typeDataList,
352                       const int *        nodalConnect,
353                       const int *        nodalConnectIndex);
354
355   map<const SUPPORT*, supportData> _supports;
356
357   GENDRIVER * copy ( void ) const ;
358 };
359
360
361 /*!
362
363   Driver GIBI for MESH : Read write.
364   - Use read method from GIBI_MESH_RDONLY_DRIVER
365   - Use write method from GIBI_MESH_WRONLY_DRIVER
366
367 */
368
369 class MEDMEM_EXPORT GIBI_MESH_RDWR_DRIVER : public GIBI_MESH_RDONLY_DRIVER, public GIBI_MESH_WRONLY_DRIVER {
370
371 public :
372
373   /*!
374     Constructor.
375   */
376   GIBI_MESH_RDWR_DRIVER() ;
377   /*!
378     Constructor.
379   */
380   GIBI_MESH_RDWR_DRIVER(const std::string & fileName, MESH * ptrMesh) ;
381   /*!
382     Copy constructor.
383   */
384   GIBI_MESH_RDWR_DRIVER(const GIBI_MESH_RDWR_DRIVER & driver) ;
385
386   /*!
387     Destructor.
388   */
389   ~GIBI_MESH_RDWR_DRIVER() ;
390
391   /*!
392     Write MESH in the specified file.
393   */
394   void write(void) const throw (MEDEXCEPTION);
395   /*!
396     Read MESH in the specified file.
397   */
398   void read (void) throw (MEDEXCEPTION);
399
400   void open();
401   void close();
402
403  private:
404   GENDRIVER * copy(void) const ;
405
406 };
407
408 class MEDMEM_EXPORT GIBI_MED_RDONLY_DRIVER : public GIBI_MESH_RDONLY_DRIVER {
409
410   std::vector<FIELD_*> * _fields;
411
412 public:
413
414   /*!
415     Constructor.
416   */
417   GIBI_MED_RDONLY_DRIVER() ;
418   /*!
419     Constructor.
420   */
421   GIBI_MED_RDONLY_DRIVER(const std::string & fileName, std::vector<FIELD_*>& ptrFields) ;
422   /*!
423     Copy constructor.
424   */
425   GIBI_MED_RDONLY_DRIVER(const GIBI_MED_RDONLY_DRIVER & driver) ;
426
427   /*!
428     Destructor.
429   */
430   virtual ~GIBI_MED_RDONLY_DRIVER() ;
431
432   /*!
433     Read MESH and FIELDs in the specified file.
434   */
435   void read ( void ) throw (MEDEXCEPTION);
436
437   MESH* getMesh() const;
438
439 private:
440
441   GENDRIVER * copy ( void ) const ;
442
443 };
444
445 /*!
446
447   Driver Med for MESH : Write only.
448
449   Implement write method.
450
451 */
452
453 class MEDMEM_EXPORT GIBI_MED_WRONLY_DRIVER : public GIBI_MESH_WRONLY_DRIVER {
454
455   std::vector<const FIELD_*> _fields;
456
457 public :
458
459   /*!
460     Constructor.
461   */
462   GIBI_MED_WRONLY_DRIVER() ;
463   /*!
464     Constructor. To write a mesh and all fields on it
465   */
466   GIBI_MED_WRONLY_DRIVER(const std::string &               fileName,
467                          const std::vector<const FIELD_*>& fields,
468                          GMESH *                           ptrMesh);
469   /*!
470     Copy constructor.
471   */
472   GIBI_MED_WRONLY_DRIVER(const GIBI_MED_WRONLY_DRIVER & driver) ;
473
474   /*!
475     Destructor.
476   */
477   virtual ~GIBI_MED_WRONLY_DRIVER() ;
478
479   /*!
480     Write MESH in the specified file.
481   */
482   void write( void ) const throw (MEDEXCEPTION);
483
484   //int getSupports(const FIELD_* field, list<const SUPPORT*>& supList) const;
485
486 private:
487
488   GENDRIVER * copy ( void ) const ;
489 };
490
491 }
492
493
494 #endif /* GIBI_MESH_DRIVER_HXX */