]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_Family.cxx
Salome HOME
NRI : Merge from V1_2.
[modules/med.git] / src / MEDMEM / MEDMEM_Family.cxx
1 //  MED MEDMEM : MED files in memory
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : MEDMEM_Family.cxx
25 //  Module : MED
26
27 using namespace std;
28 /*
29  File MEDMEM_Family.cxx
30  $Header$
31 */
32
33 #include "MEDMEM_Mesh.hxx"
34 #include "MEDMEM_Family.hxx"
35
36 FAMILY::FAMILY():_identifier(0), _numberOfAttribute(0), 
37                  _attributeIdentifier((int*)NULL),_attributeValue((int*)NULL), _attributeDescription((string*)NULL),
38                  _numberOfGroup(0), _groupName((string*)NULL) 
39 {
40     MESSAGE("FAMILY::FAMILY()");
41 };
42
43 FAMILY::FAMILY(MESH* Mesh, int Identifier, string Name, int NumberOfAttribute,
44                int *AttributeIdentifier, int *AttributeValue, string AttributeDescription,
45                int NumberOfGroup, string GroupName,
46                int * MEDArrayNodeFamily,
47                int ** MEDArrayCellFamily,
48                int ** MEDArrayFaceFamily,
49                int ** MEDArrayEdgeFamily
50                ): SUPPORT(Mesh,Name),
51                   _identifier(Identifier), 
52                   _numberOfAttribute(NumberOfAttribute), 
53                   _numberOfGroup(NumberOfGroup)
54 {
55   MESSAGE("FAMILY(int Identifier, string Name, int NumberOfAttribute,int *AttributeIdentifier,int *AttributeValue,string AttributeDescription,int NumberOfGroup,string GroupName, int ** Number) : "<<Identifier);
56
57   _isOnAllElts = false ;
58   // replace them by pointerOf ?
59   _attributeIdentifier = new int[_numberOfAttribute] ;
60   memcpy(_attributeIdentifier,AttributeIdentifier,_numberOfAttribute*sizeof(int));
61   _attributeValue = new int[_numberOfAttribute] ;
62   memcpy(_attributeValue,AttributeValue,_numberOfAttribute*sizeof(int));
63
64   _attributeDescription=new string[_numberOfAttribute];
65   for (int i=0;i<NumberOfAttribute;i++) {
66     _attributeDescription[i].assign(AttributeDescription,i*MED_TAILLE_DESC,MED_TAILLE_DESC);
67     _attributeDescription[i].erase(strlen(_attributeDescription[i].c_str()));
68     //SCRUTE(_attributeDescription[i]);
69   }
70  
71   _groupName=new string[_numberOfGroup];
72   for (int i=0;i<NumberOfGroup;i++) {
73     _groupName[i].assign(GroupName,i*MED_TAILLE_LNOM,MED_TAILLE_LNOM);
74     _groupName[i].erase(strlen(_groupName[i].c_str()));
75     //SCRUTE(_groupName[i]);
76   }
77
78   // well, we must set SUPPORT attribut
79   _description = "FAMILY" ;
80   // on which geometric type :
81   bool Find = false ;
82   
83
84   // ************************ NODES RELATED PART *************************************************
85   // Scan every node family number <NodeFamilyNumber[i]> in order to create 
86   // the list <tmp_NodesList> of node numbers <i+1> belonging to the family <_identifier>
87   int NumberOfNodes         = _mesh->getNumberOfNodes();
88   int NumberOfNodesInFamily = 0 ;
89   //int * NodeFamilyNumber    = _mesh->getMEDArrayNodeFamily() ;   // EF : TEMPORAIRE !!
90   int * tmp_NodesList       = new int[NumberOfNodes] ;           // Un peu brutal...., oui mais ce n'est qu'un tableau de travail !
91   for (int i=0; i<NumberOfNodes; i++)
92     if ( _identifier == MEDArrayNodeFamily[i] ) {
93       tmp_NodesList[NumberOfNodesInFamily]=i+1 ;
94       NumberOfNodesInFamily++;
95     }
96   //SCRUTE(NumberOfNodesInFamily);
97
98   // If we found nodes set the family attributes adequatly
99   if (NumberOfNodesInFamily>0) {
100     
101     Find = true ;
102     
103     _entity = MED_NODE ;
104 //      _numberOfGeometricType = 1 ;
105 //      _geometricType = new medGeometryElement[1] ;
106 //      _geometricType[0]=MED_NONE ;
107     
108     // family on all NODE
109     if (NumberOfNodesInFamily==NumberOfNodes) {
110       _isOnAllElts = true ;
111       update();
112     } else {
113       _numberOfGeometricType = 1 ;
114       if (_geometricType!=NULL) delete[] _geometricType ;
115       _geometricType = new medGeometryElement[1] ;
116       _geometricType[0]=MED_NONE ;
117       _isOnAllElts= false ;
118       if (_numberOfElements!=NULL) delete[] _numberOfElements ;
119       _numberOfElements = new int[1] ;
120       _numberOfElements[0]=NumberOfNodesInFamily ;
121       _totalNumberOfElements=NumberOfNodesInFamily;
122       
123 //        _number=new MEDSKYLINEARRAY(1,NumberOfNodesInFamily) ;
124 //        int * NumberIndex = _number->getIndex();
125 //        int * NumberValue = _number->getValue();
126       int * NumberIndex = new int[2];
127       int * NumberValue = new int[NumberOfNodesInFamily];
128
129       NumberIndex[0]=1;                          //set the MEDSKYLINEARRAY Index table
130       NumberIndex[1]=1+NumberOfNodesInFamily;    //set the MEDSKYLINEARRAY Index table
131       for(int i=0; i<NumberOfNodesInFamily; i++) // OH LA LA... 
132         NumberValue[i]=tmp_NodesList[i] ;        // RESIZE de tmp_NodesList serait plus efficace !!!!!!!!
133       // PG : pas de problème, si ca marche. Il faudra déplacer le delete !
134       if(_number!=NULL) delete _number ;
135       _number=new MEDSKYLINEARRAY(1,NumberOfNodesInFamily,NumberIndex,NumberValue) ;
136       delete[] NumberIndex ;
137       delete[] NumberValue ;
138     }
139   }
140   delete[] tmp_NodesList ;
141   
142
143
144   // ************************ CELLS RELATED PART *************************************************
145   // If we previously found nodes in our family don't scan the CELLS because a
146   // family contains different geometic types of only one entity type.
147   // ?? Scan every cell family number <NodeFamilyNumber[i]> in order to create 
148   // ?? the list <tmp_NodesList> of node numbers <i+1> belonging to the family <_identifier>
149   if (!Find) {
150     Find = build(MED_CELL,MEDArrayCellFamily) ;
151   }
152   // on face ?
153   if (!Find) {
154     if ((_mesh->existConnectivity(MED_NODAL,MED_FACE))|(_mesh->existConnectivity(MED_DESCENDING,MED_FACE))) {
155       Find = build(MED_FACE,MEDArrayFaceFamily) ;
156     }
157   }
158   
159   // on edge ?
160   if (!Find) {
161     if ((_mesh->existConnectivity(MED_NODAL,MED_EDGE))|(_mesh->existConnectivity(MED_DESCENDING,MED_EDGE))) {
162       Find = build(MED_EDGE,MEDArrayEdgeFamily) ;
163     }
164   }
165   // That's all !
166
167   // if not find : no entity in familly !!!
168   if (!Find) {
169     _numberOfGeometricType = 0 ;
170     _isOnAllElts = false ;
171     MESSAGE ("FAMILY() : No entity found !") ;
172   } 
173   // already done by support !!!!
174 //    else { // set gauss point number to be equal one !
175 //      _numberOfGaussPoint = new int[_numberOfGeometricType] ;
176 //      for (int i=0; i<_numberOfGeometricType; i++)
177 //        _numberOfGaussPoint[i]=1 ;
178 //    }
179 };
180
181 FAMILY::FAMILY(const FAMILY & m):SUPPORT(m)
182 {
183   MESSAGE("FAMILY::FAMILY(FAMILY & m)");
184   _identifier = m._identifier;
185   _numberOfAttribute = m._numberOfAttribute;
186   if (m._attributeIdentifier != NULL)
187     {
188       _attributeIdentifier = new int[m._numberOfAttribute];
189       memcpy(_attributeIdentifier,m._attributeIdentifier,m._numberOfAttribute*sizeof(int));
190     }
191   else
192     _attributeIdentifier = (int *) NULL;
193   if (m._attributeValue != NULL)
194     {
195       _attributeValue = new int[m._numberOfAttribute];
196       memcpy(_attributeValue,m._attributeValue,m._numberOfAttribute*sizeof(int));
197     }
198   else
199     _attributeValue = (int *) NULL;
200   if (m._attributeDescription != NULL)
201     {
202       _attributeDescription = new string[m._numberOfAttribute];
203       for (int i=0;i<m._numberOfAttribute;i++)
204         _attributeDescription[i] = m._attributeDescription[i];
205     }
206   else
207     _attributeDescription = (string *) NULL;
208   _numberOfGroup = m._numberOfGroup;
209   if (m._groupName != NULL)
210     {
211       _groupName = new string[m._numberOfGroup];
212       for (int i=0;i<m._numberOfGroup;i++)
213         _groupName[i]=m._groupName[i];
214     }
215   else
216     _groupName = (string *) NULL;
217 };
218
219 FAMILY::FAMILY(const SUPPORT & s):SUPPORT(s)
220 {
221   MESSAGE("FAMILY::FAMILY(const SUPPORT & s)");
222
223   _identifier = 0;
224   _numberOfAttribute = 0;
225   _attributeIdentifier = (int*) NULL;
226   _attributeValue = (int*) NULL;
227   _attributeDescription = (string*) NULL;
228   _numberOfGroup = 0;
229   _groupName= (string*) NULL;
230 };
231
232 FAMILY::FAMILY(FAMILY & m):SUPPORT(m)
233 {
234   _identifier = m._identifier;
235   _numberOfAttribute = m._numberOfAttribute;
236   if (m._attributeIdentifier != NULL)
237     {
238       _attributeIdentifier = new int[m._numberOfAttribute];
239       memcpy(_attributeIdentifier,m._attributeIdentifier,m._numberOfAttribute*sizeof(int));
240     }
241   else
242     _attributeIdentifier = (int *) NULL;
243   if (m._attributeValue != NULL)
244     {
245       _attributeValue = new int[m._numberOfAttribute];
246       memcpy(_attributeValue,m._attributeValue,m._numberOfAttribute*sizeof(int));
247     }
248   else
249     _attributeValue = (int *) NULL;
250   if (m._attributeDescription != NULL)
251     {
252       _attributeDescription = new string[m._numberOfAttribute];
253       for (int i=0;i<m._numberOfAttribute;i++)
254         _attributeDescription[i] = m._attributeDescription[i];
255     }
256   else
257     _attributeDescription = (string *) NULL;
258   _numberOfGroup = m._numberOfGroup;
259   if (m._groupName != NULL)
260     {
261       _groupName = new string[m._numberOfGroup];
262       for (int i=0;i<m._numberOfGroup;i++)
263         _groupName[i]=m._groupName[i];
264     }
265   else
266     _groupName = (string *) NULL;
267 };
268
269 FAMILY::~FAMILY() 
270 {
271     MESSAGE("~FAMILY()");
272     if(_attributeIdentifier!=NULL)
273        delete[] _attributeIdentifier;
274     if(_attributeValue!=NULL)
275        delete[] _attributeValue;
276     if(_attributeDescription!=NULL)
277        delete[] _attributeDescription;
278     if(_groupName!=NULL)
279        delete[] _groupName;
280 };
281   
282 FAMILY & FAMILY::operator=(const FAMILY &fam) 
283 {
284     MESSAGE("FAMILY::operator=");
285     _identifier = fam._identifier;
286     _numberOfAttribute = fam._numberOfAttribute; 
287     _attributeIdentifier = fam._attributeIdentifier;
288     _attributeValue = fam._attributeValue;
289     _attributeDescription = fam._attributeDescription;
290     _numberOfGroup = fam._numberOfGroup;
291     _groupName = fam._groupName;
292     return *this;
293 };
294
295 ostream & operator<<(ostream &os, FAMILY &myFamily)
296 {
297   // how do cast without duplicate ?
298   os << (SUPPORT) myFamily;
299
300   os << "  - Identifier : "<<myFamily.getIdentifier()<<endl;
301   int numberofattributes = myFamily.getNumberOfAttributes();
302   os << "  - Attributes ("<<numberofattributes<<") :"<<endl;
303   for (int j=1;j<numberofattributes+1;j++)
304     os << "    * "<<myFamily.getAttributeIdentifier(j)<<" : "<<myFamily.getAttributeValue(j)<<", "<<myFamily.getAttributeDescription(j).c_str()<<endl;
305   int numberofgroups = myFamily.getNumberOfGroups();
306   os << "  - Groups ("<<numberofgroups<<") :"<<endl;
307   for (int j=1;j<numberofgroups+1;j++)
308     os << "    * "<<myFamily.getGroupName(j).c_str()<<endl ;
309
310   return os;
311 };
312
313 bool FAMILY::build(medEntityMesh Entity,int **FamilyNumber /* from MED file */)
314 {
315   MESSAGE("FAMILY::build(medEntityMesh Entity,int **FamilyNumber /* from MED file */)");
316   bool Find = false ;
317   // Get types information from <_mesh>
318   int    numberOfTypes             = _mesh->getNumberOfTypes(Entity) ;
319   const medGeometryElement * types       = _mesh->getTypes(Entity) ;
320   
321   int *  numberOfElementsInFamily         = new int[numberOfTypes] ;
322   int    numberOfElementTypesInFamily     = 0 ;
323   
324   medGeometryElement * tmp_Types  = new medGeometryElement[numberOfTypes];
325   int ** tmp_ElementsLists                = new int*[numberOfTypes] ;
326   //  int *  GeometricTypeNumber           = new int[numberOfTypes] ;
327   const int *  GlobalNumberingIndex          = _mesh->getGlobalNumberingIndex(Entity);
328   
329   // we search for all elements in this family
330   for (int TypeNumber=0; TypeNumber < numberOfTypes; TypeNumber++) {
331     
332     int NumberOfElements             = _mesh->getNumberOfElements(Entity,types[TypeNumber]) ;
333     int NumberOfElementsInThisFamily = 0 ;
334     int * ElementsOfThisFamilyNumber = FamilyNumber[TypeNumber];
335     int * tmp_ElementsList           = new int[NumberOfElements];
336       
337     for (int i=0; i<NumberOfElements; i++)
338       if (_identifier == ElementsOfThisFamilyNumber[i]) {
339         tmp_ElementsList[NumberOfElementsInThisFamily]=i+GlobalNumberingIndex[TypeNumber] ;
340         NumberOfElementsInThisFamily++;
341       }
342     
343     if (NumberOfElementsInThisFamily>0) {// we have found some elements
344       numberOfElementsInFamily[numberOfElementTypesInFamily]=NumberOfElementsInThisFamily;
345
346       //int * ElementsList = tmp_ElementsList.resize(NumberOfElementsInThisFamily);
347       int * ElementsList = new int[NumberOfElementsInThisFamily] ;
348       memcpy(ElementsList,tmp_ElementsList,sizeof(int)*NumberOfElementsInThisFamily); // RESIZE de tmp_NodesList serait plus efficace !!!!!!!!
349         
350       tmp_ElementsLists[numberOfElementTypesInFamily]=ElementsList ;
351       tmp_Types[numberOfElementTypesInFamily]=types[TypeNumber];
352       //      GeometricTypeNumber[numberOfElementTypesInFamily]=TypeNumber+1;
353       numberOfElementTypesInFamily++;
354       //      delete [] ElementsList;
355     }
356     delete[] tmp_ElementsList;
357   }
358   
359   // we define all attribut in SUPPORT :
360   if (numberOfElementTypesInFamily>0) { // we have found elements
361     Find = true ;
362     _entity = Entity ;
363     _numberOfGeometricType = numberOfElementTypesInFamily ;
364     if (_geometricType!=NULL) delete[] _geometricType ;
365     _geometricType = new medGeometryElement[numberOfElementTypesInFamily] ;
366     _isOnAllElts = false ;
367     if (_numberOfElements!=NULL) delete[] _numberOfElements ;
368     _numberOfElements = new int[numberOfElementTypesInFamily] ;
369     _totalNumberOfElements=0;
370
371     //_numberOfGaussPoint = new int[numberOfElementTypesInFamily] ;
372
373     //int * numberIndex = new int[numberOfElementTypesInFamily+1];
374     //numberIndex[0]=1;
375     for (int i=0; i<numberOfElementTypesInFamily; i++) {
376       _geometricType[i]=tmp_Types[i] ;
377       //numberIndex[i+1]=numberIndex[i]+numberOfElementsInFamily[i];
378       _numberOfElements[i]=numberOfElementsInFamily[i]; // plutot un resize !!
379       _totalNumberOfElements+=_numberOfElements[i];
380       //_numberOfGaussPoint[i]=1;
381     }
382     //    delete[] numberOfElementsInFamily;
383     //    delete[] tmp_Types;
384     //    delete[] GeometricTypeNumber;
385       
386     // family on all ELEMENT ?
387     if (_totalNumberOfElements == _mesh->getNumberOfElements(Entity,MED_ALL_ELEMENTS)) {
388       _isOnAllElts = true ;
389       // all others attributs are rights !
390       for (int i=0; i<_numberOfGeometricType; i++)
391         delete[] tmp_ElementsLists[i];
392     } else {
393       int *NumberValue = new int[_totalNumberOfElements];
394       int *NumberIndex = new int[_numberOfGeometricType+1];
395       NumberIndex[0]=1;
396       for (int i=0; i<_numberOfGeometricType; i++) {
397         NumberIndex[i+1]=NumberIndex[i]+_numberOfElements[i];
398         for (int j=NumberIndex[i]; j<NumberIndex[i+1]; j++)
399           NumberValue[j-1]=tmp_ElementsLists[i][j-NumberIndex[i]];
400         delete[] tmp_ElementsLists[i];
401       }
402       if(_number!=NULL) delete _number ;
403       _number = new MEDSKYLINEARRAY(_numberOfGeometricType,_totalNumberOfElements,NumberIndex,NumberValue);
404       delete[] NumberIndex ;
405       delete[] NumberValue ;
406     }
407   }
408   delete[] tmp_Types;
409   delete[] numberOfElementsInFamily;
410
411   delete[] tmp_ElementsLists;
412
413   return Find ;
414 }
415