]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_Meshing.cxx
Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/med.git] / src / MEDMEM / MEDMEM_Meshing.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 /*
21   File MEDMEM_Meshing.cxx
22   $Header$
23 */
24
25 #include <string>
26
27 #include "MEDMEM_Utilities.hxx"
28 #include "MEDMEM_STRING.hxx"
29 #include "MEDMEM_Exception.hxx"
30 #include "MEDMEM_define.hxx"
31
32 #include "MEDMEM_Meshing.hxx"
33 #include "MEDMEM_Group.hxx"
34
35 using namespace std;
36 using namespace MEDMEM;
37 using namespace MED_EN;
38
39 /*! Create an empty MESH. */
40 MESHING::MESHING(): MESH()
41 {
42   MESSAGE("MESHING::MESHING()");
43   SCRUTE(_coordinate);
44   SCRUTE(_connectivity);
45 }
46
47 MESHING::~MESHING()
48 {
49   MESSAGE("Deletinh MESHING !!");
50 }
51
52 /*! Set the dimension of the space */
53 void MESHING::setSpaceDimension(const int SpaceDimension)
54 {
55   _spaceDimension = SpaceDimension ;
56 }
57
58 /*! Set the dimension of the MESHING */
59 void MESHING::setMeshDimension(const int MeshDimension)
60 {
61    _meshDimension = MeshDimension ;
62    if (_connectivity)
63      _connectivity->setEntityDimension(MeshDimension);
64 }
65
66 /*! Set the number of nodes used in the MESH */
67 void MESHING::setNumberOfNodes(const int NumberOfNodes)
68 {
69   _numberOfNodes = NumberOfNodes ;
70   if (_connectivity)
71     _connectivity->setNumberOfNodes(NumberOfNodes);
72 }
73
74 /*! 
75   Set the whole coordinates array in a given system and interlacing mode.
76   The system coordinates are :
77     - "CARTESIAN"
78     - "CYLINDRICAL"
79     - "SPHERICAL"
80   The interlacing mode are :
81     - MED_NO_INTERLACE   :  X1 X2 Y1 Y2 Z1 Z2
82     - MED_FULL_INTERLACE :  X1 Y1 Z1 X2 Y2 Z2
83 */
84 void MESHING::setCoordinates(const int SpaceDimension,
85                              const int NumberOfNodes,
86                              const double * Coordinates,
87                              const string System,
88                              const medModeSwitch Mode)
89 {
90   setSpaceDimension(SpaceDimension);
91   setNumberOfNodes(NumberOfNodes);
92
93   SCRUTE(_coordinate);
94   SCRUTE(_connectivity);
95   //if (NULL != _coordinate) delete _coordinate;
96
97   _coordinate = new COORDINATE(SpaceDimension,
98                                NumberOfNodes,
99                                Mode);
100   _coordinate->setCoordinates(Mode,Coordinates);
101   _coordinate->setCoordinatesSystem(System);
102 }
103
104 /*! Set the system in which coordinates are given (CARTESIAN,CYLINDRICAL,SPHERICAL) __??MED_CART??__. */
105 void MESHING::setCoordinatesSystem(const string System)
106   throw (MEDEXCEPTION)
107 {
108   if (NULL == _coordinate)
109     throw MEDEXCEPTION(LOCALIZED("MESHING::setCoordinatesSystem : no coordinates defined"));
110   _coordinate->setCoordinatesSystem(System);
111 }
112
113 /*! Set the coordinate names array ("x       ","y       ","z       ")
114   of size n*MED_TAILLE_PNOM
115 */
116 void MESHING::setCoordinatesNames(const string * name)
117 {
118 //    int SpaceDimension = getSpaceDimension() ;
119 //    _coordinate->setCoordinatesNames(SpaceDimension,name);
120   _coordinate->setCoordinatesNames(name);
121 }
122
123 /*!
124   Set the (i+1)^th component of coordinate names array
125   ("x       ","y       ","z       ") of size n*MED_TAILLE_PNOM
126 */
127 void MESHING::setCoordinateName(const string name, const int i)
128 {
129   _coordinate->setCoordinateName(name,i);
130 }
131
132 /*! Set the coordinate unit names array ("cm       ","cm       ","cm       ")
133   of size n*MED_TAILLE_PNOM
134 */
135 void MESHING::setCoordinatesUnits(const string * units)
136 {
137 //    int SpaceDimension = getSpaceDimension() ;
138 //    _coordinate->setCoordinatesUnits(SpaceDimension,units);
139   _coordinate->setCoordinatesUnits(units);
140 }
141
142 /*!
143   Set the (i+1)^th component of the coordinate unit names array
144   ("cm       ","cm       ","cm       ") of size n*MED_TAILLE_PNOM
145 */
146 void MESHING::setCoordinateUnit(const string unit, const int i)
147 {
148   _coordinate->setCoordinateUnit(unit,i);
149 }
150
151 /*!
152   Create a new connectivity object with the given number of type and
153   entity. If a connectivity already exist, delete it !
154
155   For exemple setNumberOfTypes(3,MED_CELL) create a connectivity with 3 
156   medGeometryElement in MESH for MED_CELL entity (like MED_TETRA4, 
157   MED_PYRA5 and MED_HEXA6 for example).
158
159   Return an exception if could not create the connectivity (as if we set 
160   MED_FACE connectivity before MED_CELL).
161 */
162 void MESHING::setNumberOfTypes(const int NumberOfTypes,
163                                const medEntityMesh Entity) 
164   throw (MEDEXCEPTION)
165 {
166   const char * LOC = "MESHING::setNumberOfTypes( medEntityMesh ) : ";
167
168   // No defined for MED_NODE !
169   if (Entity == MED_NODE)
170     throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"Not required with MED_NODE !"));
171
172   if (MED_CELL == Entity) {
173     SCRUTE(_connectivity);
174 //     if (_connectivity != (CONNECTIVITY *) NULL)
175 //       delete _connectivity ;
176     _connectivity = new CONNECTIVITY(NumberOfTypes,Entity) ;
177
178   } else {
179
180     if (_connectivity == NULL) // we must have defined MED_CELL connectivity
181       throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"No connectivity on MED_CELL defined !"));
182
183     if (MED_FACE == Entity)
184       if (3 != getSpaceDimension())
185         throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"No connectivity on MED_FACE could be defined in non 3D space !"));
186     
187     if (MED_EDGE == Entity)
188       if (3 == getSpaceDimension()) {
189         if (!_connectivity->existConnectivity(MED_NODAL,MED_FACE))
190           throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"No connectivity on MED_FACE defined !"));
191       } else {
192         if (2 != getSpaceDimension())
193           throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"Could not set connectivity on MED_EDGE !"));
194       }
195     // all rigth, we could create connectivity !
196     CONNECTIVITY * myConnectivity = new CONNECTIVITY(NumberOfTypes,Entity) ;
197     myConnectivity->setEntityDimension(_connectivity->getEntityDimension()-1);
198     _connectivity->setConstituent(myConnectivity);
199   }
200 }
201
202 /*!
203   Set the list of geometric types used by a given entity.
204   medEntityMesh entity could be : MED_CELL, MED_FACE, MED_EDGE
205
206   REM : Don't use MED_NODE and MED_ALL_ENTITIES
207
208   If entity is not defined, throw an exception.
209 */
210 void MESHING::setTypes(const medGeometryElement * Types,
211                        const medEntityMesh entity)
212   throw (MEDEXCEPTION)
213 {
214   if (entity == MED_NODE)
215     throw MEDEXCEPTION(LOCALIZED("MESHING::setTypes : Not defined with MED_NODE entity !"));
216
217   if (_connectivity == NULL)
218     throw MEDEXCEPTION(LOCALIZED("MESHING::setTypes : No connectivity defined !"));
219
220   _connectivity->setGeometricTypes(Types,entity) ;
221 }
222
223 /*!
224   Set the number of elements for each geometric type of given entity.
225
226   Example : setNumberOfElements({12,23},MED_FACE)
227   if we have two type of face (MED_TRIA3 and MED_QUAD4), 
228   we set 12 triangles and 23 quadrangles.
229 */
230 void MESHING::setNumberOfElements(const int * NumberOfElements,
231                                   const medEntityMesh Entity)
232   throw (MEDEXCEPTION)
233 {
234   const char * LOC = "MESHING::setNumberOfElements(const int *, medEntityMesh) : " ;
235
236   if (Entity==MED_NODE)
237     throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"Not defined with MED_NODE entity !"));
238
239   if (_connectivity == (CONNECTIVITY*)NULL)
240     throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"No connectivity defined !"));
241
242   int NumberOfTypes = getNumberOfTypes(Entity) ;
243   int * Count = new int[NumberOfTypes+1] ;
244   Count[0]=1 ;
245   for (int i=0; i<NumberOfTypes; i++)
246     Count[i+1]=Count[i]+NumberOfElements[i] ;
247   _connectivity->setCount(Count,Entity) ;
248   delete[] Count ;
249 }
250
251 /*!
252   Set the nodal connectivity for one geometric type of the given entity.
253
254   Example : setConnectivity({1,2,3,1,4,2},MED_FACE,MED_TRIA3)
255   Define 2 triangles face defined with nodes 1,2,3 and 1,4,2.
256 */
257 void MESHING::setConnectivity(const int * Connectivity,
258                               const medEntityMesh Entity,
259                               const medGeometryElement Type)
260   throw (MEDEXCEPTION)
261 {
262   const char * LOC = "MESHING::setConnectivity : " ;
263
264   if (Entity==MED_NODE)
265     throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"Not defined with MED_NODE entity !"));
266
267   if (_connectivity == (CONNECTIVITY*)NULL)
268     throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"No connectivity defined !"));
269
270   _connectivity->setNodal(Connectivity,Entity,Type) ;
271 }
272
273 void MESHING::setPolygonsConnectivity     (const int * ConnectivityIndex,
274                                            const int * ConnectivityValue,
275                                            int nbOfPolygons,
276                                            const MED_EN::medEntityMesh Entity)
277   throw (MEDEXCEPTION)
278 {
279   if (_connectivity == (CONNECTIVITY*)NULL)
280     throw MEDEXCEPTION("No connectivity defined !");
281   
282   _connectivity->setPolygonsConnectivity(MED_NODAL, Entity, ConnectivityValue, ConnectivityIndex,ConnectivityIndex[nbOfPolygons]-1,nbOfPolygons) ;
283 }
284
285 void MESHING::setPolyhedraConnectivity     (const int * PolyhedronIndex,
286                                             const int * FacesIndex,
287                                             const int * Nodes,
288                                             int nbOfPolyhedra,
289                                             const MED_EN::medEntityMesh Entity)
290   throw (MEDEXCEPTION)
291 {
292   if (_connectivity == (CONNECTIVITY*)NULL)
293     throw MEDEXCEPTION("No connectivity defined !");
294   if(_connectivity->getPolyTypeRelativeTo()==MED_EN::MED_POLYHEDRA)
295     {
296       int nbOfFacesOfAllPolyhedra=PolyhedronIndex[nbOfPolyhedra]-1;
297       _connectivity->setPolyhedronConnectivity(MED_NODAL, Nodes, PolyhedronIndex, FacesIndex[nbOfFacesOfAllPolyhedra]-1 , nbOfPolyhedra, FacesIndex, nbOfFacesOfAllPolyhedra) ;
298     }
299   else
300     throw MEDEXCEPTION("Invalid connectivity for polyhedra !!!");
301 }
302
303 /*!
304   NOT YET IMPLEMENTED !! WARNING
305 */
306 void MESHING::setConnectivities (const int * ConnectivityIndex,
307                                  const int * ConnectivityValue,
308                                  const medConnectivity ConnectivityType,
309                                  const medEntityMesh Entity)
310   throw (MEDEXCEPTION)
311 {
312   const char * LOC = "MESHING::setConnectivities : " ;
313   SCRUTE(Entity);
314   SCRUTE(ConnectivityType);
315   SCRUTE(ConnectivityValue);
316   SCRUTE(ConnectivityIndex);
317
318   throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"Not Yet Implemented :: Warning !"));
319 }
320
321 /*!
322 */
323
324 // void MESHING::setGroup(const string name,
325 //                     const string description,
326 //                     const int NumberOfElements,
327 //                     const int * ElementsNumbers,
328 //                     const medEntityMesh Entity)
329 // {
330 //   GROUP * myGroup = new GROUP() ;
331 //   myGroup->setMesh(*this) ;
332 //   myGroup->setName(name) ;
333 //   myGroup->setDescription(description) ;
334 //   myGroup->setEntity(Entity) ;
335 //   // medEntityMesh and medGeometryElement ???
336 //   myGroup->setNumberOfGeometricType(NumberOfType) ;
337 //   myGroup->setGeometricType(Type) ;
338 //   myGroup->setNumberOfGaussPoint(NumberOfGaussPoint) ;
339 //   myGroup->setNumberOfElements(NumberOfElements) ;
340 //   myGroup->setNumber(Number) ;
341 // }
342
343 void MESHING::addGroup(const GROUP & Group)
344   throw (MEDEXCEPTION)
345 {
346   const char * LOC = "MESHING::addGroup : " ;
347
348   GROUP * myGroup = new GROUP(Group) ;
349   switch(Group.getEntity()){
350   case MED_CELL : {
351     _groupCell.push_back(myGroup);
352     break;
353   }
354   case MED_FACE : {
355      _groupFace.push_back(myGroup);
356     break;
357   }
358   case MED_EDGE : {
359      _groupEdge.push_back(myGroup);
360     break;
361   }
362   case MED_NODE : {
363      _groupNode.push_back(myGroup);
364     break;
365   }
366   default :
367     throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"Bad Entity !"));
368   }
369 }