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
30 #include "StdMeshers_NumberOfSegments.hxx"
32 #include "StdMeshers_Distribution.hxx"
33 #include "SMESHDS_SubMesh.hxx"
34 #include "SMESH_Mesh.hxx"
36 #include <CASCatch_CatchSignals.hxx>
37 #include <CASCatch_ErrorHandler.hxx>
38 #include <CASCatch_Failure.hxx>
39 #include <ExprIntrp_GenExp.hxx>
40 #include <Expr_Array1OfNamedUnknown.hxx>
41 #include <Expr_NamedUnknown.hxx>
43 #include <TColStd_Array1OfReal.hxx>
44 #include <TCollection_AsciiString.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
48 #include <Standard_ErrorHandler.hxx>
52 const double PRECISION = 1e-7;
54 //=============================================================================
58 //=============================================================================
60 StdMeshers_NumberOfSegments::StdMeshers_NumberOfSegments(int hypId, int studyId,
62 : SMESH_Hypothesis(hypId, studyId, gen),
64 _distrType(DT_Regular),
66 _convMode(1) //cut negative by default
68 _name = "NumberOfSegments";
72 //=============================================================================
76 //=============================================================================
78 StdMeshers_NumberOfSegments::~StdMeshers_NumberOfSegments()
82 //=============================================================================
86 //=============================================================================
87 const std::vector<double>& StdMeshers_NumberOfSegments::BuildDistributionExpr( const char* expr, int nbSeg, int conv )
88 throw ( SALOME_Exception )
90 if( !buildDistribution( TCollection_AsciiString( ( Standard_CString )expr ), conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
95 const std::vector<double>& StdMeshers_NumberOfSegments::BuildDistributionTab( const std::vector<double>& tab,
97 throw ( SALOME_Exception )
99 if( !buildDistribution( tab, conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
104 //=============================================================================
108 //=============================================================================
110 void StdMeshers_NumberOfSegments::SetNumberOfSegments(int segmentsNumber)
111 throw(SALOME_Exception)
113 int oldNumberOfSegments = _numberOfSegments;
114 if (segmentsNumber <= 0)
116 SALOME_Exception(LOCALIZED("number of segments must be positive"));
117 _numberOfSegments = segmentsNumber;
119 if (oldNumberOfSegments != _numberOfSegments)
120 NotifySubMeshesHypothesisModification();
123 //=============================================================================
127 //=============================================================================
129 int StdMeshers_NumberOfSegments::GetNumberOfSegments() const
131 return _numberOfSegments;
134 //================================================================================
138 //================================================================================
140 void StdMeshers_NumberOfSegments::SetDistrType(DistrType typ)
141 throw(SALOME_Exception)
143 if (typ < DT_Regular || typ > DT_ExprFunc)
144 throw SALOME_Exception(LOCALIZED("distribution type is out of range"));
146 if (typ != _distrType)
149 NotifySubMeshesHypothesisModification();
153 //================================================================================
157 //================================================================================
159 StdMeshers_NumberOfSegments::DistrType StdMeshers_NumberOfSegments::GetDistrType() const
164 //================================================================================
168 //================================================================================
170 void StdMeshers_NumberOfSegments::SetScaleFactor(double scaleFactor)
171 throw(SALOME_Exception)
173 if (_distrType != DT_Scale)
174 throw SALOME_Exception(LOCALIZED("not a scale distribution"));
175 if (scaleFactor < PRECISION)
176 throw SALOME_Exception(LOCALIZED("scale factor must be positive"));
177 if (fabs(scaleFactor - 1.0) < PRECISION)
178 throw SALOME_Exception(LOCALIZED("scale factor must not be equal to 1"));
180 if (fabs(_scaleFactor - scaleFactor) > PRECISION)
182 _scaleFactor = scaleFactor;
183 NotifySubMeshesHypothesisModification();
187 //================================================================================
191 //================================================================================
193 double StdMeshers_NumberOfSegments::GetScaleFactor() const
194 throw(SALOME_Exception)
196 if (_distrType != DT_Scale)
197 throw SALOME_Exception(LOCALIZED("not a scale distribution"));
201 //================================================================================
205 //================================================================================
207 void StdMeshers_NumberOfSegments::SetTableFunction(const std::vector<double>& table)
208 throw(SALOME_Exception)
210 if (_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"));
216 double prev = -PRECISION;
217 bool isSame = table.size() == _table.size();
219 OSD::SetSignal( true );
220 CASCatch_CatchSignals aCatchSignals;
221 aCatchSignals.Activate();
224 for (i=0; i < table.size()/2; i++) {
225 double par = table[i*2];
226 double val = table[i*2+1];
231 val = pow( 10.0, val );
233 CASCatch_CATCH(CASCatch_Failure)
235 aCatchSignals.Deactivate();
236 Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();
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"));
254 double oldpar = _table[i*2];
255 double oldval = _table[i*2+1];
256 if (fabs(par - oldpar) > PRECISION || fabs(val - oldval) > PRECISION)
261 aCatchSignals.Deactivate();
264 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
269 NotifySubMeshesHypothesisModification();
273 //================================================================================
277 //================================================================================
279 const std::vector<double>& StdMeshers_NumberOfSegments::GetTableFunction() const
280 throw(SALOME_Exception)
282 if (_distrType != DT_TabFunc)
283 throw SALOME_Exception(LOCALIZED("not a table function distribution"));
287 //================================================================================
288 /*! check if only 't' is unknown variable in expression
290 //================================================================================
291 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
293 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
295 return sub->GetName()=="t";
298 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
300 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
301 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
304 if( name->GetName()!="t" )
308 res = isCorrectArg( sub );
313 //================================================================================
314 /*! this function parses the expression 'str' in order to check if syntax is correct
315 * ( result in 'syntax' ) and if only 't' is unknown variable in expression ( result in 'args' )
317 //================================================================================
318 bool process( const TCollection_AsciiString& str, int convMode,
319 bool& syntax, bool& args,
320 bool& non_neg, bool& non_zero,
321 bool& singulars, double& sing_point )
323 OSD::SetSignal( true );
324 CASCatch_CatchSignals aCatchSignals;
325 aCatchSignals.Activate();
327 bool parsed_ok = true;
328 Handle( ExprIntrp_GenExp ) myExpr;
331 myExpr = ExprIntrp_GenExp::Create();
332 myExpr->Process( str.ToCString() );
334 CASCatch_CATCH(CASCatch_Failure)
336 aCatchSignals.Deactivate();
337 Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();
340 aCatchSignals.Deactivate();
344 if( parsed_ok && myExpr->IsDone() )
347 args = isCorrectArg( myExpr->Expression() );
350 bool res = parsed_ok && syntax && args;
360 FunctionExpr f( str.ToCString(), convMode );
362 for( int i=0; i<=max; i++ )
364 double t = double(i)/double(max), val;
365 if( !f.value( t, val ) )
380 return res && non_neg && non_zero && ( !singulars );
383 //================================================================================
387 //================================================================================
389 void StdMeshers_NumberOfSegments::SetExpressionFunction(const char* expr)
390 throw(SALOME_Exception)
392 if (_distrType != DT_ExprFunc)
393 throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
395 // remove white spaces
396 TCollection_AsciiString str((Standard_CString)expr);
402 bool syntax, args, non_neg, singulars, non_zero;
404 bool res = process( str, _convMode, syntax, args, non_neg, non_zero, singulars, sing_point );
408 throw SALOME_Exception(LOCALIZED("invalid expression syntax"));
410 throw SALOME_Exception(LOCALIZED("only 't' may be used as function argument"));
412 throw SALOME_Exception(LOCALIZED("only non-negative function can be used as density"));
416 sprintf( buf, "Function has singular point in %.3f", sing_point );
417 throw SALOME_Exception( buf );
420 throw SALOME_Exception(LOCALIZED("f(t)=0 cannot be used as density"));
425 std::string func = expr;
429 NotifySubMeshesHypothesisModification();
433 //================================================================================
437 //================================================================================
439 const char* StdMeshers_NumberOfSegments::GetExpressionFunction() const
440 throw(SALOME_Exception)
442 if (_distrType != DT_ExprFunc)
443 throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
444 return _func.c_str();
447 //================================================================================
451 //================================================================================
453 void StdMeshers_NumberOfSegments::SetConversionMode( int conv )
454 throw(SALOME_Exception)
456 if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
457 throw SALOME_Exception(LOCALIZED("not a functional distribution"));
459 if( conv != _convMode )
462 NotifySubMeshesHypothesisModification();
466 //================================================================================
470 //================================================================================
472 int StdMeshers_NumberOfSegments::ConversionMode() const
473 throw(SALOME_Exception)
475 if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
476 throw SALOME_Exception(LOCALIZED("not a functional distribution"));
480 //=============================================================================
484 //=============================================================================
486 ostream & StdMeshers_NumberOfSegments::SaveTo(ostream & save)
488 save << _numberOfSegments << " " << (int)_distrType;
492 save << " " << _scaleFactor;
496 save << " " << _table.size();
497 for (i=0; i < _table.size(); i++)
498 save << " " << _table[i];
501 save << " " << _func;
508 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
509 save << " " << _convMode;
514 //=============================================================================
518 //=============================================================================
520 istream & StdMeshers_NumberOfSegments::LoadFrom(istream & load)
525 // read number of segments
528 _numberOfSegments = a;
530 load.clear(ios::badbit | load.rdstate());
532 // read second stored value. It can be two variants here:
533 // 1. If the hypothesis is stored in old format (nb.segments and scale factor),
534 // we wait here the scale factor, which is double.
535 // 2. If the hypothesis is stored in new format
536 // (nb.segments, distr.type, some other params.),
537 // we wait here the ditribution type, which is integer
539 isOK = (load >> scale_factor);
540 a = (int)scale_factor;
542 // try to interprete ditribution type,
543 // supposing that this hypothesis was written in the new format
546 if (a < DT_Regular || a > DT_ExprFunc)
547 _distrType = DT_Regular;
549 _distrType = (DistrType) a;
552 load.clear(ios::badbit | load.rdstate());
554 // parameters of distribution
565 load.clear(ios::badbit | load.rdstate());
566 // this can mean, that the hypothesis is stored in old format
567 _distrType = DT_Regular;
568 _scaleFactor = scale_factor;
577 _table.resize(a, 0.);
579 for (i=0; i < _table.size(); i++)
585 load.clear(ios::badbit | load.rdstate());
590 load.clear(ios::badbit | load.rdstate());
591 // this can mean, that the hypothesis is stored in old format
592 _distrType = DT_Regular;
593 _scaleFactor = scale_factor;
600 isOK = (load >> str);
605 load.clear(ios::badbit | load.rdstate());
606 // this can mean, that the hypothesis is stored in old format
607 _distrType = DT_Regular;
608 _scaleFactor = scale_factor;
617 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
623 load.clear(ios::badbit | load.rdstate());
629 //=============================================================================
633 //=============================================================================
635 ostream & operator <<(ostream & save, StdMeshers_NumberOfSegments & hyp)
637 return hyp.SaveTo( save );
640 //=============================================================================
644 //=============================================================================
646 istream & operator >>(istream & load, StdMeshers_NumberOfSegments & hyp)
648 return hyp.LoadFrom( load );
651 //================================================================================
653 * \brief Initialize number of segments by the mesh built on the geometry
654 * \param theMesh - the built mesh
655 * \param theShape - the geometry of interest
656 * \retval bool - true if parameter values have been successfully defined
658 //================================================================================
660 bool StdMeshers_NumberOfSegments::SetParametersByMesh(const SMESH_Mesh* theMesh,
661 const TopoDS_Shape& theShape)
663 if ( !theMesh || theShape.IsNull() )
666 _numberOfSegments = 0;
667 _distrType = DT_Regular;
670 TopTools_IndexedMapOfShape edgeMap;
671 TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
672 for ( int i = 1; i <= edgeMap.Extent(); ++i )
674 // get current segment length
675 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
676 SMESHDS_SubMesh * eSubMesh = aMeshDS->MeshElements( edgeMap( i ));
677 if ( eSubMesh && eSubMesh->NbElements())
678 _numberOfSegments += eSubMesh->NbElements();
683 _numberOfSegments /= nbEdges;