Salome HOME
Copyrights update 2015.
[plugins/netgenplugin.git] / src / NETGENPlugin / NETGENPlugin_Hypothesis.cxx
1 // Copyright (C) 2007-2015  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, or (at your option) any later version.
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     _surfaceCurvature(GetDefaultSurfaceCurvature()),
57     _fuseEdges       (GetDefaultFuseEdges())
58 {
59   _name = "NETGEN_Parameters";
60   _param_algo_dim = 3;
61   _localSize.clear();
62 }
63
64 //=============================================================================
65 /*!
66  *  
67  */
68 //=============================================================================
69 void NETGENPlugin_Hypothesis::SetMaxSize(double theSize)
70 {
71   if (theSize != _maxSize)
72   {
73     _maxSize = theSize;
74     NotifySubMeshesHypothesisModification();
75   }
76 }
77
78 //=============================================================================
79 /*!
80  *  
81  */
82 //=============================================================================
83 void NETGENPlugin_Hypothesis::SetMinSize(double theSize)
84 {
85   if (theSize != _minSize)
86   {
87     _minSize = theSize;
88     NotifySubMeshesHypothesisModification();
89   }
90 }
91
92 //=============================================================================
93 /*!
94  *  
95  */
96 //=============================================================================
97 void NETGENPlugin_Hypothesis::SetSecondOrder(bool theVal)
98 {
99   if (theVal != _secondOrder)
100   {
101     _secondOrder = theVal;
102     NotifySubMeshesHypothesisModification();
103   }
104 }
105
106 //=============================================================================
107 /*!
108  *  
109  */
110 //=============================================================================
111 void NETGENPlugin_Hypothesis::SetOptimize(bool theVal)
112 {
113   if (theVal != _optimize)
114   {
115     _optimize = theVal;
116     NotifySubMeshesHypothesisModification();
117   }
118 }
119
120 //=============================================================================
121 /*!
122  *  
123  */
124 //=============================================================================
125 void NETGENPlugin_Hypothesis::SetFineness(Fineness theFineness)
126 {
127   if (theFineness != _fineness)
128   {
129     _fineness = theFineness;
130     // the predefined values are taken from NETGEN 4.5 sources
131     switch (_fineness)
132     {
133     case VeryCoarse:
134       _growthRate = 0.7;
135       _nbSegPerEdge = 0.3;
136       _nbSegPerRadius = 1;
137       break;
138     case Coarse:
139       _growthRate = 0.5;
140       _nbSegPerEdge = 0.5;
141       _nbSegPerRadius = 1.5;
142       break;
143     case Fine:
144       _growthRate = 0.2;
145       _nbSegPerEdge = 2;
146       _nbSegPerRadius = 3;
147       break;
148     case VeryFine:
149       _growthRate = 0.1;
150       _nbSegPerEdge = 3;
151       _nbSegPerRadius = 5;
152       break;
153     case UserDefined:
154       break;
155     case Moderate:
156     default:
157       _growthRate = 0.3;
158       _nbSegPerEdge = 1;
159       _nbSegPerRadius = 2;
160       break;
161     }
162     NotifySubMeshesHypothesisModification();
163   }
164 }
165
166 //=============================================================================
167 /*!
168  *  
169  */
170 //=============================================================================
171 void NETGENPlugin_Hypothesis::SetGrowthRate(double theRate)
172 {
173   if (theRate != _growthRate)
174   {
175     _growthRate = theRate;
176     _fineness = UserDefined;
177     NotifySubMeshesHypothesisModification();
178   }
179 }
180
181 //=============================================================================
182 /*!
183  *  
184  */
185 //=============================================================================
186 void NETGENPlugin_Hypothesis::SetNbSegPerEdge(double theVal)
187 {
188   if (theVal != _nbSegPerEdge)
189   {
190     _nbSegPerEdge = theVal;
191     _fineness = UserDefined;
192     NotifySubMeshesHypothesisModification();
193   }
194 }
195
196 //=============================================================================
197 /*!
198  *  
199  */
200 //=============================================================================
201 void NETGENPlugin_Hypothesis::SetNbSegPerRadius(double theVal)
202 {
203   if (theVal != _nbSegPerRadius)
204   {
205     _nbSegPerRadius = theVal;
206     _fineness = UserDefined;
207     NotifySubMeshesHypothesisModification();
208   }
209 }
210
211 //=============================================================================
212 /*!
213  *  
214  */
215 //=============================================================================
216 void NETGENPlugin_Hypothesis::SetLocalSizeOnEntry(const std::string& entry, double localSize)
217 {
218   if(_localSize[entry] != localSize)
219     {
220       _localSize[entry] = localSize;
221       NotifySubMeshesHypothesisModification();
222     }
223 }
224
225 //=============================================================================
226 /*!
227  *  
228  */
229 //=============================================================================
230 double NETGENPlugin_Hypothesis::GetLocalSizeOnEntry(const std::string& entry)
231 {
232   TLocalSize::iterator it  = _localSize.find( entry );
233   if ( it != _localSize.end() )
234     return it->second;
235   else
236     return -1.0;
237 }
238
239 //=============================================================================
240 /*!
241  *  
242  */
243 //=============================================================================
244 void NETGENPlugin_Hypothesis::UnsetLocalSizeOnEntry(const std::string& entry)
245 {
246   _localSize.erase(entry);
247   NotifySubMeshesHypothesisModification();
248 }
249
250 //=============================================================================
251 /*!
252  *  
253  */
254 //=============================================================================
255 void NETGENPlugin_Hypothesis::SetQuadAllowed(bool theVal)
256 {
257   if (theVal != _quadAllowed)
258   {
259     _quadAllowed = theVal;
260     NotifySubMeshesHypothesisModification();
261   }
262 }
263
264 //=============================================================================
265 /*!
266  *  
267  */
268 //=============================================================================
269 bool NETGENPlugin_Hypothesis::GetDefaultQuadAllowed()
270 {
271   return false;
272 }
273
274 //=============================================================================
275 /*!
276  *  
277  */
278 //=============================================================================
279 void NETGENPlugin_Hypothesis::SetSurfaceCurvature(bool theVal)
280 {
281   if (theVal != _surfaceCurvature)
282   {
283     _surfaceCurvature = theVal;
284     NotifySubMeshesHypothesisModification();
285   }
286 }
287
288 //=============================================================================
289 /*!
290  *
291  */
292 //=============================================================================
293 bool NETGENPlugin_Hypothesis::GetDefaultSurfaceCurvature()
294 {
295   return true;
296 }
297
298 //=============================================================================
299 /*!
300  *
301  */
302 //=============================================================================
303 void NETGENPlugin_Hypothesis::SetFuseEdges(bool theVal)
304 {
305   if (theVal != _fuseEdges)
306   {
307     _fuseEdges = theVal;
308     NotifySubMeshesHypothesisModification();
309   }
310 }
311
312 //=============================================================================
313 /*!
314  *
315  */
316 //=============================================================================
317 bool NETGENPlugin_Hypothesis::GetDefaultFuseEdges()
318 {
319   return true; // false; -- for SALOME_TESTS/Grids/smesh/3D_mesh_NETGEN_05/F6
320 }
321
322 //=============================================================================
323 /*!
324  *
325  */
326 //=============================================================================
327 ostream & NETGENPlugin_Hypothesis::SaveTo(ostream & save)
328 {
329   save << _maxSize << " " << _fineness;
330
331   if (_fineness == UserDefined)
332     save << " " << _growthRate << " " << _nbSegPerEdge << " " << _nbSegPerRadius;
333
334   save << " " << (int)_secondOrder << " " << (int)_optimize;
335
336   TLocalSize::iterator it_sm  = _localSize.begin();
337   if (it_sm != _localSize.end()) {
338     save << " " << "__LOCALSIZE_BEGIN__";
339     for ( ; it_sm != _localSize.end(); ++it_sm ) {
340         save << " " << it_sm->first
341              << " " << it_sm->second << "%#"; // "%#" is a mark of value end
342     }
343     save << " " << "__LOCALSIZE_END__";
344   }
345   save << " " << _minSize;
346   save << " " << _quadAllowed;
347   save << " " << _surfaceCurvature;
348   save << " " << _fuseEdges;
349
350   return save;
351 }
352
353 //=============================================================================
354 /*!
355  *  
356  */
357 //=============================================================================
358 istream & NETGENPlugin_Hypothesis::LoadFrom(istream & load)
359 {
360   bool isOK = true;
361   int is;
362   double val;
363
364   isOK = (load >> val);
365   if (isOK)
366     _maxSize = val;
367   else
368     load.clear(ios::badbit | load.rdstate());
369
370   isOK = (load >> is);
371   if (isOK)
372     SetFineness((Fineness) is);
373   else
374     load.clear(ios::badbit | load.rdstate());
375
376   if (_fineness == UserDefined)
377   {
378     isOK = (load >> val);
379     if (isOK)
380       _growthRate = val;
381     else
382       load.clear(ios::badbit | load.rdstate());
383
384     isOK = (load >> val);
385     if (isOK)
386       _nbSegPerEdge = val;
387     else
388       load.clear(ios::badbit | load.rdstate());
389
390     isOK = (load >> val);
391     if (isOK)
392       _nbSegPerRadius = val;
393     else
394       load.clear(ios::badbit | load.rdstate());
395   }
396
397   isOK = (load >> is);
398   if (isOK)
399     _secondOrder = (bool) is;
400   else
401     load.clear(ios::badbit | load.rdstate());
402
403   isOK = (load >> is);
404   if (isOK)
405     _optimize = (bool) is;
406   else
407     load.clear(ios::badbit | load.rdstate());
408
409   std::string option_or_sm;
410   bool hasLocalSize = false;
411
412   isOK = (load >> option_or_sm);
413   if (isOK)
414     if (option_or_sm == "__LOCALSIZE_BEGIN__")
415       hasLocalSize = true;
416
417   std::string smEntry, smValue;
418   while (isOK && hasLocalSize) {
419     isOK = (load >> smEntry);
420     if (isOK) {
421       if (smEntry == "__LOCALSIZE_END__")
422         break;
423       isOK = (load >> smValue);
424     }
425     if (isOK) {
426       std::istringstream tmp(smValue);
427       double val;
428       tmp >> val;
429       _localSize[ smEntry ] = val;
430     }
431   }
432
433   if ( !hasLocalSize && !option_or_sm.empty() )
434     _minSize = atof( option_or_sm.c_str() );
435
436   isOK = ( load >> _quadAllowed );
437   if ( !isOK )
438     _quadAllowed = GetDefaultQuadAllowed();
439
440   isOK = ( load >> _surfaceCurvature );
441   if ( !isOK )
442     _surfaceCurvature = GetDefaultSurfaceCurvature();
443
444   isOK = ( load >> _fuseEdges );
445   if ( !isOK )
446     _fuseEdges = GetDefaultFuseEdges();
447
448   return load;
449 }
450
451 //=============================================================================
452 /*!
453  *  
454  */
455 //=============================================================================
456 ostream & operator <<(ostream & save, NETGENPlugin_Hypothesis & hyp)
457 {
458   return hyp.SaveTo( save );
459 }
460
461 //=============================================================================
462 /*!
463  *  
464  */
465 //=============================================================================
466 istream & operator >>(istream & load, NETGENPlugin_Hypothesis & hyp)
467 {
468   return hyp.LoadFrom( load );
469 }
470
471
472 //================================================================================
473 /*!
474  * \brief Does nothing
475  * \param theMesh - the built mesh
476  * \param theShape - the geometry of interest
477  * \retval bool - always false
478  */
479 //================================================================================
480 bool NETGENPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh*   theMesh,
481                                                   const TopoDS_Shape& theShape)
482 {
483   return false;
484 }
485
486 //================================================================================
487 /*!
488  * \brief Initialize my parameter values by default parameters.
489  *  \retval bool - true if parameter values have been successfully defined
490  */
491 //================================================================================
492
493 bool NETGENPlugin_Hypothesis::SetParametersByDefaults(const TDefaults&  dflts,
494                                                       const SMESH_Mesh* theMesh)
495 {
496   _nbSegPerEdge = dflts._nbSegments;
497   _maxSize      = dflts._elemLength;
498
499   if ( dflts._shape && !dflts._shape->IsNull() )
500     _minSize    = NETGENPlugin_Mesher::GetDefaultMinSize( *dflts._shape, _maxSize );
501   else if ( theMesh && theMesh->HasShapeToMesh() )
502     _minSize    = NETGENPlugin_Mesher::GetDefaultMinSize( theMesh->GetShapeToMesh(), _maxSize );
503
504   return _nbSegPerEdge && _maxSize > 0;
505 }
506
507 //=============================================================================
508 /*!
509  *  
510  */
511 //=============================================================================
512 double NETGENPlugin_Hypothesis::GetDefaultMaxSize()
513 {
514   return 1000;
515 }
516
517 //=============================================================================
518 /*!
519  *  
520  */
521 //=============================================================================
522 NETGENPlugin_Hypothesis::Fineness NETGENPlugin_Hypothesis::GetDefaultFineness()
523 {
524   return Moderate;
525 }
526
527 //=============================================================================
528 /*!
529  *  
530  */
531 //=============================================================================
532 double NETGENPlugin_Hypothesis::GetDefaultGrowthRate()
533 {
534   return 0.3;
535 }
536
537 //=============================================================================
538 /*!
539  *  
540  */
541 //=============================================================================
542 double NETGENPlugin_Hypothesis::GetDefaultNbSegPerEdge()
543 {
544   return 1;
545 }
546
547 //=============================================================================
548 /*!
549  *  
550  */
551 //=============================================================================
552 double NETGENPlugin_Hypothesis::GetDefaultNbSegPerRadius()
553 {
554   return 2;
555 }
556
557 //=============================================================================
558 /*!
559  *  
560  */
561 //=============================================================================
562 bool NETGENPlugin_Hypothesis::GetDefaultSecondOrder()
563 {
564   return false;
565 }
566
567 //=============================================================================
568 /*!
569  *  
570  */
571 //=============================================================================
572 bool NETGENPlugin_Hypothesis::GetDefaultOptimize()
573 {
574   return true;
575 }