1 // Copyright (C) 2007-2021 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 )
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 (typ < DT_Regular || typ > DT_ExprFunc)
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 void StdMeshers_NumberOfSegments::SetScaleFactor(double scaleFactor)
171 if (scaleFactor < PRECISION)
172 throw SALOME_Exception(LOCALIZED("scale factor must be positive"));
174 if (_distrType != DT_Scale)
175 _distrType = DT_Scale;
177 // commented by mpa for IPAL 52986
178 // if ( fabs(scaleFactor - 1.0) < PRECISION )
179 // _distrType = DT_Regular;
181 if ( fabs(_scaleFactor - scaleFactor) > PRECISION )
183 _scaleFactor = scaleFactor;
184 NotifySubMeshesHypothesisModification();
188 //================================================================================
192 //================================================================================
194 double StdMeshers_NumberOfSegments::GetScaleFactor() const
196 if (_distrType != DT_Scale)
197 throw SALOME_Exception(LOCALIZED("not a scale distribution"));
201 //================================================================================
205 //================================================================================
207 void StdMeshers_NumberOfSegments::SetTableFunction(const vector<double>& table)
209 if (_distrType != DT_TabFunc)
210 _distrType = DT_TabFunc;
211 //throw SALOME_Exception(LOCALIZED("not a table function distribution"));
212 if ( (table.size() % 2) != 0 )
213 throw SALOME_Exception(LOCALIZED("odd size of vector of table function"));
215 double prev = -PRECISION;
216 bool isSame = table.size() == _table.size();
219 for ( size_t i = 0; i < table.size() / 2; i++ )
221 double par = table[i*2];
222 double val = table[i*2+1];
227 val = pow( 10.0, val );
229 catch(Standard_Failure&) {
230 throw SALOME_Exception( LOCALIZED( "invalid value"));
234 else if( _convMode==1 && val<0.0 )
237 if ( par < 0 || par > 1)
238 throw SALOME_Exception(LOCALIZED("parameter of table function is out of range [0,1]"));
239 if ( fabs(par-prev) < PRECISION )
240 throw SALOME_Exception(LOCALIZED("two parameters are the same"));
242 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
243 if( val > PRECISION )
247 double oldpar = _table[i*2];
248 double oldval = _table[i*2+1];
249 if ( fabs(par - oldpar) > PRECISION || fabs(val - oldval) > PRECISION )
256 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
258 if ( pos && !isSame )
261 NotifySubMeshesHypothesisModification();
265 //================================================================================
269 //================================================================================
271 const vector<double>& StdMeshers_NumberOfSegments::GetTableFunction() const
273 if (_distrType != DT_TabFunc)
274 throw SALOME_Exception(LOCALIZED("not a table function distribution"));
278 //================================================================================
279 /*! check if only 't' is unknown variable in expression
281 //================================================================================
282 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
284 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
286 return sub->GetName()=="t";
289 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
291 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
292 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
295 if( name->GetName()!="t" )
299 res = isCorrectArg( sub );
304 //================================================================================
305 /*! this function parses the expression 'str' in order to check if syntax is correct
306 * ( result in 'syntax' ) and if only 't' is unknown variable in expression ( result in 'args' )
308 //================================================================================
309 bool process( const TCollection_AsciiString& str, int convMode,
310 bool& syntax, bool& args,
311 bool& non_neg, bool& non_zero,
312 bool& singulars, double& sing_point )
314 Kernel_Utils::Localizer loc;
316 bool parsed_ok = true;
317 Handle( ExprIntrp_GenExp ) myExpr;
320 myExpr = ExprIntrp_GenExp::Create();
321 myExpr->Process( str.ToCString() );
322 } catch(Standard_Failure&) {
328 if( parsed_ok && myExpr->IsDone() )
331 args = isCorrectArg( myExpr->Expression() );
334 bool res = parsed_ok && syntax && args;
344 FunctionExpr f( str.ToCString(), convMode );
346 for( int i=0; i<=max; i++ )
348 double t = double(i)/double(max), val;
349 if( !f.value( t, val ) )
365 return res && non_neg && non_zero && ( !singulars );
368 //================================================================================
372 //================================================================================
374 void StdMeshers_NumberOfSegments::SetExpressionFunction(const char* expr)
376 if (_distrType != DT_ExprFunc)
377 _distrType = DT_ExprFunc;
379 string func = CheckExpressionFunction( expr, _convMode );
383 NotifySubMeshesHypothesisModification();
387 //=======================================================================
388 //function : CheckExpressionFunction
389 //purpose : Checks validity of the expression of the function f(t), e.g. "sin(t)".
390 // In case of validity returns a cleaned expression
391 //=======================================================================
394 StdMeshers_NumberOfSegments::CheckExpressionFunction( const std::string& expr,
397 // remove white spaces
398 TCollection_AsciiString str((Standard_CString)expr.c_str());
404 bool syntax, args, non_neg, singulars, non_zero;
406 bool res = process( str, convMode, syntax, args, non_neg, non_zero, singulars, sing_point );
410 throw SALOME_Exception(SMESH_Comment("invalid expression syntax: ") << str );
412 throw SALOME_Exception(LOCALIZED("only 't' may be used as function argument"));
414 throw SALOME_Exception(LOCALIZED("only non-negative function can be used"));
418 sprintf( buf, "Function has singular point in %.3f", sing_point );
419 throw SALOME_Exception( buf );
422 throw SALOME_Exception(LOCALIZED("f(t)=0 cannot be used"));
425 return str.ToCString();
428 //================================================================================
432 //================================================================================
434 const char* StdMeshers_NumberOfSegments::GetExpressionFunction() const
436 if (_distrType != DT_ExprFunc)
437 throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
438 return _func.c_str();
441 //================================================================================
445 //================================================================================
447 void StdMeshers_NumberOfSegments::SetConversionMode( int conv )
449 // if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
450 // throw SALOME_Exception(LOCALIZED("not a functional distribution"));
452 if( conv != _convMode )
455 NotifySubMeshesHypothesisModification();
459 //================================================================================
463 //================================================================================
465 int StdMeshers_NumberOfSegments::ConversionMode() const
467 // if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
468 // throw SALOME_Exception(LOCALIZED("not a functional distribution"));
472 //=============================================================================
476 //=============================================================================
478 ostream & StdMeshers_NumberOfSegments::SaveTo(ostream & save)
480 size_t listSize = _edgeIDs.size();
481 save << _numberOfSegments << " " << (int)_distrType;
485 save << " " << _scaleFactor;
488 save << " " << _table.size();
489 for ( size_t i = 0; i < _table.size(); i++ )
490 save << " " << _table[i];
493 save << " " << _func;
500 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
501 save << " " << _convMode;
503 if ( _distrType != DT_Regular && listSize > 0 ) {
504 save << " " << listSize;
505 for ( size_t i = 0; i < listSize; i++ )
506 save << " " << _edgeIDs[i];
507 save << " " << _objEntry;
513 //=============================================================================
517 //=============================================================================
519 istream & StdMeshers_NumberOfSegments::LoadFrom(istream & load)
524 // read number of segments
525 isOK = static_cast<bool>(load >> a);
527 _numberOfSegments = a;
529 load.clear(ios::badbit | load.rdstate());
531 // read second stored value. It can be two variants here:
532 // 1. If the hypothesis is stored in old format (nb.segments and scale factor),
533 // we wait here the scale factor, which is double.
534 // 2. If the hypothesis is stored in new format
535 // (nb.segments, distr.type, some other params.),
536 // we wait here the distribution type, which is integer
538 isOK = static_cast<bool>(load >> scale_factor);
539 a = (int)scale_factor;
541 // try to interpret distribution type,
542 // supposing that this hypothesis was written in the new format
545 if (a < DT_Regular || a > DT_ExprFunc)
546 _distrType = DT_Regular;
548 _distrType = (DistrType) a;
551 load.clear(ios::badbit | load.rdstate());
553 // parameters of distribution
559 isOK = static_cast<bool>(load >> b);
564 load.clear(ios::badbit | load.rdstate());
565 // this can mean, that the hypothesis is stored in old format
566 _distrType = DT_Regular;
567 _scaleFactor = scale_factor;
573 isOK = static_cast<bool>(load >> a);
576 _table.resize(a, 0.);
577 for ( size_t i=0; i < _table.size(); i++ )
579 isOK = static_cast<bool>(load >> b);
583 load.clear(ios::badbit | load.rdstate());
588 load.clear(ios::badbit | load.rdstate());
589 // this can mean, that the hypothesis is stored in old format
590 _distrType = DT_Regular;
591 _scaleFactor = scale_factor;
598 isOK = static_cast<bool>(load >> str);
603 load.clear(ios::badbit | load.rdstate());
604 // this can mean, that the hypothesis is stored in old format
605 _distrType = DT_Regular;
606 _scaleFactor = scale_factor;
615 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
617 isOK = static_cast<bool>(load >> a);
621 load.clear(ios::badbit | load.rdstate());
624 // load reversed edges IDs
626 isOK = static_cast<bool>(load >> intVal);
627 if ( isOK && _distrType != DT_Regular && intVal > 0 ) {
628 _edgeIDs.reserve( intVal );
629 for ( size_t i = 0; i < _edgeIDs.capacity() && isOK; i++) {
630 isOK = static_cast<bool>(load >> intVal);
631 if ( isOK ) _edgeIDs.push_back( intVal );
633 isOK = static_cast<bool>(load >> _objEntry);
639 //=============================================================================
643 //=============================================================================
645 ostream & operator <<(ostream & save, StdMeshers_NumberOfSegments & hyp)
647 return hyp.SaveTo( save );
650 //=============================================================================
654 //=============================================================================
656 istream & operator >>(istream & load, StdMeshers_NumberOfSegments & hyp)
658 return hyp.LoadFrom( load );
661 //================================================================================
663 * \brief Initialize number of segments by the mesh built on the geometry
664 * \param theMesh - the built mesh
665 * \param theShape - the geometry of interest
666 * \retval bool - true if parameter values have been successfully defined
668 //================================================================================
670 bool StdMeshers_NumberOfSegments::SetParametersByMesh(const SMESH_Mesh* theMesh,
671 const TopoDS_Shape& theShape)
673 if ( !theMesh || theShape.IsNull() )
676 _numberOfSegments = 0;
677 _distrType = DT_Regular;
680 TopTools_IndexedMapOfShape edgeMap;
681 TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
682 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
683 for ( int i = 1; i <= edgeMap.Extent(); ++i )
685 // get current segment length
686 SMESHDS_SubMesh * eSubMesh = aMeshDS->MeshElements( edgeMap( i ));
687 if ( eSubMesh && eSubMesh->NbElements())
688 _numberOfSegments += eSubMesh->NbElements();
693 _numberOfSegments /= nbEdges;
695 if (_numberOfSegments == 0) _numberOfSegments = 1;
699 //================================================================================
701 * \brief Initialize my parameter values by default parameters.
702 * \retval bool - true if parameter values have been successfully defined
704 //================================================================================
706 bool StdMeshers_NumberOfSegments::SetParametersByDefaults(const TDefaults& dflts,
707 const SMESH_Mesh* /*theMesh*/)
709 return (_numberOfSegments = dflts._nbSegments );