1 // Copyright (C) 2007-2020 CEA/DEN, EDF R&D, 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 )
90 throw ( SALOME_Exception )
92 if( !buildDistribution( TCollection_AsciiString( ( Standard_CString )expr ), conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
98 StdMeshers_NumberOfSegments::BuildDistributionTab( const vector<double>& tab,
101 throw ( SALOME_Exception )
103 if( !buildDistribution( tab, conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
108 //=============================================================================
112 //=============================================================================
114 void StdMeshers_NumberOfSegments::SetNumberOfSegments(int segmentsNumber)
115 throw(SALOME_Exception)
117 int oldNumberOfSegments = _numberOfSegments;
118 if (segmentsNumber <= 0)
119 throw SALOME_Exception(LOCALIZED("number of segments must be positive"));
120 _numberOfSegments = segmentsNumber;
122 if (oldNumberOfSegments != _numberOfSegments)
123 NotifySubMeshesHypothesisModification();
126 //=============================================================================
130 //=============================================================================
132 int StdMeshers_NumberOfSegments::GetNumberOfSegments() const
134 return _numberOfSegments;
137 //================================================================================
141 //================================================================================
143 void StdMeshers_NumberOfSegments::SetDistrType(DistrType typ)
144 throw(SALOME_Exception)
146 if (typ < DT_Regular || typ > DT_ExprFunc)
147 throw SALOME_Exception(LOCALIZED("distribution type is out of range"));
149 if (typ != _distrType)
152 NotifySubMeshesHypothesisModification();
156 //================================================================================
160 //================================================================================
162 StdMeshers_NumberOfSegments::DistrType StdMeshers_NumberOfSegments::GetDistrType() const
167 //================================================================================
171 //================================================================================
173 void StdMeshers_NumberOfSegments::SetScaleFactor(double scaleFactor)
174 throw(SALOME_Exception)
176 if (scaleFactor < PRECISION)
177 throw SALOME_Exception(LOCALIZED("scale factor must be positive"));
179 if (_distrType != DT_Scale)
180 _distrType = DT_Scale;
182 // commented by mpa for IPAL 52986
183 // if ( fabs(scaleFactor - 1.0) < PRECISION )
184 // _distrType = DT_Regular;
186 if ( fabs(_scaleFactor - scaleFactor) > PRECISION )
188 _scaleFactor = scaleFactor;
189 NotifySubMeshesHypothesisModification();
193 //================================================================================
197 //================================================================================
199 double StdMeshers_NumberOfSegments::GetScaleFactor() const
200 throw(SALOME_Exception)
202 if (_distrType != DT_Scale)
203 throw SALOME_Exception(LOCALIZED("not a scale distribution"));
207 //================================================================================
211 //================================================================================
213 void StdMeshers_NumberOfSegments::SetTableFunction(const vector<double>& table)
214 throw(SALOME_Exception)
216 if (_distrType != DT_TabFunc)
217 _distrType = DT_TabFunc;
218 //throw SALOME_Exception(LOCALIZED("not a table function distribution"));
219 if ( (table.size() % 2) != 0 )
220 throw SALOME_Exception(LOCALIZED("odd size of vector of table function"));
222 double prev = -PRECISION;
223 bool isSame = table.size() == _table.size();
226 for ( size_t i = 0; i < table.size() / 2; i++ )
228 double par = table[i*2];
229 double val = table[i*2+1];
234 val = pow( 10.0, val );
236 catch(Standard_Failure) {
237 throw SALOME_Exception( LOCALIZED( "invalid value"));
241 else if( _convMode==1 && val<0.0 )
244 if ( par < 0 || par > 1)
245 throw SALOME_Exception(LOCALIZED("parameter of table function is out of range [0,1]"));
246 if ( fabs(par-prev) < PRECISION )
247 throw SALOME_Exception(LOCALIZED("two parameters are the same"));
249 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
250 if( val > PRECISION )
254 double oldpar = _table[i*2];
255 double oldval = _table[i*2+1];
256 if ( fabs(par - oldpar) > PRECISION || fabs(val - oldval) > PRECISION )
263 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
265 if ( pos && !isSame )
268 NotifySubMeshesHypothesisModification();
272 //================================================================================
276 //================================================================================
278 const vector<double>& StdMeshers_NumberOfSegments::GetTableFunction() const
279 throw(SALOME_Exception)
281 if (_distrType != DT_TabFunc)
282 throw SALOME_Exception(LOCALIZED("not a table function distribution"));
286 //================================================================================
287 /*! check if only 't' is unknown variable in expression
289 //================================================================================
290 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
292 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
294 return sub->GetName()=="t";
297 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
299 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
300 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
303 if( name->GetName()!="t" )
307 res = isCorrectArg( sub );
312 //================================================================================
313 /*! this function parses the expression 'str' in order to check if syntax is correct
314 * ( result in 'syntax' ) and if only 't' is unknown variable in expression ( result in 'args' )
316 //================================================================================
317 bool process( const TCollection_AsciiString& str, int convMode,
318 bool& syntax, bool& args,
319 bool& non_neg, bool& non_zero,
320 bool& singulars, double& sing_point )
322 Kernel_Utils::Localizer loc;
324 bool parsed_ok = true;
325 Handle( ExprIntrp_GenExp ) myExpr;
328 myExpr = ExprIntrp_GenExp::Create();
329 myExpr->Process( str.ToCString() );
330 } catch(Standard_Failure) {
336 if( parsed_ok && myExpr->IsDone() )
339 args = isCorrectArg( myExpr->Expression() );
342 bool res = parsed_ok && syntax && args;
352 FunctionExpr f( str.ToCString(), convMode );
354 for( int i=0; i<=max; i++ )
356 double t = double(i)/double(max), val;
357 if( !f.value( t, val ) )
373 return res && non_neg && non_zero && ( !singulars );
376 //================================================================================
380 //================================================================================
382 void StdMeshers_NumberOfSegments::SetExpressionFunction(const char* expr)
383 throw(SALOME_Exception)
385 if (_distrType != DT_ExprFunc)
386 _distrType = DT_ExprFunc;
388 string func = CheckExpressionFunction( expr, _convMode );
392 NotifySubMeshesHypothesisModification();
396 //=======================================================================
397 //function : CheckExpressionFunction
398 //purpose : Checks validity of the expression of the function f(t), e.g. "sin(t)".
399 // In case of validity returns a cleaned expression
400 //=======================================================================
403 StdMeshers_NumberOfSegments::CheckExpressionFunction( const std::string& expr,
405 throw (SALOME_Exception)
407 // remove white spaces
408 TCollection_AsciiString str((Standard_CString)expr.c_str());
414 bool syntax, args, non_neg, singulars, non_zero;
416 bool res = process( str, convMode, syntax, args, non_neg, non_zero, singulars, sing_point );
420 throw SALOME_Exception(SMESH_Comment("invalid expression syntax: ") << str );
422 throw SALOME_Exception(LOCALIZED("only 't' may be used as function argument"));
424 throw SALOME_Exception(LOCALIZED("only non-negative function can be used"));
428 sprintf( buf, "Function has singular point in %.3f", sing_point );
429 throw SALOME_Exception( buf );
432 throw SALOME_Exception(LOCALIZED("f(t)=0 cannot be used"));
435 return str.ToCString();
438 //================================================================================
442 //================================================================================
444 const char* StdMeshers_NumberOfSegments::GetExpressionFunction() const
445 throw(SALOME_Exception)
447 if (_distrType != DT_ExprFunc)
448 throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
449 return _func.c_str();
452 //================================================================================
456 //================================================================================
458 void StdMeshers_NumberOfSegments::SetConversionMode( int conv )
459 throw(SALOME_Exception)
461 // if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
462 // throw SALOME_Exception(LOCALIZED("not a functional distribution"));
464 if( conv != _convMode )
467 NotifySubMeshesHypothesisModification();
471 //================================================================================
475 //================================================================================
477 int StdMeshers_NumberOfSegments::ConversionMode() const
478 throw(SALOME_Exception)
480 // if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
481 // throw SALOME_Exception(LOCALIZED("not a functional distribution"));
485 //=============================================================================
489 //=============================================================================
491 ostream & StdMeshers_NumberOfSegments::SaveTo(ostream & save)
493 int listSize = _edgeIDs.size();
494 save << _numberOfSegments << " " << (int)_distrType;
498 save << " " << _scaleFactor;
501 save << " " << _table.size();
502 for ( size_t i = 0; i < _table.size(); i++ )
503 save << " " << _table[i];
506 save << " " << _func;
513 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
514 save << " " << _convMode;
516 if ( _distrType != DT_Regular && listSize > 0 ) {
517 save << " " << listSize;
518 for ( int i = 0; i < listSize; i++ )
519 save << " " << _edgeIDs[i];
520 save << " " << _objEntry;
526 //=============================================================================
530 //=============================================================================
532 istream & StdMeshers_NumberOfSegments::LoadFrom(istream & load)
537 // read number of segments
538 isOK = static_cast<bool>(load >> a);
540 _numberOfSegments = a;
542 load.clear(ios::badbit | load.rdstate());
544 // read second stored value. It can be two variants here:
545 // 1. If the hypothesis is stored in old format (nb.segments and scale factor),
546 // we wait here the scale factor, which is double.
547 // 2. If the hypothesis is stored in new format
548 // (nb.segments, distr.type, some other params.),
549 // we wait here the distribution type, which is integer
551 isOK = static_cast<bool>(load >> scale_factor);
552 a = (int)scale_factor;
554 // try to interpret distribution type,
555 // supposing that this hypothesis was written in the new format
558 if (a < DT_Regular || a > DT_ExprFunc)
559 _distrType = DT_Regular;
561 _distrType = (DistrType) a;
564 load.clear(ios::badbit | load.rdstate());
566 // parameters of distribution
572 isOK = static_cast<bool>(load >> b);
577 load.clear(ios::badbit | load.rdstate());
578 // this can mean, that the hypothesis is stored in old format
579 _distrType = DT_Regular;
580 _scaleFactor = scale_factor;
586 isOK = static_cast<bool>(load >> a);
589 _table.resize(a, 0.);
590 for ( size_t i=0; i < _table.size(); i++ )
592 isOK = static_cast<bool>(load >> b);
596 load.clear(ios::badbit | load.rdstate());
601 load.clear(ios::badbit | load.rdstate());
602 // this can mean, that the hypothesis is stored in old format
603 _distrType = DT_Regular;
604 _scaleFactor = scale_factor;
611 isOK = static_cast<bool>(load >> str);
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;
628 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
630 isOK = static_cast<bool>(load >> a);
634 load.clear(ios::badbit | load.rdstate());
637 // load reversed edges IDs
639 isOK = static_cast<bool>(load >> intVal);
640 if ( isOK && _distrType != DT_Regular && intVal > 0 ) {
641 _edgeIDs.reserve( intVal );
642 for ( size_t i = 0; i < _edgeIDs.capacity() && isOK; i++) {
643 isOK = static_cast<bool>(load >> intVal);
644 if ( isOK ) _edgeIDs.push_back( intVal );
646 isOK = static_cast<bool>(load >> _objEntry);
652 //=============================================================================
656 //=============================================================================
658 ostream & operator <<(ostream & save, StdMeshers_NumberOfSegments & hyp)
660 return hyp.SaveTo( save );
663 //=============================================================================
667 //=============================================================================
669 istream & operator >>(istream & load, StdMeshers_NumberOfSegments & hyp)
671 return hyp.LoadFrom( load );
674 //================================================================================
676 * \brief Initialize number of segments by the mesh built on the geometry
677 * \param theMesh - the built mesh
678 * \param theShape - the geometry of interest
679 * \retval bool - true if parameter values have been successfully defined
681 //================================================================================
683 bool StdMeshers_NumberOfSegments::SetParametersByMesh(const SMESH_Mesh* theMesh,
684 const TopoDS_Shape& theShape)
686 if ( !theMesh || theShape.IsNull() )
689 _numberOfSegments = 0;
690 _distrType = DT_Regular;
693 TopTools_IndexedMapOfShape edgeMap;
694 TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
695 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
696 for ( int i = 1; i <= edgeMap.Extent(); ++i )
698 // get current segment length
699 SMESHDS_SubMesh * eSubMesh = aMeshDS->MeshElements( edgeMap( i ));
700 if ( eSubMesh && eSubMesh->NbElements())
701 _numberOfSegments += eSubMesh->NbElements();
706 _numberOfSegments /= nbEdges;
708 if (_numberOfSegments == 0) _numberOfSegments = 1;
712 //================================================================================
714 * \brief Initialize my parameter values by default parameters.
715 * \retval bool - true if parameter values have been successfully defined
717 //================================================================================
719 bool StdMeshers_NumberOfSegments::SetParametersByDefaults(const TDefaults& dflts,
720 const SMESH_Mesh* /*theMesh*/)
722 return (_numberOfSegments = dflts._nbSegments );