Back to index

salome-med  6.5.0
MEDCalculatorBrowserField.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "MEDCalculatorBrowserStep.hxx"
00021 #include "MEDCalculatorBrowserField.hxx"
00022 
00023 #include "MEDLoader.hxx"
00024 #include "MEDCouplingFieldDouble.hxx"
00025 
00026 #include <set>
00027 #include <sstream>
00028 #include <algorithm>
00029 #include <functional>
00030 
00031 using namespace ParaMEDMEM;
00032 
00033 //  Default constructor
00034 //  Set field name to nm and selection to flase
00035 //  Init empty vector for all other parameters
00036 MEDCalculatorBrowserField::MEDCalculatorBrowserField(const char* nm) : _name(nm),_selection(false)
00037 {
00038 }
00039 
00040 //  Destructor
00041 MEDCalculatorBrowserField::~MEDCalculatorBrowserField()
00042 {
00043 }
00044 
00045 //  Constructor with two parameters :
00046 //   - fname, the fileName
00047 //   - fieldname, the fieldname
00048 //  First set name to fieldname
00049 //  Then, read the Med structur to fill time steps, components and meshes
00050 MEDCalculatorBrowserField::MEDCalculatorBrowserField(const char *fname, const char *fieldName) : _name(fieldName), _file_name(fname), _selection(false)
00051 {
00052   std::vector< std::string > meshNames=MEDLoader::GetMeshNamesOnField(fname,fieldName);
00053   std::vector< std::pair< std::pair<int,int>, double > > dtits=MEDLoader::GetAllFieldIterations(fname,meshNames[0].c_str(),fieldName);
00054   for(std::vector<std::pair< std::pair<int,int>, double > >::const_iterator iter=dtits.begin();iter!=dtits.end();iter++)
00055     {
00056       _steps.push_back(MEDCalculatorBrowserStep((*iter).first.first,(*iter).first.second,(*iter).second,meshNames[0]));
00057     }
00058   std::vector<TypeOfField> types=MEDLoader::GetTypesOfField(fname,meshNames[0].c_str(),fieldName);
00059   if(types.empty())
00060     throw INTERP_KERNEL::Exception("MEDCalculatorBrowserField::MEDCalculatorBrowserField : the file is not loadable using MED File 3 API ! Problably presence of field on edges faces...");
00061   _type=types[0];//To improve
00062   MEDCouplingFieldDouble *tmpf=0;
00063   try
00064     {
00065       tmpf=MEDLoader::ReadField(_type,fname,meshNames[0].c_str(),0,fieldName,dtits[0].first.first,dtits[0].first.second);
00066     }
00067   catch(INTERP_KERNEL::Exception& e)
00068     {
00069       if(_type==ON_CELLS)
00070         tmpf=MEDLoader::ReadField(_type,fname,meshNames[0].c_str(),-1,fieldName,dtits[0].first.first,dtits[0].first.second);
00071       else
00072         throw e;
00073     }
00074   int NumberOfComponents=tmpf->getNumberOfComponents();
00075   for(int i=0;i<NumberOfComponents;i++)
00076     {
00077       std::string c=tmpf->getArray()->getInfoOnComponent(i);
00078       if(c=="")
00079         c="-noname-";
00080       _components.push_back(c);
00081     }
00082   tmpf->decrRef();
00083   _corresponding_meshes=MEDLoader::GetMeshNamesOnField(fname,fieldName);
00084 }
00085 
00086 //  Equal to string operator,
00087 //  Test if fieldname of this field is the same as nm
00088 bool MEDCalculatorBrowserField::operator==(const std::string& nm)
00089 {
00090   return nm==_name;
00091 }
00092 
00093 //  Equal to bool operator,
00094 //  Test selection flag is set just as sel
00095 bool MEDCalculatorBrowserField::operator==(bool sel)
00096 {
00097   return _selection==sel;
00098 }
00099 
00100 //  str method
00101 //  Construct a std::string to print Field, using std::cout for example
00102 //  Put x or o for selected or not
00103 //  Add Field fieldname
00104 //  Add x/o components
00105 //  Add MEDCalculatorBrowserStep::str() for each step
00106 //  Return a std::string
00107 std::string MEDCalculatorBrowserField::str()
00108 {
00109   std::ostringstream res;
00110   _selection?res<<"x ":res<<"o ";
00111   res<<"Field "<<_name;
00112 
00113   res<<"\n\t\tComponents :"<<std::endl;
00114   for (unsigned int i = 0; i < _components.size(); i += 1)
00115     {
00116       res<<"\n\t\t\t"<<i<<" : "<<_components[i]<<""<<std::endl;
00117     }
00118 
00119   res<<"\n\t\tTimessteps :"<<std::endl;
00120   for (unsigned int i = 0; i < _steps.size(); i += 1)
00121     {
00122       res<<"\n\t\t\t"<<_steps[i].str();
00123     }
00124 
00125   return res.str();
00126 }
00127 
00128 //  Select a specific step according to its id
00129 //  Use std::find to get the corresponding step
00130 //  Also select all components if none is selected (avoid having time steps selected, so field selected, without components)
00131 void MEDCalculatorBrowserField::selectStep(int ts)
00132 {
00133   std::vector<MEDCalculatorBrowserStep>::iterator it = std::find(_steps.begin(),_steps.end(),ts);
00134   if(it != _steps.end())
00135     {
00136       it->select();
00137       _selection=true;
00138       std::vector<bool>::iterator itb = std::find(_selected_components.begin(),_selected_components.end(),true);
00139       if(itb == _selected_components.end())
00140         {
00141           for (unsigned int i = 0; i < _selected_components.size(); i += 1)
00142             {
00143             _selected_components[i] = true;
00144             }
00145         }
00146     }
00147 }
00148 
00149 
00150 //  Select all time steps
00151 //  Also select all components if none is selected (avoid having time steps selected, so field selected, without components)
00152 void MEDCalculatorBrowserField::selectAllSteps()
00153 {
00154   for (std::vector<MEDCalculatorBrowserStep>::iterator it = _steps.begin(); it != _steps.end(); ++it)
00155     {
00156       it->select();
00157     }
00158   std::vector<bool>::iterator itb = std::find(_selected_components.begin(),_selected_components.end(),true);
00159   if(itb == _selected_components.end())
00160     {
00161       for (unsigned int i = 0; i < _selected_components.size(); i += 1)
00162         {
00163           _selected_components[i] = true;
00164         }
00165     }
00166   _selection=true;
00167 }
00168 
00169 //  Unselect a specific time step according to its id
00170 //  Check if there is still time step selected :
00171 //  - if yes, do nothing;
00172 //  - if not, set selection flag to false
00173 void MEDCalculatorBrowserField::unselectStep(int ts)
00174 {
00175   std::vector<MEDCalculatorBrowserStep>::iterator it = std::find(_steps.begin(),_steps.end(),ts);
00176   if(it != _steps.end())
00177     {
00178       it->unselect();
00179     }
00180   it = std::find(_steps.begin(),_steps.end(),true);
00181   if(it == _steps.end())
00182     {
00183       _selection=false;
00184     }
00185 }
00186 
00187 //  Unselect all time steps
00188 //  Set selection flag to false
00189 void MEDCalculatorBrowserField::unselectAllSteps()
00190 {
00191   for (std::vector<MEDCalculatorBrowserStep>::iterator it = _steps.begin(); it != _steps.end(); ++it)
00192     {
00193       it->unselect();
00194     }
00195   _selection=false;
00196 }
00197 
00198 //  Return if this field is selected or not, i.e. if some time steps are selected or not
00199 bool MEDCalculatorBrowserField::isSelected()
00200 {
00201   return _selection;
00202 }
00203 
00204 //  Return field name
00205 const std::string& MEDCalculatorBrowserField::getName() const
00206 {
00207   return _name;
00208 }
00209 
00210 //  Return steps vector
00211 const std::vector<MEDCalculatorBrowserStep>& MEDCalculatorBrowserField::getSteps() const
00212 {
00213   return _steps;
00214 }
00215 
00216 //  Return the time value corresponding to time step with id ts
00217 const double& MEDCalculatorBrowserField::getTimeValue(int ts) const
00218 {
00219   std::vector<MEDCalculatorBrowserStep>::const_iterator it = std::find(_steps.begin(),_steps.end(),ts);
00220   return it->getTimeValue();
00221 }
00222 
00223 
00224 //  Return the number of time steps
00225 unsigned int MEDCalculatorBrowserField::getStepsSize() const
00226 {
00227   return _steps.size();
00228 }
00229 
00230 //  Add a time step to steps vector with (id, time value)
00231 void MEDCalculatorBrowserField::addStep(int ts, double tv)
00232 {
00233   _steps.push_back(MEDCalculatorBrowserStep(ts,tv));
00234 }
00235 
00236 //  Select a specific component according to its id
00237 void MEDCalculatorBrowserField::selectComponent(int id)
00238 {
00239   _selected_components[id]=true;
00240 }
00241 
00242 //  Select all components
00243 void MEDCalculatorBrowserField::selectAllComponents()
00244 {
00245   for (unsigned int i = 0; i < _selected_components.size(); i += 1)
00246     {
00247       _selected_components[i] = true;
00248     }
00249 }
00250 
00251 //  Unselect a specific component according to its id
00252 void MEDCalculatorBrowserField::unselectComponent(int id)
00253 {
00254   _selected_components[id]=false;
00255 }
00256 
00257 // Unselect all components
00258 void MEDCalculatorBrowserField::unselectAllComponents()
00259 {
00260   for (unsigned int i = 0; i < _selected_components.size(); i += 1)
00261     {
00262       _selected_components[i] = false;
00263     }
00264 }
00265 
00266 //  Return a copy of the components vector
00267 const std::vector<std::string>& MEDCalculatorBrowserField::getComponents() const
00268 {
00269   return _components;
00270 }
00271 
00272 //  Return a copy of the selected components vector
00273 const std::vector<bool>& MEDCalculatorBrowserField::getSelectedComponents() const
00274 {
00275   return _selected_components;
00276 }
00277 
00278 //  Return the number of components
00279 unsigned int MEDCalculatorBrowserField::getComponentsSize() const
00280 {
00281   return _components.size();
00282 }
00283 
00284 //  Return a std::vector of std::string which contains all the meshes name used by all the time steps as supporting meshes
00285 const std::vector<std::string>& MEDCalculatorBrowserField::getCorrespondingMeshesFromField() const
00286 {
00287   return _corresponding_meshes;
00288 }
00289 
00290 //  Return a std::string which contains all the meshes name used by the time steps (id) as supporting meshes
00291 const std::string& MEDCalculatorBrowserField::getCorrespondingMeshFromStep(int i) const
00292 {
00293   return _steps[i].getCorrespondingMeshFromStep();
00294 }
00295 
00296 //  Change the name of a component, if input is empty, the component name is -noname- (for print)
00297 void MEDCalculatorBrowserField::setComponentName(int i,const std::string& name)
00298 {
00299   std::string name2(name);
00300   if(name2 == std::string(""))
00301     name2 = "-noname-";
00302   _components[i] = name2;
00303 }
00304 
00308 bool MEDCalculatorBrowserField::isAnySelection() const
00309 {
00310   return std::find(_steps.begin(),_steps.end(),true)!=_steps.end();
00311 }
00312 
00313 void MEDCalculatorBrowserField::setMeshName(const std::string& m)
00314 {
00315   _corresponding_meshes.resize(1);
00316   _corresponding_meshes[0]=m;
00317 }
00318 
00323 MEDCalculatorBrowserField MEDCalculatorBrowserField::getSelectedTimeSteps() const throw(INTERP_KERNEL::Exception)
00324 {
00325   int nbOfTs=std::count(_steps.begin(),_steps.end(),true);
00326   std::vector<MEDCalculatorBrowserStep> ts(nbOfTs);
00327   std::vector<MEDCalculatorBrowserStep>::const_iterator it=_steps.begin();
00328   std::set<std::string> meshes;
00329   for(int i=0;i<nbOfTs;i++)
00330     {
00331       it=std::find(it,_steps.end(),true);
00332       ts[i]=*it;
00333       meshes.insert((*it).getCorrespondingMeshFromStep());
00334       if(meshes.size()>1)
00335         throw INTERP_KERNEL::Exception("Not managed fields with variable meshes during time !");
00336     }
00337   MEDCalculatorBrowserField ret(*this);
00338   ret.setSteps(ts);
00339   ret.setMeshName(*(meshes.begin()));
00340   return ret;
00341 }
00342 
00343 void MEDCalculatorBrowserField::setSteps(const std::vector<MEDCalculatorBrowserStep>& steps)
00344 {
00345   _steps=steps;
00346 }