1 // Copyright (C) 2007-2023 CEA, EDF, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // NETGENPlugin : C++ implementation
24 // File : NETGENPlugin_Hypothesis.cxx
25 // Author : Michael Sazonov (OCN)
29 #include "NETGENPlugin_Hypothesis.hxx"
31 #include "NETGENPlugin_Mesher.hxx"
32 #include "SMESH_Mesh.hxx"
34 #include <utilities.h>
38 //=============================================================================
42 //=============================================================================
43 NETGENPlugin_Hypothesis::NETGENPlugin_Hypothesis (int hypId, SMESH_Gen * gen)
45 : SMESH_Hypothesis(hypId, gen),
46 _fineness (GetDefaultFineness()),
47 _secondOrder (GetDefaultSecondOrder()),
48 _quadAllowed (GetDefaultQuadAllowed()),
49 _maxSize (GetDefaultMaxSize()),
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 _nbThreads (GetDefaultNbThreads()),
62 _surfaceCurvature (GetDefaultSurfaceCurvature()),
63 _useDelauney (GetDefaultUseDelauney()),
64 _checkOverlapping (GetDefaultCheckOverlapping()),
65 _checkChartBoundary (GetDefaultCheckChartBoundary()),
66 _fuseEdges (GetDefaultFuseEdges())
68 _name = "NETGEN_Parameters";
72 //=============================================================================
76 //=============================================================================
77 void NETGENPlugin_Hypothesis::SetMaxSize(double theSize)
79 if (theSize != _maxSize)
82 NotifySubMeshesHypothesisModification();
86 //=============================================================================
90 //=============================================================================
91 void NETGENPlugin_Hypothesis::SetMinSize(double theSize)
93 if (theSize != _minSize)
96 NotifySubMeshesHypothesisModification();
100 //=============================================================================
104 //=============================================================================
105 void NETGENPlugin_Hypothesis::SetSecondOrder(bool theVal)
107 if (theVal != _secondOrder)
109 _secondOrder = theVal;
110 NotifySubMeshesHypothesisModification();
114 //=============================================================================
118 //=============================================================================
119 void NETGENPlugin_Hypothesis::SetOptimize(bool theVal)
121 if (theVal != _optimize)
124 NotifySubMeshesHypothesisModification();
128 //=============================================================================
132 //=============================================================================
133 void NETGENPlugin_Hypothesis::SetFineness(Fineness theFineness)
135 if (theFineness != _fineness)
137 _fineness = theFineness;
138 // the predefined values are taken from NETGEN 4.5 sources
149 _nbSegPerRadius = 1.5;
170 NotifySubMeshesHypothesisModification();
174 //=============================================================================
178 //=============================================================================
179 void NETGENPlugin_Hypothesis::SetGrowthRate(double theRate)
181 if (theRate != _growthRate)
183 _growthRate = theRate;
184 _fineness = UserDefined;
185 NotifySubMeshesHypothesisModification();
189 //=============================================================================
193 //=============================================================================
194 void NETGENPlugin_Hypothesis::SetNbSegPerEdge(double theVal)
196 if (theVal != _nbSegPerEdge)
198 _nbSegPerEdge = theVal;
199 _fineness = UserDefined;
200 NotifySubMeshesHypothesisModification();
204 //=============================================================================
208 //=============================================================================
209 void NETGENPlugin_Hypothesis::SetNbSegPerRadius(double theVal)
211 if (theVal != _nbSegPerRadius)
213 _nbSegPerRadius = theVal;
214 _fineness = UserDefined;
215 NotifySubMeshesHypothesisModification();
219 //=============================================================================
223 //=============================================================================
224 void NETGENPlugin_Hypothesis::SetChordalErrorEnabled(bool theVal)
226 if (theVal != _chordalErrorEnabled)
228 _chordalErrorEnabled = theVal;
229 NotifySubMeshesHypothesisModification();
233 //=============================================================================
237 //=============================================================================
238 void NETGENPlugin_Hypothesis::SetChordalError(double theVal)
240 if (theVal != _chordalError)
242 _chordalError = theVal;
243 NotifySubMeshesHypothesisModification();
247 //=============================================================================
251 //=============================================================================
252 void NETGENPlugin_Hypothesis::SetLocalSizeOnEntry(const std::string& entry, double localSize)
254 if(_localSize[entry] != localSize)
256 _localSize[entry] = localSize;
257 NotifySubMeshesHypothesisModification();
261 //=============================================================================
265 //=============================================================================
266 double NETGENPlugin_Hypothesis::GetLocalSizeOnEntry(const std::string& entry)
268 TLocalSize::iterator it = _localSize.find( entry );
269 if ( it != _localSize.end() )
275 //=============================================================================
279 //=============================================================================
280 void NETGENPlugin_Hypothesis::UnsetLocalSizeOnEntry(const std::string& entry)
282 _localSize.erase(entry);
283 NotifySubMeshesHypothesisModification();
286 //=============================================================================
290 //=============================================================================
291 void NETGENPlugin_Hypothesis::SetMeshSizeFile(const std::string& fileName)
293 if ( fileName != _meshSizeFile )
295 _meshSizeFile = fileName;
296 NotifySubMeshesHypothesisModification();
300 //=============================================================================
304 //=============================================================================
305 void NETGENPlugin_Hypothesis::SetQuadAllowed(bool theVal)
307 if (theVal != _quadAllowed)
309 _quadAllowed = theVal;
310 NotifySubMeshesHypothesisModification();
314 //=============================================================================
318 //=============================================================================
319 void NETGENPlugin_Hypothesis::SetSurfaceCurvature(bool theVal)
321 if (theVal != _surfaceCurvature)
323 _surfaceCurvature = theVal;
324 NotifySubMeshesHypothesisModification();
328 //=============================================================================
332 //=============================================================================
333 void NETGENPlugin_Hypothesis::SetFuseEdges(bool theVal)
335 if (theVal != _fuseEdges)
338 NotifySubMeshesHypothesisModification();
342 //=======================================================================
343 //function : SetNbSurfOptSteps
345 //=======================================================================
347 void NETGENPlugin_Hypothesis::SetNbSurfOptSteps( int theVal )
349 if (theVal != _nbSurfOptSteps)
351 _nbSurfOptSteps = theVal;
352 NotifySubMeshesHypothesisModification();
356 //=======================================================================
357 //function : SetNbVolOptSteps
359 //=======================================================================
361 void NETGENPlugin_Hypothesis::SetNbVolOptSteps( int theVal )
363 if (theVal != _nbVolOptSteps)
365 _nbVolOptSteps = theVal;
366 NotifySubMeshesHypothesisModification();
370 //=======================================================================
371 //function : SetElemSizeWeight
373 //=======================================================================
375 void NETGENPlugin_Hypothesis::SetElemSizeWeight( double theVal )
377 if (theVal != _elemSizeWeight)
379 _elemSizeWeight = theVal;
380 NotifySubMeshesHypothesisModification();
384 //=======================================================================
385 //function : SetWorstElemMeasure
387 //=======================================================================
389 void NETGENPlugin_Hypothesis::SetWorstElemMeasure( int theVal )
391 if (theVal != _worstElemMeasure)
393 _worstElemMeasure = theVal;
394 NotifySubMeshesHypothesisModification();
398 //=======================================================================
399 //function : SetUseDelauney
401 //=======================================================================
403 void NETGENPlugin_Hypothesis::SetUseDelauney( bool theVal )
405 if (theVal != _useDelauney )
407 _useDelauney = theVal;
408 NotifySubMeshesHypothesisModification();
412 //=======================================================================
413 //function : SetCheckOverlapping
415 //=======================================================================
417 void NETGENPlugin_Hypothesis::SetCheckOverlapping( bool theVal )
419 if (theVal != _checkOverlapping )
421 _checkOverlapping = theVal;
422 NotifySubMeshesHypothesisModification();
426 //=======================================================================
427 //function : SetCheckChartBoundary
429 //=======================================================================
431 void NETGENPlugin_Hypothesis::SetCheckChartBoundary( bool theVal )
433 if (theVal != _checkChartBoundary)
435 _checkChartBoundary = theVal;
436 NotifySubMeshesHypothesisModification();
440 //=======================================================================
441 //function : SetNbThreads
443 //=======================================================================
445 void NETGENPlugin_Hypothesis::SetNbThreads( int theVal )
447 if (theVal != _nbThreads)
450 NotifySubMeshesHypothesisModification();
454 //=============================================================================
458 //=============================================================================
459 ostream & NETGENPlugin_Hypothesis::SaveTo(ostream & save)
461 save << _maxSize << " " << _fineness;
463 if (_fineness == UserDefined)
464 save << " " << _growthRate << " " << _nbSegPerEdge << " " << _nbSegPerRadius;
466 save << " " << (int)_secondOrder << " " << (int)_optimize;
468 TLocalSize::iterator it_sm = _localSize.begin();
469 if (it_sm != _localSize.end()) {
470 save << " " << "__LOCALSIZE_BEGIN__";
471 for ( ; it_sm != _localSize.end(); ++it_sm ) {
472 save << " " << it_sm->first
473 << " " << it_sm->second << "%#"; // "%#" is a mark of value end
475 save << " " << "__LOCALSIZE_END__";
477 save << " " << _minSize;
478 save << " " << _quadAllowed;
479 save << " " << _surfaceCurvature;
480 save << " " << _fuseEdges;
482 save << " " << _meshSizeFile.size() << " " << _meshSizeFile;
484 save << " " << ( _chordalErrorEnabled ? _chordalError : 0. );
487 // added for option set completion
489 save << " " << _nbSurfOptSteps;
490 save << " " << _nbVolOptSteps;
491 save << " " << _elemSizeWeight;
492 save << " " << _worstElemMeasure;
494 save << " " << _useDelauney;
495 save << " " << _checkOverlapping;
496 save << " " << _checkChartBoundary;
501 //=============================================================================
505 //=============================================================================
506 istream & NETGENPlugin_Hypothesis::LoadFrom(istream & load)
512 isOK = static_cast<bool>(load >> val);
516 load.clear(ios::badbit | load.rdstate());
518 isOK = static_cast<bool>(load >> is);
520 SetFineness((Fineness) is);
522 load.clear(ios::badbit | load.rdstate());
524 if (_fineness == UserDefined)
526 isOK = static_cast<bool>(load >> val);
530 load.clear(ios::badbit | load.rdstate());
532 isOK = static_cast<bool>(load >> val);
536 load.clear(ios::badbit | load.rdstate());
538 isOK = static_cast<bool>(load >> val);
540 _nbSegPerRadius = val;
542 load.clear(ios::badbit | load.rdstate());
545 isOK = static_cast<bool>(load >> is);
547 _secondOrder = (bool) is;
549 load.clear(ios::badbit | load.rdstate());
551 isOK = static_cast<bool>(load >> is);
553 _optimize = (bool) is;
555 load.clear(ios::badbit | load.rdstate());
557 std::string option_or_sm;
558 bool hasLocalSize = false;
560 isOK = static_cast<bool>(load >> option_or_sm);
562 if (option_or_sm == "__LOCALSIZE_BEGIN__")
565 std::string smEntry, smValue;
566 while (isOK && hasLocalSize) {
567 isOK = static_cast<bool>(load >> smEntry);
569 if (smEntry == "__LOCALSIZE_END__")
571 isOK = static_cast<bool>(load >> smValue);
574 std::istringstream tmp(smValue);
577 _localSize[ smEntry ] = val;
581 if ( !hasLocalSize && !option_or_sm.empty() )
582 _minSize = atof( option_or_sm.c_str() );
586 isOK = static_cast<bool>( load >> is );
588 _quadAllowed = (bool) is;
590 _quadAllowed = GetDefaultQuadAllowed();
592 isOK = static_cast<bool>( load >> is );
594 _surfaceCurvature = (bool) is;
596 _surfaceCurvature = GetDefaultSurfaceCurvature();
598 isOK = static_cast<bool>( load >> is );
600 _fuseEdges = (bool) is;
602 _fuseEdges = GetDefaultFuseEdges();
604 isOK = static_cast<bool>( load >> is >> std::ws ); // size of meshSizeFile
605 if ( isOK && is > 0 )
607 _meshSizeFile.resize( is );
608 load.get( &_meshSizeFile[0], is+1 );
611 isOK = static_cast<bool>(load >> val);
615 load.clear(ios::badbit | load.rdstate());
616 _chordalErrorEnabled = ( _chordalError > 0 );
619 // added for option set completion
621 isOK = static_cast<bool>( load >> is );
623 _nbSurfOptSteps = is;
625 isOK = static_cast<bool>( load >> is );
629 isOK = static_cast<bool>( load >> val );
631 _elemSizeWeight = val;
633 isOK = static_cast<bool>( load >> is );
635 _worstElemMeasure = is;
637 isOK = static_cast<bool>( load >> is );
639 _useDelauney = (bool) is;
641 isOK = static_cast<bool>( load >> is );
643 _checkOverlapping = (bool) is;
645 isOK = static_cast<bool>( load >> is );
647 _checkChartBoundary = (bool) is;
652 //================================================================================
654 * \brief Does nothing
655 * \param theMesh - the built mesh
656 * \param theShape - the geometry of interest
657 * \retval bool - always false
659 //================================================================================
660 bool NETGENPlugin_Hypothesis::SetParametersByMesh(const SMESH_Mesh* /*theMesh*/,
661 const TopoDS_Shape& /*theShape*/)
666 //================================================================================
668 * \brief Initialize my parameter values by default parameters.
669 * \retval bool - true if parameter values have been successfully defined
671 //================================================================================
673 bool NETGENPlugin_Hypothesis::SetParametersByDefaults(const TDefaults& dflts,
674 const SMESH_Mesh* theMesh)
676 _nbSegPerEdge = dflts._nbSegments;
677 _maxSize = dflts._elemLength;
679 if ( dflts._shape && !dflts._shape->IsNull() )
680 _minSize = NETGENPlugin_Mesher::GetDefaultMinSize( *dflts._shape, _maxSize );
681 else if ( theMesh && theMesh->HasShapeToMesh() )
682 _minSize = NETGENPlugin_Mesher::GetDefaultMinSize( theMesh->GetShapeToMesh(), _maxSize );
684 if ( dflts._way == SMESH_Hypothesis::BY_AVERAGE_LENGTH )
686 _minSize = dflts._elemLength / 100.;
688 _chordalError = dflts._elemLength / 2.;
689 _chordalErrorEnabled = true;
690 _quadAllowed = dflts._quadDominated;
693 return _nbSegPerEdge && _maxSize > 0;