Salome HOME
Merge from V6_main 01/04/2013
[plugins/netgenplugin.git] / src / NETGENPlugin / NETGENPlugin_Hypothesis.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  NETGENPlugin : C++ implementation
24 // File      : NETGENPlugin_Hypothesis.cxx
25 // Author    : Michael Sazonov (OCN)
26 // Date      : 28/03/2006
27 // Project   : SALOME
28 //
29 #include "NETGENPlugin_Hypothesis.hxx"
30
31 #include "NETGENPlugin_Mesher.hxx"
32 #include "SMESH_Mesh.hxx"
33
34 #include <utilities.h>
35
36 using namespace std;
37
38 //=============================================================================
39 /*!
40  *  
41  */
42 //=============================================================================
43 NETGENPlugin_Hypothesis::NETGENPlugin_Hypothesis (int hypId, int studyId,
44                                                   SMESH_Gen * gen)
45   : SMESH_Hypothesis(hypId, studyId, gen),
46     _maxSize       (GetDefaultMaxSize()),
47     _minSize       (0),
48     _growthRate    (GetDefaultGrowthRate()),
49     _nbSegPerEdge  (GetDefaultNbSegPerEdge()),
50     _nbSegPerRadius(GetDefaultNbSegPerRadius()),
51     _fineness      (GetDefaultFineness()),
52     _secondOrder   (GetDefaultSecondOrder()),
53     _optimize      (GetDefaultOptimize()),
54     _localSize     (GetDefaultLocalSize()),
55     _quadAllowed   (GetDefaultQuadAllowed())
56 {
57   _name = "NETGEN_Parameters";
58   _param_algo_dim = 3;
59   _localSize.clear();
60 }
61
62 //=============================================================================
63 /*!
64  *  
65  */
66 //=============================================================================
67 void NETGENPlugin_Hypothesis::SetMaxSize(double theSize)
68 {
69   if (theSize != _maxSize)
70   {
71     _maxSize = theSize;
72     NotifySubMeshesHypothesisModification();
73   }
74 }
75
76 //=============================================================================
77 /*!
78  *  
79  */
80 //=============================================================================
81 void NETGENPlugin_Hypothesis::SetMinSize(double theSize)
82 {
83   if (theSize != _minSize)
84   {
85     _minSize = theSize;
86     NotifySubMeshesHypothesisModification();
87   }
88 }
89
90 //=============================================================================
91 /*!
92  *  
93  */
94 //=============================================================================
95 void NETGENPlugin_Hypothesis::SetSecondOrder(bool theVal)
96 {
97   if (theVal != _secondOrder)
98   {
99     _secondOrder = theVal;
100     NotifySubMeshesHypothesisModification();
101   }
102 }
103
104 //=============================================================================
105 /*!
106  *  
107  */
108 //=============================================================================
109 void NETGENPlugin_Hypothesis::SetOptimize(bool theVal)
110 {
111   if (theVal != _optimize)
112   {
113     _optimize = theVal;
114     NotifySubMeshesHypothesisModification();
115   }
116 }
117
118 //=============================================================================
119 /*!
120  *  
121  */
122 //=============================================================================
123 void NETGENPlugin_Hypothesis::SetFineness(Fineness theFineness)
124 {
125   if (theFineness != _fineness)
126   {
127     _fineness = theFineness;
128     // the predefined values are taken from NETGEN 4.5 sources
129     switch (_fineness)
130     {
131     case VeryCoarse:
132       _growthRate = 0.7;
133       _nbSegPerEdge = 0.3;
134       _nbSegPerRadius = 1;
135       break;
136     case Coarse:
137       _growthRate = 0.5;
138       _nbSegPerEdge = 0.5;
139       _nbSegPerRadius = 1.5;
140       break;
141     case Fine:
142       _growthRate = 0.2;
143       _nbSegPerEdge = 2;
144       _nbSegPerRadius = 3;
145       break;
146     case VeryFine:
147       _growthRate = 0.1;
148       _nbSegPerEdge = 3;
149       _nbSegPerRadius = 5;
150       break;
151     case UserDefined:
152       break;
153     case Moderate:
154     default:
155       _growthRate = 0.3;
156       _nbSegPerEdge = 1;
157       _nbSegPerRadius = 2;
158       break;
159     }
160     NotifySubMeshesHypothesisModification();
161   }
162 }
163
164 //=============================================================================
165 /*!
166  *  
167  */
168 //=============================================================================
169 void NETGENPlugin_Hypothesis::SetGrowthRate(double theRate)
170 {
171   if (theRate != _growthRate)
172   {
173     _growthRate = theRate;
174     _fineness = UserDefined;
175     NotifySubMeshesHypothesisModification();
176   }
177 }
178
179 //=============================================================================
180 /*!
181  *  
182  */
183 //=============================================================================
184 void NETGENPlugin_Hypothesis::SetNbSegPerEdge(double theVal)
185 {
186   if (theVal != _nbSegPerEdge)
187   {
188     _nbSegPerEdge = theVal;
189     _fineness = UserDefined;
190     NotifySubMeshesHypothesisModification();
191   }
192 }
193
194 //=============================================================================
195 /*!
196  *  
197  */
198 //=============================================================================
199 void NETGENPlugin_Hypothesis::SetNbSegPerRadius(double theVal)
200 {
201   if (theVal != _nbSegPerRadius)
202   {
203     _nbSegPerRadius = theVal;
204     _fineness = UserDefined;
205     NotifySubMeshesHypothesisModification();
206   }
207 }
208
209 //=============================================================================
210 /*!
211  *  
212  */
213 //=============================================================================
214 void NETGENPlugin_Hypothesis::SetLocalSizeOnEntry(const std::string& entry, double localSize)
215 {
216   if(_localSize[entry] != localSize)
217     {
218       _localSize[entry] = localSize;
219       NotifySubMeshesHypothesisModification();
220     }
221 }
222
223 //=============================================================================
224 /*!
225  *  
226  */
227 //=============================================================================
228 double NETGENPlugin_Hypothesis::GetLocalSizeOnEntry(const std::string& entry)
229 {
230   TLocalSize::iterator it  = _localSize.find( entry );
231   if ( it != _localSize.end() )
232     return it->second;
233   else
234     return -1.0;
235 }
236
237 //=============================================================================
238 /*!
239  *  
240  */
241 //=============================================================================
242 void NETGENPlugin_Hypothesis::UnsetLocalSizeOnEntry(const std::string& entry)
243 {
244   _localSize.erase(entry);
245   NotifySubMeshesHypothesisModification();
246 }
247
248 //=============================================================================
249 /*!
250  *  
251  */
252 //=============================================================================
253 void NETGENPlugin_Hypothesis::SetQuadAllowed(bool theVal)
254 {
255   if (theVal != _quadAllowed)
256   {
257     _quadAllowed = theVal;
258     NotifySubMeshesHypothesisModification();
259   }
260 }
261
262 //=============================================================================
263 /*!
264  *  
265  */
266 //=============================================================================
267 bool NETGENPlugin_Hypothesis::GetDefaultQuadAllowed()
268 {
269   return false;
270 }
271
272 //=============================================================================
273 /*!
274  *  
275  */
276 //=============================================================================
277 ostream & NETGENPlugin_Hypothesis::SaveTo(ostream & save)
278 {
279   save << _maxSize << " " << _fineness;
280
281   if (_fineness == UserDefined)
282     save << " " << _growthRate << " " << _nbSegPerEdge << " " << _nbSegPerRadius;
283
284   save << " " << (int)_secondOrder << " " << (int)_optimize;
285
286   TLocalSize::iterator it_sm  = _localSize.begin();
287   if (it_sm != _localSize.end()) {
288     save << " " << "__LOCALSIZE_BEGIN__";
289     for ( ; it_sm != _localSize.end(); ++it_sm ) {
290         save << " " << it_sm->first
291              << " " << it_sm->second << "%#"; // "%#" is a mark of value end
292     }
293     save << " " << "__LOCALSIZE_END__";
294   }
295   save << " " << _minSize;
296   save << " " << _quadAllowed;
297
298   return save;
299 }
300
301 //=============================================================================
302 /*!
303  *  
304  */
305 //=============================================================================
306 istream & NETGENPlugin_Hypothesis::LoadFrom(istream & load)
307 {
308   bool isOK = true;
309   int is;
310   double val;
311
312   isOK = (load >> val);
313   if (isOK)
314     _maxSize = val;
315   else
316     load.clear(ios::badbit | load.rdstate());
317
318   isOK = (load >> is);
319   if (isOK)
320     SetFineness((Fineness) is);
321   else
322     load.clear(ios::badbit | load.rdstate());
323
324   if (_fineness == UserDefined)
325   {
326     isOK = (load >> val);
327     if (isOK)
328       _growthRate = val;
329     else
330       load.clear(ios::badbit | load.rdstate());
331
332     isOK = (load >> val);
333     if (isOK)
334       _nbSegPerEdge = val;
335     else
336       load.clear(ios::badbit | load.rdstate());
337
338     isOK = (load >> val);
339     if (isOK)
340       _nbSegPerRadius = val;
341     else
342       load.clear(ios::badbit | load.rdstate());
343   }
344
345   isOK = (load >> is);
346   if (isOK)
347     _secondOrder = (bool) is;
348   else
349     load.clear(ios::badbit | load.rdstate());
350
351   isOK = (load >> is);
352   if (isOK)
353     _optimize = (bool) is;
354   else
355     load.clear(ios::badbit | load.rdstate());
356
357   std::string option_or_sm;
358   bool hasLocalSize = false;
359
360   isOK = (load >> option_or_sm);
361   if (isOK)
362     if (option_or_sm == "__LOCALSIZE_BEGIN__")
363       hasLocalSize = true;
364
365   std::string smEntry, smValue;
366   while (isOK && hasLocalSize) {
367     isOK = (load >> smEntry);
368     if (isOK) {
369       if (smEntry == "__LOCALSIZE_END__")
370         break;
371       isOK = (load >> smValue);
372     }
373     if (isOK) {
374       std::istringstream tmp(smValue);
375       double val;
376       tmp >> val;
377       _localSize[ smEntry ] = val;
378     }
379   }
380
381   if ( !hasLocalSize && !option_or_sm.empty() )
382     _minSize = atof( option_or_sm.c_str() );
383
384   isOK = ( load >> _quadAllowed );
385   if ( !isOK )
386     _quadAllowed = GetDefaultQuadAllowed();
387
388   return load;
389 }
390
391 //=============================================================================
392 /*!
393  *  
394  */
395 //=============================================================================
396 ostream & operator <<(ostream & save, NETGENPlugin_Hypothesis & hyp)
397 {
398   return hyp.SaveTo( save );
399 }
400
401 //=============================================================================
402 /*!
403  *  
404  */
405 //=============================================================================
406 istream & operator >>(istream & load, NETGENPlugin_Hypothesis & hyp)
407 {
408   return hyp.LoadFrom( load );
409 }
410
411
412 //================================================================================
413 /*!
414  * \brief Does nothing
415  * \param theMesh - the built mesh
416  * \param theShape - the geometry of interest
417  * \retval bool - always false
418  */
419 //================================================================================
420 bool NETGENPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh*   theMesh,
421                                                   const TopoDS_Shape& theShape)
422 {
423   return false;
424 }
425
426 //================================================================================
427 /*!
428  * \brief Initialize my parameter values by default parameters.
429  *  \retval bool - true if parameter values have been successfully defined
430  */
431 //================================================================================
432
433 bool NETGENPlugin_Hypothesis::SetParametersByDefaults(const TDefaults&  dflts,
434                                                       const SMESH_Mesh* theMesh)
435 {
436   _nbSegPerEdge = dflts._nbSegments;
437   _maxSize      = dflts._elemLength;
438
439   if ( dflts._shape && !dflts._shape->IsNull() )
440     _minSize    = NETGENPlugin_Mesher::GetDefaultMinSize( *dflts._shape, _maxSize );
441   else if ( theMesh && theMesh->HasShapeToMesh() )
442     _minSize    = NETGENPlugin_Mesher::GetDefaultMinSize( theMesh->GetShapeToMesh(), _maxSize );
443
444   return _nbSegPerEdge && _maxSize > 0;
445 }
446
447 //=============================================================================
448 /*!
449  *  
450  */
451 //=============================================================================
452 double NETGENPlugin_Hypothesis::GetDefaultMaxSize()
453 {
454   return 1000;
455 }
456
457 //=============================================================================
458 /*!
459  *  
460  */
461 //=============================================================================
462 NETGENPlugin_Hypothesis::Fineness NETGENPlugin_Hypothesis::GetDefaultFineness()
463 {
464   return Moderate;
465 }
466
467 //=============================================================================
468 /*!
469  *  
470  */
471 //=============================================================================
472 double NETGENPlugin_Hypothesis::GetDefaultGrowthRate()
473 {
474   return 0.3;
475 }
476
477 //=============================================================================
478 /*!
479  *  
480  */
481 //=============================================================================
482 double NETGENPlugin_Hypothesis::GetDefaultNbSegPerEdge()
483 {
484   return 1;
485 }
486
487 //=============================================================================
488 /*!
489  *  
490  */
491 //=============================================================================
492 double NETGENPlugin_Hypothesis::GetDefaultNbSegPerRadius()
493 {
494   return 2;
495 }
496
497 //=============================================================================
498 /*!
499  *  
500  */
501 //=============================================================================
502 bool NETGENPlugin_Hypothesis::GetDefaultSecondOrder()
503 {
504   return false;
505 }
506
507 //=============================================================================
508 /*!
509  *  
510  */
511 //=============================================================================
512 bool NETGENPlugin_Hypothesis::GetDefaultOptimize()
513 {
514   return true;
515 }