Salome HOME
PR: merge from branch BR_UnitTests tag mergeto_trunk_17oct05
[modules/kernel.git] / src / MEDWrapper / Base / MED_Algorithm.cxx
1 //  
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   : 
25 //  Author : 
26 //  Module : 
27 //  $Header$
28
29 #include "MED_Algorithm.hxx"
30 #include "MED_Utilities.hxx"
31  
32 #ifdef _DEBUG_
33 static int MYDEBUG = 0;
34 #else
35 static int MYDEBUG = 0;
36 #endif
37
38 namespace MED{
39   //---------------------------------------------------------------
40   TElemGroup 
41   GetElemsByEntity(TWrapper& theWrapper, 
42                    const PMeshInfo& theMeshInfo,
43                    const MED::TEntityInfo& theEntityInfo)
44   {
45     MSG(MYDEBUG,"GetElemsByEntity(...)");
46     TElemGroup aGroup;
47     MED::TEntityInfo::const_iterator anIter = theEntityInfo.begin();
48     for(; anIter != theEntityInfo.end(); anIter++){
49       const EEntiteMaillage& anEntity = anIter->first;
50       const MED::TGeom& aGeom = anIter->second;
51       TElemMap& anElemMap = aGroup[anEntity];
52
53       if(anEntity == eNOEUD){
54         anElemMap[ePOINT1] = theWrapper.GetPNodeInfo(theMeshInfo);
55         continue;
56       }
57
58       MED::TGeom::const_iterator anGeomIter = aGeom.begin();
59       for(; anGeomIter != aGeom.end(); anGeomIter++){
60         const EGeometrieElement& aGeo = anGeomIter->first;
61         switch(aGeo){
62         case ePOLYGONE: {
63           anElemMap[ePOLYGONE] = theWrapper.GetPPolygoneInfo(theMeshInfo,anEntity,aGeo);
64           break;
65         }
66         case ePOLYEDRE: {
67           anElemMap[ePOLYEDRE] = theWrapper.GetPPolyedreInfo(theMeshInfo,anEntity,aGeo);
68           break;
69         }
70         default: {
71           anElemMap[aGeo] = theWrapper.GetPCellInfo(theMeshInfo,anEntity,aGeo);
72         }}
73
74       }
75     }
76     ADDMSG(MYDEBUG,"\n");
77     return aGroup;
78   }
79   
80   
81   //---------------------------------------------------------------
82   TFamilyGroup 
83   GetFamilies(TWrapper& theWrapper,
84               const PMeshInfo& theMeshInfo)
85   {
86     MSG(MYDEBUG,"GetFamilies(...)");
87     TErr anErr;
88     TFamilyGroup aGroup;
89     TInt aNbFam = theWrapper.GetNbFamilies(*theMeshInfo);
90     INITMSG(MYDEBUG,"GetNbFamilies() = "<<aNbFam<<"\n");
91     for(TInt iFam = 1; iFam <= aNbFam; iFam++){
92       PFamilyInfo aFamilyInfo = theWrapper.GetPFamilyInfo(theMeshInfo,iFam,&anErr);
93       if(anErr >= 0)
94         aGroup.insert(aFamilyInfo);
95     }
96     ADDMSG(MYDEBUG,"\n");
97     return aGroup;
98   }
99
100
101   TGroupInfo 
102   GetFamiliesByGroup(const TFamilyGroup& theGroupInfo)
103   {
104     MSG(MYDEBUG,"GetFamiliesByGroup(...)");
105     TGroupInfo aGroup;
106     TFamilyGroup::const_iterator anIter = theGroupInfo.begin();
107     for(; anIter != theGroupInfo.end(); anIter++){
108       const PFamilyInfo& aFamilyInfo = *anIter;
109       TInt aNbGroup = aFamilyInfo->GetNbGroup();
110       for(TInt iGroup = 0; iGroup < aNbGroup; iGroup++){
111         aGroup[aFamilyInfo->GetGroupName(iGroup)].insert(aFamilyInfo);
112       } 
113     }
114
115 #ifdef _DEBUG_
116     if(MYDEBUG){
117       TGroupInfo::const_iterator anIter = aGroup.begin();
118       for(; anIter != aGroup.end(); anIter++){
119         string aName = anIter->first;
120         INITMSG(MYDEBUG,"aGroupName = '"<<aName<<"'\n");
121         const TFamilyGroup& aFamilyGroup = anIter->second;
122         TFamilyGroup::const_iterator anFamIter = aFamilyGroup.begin();
123         for(; anFamIter != aFamilyGroup.end(); anFamIter++){
124           const PFamilyInfo& aFamilyInfo = *anFamIter;
125           INITMSG(MYDEBUG,"aFamilyName = '"<<aFamilyInfo->GetName()<<"'\n");
126         }
127       }
128       ADDMSG(MYDEBUG,"\n");
129     }
130 #endif
131
132     return aGroup;
133   }
134
135
136   //---------------------------------------------------------------
137   TTimeStampGroup 
138   GetFieldsByEntity(TWrapper& theWrapper, 
139                     const PMeshInfo& theMeshInfo,
140                     const MED::TEntityInfo& theEntityInfo)
141   {
142     MSG(MYDEBUG,"GetFieldsByEntity(...)");
143     TTimeStampGroup aGroup;
144     TInt aNbFields = theWrapper.GetNbFields();
145     INITMSG(MYDEBUG,"GetNbFields() = "<<aNbFields<<"\n");
146     for(TInt iField = 1; iField <= aNbFields; iField++){
147       PFieldInfo aFieldInfo = theWrapper.GetPFieldInfo(theMeshInfo,iField);
148       INITMSG(MYDEBUG,"aFieldName = '"<<aFieldInfo->GetName()<<
149               "'; aNbComp = "<<aFieldInfo->GetNbComp()<<"; ");
150       MED::TGeom aGeom;
151       EEntiteMaillage anEntity = EEntiteMaillage(-1);
152       TInt aNbTimeStamps = theWrapper.GetNbTimeStamps(aFieldInfo,theEntityInfo,anEntity,aGeom);
153       ADDMSG(MYDEBUG,"anEntity = "<<anEntity<<"; GetNbTimeStamps = "<<aNbTimeStamps<<"\n");
154       for(TInt iTimeStamp = 1; iTimeStamp <= aNbTimeStamps; iTimeStamp++){
155         PTimeStampInfo aTimeStamp = 
156           theWrapper.GetPTimeStampInfo(aFieldInfo,anEntity,aGeom,iTimeStamp);
157         aGroup[aFieldInfo].insert(aTimeStamp);
158         INITMSG(MYDEBUG,"aDt = "<<aTimeStamp->GetDt()
159                 <<", Unit = \'"<<aTimeStamp->GetUnitDt()
160                 <<"\', aNbGauss = "<<aTimeStamp->GetNbGauss()<<"\n");
161       }
162     }
163     ADDMSG(MYDEBUG,"\n");
164     return aGroup;
165   }
166   
167
168   TFieldGroup 
169   GetFieldsByEntity(const TTimeStampGroup& theTimeStampGroup)
170   {
171     TFieldGroup aGroup;
172     TTimeStampGroup::const_iterator anIter = theTimeStampGroup.begin();
173     for(; anIter != theTimeStampGroup.end(); anIter++){
174       //const PFieldInfo& aFieldInfo = anIter->first;
175       const TTimeStampSet& aTimeStampSet = anIter->second;
176       if(aTimeStampSet.empty()) continue;
177       const PTimeStampInfo& aTimeStampInfo = *aTimeStampSet.begin();
178       aGroup[aTimeStampInfo->GetEntity()].insert(*anIter);
179     }
180     return aGroup;
181   }
182   
183
184   TFamilyByEntity
185   GetFamiliesByEntity(TWrapper& theWrapper, 
186                       const TElemGroup& theElemGroup,
187                       const TFamilyGroup& theFamilyGroup)
188   {
189     MSG(MYDEBUG,"GetFamiliesByEntity(...)");
190     TFamilyByEntity aFamilyByEntity;
191     
192     typedef map<TInt,PFamilyInfo> TFamilyByIdMap;
193     TFamilyByIdMap aFamilyByIdMap;
194     TFamilyGroup::const_iterator anIter = theFamilyGroup.begin();
195     for(; anIter != theFamilyGroup.end(); anIter++){
196       const PFamilyInfo& aFamilyInfo = *anIter;
197       aFamilyByIdMap.insert(TFamilyByIdMap::value_type(aFamilyInfo->GetId(),aFamilyInfo));
198     }
199     
200     if(!aFamilyByIdMap.empty()){
201       typedef set<TInt> TFamilyIdSet;
202       typedef map<EEntiteMaillage,TFamilyIdSet> TFamilyIdByEntity;
203       TFamilyIdByEntity aFamilyIdByEntity;
204       
205       if(!theElemGroup.empty()){
206         TElemGroup::const_iterator anIter = theElemGroup.begin();
207         for(; anIter != theElemGroup.end(); anIter++){
208           const EEntiteMaillage& anEntity = anIter->first;
209           TFamilyIdSet& aFamilyIdSet = aFamilyIdByEntity[anEntity];
210           const TElemMap& anElemMap = anIter->second;
211           TElemMap::const_iterator anElemIter = anElemMap.begin();
212           for(; anElemIter != anElemMap.end(); anElemIter++){
213             const PElemInfo& aElemInfo = anElemIter->second;
214             if(TInt aNbElem = aElemInfo->GetNbElem()){
215               for(TInt i = 0; i < aNbElem; i++){
216                 aFamilyIdSet.insert(aElemInfo->GetFamNum(i));
217               }
218             }
219           }
220         }
221       }
222       
223       if(!aFamilyIdByEntity.empty()){
224         TFamilyIdByEntity::const_iterator anIter = aFamilyIdByEntity.begin();
225         for(; anIter != aFamilyIdByEntity.end(); anIter++){
226           const EEntiteMaillage& anEntity = anIter->first;
227           INITMSG(MYDEBUG,"anEntity = "<<anEntity<<":\n");
228           const TFamilyIdSet& aFamilyIdSet = anIter->second;
229           TFamilyIdSet::const_iterator anFamilyIdIter = aFamilyIdSet.begin();
230           for(; anFamilyIdIter != aFamilyIdSet.end(); anFamilyIdIter++){
231             const TInt& aFamilyId = *anFamilyIdIter;
232             TFamilyByIdMap::const_iterator 
233               anFamilyByIdMapIter = aFamilyByIdMap.find(aFamilyId);
234             if(anFamilyByIdMapIter != aFamilyByIdMap.end()){
235               const PFamilyInfo& aFamilyInfo = anFamilyByIdMapIter->second;
236               aFamilyByEntity[anEntity].insert(aFamilyInfo);
237               INITMSG(MYDEBUG,
238                       "aFamilyName = '"<<aFamilyInfo->GetName()<<
239                       "' anId = "<<aFamilyInfo->GetId()<<"\n");
240             }
241           }
242         }
243       }
244     }    
245     ADDMSG(MYDEBUG,"\n");
246     return aFamilyByEntity;
247   }
248   
249 }