Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDWrapper / Base / MED_CoordUtils.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 //  File   : 
24 //  Author : 
25 //  Module : 
26 //  $Header$
27 //
28 #include "MED_CoordUtils.hxx"
29 #include "MED_Utilities.hxx"
30  
31 namespace MED
32 {
33
34   enum ECoordName{eX, eY, eZ, eNone};
35
36   template<ECoordName TCoordId>
37   TFloat 
38   GetCoord(const TCCoordSlice& theCoordSlice)
39   {
40     return theCoordSlice[TCoordId];
41   }
42
43   template<>
44   TFloat 
45   GetCoord<eNone>(const TCCoordSlice& theCoordSlice)
46   {
47     return 0.0;
48   }
49   
50   TGetCoord
51   aXYZGetCoord[3] = {
52     &GetCoord<eX>, 
53     &GetCoord<eY>, 
54     &GetCoord<eZ>
55   };
56   
57   TGetCoord
58   aXYGetCoord[3] = {
59     &GetCoord<eX>, 
60     &GetCoord<eY>, 
61     &GetCoord<eNone>
62   };
63   
64   TGetCoord
65   aYZGetCoord[3] = {
66     &GetCoord<eNone>,
67     &GetCoord<eX>, 
68     &GetCoord<eY>
69   };
70   
71   TGetCoord 
72   aXZGetCoord[3] = {
73     &GetCoord<eX>, 
74     &GetCoord<eNone>,
75     &GetCoord<eY>
76   };
77   
78   
79   TGetCoord 
80   aXGetCoord[3] = {
81     &GetCoord<eX>, 
82     &GetCoord<eNone>,
83     &GetCoord<eNone>
84   };
85   
86   TGetCoord
87   aYGetCoord[3] = {
88     &GetCoord<eNone>,
89     &GetCoord<eX>, 
90     &GetCoord<eNone>
91   };
92
93   TGetCoord
94   aZGetCoord[3] = {
95     &GetCoord<eNone>,
96     &GetCoord<eNone>,
97     &GetCoord<eX>
98   };
99
100   
101   //---------------------------------------------------------------
102   TCoordHelper
103   ::TCoordHelper(TGetCoord* theGetCoord):
104     myGetCoord(theGetCoord)
105   {}
106
107   TFloat 
108   TCoordHelper
109   ::GetCoord(TCCoordSlice& theCoordSlice, 
110              TInt theCoordId)
111   {
112     return (*myGetCoord[theCoordId])(theCoordSlice);
113   }
114
115
116   //---------------------------------------------------------------
117   PCoordHelper
118   GetCoordHelper(PNodeInfo theNodeInfo)
119   {
120     PCoordHelper aCoordHelper;
121     {
122       PMeshInfo aMeshInfo = theNodeInfo->GetMeshInfo();
123       TInt aMeshDimension = aMeshInfo->GetDim();
124       bool anIsDimPresent[3] = {false, false, false};
125       for(int iDim = 0; iDim < aMeshDimension; iDim++){
126         // PAL16857(SMESH not conform to the MED convention) ->
127         // 1D - always along X
128         // 2D - always in XOY plane
129         anIsDimPresent[iDim] = iDim < aMeshDimension;
130 //      std::string aName = theNodeInfo->GetCoordName(iDim);
131 //         if ( aName.size() > 1 ) // PAL12148, aName has size 8 or 16
132 //           aName = aName.substr(0,1);
133 //      if(aName == "x" || aName == "X")
134 //        anIsDimPresent[eX] = true;
135 //      else if(aName == "y" || aName == "Y")
136 //        anIsDimPresent[eY] = true;
137 //      else if(aName == "z" || aName == "Z")
138 //        anIsDimPresent[eZ] = true;
139       }
140
141       switch(aMeshDimension){
142       case 3:
143         aCoordHelper.reset(new TCoordHelper(aXYZGetCoord));
144         break;
145       case 2:
146         if(anIsDimPresent[eY] && anIsDimPresent[eZ])
147           aCoordHelper.reset(new TCoordHelper(aYZGetCoord));
148         else if(anIsDimPresent[eX] && anIsDimPresent[eZ])
149           aCoordHelper.reset(new TCoordHelper(aXZGetCoord));
150         else
151           aCoordHelper.reset(new TCoordHelper(aXYGetCoord));
152         break;
153       case 1:
154         if(anIsDimPresent[eY])
155           aCoordHelper.reset(new TCoordHelper(aYGetCoord));
156         else if(anIsDimPresent[eZ])
157           aCoordHelper.reset(new TCoordHelper(aZGetCoord));
158         else
159           aCoordHelper.reset(new TCoordHelper(aXGetCoord));
160         break;
161       }
162     }
163     return aCoordHelper;
164   }
165 }