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