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>
52 #include <Basics_Utils.hxx>
56 const double PRECISION = 1e-7;
58 //=============================================================================
62 //=============================================================================
64 StdMeshers_NumberOfSegments::StdMeshers_NumberOfSegments(int hypId,
67 : SMESH_Hypothesis(hypId, studyId, gen),
68 _numberOfSegments(15),//issue 19923
69 _distrType(DT_Regular),
71 _convMode(1) //cut negative by default
73 _name = "NumberOfSegments";
77 //=============================================================================
81 //=============================================================================
83 StdMeshers_NumberOfSegments::~StdMeshers_NumberOfSegments()
87 //=============================================================================
91 //=============================================================================
93 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 vector<double>&
102 StdMeshers_NumberOfSegments::BuildDistributionTab( const vector<double>& tab,
105 throw ( SALOME_Exception )
107 if( !buildDistribution( tab, conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
112 //=============================================================================
116 //=============================================================================
118 void StdMeshers_NumberOfSegments::SetNumberOfSegments(int segmentsNumber)
119 throw(SALOME_Exception)
121 int oldNumberOfSegments = _numberOfSegments;
122 if (segmentsNumber <= 0)
123 throw SALOME_Exception(LOCALIZED("number of segments must be positive"));
124 _numberOfSegments = segmentsNumber;
126 if (oldNumberOfSegments != _numberOfSegments)
127 NotifySubMeshesHypothesisModification();
130 //=============================================================================
134 //=============================================================================
136 int StdMeshers_NumberOfSegments::GetNumberOfSegments() const
138 return _numberOfSegments;
141 //================================================================================
145 //================================================================================
147 void StdMeshers_NumberOfSegments::SetDistrType(DistrType typ)
148 throw(SALOME_Exception)
150 if (typ < DT_Regular || typ > DT_ExprFunc)
151 throw SALOME_Exception(LOCALIZED("distribution type is out of range"));
153 if (typ != _distrType)
156 NotifySubMeshesHypothesisModification();
160 //================================================================================
164 //================================================================================
166 StdMeshers_NumberOfSegments::DistrType StdMeshers_NumberOfSegments::GetDistrType() const
171 //================================================================================
175 //================================================================================
177 void StdMeshers_NumberOfSegments::SetScaleFactor(double scaleFactor)
178 throw(SALOME_Exception)
180 if (_distrType != DT_Scale)
181 _distrType = DT_Scale;
182 //throw SALOME_Exception(LOCALIZED("not a scale distribution"));
183 if (scaleFactor < PRECISION)
184 throw SALOME_Exception(LOCALIZED("scale factor must be positive"));
185 //if (fabs(scaleFactor - 1.0) < PRECISION)
186 // throw SALOME_Exception(LOCALIZED("scale factor must not be equal to 1"));
188 if (fabs(_scaleFactor - scaleFactor) > PRECISION)
190 _scaleFactor = scaleFactor;
191 NotifySubMeshesHypothesisModification();
195 //================================================================================
199 //================================================================================
201 double StdMeshers_NumberOfSegments::GetScaleFactor() const
202 throw(SALOME_Exception)
204 if (_distrType != DT_Scale)
205 throw SALOME_Exception(LOCALIZED("not a scale distribution"));
209 //================================================================================
213 //================================================================================
215 void StdMeshers_NumberOfSegments::SetTableFunction(const vector<double>& table)
216 throw(SALOME_Exception)
218 if (_distrType != DT_TabFunc)
219 _distrType = DT_TabFunc;
220 //throw SALOME_Exception(LOCALIZED("not a table function distribution"));
221 if ( (table.size() % 2) != 0 )
222 throw SALOME_Exception(LOCALIZED("odd size of vector of table function"));
225 double prev = -PRECISION;
226 bool isSame = table.size() == _table.size();
229 for (i=0; i < table.size()/2; i++) {
230 double par = table[i*2];
231 double val = table[i*2+1];
238 val = pow( 10.0, val );
239 } catch(Standard_Failure) {
240 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
241 throw SALOME_Exception( LOCALIZED( "invalid value"));
245 else if( _convMode==1 && val<0.0 )
248 if ( par<0 || par > 1)
249 throw SALOME_Exception(LOCALIZED("parameter of table function is out of range [0,1]"));
250 if ( fabs(par-prev)<PRECISION )
251 throw SALOME_Exception(LOCALIZED("two parameters are the same"));
253 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
258 double oldpar = _table[i*2];
259 double oldval = _table[i*2+1];
260 if (fabs(par - oldpar) > PRECISION || fabs(val - oldval) > PRECISION)
267 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
272 NotifySubMeshesHypothesisModification();
276 //================================================================================
280 //================================================================================
282 const vector<double>& StdMeshers_NumberOfSegments::GetTableFunction() const
283 throw(SALOME_Exception)
285 if (_distrType != DT_TabFunc)
286 throw SALOME_Exception(LOCALIZED("not a table function distribution"));
290 //================================================================================
291 /*! check if only 't' is unknown variable in expression
293 //================================================================================
294 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
296 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
298 return sub->GetName()=="t";
301 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
303 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
304 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
307 if( name->GetName()!="t" )
311 res = isCorrectArg( sub );
316 //================================================================================
317 /*! this function parses the expression 'str' in order to check if syntax is correct
318 * ( result in 'syntax' ) and if only 't' is unknown variable in expression ( result in 'args' )
320 //================================================================================
321 bool process( const TCollection_AsciiString& str, int convMode,
322 bool& syntax, bool& args,
323 bool& non_neg, bool& non_zero,
324 bool& singulars, double& sing_point )
326 Kernel_Utils::Localizer loc;
328 bool parsed_ok = true;
329 Handle( ExprIntrp_GenExp ) myExpr;
334 myExpr = ExprIntrp_GenExp::Create();
335 myExpr->Process( str.ToCString() );
336 } catch(Standard_Failure) {
337 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
343 if( parsed_ok && myExpr->IsDone() )
346 args = isCorrectArg( myExpr->Expression() );
349 bool res = parsed_ok && syntax && args;
359 FunctionExpr f( str.ToCString(), convMode );
361 for( int i=0; i<=max; i++ )
363 double t = double(i)/double(max), val;
364 if( !f.value( t, val ) )
380 return res && non_neg && non_zero && ( !singulars );
383 //================================================================================
387 //================================================================================
389 void StdMeshers_NumberOfSegments::SetExpressionFunction(const char* expr)
390 throw(SALOME_Exception)
392 if (_distrType != DT_ExprFunc)
393 _distrType = DT_ExprFunc;
394 //throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
396 // remove white spaces
397 TCollection_AsciiString str((Standard_CString)expr);
403 bool syntax, args, non_neg, singulars, non_zero;
405 bool res = process( str, _convMode, syntax, args, non_neg, non_zero, singulars, sing_point );
409 throw SALOME_Exception(LOCALIZED("invalid expression syntax"));
411 throw SALOME_Exception(LOCALIZED("only 't' may be used as function argument"));
413 throw SALOME_Exception(LOCALIZED("only non-negative function can be used as density"));
417 sprintf( buf, "Function has singular point in %.3f", sing_point );
418 throw SALOME_Exception( buf );
421 throw SALOME_Exception(LOCALIZED("f(t)=0 cannot be used as density"));
430 NotifySubMeshesHypothesisModification();
434 //================================================================================
438 //================================================================================
440 const char* StdMeshers_NumberOfSegments::GetExpressionFunction() const
441 throw(SALOME_Exception)
443 if (_distrType != DT_ExprFunc)
444 throw SALOME_Exception(LOCALIZED("not an expression function distribution"));
445 return _func.c_str();
448 //================================================================================
452 //================================================================================
454 void StdMeshers_NumberOfSegments::SetConversionMode( int conv )
455 throw(SALOME_Exception)
457 // if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
458 // throw SALOME_Exception(LOCALIZED("not a functional distribution"));
460 if( conv != _convMode )
463 NotifySubMeshesHypothesisModification();
467 //================================================================================
471 //================================================================================
473 int StdMeshers_NumberOfSegments::ConversionMode() const
474 throw(SALOME_Exception)
476 // if (_distrType != DT_TabFunc && _distrType != DT_ExprFunc)
477 // throw SALOME_Exception(LOCALIZED("not a functional distribution"));
481 //=============================================================================
485 //=============================================================================
487 ostream & StdMeshers_NumberOfSegments::SaveTo(ostream & save)
489 int listSize = _edgeIDs.size();
490 save << _numberOfSegments << " " << (int)_distrType;
494 save << " " << _scaleFactor;
498 save << " " << _table.size();
499 for (i=0; i < _table.size(); i++)
500 save << " " << _table[i];
503 save << " " << _func;
510 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
511 save << " " << _convMode;
513 if ( _distrType != DT_Regular && listSize > 0 ) {
514 save << " " << listSize;
515 for ( int i = 0; i < listSize; i++ )
516 save << " " << _edgeIDs[i];
517 save << " " << _objEntry;
523 //=============================================================================
527 //=============================================================================
529 istream & StdMeshers_NumberOfSegments::LoadFrom(istream & load)
534 // read number of segments
537 _numberOfSegments = a;
539 load.clear(ios::badbit | load.rdstate());
541 // read second stored value. It can be two variants here:
542 // 1. If the hypothesis is stored in old format (nb.segments and scale factor),
543 // we wait here the scale factor, which is double.
544 // 2. If the hypothesis is stored in new format
545 // (nb.segments, distr.type, some other params.),
546 // we wait here the ditribution type, which is integer
548 isOK = (load >> scale_factor);
549 a = (int)scale_factor;
551 // try to interprete ditribution type,
552 // supposing that this hypothesis was written in the new format
555 if (a < DT_Regular || a > DT_ExprFunc)
556 _distrType = DT_Regular;
558 _distrType = (DistrType) a;
561 load.clear(ios::badbit | load.rdstate());
563 // parameters of distribution
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;
586 _table.resize(a, 0.);
588 for (i=0; i < _table.size(); i++)
594 load.clear(ios::badbit | load.rdstate());
599 load.clear(ios::badbit | load.rdstate());
600 // this can mean, that the hypothesis is stored in old format
601 _distrType = DT_Regular;
602 _scaleFactor = scale_factor;
609 isOK = (load >> str);
614 load.clear(ios::badbit | load.rdstate());
615 // this can mean, that the hypothesis is stored in old format
616 _distrType = DT_Regular;
617 _scaleFactor = scale_factor;
626 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
632 load.clear(ios::badbit | load.rdstate());
635 // load reversed edges IDs
637 isOK = (load >> intVal);
638 if ( isOK && _distrType != DT_Regular && intVal > 0 ) {
639 _edgeIDs.reserve( intVal );
640 for (int i = 0; i < _edgeIDs.capacity() && isOK; i++) {
641 isOK = (load >> intVal);
642 if ( isOK ) _edgeIDs.push_back( intVal );
644 isOK = (load >> _objEntry);
650 //=============================================================================
654 //=============================================================================
656 ostream & operator <<(ostream & save, StdMeshers_NumberOfSegments & hyp)
658 return hyp.SaveTo( save );
661 //=============================================================================
665 //=============================================================================
667 istream & operator >>(istream & load, StdMeshers_NumberOfSegments & hyp)
669 return hyp.LoadFrom( load );
672 //================================================================================
674 * \brief Initialize number of segments by the mesh built on the geometry
675 * \param theMesh - the built mesh
676 * \param theShape - the geometry of interest
677 * \retval bool - true if parameter values have been successfully defined
679 //================================================================================
681 bool StdMeshers_NumberOfSegments::SetParametersByMesh(const SMESH_Mesh* theMesh,
682 const TopoDS_Shape& theShape)
684 if ( !theMesh || theShape.IsNull() )
687 _numberOfSegments = 0;
688 _distrType = DT_Regular;
691 TopTools_IndexedMapOfShape edgeMap;
692 TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
693 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
694 for ( int i = 1; i <= edgeMap.Extent(); ++i )
696 // get current segment length
697 SMESHDS_SubMesh * eSubMesh = aMeshDS->MeshElements( edgeMap( i ));
698 if ( eSubMesh && eSubMesh->NbElements())
699 _numberOfSegments += eSubMesh->NbElements();
704 _numberOfSegments /= nbEdges;
706 if (_numberOfSegments == 0) _numberOfSegments = 1;
710 //================================================================================
712 * \brief Initialize my parameter values by default parameters.
713 * \retval bool - true if parameter values have been successfully defined
715 //================================================================================
717 bool StdMeshers_NumberOfSegments::SetParametersByDefaults(const TDefaults& dflts,
718 const SMESH_Mesh* /*theMesh*/)
720 return (_numberOfSegments = dflts._nbSegments );
723 //=============================================================================
727 //=============================================================================
729 void StdMeshers_NumberOfSegments::SetReversedEdges( std::vector<int>& ids )
731 if ( ids != _edgeIDs ) {
734 NotifySubMeshesHypothesisModification();