Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDMEM / MEDMEM_GMesh.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 // File      : MEDMEM_GMesh.hxx
23 // Created   : Fri Jul 23 10:17:08 2010
24 // Author    : Edward AGAPOV (eap)
25 //
26
27 #ifndef __MEDMEM_GMesh_HXX__
28 #define __MEDMEM_GMesh_HXX__
29
30 #include "MEDMEM.hxx"
31 #include "MEDMEM_define.hxx"
32 #include "MEDMEM_Exception.hxx"
33 #include "MEDMEM_RCBase.hxx"
34 #include "MEDMEM_GenDriver.hxx"
35 #include "MEDMEM_FieldForward.hxx"
36 #include "MEDMEM_Support.hxx"
37
38 #include <map>
39
40 namespace MEDMEM {
41
42 class FAMILY;
43 class GROUP;
44 class SUPPORT;
45 class GMESH;
46 class MESH;
47
48 MEDMEM_EXPORT std::ostream & operator<<(std::ostream &os, const GMESH &me);
49
50 /*!
51  * \brief Base class of structured and unstructured meshes
52  */
53 class MEDMEM_EXPORT GMESH: public RCBASE
54 {
55  protected:
56
57   std::string          _name;
58   std::string          _description;
59   int                  _spaceDimension;
60
61   std::vector<FAMILY*> _familyNode;
62   std::vector<FAMILY*> _familyCell;
63   std::vector<FAMILY*> _familyFace;
64   std::vector<FAMILY*> _familyEdge;
65
66   std::vector<GROUP*>  _groupNode;
67   std::vector<GROUP*>  _groupCell;
68   std::vector<GROUP*>  _groupFace;
69   std::vector<GROUP*>  _groupEdge;
70
71   std::vector<GENDRIVER *> _drivers; // Storage of the drivers currently in use
72
73   std::map<MED_EN::medEntityMesh,SUPPORT*> _entitySupport;
74
75   virtual bool isEmpty() const = 0;
76
77   // Add your personnal driver line (step 2)
78   friend class MED_MESH_RDONLY_DRIVER;
79   friend class MED_MESH_WRONLY_DRIVER;
80
81   friend class GIBI_MESH_RDONLY_DRIVER;
82   friend class GIBI_MESH_WRONLY_DRIVER;
83   friend class GIBI_MESH_RDWR_DRIVER;
84
85   friend class PORFLOW_MESH_RDONLY_DRIVER;
86   friend class PORFLOW_MESH_WRONLY_DRIVER;
87   friend class PORFLOW_MESH_RDWR_DRIVER;
88
89   friend class VTK_MESH_DRIVER;
90
91   friend class ENSIGHT_MESH_RDONLY_DRIVER;
92
93  public:
94
95   GMESH();
96   GMESH(GMESH &m);
97   virtual ~GMESH();
98
99   virtual void init();
100   GMESH & operator=(const GMESH &m);
101   virtual bool operator==(const GMESH& other) const;
102   virtual bool deepCompare(const GMESH& other) const = 0;
103
104   friend std::ostream & operator<<(std::ostream &os, const GMESH &me);
105   virtual void printMySelf(std::ostream &os) const = 0;
106
107   int  addDriver(driverTypes            driverType,
108                  const std::string &    fileName  ="Default File Name.med",
109                  const std::string &    driverName="Default Mesh Name",
110                  MED_EN::med_mode_acces access=MED_EN::RDWR);
111   int  addDriver(GENDRIVER & driver);
112   void rmDriver(int index=0);
113
114   virtual void read(int index=0);
115   virtual void read(const GENDRIVER & genDriver);
116   virtual void read(driverTypes        driverType,
117                     const std::string& filename,
118                     const std::string& meshname);
119
120   virtual void write(int index=0) const ;
121   virtual void write(const GENDRIVER & genDriver,
122                      MED_EN::med_mode_acces medMode=MED_EN::WRONLY) const;
123   virtual void write(driverTypes        driverType,
124                      const std::string& filename,
125                      const std::string& meshname="",
126                      MED_EN::med_mode_acces medMode=MED_EN::WRONLY) const;
127
128   void                               setName(std::string name);
129   std::string                        getName() const;
130
131   void                               setDescription(std::string description);
132   std::string                        getDescription() const;
133
134   int                                getSpaceDimension() const;
135   virtual int                        getMeshDimension() const = 0;
136
137   virtual bool                       getIsAGrid() const = 0;
138
139   virtual std::string                getCoordinatesSystem() const = 0;
140   virtual const std::string *        getCoordinatesNames() const = 0;
141   virtual const std::string *        getCoordinatesUnits() const = 0;
142
143   virtual int                        getNumberOfNodes() const = 0;
144   virtual int                        getNumberOfTypes(MED_EN::medEntityMesh Entity) const = 0;
145   virtual int                        getNumberOfElements(MED_EN::medEntityMesh      Entity,
146                                                          MED_EN::medGeometryElement Type) const = 0;
147   virtual const MED_EN::medGeometryElement * getTypes(MED_EN::medEntityMesh Entity) const = 0;
148   virtual MED_EN::medGeometryElement getElementType(MED_EN::medEntityMesh Entity,
149                                                     int                   Number) const = 0;
150
151
152   virtual int                        getNumberOfFamilies(MED_EN::medEntityMesh Entity) const;
153   virtual const std::vector<FAMILY*> getFamilies(MED_EN::medEntityMesh Entity) const;
154   virtual const FAMILY*              getFamily(MED_EN::medEntityMesh Entity,int i) const;
155
156   virtual int                        getNumberOfGroups(MED_EN::medEntityMesh Entity) const;
157   virtual const std::vector<GROUP*>  getGroups(MED_EN::medEntityMesh Entity) const;
158   virtual const GROUP*               getGroup(MED_EN::medEntityMesh Entity,int i) const;
159   virtual const GROUP*               getGroup(const std::string& name) const throw (MEDEXCEPTION);
160
161   /*!
162    * \brief Return unstructured mesh. Call removeReference() after having finished using it!!!
163    */
164   virtual const MESH *               convertInMESH() const = 0;
165
166
167   virtual SUPPORT *                  getBoundaryElements(MED_EN::medEntityMesh Entity) const
168                                                 throw (MEDEXCEPTION) = 0;
169   virtual SUPPORT *                  getSkin(const SUPPORT * Support3D)
170                                                 throw (MEDEXCEPTION) = 0;
171
172   virtual const SUPPORT *            getSupportOnAll(MED_EN::medEntityMesh Entity) const
173                                                 throw (MEDEXCEPTION);
174
175   static SUPPORT *                   mergeSupports(const std::vector<SUPPORT *> Supports)
176                                                 throw (MEDEXCEPTION) ;
177   static SUPPORT *                   intersectSupports(const std::vector<SUPPORT *> Supports)
178                                                 throw (MEDEXCEPTION) ;
179
180   virtual SUPPORT *buildSupportOnNodeFromElementList(const std::list<int>& listOfElt, MED_EN::medEntityMesh entity) const throw (MEDEXCEPTION) = 0;
181   virtual void     fillSupportOnNodeFromElementList(const std::list<int>& listOfElt, SUPPORT *supportToFill) const throw (MEDEXCEPTION) = 0;
182   virtual SUPPORT *buildSupportOnElementsFromElementList(const std::list<int>& listOfElt, MED_EN::medEntityMesh entity) const throw (MEDEXCEPTION);
183
184
185   virtual FIELD<double>* getVolume (const SUPPORT * Support, bool isAbs = true) const
186                                 throw (MEDEXCEPTION) = 0;
187                                 // Support must be on 3D elements
188   virtual FIELD<double>* getArea (const SUPPORT * Support) const
189                                 throw (MEDEXCEPTION) = 0;
190                                 // Support must be on 2D elements
191   virtual FIELD<double>* getLength (const SUPPORT * Support) const
192                                 throw (MEDEXCEPTION) = 0;
193                                 // Support must be on 1D elements
194   virtual FIELD<double>* getNormal (const SUPPORT * Support) const
195                                 throw (MEDEXCEPTION) = 0;
196                                 // Support must be on 2D elements
197   virtual FIELD<double>* getBarycenter (const SUPPORT * Support) const
198                                 throw (MEDEXCEPTION) = 0;
199   
200   virtual vector< vector<double> >   getBoundingBox() const = 0;
201
202   template<class T> static FIELD<T>* mergeFields(const std::vector< FIELD<T> * > & others,
203                                                  bool meshCompare=false);
204
205   void                               createFamilies();
206   void                               createGroups();
207 };
208
209 // ================================================================================================
210 //Create a new FIELD that should be deallocated based on a SUPPORT that should be deallocated too.
211 // ================================================================================================
212 template<class T>
213 FIELD<T> * GMESH::mergeFields(const std::vector< FIELD<T> * > & others, bool meshCompare)
214 {
215   const char * LOC = "MESH::mergeFields(const vector< FIELD<T>* >& others,bool meshCompare): ";
216   BEGIN_OF_MED(LOC);
217   int i,j;
218   if(others.size()==0)
219     return 0;
220   std::vector<SUPPORT *> sup;
221   typename std::vector< FIELD<T, FullInterlace>* >::const_iterator iter;
222   iter = others.begin();
223   MED_EN::med_type_champ valueType = (*iter)->getValueType();
224   for(iter=others.begin();iter!=others.end();iter++)
225     {
226       MED_EN::med_type_champ valueTypeIter = (*iter)->getValueType();
227       if (valueTypeIter != valueType)
228         throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<" Fields vector have not the same value type"));
229
230       sup.push_back((SUPPORT *)(*iter)->getSupport());
231     }
232   iter=others.begin();
233   SUPPORT *retSup=mergeSupports(sup);
234   int retNumberOfComponents=(*iter)->getNumberOfComponents();
235   FIELD<T, FullInterlace> *ret=new FIELD<T, FullInterlace>(retSup, retNumberOfComponents);
236   T* valuesToSet=(T*)ret->getValue();
237   int nbOfEltsRetSup=retSup->getNumberOfElements(MED_EN::MED_ALL_ELEMENTS);
238   T* tempValues=new T[retNumberOfComponents];
239   if(retSup->isOnAllElements())
240     {
241       for(i=0;i<nbOfEltsRetSup;i++)
242         {
243           bool found=false;
244           for(iter=others.begin();iter!=others.end() && !found;iter++)
245             {
246               found=(*iter)->getValueOnElement(i+1,tempValues);
247               if(found)
248                 for(j=0;j<retNumberOfComponents;j++)
249                   valuesToSet[i*retNumberOfComponents+j]=tempValues[j];
250             }
251         }
252     }
253   else
254     {
255       const int *eltsRetSup=retSup->getNumber(MED_EN::MED_ALL_ELEMENTS);
256       for(i=0;i<nbOfEltsRetSup;i++)
257         {
258           bool found=false;
259           for(iter=others.begin();iter!=others.end() && !found;iter++)
260             {
261               found=(*iter)->getValueOnElement(eltsRetSup[i],tempValues);
262               if(found)
263                 for(j=0;j<retNumberOfComponents;j++)
264                   valuesToSet[i*retNumberOfComponents+j]=tempValues[j];
265             }
266           if(!found)
267             throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<" Merging error due to an error in merging support"));
268         }
269     }
270   if(retSup)
271     retSup->removeReference();
272   delete [] tempValues;
273   END_OF_MED(LOC);
274   return ret;
275 }
276 }
277 #endif