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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 <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>
56 const double PRECISION = 1e-7;
58 //=============================================================================
62 //=============================================================================
64 StdMeshers_NumberOfSegments::StdMeshers_NumberOfSegments(int hypId,
67 : SMESH_Hypothesis(hypId, studyId, gen),
69 _distrType(DT_Regular),
71 _convMode(1) //cut negative by default
73 _name = "NumberOfSegments";
77 //=============================================================================
81 //=============================================================================
83 StdMeshers_NumberOfSegments::~StdMeshers_NumberOfSegments()
87 //=============================================================================
91 //=============================================================================
93 StdMeshers_NumberOfSegments::BuildDistributionExpr( const char* expr,int nbSeg,int conv )
94 throw ( SALOME_Exception )
96 if( !buildDistribution( TCollection_AsciiString( ( Standard_CString )expr ), conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
101 const vector<double>&
102 StdMeshers_NumberOfSegments::BuildDistributionTab( const vector<double>& tab,
105 throw ( SALOME_Exception )
107 if( !buildDistribution( tab, conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
112 //=============================================================================
116 //=============================================================================
118 void StdMeshers_NumberOfSegments::SetNumberOfSegments(int segmentsNumber)
119 throw(SALOME_Exception)
121 int oldNumberOfSegments = _numberOfSegments;
122 if (segmentsNumber <= 0)
123 throw SALOME_Exception(LOCALIZED("number of segments must be positive"));
124 _numberOfSegments = segmentsNumber;
126 if (oldNumberOfSegments != _numberOfSegments)
127 NotifySubMeshesHypothesisModification();
130 //=============================================================================
134 //=============================================================================
136 int StdMeshers_NumberOfSegments::GetNumberOfSegments() const
138 return _numberOfSegments;
141 //================================================================================
145 //================================================================================
147 void StdMeshers_NumberOfSegments::SetDistrType(DistrType typ)
148 throw(SALOME_Exception)
150 if (typ < DT_Regular || typ > DT_ExprFunc)
151 throw SALOME_Exception(LOCALIZED("distribution type is out of range"));
153 if (typ != _distrType)
156 NotifySubMeshesHypothesisModification();
160 //================================================================================
164 //================================================================================
166 StdMeshers_NumberOfSegments::DistrType StdMeshers_NumberOfSegments::GetDistrType() const
171 //================================================================================
175 //================================================================================
177 void StdMeshers_NumberOfSegments::SetScaleFactor(double scaleFactor)
178 throw(SALOME_Exception)
180 if (_distrType != DT_Scale)
181 _distrType = DT_Scale;
182 //throw SALOME_Exception(LOCALIZED("not a scale distribution"));
183 if (scaleFactor < PRECISION)
184 throw SALOME_Exception(LOCALIZED("scale factor must be positive"));
185 //if (fabs(scaleFactor - 1.0) < PRECISION)
186 // throw SALOME_Exception(LOCALIZED("scale factor must not be equal to 1"));
188 if (fabs(_scaleFactor - scaleFactor) > PRECISION)
190 _scaleFactor = scaleFactor;
191 NotifySubMeshesHypothesisModification();
195 //================================================================================
199 //================================================================================
201 double StdMeshers_NumberOfSegments::GetScaleFactor() const
202 throw(SALOME_Exception)
204 if (_distrType != DT_Scale)
205 throw SALOME_Exception(LOCALIZED("not a scale distribution"));
209 //================================================================================
213 //================================================================================
215 void StdMeshers_NumberOfSegments::SetTableFunction(const vector<double>& table)
216 throw(SALOME_Exception)
218 if (_distrType != DT_TabFunc)
219 _distrType = DT_TabFunc;
220 //throw SALOME_Exception(LOCALIZED("not a table function distribution"));
221 if ( (table.size() % 2) != 0 )
222 throw SALOME_Exception(LOCALIZED("odd size of vector of table function"));
225 double prev = -PRECISION;
226 bool isSame = table.size() == _table.size();
229 for (i=0; i < table.size()/2; i++) {
230 double par = table[i*2];
231 double val = table[i*2+1];
238 val = pow( 10.0, val );
239 } catch(Standard_Failure) {
240 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
241 throw SALOME_Exception( LOCALIZED( "invalid value"));
245 else if( _convMode==1 && val<0.0 )
248 if ( par<0 || par > 1)
249 throw SALOME_Exception(LOCALIZED("parameter of table function is out of range [0,1]"));
250 if ( fabs(par-prev)<PRECISION )
251 throw SALOME_Exception(LOCALIZED("two parameters are the same"));
253 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
258 double oldpar = _table[i*2];
259 double oldval = _table[i*2+1];
260 if (fabs(par - oldpar) > PRECISION || fabs(val - oldval) > PRECISION)
267 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
272 NotifySubMeshesHypothesisModification();
276 //================================================================================
280 //================================================================================
282 const vector<double>& StdMeshers_NumberOfSegments::GetTableFunction() const
283 throw(SALOME_Exception)
285 if (_distrType != DT_TabFunc)
286 throw SALOME_Exception(LOCALIZED("not a table function distribution"));
290 //================================================================================
291 /*! check if only 't' is unknown variable in expression
293 //================================================================================
294 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
296 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
298 return sub->GetName()=="t";
301 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
303 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
304 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
307 if( name->GetName()!="t" )
311 res = isCorrectArg( sub );
316 //================================================================================
317 /*! this function parses the expression 'str' in order to check if syntax is correct
318 * ( result in 'syntax' ) and if only 't' is unknown variable in expression ( result in 'args' )
320 //================================================================================
321 bool process( const TCollection_AsciiString& str, int convMode,
322 bool& syntax, bool& args,
323 bool& non_neg, bool& non_zero,
324 bool& singulars, double& sing_point )
326 bool parsed_ok = true;
327 Handle( ExprIntrp_GenExp ) myExpr;
332 myExpr = ExprIntrp_GenExp::Create();
333 myExpr->Process( str.ToCString() );
334 } catch(Standard_Failure) {
335 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
341 if( parsed_ok && myExpr->IsDone() )
344 args = isCorrectArg( myExpr->Expression() );
347 bool res = parsed_ok && syntax && args;
357 FunctionExpr f( str.ToCString(), convMode );
359 for( int i=0; i<=max; i++ )
361 double t = double(i)/double(max), val;
362 if( !f.value( t, val ) )
377 return res && non_neg && non_zero && ( !singulars );
380 //================================================================================
384 //================================================================================
386 void StdMeshers_NumberOfSegments::SetExpressionFunction(const char* expr)
387 throw(SALOME_Exception)
389 if (_distrType != DT_ExprFunc)
390 _distrType = DT_ExprFunc;
391 //throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
393 // remove white spaces
394 TCollection_AsciiString str((Standard_CString)expr);
400 bool syntax, args, non_neg, singulars, non_zero;
402 bool res = process( str, _convMode, syntax, args, non_neg, non_zero, singulars, sing_point );
406 throw SALOME_Exception(LOCALIZED("invalid expression syntax"));
408 throw SALOME_Exception(LOCALIZED("only 't' may be used as function argument"));
410 throw SALOME_Exception(LOCALIZED("only non-negative function can be used as density"));
414 sprintf( buf, "Function has singular point in %.3f", sing_point );
415 throw SALOME_Exception( buf );
418 throw SALOME_Exception(LOCALIZED("f(t)=0 cannot be used as density"));
427 NotifySubMeshesHypothesisModification();
431 //================================================================================
435 //================================================================================
437 const char* StdMeshers_NumberOfSegments::GetExpressionFunction() const
438 throw(SALOME_Exception)
440 if (_distrType != DT_ExprFunc)
441 throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
442 return _func.c_str();
445 //================================================================================
449 //================================================================================
451 void StdMeshers_NumberOfSegments::SetConversionMode( int conv )
452 throw(SALOME_Exception)
454 // if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
455 // throw SALOME_Exception(LOCALIZED("not a functional distribution"));
457 if( conv != _convMode )
460 NotifySubMeshesHypothesisModification();
464 //================================================================================
468 //================================================================================
470 int StdMeshers_NumberOfSegments::ConversionMode() const
471 throw(SALOME_Exception)
473 // if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
474 // throw SALOME_Exception(LOCALIZED("not a functional distribution"));
478 //=============================================================================
482 //=============================================================================
484 ostream & StdMeshers_NumberOfSegments::SaveTo(ostream & save)
486 save << _numberOfSegments << " " << (int)_distrType;
490 save << " " << _scaleFactor;
494 save << " " << _table.size();
495 for (i=0; i < _table.size(); i++)
496 save << " " << _table[i];
499 save << " " << _func;
506 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
507 save << " " << _convMode;
512 //=============================================================================
516 //=============================================================================
518 istream & StdMeshers_NumberOfSegments::LoadFrom(istream & load)
523 // read number of segments
526 _numberOfSegments = a;
528 load.clear(ios::badbit | load.rdstate());
530 // read second stored value. It can be two variants here:
531 // 1. If the hypothesis is stored in old format (nb.segments and scale factor),
532 // we wait here the scale factor, which is double.
533 // 2. If the hypothesis is stored in new format
534 // (nb.segments, distr.type, some other params.),
535 // we wait here the ditribution type, which is integer
537 isOK = (load >> scale_factor);
538 a = (int)scale_factor;
540 // try to interprete ditribution type,
541 // supposing that this hypothesis was written in the new format
544 if (a < DT_Regular || a > DT_ExprFunc)
545 _distrType = DT_Regular;
547 _distrType = (DistrType) a;
550 load.clear(ios::badbit | load.rdstate());
552 // parameters of distribution
563 load.clear(ios::badbit | load.rdstate());
564 // this can mean, that the hypothesis is stored in old format
565 _distrType = DT_Regular;
566 _scaleFactor = scale_factor;
575 _table.resize(a, 0.);
577 for (i=0; i < _table.size(); i++)
583 load.clear(ios::badbit | load.rdstate());
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;
598 isOK = (load >> str);
603 load.clear(ios::badbit | load.rdstate());
604 // this can mean, that the hypothesis is stored in old format
605 _distrType = DT_Regular;
606 _scaleFactor = scale_factor;
615 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
621 load.clear(ios::badbit | load.rdstate());
627 //=============================================================================
631 //=============================================================================
633 ostream & operator <<(ostream & save, StdMeshers_NumberOfSegments & hyp)
635 return hyp.SaveTo( save );
638 //=============================================================================
642 //=============================================================================
644 istream & operator >>(istream & load, StdMeshers_NumberOfSegments & hyp)
646 return hyp.LoadFrom( load );
649 //================================================================================
651 * \brief Initialize number of segments by the mesh built on the geometry
652 * \param theMesh - the built mesh
653 * \param theShape - the geometry of interest
654 * \retval bool - true if parameter values have been successfully defined
656 //================================================================================
658 bool StdMeshers_NumberOfSegments::SetParametersByMesh(const SMESH_Mesh* theMesh,
659 const TopoDS_Shape& theShape)
661 if ( !theMesh || theShape.IsNull() )
664 _numberOfSegments = 0;
665 _distrType = DT_Regular;
668 TopTools_IndexedMapOfShape edgeMap;
669 TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
670 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
671 for ( int i = 1; i <= edgeMap.Extent(); ++i )
673 // get current segment length
674 SMESHDS_SubMesh * eSubMesh = aMeshDS->MeshElements( edgeMap( i ));
675 if ( eSubMesh && eSubMesh->NbElements())
676 _numberOfSegments += eSubMesh->NbElements();
681 _numberOfSegments /= nbEdges;