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
28 #include "StdMeshers_NumberOfSegments.hxx"
30 #include "StdMeshers_Distribution.hxx"
31 #include "SMESHDS_SubMesh.hxx"
32 #include "SMESH_Mesh.hxx"
34 #include <ExprIntrp_GenExp.hxx>
35 #include <Expr_Array1OfNamedUnknown.hxx>
36 #include <Expr_NamedUnknown.hxx>
37 #include <TColStd_Array1OfReal.hxx>
38 #include <TCollection_AsciiString.hxx>
40 #include <TopTools_IndexedMapOfShape.hxx>
42 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
46 #include <Standard_Failure.hxx>
49 #include <Standard_ErrorHandler.hxx>
54 const double PRECISION = 1e-7;
56 //=============================================================================
60 //=============================================================================
62 StdMeshers_NumberOfSegments::StdMeshers_NumberOfSegments(int hypId,
65 : SMESH_Hypothesis(hypId, studyId, gen),
66 _numberOfSegments(15),//issue 19923
67 _distrType(DT_Regular),
69 _convMode(1) //cut negative by default
71 _name = "NumberOfSegments";
75 //=============================================================================
79 //=============================================================================
81 StdMeshers_NumberOfSegments::~StdMeshers_NumberOfSegments()
85 //=============================================================================
89 //=============================================================================
91 StdMeshers_NumberOfSegments::BuildDistributionExpr( const char* expr,int nbSeg,int conv )
92 throw ( SALOME_Exception )
94 if( !buildDistribution( TCollection_AsciiString( ( Standard_CString )expr ), conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
100 StdMeshers_NumberOfSegments::BuildDistributionTab( const vector<double>& tab,
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)
121 throw SALOME_Exception(LOCALIZED("number of segments must be positive"));
122 _numberOfSegments = segmentsNumber;
124 if (oldNumberOfSegments != _numberOfSegments)
125 NotifySubMeshesHypothesisModification();
128 //=============================================================================
132 //=============================================================================
134 int StdMeshers_NumberOfSegments::GetNumberOfSegments() const
136 return _numberOfSegments;
139 //================================================================================
143 //================================================================================
145 void StdMeshers_NumberOfSegments::SetDistrType(DistrType typ)
146 throw(SALOME_Exception)
148 if (typ < DT_Regular || typ > DT_ExprFunc)
149 throw SALOME_Exception(LOCALIZED("distribution type is out of range"));
151 if (typ != _distrType)
154 NotifySubMeshesHypothesisModification();
158 //================================================================================
162 //================================================================================
164 StdMeshers_NumberOfSegments::DistrType StdMeshers_NumberOfSegments::GetDistrType() const
169 //================================================================================
173 //================================================================================
175 void StdMeshers_NumberOfSegments::SetScaleFactor(double scaleFactor)
176 throw(SALOME_Exception)
178 if (_distrType != DT_Scale)
179 _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 vector<double>& table)
214 throw(SALOME_Exception)
216 if (_distrType != DT_TabFunc)
217 _distrType = DT_TabFunc;
218 //throw SALOME_Exception(LOCALIZED("not a table function distribution"));
219 if ( (table.size() % 2) != 0 )
220 throw SALOME_Exception(LOCALIZED("odd size of vector of table function"));
223 double prev = -PRECISION;
224 bool isSame = table.size() == _table.size();
227 for (i=0; i < table.size()/2; i++) {
228 double par = table[i*2];
229 double val = table[i*2+1];
236 val = pow( 10.0, val );
237 } catch(Standard_Failure) {
238 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
239 throw SALOME_Exception( LOCALIZED( "invalid value"));
243 else if( _convMode==1 && val<0.0 )
246 if ( par<0 || par > 1)
247 throw SALOME_Exception(LOCALIZED("parameter of table function is out of range [0,1]"));
248 if ( fabs(par-prev)<PRECISION )
249 throw SALOME_Exception(LOCALIZED("two parameters are the same"));
251 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
256 double oldpar = _table[i*2];
257 double oldval = _table[i*2+1];
258 if (fabs(par - oldpar) > PRECISION || fabs(val - oldval) > PRECISION)
265 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
270 NotifySubMeshesHypothesisModification();
274 //================================================================================
278 //================================================================================
280 const vector<double>& StdMeshers_NumberOfSegments::GetTableFunction() const
281 throw(SALOME_Exception)
283 if (_distrType != DT_TabFunc)
284 throw SALOME_Exception(LOCALIZED("not a table function distribution"));
288 //================================================================================
289 /*! check if only 't' is unknown variable in expression
291 //================================================================================
292 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
294 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
296 return sub->GetName()=="t";
299 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
301 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
302 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
305 if( name->GetName()!="t" )
309 res = isCorrectArg( sub );
314 //================================================================================
315 /*! this function parses the expression 'str' in order to check if syntax is correct
316 * ( result in 'syntax' ) and if only 't' is unknown variable in expression ( result in 'args' )
318 //================================================================================
319 bool process( const TCollection_AsciiString& str, int convMode,
320 bool& syntax, bool& args,
321 bool& non_neg, bool& non_zero,
322 bool& singulars, double& sing_point )
324 bool parsed_ok = true;
325 Handle( ExprIntrp_GenExp ) myExpr;
330 myExpr = ExprIntrp_GenExp::Create();
331 myExpr->Process( str.ToCString() );
332 } catch(Standard_Failure) {
333 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
339 if( parsed_ok && myExpr->IsDone() )
342 args = isCorrectArg( myExpr->Expression() );
345 bool res = parsed_ok && syntax && args;
355 FunctionExpr f( str.ToCString(), convMode );
357 for( int i=0; i<=max; i++ )
359 double t = double(i)/double(max), val;
360 if( !f.value( t, val ) )
375 return res && non_neg && non_zero && ( !singulars );
378 //================================================================================
382 //================================================================================
384 void StdMeshers_NumberOfSegments::SetExpressionFunction(const char* expr)
385 throw(SALOME_Exception)
387 if (_distrType != DT_ExprFunc)
388 _distrType = DT_ExprFunc;
389 //throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
391 // remove white spaces
392 TCollection_AsciiString str((Standard_CString)expr);
398 bool syntax, args, non_neg, singulars, non_zero;
400 bool res = process( str, _convMode, syntax, args, non_neg, non_zero, singulars, sing_point );
404 throw SALOME_Exception(LOCALIZED("invalid expression syntax"));
406 throw SALOME_Exception(LOCALIZED("only 't' may be used as function argument"));
408 throw SALOME_Exception(LOCALIZED("only non-negative function can be used as density"));
412 sprintf( buf, "Function has singular point in %.3f", sing_point );
413 throw SALOME_Exception( buf );
416 throw SALOME_Exception(LOCALIZED("f(t)=0 cannot be used as density"));
425 NotifySubMeshesHypothesisModification();
429 //================================================================================
433 //================================================================================
435 const char* StdMeshers_NumberOfSegments::GetExpressionFunction() const
436 throw(SALOME_Exception)
438 if (_distrType != DT_ExprFunc)
439 throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
440 return _func.c_str();
443 //================================================================================
447 //================================================================================
449 void StdMeshers_NumberOfSegments::SetConversionMode( int conv )
450 throw(SALOME_Exception)
452 // if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
453 // throw SALOME_Exception(LOCALIZED("not a functional distribution"));
455 if( conv != _convMode )
458 NotifySubMeshesHypothesisModification();
462 //================================================================================
466 //================================================================================
468 int StdMeshers_NumberOfSegments::ConversionMode() const
469 throw(SALOME_Exception)
471 // if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
472 // throw SALOME_Exception(LOCALIZED("not a functional distribution"));
476 //=============================================================================
480 //=============================================================================
482 ostream & StdMeshers_NumberOfSegments::SaveTo(ostream & save)
484 save << _numberOfSegments << " " << (int)_distrType;
488 save << " " << _scaleFactor;
492 save << " " << _table.size();
493 for (i=0; i < _table.size(); i++)
494 save << " " << _table[i];
497 save << " " << _func;
504 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
505 save << " " << _convMode;
510 //=============================================================================
514 //=============================================================================
516 istream & StdMeshers_NumberOfSegments::LoadFrom(istream & load)
521 // read number of segments
524 _numberOfSegments = a;
526 load.clear(ios::badbit | load.rdstate());
528 // read second stored value. It can be two variants here:
529 // 1. If the hypothesis is stored in old format (nb.segments and scale factor),
530 // we wait here the scale factor, which is double.
531 // 2. If the hypothesis is stored in new format
532 // (nb.segments, distr.type, some other params.),
533 // we wait here the ditribution type, which is integer
535 isOK = (load >> scale_factor);
536 a = (int)scale_factor;
538 // try to interprete ditribution type,
539 // supposing that this hypothesis was written in the new format
542 if (a < DT_Regular || a > DT_ExprFunc)
543 _distrType = DT_Regular;
545 _distrType = (DistrType) a;
548 load.clear(ios::badbit | load.rdstate());
550 // parameters of distribution
561 load.clear(ios::badbit | load.rdstate());
562 // this can mean, that the hypothesis is stored in old format
563 _distrType = DT_Regular;
564 _scaleFactor = scale_factor;
573 _table.resize(a, 0.);
575 for (i=0; i < _table.size(); i++)
581 load.clear(ios::badbit | load.rdstate());
586 load.clear(ios::badbit | load.rdstate());
587 // this can mean, that the hypothesis is stored in old format
588 _distrType = DT_Regular;
589 _scaleFactor = scale_factor;
596 isOK = (load >> str);
601 load.clear(ios::badbit | load.rdstate());
602 // this can mean, that the hypothesis is stored in old format
603 _distrType = DT_Regular;
604 _scaleFactor = scale_factor;
613 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
619 load.clear(ios::badbit | load.rdstate());
625 //=============================================================================
629 //=============================================================================
631 ostream & operator <<(ostream & save, StdMeshers_NumberOfSegments & hyp)
633 return hyp.SaveTo( save );
636 //=============================================================================
640 //=============================================================================
642 istream & operator >>(istream & load, StdMeshers_NumberOfSegments & hyp)
644 return hyp.LoadFrom( load );
647 //================================================================================
649 * \brief Initialize number of segments by the mesh built on the geometry
650 * \param theMesh - the built mesh
651 * \param theShape - the geometry of interest
652 * \retval bool - true if parameter values have been successfully defined
654 //================================================================================
656 bool StdMeshers_NumberOfSegments::SetParametersByMesh(const SMESH_Mesh* theMesh,
657 const TopoDS_Shape& theShape)
659 if ( !theMesh || theShape.IsNull() )
662 _numberOfSegments = 0;
663 _distrType = DT_Regular;
666 TopTools_IndexedMapOfShape edgeMap;
667 TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
668 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
669 for ( int i = 1; i <= edgeMap.Extent(); ++i )
671 // get current segment length
672 SMESHDS_SubMesh * eSubMesh = aMeshDS->MeshElements( edgeMap( i ));
673 if ( eSubMesh && eSubMesh->NbElements())
674 _numberOfSegments += eSubMesh->NbElements();
679 _numberOfSegments /= nbEdges;
681 if (_numberOfSegments == 0) _numberOfSegments = 1;
685 //================================================================================
687 * \brief Initialize my parameter values by linear size of mesh element.
688 * \retval bool - true if parameter values have been successfully defined
690 //================================================================================
692 bool StdMeshers_NumberOfSegments::SetParametersByElementSize(double elemLenght,
693 const SMESH_Mesh* /*theMesh*/)