Salome HOME
Merge branch 'V8_0_BR'
[plugins/hexoticplugin.git] / src / HexoticPlugin / HexoticPlugin_Hypothesis.cxx
1 // Copyright (C) 2007-2015  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::SetTextOptions(const std::string& theOptions)
166 {
167   if (_textOptions != theOptions ) {
168     _textOptions = theOptions;
169     NotifySubMeshesHypothesisModification();
170   }
171 }
172
173 bool HexoticPlugin_Hypothesis::AddSizeMap(std::string theEntry, double theSize) {
174   THexoticSizeMaps::iterator it;
175   it=_sizeMaps.find(theEntry);
176   
177   if(theSize <= 0)
178     return false;
179   
180   if( it == _sizeMaps.end() ) // If no size map is defined on the given object
181   {
182     _sizeMaps[theEntry] = theSize;
183     MESSAGE("NEW size map, entry :"<<theEntry<<", size : "<<theSize);
184     NotifySubMeshesHypothesisModification();
185     return true;
186   }
187   else if( it->second != theSize ) // If a size map exists with a different size value
188   {
189     it->second = theSize;
190     MESSAGE("MODIFIED size map, entry :"<<theEntry<<"with size : "<<theSize);
191     NotifySubMeshesHypothesisModification();
192     return true;
193   }
194   else
195   {
196     MESSAGE("NO size map added")
197     return false; // No size map added
198   }
199 }
200
201 bool HexoticPlugin_Hypothesis::UnsetSizeMap(std::string theEntry) {
202   THexoticSizeMaps::iterator it;
203   it=_sizeMaps.find(theEntry);
204   if( it != _sizeMaps.end() )
205   {
206     _sizeMaps.erase(it);  
207     return true;
208   }
209   else
210     return false;
211 }
212
213 void HexoticPlugin_Hypothesis::SetNbLayers(int theVal) {
214   if (theVal != _nbLayers) {
215     _nbLayers = theVal;
216     NotifySubMeshesHypothesisModification();
217   }
218 }
219
220 void HexoticPlugin_Hypothesis::SetFirstLayerSize(double theVal) {
221   if (theVal != _firstLayerSize) {
222     _firstLayerSize = theVal;
223     NotifySubMeshesHypothesisModification();
224   }
225 }
226
227 void HexoticPlugin_Hypothesis::SetDirection(bool theVal) {
228   if (theVal != _direction) {
229     _direction = theVal;
230     NotifySubMeshesHypothesisModification();
231   }
232 }
233
234 void HexoticPlugin_Hypothesis::SetGrowth(double theVal) {
235   if (theVal != _growth) {
236     _growth = theVal;
237     NotifySubMeshesHypothesisModification();
238   }
239 }
240
241 bool HexoticPlugin_Hypothesis::SetFacesWithLayers(const std::vector<int>& theVal) {
242   if ( _facesWithLayers != theVal ) {
243           _facesWithLayers = theVal;
244     NotifySubMeshesHypothesisModification();
245     return true;
246   }
247   return false;
248 }
249
250 bool HexoticPlugin_Hypothesis::SetImprintedFaces(const std::vector<int>& theVal) {
251   if ( _imprintedFaces != theVal ) {
252           _imprintedFaces = theVal;
253     NotifySubMeshesHypothesisModification();
254     return true;
255   }
256   return false;
257 }
258
259 //=============================================================================
260 /*!
261  *  
262  */
263 //=============================================================================
264 std::ostream& HexoticPlugin_Hypothesis::SaveTo(std::ostream& save)
265 {
266   //explicit outputs for future code compatibility of saved .hdf
267   //save without any whitespaces!
268   //int dummy = -1;
269   save<<"hexesMinLevel="<<_hexesMinLevel<<";"; 
270   save<<"hexesMaxLevel="<<_hexesMaxLevel<<";";
271   save<<"hexoticIgnoreRidges="<<(int)_hexoticIgnoreRidges<<";";
272   save<<"hexoticInvalidElements="<<(int)_hexoticInvalidElements<<";";
273   save<<"hexoticSharpAngleThreshold="<<_hexoticSharpAngleThreshold<<";";
274   save<<"hexoticNbProc="<<_hexoticNbProc<<";";
275   save<<"hexoticWorkingDirectory="<<_hexoticWorkingDirectory<<";";
276   save<<"minSize="<<_minSize<<";";
277   save<<"maxSize="<<_maxSize<<";";
278   save<<"hexoticSdMode="<<_hexoticSdMode<<";";
279   save<<"hexoticVerbosity="<<_hexoticVerbosity<<";";
280   save<<"hexoticMaxMemory="<<_hexoticMaxMemory<<";";
281   replace(_textOptions.begin(), _textOptions.end(), ' ', '*');
282   save<<"textOptions="<<_textOptions<<";";
283   THexoticSizeMaps::iterator it = _sizeMaps.begin();
284   if ( it != _sizeMaps.end() )
285   {
286     save<<"sizeMaps=";
287     for ( ; it!=_sizeMaps.end() ; it++ )
288     {
289       save<< it->first << "/" << it->second << "#" ;
290     }
291     save<<";";
292   }
293   save<<"nbLayers="<<_nbLayers<<";";
294   save<<"firstLayerSize="<<_firstLayerSize<<";";
295   save<<"direction="<<_direction<<";";
296   save<<"growth="<<_growth<<";";
297   if ( !_facesWithLayers.empty() )
298   {
299     save<<"facesWithLayers=";
300     for ( size_t i = 0; i < _facesWithLayers.size(); i++ )
301     {
302       save<< _facesWithLayers.at(i) << "#" ;
303     }
304     save<<";";
305   }
306   if ( !_imprintedFaces.empty() )
307   {
308     save<<"imprintedFaces=";
309     for ( size_t i = 0; i < _imprintedFaces.size(); i++ )
310     {
311       save<< _imprintedFaces.at(i) << "#" ;
312     }
313     save<<";";
314   }
315   return save;
316 }
317
318 //=============================================================================
319 /*!
320  *  
321  */
322 //=============================================================================
323 std::istream& HexoticPlugin_Hypothesis::LoadFrom(std::istream& load)
324 {
325    //explicit inputs for future code compatibility of saved .hdf
326    bool isOK = true;
327    std::string str1,str2,str3,str4;
328
329    //save without any whitespaces!
330    isOK = static_cast<bool>(load >> str1);
331    if (!(isOK)) {
332      //defaults values assumed
333      load.clear(std::ios::badbit | load.rdstate());
334      return load;
335    }
336    int pos = 0;
337    int len = str1.length();
338    while (pos < len) {
339       int found = str1.find(';',pos);
340       str2 = str1.substr(pos,found-pos);
341       int eqpos = str2.find('=',0);
342       str3 = str2.substr(0,eqpos);
343       str4 = str2.substr(eqpos+1);
344       pos = found + 1;
345
346       if (str3=="hexesMinLevel") _hexesMinLevel = atoi(str4.c_str());
347       if (str3=="hexesMaxLevel") _hexesMaxLevel = atoi(str4.c_str());
348       if (str3=="hexoticQuadrangles") {}
349       if (str3=="hexoticIgnoreRidges") _hexoticIgnoreRidges = (bool) atoi(str4.c_str());
350       if (str3=="hexoticInvalidElements") _hexoticInvalidElements = (bool) atoi(str4.c_str());
351       if (str3=="hexoticSharpAngleThreshold") _hexoticSharpAngleThreshold = atof(str4.c_str());
352       if (str3=="hexoticNbProc") _hexoticNbProc = atoi(str4.c_str());
353       if (str3=="hexoticWorkingDirectory") _hexoticWorkingDirectory = str4;
354       if (str3=="minSize") _minSize = atof(str4.c_str());
355       if (str3=="maxSize") _maxSize = atof(str4.c_str());
356       if (str3=="hexoticSdMode") _hexoticSdMode = atoi(str4.c_str());
357       if (str3=="hexoticVerbosity") _hexoticVerbosity = atoi(str4.c_str());
358       if (str3=="hexoticMaxMemory") _hexoticMaxMemory = atoi(str4.c_str());
359       if (str3=="textOptions")
360       {
361         replace(str4.begin(), str4.end(), '*', ' ');
362         _textOptions = str4;
363       }
364       if (str3=="sizeMaps")
365       {
366         std::string sm_substr, sm_substr1, sm_substr2;
367         int sm_pos = 0;
368         int sm_len = str4.length();
369         while ( sm_pos < sm_len )
370         {
371           int sm_found = str4.find('#',sm_pos);
372           sm_substr = str4.substr(sm_pos,sm_found-sm_pos);
373           int sm_slashpos = sm_substr.find('/',0);
374           sm_substr1 = sm_substr.substr(0,sm_slashpos);
375           sm_substr2 = sm_substr.substr(sm_slashpos+1);
376           _sizeMaps[sm_substr1] = atof(sm_substr2.c_str());
377           sm_pos = sm_found + 1;
378         }
379       }
380       if (str3 == "nbLayers") _nbLayers = atoi(str4.c_str());
381       if (str3 == "firstLayerSize") _firstLayerSize = atof(str4.c_str());
382       if (str3 == "direction") _direction = atoi(str4.c_str());
383       if (str3 == "growth") _growth = atof(str4.c_str());
384       if (str3 == "facesWithLayers")
385       {
386         std::string id;
387         size_t pos = 0;
388         while ( pos < str4.length() )
389         {
390           int found = str4.find('#',pos);
391           id = str4.substr(pos, found-pos);
392           _facesWithLayers.push_back(atoi(id.c_str()));
393           pos = found + 1;
394         }
395       }
396       if (str3 == "imprintedFaces")
397       {
398         std::string id;
399         size_t pos = 0;
400         while ( pos < str4.length() )
401         {
402           int found = str4.find('#',pos);
403           id = str4.substr(pos, found-pos);
404           _imprintedFaces.push_back(atoi(id.c_str()));
405           pos = found + 1;
406         }
407       }
408    }
409    return load;
410 }
411
412 //=============================================================================
413 /*!
414  *  
415  */
416 //=============================================================================
417 std::ostream& operator <<(std::ostream& save, HexoticPlugin_Hypothesis& hyp)
418 {
419   return hyp.SaveTo( save );
420 }
421
422 //=============================================================================
423 /*!
424  *  
425  */
426 //=============================================================================
427 std::istream& operator >>(std::istream& load, HexoticPlugin_Hypothesis& hyp)
428 {
429   return hyp.LoadFrom( load );
430 }
431
432
433 //================================================================================
434 /*!
435  * \brief Does nothing
436  * \param theMesh - the built mesh
437  * \param theShape - the geometry of interest
438  * \retval bool - always false
439  */
440 //================================================================================
441 bool HexoticPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh*   theMesh,
442                                                    const TopoDS_Shape& theShape)
443 {
444   return false;
445 }
446 //================================================================================
447 /*!
448  * \brief Initialize my parameter values by default parameters.
449  *  \retval bool - true if parameter values have been successfully defined
450  */
451 //================================================================================
452
453 bool HexoticPlugin_Hypothesis::SetParametersByDefaults(const TDefaults&  /*dflts*/,
454                                                        const SMESH_Mesh* /*theMesh*/)
455 {
456   return false;
457 }
458
459 //=============================================================================
460 int HexoticPlugin_Hypothesis::GetDefaultHexesMinLevel()
461 {
462   return 6;
463 }
464
465 int HexoticPlugin_Hypothesis::GetDefaultHexesMaxLevel()
466 {
467   return 10;
468 }
469
470 double HexoticPlugin_Hypothesis::GetDefaultMinSize()
471 {
472   return 0.0;
473 }
474
475 double HexoticPlugin_Hypothesis::GetDefaultMaxSize()
476 {
477   return 0.0;
478 }
479
480 bool HexoticPlugin_Hypothesis::GetDefaultHexoticIgnoreRidges()
481 {
482   return false;
483 }
484
485 bool HexoticPlugin_Hypothesis::GetDefaultHexoticInvalidElements()
486 {
487   return false;
488 }
489
490 double HexoticPlugin_Hypothesis::GetDefaultHexoticSharpAngleThreshold()
491 {
492   return 60.0;
493 }
494
495 int HexoticPlugin_Hypothesis::GetDefaultHexoticNbProc()
496 {
497   return 4;
498 }
499
500 std::string HexoticPlugin_Hypothesis::GetDefaultHexoticWorkingDirectory()
501 {
502   std::string aTmpDir;
503
504   char *Tmp_dir = getenv("SALOME_TMP_DIR");
505 #ifdef WIN32
506   if(Tmp_dir == NULL) {
507     Tmp_dir = getenv("TEMP");
508     if( Tmp_dir== NULL )
509       Tmp_dir = getenv("TMP");
510   }
511 #endif
512   if( Tmp_dir != NULL ) {
513     aTmpDir = std::string(Tmp_dir);
514 #ifdef WIN32
515     if(aTmpDir[aTmpDir.size()-1] != '\\') aTmpDir+='\\';
516 #else
517     if(aTmpDir[aTmpDir.size()-1] != '/') aTmpDir+='/';
518 #endif
519   }
520   else {
521 #ifdef WIN32
522     aTmpDir = "C:\\";
523 #else
524     aTmpDir = "/tmp/";
525 #endif
526   }
527   return aTmpDir;
528 }
529
530 int HexoticPlugin_Hypothesis::GetDefaultHexoticSdMode()
531 {
532   return 4;
533 }
534
535 int HexoticPlugin_Hypothesis::GetDefaultHexoticVerbosity()
536 {
537   return 1;
538 }
539
540 int HexoticPlugin_Hypothesis::GetDefaultHexoticMaxMemory()
541 {
542   return 2048;
543 }
544
545 std::string HexoticPlugin_Hypothesis::GetDefaultTextOptions()
546 {
547   return "";
548 }
549
550 HexoticPlugin_Hypothesis::THexoticSizeMaps HexoticPlugin_Hypothesis::GetDefaultHexoticSizeMaps()
551 {
552   return THexoticSizeMaps();
553 }
554
555 int HexoticPlugin_Hypothesis::GetDefaultNbLayers()
556 {
557   return 0;
558 }
559
560 double HexoticPlugin_Hypothesis::GetDefaultFirstLayerSize()
561 {
562   return 0.0;
563 }
564
565 bool HexoticPlugin_Hypothesis::GetDefaultDirection()
566 {
567   return true;
568 }
569
570 double HexoticPlugin_Hypothesis::GetDefaultGrowth()
571 {
572   return 0.0;
573 }
574
575 std::vector<int> HexoticPlugin_Hypothesis::GetDefaultFacesWithLayers()
576 {
577   return std::vector<int>();
578 }
579
580 std::vector<int> HexoticPlugin_Hypothesis::GetDefaultImprintedFaces()
581 {
582   return std::vector<int>();
583 }