1 // Copyright (C) 2007-2008 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
22 // SMESH SMESH : implementaion of SMESH idl descriptions
23 // File : StdMeshers_NumberOfSegments.cxx
24 // Moved here from SMESH_NumberOfSegments.cxx
25 // Author : Paul RASCLE, EDF
29 #include "StdMeshers_NumberOfSegments.hxx"
31 #include "StdMeshers_Distribution.hxx"
32 #include "SMESHDS_SubMesh.hxx"
33 #include "SMESH_Mesh.hxx"
35 #include <ExprIntrp_GenExp.hxx>
36 #include <Expr_Array1OfNamedUnknown.hxx>
37 #include <Expr_NamedUnknown.hxx>
38 #include <TColStd_Array1OfReal.hxx>
39 #include <TCollection_AsciiString.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
43 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
47 #include <Standard_Failure.hxx>
50 #include <Standard_ErrorHandler.hxx>
55 const double PRECISION = 1e-7;
57 //=============================================================================
61 //=============================================================================
63 StdMeshers_NumberOfSegments::StdMeshers_NumberOfSegments(int hypId,
66 : SMESH_Hypothesis(hypId, studyId, gen),
68 _distrType(DT_Regular),
70 _convMode(1) //cut negative by default
72 _name = "NumberOfSegments";
76 //=============================================================================
80 //=============================================================================
82 StdMeshers_NumberOfSegments::~StdMeshers_NumberOfSegments()
86 //=============================================================================
90 //=============================================================================
92 StdMeshers_NumberOfSegments::BuildDistributionExpr( const char* expr,int nbSeg,int conv )
93 throw ( SALOME_Exception )
95 if( !buildDistribution( TCollection_AsciiString( ( Standard_CString )expr ), conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
100 const vector<double>&
101 StdMeshers_NumberOfSegments::BuildDistributionTab( const vector<double>& tab,
104 throw ( SALOME_Exception )
106 if( !buildDistribution( tab, conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
111 //=============================================================================
115 //=============================================================================
117 void StdMeshers_NumberOfSegments::SetNumberOfSegments(int segmentsNumber)
118 throw(SALOME_Exception)
120 int oldNumberOfSegments = _numberOfSegments;
121 if (segmentsNumber <= 0)
122 throw SALOME_Exception(LOCALIZED("number of segments must be positive"));
123 _numberOfSegments = segmentsNumber;
125 if (oldNumberOfSegments != _numberOfSegments)
126 NotifySubMeshesHypothesisModification();
129 //=============================================================================
133 //=============================================================================
135 int StdMeshers_NumberOfSegments::GetNumberOfSegments() const
137 return _numberOfSegments;
140 //================================================================================
144 //================================================================================
146 void StdMeshers_NumberOfSegments::SetDistrType(DistrType typ)
147 throw(SALOME_Exception)
149 if (typ < DT_Regular || typ > DT_ExprFunc)
150 throw SALOME_Exception(LOCALIZED("distribution type is out of range"));
152 if (typ != _distrType)
155 NotifySubMeshesHypothesisModification();
159 //================================================================================
163 //================================================================================
165 StdMeshers_NumberOfSegments::DistrType StdMeshers_NumberOfSegments::GetDistrType() const
170 //================================================================================
174 //================================================================================
176 void StdMeshers_NumberOfSegments::SetScaleFactor(double scaleFactor)
177 throw(SALOME_Exception)
179 if (_distrType != DT_Scale)
180 _distrType = DT_Scale;
181 //throw SALOME_Exception(LOCALIZED("not a scale distribution"));
182 if (scaleFactor < PRECISION)
183 throw SALOME_Exception(LOCALIZED("scale factor must be positive"));
184 //if (fabs(scaleFactor - 1.0) < PRECISION)
185 // throw SALOME_Exception(LOCALIZED("scale factor must not be equal to 1"));
187 if (fabs(_scaleFactor - scaleFactor) > PRECISION)
189 _scaleFactor = scaleFactor;
190 NotifySubMeshesHypothesisModification();
194 //================================================================================
198 //================================================================================
200 double StdMeshers_NumberOfSegments::GetScaleFactor() const
201 throw(SALOME_Exception)
203 if (_distrType != DT_Scale)
204 throw SALOME_Exception(LOCALIZED("not a scale distribution"));
208 //================================================================================
212 //================================================================================
214 void StdMeshers_NumberOfSegments::SetTableFunction(const vector<double>& table)
215 throw(SALOME_Exception)
217 if (_distrType != DT_TabFunc)
218 _distrType = DT_TabFunc;
219 //throw SALOME_Exception(LOCALIZED("not a table function distribution"));
220 if ( (table.size() % 2) != 0 )
221 throw SALOME_Exception(LOCALIZED("odd size of vector of table function"));
224 double prev = -PRECISION;
225 bool isSame = table.size() == _table.size();
228 for (i=0; i < table.size()/2; i++) {
229 double par = table[i*2];
230 double val = table[i*2+1];
237 val = pow( 10.0, val );
238 } catch(Standard_Failure) {
239 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
240 throw SALOME_Exception( LOCALIZED( "invalid value"));
244 else if( _convMode==1 && val<0.0 )
247 if ( par<0 || par > 1)
248 throw SALOME_Exception(LOCALIZED("parameter of table function is out of range [0,1]"));
249 if ( fabs(par-prev)<PRECISION )
250 throw SALOME_Exception(LOCALIZED("two parameters are the same"));
252 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
257 double oldpar = _table[i*2];
258 double oldval = _table[i*2+1];
259 if (fabs(par - oldpar) > PRECISION || fabs(val - oldval) > PRECISION)
266 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
271 NotifySubMeshesHypothesisModification();
275 //================================================================================
279 //================================================================================
281 const vector<double>& StdMeshers_NumberOfSegments::GetTableFunction() const
282 throw(SALOME_Exception)
284 if (_distrType != DT_TabFunc)
285 throw SALOME_Exception(LOCALIZED("not a table function distribution"));
289 //================================================================================
290 /*! check if only 't' is unknown variable in expression
292 //================================================================================
293 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
295 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
297 return sub->GetName()=="t";
300 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
302 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
303 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
306 if( name->GetName()!="t" )
310 res = isCorrectArg( sub );
315 //================================================================================
316 /*! this function parses the expression 'str' in order to check if syntax is correct
317 * ( result in 'syntax' ) and if only 't' is unknown variable in expression ( result in 'args' )
319 //================================================================================
320 bool process( const TCollection_AsciiString& str, int convMode,
321 bool& syntax, bool& args,
322 bool& non_neg, bool& non_zero,
323 bool& singulars, double& sing_point )
325 bool parsed_ok = true;
326 Handle( ExprIntrp_GenExp ) myExpr;
331 myExpr = ExprIntrp_GenExp::Create();
332 myExpr->Process( str.ToCString() );
333 } catch(Standard_Failure) {
334 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
340 if( parsed_ok && myExpr->IsDone() )
343 args = isCorrectArg( myExpr->Expression() );
346 bool res = parsed_ok && syntax && args;
356 FunctionExpr f( str.ToCString(), convMode );
358 for( int i=0; i<=max; i++ )
360 double t = double(i)/double(max), val;
361 if( !f.value( t, val ) )
376 return res && non_neg && non_zero && ( !singulars );
379 //================================================================================
383 //================================================================================
385 void StdMeshers_NumberOfSegments::SetExpressionFunction(const char* expr)
386 throw(SALOME_Exception)
388 if (_distrType != DT_ExprFunc)
389 _distrType = DT_ExprFunc;
390 //throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
392 // remove white spaces
393 TCollection_AsciiString str((Standard_CString)expr);
399 bool syntax, args, non_neg, singulars, non_zero;
401 bool res = process( str, _convMode, syntax, args, non_neg, non_zero, singulars, sing_point );
405 throw SALOME_Exception(LOCALIZED("invalid expression syntax"));
407 throw SALOME_Exception(LOCALIZED("only 't' may be used as function argument"));
409 throw SALOME_Exception(LOCALIZED("only non-negative function can be used as density"));
413 sprintf( buf, "Function has singular point in %.3f", sing_point );
414 throw SALOME_Exception( buf );
417 throw SALOME_Exception(LOCALIZED("f(t)=0 cannot be used as density"));
426 NotifySubMeshesHypothesisModification();
430 //================================================================================
434 //================================================================================
436 const char* StdMeshers_NumberOfSegments::GetExpressionFunction() const
437 throw(SALOME_Exception)
439 if (_distrType != DT_ExprFunc)
440 throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
441 return _func.c_str();
444 //================================================================================
448 //================================================================================
450 void StdMeshers_NumberOfSegments::SetConversionMode( int conv )
451 throw(SALOME_Exception)
453 // if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
454 // throw SALOME_Exception(LOCALIZED("not a functional distribution"));
456 if( conv != _convMode )
459 NotifySubMeshesHypothesisModification();
463 //================================================================================
467 //================================================================================
469 int StdMeshers_NumberOfSegments::ConversionMode() const
470 throw(SALOME_Exception)
472 // if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
473 // throw SALOME_Exception(LOCALIZED("not a functional distribution"));
477 //=============================================================================
481 //=============================================================================
483 ostream & StdMeshers_NumberOfSegments::SaveTo(ostream & save)
485 save << _numberOfSegments << " " << (int)_distrType;
489 save << " " << _scaleFactor;
493 save << " " << _table.size();
494 for (i=0; i < _table.size(); i++)
495 save << " " << _table[i];
498 save << " " << _func;
505 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
506 save << " " << _convMode;
511 //=============================================================================
515 //=============================================================================
517 istream & StdMeshers_NumberOfSegments::LoadFrom(istream & load)
522 // read number of segments
525 _numberOfSegments = a;
527 load.clear(ios::badbit | load.rdstate());
529 // read second stored value. It can be two variants here:
530 // 1. If the hypothesis is stored in old format (nb.segments and scale factor),
531 // we wait here the scale factor, which is double.
532 // 2. If the hypothesis is stored in new format
533 // (nb.segments, distr.type, some other params.),
534 // we wait here the ditribution type, which is integer
536 isOK = (load >> scale_factor);
537 a = (int)scale_factor;
539 // try to interprete ditribution type,
540 // supposing that this hypothesis was written in the new format
543 if (a < DT_Regular || a > DT_ExprFunc)
544 _distrType = DT_Regular;
546 _distrType = (DistrType) a;
549 load.clear(ios::badbit | load.rdstate());
551 // parameters of distribution
562 load.clear(ios::badbit | load.rdstate());
563 // this can mean, that the hypothesis is stored in old format
564 _distrType = DT_Regular;
565 _scaleFactor = scale_factor;
574 _table.resize(a, 0.);
576 for (i=0; i < _table.size(); i++)
582 load.clear(ios::badbit | load.rdstate());
587 load.clear(ios::badbit | load.rdstate());
588 // this can mean, that the hypothesis is stored in old format
589 _distrType = DT_Regular;
590 _scaleFactor = scale_factor;
597 isOK = (load >> str);
602 load.clear(ios::badbit | load.rdstate());
603 // this can mean, that the hypothesis is stored in old format
604 _distrType = DT_Regular;
605 _scaleFactor = scale_factor;
614 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
620 load.clear(ios::badbit | load.rdstate());
626 //=============================================================================
630 //=============================================================================
632 ostream & operator <<(ostream & save, StdMeshers_NumberOfSegments & hyp)
634 return hyp.SaveTo( save );
637 //=============================================================================
641 //=============================================================================
643 istream & operator >>(istream & load, StdMeshers_NumberOfSegments & hyp)
645 return hyp.LoadFrom( load );
648 //================================================================================
650 * \brief Initialize number of segments by the mesh built on the geometry
651 * \param theMesh - the built mesh
652 * \param theShape - the geometry of interest
653 * \retval bool - true if parameter values have been successfully defined
655 //================================================================================
657 bool StdMeshers_NumberOfSegments::SetParametersByMesh(const SMESH_Mesh* theMesh,
658 const TopoDS_Shape& theShape)
660 if ( !theMesh || theShape.IsNull() )
663 _numberOfSegments = 0;
664 _distrType = DT_Regular;
667 TopTools_IndexedMapOfShape edgeMap;
668 TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
669 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
670 for ( int i = 1; i <= edgeMap.Extent(); ++i )
672 // get current segment length
673 SMESHDS_SubMesh * eSubMesh = aMeshDS->MeshElements( edgeMap( i ));
674 if ( eSubMesh && eSubMesh->NbElements())
675 _numberOfSegments += eSubMesh->NbElements();
680 _numberOfSegments /= nbEdges;