Salome HOME
Merge V9_dev branch into master
[plugins/hexoticplugin.git] / src / HexoticPlugin / HexoticPlugin_Hypothesis.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
20 // ---
21 // File   : HexoticPlugin_Hypothesis.cxx
22 // Author : Lioka RAZAFINDRAZAKA (CEA)
23 // ---
24 //
25 #include "HexoticPlugin_Hypothesis.hxx"
26 #include <utilities.h>
27
28 #include <TCollection_AsciiString.hxx>
29
30 //=============================================================================
31 /*!
32  *  
33  */
34 //=============================================================================
35 HexoticPlugin_Hypothesis::HexoticPlugin_Hypothesis (int hypId, SMESH_Gen* gen)
36   : SMESH_Hypothesis(hypId, gen),
37     _hexesMinLevel( GetDefaultHexesMinLevel() ),
38     _hexesMaxLevel( GetDefaultHexesMaxLevel() ),
39     _minSize( GetDefaultMinSize() ),
40     _maxSize( GetDefaultMaxSize() ),
41     _hexoticIgnoreRidges( GetDefaultHexoticIgnoreRidges() ),
42     _hexoticInvalidElements( GetDefaultHexoticInvalidElements() ), 
43     _hexoticSharpAngleThreshold( GetDefaultHexoticSharpAngleThreshold() ),
44     _hexoticNbProc( GetDefaultHexoticNbProc() ),
45     _hexoticSdMode(GetDefaultHexoticSdMode()),
46     _hexoticVerbosity(GetDefaultHexoticVerbosity()),
47     _hexoticMaxMemory(GetDefaultHexoticMaxMemory()),
48     _textOptions(GetDefaultTextOptions()),
49     _sizeMaps(GetDefaultHexoticSizeMaps()),
50     _hexoticWorkingDirectory( GetDefaultHexoticWorkingDirectory() ),
51     _nbLayers(GetDefaultNbLayers()),
52     _firstLayerSize(GetDefaultFirstLayerSize()),
53     _direction(GetDefaultDirection()),
54     _growth(GetDefaultGrowth()),
55     _facesWithLayers(GetDefaultFacesWithLayers()),
56     _imprintedFaces(GetDefaultImprintedFaces())
57 {
58   MESSAGE("HexoticPlugin_Hypothesis::HexoticPlugin_Hypothesis");
59   _name = GetHypType();
60   _param_algo_dim = 3;
61 }
62
63 //=============================================================================
64 /*!
65  *  
66  */
67 //=============================================================================
68
69 void HexoticPlugin_Hypothesis::SetHexesMinLevel(int theVal) {
70   if (theVal != _hexesMinLevel) {
71     _hexesMinLevel = theVal;
72     NotifySubMeshesHypothesisModification();
73   }
74 }
75
76 void HexoticPlugin_Hypothesis::SetHexesMaxLevel(int theVal) {
77   if (theVal != _hexesMaxLevel) {
78     _hexesMaxLevel = theVal;
79     NotifySubMeshesHypothesisModification();
80   }
81 }
82
83 void HexoticPlugin_Hypothesis::SetMinSize(double theVal) {
84   if (theVal != _minSize) {
85           _minSize = theVal;
86     NotifySubMeshesHypothesisModification();
87   }
88 }
89
90 void HexoticPlugin_Hypothesis::SetMaxSize(double theVal) {
91   if (theVal != _maxSize) {
92           _maxSize = theVal;
93     NotifySubMeshesHypothesisModification();
94   }
95 }
96
97 void HexoticPlugin_Hypothesis::SetHexoticIgnoreRidges(bool theVal) {
98   if (theVal != _hexoticIgnoreRidges) {
99     _hexoticIgnoreRidges = theVal;
100     NotifySubMeshesHypothesisModification();
101   }
102 }
103
104 void HexoticPlugin_Hypothesis::SetHexoticInvalidElements(bool theVal) {
105   if (theVal != _hexoticInvalidElements) {
106     _hexoticInvalidElements = theVal;
107     NotifySubMeshesHypothesisModification();
108   }
109 }
110
111 void HexoticPlugin_Hypothesis::SetHexoticSharpAngleThreshold(double theVal) {
112   if (theVal != _hexoticSharpAngleThreshold) {
113     _hexoticSharpAngleThreshold = theVal;
114     NotifySubMeshesHypothesisModification();
115   }
116 }
117
118 void HexoticPlugin_Hypothesis::SetHexoticNbProc(int theVal) {
119   if (theVal != _hexoticNbProc) {
120     _hexoticNbProc = theVal;
121     NotifySubMeshesHypothesisModification();
122   }
123 }
124
125 void HexoticPlugin_Hypothesis::SetHexoticWorkingDirectory(const std::string& path)
126 {
127   if ( _hexoticWorkingDirectory != path ) {
128     _hexoticWorkingDirectory = path;
129     if ( !path.empty() )
130     {
131 #ifdef WIN32
132       if( path[ path.size()-1 ] != '\\')
133         _hexoticWorkingDirectory += '\\';
134 #else
135       if( path[ path.size()-1 ] != '/')
136         _hexoticWorkingDirectory += '/';
137 #endif
138     }
139     NotifySubMeshesHypothesisModification();
140   }
141 }
142
143 void HexoticPlugin_Hypothesis::SetHexoticSdMode(int theVal) {
144   if (theVal != _hexoticSdMode) {
145     _hexoticSdMode = theVal;
146     NotifySubMeshesHypothesisModification();
147   }
148 }
149
150 void HexoticPlugin_Hypothesis::SetHexoticVerbosity(int theVal) {
151   if (theVal != _hexoticVerbosity) {
152     _hexoticVerbosity = theVal;
153     NotifySubMeshesHypothesisModification();
154   }
155 }
156
157 void HexoticPlugin_Hypothesis::SetHexoticMaxMemory(int theVal) {
158   if (theVal != _hexoticMaxMemory) {
159     _hexoticMaxMemory = theVal;
160     NotifySubMeshesHypothesisModification();
161   }
162 }
163
164 void HexoticPlugin_Hypothesis::SetAdvancedOption(const std::string& theOptions)
165 {
166   if (_textOptions != theOptions ) {
167     _textOptions = theOptions;
168     NotifySubMeshesHypothesisModification();
169   }
170 }
171 void HexoticPlugin_Hypothesis::SetTextOptions(const std::string& theOptions)
172 {
173   if (_textOptions != theOptions ) {
174     _textOptions = theOptions;
175     NotifySubMeshesHypothesisModification();
176   }
177 }
178
179 bool HexoticPlugin_Hypothesis::AddSizeMap(std::string theEntry, double theSize) {
180   THexoticSizeMaps::iterator it;
181   it=_sizeMaps.find(theEntry);
182   
183   if(theSize <= 0)
184     return false;
185   
186   if( it == _sizeMaps.end() ) // If no size map is defined on the given object
187   {
188     _sizeMaps[theEntry] = theSize;
189     MESSAGE("NEW size map, entry :"<<theEntry<<", size : "<<theSize);
190     NotifySubMeshesHypothesisModification();
191     return true;
192   }
193   else if( it->second != theSize ) // If a size map exists with a different size value
194   {
195     it->second = theSize;
196     MESSAGE("MODIFIED size map, entry :"<<theEntry<<"with size : "<<theSize);
197     NotifySubMeshesHypothesisModification();
198     return true;
199   }
200   else
201   {
202     MESSAGE("NO size map added")
203     return false; // No size map added
204   }
205 }
206
207 bool HexoticPlugin_Hypothesis::UnsetSizeMap(std::string theEntry) {
208   THexoticSizeMaps::iterator it;
209   it=_sizeMaps.find(theEntry);
210   if( it != _sizeMaps.end() )
211   {
212     _sizeMaps.erase(it);  
213     return true;
214   }
215   else
216     return false;
217 }
218
219 void HexoticPlugin_Hypothesis::SetNbLayers(int theVal) {
220   if (theVal != _nbLayers) {
221     _nbLayers = theVal;
222     NotifySubMeshesHypothesisModification();
223   }
224 }
225
226 void HexoticPlugin_Hypothesis::SetFirstLayerSize(double theVal) {
227   if (theVal != _firstLayerSize) {
228     _firstLayerSize = theVal;
229     NotifySubMeshesHypothesisModification();
230   }
231 }
232
233 void HexoticPlugin_Hypothesis::SetDirection(bool theVal) {
234   if (theVal != _direction) {
235     _direction = theVal;
236     NotifySubMeshesHypothesisModification();
237   }
238 }
239
240 void HexoticPlugin_Hypothesis::SetGrowth(double theVal) {
241   if (theVal != _growth) {
242     _growth = theVal;
243     NotifySubMeshesHypothesisModification();
244   }
245 }
246
247 bool HexoticPlugin_Hypothesis::SetFacesWithLayers(const std::vector<int>& theVal) {
248   if ( _facesWithLayers != theVal ) {
249           _facesWithLayers = theVal;
250     NotifySubMeshesHypothesisModification();
251     return true;
252   }
253   return false;
254 }
255
256 bool HexoticPlugin_Hypothesis::SetImprintedFaces(const std::vector<int>& theVal) {
257   if ( _imprintedFaces != theVal ) {
258           _imprintedFaces = theVal;
259     NotifySubMeshesHypothesisModification();
260     return true;
261   }
262   return false;
263 }
264
265 //=============================================================================
266 /*!
267  *  
268  */
269 //=============================================================================
270 std::ostream& HexoticPlugin_Hypothesis::SaveTo(std::ostream& save)
271 {
272   //explicit outputs for future code compatibility of saved .hdf
273   //save without any whitespaces!
274   //int dummy = -1;
275   save<<"hexesMinLevel="<<_hexesMinLevel<<";"; 
276   save<<"hexesMaxLevel="<<_hexesMaxLevel<<";";
277   save<<"hexoticIgnoreRidges="<<(int)_hexoticIgnoreRidges<<";";
278   save<<"hexoticInvalidElements="<<(int)_hexoticInvalidElements<<";";
279   save<<"hexoticSharpAngleThreshold="<<_hexoticSharpAngleThreshold<<";";
280   save<<"hexoticNbProc="<<_hexoticNbProc<<";";
281   save<<"hexoticWorkingDirectory="<<_hexoticWorkingDirectory<<";";
282   save<<"minSize="<<_minSize<<";";
283   save<<"maxSize="<<_maxSize<<";";
284   save<<"hexoticSdMode="<<_hexoticSdMode<<";";
285   save<<"hexoticVerbosity="<<_hexoticVerbosity<<";";
286   save<<"hexoticMaxMemory="<<_hexoticMaxMemory<<";";
287   std::string textOptions = _textOptions; // save _textOptions
288   replace(textOptions.begin(), textOptions.end(), ' ', '*');
289   save<<"textOptions="<<textOptions<<";";
290   THexoticSizeMaps::iterator it = _sizeMaps.begin();
291   if ( it != _sizeMaps.end() )
292   {
293     save<<"sizeMaps=";
294     for ( ; it!=_sizeMaps.end() ; it++ )
295     {
296       save<< it->first << "/" << it->second << "#" ;
297     }
298     save<<";";
299   }
300   save<<"nbLayers="<<_nbLayers<<";";
301   save<<"firstLayerSize="<<_firstLayerSize<<";";
302   save<<"direction="<<_direction<<";";
303   save<<"growth="<<_growth<<";";
304   if ( !_facesWithLayers.empty() )
305   {
306     save<<"facesWithLayers=";
307     for ( size_t i = 0; i < _facesWithLayers.size(); i++ )
308     {
309       save<< _facesWithLayers.at(i) << "#" ;
310     }
311     save<<";";
312   }
313   if ( !_imprintedFaces.empty() )
314   {
315     save<<"imprintedFaces=";
316     for ( size_t i = 0; i < _imprintedFaces.size(); i++ )
317     {
318       save<< _imprintedFaces.at(i) << "#" ;
319     }
320     save<<";";
321   }
322   return save;
323 }
324
325 //=============================================================================
326 /*!
327  *  
328  */
329 //=============================================================================
330 std::istream& HexoticPlugin_Hypothesis::LoadFrom(std::istream& load)
331 {
332    //explicit inputs for future code compatibility of saved .hdf
333    bool isOK = true;
334    std::string str1,str2,str3,str4;
335
336    //save without any whitespaces!
337    isOK = static_cast<bool>(load >> str1);
338    if (!(isOK)) {
339      //defaults values assumed
340      load.clear(std::ios::badbit | load.rdstate());
341      return load;
342    }
343    int pos = 0;
344    int len = str1.length();
345    while (pos < len) {
346       int found = str1.find(';',pos);
347       str2 = str1.substr(pos,found-pos);
348       int eqpos = str2.find('=',0);
349       str3 = str2.substr(0,eqpos);
350       str4 = str2.substr(eqpos+1);
351       pos = found + 1;
352
353       if (str3=="hexesMinLevel") _hexesMinLevel = atoi(str4.c_str());
354       if (str3=="hexesMaxLevel") _hexesMaxLevel = atoi(str4.c_str());
355       if (str3=="hexoticQuadrangles") {}
356       if (str3=="hexoticIgnoreRidges") _hexoticIgnoreRidges = (bool) atoi(str4.c_str());
357       if (str3=="hexoticInvalidElements") _hexoticInvalidElements = (bool) atoi(str4.c_str());
358       if (str3=="hexoticSharpAngleThreshold") _hexoticSharpAngleThreshold = atof(str4.c_str());
359       if (str3=="hexoticNbProc") _hexoticNbProc = atoi(str4.c_str());
360       if (str3=="hexoticWorkingDirectory") _hexoticWorkingDirectory = str4;
361       if (str3=="minSize") _minSize = atof(str4.c_str());
362       if (str3=="maxSize") _maxSize = atof(str4.c_str());
363       if (str3=="hexoticSdMode") _hexoticSdMode = atoi(str4.c_str());
364       if (str3=="hexoticVerbosity") _hexoticVerbosity = atoi(str4.c_str());
365       if (str3=="hexoticMaxMemory") _hexoticMaxMemory = atoi(str4.c_str());
366       if (str3=="textOptions")
367       {
368         replace(str4.begin(), str4.end(), '*', ' ');
369         _textOptions = str4;
370       }
371       if (str3=="sizeMaps")
372       {
373         std::string sm_substr, sm_substr1, sm_substr2;
374         int sm_pos = 0;
375         int sm_len = str4.length();
376         while ( sm_pos < sm_len )
377         {
378           int sm_found = str4.find('#',sm_pos);
379           sm_substr = str4.substr(sm_pos,sm_found-sm_pos);
380           int sm_slashpos = sm_substr.find('/',0);
381           sm_substr1 = sm_substr.substr(0,sm_slashpos);
382           sm_substr2 = sm_substr.substr(sm_slashpos+1);
383           _sizeMaps[sm_substr1] = atof(sm_substr2.c_str());
384           sm_pos = sm_found + 1;
385         }
386       }
387       if (str3 == "nbLayers") _nbLayers = atoi(str4.c_str());
388       if (str3 == "firstLayerSize") _firstLayerSize = atof(str4.c_str());
389       if (str3 == "direction") _direction = atoi(str4.c_str());
390       if (str3 == "growth") _growth = atof(str4.c_str());
391       if (str3 == "facesWithLayers")
392       {
393         std::string id;
394         size_t pos = 0;
395         while ( pos < str4.length() )
396         {
397           int found = str4.find('#',pos);
398           id = str4.substr(pos, found-pos);
399           _facesWithLayers.push_back(atoi(id.c_str()));
400           pos = found + 1;
401         }
402       }
403       if (str3 == "imprintedFaces")
404       {
405         std::string id;
406         size_t pos = 0;
407         while ( pos < str4.length() )
408         {
409           int found = str4.find('#',pos);
410           id = str4.substr(pos, found-pos);
411           _imprintedFaces.push_back(atoi(id.c_str()));
412           pos = found + 1;
413         }
414       }
415    }
416    return load;
417 }
418
419 //=============================================================================
420 /*!
421  *  
422  */
423 //=============================================================================
424 std::ostream& operator <<(std::ostream& save, HexoticPlugin_Hypothesis& hyp)
425 {
426   return hyp.SaveTo( save );
427 }
428
429 //=============================================================================
430 /*!
431  *  
432  */
433 //=============================================================================
434 std::istream& operator >>(std::istream& load, HexoticPlugin_Hypothesis& hyp)
435 {
436   return hyp.LoadFrom( load );
437 }
438
439
440 //================================================================================
441 /*!
442  * \brief Does nothing
443  * \param theMesh - the built mesh
444  * \param theShape - the geometry of interest
445  * \retval bool - always false
446  */
447 //================================================================================
448 bool HexoticPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh*   theMesh,
449                                                    const TopoDS_Shape& theShape)
450 {
451   return false;
452 }
453 //================================================================================
454 /*!
455  * \brief Initialize my parameter values by default parameters.
456  *  \retval bool - true if parameter values have been successfully defined
457  */
458 //================================================================================
459
460 bool HexoticPlugin_Hypothesis::SetParametersByDefaults(const TDefaults&  /*dflts*/,
461                                                        const SMESH_Mesh* /*theMesh*/)
462 {
463   return false;
464 }
465
466 //=============================================================================
467 int HexoticPlugin_Hypothesis::GetDefaultHexesMinLevel()
468 {
469   return 6;
470 }
471
472 int HexoticPlugin_Hypothesis::GetDefaultHexesMaxLevel()
473 {
474   return 10;
475 }
476
477 double HexoticPlugin_Hypothesis::GetDefaultMinSize()
478 {
479   return 0.0;
480 }
481
482 double HexoticPlugin_Hypothesis::GetDefaultMaxSize()
483 {
484   return 0.0;
485 }
486
487 bool HexoticPlugin_Hypothesis::GetDefaultHexoticIgnoreRidges()
488 {
489   return false;
490 }
491
492 bool HexoticPlugin_Hypothesis::GetDefaultHexoticInvalidElements()
493 {
494   return false;
495 }
496
497 double HexoticPlugin_Hypothesis::GetDefaultHexoticSharpAngleThreshold()
498 {
499   return 60.0;
500 }
501
502 int HexoticPlugin_Hypothesis::GetDefaultHexoticNbProc()
503 {
504   return 4;
505 }
506
507 std::string HexoticPlugin_Hypothesis::GetDefaultHexoticWorkingDirectory()
508 {
509   std::string aTmpDir;
510
511   char *Tmp_dir = getenv("SALOME_TMP_DIR");
512 #ifdef WIN32
513   if(Tmp_dir == NULL) {
514     Tmp_dir = getenv("TEMP");
515     if( Tmp_dir== NULL )
516       Tmp_dir = getenv("TMP");
517   }
518 #endif
519   if( Tmp_dir != NULL ) {
520     aTmpDir = std::string(Tmp_dir);
521 #ifdef WIN32
522     if(aTmpDir[aTmpDir.size()-1] != '\\') aTmpDir+='\\';
523 #else
524     if(aTmpDir[aTmpDir.size()-1] != '/') aTmpDir+='/';
525 #endif
526   }
527   else {
528 #ifdef WIN32
529     aTmpDir = "C:\\";
530 #else
531     aTmpDir = "/tmp/";
532 #endif
533   }
534   return aTmpDir;
535 }
536
537 int HexoticPlugin_Hypothesis::GetDefaultHexoticSdMode()
538 {
539   return 4;
540 }
541
542 int HexoticPlugin_Hypothesis::GetDefaultHexoticVerbosity()
543 {
544   return 1;
545 }
546
547 int HexoticPlugin_Hypothesis::GetDefaultHexoticMaxMemory()
548 {
549   return 2048;
550 }
551
552 std::string HexoticPlugin_Hypothesis::GetDefaultTextOptions()
553 {
554   return "";
555 }
556
557 HexoticPlugin_Hypothesis::THexoticSizeMaps HexoticPlugin_Hypothesis::GetDefaultHexoticSizeMaps()
558 {
559   return THexoticSizeMaps();
560 }
561
562 int HexoticPlugin_Hypothesis::GetDefaultNbLayers()
563 {
564   return 0;
565 }
566
567 double HexoticPlugin_Hypothesis::GetDefaultFirstLayerSize()
568 {
569   return 0.0;
570 }
571
572 bool HexoticPlugin_Hypothesis::GetDefaultDirection()
573 {
574   return true;
575 }
576
577 double HexoticPlugin_Hypothesis::GetDefaultGrowth()
578 {
579   return 0.0;
580 }
581
582 std::vector<int> HexoticPlugin_Hypothesis::GetDefaultFacesWithLayers()
583 {
584   return std::vector<int>();
585 }
586
587 std::vector<int> HexoticPlugin_Hypothesis::GetDefaultImprintedFaces()
588 {
589   return std::vector<int>();
590 }