Salome HOME
#18963 Minimize compiler warnings
[plugins/netgenplugin.git] / src / NETGENPlugin / NETGENPlugin_Hypothesis.cxx
1 // Copyright (C) 2007-2020  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, SMESH_Gen * gen)
44
45   : SMESH_Hypothesis(hypId, gen),
46     _fineness           (GetDefaultFineness()),
47     _secondOrder        (GetDefaultSecondOrder()),
48     _quadAllowed        (GetDefaultQuadAllowed()),
49     _maxSize            (GetDefaultMaxSize()),
50     _minSize            (0),
51     _growthRate         (GetDefaultGrowthRate()),
52     _nbSegPerRadius     (GetDefaultNbSegPerRadius()),
53     _nbSegPerEdge       (GetDefaultNbSegPerEdge()),
54     _chordalErrorEnabled(GetDefaultChordalError() > 0),
55     _chordalError       (GetDefaultChordalError() ),
56     _optimize           (GetDefaultOptimize()),
57     _nbSurfOptSteps     (GetDefaultNbSurfOptSteps()),
58     _nbVolOptSteps      (GetDefaultNbVolOptSteps()),
59     _elemSizeWeight     (GetDefaultElemSizeWeight()),
60     _worstElemMeasure   (GetDefaultWorstElemMeasure()),
61     _surfaceCurvature   (GetDefaultSurfaceCurvature()),
62     _useDelauney        (GetDefaultUseDelauney()),
63     _checkOverlapping   (GetDefaultCheckOverlapping()),
64     _checkChartBoundary (GetDefaultCheckChartBoundary()),
65     _fuseEdges          (GetDefaultFuseEdges())
66 {
67   _name = "NETGEN_Parameters";
68   _param_algo_dim = 3;
69 }
70
71 //=============================================================================
72 /*!
73  *
74  */
75 //=============================================================================
76 void NETGENPlugin_Hypothesis::SetMaxSize(double theSize)
77 {
78   if (theSize != _maxSize)
79   {
80     _maxSize = theSize;
81     NotifySubMeshesHypothesisModification();
82   }
83 }
84
85 //=============================================================================
86 /*!
87  *  
88  */
89 //=============================================================================
90 void NETGENPlugin_Hypothesis::SetMinSize(double theSize)
91 {
92   if (theSize != _minSize)
93   {
94     _minSize = theSize;
95     NotifySubMeshesHypothesisModification();
96   }
97 }
98
99 //=============================================================================
100 /*!
101  *  
102  */
103 //=============================================================================
104 void NETGENPlugin_Hypothesis::SetSecondOrder(bool theVal)
105 {
106   if (theVal != _secondOrder)
107   {
108     _secondOrder = theVal;
109     NotifySubMeshesHypothesisModification();
110   }
111 }
112
113 //=============================================================================
114 /*!
115  *  
116  */
117 //=============================================================================
118 void NETGENPlugin_Hypothesis::SetOptimize(bool theVal)
119 {
120   if (theVal != _optimize)
121   {
122     _optimize = theVal;
123     NotifySubMeshesHypothesisModification();
124   }
125 }
126
127 //=============================================================================
128 /*!
129  *  
130  */
131 //=============================================================================
132 void NETGENPlugin_Hypothesis::SetFineness(Fineness theFineness)
133 {
134   if (theFineness != _fineness)
135   {
136     _fineness = theFineness;
137     // the predefined values are taken from NETGEN 4.5 sources
138     switch (_fineness)
139     {
140     case VeryCoarse:
141       _growthRate = 0.7;
142       _nbSegPerEdge = 0.3;
143       _nbSegPerRadius = 1;
144       break;
145     case Coarse:
146       _growthRate = 0.5;
147       _nbSegPerEdge = 0.5;
148       _nbSegPerRadius = 1.5;
149       break;
150     case Fine:
151       _growthRate = 0.2;
152       _nbSegPerEdge = 2;
153       _nbSegPerRadius = 3;
154       break;
155     case VeryFine:
156       _growthRate = 0.1;
157       _nbSegPerEdge = 3;
158       _nbSegPerRadius = 5;
159       break;
160     case UserDefined:
161       break;
162     case Moderate:
163     default:
164       _growthRate = 0.3;
165       _nbSegPerEdge = 1;
166       _nbSegPerRadius = 2;
167       break;
168     }
169     NotifySubMeshesHypothesisModification();
170   }
171 }
172
173 //=============================================================================
174 /*!
175  *  
176  */
177 //=============================================================================
178 void NETGENPlugin_Hypothesis::SetGrowthRate(double theRate)
179 {
180   if (theRate != _growthRate)
181   {
182     _growthRate = theRate;
183     _fineness = UserDefined;
184     NotifySubMeshesHypothesisModification();
185   }
186 }
187
188 //=============================================================================
189 /*!
190  *  
191  */
192 //=============================================================================
193 void NETGENPlugin_Hypothesis::SetNbSegPerEdge(double theVal)
194 {
195   if (theVal != _nbSegPerEdge)
196   {
197     _nbSegPerEdge = theVal;
198     _fineness = UserDefined;
199     NotifySubMeshesHypothesisModification();
200   }
201 }
202
203 //=============================================================================
204 /*!
205  *  
206  */
207 //=============================================================================
208 void NETGENPlugin_Hypothesis::SetNbSegPerRadius(double theVal)
209 {
210   if (theVal != _nbSegPerRadius)
211   {
212     _nbSegPerRadius = theVal;
213     _fineness = UserDefined;
214     NotifySubMeshesHypothesisModification();
215   }
216 }
217
218 //=============================================================================
219 /*!
220  *  
221  */
222 //=============================================================================
223 void NETGENPlugin_Hypothesis::SetChordalErrorEnabled(bool theVal)
224 {
225   if (theVal != _chordalErrorEnabled)
226   {
227     _chordalErrorEnabled = theVal;
228     NotifySubMeshesHypothesisModification();
229   }
230 }
231
232 //=============================================================================
233 /*!
234  *  
235  */
236 //=============================================================================
237 void NETGENPlugin_Hypothesis::SetChordalError(double theVal)
238 {
239   if (theVal != _chordalError)
240   {
241     _chordalError = theVal;
242     NotifySubMeshesHypothesisModification();
243   }
244 }
245
246 //=============================================================================
247 /*!
248  *  
249  */
250 //=============================================================================
251 void NETGENPlugin_Hypothesis::SetLocalSizeOnEntry(const std::string& entry, double localSize)
252 {
253   if(_localSize[entry] != localSize)
254   {
255     _localSize[entry] = localSize;
256     NotifySubMeshesHypothesisModification();
257   }
258 }
259
260 //=============================================================================
261 /*!
262  *
263  */
264 //=============================================================================
265 double NETGENPlugin_Hypothesis::GetLocalSizeOnEntry(const std::string& entry)
266 {
267   TLocalSize::iterator it  = _localSize.find( entry );
268   if ( it != _localSize.end() )
269     return it->second;
270   else
271     return -1.0;
272 }
273
274 //=============================================================================
275 /*!
276  *  
277  */
278 //=============================================================================
279 void NETGENPlugin_Hypothesis::UnsetLocalSizeOnEntry(const std::string& entry)
280 {
281   _localSize.erase(entry);
282   NotifySubMeshesHypothesisModification();
283 }
284
285 //=============================================================================
286 /*!
287  *  
288  */
289 //=============================================================================
290 void NETGENPlugin_Hypothesis::SetMeshSizeFile(const std::string& fileName)
291 {
292   if ( fileName != _meshSizeFile )
293   {
294     _meshSizeFile = fileName;
295     NotifySubMeshesHypothesisModification();
296   }
297 }
298
299 //=============================================================================
300 /*!
301  *  
302  */
303 //=============================================================================
304 void NETGENPlugin_Hypothesis::SetQuadAllowed(bool theVal)
305 {
306   if (theVal != _quadAllowed)
307   {
308     _quadAllowed = theVal;
309     NotifySubMeshesHypothesisModification();
310   }
311 }
312
313 //=============================================================================
314 /*!
315  *  
316  */
317 //=============================================================================
318 void NETGENPlugin_Hypothesis::SetSurfaceCurvature(bool theVal)
319 {
320   if (theVal != _surfaceCurvature)
321   {
322     _surfaceCurvature = theVal;
323     NotifySubMeshesHypothesisModification();
324   }
325 }
326
327 //=============================================================================
328 /*!
329  *
330  */
331 //=============================================================================
332 void NETGENPlugin_Hypothesis::SetFuseEdges(bool theVal)
333 {
334   if (theVal != _fuseEdges)
335   {
336     _fuseEdges = theVal;
337     NotifySubMeshesHypothesisModification();
338   }
339 }
340
341 //=======================================================================
342 //function : SetNbSurfOptSteps
343 //purpose  : 
344 //=======================================================================
345
346 void NETGENPlugin_Hypothesis::SetNbSurfOptSteps( int theVal )
347 {
348   if (theVal != _nbSurfOptSteps)
349   {
350     _nbSurfOptSteps = theVal;
351     NotifySubMeshesHypothesisModification();
352   }
353 }
354
355 //=======================================================================
356 //function : SetNbVolOptSteps
357 //purpose  : 
358 //=======================================================================
359
360 void NETGENPlugin_Hypothesis::SetNbVolOptSteps( int theVal )
361 {
362   if (theVal != _nbVolOptSteps)
363   {
364     _nbVolOptSteps = theVal;
365     NotifySubMeshesHypothesisModification();
366   }
367 }
368
369 //=======================================================================
370 //function : SetElemSizeWeight
371 //purpose  : 
372 //=======================================================================
373
374 void NETGENPlugin_Hypothesis::SetElemSizeWeight( double theVal )
375 {
376   if (theVal != _elemSizeWeight)
377   {
378     _elemSizeWeight = theVal;
379     NotifySubMeshesHypothesisModification();
380   }
381 }
382
383 //=======================================================================
384 //function : SetWorstElemMeasure
385 //purpose  : 
386 //=======================================================================
387
388 void NETGENPlugin_Hypothesis::SetWorstElemMeasure( int theVal )
389 {
390   if (theVal != _worstElemMeasure)
391   {
392     _worstElemMeasure = theVal;
393     NotifySubMeshesHypothesisModification();
394   }
395 }
396
397 //=======================================================================
398 //function : SetUseDelauney
399 //purpose  : 
400 //=======================================================================
401
402 void NETGENPlugin_Hypothesis::SetUseDelauney( bool theVal )
403 {
404   if (theVal != _useDelauney )
405   {
406     _useDelauney = theVal;
407     NotifySubMeshesHypothesisModification();
408   }
409 }
410
411 //=======================================================================
412 //function : SetCheckOverlapping
413 //purpose  : 
414 //=======================================================================
415
416 void NETGENPlugin_Hypothesis::SetCheckOverlapping( bool theVal )
417 {
418   if (theVal != _checkOverlapping )
419   {
420     _checkOverlapping = theVal;
421     NotifySubMeshesHypothesisModification();
422   }
423 }
424
425 //=======================================================================
426 //function : SetCheckChartBoundary
427 //purpose  : 
428 //=======================================================================
429
430 void NETGENPlugin_Hypothesis::SetCheckChartBoundary( bool theVal )
431 {
432   if (theVal != _checkChartBoundary)
433   {
434     _checkChartBoundary = theVal;
435     NotifySubMeshesHypothesisModification();
436   }
437 }
438
439 //=============================================================================
440 /*!
441  *
442  */
443 //=============================================================================
444 ostream & NETGENPlugin_Hypothesis::SaveTo(ostream & save)
445 {
446   save << _maxSize << " " << _fineness;
447
448   if (_fineness == UserDefined)
449     save << " " << _growthRate << " " << _nbSegPerEdge << " " << _nbSegPerRadius;
450
451   save << " " << (int)_secondOrder << " " << (int)_optimize;
452
453   TLocalSize::iterator it_sm  = _localSize.begin();
454   if (it_sm != _localSize.end()) {
455     save << " " << "__LOCALSIZE_BEGIN__";
456     for ( ; it_sm != _localSize.end(); ++it_sm ) {
457       save << " " << it_sm->first
458            << " " << it_sm->second << "%#"; // "%#" is a mark of value end
459     }
460     save << " " << "__LOCALSIZE_END__";
461   }
462   save << " " << _minSize;
463   save << " " << _quadAllowed;
464   save << " " << _surfaceCurvature;
465   save << " " << _fuseEdges;
466
467   save << " " << _meshSizeFile.size() << " " << _meshSizeFile;
468
469   save << " " << ( _chordalErrorEnabled ? _chordalError : 0. );
470
471
472   // added for option set completion
473
474   save << " " << _nbSurfOptSteps;
475   save << " " << _nbVolOptSteps;
476   save << " " << _elemSizeWeight;
477   save << " " << _worstElemMeasure;
478
479   save << " " << _useDelauney;
480   save << " " << _checkOverlapping;
481   save << " " << _checkChartBoundary;
482
483   return save;
484 }
485
486 //=============================================================================
487 /*!
488  *  
489  */
490 //=============================================================================
491 istream & NETGENPlugin_Hypothesis::LoadFrom(istream & load)
492 {
493   bool isOK = true;
494   int is;
495   double val;
496
497   isOK = static_cast<bool>(load >> val);
498   if (isOK)
499     _maxSize = val;
500   else
501     load.clear(ios::badbit | load.rdstate());
502
503   isOK = static_cast<bool>(load >> is);
504   if (isOK)
505     SetFineness((Fineness) is);
506   else
507     load.clear(ios::badbit | load.rdstate());
508
509   if (_fineness == UserDefined)
510   {
511     isOK = static_cast<bool>(load >> val);
512     if (isOK)
513       _growthRate = val;
514     else
515       load.clear(ios::badbit | load.rdstate());
516
517     isOK = static_cast<bool>(load >> val);
518     if (isOK)
519       _nbSegPerEdge = val;
520     else
521       load.clear(ios::badbit | load.rdstate());
522
523     isOK = static_cast<bool>(load >> val);
524     if (isOK)
525       _nbSegPerRadius = val;
526     else
527       load.clear(ios::badbit | load.rdstate());
528   }
529
530   isOK = static_cast<bool>(load >> is);
531   if (isOK)
532     _secondOrder = (bool) is;
533   else
534     load.clear(ios::badbit | load.rdstate());
535
536   isOK = static_cast<bool>(load >> is);
537   if (isOK)
538     _optimize = (bool) is;
539   else
540     load.clear(ios::badbit | load.rdstate());
541
542   std::string option_or_sm;
543   bool hasLocalSize = false;
544
545   isOK = static_cast<bool>(load >> option_or_sm);
546   if (isOK)
547     if (option_or_sm == "__LOCALSIZE_BEGIN__")
548       hasLocalSize = true;
549
550   std::string smEntry, smValue;
551   while (isOK && hasLocalSize) {
552     isOK = static_cast<bool>(load >> smEntry);
553     if (isOK) {
554       if (smEntry == "__LOCALSIZE_END__")
555         break;
556       isOK = static_cast<bool>(load >> smValue);
557     }
558     if (isOK) {
559       std::istringstream tmp(smValue);
560       double val;
561       tmp >> val;
562       _localSize[ smEntry ] = val;
563     }
564   }
565
566   if ( !hasLocalSize && !option_or_sm.empty() )
567     _minSize = atof( option_or_sm.c_str() );
568   else
569     load >> _minSize;
570
571   isOK = static_cast<bool>( load >> is );
572   if ( isOK )
573     _quadAllowed = (bool) is;
574   else
575     _quadAllowed = GetDefaultQuadAllowed();
576
577   isOK = static_cast<bool>( load >> is );
578   if ( isOK )
579     _surfaceCurvature = (bool) is;
580   else
581     _surfaceCurvature = GetDefaultSurfaceCurvature();
582
583   isOK = static_cast<bool>( load >> is );
584   if ( isOK )
585     _fuseEdges = (bool) is;
586   else
587     _fuseEdges = GetDefaultFuseEdges();
588
589   isOK = static_cast<bool>( load >> is >> std::ws ); // size of meshSizeFile
590   if ( isOK && is > 0 )
591   {
592     _meshSizeFile.resize( is );
593     load.get( &_meshSizeFile[0], is+1 );
594   }
595
596   isOK = static_cast<bool>(load >> val);
597   if (isOK)
598     _chordalError = val;
599   else
600     load.clear(ios::badbit | load.rdstate());
601   _chordalErrorEnabled = ( _chordalError > 0 );
602
603
604   // added for option set completion
605
606   isOK = static_cast<bool>( load >> is );
607   if ( isOK )
608     _nbSurfOptSteps = is;
609
610   isOK = static_cast<bool>( load >> is );
611   if ( isOK )
612     _nbVolOptSteps = is;
613
614   isOK = static_cast<bool>( load >> val );
615   if ( isOK )
616     _elemSizeWeight =  val;
617
618   isOK = static_cast<bool>( load >> is );
619   if ( isOK )
620     _worstElemMeasure = is;
621
622   isOK = static_cast<bool>( load >> is );
623   if ( isOK )
624     _useDelauney = (bool) is;
625
626   isOK = static_cast<bool>( load >> is );
627   if ( isOK )
628     _checkOverlapping = (bool) is;
629
630   isOK = static_cast<bool>( load >> is );
631   if ( isOK )
632     _checkChartBoundary = (bool) is;
633
634   return load;
635 }
636
637 //================================================================================
638 /*!
639  * \brief Does nothing
640  * \param theMesh - the built mesh
641  * \param theShape - the geometry of interest
642  * \retval bool - always false
643  */
644 //================================================================================
645 bool NETGENPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh*   /*theMesh*/,
646                                                   const TopoDS_Shape& /*theShape*/)
647 {
648   return false;
649 }
650
651 //================================================================================
652 /*!
653  * \brief Initialize my parameter values by default parameters.
654  *  \retval bool - true if parameter values have been successfully defined
655  */
656 //================================================================================
657
658 bool NETGENPlugin_Hypothesis::SetParametersByDefaults(const TDefaults&  dflts,
659                                                       const SMESH_Mesh* theMesh)
660 {
661   _nbSegPerEdge = dflts._nbSegments;
662   _maxSize      = dflts._elemLength;
663
664   if ( dflts._shape && !dflts._shape->IsNull() )
665     _minSize    = NETGENPlugin_Mesher::GetDefaultMinSize( *dflts._shape, _maxSize );
666   else if ( theMesh && theMesh->HasShapeToMesh() )
667     _minSize    = NETGENPlugin_Mesher::GetDefaultMinSize( theMesh->GetShapeToMesh(), _maxSize );
668
669   if ( dflts._way == SMESH_Hypothesis::BY_AVERAGE_LENGTH )
670   {
671     _minSize      = dflts._elemLength / 100.;
672     _nbSegPerEdge = 1;
673     _chordalError = dflts._elemLength / 2.;
674     _chordalErrorEnabled = true;
675     _quadAllowed  = dflts._quadDominated;
676   }
677
678   return _nbSegPerEdge && _maxSize > 0;
679 }