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>
53 #include "CASCatch.hxx"
58 const double PRECISION = 1e-7;
60 //=============================================================================
64 //=============================================================================
66 StdMeshers_NumberOfSegments::StdMeshers_NumberOfSegments(int hypId, int studyId,
68 : SMESH_Hypothesis(hypId, studyId, gen),
70 _distrType(DT_Regular),
72 _convMode(1) //cut negative by default
74 _name = "NumberOfSegments";
78 //=============================================================================
82 //=============================================================================
84 StdMeshers_NumberOfSegments::~StdMeshers_NumberOfSegments()
88 //=============================================================================
92 //=============================================================================
93 const std::vector<double>& 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 std::vector<double>& StdMeshers_NumberOfSegments::BuildDistributionTab( const std::vector<double>& tab,
102 int nbSeg, int conv )
103 throw ( SALOME_Exception )
105 if( !buildDistribution( tab, conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
110 //=============================================================================
114 //=============================================================================
116 void StdMeshers_NumberOfSegments::SetNumberOfSegments(int segmentsNumber)
117 throw(SALOME_Exception)
119 int oldNumberOfSegments = _numberOfSegments;
120 if (segmentsNumber <= 0)
122 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 throw SALOME_Exception(LOCALIZED("not a scale distribution"));
181 if (scaleFactor < PRECISION)
182 throw SALOME_Exception(LOCALIZED("scale factor must be positive"));
183 //if (fabs(scaleFactor - 1.0) < PRECISION)
184 // throw SALOME_Exception(LOCALIZED("scale factor must not be equal to 1"));
186 if (fabs(_scaleFactor - scaleFactor) > PRECISION)
188 _scaleFactor = scaleFactor;
189 NotifySubMeshesHypothesisModification();
193 //================================================================================
197 //================================================================================
199 double StdMeshers_NumberOfSegments::GetScaleFactor() const
200 throw(SALOME_Exception)
202 if (_distrType != DT_Scale)
203 throw SALOME_Exception(LOCALIZED("not a scale distribution"));
207 //================================================================================
211 //================================================================================
213 void StdMeshers_NumberOfSegments::SetTableFunction(const std::vector<double>& table)
214 throw(SALOME_Exception)
216 if (_distrType != DT_TabFunc)
217 throw SALOME_Exception(LOCALIZED("not a table function distribution"));
218 if ( (table.size() % 2) != 0 )
219 throw SALOME_Exception(LOCALIZED("odd size of vector of table function"));
222 double prev = -PRECISION;
223 bool isSame = table.size() == _table.size();
226 for (i=0; i < table.size()/2; i++) {
227 double par = table[i*2];
228 double val = table[i*2+1];
237 val = pow( 10.0, val );
239 } catch(Standard_Failure) {
241 } CASCatch_CATCH(Standard_Failure) {
243 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
244 throw SALOME_Exception( LOCALIZED( "invalid value"));
248 else if( _convMode==1 && val<0.0 )
251 if ( par<0 || par > 1)
252 throw SALOME_Exception(LOCALIZED("parameter of table function is out of range [0,1]"));
253 if ( fabs(par-prev)<PRECISION )
254 throw SALOME_Exception(LOCALIZED("two parameters are the same"));
256 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
261 double oldpar = _table[i*2];
262 double oldval = _table[i*2+1];
263 if (fabs(par - oldpar) > PRECISION || fabs(val - oldval) > PRECISION)
270 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
275 NotifySubMeshesHypothesisModification();
279 //================================================================================
283 //================================================================================
285 const std::vector<double>& StdMeshers_NumberOfSegments::GetTableFunction() const
286 throw(SALOME_Exception)
288 if (_distrType != DT_TabFunc)
289 throw SALOME_Exception(LOCALIZED("not a table function distribution"));
293 //================================================================================
294 /*! check if only 't' is unknown variable in expression
296 //================================================================================
297 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
299 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
301 return sub->GetName()=="t";
304 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
306 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
307 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
310 if( name->GetName()!="t" )
314 res = isCorrectArg( sub );
319 //================================================================================
320 /*! this function parses the expression 'str' in order to check if syntax is correct
321 * ( result in 'syntax' ) and if only 't' is unknown variable in expression ( result in 'args' )
323 //================================================================================
324 bool process( const TCollection_AsciiString& str, int convMode,
325 bool& syntax, bool& args,
326 bool& non_neg, bool& non_zero,
327 bool& singulars, double& sing_point )
329 bool parsed_ok = true;
330 Handle( ExprIntrp_GenExp ) myExpr;
337 myExpr = ExprIntrp_GenExp::Create();
338 myExpr->Process( str.ToCString() );
340 } catch(Standard_Failure) {
342 } CASCatch_CATCH(Standard_Failure) {
344 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
350 if( parsed_ok && myExpr->IsDone() )
353 args = isCorrectArg( myExpr->Expression() );
356 bool res = parsed_ok && syntax && args;
366 FunctionExpr f( str.ToCString(), convMode );
368 for( int i=0; i<=max; i++ )
370 double t = double(i)/double(max), val;
371 if( !f.value( t, val ) )
386 return res && non_neg && non_zero && ( !singulars );
389 //================================================================================
393 //================================================================================
395 void StdMeshers_NumberOfSegments::SetExpressionFunction(const char* expr)
396 throw(SALOME_Exception)
398 if (_distrType != DT_ExprFunc)
399 throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
401 // remove white spaces
402 TCollection_AsciiString str((Standard_CString)expr);
408 bool syntax, args, non_neg, singulars, non_zero;
410 bool res = process( str, _convMode, syntax, args, non_neg, non_zero, singulars, sing_point );
414 throw SALOME_Exception(LOCALIZED("invalid expression syntax"));
416 throw SALOME_Exception(LOCALIZED("only 't' may be used as function argument"));
418 throw SALOME_Exception(LOCALIZED("only non-negative function can be used as density"));
422 sprintf( buf, "Function has singular point in %.3f", sing_point );
423 throw SALOME_Exception( buf );
426 throw SALOME_Exception(LOCALIZED("f(t)=0 cannot be used as density"));
431 std::string func = expr;
435 NotifySubMeshesHypothesisModification();
439 //================================================================================
443 //================================================================================
445 const char* StdMeshers_NumberOfSegments::GetExpressionFunction() const
446 throw(SALOME_Exception)
448 if (_distrType != DT_ExprFunc)
449 throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
450 return _func.c_str();
453 //================================================================================
457 //================================================================================
459 void StdMeshers_NumberOfSegments::SetConversionMode( int conv )
460 throw(SALOME_Exception)
462 if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
463 throw SALOME_Exception(LOCALIZED("not a functional distribution"));
465 if( conv != _convMode )
468 NotifySubMeshesHypothesisModification();
472 //================================================================================
476 //================================================================================
478 int StdMeshers_NumberOfSegments::ConversionMode() const
479 throw(SALOME_Exception)
481 if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
482 throw SALOME_Exception(LOCALIZED("not a functional distribution"));
486 //=============================================================================
490 //=============================================================================
492 ostream & StdMeshers_NumberOfSegments::SaveTo(ostream & save)
494 save << _numberOfSegments << " " << (int)_distrType;
498 save << " " << _scaleFactor;
502 save << " " << _table.size();
503 for (i=0; i < _table.size(); i++)
504 save << " " << _table[i];
507 save << " " << _func;
514 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
515 save << " " << _convMode;
520 //=============================================================================
524 //=============================================================================
526 istream & StdMeshers_NumberOfSegments::LoadFrom(istream & load)
531 // read number of segments
534 _numberOfSegments = a;
536 load.clear(ios::badbit | load.rdstate());
538 // read second stored value. It can be two variants here:
539 // 1. If the hypothesis is stored in old format (nb.segments and scale factor),
540 // we wait here the scale factor, which is double.
541 // 2. If the hypothesis is stored in new format
542 // (nb.segments, distr.type, some other params.),
543 // we wait here the ditribution type, which is integer
545 isOK = (load >> scale_factor);
546 a = (int)scale_factor;
548 // try to interprete ditribution type,
549 // supposing that this hypothesis was written in the new format
552 if (a < DT_Regular || a > DT_ExprFunc)
553 _distrType = DT_Regular;
555 _distrType = (DistrType) a;
558 load.clear(ios::badbit | load.rdstate());
560 // parameters of distribution
571 load.clear(ios::badbit | load.rdstate());
572 // this can mean, that the hypothesis is stored in old format
573 _distrType = DT_Regular;
574 _scaleFactor = scale_factor;
583 _table.resize(a, 0.);
585 for (i=0; i < _table.size(); i++)
591 load.clear(ios::badbit | load.rdstate());
596 load.clear(ios::badbit | load.rdstate());
597 // this can mean, that the hypothesis is stored in old format
598 _distrType = DT_Regular;
599 _scaleFactor = scale_factor;
606 isOK = (load >> str);
611 load.clear(ios::badbit | load.rdstate());
612 // this can mean, that the hypothesis is stored in old format
613 _distrType = DT_Regular;
614 _scaleFactor = scale_factor;
623 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
629 load.clear(ios::badbit | load.rdstate());
635 //=============================================================================
639 //=============================================================================
641 ostream & operator <<(ostream & save, StdMeshers_NumberOfSegments & hyp)
643 return hyp.SaveTo( save );
646 //=============================================================================
650 //=============================================================================
652 istream & operator >>(istream & load, StdMeshers_NumberOfSegments & hyp)
654 return hyp.LoadFrom( load );
657 //================================================================================
659 * \brief Initialize number of segments by the mesh built on the geometry
660 * \param theMesh - the built mesh
661 * \param theShape - the geometry of interest
662 * \retval bool - true if parameter values have been successfully defined
664 //================================================================================
666 bool StdMeshers_NumberOfSegments::SetParametersByMesh(const SMESH_Mesh* theMesh,
667 const TopoDS_Shape& theShape)
669 if ( !theMesh || theShape.IsNull() )
672 _numberOfSegments = 0;
673 _distrType = DT_Regular;
676 TopTools_IndexedMapOfShape edgeMap;
677 TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
678 for ( int i = 1; i <= edgeMap.Extent(); ++i )
680 // get current segment length
681 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
682 SMESHDS_SubMesh * eSubMesh = aMeshDS->MeshElements( edgeMap( i ));
683 if ( eSubMesh && eSubMesh->NbElements())
684 _numberOfSegments += eSubMesh->NbElements();
689 _numberOfSegments /= nbEdges;