]> 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() 
233 {
234     MESSAGE("~FAMILY()");
235     if(_attributeIdentifier!=NULL)
236        delete[] _attributeIdentifier;
237     if(_attributeValue!=NULL)
238        delete[] _attributeValue;
239     if(_attributeDescription!=NULL)
240        delete[] _attributeDescription;
241     if(_groupName!=NULL)
242        delete[] _groupName;
243 };
244   
245 FAMILY & FAMILY::operator=(const FAMILY &fam) 
246 {
247     MESSAGE("FAMILY::operator=");
248     _identifier = fam._identifier;
249     _numberOfAttribute = fam._numberOfAttribute; 
250     _attributeIdentifier = fam._attributeIdentifier;
251     _attributeValue = fam._attributeValue;
252     _attributeDescription = fam._attributeDescription;
253     _numberOfGroup = fam._numberOfGroup;
254     _groupName = fam._groupName;
255     return *this;
256 };
257
258 ostream & operator<<(ostream &os, FAMILY &myFamily)
259 {
260   // how do cast without duplicate ?
261   os << (SUPPORT) myFamily;
262
263   os << "  - Identifier : "<<myFamily.getIdentifier()<<endl;
264   int numberofattributes = myFamily.getNumberOfAttributes();
265   os << "  - Attributes ("<<numberofattributes<<") :"<<endl;
266   for (int j=1;j<numberofattributes+1;j++)
267     os << "    * "<<myFamily.getAttributeIdentifier(j)<<" : "<<myFamily.getAttributeValue(j)<<", "<<myFamily.getAttributeDescription(j).c_str()<<endl;
268   int numberofgroups = myFamily.getNumberOfGroups();
269   os << "  - Groups ("<<numberofgroups<<") :"<<endl;
270   for (int j=1;j<numberofgroups+1;j++)
271     os << "    * "<<myFamily.getGroupName(j).c_str()<<endl ;
272
273   return os;
274 };
275
276 bool FAMILY::build(medEntityMesh Entity,int **FamilyNumber /* from MED file */)
277 {
278   MESSAGE("FAMILY::build(medEntityMesh Entity,int **FamilyNumber /* from MED file */)");
279   bool Find = false ;
280   // Get types information from <_mesh>
281   int    numberOfTypes             = _mesh->getNumberOfTypes(Entity) ;
282   const medGeometryElement * types       = _mesh->getTypes(Entity) ;
283   
284   int *  numberOfElementsInFamily         = new int[numberOfTypes] ;
285   int    numberOfElementTypesInFamily     = 0 ;
286   
287   medGeometryElement * tmp_Types  = new medGeometryElement[numberOfTypes];
288   int ** tmp_ElementsLists                = new int*[numberOfTypes] ;
289   //  int *  GeometricTypeNumber           = new int[numberOfTypes] ;
290   const int *  GlobalNumberingIndex          = _mesh->getGlobalNumberingIndex(Entity);
291   
292   // we search for all elements in this family
293   for (int TypeNumber=0; TypeNumber < numberOfTypes; TypeNumber++) {
294     
295     int NumberOfElements             = _mesh->getNumberOfElements(Entity,types[TypeNumber]) ;
296     int NumberOfElementsInThisFamily = 0 ;
297     int * ElementsOfThisFamilyNumber = FamilyNumber[TypeNumber];
298     int * tmp_ElementsList           = new int[NumberOfElements];
299       
300     for (int i=0; i<NumberOfElements; i++)
301       if (_identifier == ElementsOfThisFamilyNumber[i]) {
302         tmp_ElementsList[NumberOfElementsInThisFamily]=i+GlobalNumberingIndex[TypeNumber] ;
303         NumberOfElementsInThisFamily++;
304       }
305     
306     if (NumberOfElementsInThisFamily>0) {// we have found some elements
307       numberOfElementsInFamily[numberOfElementTypesInFamily]=NumberOfElementsInThisFamily;
308
309       //int * ElementsList = tmp_ElementsList.resize(NumberOfElementsInThisFamily);
310       int * ElementsList = new int[NumberOfElementsInThisFamily] ;
311       memcpy(ElementsList,tmp_ElementsList,sizeof(int)*NumberOfElementsInThisFamily); // RESIZE de tmp_NodesList serait plus efficace !!!!!!!!
312         
313       tmp_ElementsLists[numberOfElementTypesInFamily]=ElementsList ;
314       tmp_Types[numberOfElementTypesInFamily]=types[TypeNumber];
315       //      GeometricTypeNumber[numberOfElementTypesInFamily]=TypeNumber+1;
316       numberOfElementTypesInFamily++;
317       //      delete [] ElementsList;
318     }
319     delete[] tmp_ElementsList;
320   }
321   
322   // we define all attribut in SUPPORT :
323   if (numberOfElementTypesInFamily>0) { // we have found elements
324     Find = true ;
325     _entity = Entity ;
326     _numberOfGeometricType = numberOfElementTypesInFamily ;
327     if (_geometricType!=NULL) delete[] _geometricType ;
328     _geometricType = new medGeometryElement[numberOfElementTypesInFamily] ;
329     _isOnAllElts = false ;
330     if (_numberOfElements!=NULL) delete[] _numberOfElements ;
331     _numberOfElements = new int[numberOfElementTypesInFamily] ;
332     _totalNumberOfElements=0;
333
334     //_numberOfGaussPoint = new int[numberOfElementTypesInFamily] ;
335
336     //int * numberIndex = new int[numberOfElementTypesInFamily+1];
337     //numberIndex[0]=1;
338     for (int i=0; i<numberOfElementTypesInFamily; i++) {
339       _geometricType[i]=tmp_Types[i] ;
340       //numberIndex[i+1]=numberIndex[i]+numberOfElementsInFamily[i];
341       _numberOfElements[i]=numberOfElementsInFamily[i]; // plutot un resize !!
342       _totalNumberOfElements+=_numberOfElements[i];
343       //_numberOfGaussPoint[i]=1;
344     }
345     //    delete[] numberOfElementsInFamily;
346     //    delete[] tmp_Types;
347     //    delete[] GeometricTypeNumber;
348       
349     // family on all ELEMENT ?
350     if (_totalNumberOfElements == _mesh->getNumberOfElements(Entity,MED_ALL_ELEMENTS)) {
351       _isOnAllElts = true ;
352       // all others attributs are rights !
353       for (int i=0; i<_numberOfGeometricType; i++)
354         delete[] tmp_ElementsLists[i];
355     } else {
356       int *NumberValue = new int[_totalNumberOfElements];
357       int *NumberIndex = new int[_numberOfGeometricType+1];
358       NumberIndex[0]=1;
359       for (int i=0; i<_numberOfGeometricType; i++) {
360         NumberIndex[i+1]=NumberIndex[i]+_numberOfElements[i];
361         for (int j=NumberIndex[i]; j<NumberIndex[i+1]; j++)
362           NumberValue[j-1]=tmp_ElementsLists[i][j-NumberIndex[i]];
363         delete[] tmp_ElementsLists[i];
364       }
365       if(_number!=NULL) delete _number ;
366       _number = new MEDSKYLINEARRAY(_numberOfGeometricType,_totalNumberOfElements,NumberIndex,NumberValue);
367       delete[] NumberIndex ;
368       delete[] NumberValue ;
369     }
370   }
371   delete[] tmp_Types;
372   delete[] numberOfElementsInFamily;
373
374   delete[] tmp_ElementsLists;
375
376   return Find ;
377 }
378