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 #include <Standard_Failure.hxx>
45 #include <Standard_ErrorHandler.hxx>
49 const double PRECISION = 1e-7;
51 //=============================================================================
55 //=============================================================================
57 StdMeshers_NumberOfSegments::StdMeshers_NumberOfSegments(int hypId, int studyId,
59 : SMESH_Hypothesis(hypId, studyId, gen),
61 _distrType(DT_Regular),
63 _convMode(1) //cut negative by default
65 _name = "NumberOfSegments";
69 //=============================================================================
73 //=============================================================================
75 StdMeshers_NumberOfSegments::~StdMeshers_NumberOfSegments()
79 //=============================================================================
83 //=============================================================================
84 const std::vector<double>& StdMeshers_NumberOfSegments::BuildDistributionExpr( const char* expr, int nbSeg, int conv )
85 throw ( SALOME_Exception )
87 if( !buildDistribution( TCollection_AsciiString( ( Standard_CString )expr ), conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
92 const std::vector<double>& StdMeshers_NumberOfSegments::BuildDistributionTab( const std::vector<double>& tab,
94 throw ( SALOME_Exception )
96 if( !buildDistribution( tab, conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
101 //=============================================================================
105 //=============================================================================
107 void StdMeshers_NumberOfSegments::SetNumberOfSegments(int segmentsNumber)
108 throw(SALOME_Exception)
110 int oldNumberOfSegments = _numberOfSegments;
111 if (segmentsNumber <= 0)
113 SALOME_Exception(LOCALIZED("number of segments must be positive"));
114 _numberOfSegments = segmentsNumber;
116 if (oldNumberOfSegments != _numberOfSegments)
117 NotifySubMeshesHypothesisModification();
120 //=============================================================================
124 //=============================================================================
126 int StdMeshers_NumberOfSegments::GetNumberOfSegments() const
128 return _numberOfSegments;
131 //================================================================================
135 //================================================================================
137 void StdMeshers_NumberOfSegments::SetDistrType(DistrType typ)
138 throw(SALOME_Exception)
140 if (typ < DT_Regular || typ > DT_ExprFunc)
141 throw SALOME_Exception(LOCALIZED("distribution type is out of range"));
143 if (typ != _distrType)
146 NotifySubMeshesHypothesisModification();
150 //================================================================================
154 //================================================================================
156 StdMeshers_NumberOfSegments::DistrType StdMeshers_NumberOfSegments::GetDistrType() const
161 //================================================================================
165 //================================================================================
167 void StdMeshers_NumberOfSegments::SetScaleFactor(double scaleFactor)
168 throw(SALOME_Exception)
170 if (_distrType != DT_Scale)
171 throw SALOME_Exception(LOCALIZED("not a scale distribution"));
172 if (scaleFactor < PRECISION)
173 throw SALOME_Exception(LOCALIZED("scale factor must be positive"));
174 //if (fabs(scaleFactor - 1.0) < PRECISION)
175 // throw SALOME_Exception(LOCALIZED("scale factor must not be equal to 1"));
177 if (fabs(_scaleFactor - scaleFactor) > PRECISION)
179 _scaleFactor = scaleFactor;
180 NotifySubMeshesHypothesisModification();
184 //================================================================================
188 //================================================================================
190 double StdMeshers_NumberOfSegments::GetScaleFactor() const
191 throw(SALOME_Exception)
193 if (_distrType != DT_Scale)
194 throw SALOME_Exception(LOCALIZED("not a scale distribution"));
198 //================================================================================
202 //================================================================================
204 void StdMeshers_NumberOfSegments::SetTableFunction(const std::vector<double>& table)
205 throw(SALOME_Exception)
207 if (_distrType != DT_TabFunc)
208 throw SALOME_Exception(LOCALIZED("not a table function distribution"));
209 if ( (table.size() % 2) != 0 )
210 throw SALOME_Exception(LOCALIZED("odd size of vector of table function"));
213 double prev = -PRECISION;
214 bool isSame = table.size() == _table.size();
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 catch(Standard_Failure)
228 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
229 throw SALOME_Exception( LOCALIZED( "invalid value"));
233 else if( _convMode==1 && val<0.0 )
236 if ( par<0 || par > 1)
237 throw SALOME_Exception(LOCALIZED("parameter of table function is out of range [0,1]"));
238 if ( fabs(par-prev)<PRECISION )
239 throw SALOME_Exception(LOCALIZED("two parameters are the same"));
241 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
246 double oldpar = _table[i*2];
247 double oldval = _table[i*2+1];
248 if (fabs(par - oldpar) > PRECISION || fabs(val - oldval) > PRECISION)
255 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
260 NotifySubMeshesHypothesisModification();
264 //================================================================================
268 //================================================================================
270 const std::vector<double>& StdMeshers_NumberOfSegments::GetTableFunction() const
271 throw(SALOME_Exception)
273 if (_distrType != DT_TabFunc)
274 throw SALOME_Exception(LOCALIZED("not a table function distribution"));
278 //================================================================================
279 /*! check if only 't' is unknown variable in expression
281 //================================================================================
282 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
284 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
286 return sub->GetName()=="t";
289 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
291 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
292 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
295 if( name->GetName()!="t" )
299 res = isCorrectArg( sub );
304 //================================================================================
305 /*! this function parses the expression 'str' in order to check if syntax is correct
306 * ( result in 'syntax' ) and if only 't' is unknown variable in expression ( result in 'args' )
308 //================================================================================
309 bool process( const TCollection_AsciiString& str, int convMode,
310 bool& syntax, bool& args,
311 bool& non_neg, bool& non_zero,
312 bool& singulars, double& sing_point )
314 bool parsed_ok = true;
315 Handle( ExprIntrp_GenExp ) myExpr;
318 myExpr = ExprIntrp_GenExp::Create();
319 myExpr->Process( str.ToCString() );
321 catch(Standard_Failure)
323 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
329 if( parsed_ok && myExpr->IsDone() )
332 args = isCorrectArg( myExpr->Expression() );
335 bool res = parsed_ok && syntax && args;
345 FunctionExpr f( str.ToCString(), convMode );
347 for( int i=0; i<=max; i++ )
349 double t = double(i)/double(max), val;
350 if( !f.value( t, val ) )
365 return res && non_neg && non_zero && ( !singulars );
368 //================================================================================
372 //================================================================================
374 void StdMeshers_NumberOfSegments::SetExpressionFunction(const char* expr)
375 throw(SALOME_Exception)
377 if (_distrType != DT_ExprFunc)
378 throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
380 // remove white spaces
381 TCollection_AsciiString str((Standard_CString)expr);
387 bool syntax, args, non_neg, singulars, non_zero;
389 bool res = process( str, _convMode, syntax, args, non_neg, non_zero, singulars, sing_point );
393 throw SALOME_Exception(LOCALIZED("invalid expression syntax"));
395 throw SALOME_Exception(LOCALIZED("only 't' may be used as function argument"));
397 throw SALOME_Exception(LOCALIZED("only non-negative function can be used as density"));
401 sprintf( buf, "Function has singular point in %.3f", sing_point );
402 throw SALOME_Exception( buf );
405 throw SALOME_Exception(LOCALIZED("f(t)=0 cannot be used as density"));
410 std::string func = expr;
414 NotifySubMeshesHypothesisModification();
418 //================================================================================
422 //================================================================================
424 const char* StdMeshers_NumberOfSegments::GetExpressionFunction() const
425 throw(SALOME_Exception)
427 if (_distrType != DT_ExprFunc)
428 throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
429 return _func.c_str();
432 //================================================================================
436 //================================================================================
438 void StdMeshers_NumberOfSegments::SetConversionMode( int conv )
439 throw(SALOME_Exception)
441 if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
442 throw SALOME_Exception(LOCALIZED("not a functional distribution"));
444 if( conv != _convMode )
447 NotifySubMeshesHypothesisModification();
451 //================================================================================
455 //================================================================================
457 int StdMeshers_NumberOfSegments::ConversionMode() const
458 throw(SALOME_Exception)
460 if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
461 throw SALOME_Exception(LOCALIZED("not a functional distribution"));
465 //=============================================================================
469 //=============================================================================
471 ostream & StdMeshers_NumberOfSegments::SaveTo(ostream & save)
473 save << _numberOfSegments << " " << (int)_distrType;
477 save << " " << _scaleFactor;
481 save << " " << _table.size();
482 for (i=0; i < _table.size(); i++)
483 save << " " << _table[i];
486 save << " " << _func;
493 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
494 save << " " << _convMode;
499 //=============================================================================
503 //=============================================================================
505 istream & StdMeshers_NumberOfSegments::LoadFrom(istream & load)
510 // read number of segments
513 _numberOfSegments = a;
515 load.clear(ios::badbit | load.rdstate());
517 // read second stored value. It can be two variants here:
518 // 1. If the hypothesis is stored in old format (nb.segments and scale factor),
519 // we wait here the scale factor, which is double.
520 // 2. If the hypothesis is stored in new format
521 // (nb.segments, distr.type, some other params.),
522 // we wait here the ditribution type, which is integer
524 isOK = (load >> scale_factor);
525 a = (int)scale_factor;
527 // try to interprete ditribution type,
528 // supposing that this hypothesis was written in the new format
531 if (a < DT_Regular || a > DT_ExprFunc)
532 _distrType = DT_Regular;
534 _distrType = (DistrType) a;
537 load.clear(ios::badbit | load.rdstate());
539 // parameters of distribution
550 load.clear(ios::badbit | load.rdstate());
551 // this can mean, that the hypothesis is stored in old format
552 _distrType = DT_Regular;
553 _scaleFactor = scale_factor;
562 _table.resize(a, 0.);
564 for (i=0; i < _table.size(); i++)
570 load.clear(ios::badbit | load.rdstate());
575 load.clear(ios::badbit | load.rdstate());
576 // this can mean, that the hypothesis is stored in old format
577 _distrType = DT_Regular;
578 _scaleFactor = scale_factor;
585 isOK = (load >> str);
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;
602 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
608 load.clear(ios::badbit | load.rdstate());
614 //=============================================================================
618 //=============================================================================
620 ostream & operator <<(ostream & save, StdMeshers_NumberOfSegments & hyp)
622 return hyp.SaveTo( save );
625 //=============================================================================
629 //=============================================================================
631 istream & operator >>(istream & load, StdMeshers_NumberOfSegments & hyp)
633 return hyp.LoadFrom( load );
636 //================================================================================
638 * \brief Initialize number of segments by the mesh built on the geometry
639 * \param theMesh - the built mesh
640 * \param theShape - the geometry of interest
641 * \retval bool - true if parameter values have been successfully defined
643 //================================================================================
645 bool StdMeshers_NumberOfSegments::SetParametersByMesh(const SMESH_Mesh* theMesh,
646 const TopoDS_Shape& theShape)
648 if ( !theMesh || theShape.IsNull() )
651 _numberOfSegments = 0;
652 _distrType = DT_Regular;
655 TopTools_IndexedMapOfShape edgeMap;
656 TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
657 for ( int i = 1; i <= edgeMap.Extent(); ++i )
659 // get current segment length
660 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
661 SMESHDS_SubMesh * eSubMesh = aMeshDS->MeshElements( edgeMap( i ));
662 if ( eSubMesh && eSubMesh->NbElements())
663 _numberOfSegments += eSubMesh->NbElements();
668 _numberOfSegments /= nbEdges;