1 // Copyright (C) 2007-2024 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 // SMESH SMESH : implementation of SMESH idl descriptions
24 // File : StdMeshers_NumberOfSegments.cxx
25 // Moved here from SMESH_NumberOfSegments.cxx
26 // Author : Paul RASCLE, EDF
29 #include "StdMeshers_NumberOfSegments.hxx"
31 #include "SMESHDS_Mesh.hxx"
32 #include "SMESHDS_SubMesh.hxx"
33 #include "SMESH_Comment.hxx"
34 #include "SMESH_Mesh.hxx"
35 #include "StdMeshers_Distribution.hxx"
37 #include <ExprIntrp_GenExp.hxx>
38 #include <Expr_Array1OfNamedUnknown.hxx>
39 #include <Expr_NamedUnknown.hxx>
40 #include <TColStd_Array1OfReal.hxx>
41 #include <TCollection_AsciiString.hxx>
43 #include <TopTools_IndexedMapOfShape.hxx>
45 #include <Standard_Failure.hxx>
46 #include <Standard_ErrorHandler.hxx>
48 #include <Basics_Utils.hxx>
50 using namespace StdMeshers;
53 const double PRECISION = 1e-7;
55 //=============================================================================
59 //=============================================================================
61 StdMeshers_NumberOfSegments::StdMeshers_NumberOfSegments(int hypId,
63 : StdMeshers_Reversible1D(hypId, gen),
64 _numberOfSegments(15),//issue 19923
65 _distrType(DT_Regular),
67 _convMode(1) //cut negative by default
69 _name = "NumberOfSegments";
73 //=============================================================================
77 //=============================================================================
79 StdMeshers_NumberOfSegments::~StdMeshers_NumberOfSegments()
83 //=============================================================================
87 //=============================================================================
89 StdMeshers_NumberOfSegments::BuildDistributionExpr( const char* expr,int nbSeg,int conv )
91 if( !buildDistribution( TCollection_AsciiString( ( Standard_CString )expr ), conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
97 StdMeshers_NumberOfSegments::BuildDistributionTab( const vector<double>& tab,
101 if( !buildDistribution( tab, conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
106 //=============================================================================
110 //=============================================================================
112 void StdMeshers_NumberOfSegments::SetNumberOfSegments(smIdType segmentsNumber)
114 smIdType oldNumberOfSegments = _numberOfSegments;
115 if (segmentsNumber <= 0)
116 throw SALOME_Exception(LOCALIZED("number of segments must be positive"));
117 _numberOfSegments = segmentsNumber;
119 if (oldNumberOfSegments != _numberOfSegments)
120 NotifySubMeshesHypothesisModification();
123 //=============================================================================
127 //=============================================================================
129 smIdType StdMeshers_NumberOfSegments::GetNumberOfSegments() const
131 return _numberOfSegments;
134 //================================================================================
138 //================================================================================
140 void StdMeshers_NumberOfSegments::SetDistrType(DistrType typ)
142 if (!IsValidDistrType(typ))
143 throw SALOME_Exception(LOCALIZED("distribution type is out of range"));
145 if (typ != _distrType)
148 NotifySubMeshesHypothesisModification();
152 //================================================================================
156 //================================================================================
158 StdMeshers_NumberOfSegments::DistrType StdMeshers_NumberOfSegments::GetDistrType() const
163 //================================================================================
167 //================================================================================
169 bool StdMeshers_NumberOfSegments::IsValidDistrType(int distrType) const
171 // DistrType is sequential, so we can just check against its first and last values
172 return distrType >= DT_Regular && distrType <= DT_BetaLaw;
175 //================================================================================
179 //================================================================================
181 void StdMeshers_NumberOfSegments::SetScaleFactor(double scaleFactor)
183 if (scaleFactor < PRECISION)
184 throw SALOME_Exception(LOCALIZED("scale factor must be positive"));
186 if (_distrType != DT_Scale)
187 _distrType = DT_Scale;
189 // commented by mpa for IPAL 52986
190 // if ( fabs(scaleFactor - 1.0) < PRECISION )
191 // _distrType = DT_Regular;
193 if ( fabs(_scaleFactor - scaleFactor) > PRECISION )
195 _scaleFactor = scaleFactor;
196 NotifySubMeshesHypothesisModification();
200 //================================================================================
204 //================================================================================
206 double StdMeshers_NumberOfSegments::GetScaleFactor() const
208 if (_distrType != DT_Scale)
209 throw SALOME_Exception(LOCALIZED("not a scale distribution"));
213 //================================================================================
217 //================================================================================
219 void StdMeshers_NumberOfSegments::SetBeta(double beta)
221 if (_distrType != DT_BetaLaw)
222 throw SALOME_Exception(LOCALIZED("not a Beta Law distribution"));
224 const double diff = fabs(fabs(_beta) - fabs(beta));
225 if (diff <= PRECISION)
227 // Check for a special case where we have values with
228 // equal base but opposite signs like -1.01 and 1.01
229 if (std::signbit(_beta) == std::signbit(beta))
234 NotifySubMeshesHypothesisModification();
237 //================================================================================
241 //================================================================================
243 double StdMeshers_NumberOfSegments::GetBeta() const
245 if (_distrType != DT_BetaLaw)
246 throw SALOME_Exception(LOCALIZED("not a Beta Law distribution"));
250 //================================================================================
254 //================================================================================
256 void StdMeshers_NumberOfSegments::SetTableFunction(const vector<double>& table)
258 if (_distrType != DT_TabFunc)
259 _distrType = DT_TabFunc;
260 //throw SALOME_Exception(LOCALIZED("not a table function distribution"));
261 if ( (table.size() % 2) != 0 )
262 throw SALOME_Exception(LOCALIZED("odd size of vector of table function"));
264 double prev = -PRECISION;
265 bool isSame = table.size() == _table.size();
268 for ( size_t i = 0; i < table.size() / 2; i++ )
270 double par = table[i*2];
271 double val = table[i*2+1];
276 val = pow( 10.0, val );
278 catch(Standard_Failure&) {
279 throw SALOME_Exception( LOCALIZED( "invalid value"));
283 else if( _convMode==1 && val<0.0 )
286 if ( par < 0 || par > 1)
287 throw SALOME_Exception(LOCALIZED("parameter of table function is out of range [0,1]"));
288 if ( fabs(par-prev) < PRECISION )
289 throw SALOME_Exception(LOCALIZED("two parameters are the same"));
291 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
292 if( val > PRECISION )
296 double oldpar = _table[i*2];
297 double oldval = _table[i*2+1];
298 if ( fabs(par - oldpar) > PRECISION || fabs(val - oldval) > PRECISION )
305 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
307 if ( pos && !isSame )
310 NotifySubMeshesHypothesisModification();
314 //================================================================================
318 //================================================================================
320 const vector<double>& StdMeshers_NumberOfSegments::GetTableFunction() const
322 if (_distrType != DT_TabFunc)
323 throw SALOME_Exception(LOCALIZED("not a table function distribution"));
327 //================================================================================
328 /*! check if only 't' is unknown variable in expression
330 //================================================================================
331 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
333 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
335 return sub->GetName()=="t";
338 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
340 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
341 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
344 if( name->GetName()!="t" )
348 res = isCorrectArg( sub );
353 //================================================================================
354 /*! this function parses the expression 'str' in order to check if syntax is correct
355 * ( result in 'syntax' ) and if only 't' is unknown variable in expression ( result in 'args' )
357 //================================================================================
358 bool process( const TCollection_AsciiString& str, int convMode,
359 bool& syntax, bool& args,
360 bool& non_neg, bool& non_zero,
361 bool& singulars, double& sing_point )
363 Kernel_Utils::Localizer loc;
365 bool parsed_ok = true;
366 Handle( ExprIntrp_GenExp ) myExpr;
369 myExpr = ExprIntrp_GenExp::Create();
370 myExpr->Process( str.ToCString() );
371 } catch(Standard_Failure&) {
377 if( parsed_ok && myExpr->IsDone() )
380 args = isCorrectArg( myExpr->Expression() );
383 bool res = parsed_ok && syntax && args;
393 FunctionExpr f( str.ToCString(), convMode );
395 for( int i=0; i<=max; i++ )
397 double t = double(i)/double(max), val;
398 if( !f.value( t, val ) )
414 return res && non_neg && non_zero && ( !singulars );
417 //================================================================================
421 //================================================================================
423 void StdMeshers_NumberOfSegments::SetExpressionFunction(const char* expr)
425 if (_distrType != DT_ExprFunc)
426 _distrType = DT_ExprFunc;
428 string func = CheckExpressionFunction( expr, _convMode );
432 NotifySubMeshesHypothesisModification();
436 //=======================================================================
437 //function : CheckExpressionFunction
438 //purpose : Checks validity of the expression of the function f(t), e.g. "sin(t)".
439 // In case of validity returns a cleaned expression
440 //=======================================================================
443 StdMeshers_NumberOfSegments::CheckExpressionFunction( const std::string& expr,
446 // remove white spaces
447 TCollection_AsciiString str((Standard_CString)expr.c_str());
453 bool syntax, args, non_neg, singulars, non_zero;
455 bool res = process( str, convMode, syntax, args, non_neg, non_zero, singulars, sing_point );
459 throw SALOME_Exception(SMESH_Comment("invalid expression syntax: ") << str );
461 throw SALOME_Exception(LOCALIZED("only 't' may be used as function argument"));
463 throw SALOME_Exception(LOCALIZED("only non-negative function can be used"));
467 sprintf( buf, "Function has singular point in %.3f", sing_point );
468 throw SALOME_Exception( buf );
471 throw SALOME_Exception(LOCALIZED("f(t)=0 cannot be used"));
474 return str.ToCString();
477 //================================================================================
481 //================================================================================
483 const char* StdMeshers_NumberOfSegments::GetExpressionFunction() const
485 if (_distrType != DT_ExprFunc)
486 throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
487 return _func.c_str();
490 //================================================================================
494 //================================================================================
496 void StdMeshers_NumberOfSegments::SetConversionMode( int conv )
498 // if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
499 // throw SALOME_Exception(LOCALIZED("not a functional distribution"));
501 if( conv != _convMode )
504 NotifySubMeshesHypothesisModification();
508 //================================================================================
512 //================================================================================
514 int StdMeshers_NumberOfSegments::ConversionMode() const
516 // if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
517 // throw SALOME_Exception(LOCALIZED("not a functional distribution"));
521 //=============================================================================
525 //=============================================================================
527 ostream & StdMeshers_NumberOfSegments::SaveTo(ostream & save)
529 size_t listSize = _edgeIDs.size();
530 save << _numberOfSegments << " " << (int)_distrType;
534 save << " " << _scaleFactor;
537 save << " " << _table.size();
538 for ( size_t i = 0; i < _table.size(); i++ )
539 save << " " << _table[i];
542 save << " " << _func;
545 save << " " << _beta;
552 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
553 save << " " << _convMode;
555 if ( _distrType != DT_Regular && listSize > 0 ) {
556 save << " " << listSize;
557 for ( size_t i = 0; i < listSize; i++ )
558 save << " " << _edgeIDs[i];
559 save << " " << _objEntry;
565 //=============================================================================
569 //=============================================================================
571 istream & StdMeshers_NumberOfSegments::LoadFrom(istream & load)
576 // read number of segments
577 isOK = static_cast<bool>(load >> a);
579 _numberOfSegments = a;
581 load.clear(ios::badbit | load.rdstate());
583 // read second stored value. It can be two variants here:
584 // 1. If the hypothesis is stored in old format (nb.segments and scale factor),
585 // we wait here the scale factor, which is double.
586 // 2. If the hypothesis is stored in new format
587 // (nb.segments, distr.type, some other params.),
588 // we wait here the distribution type, which is integer
590 isOK = static_cast<bool>(load >> scale_factor);
591 a = (int)scale_factor;
593 // try to interpret distribution type,
594 // supposing that this hypothesis was written in the new format
597 if (!IsValidDistrType(a))
598 _distrType = DT_Regular;
600 _distrType = (DistrType) a;
603 load.clear(ios::badbit | load.rdstate());
605 // parameters of distribution
611 isOK = static_cast<bool>(load >> b);
616 load.clear(ios::badbit | load.rdstate());
617 // this can mean, that the hypothesis is stored in old format
618 _distrType = DT_Regular;
619 _scaleFactor = scale_factor;
625 isOK = static_cast<bool>(load >> a);
628 _table.resize(a, 0.);
629 for ( size_t i=0; i < _table.size(); i++ )
631 isOK = static_cast<bool>(load >> b);
635 load.clear(ios::badbit | load.rdstate());
640 load.clear(ios::badbit | load.rdstate());
641 // this can mean, that the hypothesis is stored in old format
642 _distrType = DT_Regular;
643 _scaleFactor = scale_factor;
650 isOK = static_cast<bool>(load >> str);
655 load.clear(ios::badbit | load.rdstate());
656 // this can mean, that the hypothesis is stored in old format
657 _distrType = DT_Regular;
658 _scaleFactor = scale_factor;
665 isOK = static_cast<bool>(load >> b);
670 load.clear(ios::badbit | load.rdstate());
671 // this can mean, that the hypothesis is stored in old format
672 _distrType = DT_Regular;
673 _scaleFactor = scale_factor;
683 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
685 isOK = static_cast<bool>(load >> a);
689 load.clear(ios::badbit | load.rdstate());
692 // load reversed edges IDs
694 isOK = static_cast<bool>(load >> intVal);
695 if ( isOK && _distrType != DT_Regular && intVal > 0 ) {
696 _edgeIDs.reserve( intVal );
697 for ( size_t i = 0; i < _edgeIDs.capacity() && isOK; i++) {
698 isOK = static_cast<bool>(load >> intVal);
699 if ( isOK ) _edgeIDs.push_back( intVal );
701 isOK = static_cast<bool>(load >> _objEntry);
707 //=============================================================================
711 //=============================================================================
713 ostream & operator <<(ostream & save, StdMeshers_NumberOfSegments & hyp)
715 return hyp.SaveTo( save );
718 //=============================================================================
722 //=============================================================================
724 istream & operator >>(istream & load, StdMeshers_NumberOfSegments & hyp)
726 return hyp.LoadFrom( load );
729 //================================================================================
731 * \brief Initialize number of segments by the mesh built on the geometry
732 * \param theMesh - the built mesh
733 * \param theShape - the geometry of interest
734 * \retval bool - true if parameter values have been successfully defined
736 //================================================================================
738 bool StdMeshers_NumberOfSegments::SetParametersByMesh(const SMESH_Mesh* theMesh,
739 const TopoDS_Shape& theShape)
741 if ( !theMesh || theShape.IsNull() )
744 _numberOfSegments = 0;
745 _distrType = DT_Regular;
748 TopTools_IndexedMapOfShape edgeMap;
749 TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
750 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
751 for ( int i = 1; i <= edgeMap.Extent(); ++i )
753 // get current segment length
754 SMESHDS_SubMesh * eSubMesh = aMeshDS->MeshElements( edgeMap( i ));
755 if ( eSubMesh && eSubMesh->NbElements())
756 _numberOfSegments += eSubMesh->NbElements();
761 _numberOfSegments /= nbEdges;
763 if (_numberOfSegments == 0) _numberOfSegments = 1;
767 //================================================================================
769 * \brief Initialize my parameter values by default parameters.
770 * \retval bool - true if parameter values have been successfully defined
772 //================================================================================
774 bool StdMeshers_NumberOfSegments::SetParametersByDefaults(const TDefaults& dflts,
775 const SMESH_Mesh* /*theMesh*/)
777 return (_numberOfSegments = dflts._nbSegments );