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