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 "CASCatch.hxx"
38 #include <ExprIntrp_GenExp.hxx>
39 #include <Expr_Array1OfNamedUnknown.hxx>
40 #include <Expr_NamedUnknown.hxx>
41 #include <TColStd_Array1OfReal.hxx>
42 #include <TCollection_AsciiString.hxx>
44 #include <TopTools_IndexedMapOfShape.hxx>
48 const double PRECISION = 1e-7;
50 //=============================================================================
54 //=============================================================================
56 StdMeshers_NumberOfSegments::StdMeshers_NumberOfSegments(int hypId, int studyId,
58 : SMESH_Hypothesis(hypId, studyId, gen),
60 _distrType(DT_Regular),
62 _convMode(1) //cut negative by default
64 _name = "NumberOfSegments";
68 //=============================================================================
72 //=============================================================================
74 StdMeshers_NumberOfSegments::~StdMeshers_NumberOfSegments()
78 //=============================================================================
82 //=============================================================================
83 const std::vector<double>& StdMeshers_NumberOfSegments::BuildDistributionExpr( const char* expr, int nbSeg, int conv )
84 throw ( SALOME_Exception )
86 if( !buildDistribution( TCollection_AsciiString( ( Standard_CString )expr ), conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
91 const std::vector<double>& StdMeshers_NumberOfSegments::BuildDistributionTab( const std::vector<double>& tab,
93 throw ( SALOME_Exception )
95 if( !buildDistribution( tab, conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
100 //=============================================================================
104 //=============================================================================
106 void StdMeshers_NumberOfSegments::SetNumberOfSegments(int segmentsNumber)
107 throw(SALOME_Exception)
109 int oldNumberOfSegments = _numberOfSegments;
110 if (segmentsNumber <= 0)
112 SALOME_Exception(LOCALIZED("number of segments must be positive"));
113 _numberOfSegments = segmentsNumber;
115 if (oldNumberOfSegments != _numberOfSegments)
116 NotifySubMeshesHypothesisModification();
119 //=============================================================================
123 //=============================================================================
125 int StdMeshers_NumberOfSegments::GetNumberOfSegments() const
127 return _numberOfSegments;
130 //================================================================================
134 //================================================================================
136 void StdMeshers_NumberOfSegments::SetDistrType(DistrType typ)
137 throw(SALOME_Exception)
139 if (typ < DT_Regular || typ > DT_ExprFunc)
140 throw SALOME_Exception(LOCALIZED("distribution type is out of range"));
142 if (typ != _distrType)
145 NotifySubMeshesHypothesisModification();
149 //================================================================================
153 //================================================================================
155 StdMeshers_NumberOfSegments::DistrType StdMeshers_NumberOfSegments::GetDistrType() const
160 //================================================================================
164 //================================================================================
166 void StdMeshers_NumberOfSegments::SetScaleFactor(double scaleFactor)
167 throw(SALOME_Exception)
169 if (_distrType != DT_Scale)
170 throw SALOME_Exception(LOCALIZED("not a scale distribution"));
171 if (scaleFactor < PRECISION)
172 throw SALOME_Exception(LOCALIZED("scale factor must be positive"));
173 //if (fabs(scaleFactor - 1.0) < PRECISION)
174 // throw SALOME_Exception(LOCALIZED("scale factor must not be equal to 1"));
176 if (fabs(_scaleFactor - scaleFactor) > PRECISION)
178 _scaleFactor = scaleFactor;
179 NotifySubMeshesHypothesisModification();
183 //================================================================================
187 //================================================================================
189 double StdMeshers_NumberOfSegments::GetScaleFactor() const
190 throw(SALOME_Exception)
192 if (_distrType != DT_Scale)
193 throw SALOME_Exception(LOCALIZED("not a scale distribution"));
197 //================================================================================
201 //================================================================================
203 void StdMeshers_NumberOfSegments::SetTableFunction(const std::vector<double>& table)
204 throw(SALOME_Exception)
206 if (_distrType != DT_TabFunc)
207 throw SALOME_Exception(LOCALIZED("not a table function distribution"));
208 if ( (table.size() % 2) != 0 )
209 throw SALOME_Exception(LOCALIZED("odd size of vector of table function"));
212 double prev = -PRECISION;
213 bool isSame = table.size() == _table.size();
216 for (i=0; i < table.size()/2; i++) {
217 double par = table[i*2];
218 double val = table[i*2+1];
223 val = pow( 10.0, val );
225 CASCatch_CATCH(Standard_Failure)
227 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
228 throw SALOME_Exception( LOCALIZED( "invalid value"));
232 else if( _convMode==1 && val<0.0 )
235 if ( par<0 || par > 1)
236 throw SALOME_Exception(LOCALIZED("parameter of table function is out of range [0,1]"));
237 if ( fabs(par-prev)<PRECISION )
238 throw SALOME_Exception(LOCALIZED("two parameters are the same"));
240 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
245 double oldpar = _table[i*2];
246 double oldval = _table[i*2+1];
247 if (fabs(par - oldpar) > PRECISION || fabs(val - oldval) > PRECISION)
254 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
259 NotifySubMeshesHypothesisModification();
263 //================================================================================
267 //================================================================================
269 const std::vector<double>& StdMeshers_NumberOfSegments::GetTableFunction() const
270 throw(SALOME_Exception)
272 if (_distrType != DT_TabFunc)
273 throw SALOME_Exception(LOCALIZED("not a table function distribution"));
277 //================================================================================
278 /*! check if only 't' is unknown variable in expression
280 //================================================================================
281 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
283 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
285 return sub->GetName()=="t";
288 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
290 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
291 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
294 if( name->GetName()!="t" )
298 res = isCorrectArg( sub );
303 //================================================================================
304 /*! this function parses the expression 'str' in order to check if syntax is correct
305 * ( result in 'syntax' ) and if only 't' is unknown variable in expression ( result in 'args' )
307 //================================================================================
308 bool process( const TCollection_AsciiString& str, int convMode,
309 bool& syntax, bool& args,
310 bool& non_neg, bool& non_zero,
311 bool& singulars, double& sing_point )
313 bool parsed_ok = true;
314 Handle( ExprIntrp_GenExp ) myExpr;
317 myExpr = ExprIntrp_GenExp::Create();
318 myExpr->Process( str.ToCString() );
320 CASCatch_CATCH(Standard_Failure)
322 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
328 if( parsed_ok && myExpr->IsDone() )
331 args = isCorrectArg( myExpr->Expression() );
334 bool res = parsed_ok && syntax && args;
344 FunctionExpr f( str.ToCString(), convMode );
346 for( int i=0; i<=max; i++ )
348 double t = double(i)/double(max), val;
349 if( !f.value( t, val ) )
364 return res && non_neg && non_zero && ( !singulars );
367 //================================================================================
371 //================================================================================
373 void StdMeshers_NumberOfSegments::SetExpressionFunction(const char* expr)
374 throw(SALOME_Exception)
376 if (_distrType != DT_ExprFunc)
377 throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
379 // remove white spaces
380 TCollection_AsciiString str((Standard_CString)expr);
386 bool syntax, args, non_neg, singulars, non_zero;
388 bool res = process( str, _convMode, syntax, args, non_neg, non_zero, singulars, sing_point );
392 throw SALOME_Exception(LOCALIZED("invalid expression syntax"));
394 throw SALOME_Exception(LOCALIZED("only 't' may be used as function argument"));
396 throw SALOME_Exception(LOCALIZED("only non-negative function can be used as density"));
400 sprintf( buf, "Function has singular point in %.3f", sing_point );
401 throw SALOME_Exception( buf );
404 throw SALOME_Exception(LOCALIZED("f(t)=0 cannot be used as density"));
409 std::string func = expr;
413 NotifySubMeshesHypothesisModification();
417 //================================================================================
421 //================================================================================
423 const char* StdMeshers_NumberOfSegments::GetExpressionFunction() const
424 throw(SALOME_Exception)
426 if (_distrType != DT_ExprFunc)
427 throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
428 return _func.c_str();
431 //================================================================================
435 //================================================================================
437 void StdMeshers_NumberOfSegments::SetConversionMode( int conv )
438 throw(SALOME_Exception)
440 if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
441 throw SALOME_Exception(LOCALIZED("not a functional distribution"));
443 if( conv != _convMode )
446 NotifySubMeshesHypothesisModification();
450 //================================================================================
454 //================================================================================
456 int StdMeshers_NumberOfSegments::ConversionMode() const
457 throw(SALOME_Exception)
459 if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
460 throw SALOME_Exception(LOCALIZED("not a functional distribution"));
464 //=============================================================================
468 //=============================================================================
470 ostream & StdMeshers_NumberOfSegments::SaveTo(ostream & save)
472 save << _numberOfSegments << " " << (int)_distrType;
476 save << " " << _scaleFactor;
480 save << " " << _table.size();
481 for (i=0; i < _table.size(); i++)
482 save << " " << _table[i];
485 save << " " << _func;
492 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
493 save << " " << _convMode;
498 //=============================================================================
502 //=============================================================================
504 istream & StdMeshers_NumberOfSegments::LoadFrom(istream & load)
509 // read number of segments
512 _numberOfSegments = a;
514 load.clear(ios::badbit | load.rdstate());
516 // read second stored value. It can be two variants here:
517 // 1. If the hypothesis is stored in old format (nb.segments and scale factor),
518 // we wait here the scale factor, which is double.
519 // 2. If the hypothesis is stored in new format
520 // (nb.segments, distr.type, some other params.),
521 // we wait here the ditribution type, which is integer
523 isOK = (load >> scale_factor);
524 a = (int)scale_factor;
526 // try to interprete ditribution type,
527 // supposing that this hypothesis was written in the new format
530 if (a < DT_Regular || a > DT_ExprFunc)
531 _distrType = DT_Regular;
533 _distrType = (DistrType) a;
536 load.clear(ios::badbit | load.rdstate());
538 // parameters of distribution
549 load.clear(ios::badbit | load.rdstate());
550 // this can mean, that the hypothesis is stored in old format
551 _distrType = DT_Regular;
552 _scaleFactor = scale_factor;
561 _table.resize(a, 0.);
563 for (i=0; i < _table.size(); i++)
569 load.clear(ios::badbit | load.rdstate());
574 load.clear(ios::badbit | load.rdstate());
575 // this can mean, that the hypothesis is stored in old format
576 _distrType = DT_Regular;
577 _scaleFactor = scale_factor;
584 isOK = (load >> str);
589 load.clear(ios::badbit | load.rdstate());
590 // this can mean, that the hypothesis is stored in old format
591 _distrType = DT_Regular;
592 _scaleFactor = scale_factor;
601 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
607 load.clear(ios::badbit | load.rdstate());
613 //=============================================================================
617 //=============================================================================
619 ostream & operator <<(ostream & save, StdMeshers_NumberOfSegments & hyp)
621 return hyp.SaveTo( save );
624 //=============================================================================
628 //=============================================================================
630 istream & operator >>(istream & load, StdMeshers_NumberOfSegments & hyp)
632 return hyp.LoadFrom( load );
635 //================================================================================
637 * \brief Initialize number of segments by the mesh built on the geometry
638 * \param theMesh - the built mesh
639 * \param theShape - the geometry of interest
640 * \retval bool - true if parameter values have been successfully defined
642 //================================================================================
644 bool StdMeshers_NumberOfSegments::SetParametersByMesh(const SMESH_Mesh* theMesh,
645 const TopoDS_Shape& theShape)
647 if ( !theMesh || theShape.IsNull() )
650 _numberOfSegments = 0;
651 _distrType = DT_Regular;
654 TopTools_IndexedMapOfShape edgeMap;
655 TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
656 for ( int i = 1; i <= edgeMap.Extent(); ++i )
658 // get current segment length
659 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
660 SMESHDS_SubMesh * eSubMesh = aMeshDS->MeshElements( edgeMap( i ));
661 if ( eSubMesh && eSubMesh->NbElements())
662 _numberOfSegments += eSubMesh->NbElements();
667 _numberOfSegments /= nbEdges;