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