Salome HOME
46ae6549104403da0ac27c0c0ede12a86dc6ba3e
[modules/smesh.git] / src / DriverMED / DriverMED_Family.cxx
1 //  SMESH DriverMED : tool to split groups on families
2 //
3 //  Copyright (C) 2003  CEA
4 // 
5 //  This library is free software; you can redistribute it and/or 
6 //  modify it under the terms of the GNU Lesser General Public 
7 //  License as published by the Free Software Foundation; either 
8 //  version 2.1 of the License. 
9 // 
10 //  This library is distributed in the hope that it will be useful, 
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
13 //  Lesser General Public License for more details. 
14 // 
15 //  You should have received a copy of the GNU Lesser General Public 
16 //  License along with this library; if not, write to the Free Software 
17 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
18 // 
19 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 //
21 //
22 //
23 //  File   : DriverMED_Family.cxx
24 //  Author : Julia DOROVSKIKH
25 //  Module : SMESH
26 //  $Header$
27
28 #include "DriverMED_Family.h"
29 #include "MED_Factory.hxx"
30
31 #include <sstream>      
32
33 using namespace std;
34
35 //=============================================================================
36 /*!
37  *  Default constructor
38  */
39 //=============================================================================
40 DriverMED_Family
41 ::DriverMED_Family():
42   myGroupAttributVal(0)
43 {}
44
45
46 //=============================================================================
47 const ElementsSet& 
48 DriverMED_Family
49 ::GetElements () const 
50
51   return myElements; 
52 }
53
54 int 
55 DriverMED_Family
56 ::GetId () const 
57
58   return myId; 
59 }
60
61 void 
62 DriverMED_Family
63 ::SetId (const int theId) 
64
65   myId = theId; 
66 }
67
68 void
69 DriverMED_Family
70 ::AddElement(const SMDS_MeshElement* theElement)
71 {
72   myElements.insert(theElement); 
73 }
74
75 void
76 DriverMED_Family
77 ::AddGroupName(std::string theGroupName)
78 {
79   myGroupNames.insert(theGroupName); 
80 }
81
82 void
83 DriverMED_Family
84 ::SetType(const SMDSAbs_ElementType theType) 
85
86   myType = theType; 
87 }
88
89 SMDSAbs_ElementType
90 DriverMED_Family
91 ::GetType()
92 {
93   return myType; 
94 }
95
96 bool
97 DriverMED_Family
98 ::MemberOf(std::string theGroupName) const
99
100   return myGroupNames.find(theGroupName) != myGroupNames.end(); 
101 }
102
103 const MED::TStringSet& 
104 DriverMED_Family
105 ::GetGroupNames () const 
106
107   return myGroupNames; 
108 }
109
110
111 int 
112 DriverMED_Family
113 ::GetGroupAttributVal() const 
114 {
115   return myGroupAttributVal;
116
117
118 void
119 DriverMED_Family
120 ::SetGroupAttributVal( int theValue) 
121 {
122   myGroupAttributVal = theValue;
123 }
124
125 bool
126 DriverMED_Family
127 ::IsEmpty () const 
128
129   return myElements.empty(); 
130 }
131
132 bool CompareColors( const SALOMEDS::Color& theColor, const SALOMEDS::Color& theRefColor )
133 {
134   if( fabs( theColor.R - theRefColor.R ) < 0.01 &&
135       fabs( theColor.G - theRefColor.G ) < 0.01 &&
136       fabs( theColor.B - theRefColor.B ) < 0.01 )
137     return true;
138
139   return false;
140 }
141
142 //=============================================================================
143 /*!
144  *  Split each group from list <aGroups> on some parts (families)
145  *  on the basis of the elements membership in other groups from this list.
146  *  Resulting families have no common elements.
147  */
148 //=============================================================================
149 DriverMED_FamilyPtrList 
150 DriverMED_Family
151 ::MakeFamilies(const SMESHDS_SubMeshPtrMap& theSubMeshes,
152                const SMESHDS_GroupBasePtrList& theGroups,
153                const bool doGroupOfNodes,
154                const bool doGroupOfEdges,
155                const bool doGroupOfFaces,
156                const bool doGroupOfVolumes)
157 {
158   DriverMED_FamilyPtrList aFamilies;
159
160   string anAllNodesGroupName = "Group_Of_All_Nodes";
161   string anAllEdgesGroupName = "Group_Of_All_Edges";
162   string anAllFacesGroupName = "Group_Of_All_Faces";
163   string anAllVolumesGroupName = "Group_Of_All_Volumes";
164
165   // Reserve four ids for families of free elements
166   // (1 - nodes, -1 - edges, -2 - faces, -3 - volumes).
167   // 'Free' means here not belonging to any group.
168   int aNodeFamId = FIRST_NODE_FAMILY;
169   int aElemFamId = FIRST_ELEM_FAMILY;
170
171   // Process sub-meshes
172   SMESHDS_SubMeshPtrMap::const_iterator aSMIter = theSubMeshes.begin();
173   for (; aSMIter != theSubMeshes.end(); aSMIter++)
174   {
175     const int anId = aSMIter->first;
176     SMESHDS_SubMesh* aSubMesh = aSMIter->second;
177     if ( aSubMesh->IsComplexSubmesh() )
178       continue; // submesh containing other submeshs
179     DriverMED_FamilyPtrList aSMFams = SplitByType(aSubMesh,anId);
180     DriverMED_FamilyPtrList::iterator aSMFamsIter = aSMFams.begin();
181     for (; aSMFamsIter != aSMFams.end(); aSMFamsIter++)
182     {
183       DriverMED_FamilyPtr aFam2 = (*aSMFamsIter);
184       DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
185       while (aFamsIter != aFamilies.end())
186       {
187         DriverMED_FamilyPtr aFam1 = *aFamsIter;
188         DriverMED_FamilyPtrList::iterator aCurrIter = aFamsIter++;
189         if (aFam1->myType == aFam2->myType)
190         {
191           DriverMED_FamilyPtr aCommon (new DriverMED_Family);
192           aFam1->Split(aFam2, aCommon);
193           if (!aCommon->IsEmpty())
194           {
195             aFamilies.push_back(aCommon);
196           }
197           if (aFam1->IsEmpty())
198           {
199             aFamilies.erase(aCurrIter);
200           }
201           if (aFam2->IsEmpty()) 
202             break;
203         }
204       }
205       // The rest elements of family
206       if (!aFam2->IsEmpty())
207       {
208         aFamilies.push_back(aFam2);
209       }
210     }
211   }
212
213   // Process groups
214   SMESHDS_GroupBasePtrList::const_iterator aGroupsIter;
215
216   int id = 0;
217   ColorMap aColorMap;
218   for (aGroupsIter = theGroups.begin(); aGroupsIter != theGroups.end(); aGroupsIter++)
219   {
220     SALOMEDS::Color aColor = (*aGroupsIter)->GetColor();
221
222     bool isFound = false;
223     for (ColorMap::iterator aColorIter = aColorMap.begin(); aColorIter != aColorMap.end(); aColorIter++)
224     {
225       SALOMEDS::Color aRefColor = aColorIter->second;
226       if( CompareColors( aColor, aRefColor ) )
227       {
228         isFound = true;
229         break;
230       }
231     }
232
233     if( !isFound )
234       aColorMap[ id++ ] = aColor;
235   }
236
237   for (aGroupsIter = theGroups.begin(); aGroupsIter != theGroups.end(); aGroupsIter++)
238   {
239     DriverMED_FamilyPtr aFam2 (new DriverMED_Family);
240     aFam2->Init(*aGroupsIter, aColorMap);
241
242     DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
243     while (aFamsIter != aFamilies.end())
244     {
245       DriverMED_FamilyPtr aFam1 = *aFamsIter;
246       DriverMED_FamilyPtrList::iterator aCurrIter = aFamsIter++;
247       if (aFam1->myType == aFam2->myType)
248       {
249         DriverMED_FamilyPtr aCommon (new DriverMED_Family);
250         aFam1->Split(aFam2, aCommon);
251         if (!aCommon->IsEmpty())
252         {
253           aCommon->SetGroupAttributVal(0);
254           aFamilies.push_back(aCommon);
255         }
256         if (aFam1->IsEmpty())
257         {
258           aFamilies.erase(aCurrIter);
259         }
260         if (aFam2->IsEmpty()) 
261           break;
262       }
263     }
264     // The rest elements of group
265     if (!aFam2->IsEmpty())
266     {
267       aFamilies.push_back(aFam2);
268     }
269   }
270
271   DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
272   for (; aFamsIter != aFamilies.end(); aFamsIter++)
273   {
274     DriverMED_FamilyPtr aFam = *aFamsIter;
275     if (aFam->myType == SMDSAbs_Node) {
276       aFam->SetId(aNodeFamId++);
277       if (doGroupOfNodes) aFam->myGroupNames.insert(anAllNodesGroupName);
278     }
279     else {
280       aFam->SetId(aElemFamId--);
281       if (aFam->myType == SMDSAbs_Edge) {
282         if (doGroupOfEdges) aFam->myGroupNames.insert(anAllEdgesGroupName);
283       }
284       else if (aFam->myType == SMDSAbs_Face) {
285         if (doGroupOfFaces) aFam->myGroupNames.insert(anAllFacesGroupName);
286       }
287       else if (aFam->myType == SMDSAbs_Volume) {
288         if (doGroupOfVolumes) aFam->myGroupNames.insert(anAllVolumesGroupName);
289       }
290     }
291   }
292
293   // Create families for elements, not belonging to any group
294   if (doGroupOfNodes)
295   {
296     DriverMED_FamilyPtr aFreeNodesFam (new DriverMED_Family);
297     aFreeNodesFam->SetId(REST_NODES_FAMILY);
298     aFreeNodesFam->myType = SMDSAbs_Node;
299     aFreeNodesFam->myGroupNames.insert(anAllNodesGroupName);
300     aFamilies.push_back(aFreeNodesFam);
301   }
302
303   if (doGroupOfEdges)
304   {
305     DriverMED_FamilyPtr aFreeEdgesFam (new DriverMED_Family);
306     aFreeEdgesFam->SetId(REST_EDGES_FAMILY);
307     aFreeEdgesFam->myType = SMDSAbs_Edge;
308     aFreeEdgesFam->myGroupNames.insert(anAllEdgesGroupName);
309     aFamilies.push_back(aFreeEdgesFam);
310   }
311
312   if (doGroupOfFaces)
313   {
314     DriverMED_FamilyPtr aFreeFacesFam (new DriverMED_Family);
315     aFreeFacesFam->SetId(REST_FACES_FAMILY);
316     aFreeFacesFam->myType = SMDSAbs_Face;
317     aFreeFacesFam->myGroupNames.insert(anAllFacesGroupName);
318     aFamilies.push_back(aFreeFacesFam);
319   }
320
321   if (doGroupOfVolumes)
322   {
323     DriverMED_FamilyPtr aFreeVolumesFam (new DriverMED_Family);
324     aFreeVolumesFam->SetId(REST_VOLUMES_FAMILY);
325     aFreeVolumesFam->myType = SMDSAbs_Volume;
326     aFreeVolumesFam->myGroupNames.insert(anAllVolumesGroupName);
327     aFamilies.push_back(aFreeVolumesFam);
328   }
329
330   DriverMED_FamilyPtr aNullFam (new DriverMED_Family);
331   aNullFam->SetId(0);
332   aNullFam->myType = SMDSAbs_All;
333   aFamilies.push_back(aNullFam);
334
335   return aFamilies;
336 }
337
338 //=============================================================================
339 /*!
340  *  Create TFamilyInfo for this family
341  */
342 //=============================================================================
343 MED::PFamilyInfo 
344 DriverMED_Family::GetFamilyInfo(const MED::PWrapper& theWrapper, 
345                                 const MED::PMeshInfo& theMeshInfo) const
346 {
347   ostringstream aStr;
348   aStr << "FAM_" << myId;
349   set<string>::const_iterator aGrIter = myGroupNames.begin();
350   for(; aGrIter != myGroupNames.end(); aGrIter++){
351     aStr << "_" << *aGrIter;
352   }
353
354   MED::PFamilyInfo anInfo;
355   string aValue = aStr.str();
356   if(myId == 0 || myGroupAttributVal == 0){
357     anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
358                                       aValue,
359                                       myId,
360                                       myGroupNames);
361   }else{
362     MED::TStringVector anAttrDescs (1, "");  // 1 attribute with empty description,
363     MED::TIntVector anAttrIds (1, myId);        // Id=0,
364     MED::TIntVector anAttrVals (1, myGroupAttributVal);
365     anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
366                                       aValue,
367                                       myId,
368                                       myGroupNames,
369                                       anAttrDescs,
370                                       anAttrIds,
371                                       anAttrVals);
372   }
373
374 //  cout << endl;
375 //  cout << "Groups: ";
376 //  set<string>::iterator aGrIter = myGroupNames.begin();
377 //  for (; aGrIter != myGroupNames.end(); aGrIter++)
378 //  {
379 //    cout << " " << *aGrIter;
380 //  }
381 //  cout << endl;
382 //
383 //  cout << "Elements: ";
384 //  set<const SMDS_MeshElement *>::iterator anIter = myElements.begin();
385 //  for (; anIter != myElements.end(); anIter++)
386 //  {
387 //    cout << " " << (*anIter)->GetID();
388 //  }
389 //  cout << endl;
390
391   return anInfo;
392 }
393
394 //=============================================================================
395 /*!
396  *  Initialize the tool by SMESHDS_GroupBase
397  */
398 //=============================================================================
399 void DriverMED_Family::Init (SMESHDS_GroupBase* theGroup, const ColorMap& theColorMap)
400 {
401   // Elements
402   myElements.clear();
403   SMDS_ElemIteratorPtr elemIt = theGroup->GetElements();
404   while (elemIt->more())
405   {
406     myElements.insert(elemIt->next());
407   }
408
409   // Type
410   myType = theGroup->GetType();
411
412   // Groups list
413   myGroupNames.clear();
414   myGroupNames.insert(string(theGroup->GetStoreName()));
415
416   myGroupAttributVal = 0;
417   
418   ColorMap::const_iterator aColorIter = theColorMap.begin();
419   for (; aColorIter != theColorMap.end(); aColorIter++)
420   {
421     SALOMEDS::Color aColor = aColorIter->second;
422     if( CompareColors( theGroup->GetColor(), aColor ) )
423     {
424       myGroupAttributVal = aColorIter->first;
425       break;
426     }
427   }
428 }
429
430 //=============================================================================
431 /*!
432  *  Split <theSubMesh> on some parts (families)
433  *  on the basis of the elements type.
434  */
435 //=============================================================================
436 DriverMED_FamilyPtrList 
437 DriverMED_Family
438 ::SplitByType (SMESHDS_SubMesh* theSubMesh,
439                const int        theId)
440 {
441   DriverMED_FamilyPtrList aFamilies;
442   DriverMED_FamilyPtr aNodesFamily   (new DriverMED_Family);
443   DriverMED_FamilyPtr anEdgesFamily  (new DriverMED_Family);
444   DriverMED_FamilyPtr aFacesFamily   (new DriverMED_Family);
445   DriverMED_FamilyPtr aVolumesFamily (new DriverMED_Family);
446
447   char submeshGrpName[ 30 ];
448   sprintf( submeshGrpName, "SubMesh %d", theId );
449
450   SMDS_NodeIteratorPtr aNodesIter = theSubMesh->GetNodes();
451   while (aNodesIter->more())
452   {
453     const SMDS_MeshNode* aNode = aNodesIter->next();
454     aNodesFamily->AddElement(aNode);
455   }
456
457   SMDS_ElemIteratorPtr anElemsIter = theSubMesh->GetElements();
458   while (anElemsIter->more())
459   {
460     const SMDS_MeshElement* anElem = anElemsIter->next();
461     switch (anElem->GetType())
462     {
463     case SMDSAbs_Edge:
464       anEdgesFamily->AddElement(anElem);
465       break;
466     case SMDSAbs_Face:
467       aFacesFamily->AddElement(anElem);
468       break;
469     case SMDSAbs_Volume:
470       aVolumesFamily->AddElement(anElem);
471       break;
472     default:
473       break;
474     }
475   }
476
477   if (!aNodesFamily->IsEmpty()) {
478     aNodesFamily->SetType(SMDSAbs_Node);
479     aNodesFamily->AddGroupName(submeshGrpName);
480     aFamilies.push_back(aNodesFamily);
481   }
482   if (!anEdgesFamily->IsEmpty()) {
483     anEdgesFamily->SetType(SMDSAbs_Edge);
484     anEdgesFamily->AddGroupName(submeshGrpName);
485     aFamilies.push_back(anEdgesFamily);
486   }
487   if (!aFacesFamily->IsEmpty()) {
488     aFacesFamily->SetType(SMDSAbs_Face);
489     aFacesFamily->AddGroupName(submeshGrpName);
490     aFamilies.push_back(aFacesFamily);
491   }
492   if (!aVolumesFamily->IsEmpty()) {
493     aVolumesFamily->SetType(SMDSAbs_Volume);
494     aVolumesFamily->AddGroupName(submeshGrpName);
495     aFamilies.push_back(aVolumesFamily);
496   }
497
498   return aFamilies;
499 }
500
501 //=============================================================================
502 /*!
503  *  Remove from <myElements> elements, common with <by>,
504  *  Remove from <by> elements, common with <myElements>,
505  *  Create family <common> from common elements, with combined groups list.
506  */
507 //=============================================================================
508 void DriverMED_Family::Split (DriverMED_FamilyPtr by,
509                               DriverMED_FamilyPtr common)
510 {
511   // Elements
512   ElementsSet::iterator anIter = by->myElements.begin();
513   while ( anIter != by->myElements.end())
514   {
515     if (myElements.find(*anIter) != myElements.end())
516     {
517       common->myElements.insert(*anIter);
518       myElements.erase(*anIter);
519       by->myElements.erase(anIter++);
520     }
521     else
522       anIter++;
523   }
524
525   if (!common->IsEmpty())
526   {
527     // Groups list
528     common->myGroupNames = myGroupNames;
529     MED::TStringSet::iterator aGrNamesIter = by->myGroupNames.begin();
530     for (; aGrNamesIter != by->myGroupNames.end(); aGrNamesIter++)
531     {
532       common->myGroupNames.insert(*aGrNamesIter);
533     }
534
535     // Type
536     common->myType = myType;
537   }
538 }