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