Salome HOME
Update C++ code consecutive to API modification of ReadField
[modules/med.git] / src / MEDCalculator / MEDCalculatorBrowserField.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 // Author : Anthony Geay (CEA/DEN)
20
21 #include "MEDCalculatorBrowserStep.hxx"
22 #include "MEDCalculatorBrowserField.hxx"
23
24 #include "MEDLoader.hxx"
25 #include "MEDCouplingFieldDouble.hxx"
26
27 #include <set>
28 #include <sstream>
29 #include <algorithm>
30 #include <functional>
31
32 using namespace MEDCoupling;
33
34 //  Default constructor
35 //  Set field name to nm and selection to flase
36 //  Init empty vector for all other parameters
37 MEDCalculatorBrowserField::MEDCalculatorBrowserField(const char* nm) : _name(nm),_selection(false)
38 {
39 }
40
41 //  Destructor
42 MEDCalculatorBrowserField::~MEDCalculatorBrowserField()
43 {
44 }
45
46 //  Constructor with two parameters :
47 //   - fname, the fileName
48 //   - fieldname, the fieldname
49 //  First set name to fieldname
50 //  Then, read the Med structur to fill time steps, components and meshes
51 MEDCalculatorBrowserField::MEDCalculatorBrowserField(const char *fname, const char *fieldName) : _name(fieldName), _file_name(fname), _selection(false)
52 {
53   std::vector< std::string > meshNames=GetMeshNamesOnField(fname,fieldName);
54   std::vector< std::pair< std::pair<int,int>, double > > dtits=GetAllFieldIterations(fname, fieldName);
55   for(std::vector<std::pair< std::pair<int,int>, double > >::const_iterator iter=dtits.begin();iter!=dtits.end();iter++)
56     {
57       _steps.push_back(MEDCalculatorBrowserStep((*iter).first.first,(*iter).first.second,(*iter).second,meshNames[0]));
58     }
59   std::vector<TypeOfField> types=GetTypesOfField(fname,meshNames[0].c_str(),fieldName);
60   if(types.empty())
61     throw INTERP_KERNEL::Exception("MEDCalculatorBrowserField::MEDCalculatorBrowserField : the file is not loadable using MED File 3 API ! Problably presence of field on edges faces...");
62   _type=types[0];//To improve
63   MCAuto<MEDCouplingFieldDouble> tmpf;
64   try
65     {
66       tmpf=ReadField(_type,fname,meshNames[0].c_str(),0,fieldName,dtits[0].first.first,dtits[0].first.second);
67     }
68   catch(INTERP_KERNEL::Exception& e)
69     {
70       if(_type==ON_CELLS)
71         tmpf=ReadField(_type,fname,meshNames[0].c_str(),-1,fieldName,dtits[0].first.first,dtits[0].first.second);
72       else
73         throw e;
74     }
75   int NumberOfComponents=tmpf->getNumberOfComponents();
76   for(int i=0;i<NumberOfComponents;i++)
77     {
78       std::string c=tmpf->getArray()->getInfoOnComponent(i);
79       if(c=="")
80         c="-noname-";
81       _components.push_back(c);
82     }
83   _corresponding_meshes=GetMeshNamesOnField(fname,fieldName);
84 }
85
86 //  Equal to string operator,
87 //  Test if fieldname of this field is the same as nm
88 bool MEDCalculatorBrowserField::operator==(const std::string& nm)
89 {
90   return nm==_name;
91 }
92
93 //  Equal to bool operator,
94 //  Test selection flag is set just as sel
95 bool MEDCalculatorBrowserField::operator==(bool sel)
96 {
97   return _selection==sel;
98 }
99
100 //  str method
101 //  Construct a std::string to print Field, using std::cout for example
102 //  Put x or o for selected or not
103 //  Add Field fieldname
104 //  Add x/o components
105 //  Add MEDCalculatorBrowserStep::str() for each step
106 //  Return a std::string
107 std::string MEDCalculatorBrowserField::str()
108 {
109   std::ostringstream res;
110   _selection?res<<"x ":res<<"o ";
111   res<<"Field "<<_name;
112
113   res<<"\n\t\tComponents :"<<std::endl;
114   for (unsigned int i = 0; i < _components.size(); i += 1)
115     {
116       res<<"\n\t\t\t"<<i<<" : "<<_components[i]<<""<<std::endl;
117     }
118
119   res<<"\n\t\tTimessteps :"<<std::endl;
120   for (unsigned int i = 0; i < _steps.size(); i += 1)
121     {
122       res<<"\n\t\t\t"<<_steps[i].str();
123     }
124
125   return res.str();
126 }
127
128 //  Select a specific step according to its id
129 //  Use std::find to get the corresponding step
130 //  Also select all components if none is selected (avoid having time steps selected, so field selected, without components)
131 void MEDCalculatorBrowserField::selectStep(int ts)
132 {
133   std::vector<MEDCalculatorBrowserStep>::iterator it = std::find(_steps.begin(),_steps.end(),ts);
134   if(it != _steps.end())
135     {
136       it->select();
137       _selection=true;
138       std::vector<bool>::iterator itb = std::find(_selected_components.begin(),_selected_components.end(),true);
139       if(itb == _selected_components.end())
140         {
141           for (unsigned int i = 0; i < _selected_components.size(); i += 1)
142             {
143             _selected_components[i] = true;
144             }
145         }
146     }
147 }
148
149
150 //  Select all time steps
151 //  Also select all components if none is selected (avoid having time steps selected, so field selected, without components)
152 void MEDCalculatorBrowserField::selectAllSteps()
153 {
154   for (std::vector<MEDCalculatorBrowserStep>::iterator it = _steps.begin(); it != _steps.end(); ++it)
155     {
156       it->select();
157     }
158   std::vector<bool>::iterator itb = std::find(_selected_components.begin(),_selected_components.end(),true);
159   if(itb == _selected_components.end())
160     {
161       for (unsigned int i = 0; i < _selected_components.size(); i += 1)
162         {
163           _selected_components[i] = true;
164         }
165     }
166   _selection=true;
167 }
168
169 //  Unselect a specific time step according to its id
170 //  Check if there is still time step selected :
171 //  - if yes, do nothing;
172 //  - if not, set selection flag to false
173 void MEDCalculatorBrowserField::unselectStep(int ts)
174 {
175   std::vector<MEDCalculatorBrowserStep>::iterator it = std::find(_steps.begin(),_steps.end(),ts);
176   if(it != _steps.end())
177     {
178       it->unselect();
179     }
180   it = std::find(_steps.begin(),_steps.end(),true);
181   if(it == _steps.end())
182     {
183       _selection=false;
184     }
185 }
186
187 //  Unselect all time steps
188 //  Set selection flag to false
189 void MEDCalculatorBrowserField::unselectAllSteps()
190 {
191   for (std::vector<MEDCalculatorBrowserStep>::iterator it = _steps.begin(); it != _steps.end(); ++it)
192     {
193       it->unselect();
194     }
195   _selection=false;
196 }
197
198 //  Return if this field is selected or not, i.e. if some time steps are selected or not
199 bool MEDCalculatorBrowserField::isSelected()
200 {
201   return _selection;
202 }
203
204 //  Return field name
205 const std::string& MEDCalculatorBrowserField::getName() const
206 {
207   return _name;
208 }
209
210 //  Return steps vector
211 const std::vector<MEDCalculatorBrowserStep>& MEDCalculatorBrowserField::getSteps() const
212 {
213   return _steps;
214 }
215
216 //  Return the time value corresponding to time step with id ts
217 const double& MEDCalculatorBrowserField::getTimeValue(int ts) const
218 {
219   std::vector<MEDCalculatorBrowserStep>::const_iterator it = std::find(_steps.begin(),_steps.end(),ts);
220   return it->getTimeValue();
221 }
222
223
224 //  Return the number of time steps
225 unsigned int MEDCalculatorBrowserField::getStepsSize() const
226 {
227   return _steps.size();
228 }
229
230 //  Add a time step to steps vector with (id, time value)
231 void MEDCalculatorBrowserField::addStep(int ts, double tv)
232 {
233   _steps.push_back(MEDCalculatorBrowserStep(ts,tv));
234 }
235
236 //  Select a specific component according to its id
237 void MEDCalculatorBrowserField::selectComponent(int id)
238 {
239   _selected_components[id]=true;
240 }
241
242 //  Select all components
243 void MEDCalculatorBrowserField::selectAllComponents()
244 {
245   for (unsigned int i = 0; i < _selected_components.size(); i += 1)
246     {
247       _selected_components[i] = true;
248     }
249 }
250
251 //  Unselect a specific component according to its id
252 void MEDCalculatorBrowserField::unselectComponent(int id)
253 {
254   _selected_components[id]=false;
255 }
256
257 // Unselect all components
258 void MEDCalculatorBrowserField::unselectAllComponents()
259 {
260   for (unsigned int i = 0; i < _selected_components.size(); i += 1)
261     {
262       _selected_components[i] = false;
263     }
264 }
265
266 //  Return a copy of the components vector
267 const std::vector<std::string>& MEDCalculatorBrowserField::getComponents() const
268 {
269   return _components;
270 }
271
272 //  Return a copy of the selected components vector
273 const std::vector<bool>& MEDCalculatorBrowserField::getSelectedComponents() const
274 {
275   return _selected_components;
276 }
277
278 //  Return the number of components
279 unsigned int MEDCalculatorBrowserField::getComponentsSize() const
280 {
281   return _components.size();
282 }
283
284 //  Return a std::vector of std::string which contains all the meshes name used by all the time steps as supporting meshes
285 const std::vector<std::string>& MEDCalculatorBrowserField::getCorrespondingMeshesFromField() const
286 {
287   return _corresponding_meshes;
288 }
289
290 //  Return a std::string which contains all the meshes name used by the time steps (id) as supporting meshes
291 const std::string& MEDCalculatorBrowserField::getCorrespondingMeshFromStep(int i) const
292 {
293   return _steps[i].getCorrespondingMeshFromStep();
294 }
295
296 //  Change the name of a component, if input is empty, the component name is -noname- (for print)
297 void MEDCalculatorBrowserField::setComponentName(int i,const std::string& name)
298 {
299   std::string name2(name);
300   if(name2 == std::string(""))
301     name2 = "-noname-";
302   _components[i] = name2;
303 }
304
305 /*!
306  * Contrary to isSelected see all time steps.
307  */
308 bool MEDCalculatorBrowserField::isAnySelection() const
309 {
310   return std::find(_steps.begin(),_steps.end(),true)!=_steps.end();
311 }
312
313 void MEDCalculatorBrowserField::setMeshName(const std::string& m)
314 {
315   _corresponding_meshes.resize(1);
316   _corresponding_meshes[0]=m;
317 }
318
319 /*!
320  * This method returns a restriction of this containing only selected TimeSteps.
321  * This methods throws an exception if multiple meshes exists in selected time steps.
322  */
323 MEDCalculatorBrowserField MEDCalculatorBrowserField::getSelectedTimeSteps() const throw(INTERP_KERNEL::Exception)
324 {
325   int nbOfTs=std::count(_steps.begin(),_steps.end(),true);
326   std::vector<MEDCalculatorBrowserStep> ts(nbOfTs);
327   std::vector<MEDCalculatorBrowserStep>::const_iterator it=_steps.begin();
328   std::set<std::string> meshes;
329   for(int i=0;i<nbOfTs;i++)
330     {
331       it=std::find(it,_steps.end(),true);
332       ts[i]=*it;
333       meshes.insert((*it).getCorrespondingMeshFromStep());
334       if(meshes.size()>1)
335         throw INTERP_KERNEL::Exception("Not managed fields with variable meshes during time !");
336     }
337   MEDCalculatorBrowserField ret(*this);
338   ret.setSteps(ts);
339   ret.setMeshName(*(meshes.begin()));
340   return ret;
341 }
342
343 void MEDCalculatorBrowserField::setSteps(const std::vector<MEDCalculatorBrowserStep>& steps)
344 {
345   _steps=steps;
346 }