1 // Copyright (C) 2007-2012 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.
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 "StdMeshers_Distribution.hxx"
32 #include "SMESHDS_SubMesh.hxx"
33 #include "SMESH_Mesh.hxx"
34 #include "SMESH_Comment.hxx"
36 #include <ExprIntrp_GenExp.hxx>
37 #include <Expr_Array1OfNamedUnknown.hxx>
38 #include <Expr_NamedUnknown.hxx>
39 #include <TColStd_Array1OfReal.hxx>
40 #include <TCollection_AsciiString.hxx>
42 #include <TopTools_IndexedMapOfShape.hxx>
44 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
48 #include <Standard_Failure.hxx>
51 #include <Standard_ErrorHandler.hxx>
54 #include <Basics_Utils.hxx>
58 const double PRECISION = 1e-7;
60 //=============================================================================
64 //=============================================================================
66 StdMeshers_NumberOfSegments::StdMeshers_NumberOfSegments(int hypId,
69 : SMESH_Hypothesis(hypId, studyId, gen),
70 _numberOfSegments(15),//issue 19923
71 _distrType(DT_Regular),
73 _convMode(1) //cut negative by default
75 _name = "NumberOfSegments";
79 //=============================================================================
83 //=============================================================================
85 StdMeshers_NumberOfSegments::~StdMeshers_NumberOfSegments()
89 //=============================================================================
93 //=============================================================================
95 StdMeshers_NumberOfSegments::BuildDistributionExpr( const char* expr,int nbSeg,int conv )
96 throw ( SALOME_Exception )
98 if( !buildDistribution( TCollection_AsciiString( ( Standard_CString )expr ), conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
103 const vector<double>&
104 StdMeshers_NumberOfSegments::BuildDistributionTab( const vector<double>& tab,
107 throw ( SALOME_Exception )
109 if( !buildDistribution( tab, conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
114 //=============================================================================
118 //=============================================================================
120 void StdMeshers_NumberOfSegments::SetNumberOfSegments(int segmentsNumber)
121 throw(SALOME_Exception)
123 int oldNumberOfSegments = _numberOfSegments;
124 if (segmentsNumber <= 0)
125 throw SALOME_Exception(LOCALIZED("number of segments must be positive"));
126 _numberOfSegments = segmentsNumber;
128 if (oldNumberOfSegments != _numberOfSegments)
129 NotifySubMeshesHypothesisModification();
132 //=============================================================================
136 //=============================================================================
138 int StdMeshers_NumberOfSegments::GetNumberOfSegments() const
140 return _numberOfSegments;
143 //================================================================================
147 //================================================================================
149 void StdMeshers_NumberOfSegments::SetDistrType(DistrType typ)
150 throw(SALOME_Exception)
152 if (typ < DT_Regular || typ > DT_ExprFunc)
153 throw SALOME_Exception(LOCALIZED("distribution type is out of range"));
155 if (typ != _distrType)
158 NotifySubMeshesHypothesisModification();
162 //================================================================================
166 //================================================================================
168 StdMeshers_NumberOfSegments::DistrType StdMeshers_NumberOfSegments::GetDistrType() const
173 //================================================================================
177 //================================================================================
179 void StdMeshers_NumberOfSegments::SetScaleFactor(double scaleFactor)
180 throw(SALOME_Exception)
182 if (_distrType != DT_Scale)
183 _distrType = DT_Scale;
184 //throw SALOME_Exception(LOCALIZED("not a scale distribution"));
185 if (scaleFactor < PRECISION)
186 throw SALOME_Exception(LOCALIZED("scale factor must be positive"));
187 //if (fabs(scaleFactor - 1.0) < PRECISION)
188 // throw SALOME_Exception(LOCALIZED("scale factor must not be equal to 1"));
190 if (fabs(_scaleFactor - scaleFactor) > PRECISION)
192 _scaleFactor = scaleFactor;
193 NotifySubMeshesHypothesisModification();
197 //================================================================================
201 //================================================================================
203 double StdMeshers_NumberOfSegments::GetScaleFactor() const
204 throw(SALOME_Exception)
206 if (_distrType != DT_Scale)
207 throw SALOME_Exception(LOCALIZED("not a scale distribution"));
211 //================================================================================
215 //================================================================================
217 void StdMeshers_NumberOfSegments::SetTableFunction(const vector<double>& table)
218 throw(SALOME_Exception)
220 if (_distrType != DT_TabFunc)
221 _distrType = DT_TabFunc;
222 //throw SALOME_Exception(LOCALIZED("not a table function distribution"));
223 if ( (table.size() % 2) != 0 )
224 throw SALOME_Exception(LOCALIZED("odd size of vector of table function"));
227 double prev = -PRECISION;
228 bool isSame = table.size() == _table.size();
231 for (i=0; i < table.size()/2; i++) {
232 double par = table[i*2];
233 double val = table[i*2+1];
240 val = pow( 10.0, val );
241 } catch(Standard_Failure) {
242 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
243 throw SALOME_Exception( LOCALIZED( "invalid value"));
247 else if( _convMode==1 && val<0.0 )
250 if ( par<0 || par > 1)
251 throw SALOME_Exception(LOCALIZED("parameter of table function is out of range [0,1]"));
252 if ( fabs(par-prev)<PRECISION )
253 throw SALOME_Exception(LOCALIZED("two parameters are the same"));
255 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
260 double oldpar = _table[i*2];
261 double oldval = _table[i*2+1];
262 if (fabs(par - oldpar) > PRECISION || fabs(val - oldval) > PRECISION)
269 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
274 NotifySubMeshesHypothesisModification();
278 //================================================================================
282 //================================================================================
284 const vector<double>& StdMeshers_NumberOfSegments::GetTableFunction() const
285 throw(SALOME_Exception)
287 if (_distrType != DT_TabFunc)
288 throw SALOME_Exception(LOCALIZED("not a table function distribution"));
292 //================================================================================
293 /*! check if only 't' is unknown variable in expression
295 //================================================================================
296 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
298 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
300 return sub->GetName()=="t";
303 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
305 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
306 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
309 if( name->GetName()!="t" )
313 res = isCorrectArg( sub );
318 //================================================================================
319 /*! this function parses the expression 'str' in order to check if syntax is correct
320 * ( result in 'syntax' ) and if only 't' is unknown variable in expression ( result in 'args' )
322 //================================================================================
323 bool process( const TCollection_AsciiString& str, int convMode,
324 bool& syntax, bool& args,
325 bool& non_neg, bool& non_zero,
326 bool& singulars, double& sing_point )
328 Kernel_Utils::Localizer loc;
330 bool parsed_ok = true;
331 Handle( ExprIntrp_GenExp ) myExpr;
336 myExpr = ExprIntrp_GenExp::Create();
337 myExpr->Process( str.ToCString() );
338 } catch(Standard_Failure) {
339 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
345 if( parsed_ok && myExpr->IsDone() )
348 args = isCorrectArg( myExpr->Expression() );
351 bool res = parsed_ok && syntax && args;
361 FunctionExpr f( str.ToCString(), convMode );
363 for( int i=0; i<=max; i++ )
365 double t = double(i)/double(max), val;
366 if( !f.value( t, val ) )
382 return res && non_neg && non_zero && ( !singulars );
385 //================================================================================
389 //================================================================================
391 void StdMeshers_NumberOfSegments::SetExpressionFunction(const char* expr)
392 throw(SALOME_Exception)
394 if (_distrType != DT_ExprFunc)
395 _distrType = DT_ExprFunc;
396 //throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
398 string func = CheckExpressionFunction( expr, _convMode );
402 NotifySubMeshesHypothesisModification();
406 //=======================================================================
407 //function : CheckExpressionFunction
408 //purpose : Checks validity of the expression of the function f(t), e.g. "sin(t)".
409 // In case of validity returns a cleaned expression
410 //=======================================================================
413 StdMeshers_NumberOfSegments::CheckExpressionFunction( const std::string& expr,
415 throw (SALOME_Exception)
417 // remove white spaces
418 TCollection_AsciiString str((Standard_CString)expr.c_str());
424 bool syntax, args, non_neg, singulars, non_zero;
426 bool res = process( str, convMode, syntax, args, non_neg, non_zero, singulars, sing_point );
430 throw SALOME_Exception(SMESH_Comment("invalid expression syntax: ") << str );
432 throw SALOME_Exception(LOCALIZED("only 't' may be used as function argument"));
434 throw SALOME_Exception(LOCALIZED("only non-negative function can be used"));
438 sprintf( buf, "Function has singular point in %.3f", sing_point );
439 throw SALOME_Exception( buf );
442 throw SALOME_Exception(LOCALIZED("f(t)=0 cannot be used"));
445 return str.ToCString();
448 //================================================================================
452 //================================================================================
454 const char* StdMeshers_NumberOfSegments::GetExpressionFunction() const
455 throw(SALOME_Exception)
457 if (_distrType != DT_ExprFunc)
458 throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
459 return _func.c_str();
462 //================================================================================
466 //================================================================================
468 void StdMeshers_NumberOfSegments::SetConversionMode( int conv )
469 throw(SALOME_Exception)
471 // if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
472 // throw SALOME_Exception(LOCALIZED("not a functional distribution"));
474 if( conv != _convMode )
477 NotifySubMeshesHypothesisModification();
481 //================================================================================
485 //================================================================================
487 int StdMeshers_NumberOfSegments::ConversionMode() const
488 throw(SALOME_Exception)
490 // if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
491 // throw SALOME_Exception(LOCALIZED("not a functional distribution"));
495 //=============================================================================
499 //=============================================================================
501 ostream & StdMeshers_NumberOfSegments::SaveTo(ostream & save)
503 int listSize = _edgeIDs.size();
504 save << _numberOfSegments << " " << (int)_distrType;
508 save << " " << _scaleFactor;
512 save << " " << _table.size();
513 for (i=0; i < _table.size(); i++)
514 save << " " << _table[i];
517 save << " " << _func;
524 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
525 save << " " << _convMode;
527 if ( _distrType != DT_Regular && listSize > 0 ) {
528 save << " " << listSize;
529 for ( int i = 0; i < listSize; i++ )
530 save << " " << _edgeIDs[i];
531 save << " " << _objEntry;
537 //=============================================================================
541 //=============================================================================
543 istream & StdMeshers_NumberOfSegments::LoadFrom(istream & load)
548 // read number of segments
551 _numberOfSegments = a;
553 load.clear(ios::badbit | load.rdstate());
555 // read second stored value. It can be two variants here:
556 // 1. If the hypothesis is stored in old format (nb.segments and scale factor),
557 // we wait here the scale factor, which is double.
558 // 2. If the hypothesis is stored in new format
559 // (nb.segments, distr.type, some other params.),
560 // we wait here the ditribution type, which is integer
562 isOK = (load >> scale_factor);
563 a = (int)scale_factor;
565 // try to interprete ditribution type,
566 // supposing that this hypothesis was written in the new format
569 if (a < DT_Regular || a > DT_ExprFunc)
570 _distrType = DT_Regular;
572 _distrType = (DistrType) a;
575 load.clear(ios::badbit | load.rdstate());
577 // parameters of distribution
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;
600 _table.resize(a, 0.);
602 for (i=0; i < _table.size(); i++)
608 load.clear(ios::badbit | load.rdstate());
613 load.clear(ios::badbit | load.rdstate());
614 // this can mean, that the hypothesis is stored in old format
615 _distrType = DT_Regular;
616 _scaleFactor = scale_factor;
623 isOK = (load >> str);
628 load.clear(ios::badbit | load.rdstate());
629 // this can mean, that the hypothesis is stored in old format
630 _distrType = DT_Regular;
631 _scaleFactor = scale_factor;
640 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
646 load.clear(ios::badbit | load.rdstate());
649 // load reversed edges IDs
651 isOK = (load >> intVal);
652 if ( isOK && _distrType != DT_Regular && intVal > 0 ) {
653 _edgeIDs.reserve( intVal );
654 for (int i = 0; i < _edgeIDs.capacity() && isOK; i++) {
655 isOK = (load >> intVal);
656 if ( isOK ) _edgeIDs.push_back( intVal );
658 isOK = (load >> _objEntry);
664 //=============================================================================
668 //=============================================================================
670 ostream & operator <<(ostream & save, StdMeshers_NumberOfSegments & hyp)
672 return hyp.SaveTo( save );
675 //=============================================================================
679 //=============================================================================
681 istream & operator >>(istream & load, StdMeshers_NumberOfSegments & hyp)
683 return hyp.LoadFrom( load );
686 //================================================================================
688 * \brief Initialize number of segments by the mesh built on the geometry
689 * \param theMesh - the built mesh
690 * \param theShape - the geometry of interest
691 * \retval bool - true if parameter values have been successfully defined
693 //================================================================================
695 bool StdMeshers_NumberOfSegments::SetParametersByMesh(const SMESH_Mesh* theMesh,
696 const TopoDS_Shape& theShape)
698 if ( !theMesh || theShape.IsNull() )
701 _numberOfSegments = 0;
702 _distrType = DT_Regular;
705 TopTools_IndexedMapOfShape edgeMap;
706 TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
707 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
708 for ( int i = 1; i <= edgeMap.Extent(); ++i )
710 // get current segment length
711 SMESHDS_SubMesh * eSubMesh = aMeshDS->MeshElements( edgeMap( i ));
712 if ( eSubMesh && eSubMesh->NbElements())
713 _numberOfSegments += eSubMesh->NbElements();
718 _numberOfSegments /= nbEdges;
720 if (_numberOfSegments == 0) _numberOfSegments = 1;
724 //================================================================================
726 * \brief Initialize my parameter values by default parameters.
727 * \retval bool - true if parameter values have been successfully defined
729 //================================================================================
731 bool StdMeshers_NumberOfSegments::SetParametersByDefaults(const TDefaults& dflts,
732 const SMESH_Mesh* /*theMesh*/)
734 return (_numberOfSegments = dflts._nbSegments );
737 //=============================================================================
741 //=============================================================================
743 void StdMeshers_NumberOfSegments::SetReversedEdges( std::vector<int>& ids )
745 if ( ids != _edgeIDs ) {
748 NotifySubMeshesHypothesisModification();