Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / MEDCoupling / MEDCouplingSMesh.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "MEDCouplingSMesh.hxx"
20 #include "MemArray.hxx"
21
22 using namespace ParaMEDMEM;
23
24 MEDCouplingSMesh::MEDCouplingSMesh():_x_array(0),_y_array(0),_z_array(0)
25 {
26 }
27
28 MEDCouplingSMesh::~MEDCouplingSMesh()
29 {
30   if(_x_array)
31     _x_array->decrRef();
32   if(_y_array)
33     _y_array->decrRef();
34   if(_z_array)
35     _z_array->decrRef();
36 }
37
38 MEDCouplingSMesh *MEDCouplingSMesh::New()
39 {
40   return new MEDCouplingSMesh;
41 }
42
43 void MEDCouplingSMesh::updateTime()
44 {
45   if(_x_array)
46     updateTimeWith(*_x_array);
47   if(_y_array)
48     updateTimeWith(*_y_array);
49   if(_z_array)
50     updateTimeWith(*_z_array);
51 }
52
53 void MEDCouplingSMesh::checkCoherency() const throw(INTERP_KERNEL::Exception)
54 {
55   const char msg0[]="Invalid ";
56   const char msg1[]=" array ! Must contain more than 1 element.";
57   if(_x_array)
58     if(_x_array->getNbOfElems()<2)
59       {
60         std::ostringstream os; os << msg0 << 'X' << msg1;
61         throw INTERP_KERNEL::Exception(os.str().c_str());
62       }
63   if(_y_array)
64     if(_y_array->getNbOfElems()<2)
65       {
66         std::ostringstream os; os << msg0 << 'Y' << msg1;
67         throw INTERP_KERNEL::Exception(os.str().c_str());
68       }
69   if(_z_array)
70     if(_z_array->getNbOfElems()<2)
71       {
72         std::ostringstream os; os << msg0 << 'Z' << msg1;
73         throw INTERP_KERNEL::Exception(os.str().c_str());
74       }
75 }
76
77 bool MEDCouplingSMesh::isStructured() const
78 {
79   return true;
80 }
81
82 int MEDCouplingSMesh::getNumberOfCells() const
83 {
84   int ret=1;
85   if(_x_array)
86     ret*=_x_array->getNbOfElems()-1;
87   if(_y_array)
88     ret*=_y_array->getNbOfElems()-1;
89   if(_z_array)
90     ret*=_z_array->getNbOfElems()-1;
91   return ret;
92 }
93
94 int MEDCouplingSMesh::getNumberOfNodes() const
95 {
96   int ret=1;
97   if(_x_array)
98     ret*=_x_array->getNbOfElems();
99   if(_y_array)
100     ret*=_y_array->getNbOfElems();
101   if(_z_array)
102     ret*=_z_array->getNbOfElems();
103   return ret;
104 }
105
106 int MEDCouplingSMesh::getSpaceDimension() const
107 {
108   int ret=0;
109   if(_x_array)
110     ret++;
111   if(_y_array)
112     ret++;
113   if(_z_array)
114     ret++;
115   return ret;
116 }
117
118 int MEDCouplingSMesh::getMeshDimension() const
119 {
120   int ret=0;
121   if(_x_array)
122     ret++;
123   if(_y_array)
124     ret++;
125   if(_z_array)
126     ret++;
127   return ret;
128 }
129
130 DataArrayDouble *MEDCouplingSMesh::getCoordsAt(int i) const throw(INTERP_KERNEL::Exception)
131 {
132   switch(i)
133     {
134     case 0:
135       return _x_array;
136     case 1:
137       return _y_array;
138     case 2:
139       return _z_array;
140     default:
141       throw INTERP_KERNEL::Exception("Invalid rank specified must be 0 or 1 or 2.");
142     }
143 }
144
145 void MEDCouplingSMesh::setCoords(DataArrayDouble *coordsX, DataArrayDouble *coordsY, DataArrayDouble *coordsZ)
146 {
147   if(_x_array)
148     _x_array->decrRef();
149   _x_array=coordsX;
150   if(_x_array)
151     _x_array->incrRef();
152   if(_y_array)
153     _y_array->decrRef();
154   _y_array=coordsY;
155   if(_y_array)
156     _y_array->incrRef();
157   if(_z_array)
158     _z_array->decrRef();
159   _z_array=coordsZ;
160   if(_z_array)
161     _z_array->incrRef();
162   declareAsNew();
163 }
164