1 // Copyright (C) 2007-2011 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
23 // SMESH SMESH : implementaion of SMESH idl descriptions
24 // File : StdMeshers_NumberOfSegments.cxx
25 // Moved here from SMESH_NumberOfSegments.cxx
26 // Author : Paul RASCLE, EDF
29 #include "StdMeshers_NumberOfSegments.hxx"
31 #include "StdMeshers_Distribution.hxx"
32 #include "SMESHDS_SubMesh.hxx"
33 #include "SMESH_Mesh.hxx"
35 #include <ExprIntrp_GenExp.hxx>
36 #include <Expr_Array1OfNamedUnknown.hxx>
37 #include <Expr_NamedUnknown.hxx>
38 #include <TColStd_Array1OfReal.hxx>
39 #include <TCollection_AsciiString.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
43 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
47 #include <Standard_Failure.hxx>
50 #include <Standard_ErrorHandler.hxx>
53 #include <Basics_Utils.hxx>
57 const double PRECISION = 1e-7;
59 //=============================================================================
63 //=============================================================================
65 StdMeshers_NumberOfSegments::StdMeshers_NumberOfSegments(int hypId,
68 : SMESH_Hypothesis(hypId, studyId, gen),
69 _numberOfSegments(15),//issue 19923
70 _distrType(DT_Regular),
72 _convMode(1) //cut negative by default
74 _name = "NumberOfSegments";
78 //=============================================================================
82 //=============================================================================
84 StdMeshers_NumberOfSegments::~StdMeshers_NumberOfSegments()
88 //=============================================================================
92 //=============================================================================
94 StdMeshers_NumberOfSegments::BuildDistributionExpr( const char* expr,int nbSeg,int conv )
95 throw ( SALOME_Exception )
97 if( !buildDistribution( TCollection_AsciiString( ( Standard_CString )expr ), conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
102 const vector<double>&
103 StdMeshers_NumberOfSegments::BuildDistributionTab( const vector<double>& tab,
106 throw ( SALOME_Exception )
108 if( !buildDistribution( tab, conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
113 //=============================================================================
117 //=============================================================================
119 void StdMeshers_NumberOfSegments::SetNumberOfSegments(int segmentsNumber)
120 throw(SALOME_Exception)
122 int oldNumberOfSegments = _numberOfSegments;
123 if (segmentsNumber <= 0)
124 throw SALOME_Exception(LOCALIZED("number of segments must be positive"));
125 _numberOfSegments = segmentsNumber;
127 if (oldNumberOfSegments != _numberOfSegments)
128 NotifySubMeshesHypothesisModification();
131 //=============================================================================
135 //=============================================================================
137 int StdMeshers_NumberOfSegments::GetNumberOfSegments() const
139 return _numberOfSegments;
142 //================================================================================
146 //================================================================================
148 void StdMeshers_NumberOfSegments::SetDistrType(DistrType typ)
149 throw(SALOME_Exception)
151 if (typ < DT_Regular || typ > DT_ExprFunc)
152 throw SALOME_Exception(LOCALIZED("distribution type is out of range"));
154 if (typ != _distrType)
157 NotifySubMeshesHypothesisModification();
161 //================================================================================
165 //================================================================================
167 StdMeshers_NumberOfSegments::DistrType StdMeshers_NumberOfSegments::GetDistrType() const
172 //================================================================================
176 //================================================================================
178 void StdMeshers_NumberOfSegments::SetScaleFactor(double scaleFactor)
179 throw(SALOME_Exception)
181 if (_distrType != DT_Scale)
182 _distrType = DT_Scale;
183 //throw SALOME_Exception(LOCALIZED("not a scale distribution"));
184 if (scaleFactor < PRECISION)
185 throw SALOME_Exception(LOCALIZED("scale factor must be positive"));
186 //if (fabs(scaleFactor - 1.0) < PRECISION)
187 // throw SALOME_Exception(LOCALIZED("scale factor must not be equal to 1"));
189 if (fabs(_scaleFactor - scaleFactor) > PRECISION)
191 _scaleFactor = scaleFactor;
192 NotifySubMeshesHypothesisModification();
196 //================================================================================
200 //================================================================================
202 double StdMeshers_NumberOfSegments::GetScaleFactor() const
203 throw(SALOME_Exception)
205 if (_distrType != DT_Scale)
206 throw SALOME_Exception(LOCALIZED("not a scale distribution"));
210 //================================================================================
214 //================================================================================
216 void StdMeshers_NumberOfSegments::SetTableFunction(const vector<double>& table)
217 throw(SALOME_Exception)
219 if (_distrType != DT_TabFunc)
220 _distrType = DT_TabFunc;
221 //throw SALOME_Exception(LOCALIZED("not a table function distribution"));
222 if ( (table.size() % 2) != 0 )
223 throw SALOME_Exception(LOCALIZED("odd size of vector of table function"));
226 double prev = -PRECISION;
227 bool isSame = table.size() == _table.size();
230 for (i=0; i < table.size()/2; i++) {
231 double par = table[i*2];
232 double val = table[i*2+1];
239 val = pow( 10.0, val );
240 } catch(Standard_Failure) {
241 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
242 throw SALOME_Exception( LOCALIZED( "invalid value"));
246 else if( _convMode==1 && val<0.0 )
249 if ( par<0 || par > 1)
250 throw SALOME_Exception(LOCALIZED("parameter of table function is out of range [0,1]"));
251 if ( fabs(par-prev)<PRECISION )
252 throw SALOME_Exception(LOCALIZED("two parameters are the same"));
254 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
259 double oldpar = _table[i*2];
260 double oldval = _table[i*2+1];
261 if (fabs(par - oldpar) > PRECISION || fabs(val - oldval) > PRECISION)
268 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
273 NotifySubMeshesHypothesisModification();
277 //================================================================================
281 //================================================================================
283 const vector<double>& StdMeshers_NumberOfSegments::GetTableFunction() const
284 throw(SALOME_Exception)
286 if (_distrType != DT_TabFunc)
287 throw SALOME_Exception(LOCALIZED("not a table function distribution"));
291 //================================================================================
292 /*! check if only 't' is unknown variable in expression
294 //================================================================================
295 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
297 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
299 return sub->GetName()=="t";
302 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
304 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
305 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
308 if( name->GetName()!="t" )
312 res = isCorrectArg( sub );
317 //================================================================================
318 /*! this function parses the expression 'str' in order to check if syntax is correct
319 * ( result in 'syntax' ) and if only 't' is unknown variable in expression ( result in 'args' )
321 //================================================================================
322 bool process( const TCollection_AsciiString& str, int convMode,
323 bool& syntax, bool& args,
324 bool& non_neg, bool& non_zero,
325 bool& singulars, double& sing_point )
327 Kernel_Utils::Localizer loc;
329 bool parsed_ok = true;
330 Handle( ExprIntrp_GenExp ) myExpr;
335 myExpr = ExprIntrp_GenExp::Create();
336 myExpr->Process( str.ToCString() );
337 } catch(Standard_Failure) {
338 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
344 if( parsed_ok && myExpr->IsDone() )
347 args = isCorrectArg( myExpr->Expression() );
350 bool res = parsed_ok && syntax && args;
360 FunctionExpr f( str.ToCString(), convMode );
362 for( int i=0; i<=max; i++ )
364 double t = double(i)/double(max), val;
365 if( !f.value( t, val ) )
381 return res && non_neg && non_zero && ( !singulars );
384 //================================================================================
388 //================================================================================
390 void StdMeshers_NumberOfSegments::SetExpressionFunction(const char* expr)
391 throw(SALOME_Exception)
393 if (_distrType != DT_ExprFunc)
394 _distrType = DT_ExprFunc;
395 //throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
397 // remove white spaces
398 TCollection_AsciiString str((Standard_CString)expr);
404 bool syntax, args, non_neg, singulars, non_zero;
406 bool res = process( str, _convMode, syntax, args, non_neg, non_zero, singulars, sing_point );
410 throw SALOME_Exception(LOCALIZED("invalid expression syntax"));
412 throw SALOME_Exception(LOCALIZED("only 't' may be used as function argument"));
414 throw SALOME_Exception(LOCALIZED("only non-negative function can be used as density"));
418 sprintf( buf, "Function has singular point in %.3f", sing_point );
419 throw SALOME_Exception( buf );
422 throw SALOME_Exception(LOCALIZED("f(t)=0 cannot be used as density"));
431 NotifySubMeshesHypothesisModification();
435 //================================================================================
439 //================================================================================
441 const char* StdMeshers_NumberOfSegments::GetExpressionFunction() const
442 throw(SALOME_Exception)
444 if (_distrType != DT_ExprFunc)
445 throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
446 return _func.c_str();
449 //================================================================================
453 //================================================================================
455 void StdMeshers_NumberOfSegments::SetConversionMode( int conv )
456 throw(SALOME_Exception)
458 // if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
459 // throw SALOME_Exception(LOCALIZED("not a functional distribution"));
461 if( conv != _convMode )
464 NotifySubMeshesHypothesisModification();
468 //================================================================================
472 //================================================================================
474 int StdMeshers_NumberOfSegments::ConversionMode() const
475 throw(SALOME_Exception)
477 // if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
478 // throw SALOME_Exception(LOCALIZED("not a functional distribution"));
482 //=============================================================================
486 //=============================================================================
488 ostream & StdMeshers_NumberOfSegments::SaveTo(ostream & save)
490 int listSize = _edgeIDs.size();
491 save << _numberOfSegments << " " << (int)_distrType;
495 save << " " << _scaleFactor;
499 save << " " << _table.size();
500 for (i=0; i < _table.size(); i++)
501 save << " " << _table[i];
504 save << " " << _func;
511 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
512 save << " " << _convMode;
514 if ( _distrType != DT_Regular && listSize > 0 ) {
515 save << " " << listSize;
516 for ( int i = 0; i < listSize; i++ )
517 save << " " << _edgeIDs[i];
518 save << " " << _objEntry;
524 //=============================================================================
528 //=============================================================================
530 istream & StdMeshers_NumberOfSegments::LoadFrom(istream & load)
535 // read number of segments
538 _numberOfSegments = a;
540 load.clear(ios::badbit | load.rdstate());
542 // read second stored value. It can be two variants here:
543 // 1. If the hypothesis is stored in old format (nb.segments and scale factor),
544 // we wait here the scale factor, which is double.
545 // 2. If the hypothesis is stored in new format
546 // (nb.segments, distr.type, some other params.),
547 // we wait here the ditribution type, which is integer
549 isOK = (load >> scale_factor);
550 a = (int)scale_factor;
552 // try to interprete ditribution type,
553 // supposing that this hypothesis was written in the new format
556 if (a < DT_Regular || a > DT_ExprFunc)
557 _distrType = DT_Regular;
559 _distrType = (DistrType) a;
562 load.clear(ios::badbit | load.rdstate());
564 // parameters of distribution
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;
587 _table.resize(a, 0.);
589 for (i=0; i < _table.size(); i++)
595 load.clear(ios::badbit | load.rdstate());
600 load.clear(ios::badbit | load.rdstate());
601 // this can mean, that the hypothesis is stored in old format
602 _distrType = DT_Regular;
603 _scaleFactor = scale_factor;
610 isOK = (load >> str);
615 load.clear(ios::badbit | load.rdstate());
616 // this can mean, that the hypothesis is stored in old format
617 _distrType = DT_Regular;
618 _scaleFactor = scale_factor;
627 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
633 load.clear(ios::badbit | load.rdstate());
636 // load reversed edges IDs
638 isOK = (load >> intVal);
639 if ( isOK && _distrType != DT_Regular && intVal > 0 ) {
640 _edgeIDs.reserve( intVal );
641 for (int i = 0; i < _edgeIDs.capacity() && isOK; i++) {
642 isOK = (load >> intVal);
643 if ( isOK ) _edgeIDs.push_back( intVal );
645 isOK = (load >> _objEntry);
651 //=============================================================================
655 //=============================================================================
657 ostream & operator <<(ostream & save, StdMeshers_NumberOfSegments & hyp)
659 return hyp.SaveTo( save );
662 //=============================================================================
666 //=============================================================================
668 istream & operator >>(istream & load, StdMeshers_NumberOfSegments & hyp)
670 return hyp.LoadFrom( load );
673 //================================================================================
675 * \brief Initialize number of segments by the mesh built on the geometry
676 * \param theMesh - the built mesh
677 * \param theShape - the geometry of interest
678 * \retval bool - true if parameter values have been successfully defined
680 //================================================================================
682 bool StdMeshers_NumberOfSegments::SetParametersByMesh(const SMESH_Mesh* theMesh,
683 const TopoDS_Shape& theShape)
685 if ( !theMesh || theShape.IsNull() )
688 _numberOfSegments = 0;
689 _distrType = DT_Regular;
692 TopTools_IndexedMapOfShape edgeMap;
693 TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
694 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
695 for ( int i = 1; i <= edgeMap.Extent(); ++i )
697 // get current segment length
698 SMESHDS_SubMesh * eSubMesh = aMeshDS->MeshElements( edgeMap( i ));
699 if ( eSubMesh && eSubMesh->NbElements())
700 _numberOfSegments += eSubMesh->NbElements();
705 _numberOfSegments /= nbEdges;
707 if (_numberOfSegments == 0) _numberOfSegments = 1;
711 //================================================================================
713 * \brief Initialize my parameter values by default parameters.
714 * \retval bool - true if parameter values have been successfully defined
716 //================================================================================
718 bool StdMeshers_NumberOfSegments::SetParametersByDefaults(const TDefaults& dflts,
719 const SMESH_Mesh* /*theMesh*/)
721 return (_numberOfSegments = dflts._nbSegments );
724 //=============================================================================
728 //=============================================================================
730 void StdMeshers_NumberOfSegments::SetReversedEdges( std::vector<int>& ids )
732 if ( ids != _edgeIDs ) {
735 NotifySubMeshesHypothesisModification();