Salome HOME
update with the version in the OCC branch OCC_development_generic_2006.
[modules/med.git] / src / MEDWrapper / Base / MED_Structures.cxx
1 //  Copyright (C) 2003  CEA/DEN, EDF R&D
2 //
3 //
4 //
5 //  File   : VISU_Structure.cxx
6 //  Author : Eugeny NIKOLAEV
7 //  Module : VISU
8
9 #include "MED_Structures.hxx"
10 #include "MED_Utilities.hxx"
11
12 using namespace MED;
13
14 namespace MED
15 {
16   TInt
17   GetNbNodes(EGeometrieElement typmai)
18   {
19     return typmai%100;
20   }
21
22   std::string 
23   GetString(TInt theId, 
24             TInt theStep, 
25             const TString& theString)
26   {
27     const char* aPos = &theString[theId*theStep];
28     TInt aSize = std::min(TInt(strlen(aPos)),theStep);
29     return std::string(aPos,aSize);
30   }
31
32   void 
33   SetString(TInt theId, 
34             TInt theStep, 
35             TString& theString, 
36             const std::string& theValue)
37   {
38     TInt aSize = std::min(TInt(theValue.size()+1),theStep);
39     char* aPos = &theString[theId*theStep];
40     strncpy(aPos,theValue.c_str(),aSize);
41   }
42
43   TInt
44   GetDimGaussCoord(EGeometrieElement theGeom)
45   {
46     return theGeom/100;
47   }
48
49   TInt
50   GetNbRefCoord(EGeometrieElement theGeom)
51   {
52     return (theGeom%100);
53   }
54
55 }
56
57 //---------------------------------------------------------------
58 TInt
59 TFamilyInfo
60 ::GetAttrId(TInt theId) const 
61 {
62   return myAttrId[theId];
63 }
64
65 TInt
66 TFamilyInfo
67 ::GetAttrVal(TInt theId) const 
68 {
69   return myAttrVal[theId];
70 }
71
72 void
73 TFamilyInfo
74 ::SetAttrId(TInt theId,TInt theVal) 
75 {
76   myAttrId[theId] = theVal;
77 }
78
79 void
80 TFamilyInfo
81 ::SetAttrVal(TInt theId,TInt theVal) 
82 {
83   myAttrVal[theId] = theVal;
84 }
85
86 //---------------------------------------------------------------
87 TInt
88 TElemInfo
89 ::GetFamNum(TInt theId) const 
90 {
91   return myFamNum[theId];
92 }
93
94 TInt
95 TElemInfo
96 ::GetElemNum(TInt theId) const 
97 {
98   return myElemNum[theId];
99 }
100
101 void
102 TElemInfo
103 ::SetFamNum(TInt theId,TInt theVal) 
104 {
105   myFamNum[theId] = theVal;
106 }
107
108 //---------------------------------------------------------------
109 TCCoordSlice 
110 TNodeInfo
111 ::GetCoordSlice(TInt theId) const
112 {
113   TInt aDim = myMeshInfo->GetDim();
114   if(GetModeSwitch() == eFULL_INTERLACE)
115     return TCCoordSlice(myCoord,std::slice(theId*aDim,aDim,1));
116   else
117     return TCCoordSlice(myCoord,std::slice(theId,aDim,aDim));
118 }
119
120 TCoordSlice 
121 TNodeInfo
122 ::GetCoordSlice(TInt theId)
123 {
124   TInt aDim = myMeshInfo->GetDim();
125   if(GetModeSwitch() == eFULL_INTERLACE)
126     return TCoordSlice(myCoord,std::slice(theId*aDim,aDim,1));
127   else
128     return TCoordSlice(myCoord,std::slice(theId,aDim,aDim));
129 }
130
131 //---------------------------------------------------------------
132 TCConnSlice 
133 TCellInfo
134 ::GetConnSlice(TInt theElemId) const
135 {
136   if(GetModeSwitch() == eFULL_INTERLACE)
137     return TCConnSlice(myConn,std::slice(GetConnDim()*theElemId,GetNbNodes(myGeom),1));
138   else
139     return TCConnSlice(myConn,std::slice(theElemId,GetNbNodes(myGeom),GetConnDim()));
140 }
141
142 TConnSlice 
143 TCellInfo
144 ::GetConnSlice(TInt theElemId)
145 {
146   if(GetModeSwitch() == eFULL_INTERLACE)
147     return TConnSlice(myConn,std::slice(GetConnDim()*theElemId,GetNbNodes(myGeom),1));
148   else
149     return TConnSlice(myConn,std::slice(theElemId,GetNbNodes(myGeom),GetConnDim()));
150 }
151
152
153 //---------------------------------------------------------------
154 TInt
155 TPolygoneInfo
156 ::GetNbConn(TInt theElemId) const 
157 {
158   return myIndex[theElemId + 1] - myIndex[theElemId];
159 }
160
161 TCConnSlice 
162 TPolygoneInfo
163 ::GetConnSlice(TInt theElemId) const
164 {
165   return TCConnSlice(myConn,std::slice(myIndex[theElemId]-1,GetNbConn(theElemId),1));
166 }
167
168 TConnSlice 
169 TPolygoneInfo
170 ::GetConnSlice(TInt theElemId)
171 {
172   return TConnSlice(myConn,std::slice(myIndex[theElemId]-1,GetNbConn(theElemId),1));
173 }
174
175
176 //---------------------------------------------------------------
177 TInt 
178 TPolyedreInfo
179 ::GetNbFaces(TInt theElemId) const 
180 {
181   return myIndex[theElemId+1] - myIndex[theElemId];
182 }
183
184 TInt 
185 TPolyedreInfo
186 ::GetNbNodes(TInt theElemId) const 
187 {
188   TInt aNbNodes = 0;
189   TInt aNbFaces = GetNbFaces(theElemId);
190   TInt aStartFaceId = myIndex[theElemId] - 1;
191   for(TInt aFaceId = 0; aFaceId < aNbFaces; aFaceId++, aStartFaceId++){
192     TInt aCurrentId = myFaces[aStartFaceId];
193     TInt aDiff = myFaces[aStartFaceId + 1] - aCurrentId;
194     aNbNodes += aDiff;
195   }
196   return aNbNodes;
197 }
198
199 TCConnSliceArr 
200 TPolyedreInfo
201 ::GetConnSliceArr(TInt theElemId) const
202 {
203   TInt aNbFaces = GetNbFaces(theElemId);
204   TCConnSliceArr aConnSliceArr(aNbFaces);
205   TInt aStartFaceId = myIndex[theElemId] - 1;
206   for(TInt aFaceId = 0; aFaceId < aNbFaces; aFaceId++, aStartFaceId++){
207     TInt aCurrentId = myFaces[aStartFaceId];
208     TInt aDiff = myFaces[aStartFaceId + 1] - aCurrentId;
209     aConnSliceArr[aFaceId] =
210       TCConnSlice(myConn,std::slice(aCurrentId - 1,aDiff,1));
211   }
212   return aConnSliceArr;
213 }
214
215 TConnSliceArr 
216 TPolyedreInfo
217 ::GetConnSliceArr(TInt theElemId)
218 {
219   TInt aNbFaces = GetNbFaces(theElemId);
220   TConnSliceArr aConnSliceArr(aNbFaces);
221   TInt aStartFaceId = myIndex[theElemId] - 1;
222   for(TInt aFaceId = 0; aFaceId < aNbFaces; aFaceId++, aStartFaceId++){
223     TInt aCurrentId = myFaces[aStartFaceId];
224     TInt aDiff = myFaces[aStartFaceId + 1] - aCurrentId;
225     aConnSliceArr[aFaceId] =
226       TConnSlice(myConn,std::slice(aCurrentId - 1,aDiff,1));
227   }
228   return aConnSliceArr;
229 }
230
231
232 //---------------------------------------------------------------
233 TInt
234 TProfileInfo
235 ::GetElemNum(TInt theId) const 
236 {
237   return myElemNum[theId];
238 }
239
240 void
241 TProfileInfo
242 ::SetElemNum(TInt theId,TInt theVal) 
243 {
244   myElemNum[theId] = theVal;
245 }
246
247 //---------------------------------------------------------------
248 bool
249 TGaussInfo::TLess
250 ::operator()(const TKey& theLeft, const TKey& theRight) const
251 {
252   EGeometrieElement aLGeom = boost::get<0>(theLeft);
253   EGeometrieElement aRGeom = boost::get<0>(theRight);
254   if(aLGeom != aRGeom)
255     return aLGeom < aRGeom;
256
257   const std::string& aLStr = boost::get<1>(theLeft);
258   const std::string& aRStr = boost::get<1>(theRight);
259   return aLStr < aRStr;
260 }
261
262 bool
263 TGaussInfo::TLess
264 ::operator()(const TGaussInfo& theLeft, const TGaussInfo& theRight) const
265 {
266   if(theLeft.myGeom != theRight.myGeom)
267     return theLeft.myGeom < theRight.myGeom;
268
269   if(theLeft.myRefCoord != theRight.myRefCoord)
270     return theLeft.myRefCoord < theRight.myRefCoord;
271
272   return theLeft.myGaussCoord < theRight.myGaussCoord;
273 }
274
275 TCCoordSlice 
276 TGaussInfo
277 ::GetRefCoordSlice(TInt theId) const
278 {
279   if(GetModeSwitch() == eFULL_INTERLACE)
280     return TCCoordSlice(myRefCoord,std::slice(theId*GetDim(),GetDim(),1));
281   else
282     return TCCoordSlice(myRefCoord,std::slice(theId,GetDim(),GetDim()));
283 }
284
285 TCoordSlice 
286 TGaussInfo
287 ::GetRefCoordSlice(TInt theId)
288 {
289   if(GetModeSwitch() == eFULL_INTERLACE)
290     return TCoordSlice(myRefCoord,std::slice(theId*GetDim(),GetDim(),1));
291   else
292     return TCoordSlice(myRefCoord,std::slice(theId,GetDim(),GetDim()));
293 }
294
295 TCCoordSlice 
296 TGaussInfo
297 ::GetGaussCoordSlice(TInt theId) const
298 {
299   if(GetModeSwitch() == eFULL_INTERLACE)
300     return TCCoordSlice(myGaussCoord,std::slice(theId*GetDim(),GetDim(),1));
301   else
302     return TCCoordSlice(myGaussCoord,std::slice(theId,GetDim(),GetDim()));
303 }
304
305 TCoordSlice 
306 TGaussInfo
307 ::GetGaussCoordSlice(TInt theId)
308 {
309   if(GetModeSwitch() == eFULL_INTERLACE)
310     return TCoordSlice(myGaussCoord,std::slice(theId*GetDim(),GetNbGauss(),1));
311   else
312     return TCoordSlice(myGaussCoord,std::slice(theId,GetNbGauss(),GetDim()));
313 }
314
315
316 //---------------------------------------------------------------
317 TInt
318 TTimeStampInfo
319 ::GetNbGauss(EGeometrieElement theGeom) const
320 {
321   TGeom2NbGauss::const_iterator anIter = myGeom2NbGauss.find(theGeom);
322   if(anIter == myGeom2NbGauss.end())
323     EXCEPTION(runtime_error,"TTimeStampInfo::GetNbGauss - myGeom2NbGauss.find(theGeom) fails");
324
325   return anIter->second;
326 }
327
328
329 //---------------------------------------------------------------
330 void
331 TMeshValue
332 ::Init(TInt theNbElem,
333        TInt theNbGauss,
334        TInt theNbComp,
335        EModeSwitch theMode)
336 {
337   myModeSwitch = theMode;
338   
339   myNbElem = theNbElem;
340   myNbGauss = theNbGauss;
341   myNbComp = theNbComp;
342   
343   myStep = theNbComp*theNbGauss;
344   
345   myValue.resize(theNbElem*myStep);
346 }
347
348 //---------------------------------------------------------------
349 TCValueSliceArr 
350 TMeshValue
351 ::GetGaussValueSliceArr(TInt theElemId) const
352 {
353   TCValueSliceArr aValueSliceArr(myNbGauss);
354   if(GetModeSwitch() == eFULL_INTERLACE){
355     TInt anId = theElemId*myStep;
356     for(TInt aGaussId = 0; aGaussId < myNbGauss; aGaussId++){
357       aValueSliceArr[aGaussId] =
358         TCValueSlice(myValue,std::slice(anId,myNbComp,1));
359       anId += myNbComp;
360     }
361   }
362   else{
363     for(TInt aGaussId = 0; aGaussId < myNbGauss; aGaussId++){
364       aValueSliceArr[aGaussId] =
365         TCValueSlice(myValue,std::slice(theElemId,myNbComp,myStep));
366     }
367   }
368   return aValueSliceArr;
369 }
370
371 TValueSliceArr 
372 TMeshValue
373 ::GetGaussValueSliceArr(TInt theElemId)
374 {
375   TValueSliceArr aValueSliceArr(myNbGauss);
376   if(GetModeSwitch() == eFULL_INTERLACE){
377     TInt anId = theElemId*myStep;
378     for(TInt aGaussId = 0; aGaussId < myNbGauss; aGaussId++){
379       aValueSliceArr[aGaussId] =
380         TValueSlice(myValue,std::slice(anId,myNbComp,1));
381       anId += myNbComp;
382     }
383   }
384   else{
385     for(TInt aGaussId = 0; aGaussId < myNbGauss; aGaussId++){
386       aValueSliceArr[aGaussId] =
387         TValueSlice(myValue,std::slice(theElemId,myNbComp,myStep));
388     }
389   }
390   return aValueSliceArr;
391 }
392
393 //---------------------------------------------------------------
394 TCValueSliceArr 
395 TMeshValue
396 ::GetCompValueSliceArr(TInt theElemId) const
397 {
398   TCValueSliceArr aValueSliceArr(myNbComp);
399   if(GetModeSwitch() == eFULL_INTERLACE){
400     TInt anId = theElemId*myStep;
401     for(TInt aCompId = 0; aCompId < myNbComp; aCompId++){
402       aValueSliceArr[aCompId] =
403         TCValueSlice(myValue,std::slice(anId,myNbGauss,myNbComp));
404       anId += 1;
405     }
406   }
407   else{
408     for(TInt aCompId = 0; aCompId < myNbComp; aCompId++){
409       aValueSliceArr[aCompId] =
410         TCValueSlice(myValue,std::slice(theElemId,myNbGauss,myStep));
411     }
412   }
413   return aValueSliceArr;
414 }
415
416
417 TValueSliceArr 
418 TMeshValue
419 ::GetCompValueSliceArr(TInt theElemId)
420 {
421   TValueSliceArr aValueSliceArr(myNbGauss);
422   if(GetModeSwitch() == eFULL_INTERLACE){
423     TInt anId = theElemId*myStep;
424     for(TInt aCompId = 0; aCompId < myNbComp; aCompId++){
425       aValueSliceArr[aCompId] =
426         TValueSlice(myValue,std::slice(anId,myNbGauss,myNbComp));
427       anId += 1;
428     }
429   }
430   else{
431     for(TInt aGaussId = 0; aGaussId < myNbGauss; aGaussId++){
432       aValueSliceArr[aGaussId] =
433         TValueSlice(myValue,std::slice(theElemId,myNbComp,myStep));
434     }
435   }
436   return aValueSliceArr;
437 }
438
439
440 //---------------------------------------------------------------
441 const TMeshValue& 
442 TTimeStampVal
443 ::GetMeshValue(EGeometrieElement theGeom) const
444 {
445   TGeom2Value::const_iterator anIter = myGeom2Value.find(theGeom);
446   if(anIter == myGeom2Value.end())
447     EXCEPTION(runtime_error,"TTimeStampVal::GetMeshValue - myGeom2Value.find(theGeom) fails");
448   return anIter->second;
449 }
450
451 TMeshValue& 
452 TTimeStampVal
453 ::GetMeshValue(EGeometrieElement theGeom)
454 {
455   return myGeom2Value[theGeom];
456 }