Salome HOME
Merge branch 'V7_main'
[plugins/hexoticplugin.git] / src / HexoticPlugin / HexoticPlugin_Hypothesis.cxx
1 // Copyright (C) 2007-2014  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     _hexoticWorkingDirectory( GetDefaultHexoticWorkingDirectory() ),
47     _hexoticSdMode(GetDefaultHexoticSdMode()),
48     _hexoticVerbosity(GetDefaultHexoticVerbosity()),
49     _hexoticMaxMemory(GetDefaultHexoticMaxMemory()),
50     _sizeMaps(GetDefaultHexoticSizeMaps())
51 {
52   MESSAGE("HexoticPlugin_Hypothesis::HexoticPlugin_Hypothesis");
53   _name = "Hexotic_Parameters";
54   _param_algo_dim = 3;
55 }
56
57 //=============================================================================
58 /*!
59  *  
60  */
61 //=============================================================================
62
63 void HexoticPlugin_Hypothesis::SetHexesMinLevel(int theVal) {
64   if (theVal != _hexesMinLevel) {
65     _hexesMinLevel = theVal;
66     NotifySubMeshesHypothesisModification();
67   }
68 }
69
70 void HexoticPlugin_Hypothesis::SetHexesMaxLevel(int theVal) {
71   if (theVal != _hexesMaxLevel) {
72     _hexesMaxLevel = theVal;
73     NotifySubMeshesHypothesisModification();
74   }
75 }
76
77 void HexoticPlugin_Hypothesis::SetMinSize(double theVal) {
78   if (theVal != _minSize) {
79           _minSize = theVal;
80     NotifySubMeshesHypothesisModification();
81   }
82 }
83
84 void HexoticPlugin_Hypothesis::SetMaxSize(double theVal) {
85   if (theVal != _maxSize) {
86           _maxSize = theVal;
87     NotifySubMeshesHypothesisModification();
88   }
89 }
90
91 void HexoticPlugin_Hypothesis::SetHexoticIgnoreRidges(bool theVal) {
92   if (theVal != _hexoticIgnoreRidges) {
93     _hexoticIgnoreRidges = theVal;
94     NotifySubMeshesHypothesisModification();
95   }
96 }
97
98 void HexoticPlugin_Hypothesis::SetHexoticInvalidElements(bool theVal) {
99   if (theVal != _hexoticInvalidElements) {
100     _hexoticInvalidElements = theVal;
101     NotifySubMeshesHypothesisModification();
102   }
103 }
104
105 void HexoticPlugin_Hypothesis::SetHexoticSharpAngleThreshold(double theVal) {
106   if (theVal != _hexoticSharpAngleThreshold) {
107     _hexoticSharpAngleThreshold = theVal;
108     NotifySubMeshesHypothesisModification();
109   }
110 }
111
112 void HexoticPlugin_Hypothesis::SetHexoticNbProc(int theVal) {
113   if (theVal != _hexoticNbProc) {
114     _hexoticNbProc = theVal;
115     NotifySubMeshesHypothesisModification();
116   }
117 }
118
119 void HexoticPlugin_Hypothesis::SetHexoticWorkingDirectory(const std::string& path)
120 {
121   if ( _hexoticWorkingDirectory != path ) {
122     _hexoticWorkingDirectory = path;
123     if ( !path.empty() )
124     {
125 #ifdef WIN32
126       if( path[ path.size()-1 ] != '\\')
127         _hexoticWorkingDirectory += '\\';
128 #else
129       if( path[ path.size()-1 ] != '/')
130         _hexoticWorkingDirectory += '/';
131 #endif
132     }
133     NotifySubMeshesHypothesisModification();
134   }
135 }
136
137 void HexoticPlugin_Hypothesis::SetHexoticSdMode(int theVal) {
138   if (theVal != _hexoticSdMode) {
139     _hexoticSdMode = theVal;
140     NotifySubMeshesHypothesisModification();
141   }
142 }
143
144 void HexoticPlugin_Hypothesis::SetHexoticVerbosity(int theVal) {
145   if (theVal != _hexoticVerbosity) {
146     _hexoticVerbosity = theVal;
147     NotifySubMeshesHypothesisModification();
148   }
149 }
150
151 void HexoticPlugin_Hypothesis::SetHexoticMaxMemory(int theVal) {
152   if (theVal != _hexoticMaxMemory) {
153     _hexoticMaxMemory = theVal;
154     NotifySubMeshesHypothesisModification();
155   }
156 }
157
158 bool HexoticPlugin_Hypothesis::AddSizeMap(std::string theEntry, double theSize) {
159   THexoticSizeMaps::iterator it;
160   it=_sizeMaps.find(theEntry);
161   
162   if(theSize <= 0)
163     return false;
164   
165   if( it == _sizeMaps.end() ) // If no size map is defined on the given object
166   {
167     _sizeMaps[theEntry] = theSize;
168     MESSAGE("NEW size map, entry :"<<theEntry<<", size : "<<theSize);
169     NotifySubMeshesHypothesisModification();
170     return true;
171   }
172   else if( it->second != theSize ) // If a size map exists with a different size value
173   {
174     it->second = theSize;
175     MESSAGE("MODIFIED size map, entry :"<<theEntry<<"with size : "<<theSize);
176     NotifySubMeshesHypothesisModification();
177     return true;
178   }
179   else
180   {
181     MESSAGE("NO size map added")
182     return false; // No size map added
183   }
184 }
185
186 bool HexoticPlugin_Hypothesis::UnsetSizeMap(std::string theEntry) {
187   THexoticSizeMaps::iterator it;
188   it=_sizeMaps.find(theEntry);
189   if( it != _sizeMaps.end() )
190   {
191     _sizeMaps.erase(it);  
192     return true;
193   }
194   else
195     return false;
196 }
197
198 //=============================================================================
199 /*!
200  *  
201  */
202 //=============================================================================
203 std::ostream& HexoticPlugin_Hypothesis::SaveTo(std::ostream& save)
204 {
205   //explicit outputs for future code compatibility of saved .hdf
206   //save without any whitespaces!
207   int dummy = -1;
208   save<<"hexesMinLevel="<<_hexesMinLevel<<";"; 
209   save<<"hexesMaxLevel="<<_hexesMaxLevel<<";";
210   save<<"hexoticIgnoreRidges="<<(int)_hexoticIgnoreRidges<<";";
211   save<<"hexoticInvalidElements="<<(int)_hexoticInvalidElements<<";";
212   save<<"hexoticSharpAngleThreshold="<<_hexoticSharpAngleThreshold<<";";
213   save<<"hexoticNbProc="<<_hexoticNbProc<<";";
214   save<<"hexoticWorkingDirectory="<<_hexoticWorkingDirectory<<";";
215   save<<"minSize="<<_minSize<<";";
216   save<<"maxSize="<<_maxSize<<";";
217   save<<"hexoticSdMode="<<_hexoticSdMode<<";";
218   save<<"hexoticVerbosity="<<_hexoticVerbosity<<";";
219   save<<"hexoticMaxMemory="<<_hexoticMaxMemory<<";";
220   THexoticSizeMaps::iterator it = _sizeMaps.begin();
221   if ( it != _sizeMaps.end() )
222   {
223     save<<"sizeMaps=";
224     for ( ; it!=_sizeMaps.end() ; it++ )
225     {
226       save<< it->first << "/" << it->second << "#" ;
227     }
228     save<<";";
229   }
230   return save;
231 }
232
233 //=============================================================================
234 /*!
235  *  
236  */
237 //=============================================================================
238 std::istream& HexoticPlugin_Hypothesis::LoadFrom(std::istream& load)
239 {
240    //explicit inputs for future code compatibility of saved .hdf
241    bool isOK = true;
242    std::string str1,str2,str3,str4;
243
244    //save without any whitespaces!
245    isOK = (load >> str1);
246    if (!(isOK)) {
247      //defaults values assumed
248      load.clear(std::ios::badbit | load.rdstate());
249      return load;
250    }
251    int pos = 0;
252    int len = str1.length();
253    while (pos < len) {
254       int found = str1.find(';',pos);
255       str2 = str1.substr(pos,found-pos);
256       int eqpos = str2.find('=',0);
257       str3 = str2.substr(0,eqpos);
258       str4 = str2.substr(eqpos+1);
259       pos = found + 1;
260
261       if (str3=="hexesMinLevel") _hexesMinLevel = atoi(str4.c_str());
262       if (str3=="hexesMaxLevel") _hexesMaxLevel = atoi(str4.c_str());
263       if (str3=="hexoticQuadrangles") {}
264       if (str3=="hexoticIgnoreRidges") _hexoticIgnoreRidges = (bool) atoi(str4.c_str());
265       if (str3=="hexoticInvalidElements") _hexoticInvalidElements = (bool) atoi(str4.c_str());
266       if (str3=="hexoticSharpAngleThreshold") _hexoticSharpAngleThreshold = atof(str4.c_str());
267       if (str3=="hexoticNbProc") _hexoticNbProc = atoi(str4.c_str());
268       if (str3=="hexoticWorkingDirectory") _hexoticWorkingDirectory = str4;
269       if (str3=="minSize") _minSize = atof(str4.c_str());
270       if (str3=="maxSize") _maxSize = atof(str4.c_str());
271       if (str3=="hexoticSdMode") _hexoticSdMode = atoi(str4.c_str());
272       if (str3=="hexoticVerbosity") _hexoticVerbosity = atoi(str4.c_str());
273       if (str3=="hexoticMaxMemory") _hexoticMaxMemory = atoi(str4.c_str());
274       if (str3=="sizeMaps")
275       {
276         std::string sm_substr, sm_substr1, sm_substr2;
277         int sm_pos = 0;
278         int sm_len = str4.length();
279         while ( sm_pos < sm_len )
280         {
281           int sm_found = str4.find('#',sm_pos);
282           sm_substr = str4.substr(sm_pos,sm_found-sm_pos);
283           int sm_slashpos = sm_substr.find('/',0);
284           sm_substr1 = sm_substr.substr(0,sm_slashpos);
285           sm_substr2 = sm_substr.substr(sm_slashpos+1);
286           _sizeMaps[sm_substr1] = atof(sm_substr2.c_str());
287           sm_pos = sm_found + 1;
288         }
289       }
290    }
291    return load;
292 }
293
294 //=============================================================================
295 /*!
296  *  
297  */
298 //=============================================================================
299 std::ostream& operator <<(std::ostream& save, HexoticPlugin_Hypothesis& hyp)
300 {
301   return hyp.SaveTo( save );
302 }
303
304 //=============================================================================
305 /*!
306  *  
307  */
308 //=============================================================================
309 std::istream& operator >>(std::istream& load, HexoticPlugin_Hypothesis& hyp)
310 {
311   return hyp.LoadFrom( load );
312 }
313
314
315 //================================================================================
316 /*!
317  * \brief Does nothing
318  * \param theMesh - the built mesh
319  * \param theShape - the geometry of interest
320  * \retval bool - always false
321  */
322 //================================================================================
323 bool HexoticPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh*   theMesh,
324                                                    const TopoDS_Shape& theShape)
325 {
326   return false;
327 }
328 //================================================================================
329 /*!
330  * \brief Initialize my parameter values by default parameters.
331  *  \retval bool - true if parameter values have been successfully defined
332  */
333 //================================================================================
334
335 bool HexoticPlugin_Hypothesis::SetParametersByDefaults(const TDefaults&  /*dflts*/,
336                                                        const SMESH_Mesh* /*theMesh*/)
337 {
338   return false;
339 }
340
341 //=============================================================================
342 int HexoticPlugin_Hypothesis::GetDefaultHexesMinLevel()
343 {
344   return 6;
345 }
346
347 int HexoticPlugin_Hypothesis::GetDefaultHexesMaxLevel()
348 {
349   return 10;
350 }
351
352 double HexoticPlugin_Hypothesis::GetDefaultMinSize()
353 {
354   return 0.0;
355 }
356
357 double HexoticPlugin_Hypothesis::GetDefaultMaxSize()
358 {
359   return 0.0;
360 }
361
362 bool HexoticPlugin_Hypothesis::GetDefaultHexoticIgnoreRidges()
363 {
364   return false;
365 }
366
367 bool HexoticPlugin_Hypothesis::GetDefaultHexoticInvalidElements()
368 {
369   return false;
370 }
371
372 double HexoticPlugin_Hypothesis::GetDefaultHexoticSharpAngleThreshold()
373 {
374   return 60.0;
375 }
376
377 int HexoticPlugin_Hypothesis::GetDefaultHexoticNbProc()
378 {
379   return 4;
380 }
381
382 std::string HexoticPlugin_Hypothesis::GetDefaultHexoticWorkingDirectory()
383 {
384   std::string aTmpDir;
385
386   char *Tmp_dir = getenv("SALOME_TMP_DIR");
387 #ifdef WIN32
388   if(Tmp_dir == NULL) {
389     Tmp_dir = getenv("TEMP");
390     if( Tmp_dir== NULL )
391       Tmp_dir = getenv("TMP");
392   }
393 #endif
394   if( Tmp_dir != NULL ) {
395     aTmpDir = std::string(Tmp_dir);
396 #ifdef WIN32
397     if(aTmpDir[aTmpDir.size()-1] != '\\') aTmpDir+='\\';
398 #else
399     if(aTmpDir[aTmpDir.size()-1] != '/') aTmpDir+='/';
400 #endif
401   }
402   else {
403 #ifdef WIN32
404     aTmpDir = "C:\\";
405 #else
406     aTmpDir = "/tmp/";
407 #endif
408   }
409   return aTmpDir;
410 }
411
412 int HexoticPlugin_Hypothesis::GetDefaultHexoticSdMode()
413 {
414   return 4;
415 }
416
417 int HexoticPlugin_Hypothesis::GetDefaultHexoticVerbosity()
418 {
419   return 1;
420 }
421
422 int HexoticPlugin_Hypothesis::GetDefaultHexoticMaxMemory()
423 {
424   return 2048;
425 }
426
427 HexoticPlugin_Hypothesis::THexoticSizeMaps HexoticPlugin_Hypothesis::GetDefaultHexoticSizeMaps()
428 {
429   return THexoticSizeMaps();
430 }
431
432