Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / MEDMEM / MEDMEM_Coordinate.cxx
1 //  Copyright (C) 2007-2008  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 #include "MEDMEM_Coordinate.hxx"
23 #include "MEDMEM_Exception.hxx"
24 #include "MEDMEM_STRING.hxx"
25
26 #include "MEDMEM_Utilities.hxx"
27
28 using namespace std;
29 using namespace MEDMEM;
30 using namespace MED_EN;
31
32 /*! Default Constructor : should not be used */
33 //----------------------------------------------------------//
34 COORDINATE::COORDINATE():_coordinateSystem(""),
35                          _coordinate(MEDARRAY<double>()),
36                          _coordinateName(),
37                          _coordinateUnit(),
38                          _nodeNumber()
39 //----------------------------------------------------------//
40 {
41   const char* LOC = "Default Constructor COORDINATE";
42   BEGIN_OF_MED(LOC);
43 }
44
45 /*! This constructor allocate a MEDARRAY of SpaceDimension * NumberOfNodes.\n
46     It will create empty array for optional data (nodeNumber..) */
47 //------------------------------------------------------------------------------//
48 COORDINATE::COORDINATE(int SpaceDimension, int NumberOfNodes, medModeSwitch Mode):
49   _coordinateSystem(""),
50   _coordinate(SpaceDimension,NumberOfNodes,Mode),
51   _coordinateName(SpaceDimension),
52   _coordinateUnit(SpaceDimension),
53   _nodeNumber()
54
55 //------------------------------------------------------------------------------//
56 {
57   const char* LOC = "Constructor COORDINATE";
58   BEGIN_OF_MED(LOC);
59 }
60
61 /*! This constructor is specialy designed for use in class GRID.\n
62  *  The purpose is to create a COORDINATE without allocating memory for nodes (in the MEDARRAY).
63  *  The allocation (call to setCoordinates) is performed afterward by GRID, if necessary.
64  *  We set _coordinateName & _coordinateUnit in the constructor, because calls to setCoordinateName
65  *  and setCoordinateUnit are not possible before allocation of MEDARRAY.
66  */
67 //------------------------------------------------------------------------------//
68 COORDINATE::COORDINATE(int SpaceDimension,const string * CoordinateName, const string * CoordinateUnit): 
69    _coordinateSystem(""), _coordinate(MEDARRAY<double>()), _coordinateName(SpaceDimension), 
70    _coordinateUnit(SpaceDimension), _nodeNumber()
71 {
72     for (int i=0; i<SpaceDimension; ++i)
73     {
74         _coordinateName[i]=CoordinateName[i];
75         _coordinateUnit[i]=CoordinateUnit[i];
76     }
77 }
78
79
80 /*! This constructor will COPY all data (it is a deep copy) included in m.\n  
81     But only the default storage mode of coordinates array
82     will be copied (not both storage representation modes even if they both
83     exist in original object) :  for example only full_interlace mode  */
84 //------------------------------------------------------------------------------//
85 COORDINATE::COORDINATE(const COORDINATE & m):
86   _coordinateSystem(m._coordinateSystem)
87 //------------------------------------------------------------------------------//
88 {
89   const char* LOC = "Copy Constructor COORDINATE";
90   BEGIN_OF_MED(LOC);
91
92   _coordinate = m._coordinate;
93   //int spaceDimension = _coordinate.getLeadingValue();
94   int numberOfNodes = _coordinate.getLengthValue();
95
96   SCRUTE_MED(_coordinate.getLeadingValue());
97   //setCoordinatesNames((const string*)m._coordinateName) ;
98   //setCoordinatesUnits((const string*)m._coordinateUnit) ;
99   setCoordinatesNames( m.getCoordinatesNames() );
100   setCoordinatesUnits( m.getCoordinatesUnits() );
101
102   if ( (const int * const) m._nodeNumber != NULL)
103     _nodeNumber.set(numberOfNodes,(const int*)m._nodeNumber);
104   // PG : it's default no ?
105 //    else
106 //      {
107 //        _nodeNumber.set(0);
108 //      }
109 }
110
111
112 /* does nothing :   all attributs are object (not pointers) */
113 //----------------------//
114 COORDINATE::~COORDINATE()
115 //----------------------//
116 {
117   MESSAGE_MED("~COORDINATE()");
118 }
119
120 /*! sets the attribute _coordinate with Coordinate           */
121 //----------------------------------------------------------//
122 void COORDINATE::setCoordinates(MEDARRAY<double> *Coordinate,bool shallowCopy) 
123 //----------------------------------------------------------//
124
125
126   const medModeSwitch mode = Coordinate->getMode(); 
127   //  const int  spaceDimension = (int) Coordinate->getLeadingValue();
128   //  const int  numberOfNodes  = (int) Coordinate->getLengthValue();
129   if ( Coordinate->get(mode) != NULL)
130   {
131     if(shallowCopy)
132       {
133         _coordinate.shallowCopy(*Coordinate);
134       }
135     else
136       {
137         MEDARRAY<double> pourAttribut(*Coordinate,false);
138         _coordinate = pourAttribut;
139         //_coordinate.set(mode,Coordinate->get(mode));
140       }
141   }
142   else
143   {
144         throw MED_EXCEPTION ( LOCALIZED(STRING("setCoordinates(MEDARRAY<double> *Coordinate)") << "No Coordinate"));
145   }
146 }
147
148 /*!
149   Sets the value in attribute _coordinate with Coordinate.
150   _coordinate must be allocated (with 
151   COORDINATE::COORDINATE(int,int,medModeSwitch).
152 */
153 //----------------------------------------------------------//
154 void COORDINATE::setCoordinates(const medModeSwitch Mode, 
155                                 const double *Coordinate) 
156 //----------------------------------------------------------//
157
158 //    if (_coordinate == NULL)
159 //      throw MEDEXCEPTION("COORDINATE::setCoordinates(double*) : coordinate array not allocated !");
160
161   _coordinate.set(Mode,Coordinate);
162 }
163
164 /*! sets the attribute _coordinateName with CoordinateName   */
165 //----------------------------------------------------------//
166 void COORDINATE::setCoordinatesNames(const string * CoordinateName) 
167 //----------------------------------------------------------//
168 {
169   int SpaceDimension = getSpaceDimension() ;
170   //_coordinateName.set(SpaceDimension) ;
171   _coordinateName.resize(SpaceDimension);
172   for (int i=0; i<SpaceDimension; i++)
173     _coordinateName[i]=CoordinateName[i];
174 }
175
176 /*!
177   sets the (i+1)^th component of the attribute _coordinateName with
178   CoordinateName
179 */
180 //----------------------------------------------------------//
181 void COORDINATE::setCoordinateName(const string CoordinateName, const int i)
182 //----------------------------------------------------------//
183 {
184   _coordinateName[i]=CoordinateName;
185 }
186
187 /*! sets the attribute _coordinateUnit with CoordinateUnit   */
188 //----------------------------------------------------------//
189 void COORDINATE::setCoordinatesUnits(const string * CoordinateUnit) 
190 //----------------------------------------------------------//
191
192   int SpaceDimension = getSpaceDimension() ;
193   //_coordinateUnit.set(SpaceDimension) ; 
194   _coordinateUnit.resize(SpaceDimension) ; 
195   for (int i=0; i<SpaceDimension; i++)
196     _coordinateUnit[i]=CoordinateUnit[i];
197 }
198
199 /*!
200   sets the (i+1)^th component of the attribute _coordinateUnit with
201   CoordinateUnit
202 */
203 //----------------------------------------------------------//
204 void COORDINATE::setCoordinateUnit(const string CoordinateUnit, const int i) 
205 //----------------------------------------------------------//
206
207   _coordinateUnit[i]=CoordinateUnit;
208 }
209
210 /*! sets the attribute _coordinateSystem with CoordinateSystem   */
211 //----------------------------------------------------------//
212 void COORDINATE::setCoordinatesSystem(const string CoordinateSystem) 
213 //----------------------------------------------------------//
214
215         _coordinateSystem=CoordinateSystem; 
216 }
217
218 /*! sets the attribute _nodeNumber with NodeNumber */
219 //------------------------------------------------//
220 void COORDINATE::setNodesNumbers(const int * NodeNumber) 
221 //------------------------------------------------//
222 {       
223   int NumberOfNodes = getNumberOfNodes() ;
224   _nodeNumber.set(NumberOfNodes,NodeNumber) ; 
225 }
226
227 int COORDINATE::getSpaceDimension() const
228 {       
229   return _coordinate.getLeadingValue() ; 
230 }
231
232 int COORDINATE::getNumberOfNodes() const
233 {       
234   return _coordinate.getLengthValue() ; 
235 }
236
237
238 /*! returns a pointer to the optional array storing 
239     eventual nodes numbers */
240 //-------------------------------------------------//
241 const int * COORDINATE::getNodesNumbers() const
242 //-------------------------------------------------//
243 {
244         return  (const int *)_nodeNumber;
245 }
246
247 /*! returns a Pointer to Coordinates Array in specified mode representation */
248 //--------------------------------------------------------------------------//
249 const double *  COORDINATE::getCoordinates (medModeSwitch Mode)
250 //--------------------------------------------------------------------------//
251 {
252         return _coordinate.get(Mode) ;
253 }
254
255 /* returns the coordinate of node Number on axis Axis */
256 //----------------------------------------------------//
257 double COORDINATE::getCoordinate(int Number,int Axis) 
258 //----------------------------------------------------//
259 {       
260         return _coordinate.getIJ(Number,Axis) ;
261 }
262
263 /* returns all nodes coordinates from  axis Axis      */
264 //----------------------------------------------------//
265 const double *  COORDINATE::getCoordinateAxis(int Axis)
266 //----------------------------------------------------//
267 {                       //< return all nodes coordinates from axis Axis
268          return _coordinate.getColumn(Axis) ;
269 }
270
271 /*! returns an array with names of coordinates. \n
272       Example : \n
273       - x,y,z \n
274       - r,teta,phi \n
275       - ... */
276 //--------------------------------------//
277 const string * COORDINATE::getCoordinatesNames() const
278 {
279   return &(_coordinateName[0]) ;
280 }
281
282 /* returns the name of axis Axis             */
283 //-------------------------------------------//
284 string COORDINATE::getCoordinateName(int Axis) const
285 //-------------------------------------------//
286 {
287   return _coordinateName[Axis-1];
288 }
289
290 /*!  returns an array with units of coordinates (cm, m, mm, ...)
291      It could be empty. We suppose we are IS (meter).  */
292 //-----------------------------------------------------//
293 const string * COORDINATE::getCoordinatesUnits() const
294 //-----------------------------------------------------//
295 {
296   return &(_coordinateUnit[0]) ;
297 }
298
299 /*! returns the unit of axis Axis           */
300 //------------------------------------------//
301 string COORDINATE::getCoordinateUnit(int Axis) const
302 //------------------------------------------//
303 {
304   return _coordinateUnit[Axis-1] ;
305 }
306 /*! returns "CARTESIAN", "CYLINDRICAL" or "SPHERICAL"*/
307 //---------------------------------------------------//
308 string COORDINATE::getCoordinatesSystem() const
309 //---------------------------------------------------//
310 {
311   return _coordinateSystem ;
312 }