1 // SMESH SMESH : implementaion of SMESH idl descriptions
3 // Copyright (C) 2003 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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : StdMeshers_NumberOfSegments.cxx
25 // Moved here from SMESH_NumberOfSegments.cxx
26 // Author : Paul RASCLE, EDF
31 #include "StdMeshers_NumberOfSegments.hxx"
32 #include "StdMeshers_Distribution.hxx"
33 #include <Standard_ErrorHandler.hxx>
34 #include <TCollection_AsciiString.hxx>
35 #include <ExprIntrp_GenExp.hxx>
36 #include <Expr_NamedUnknown.hxx>
37 #include <CASCatch_CatchSignals.hxx>
38 #include <CASCatch_Failure.hxx>
39 #include <CASCatch_ErrorHandler.hxx>
41 #include <Expr_Array1OfNamedUnknown.hxx>
42 #include <TColStd_Array1OfReal.hxx>
45 const double PRECISION = 1e-7;
47 //=============================================================================
51 //=============================================================================
53 StdMeshers_NumberOfSegments::StdMeshers_NumberOfSegments(int hypId, int studyId,
55 : SMESH_Hypothesis(hypId, studyId, gen),
57 _distrType(DT_Regular),
59 _convMode(1) //cut negative by default
61 _name = "NumberOfSegments";
65 //=============================================================================
69 //=============================================================================
71 StdMeshers_NumberOfSegments::~StdMeshers_NumberOfSegments()
75 //=============================================================================
79 //=============================================================================
80 const std::vector<double>& StdMeshers_NumberOfSegments::BuildDistributionExpr( const char* expr, int nbSeg, int conv )
81 throw ( SALOME_Exception )
83 if( !buildDistribution( TCollection_AsciiString( ( Standard_CString )expr ), conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
88 const std::vector<double>& StdMeshers_NumberOfSegments::BuildDistributionTab( const std::vector<double>& tab,
90 throw ( SALOME_Exception )
92 if( !buildDistribution( tab, conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
97 //=============================================================================
101 //=============================================================================
103 void StdMeshers_NumberOfSegments::SetNumberOfSegments(int segmentsNumber)
104 throw(SALOME_Exception)
106 int oldNumberOfSegments = _numberOfSegments;
107 if (segmentsNumber <= 0)
109 SALOME_Exception(LOCALIZED("number of segments must be positive"));
110 _numberOfSegments = segmentsNumber;
112 if (oldNumberOfSegments != _numberOfSegments)
113 NotifySubMeshesHypothesisModification();
116 //=============================================================================
120 //=============================================================================
122 int StdMeshers_NumberOfSegments::GetNumberOfSegments() const
124 return _numberOfSegments;
127 //================================================================================
131 //================================================================================
133 void StdMeshers_NumberOfSegments::SetDistrType(DistrType typ)
134 throw(SALOME_Exception)
136 if (typ < DT_Regular || typ > DT_ExprFunc)
137 throw SALOME_Exception(LOCALIZED("distribution type is out of range"));
139 if (typ != _distrType)
142 NotifySubMeshesHypothesisModification();
146 //================================================================================
150 //================================================================================
152 StdMeshers_NumberOfSegments::DistrType StdMeshers_NumberOfSegments::GetDistrType() const
157 //================================================================================
161 //================================================================================
163 void StdMeshers_NumberOfSegments::SetScaleFactor(double scaleFactor)
164 throw(SALOME_Exception)
166 if (_distrType != DT_Scale)
167 throw SALOME_Exception(LOCALIZED("not a scale distribution"));
168 if (scaleFactor < PRECISION)
169 throw SALOME_Exception(LOCALIZED("scale factor must be positive"));
170 if (fabs(scaleFactor - 1.0) < PRECISION)
171 throw SALOME_Exception(LOCALIZED("scale factor must not be equal to 1"));
173 if (fabs(_scaleFactor - scaleFactor) > PRECISION)
175 _scaleFactor = scaleFactor;
176 NotifySubMeshesHypothesisModification();
180 //================================================================================
184 //================================================================================
186 double StdMeshers_NumberOfSegments::GetScaleFactor() const
187 throw(SALOME_Exception)
189 if (_distrType != DT_Scale)
190 throw SALOME_Exception(LOCALIZED("not a scale distribution"));
194 //================================================================================
198 //================================================================================
200 void StdMeshers_NumberOfSegments::SetTableFunction(const std::vector<double>& table)
201 throw(SALOME_Exception)
203 if (_distrType != DT_TabFunc)
204 throw SALOME_Exception(LOCALIZED("not a table function distribution"));
205 if ( (table.size() % 2) != 0 )
206 throw SALOME_Exception(LOCALIZED("odd size of vector of table function"));
209 double prev = -PRECISION;
210 bool isSame = table.size() == _table.size();
212 OSD::SetSignal( true );
213 CASCatch_CatchSignals aCatchSignals;
214 aCatchSignals.Activate();
217 for (i=0; i < table.size()/2; i++) {
218 double par = table[i*2];
219 double val = table[i*2+1];
224 val = pow( 10.0, val );
226 CASCatch_CATCH(CASCatch_Failure)
228 aCatchSignals.Deactivate();
229 Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();
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"));
247 double oldpar = _table[i*2];
248 double oldval = _table[i*2+1];
249 if (fabs(par - oldpar) > PRECISION || fabs(val - oldval) > PRECISION)
254 aCatchSignals.Deactivate();
257 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
262 NotifySubMeshesHypothesisModification();
266 //================================================================================
270 //================================================================================
272 const std::vector<double>& StdMeshers_NumberOfSegments::GetTableFunction() const
273 throw(SALOME_Exception)
275 if (_distrType != DT_TabFunc)
276 throw SALOME_Exception(LOCALIZED("not a table function distribution"));
280 //================================================================================
281 /*! check if only 't' is unknown variable in expression
283 //================================================================================
284 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
286 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
288 return sub->GetName()=="t";
291 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
293 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
294 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
297 if( name->GetName()!="t" )
301 res = isCorrectArg( sub );
306 //================================================================================
307 /*! this function parses the expression 'str' in order to check if syntax is correct
308 * ( result in 'syntax' ) and if only 't' is unknown variable in expression ( result in 'args' )
310 //================================================================================
311 bool process( const TCollection_AsciiString& str, int convMode,
312 bool& syntax, bool& args,
313 bool& non_neg, bool& non_zero,
314 bool& singulars, double& sing_point )
316 OSD::SetSignal( true );
317 CASCatch_CatchSignals aCatchSignals;
318 aCatchSignals.Activate();
320 bool parsed_ok = true;
321 Handle( ExprIntrp_GenExp ) myExpr;
324 myExpr = ExprIntrp_GenExp::Create();
325 myExpr->Process( str.ToCString() );
327 CASCatch_CATCH(CASCatch_Failure)
329 aCatchSignals.Deactivate();
330 Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();
333 aCatchSignals.Deactivate();
337 if( parsed_ok && myExpr->IsDone() )
340 args = isCorrectArg( myExpr->Expression() );
343 bool res = parsed_ok && syntax && args;
353 FunctionExpr f( str.ToCString(), convMode );
355 for( int i=0; i<=max; i++ )
357 double t = double(i)/double(max), val;
358 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 throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
388 // remove white spaces
389 TCollection_AsciiString str((Standard_CString)expr);
395 bool syntax, args, non_neg, singulars, non_zero;
397 bool res = process( str, _convMode, syntax, args, non_neg, non_zero, singulars, sing_point );
401 throw SALOME_Exception(LOCALIZED("invalid expression syntax"));
403 throw SALOME_Exception(LOCALIZED("only 't' may be used as function argument"));
405 throw SALOME_Exception(LOCALIZED("only non-negative function can be used as density"));
409 sprintf( buf, "Function has singular point in %.3f", sing_point );
410 throw SALOME_Exception( buf );
413 throw SALOME_Exception(LOCALIZED("f(t)=0 cannot be used as density"));
418 std::string func = expr;
422 NotifySubMeshesHypothesisModification();
426 //================================================================================
430 //================================================================================
432 const char* StdMeshers_NumberOfSegments::GetExpressionFunction() const
433 throw(SALOME_Exception)
435 if (_distrType != DT_ExprFunc)
436 throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
437 return _func.c_str();
440 //================================================================================
444 //================================================================================
446 void StdMeshers_NumberOfSegments::SetConversionMode( int conv )
447 throw(SALOME_Exception)
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
466 throw(SALOME_Exception)
468 if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
469 throw SALOME_Exception(LOCALIZED("not a functional distribution"));
473 //=============================================================================
477 //=============================================================================
479 ostream & StdMeshers_NumberOfSegments::SaveTo(ostream & save)
481 save << _numberOfSegments << " " << (int)_distrType;
485 save << " " << _scaleFactor;
489 save << " " << _table.size();
490 for (i=0; i < _table.size(); i++)
491 save << " " << _table[i];
494 save << " " << _func;
501 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
502 save << " " << _convMode;
507 //=============================================================================
511 //=============================================================================
513 istream & StdMeshers_NumberOfSegments::LoadFrom(istream & load)
518 // read number of segments
521 _numberOfSegments = a;
523 load.clear(ios::badbit | load.rdstate());
525 // read second stored value. It can be two variants here:
526 // 1. If the hypothesis is stored in old format (nb.segments and scale factor),
527 // we wait here the scale factor, which is double.
528 // 2. If the hypothesis is stored in new format
529 // (nb.segments, distr.type, some other params.),
530 // we wait here the ditribution type, which is integer
532 isOK = (load >> scale_factor);
533 a = (int)scale_factor;
535 // try to interprete ditribution type,
536 // supposing that this hypothesis was written in the new format
539 if (a < DT_Regular || a > DT_ExprFunc)
540 _distrType = DT_Regular;
542 _distrType = (DistrType) a;
545 load.clear(ios::badbit | load.rdstate());
547 // parameters of distribution
558 load.clear(ios::badbit | load.rdstate());
559 // this can mean, that the hypothesis is stored in old format
560 _distrType = DT_Regular;
561 _scaleFactor = scale_factor;
570 _table.resize(a, 0.);
572 for (i=0; i < _table.size(); i++)
578 load.clear(ios::badbit | load.rdstate());
583 load.clear(ios::badbit | load.rdstate());
584 // this can mean, that the hypothesis is stored in old format
585 _distrType = DT_Regular;
586 _scaleFactor = scale_factor;
593 isOK = (load >> str);
598 load.clear(ios::badbit | load.rdstate());
599 // this can mean, that the hypothesis is stored in old format
600 _distrType = DT_Regular;
601 _scaleFactor = scale_factor;
610 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
616 load.clear(ios::badbit | load.rdstate());
622 //=============================================================================
626 //=============================================================================
628 ostream & operator <<(ostream & save, StdMeshers_NumberOfSegments & hyp)
630 return hyp.SaveTo( save );
633 //=============================================================================
637 //=============================================================================
639 istream & operator >>(istream & load, StdMeshers_NumberOfSegments & hyp)
641 return hyp.LoadFrom( load );