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