1 // Copyright (C) 2007-2016 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 : implementaion 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 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
49 #include <Standard_Failure.hxx>
52 #include <Standard_ErrorHandler.hxx>
55 #include <Basics_Utils.hxx>
57 using namespace StdMeshers;
60 const double PRECISION = 1e-7;
62 //=============================================================================
66 //=============================================================================
68 StdMeshers_NumberOfSegments::StdMeshers_NumberOfSegments(int hypId,
71 : SMESH_Hypothesis(hypId, studyId, gen),
72 _numberOfSegments(15),//issue 19923
73 _distrType(DT_Regular),
75 _convMode(1) //cut negative by default
77 _name = "NumberOfSegments";
81 //=============================================================================
85 //=============================================================================
87 StdMeshers_NumberOfSegments::~StdMeshers_NumberOfSegments()
91 //=============================================================================
95 //=============================================================================
97 StdMeshers_NumberOfSegments::BuildDistributionExpr( const char* expr,int nbSeg,int conv )
98 throw ( SALOME_Exception )
100 if( !buildDistribution( TCollection_AsciiString( ( Standard_CString )expr ), conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
105 const vector<double>&
106 StdMeshers_NumberOfSegments::BuildDistributionTab( const vector<double>& tab,
109 throw ( SALOME_Exception )
111 if( !buildDistribution( tab, conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
116 //=============================================================================
120 //=============================================================================
122 void StdMeshers_NumberOfSegments::SetNumberOfSegments(int segmentsNumber)
123 throw(SALOME_Exception)
125 int oldNumberOfSegments = _numberOfSegments;
126 if (segmentsNumber <= 0)
127 throw SALOME_Exception(LOCALIZED("number of segments must be positive"));
128 _numberOfSegments = segmentsNumber;
130 if (oldNumberOfSegments != _numberOfSegments)
131 NotifySubMeshesHypothesisModification();
134 //=============================================================================
138 //=============================================================================
140 int StdMeshers_NumberOfSegments::GetNumberOfSegments() const
142 return _numberOfSegments;
145 //================================================================================
149 //================================================================================
151 void StdMeshers_NumberOfSegments::SetDistrType(DistrType typ)
152 throw(SALOME_Exception)
154 if (typ < DT_Regular || typ > DT_ExprFunc)
155 throw SALOME_Exception(LOCALIZED("distribution type is out of range"));
157 if (typ != _distrType)
160 NotifySubMeshesHypothesisModification();
164 //================================================================================
168 //================================================================================
170 StdMeshers_NumberOfSegments::DistrType StdMeshers_NumberOfSegments::GetDistrType() const
175 //================================================================================
179 //================================================================================
181 void StdMeshers_NumberOfSegments::SetScaleFactor(double scaleFactor)
182 throw(SALOME_Exception)
184 if (scaleFactor < PRECISION)
185 throw SALOME_Exception(LOCALIZED("scale factor must be positive"));
187 if (_distrType != DT_Scale)
188 _distrType = DT_Scale;
190 // commented by mpa for IPAL 52986
191 // if ( fabs(scaleFactor - 1.0) < PRECISION )
192 // _distrType = DT_Regular;
194 if ( fabs(_scaleFactor - scaleFactor) > PRECISION )
196 _scaleFactor = scaleFactor;
197 NotifySubMeshesHypothesisModification();
201 //================================================================================
205 //================================================================================
207 double StdMeshers_NumberOfSegments::GetScaleFactor() const
208 throw(SALOME_Exception)
210 if (_distrType != DT_Scale)
211 throw SALOME_Exception(LOCALIZED("not a scale distribution"));
215 //================================================================================
219 //================================================================================
221 void StdMeshers_NumberOfSegments::SetTableFunction(const vector<double>& table)
222 throw(SALOME_Exception)
224 if (_distrType != DT_TabFunc)
225 _distrType = DT_TabFunc;
226 //throw SALOME_Exception(LOCALIZED("not a table function distribution"));
227 if ( (table.size() % 2) != 0 )
228 throw SALOME_Exception(LOCALIZED("odd size of vector of table function"));
230 double prev = -PRECISION;
231 bool isSame = table.size() == _table.size();
234 for ( size_t i = 0; i < table.size() / 2; i++ )
236 double par = table[i*2];
237 double val = table[i*2+1];
244 val = pow( 10.0, val );
246 catch(Standard_Failure) {
247 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
248 throw SALOME_Exception( LOCALIZED( "invalid value"));
252 else if( _convMode==1 && val<0.0 )
255 if ( par < 0 || par > 1)
256 throw SALOME_Exception(LOCALIZED("parameter of table function is out of range [0,1]"));
257 if ( fabs(par-prev) < PRECISION )
258 throw SALOME_Exception(LOCALIZED("two parameters are the same"));
260 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
261 if( val > PRECISION )
265 double oldpar = _table[i*2];
266 double oldval = _table[i*2+1];
267 if ( fabs(par - oldpar) > PRECISION || fabs(val - oldval) > PRECISION )
274 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
276 if ( pos && !isSame )
279 NotifySubMeshesHypothesisModification();
283 //================================================================================
287 //================================================================================
289 const vector<double>& StdMeshers_NumberOfSegments::GetTableFunction() const
290 throw(SALOME_Exception)
292 if (_distrType != DT_TabFunc)
293 throw SALOME_Exception(LOCALIZED("not a table function distribution"));
297 //================================================================================
298 /*! check if only 't' is unknown variable in expression
300 //================================================================================
301 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
303 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
305 return sub->GetName()=="t";
308 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
310 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
311 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
314 if( name->GetName()!="t" )
318 res = isCorrectArg( sub );
323 //================================================================================
324 /*! this function parses the expression 'str' in order to check if syntax is correct
325 * ( result in 'syntax' ) and if only 't' is unknown variable in expression ( result in 'args' )
327 //================================================================================
328 bool process( const TCollection_AsciiString& str, int convMode,
329 bool& syntax, bool& args,
330 bool& non_neg, bool& non_zero,
331 bool& singulars, double& sing_point )
333 Kernel_Utils::Localizer loc;
335 bool parsed_ok = true;
336 Handle( ExprIntrp_GenExp ) myExpr;
341 myExpr = ExprIntrp_GenExp::Create();
342 myExpr->Process( str.ToCString() );
343 } catch(Standard_Failure) {
344 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
350 if( parsed_ok && myExpr->IsDone() )
353 args = isCorrectArg( myExpr->Expression() );
356 bool res = parsed_ok && syntax && args;
366 FunctionExpr f( str.ToCString(), convMode );
368 for( int i=0; i<=max; i++ )
370 double t = double(i)/double(max), val;
371 if( !f.value( t, val ) )
387 return res && non_neg && non_zero && ( !singulars );
390 //================================================================================
394 //================================================================================
396 void StdMeshers_NumberOfSegments::SetExpressionFunction(const char* expr)
397 throw(SALOME_Exception)
399 if (_distrType != DT_ExprFunc)
400 _distrType = DT_ExprFunc;
402 string func = CheckExpressionFunction( expr, _convMode );
406 NotifySubMeshesHypothesisModification();
410 //=======================================================================
411 //function : CheckExpressionFunction
412 //purpose : Checks validity of the expression of the function f(t), e.g. "sin(t)".
413 // In case of validity returns a cleaned expression
414 //=======================================================================
417 StdMeshers_NumberOfSegments::CheckExpressionFunction( const std::string& expr,
419 throw (SALOME_Exception)
421 // remove white spaces
422 TCollection_AsciiString str((Standard_CString)expr.c_str());
428 bool syntax, args, non_neg, singulars, non_zero;
430 bool res = process( str, convMode, syntax, args, non_neg, non_zero, singulars, sing_point );
434 throw SALOME_Exception(SMESH_Comment("invalid expression syntax: ") << str );
436 throw SALOME_Exception(LOCALIZED("only 't' may be used as function argument"));
438 throw SALOME_Exception(LOCALIZED("only non-negative function can be used"));
442 sprintf( buf, "Function has singular point in %.3f", sing_point );
443 throw SALOME_Exception( buf );
446 throw SALOME_Exception(LOCALIZED("f(t)=0 cannot be used"));
449 return str.ToCString();
452 //================================================================================
456 //================================================================================
458 const char* StdMeshers_NumberOfSegments::GetExpressionFunction() const
459 throw(SALOME_Exception)
461 if (_distrType != DT_ExprFunc)
462 throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
463 return _func.c_str();
466 //================================================================================
470 //================================================================================
472 void StdMeshers_NumberOfSegments::SetConversionMode( int conv )
473 throw(SALOME_Exception)
475 // if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
476 // throw SALOME_Exception(LOCALIZED("not a functional distribution"));
478 if( conv != _convMode )
481 NotifySubMeshesHypothesisModification();
485 //================================================================================
489 //================================================================================
491 int StdMeshers_NumberOfSegments::ConversionMode() const
492 throw(SALOME_Exception)
494 // if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
495 // throw SALOME_Exception(LOCALIZED("not a functional distribution"));
499 //=============================================================================
503 //=============================================================================
505 ostream & StdMeshers_NumberOfSegments::SaveTo(ostream & save)
507 int listSize = _edgeIDs.size();
508 save << _numberOfSegments << " " << (int)_distrType;
512 save << " " << _scaleFactor;
515 save << " " << _table.size();
516 for ( size_t i = 0; i < _table.size(); i++ )
517 save << " " << _table[i];
520 save << " " << _func;
527 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
528 save << " " << _convMode;
530 if ( _distrType != DT_Regular && listSize > 0 ) {
531 save << " " << listSize;
532 for ( int i = 0; i < listSize; i++ )
533 save << " " << _edgeIDs[i];
534 save << " " << _objEntry;
540 //=============================================================================
544 //=============================================================================
546 istream & StdMeshers_NumberOfSegments::LoadFrom(istream & load)
551 // read number of segments
552 isOK = static_cast<bool>(load >> a);
554 _numberOfSegments = a;
556 load.clear(ios::badbit | load.rdstate());
558 // read second stored value. It can be two variants here:
559 // 1. If the hypothesis is stored in old format (nb.segments and scale factor),
560 // we wait here the scale factor, which is double.
561 // 2. If the hypothesis is stored in new format
562 // (nb.segments, distr.type, some other params.),
563 // we wait here the ditribution type, which is integer
565 isOK = static_cast<bool>(load >> scale_factor);
566 a = (int)scale_factor;
568 // try to interprete ditribution type,
569 // supposing that this hypothesis was written in the new format
572 if (a < DT_Regular || a > DT_ExprFunc)
573 _distrType = DT_Regular;
575 _distrType = (DistrType) a;
578 load.clear(ios::badbit | load.rdstate());
580 // parameters of distribution
586 isOK = static_cast<bool>(load >> b);
591 load.clear(ios::badbit | load.rdstate());
592 // this can mean, that the hypothesis is stored in old format
593 _distrType = DT_Regular;
594 _scaleFactor = scale_factor;
600 isOK = static_cast<bool>(load >> a);
603 _table.resize(a, 0.);
604 for ( size_t i=0; i < _table.size(); i++ )
606 isOK = static_cast<bool>(load >> b);
610 load.clear(ios::badbit | load.rdstate());
615 load.clear(ios::badbit | load.rdstate());
616 // this can mean, that the hypothesis is stored in old format
617 _distrType = DT_Regular;
618 _scaleFactor = scale_factor;
625 isOK = static_cast<bool>(load >> str);
630 load.clear(ios::badbit | load.rdstate());
631 // this can mean, that the hypothesis is stored in old format
632 _distrType = DT_Regular;
633 _scaleFactor = scale_factor;
642 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
644 isOK = static_cast<bool>(load >> a);
648 load.clear(ios::badbit | load.rdstate());
651 // load reversed edges IDs
653 isOK = static_cast<bool>(load >> intVal);
654 if ( isOK && _distrType != DT_Regular && intVal > 0 ) {
655 _edgeIDs.reserve( intVal );
656 for ( size_t i = 0; i < _edgeIDs.capacity() && isOK; i++) {
657 isOK = static_cast<bool>(load >> intVal);
658 if ( isOK ) _edgeIDs.push_back( intVal );
660 isOK = static_cast<bool>(load >> _objEntry);
666 //=============================================================================
670 //=============================================================================
672 ostream & operator <<(ostream & save, StdMeshers_NumberOfSegments & hyp)
674 return hyp.SaveTo( save );
677 //=============================================================================
681 //=============================================================================
683 istream & operator >>(istream & load, StdMeshers_NumberOfSegments & hyp)
685 return hyp.LoadFrom( load );
688 //================================================================================
690 * \brief Initialize number of segments by the mesh built on the geometry
691 * \param theMesh - the built mesh
692 * \param theShape - the geometry of interest
693 * \retval bool - true if parameter values have been successfully defined
695 //================================================================================
697 bool StdMeshers_NumberOfSegments::SetParametersByMesh(const SMESH_Mesh* theMesh,
698 const TopoDS_Shape& theShape)
700 if ( !theMesh || theShape.IsNull() )
703 _numberOfSegments = 0;
704 _distrType = DT_Regular;
707 TopTools_IndexedMapOfShape edgeMap;
708 TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
709 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
710 for ( int i = 1; i <= edgeMap.Extent(); ++i )
712 // get current segment length
713 SMESHDS_SubMesh * eSubMesh = aMeshDS->MeshElements( edgeMap( i ));
714 if ( eSubMesh && eSubMesh->NbElements())
715 _numberOfSegments += eSubMesh->NbElements();
720 _numberOfSegments /= nbEdges;
722 if (_numberOfSegments == 0) _numberOfSegments = 1;
726 //================================================================================
728 * \brief Initialize my parameter values by default parameters.
729 * \retval bool - true if parameter values have been successfully defined
731 //================================================================================
733 bool StdMeshers_NumberOfSegments::SetParametersByDefaults(const TDefaults& dflts,
734 const SMESH_Mesh* /*theMesh*/)
736 return (_numberOfSegments = dflts._nbSegments );
739 //=============================================================================
743 //=============================================================================
745 void StdMeshers_NumberOfSegments::SetReversedEdges( std::vector<int>& ids )
747 if ( ids != _edgeIDs ) {
750 NotifySubMeshesHypothesisModification();