1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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 : implementation 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 "SMESHDS_Mesh.hxx"
32 #include "SMESHDS_SubMesh.hxx"
33 #include "SMESH_Comment.hxx"
34 #include "SMESH_Mesh.hxx"
35 #include "StdMeshers_Distribution.hxx"
37 #include <ExprIntrp_GenExp.hxx>
38 #include <Expr_Array1OfNamedUnknown.hxx>
39 #include <Expr_NamedUnknown.hxx>
40 #include <TColStd_Array1OfReal.hxx>
41 #include <TCollection_AsciiString.hxx>
43 #include <TopTools_IndexedMapOfShape.hxx>
45 #include <Standard_Failure.hxx>
46 #include <Standard_ErrorHandler.hxx>
48 #include <Basics_Utils.hxx>
50 using namespace StdMeshers;
53 const double PRECISION = 1e-7;
55 //=============================================================================
59 //=============================================================================
61 StdMeshers_NumberOfSegments::StdMeshers_NumberOfSegments(int hypId,
64 : SMESH_Hypothesis(hypId, studyId, gen),
65 _numberOfSegments(15),//issue 19923
66 _distrType(DT_Regular),
68 _convMode(1) //cut negative by default
70 _name = "NumberOfSegments";
74 //=============================================================================
78 //=============================================================================
80 StdMeshers_NumberOfSegments::~StdMeshers_NumberOfSegments()
84 //=============================================================================
88 //=============================================================================
90 StdMeshers_NumberOfSegments::BuildDistributionExpr( const char* expr,int nbSeg,int conv )
91 throw ( SALOME_Exception )
93 if( !buildDistribution( TCollection_AsciiString( ( Standard_CString )expr ), conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
99 StdMeshers_NumberOfSegments::BuildDistributionTab( const vector<double>& tab,
102 throw ( SALOME_Exception )
104 if( !buildDistribution( tab, conv, 0.0, 1.0, nbSeg, _distr, 1E-4 ) )
109 //=============================================================================
113 //=============================================================================
115 void StdMeshers_NumberOfSegments::SetNumberOfSegments(int segmentsNumber)
116 throw(SALOME_Exception)
118 int oldNumberOfSegments = _numberOfSegments;
119 if (segmentsNumber <= 0)
120 throw SALOME_Exception(LOCALIZED("number of segments must be positive"));
121 _numberOfSegments = segmentsNumber;
123 if (oldNumberOfSegments != _numberOfSegments)
124 NotifySubMeshesHypothesisModification();
127 //=============================================================================
131 //=============================================================================
133 int StdMeshers_NumberOfSegments::GetNumberOfSegments() const
135 return _numberOfSegments;
138 //================================================================================
142 //================================================================================
144 void StdMeshers_NumberOfSegments::SetDistrType(DistrType typ)
145 throw(SALOME_Exception)
147 if (typ < DT_Regular || typ > DT_ExprFunc)
148 throw SALOME_Exception(LOCALIZED("distribution type is out of range"));
150 if (typ != _distrType)
153 NotifySubMeshesHypothesisModification();
157 //================================================================================
161 //================================================================================
163 StdMeshers_NumberOfSegments::DistrType StdMeshers_NumberOfSegments::GetDistrType() const
168 //================================================================================
172 //================================================================================
174 void StdMeshers_NumberOfSegments::SetScaleFactor(double scaleFactor)
175 throw(SALOME_Exception)
177 if (scaleFactor < PRECISION)
178 throw SALOME_Exception(LOCALIZED("scale factor must be positive"));
180 if (_distrType != DT_Scale)
181 _distrType = DT_Scale;
183 // commented by mpa for IPAL 52986
184 // if ( fabs(scaleFactor - 1.0) < PRECISION )
185 // _distrType = DT_Regular;
187 if ( fabs(_scaleFactor - scaleFactor) > PRECISION )
189 _scaleFactor = scaleFactor;
190 NotifySubMeshesHypothesisModification();
194 //================================================================================
198 //================================================================================
200 double StdMeshers_NumberOfSegments::GetScaleFactor() const
201 throw(SALOME_Exception)
203 if (_distrType != DT_Scale)
204 throw SALOME_Exception(LOCALIZED("not a scale distribution"));
208 //================================================================================
212 //================================================================================
214 void StdMeshers_NumberOfSegments::SetTableFunction(const vector<double>& table)
215 throw(SALOME_Exception)
217 if (_distrType != DT_TabFunc)
218 _distrType = DT_TabFunc;
219 //throw SALOME_Exception(LOCALIZED("not a table function distribution"));
220 if ( (table.size() % 2) != 0 )
221 throw SALOME_Exception(LOCALIZED("odd size of vector of table function"));
223 double prev = -PRECISION;
224 bool isSame = table.size() == _table.size();
227 for ( size_t i = 0; i < table.size() / 2; i++ )
229 double par = table[i*2];
230 double val = table[i*2+1];
235 val = pow( 10.0, val );
237 catch(Standard_Failure) {
238 throw SALOME_Exception( LOCALIZED( "invalid value"));
242 else if( _convMode==1 && val<0.0 )
245 if ( par < 0 || par > 1)
246 throw SALOME_Exception(LOCALIZED("parameter of table function is out of range [0,1]"));
247 if ( fabs(par-prev) < PRECISION )
248 throw SALOME_Exception(LOCALIZED("two parameters are the same"));
250 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
251 if( val > PRECISION )
255 double oldpar = _table[i*2];
256 double oldval = _table[i*2+1];
257 if ( fabs(par - oldpar) > PRECISION || fabs(val - oldval) > PRECISION )
264 throw SALOME_Exception(LOCALIZED("value of table function is not positive"));
266 if ( pos && !isSame )
269 NotifySubMeshesHypothesisModification();
273 //================================================================================
277 //================================================================================
279 const vector<double>& StdMeshers_NumberOfSegments::GetTableFunction() const
280 throw(SALOME_Exception)
282 if (_distrType != DT_TabFunc)
283 throw SALOME_Exception(LOCALIZED("not a table function distribution"));
287 //================================================================================
288 /*! check if only 't' is unknown variable in expression
290 //================================================================================
291 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
293 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
295 return sub->GetName()=="t";
298 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
300 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
301 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
304 if( name->GetName()!="t" )
308 res = isCorrectArg( sub );
313 //================================================================================
314 /*! this function parses the expression 'str' in order to check if syntax is correct
315 * ( result in 'syntax' ) and if only 't' is unknown variable in expression ( result in 'args' )
317 //================================================================================
318 bool process( const TCollection_AsciiString& str, int convMode,
319 bool& syntax, bool& args,
320 bool& non_neg, bool& non_zero,
321 bool& singulars, double& sing_point )
323 Kernel_Utils::Localizer loc;
325 bool parsed_ok = true;
326 Handle( ExprIntrp_GenExp ) myExpr;
329 myExpr = ExprIntrp_GenExp::Create();
330 myExpr->Process( str.ToCString() );
331 } catch(Standard_Failure) {
337 if( parsed_ok && myExpr->IsDone() )
340 args = isCorrectArg( myExpr->Expression() );
343 bool res = parsed_ok && syntax && args;
353 FunctionExpr f( str.ToCString(), convMode );
355 for( int i=0; i<=max; i++ )
357 double t = double(i)/double(max), val;
358 if( !f.value( t, val ) )
374 return res && non_neg && non_zero && ( !singulars );
377 //================================================================================
381 //================================================================================
383 void StdMeshers_NumberOfSegments::SetExpressionFunction(const char* expr)
384 throw(SALOME_Exception)
386 if (_distrType != DT_ExprFunc)
387 _distrType = DT_ExprFunc;
389 string func = CheckExpressionFunction( expr, _convMode );
393 NotifySubMeshesHypothesisModification();
397 //=======================================================================
398 //function : CheckExpressionFunction
399 //purpose : Checks validity of the expression of the function f(t), e.g. "sin(t)".
400 // In case of validity returns a cleaned expression
401 //=======================================================================
404 StdMeshers_NumberOfSegments::CheckExpressionFunction( const std::string& expr,
406 throw (SALOME_Exception)
408 // remove white spaces
409 TCollection_AsciiString str((Standard_CString)expr.c_str());
415 bool syntax, args, non_neg, singulars, non_zero;
417 bool res = process( str, convMode, syntax, args, non_neg, non_zero, singulars, sing_point );
421 throw SALOME_Exception(SMESH_Comment("invalid expression syntax: ") << str );
423 throw SALOME_Exception(LOCALIZED("only 't' may be used as function argument"));
425 throw SALOME_Exception(LOCALIZED("only non-negative function can be used"));
429 sprintf( buf, "Function has singular point in %.3f", sing_point );
430 throw SALOME_Exception( buf );
433 throw SALOME_Exception(LOCALIZED("f(t)=0 cannot be used"));
436 return str.ToCString();
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 int listSize = _edgeIDs.size();
495 save << _numberOfSegments << " " << (int)_distrType;
499 save << " " << _scaleFactor;
502 save << " " << _table.size();
503 for ( size_t i = 0; i < _table.size(); i++ )
504 save << " " << _table[i];
507 save << " " << _func;
514 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
515 save << " " << _convMode;
517 if ( _distrType != DT_Regular && listSize > 0 ) {
518 save << " " << listSize;
519 for ( int i = 0; i < listSize; i++ )
520 save << " " << _edgeIDs[i];
521 save << " " << _objEntry;
527 //=============================================================================
531 //=============================================================================
533 istream & StdMeshers_NumberOfSegments::LoadFrom(istream & load)
538 // read number of segments
539 isOK = static_cast<bool>(load >> a);
541 _numberOfSegments = a;
543 load.clear(ios::badbit | load.rdstate());
545 // read second stored value. It can be two variants here:
546 // 1. If the hypothesis is stored in old format (nb.segments and scale factor),
547 // we wait here the scale factor, which is double.
548 // 2. If the hypothesis is stored in new format
549 // (nb.segments, distr.type, some other params.),
550 // we wait here the ditribution type, which is integer
552 isOK = static_cast<bool>(load >> scale_factor);
553 a = (int)scale_factor;
555 // try to interprete ditribution type,
556 // supposing that this hypothesis was written in the new format
559 if (a < DT_Regular || a > DT_ExprFunc)
560 _distrType = DT_Regular;
562 _distrType = (DistrType) a;
565 load.clear(ios::badbit | load.rdstate());
567 // parameters of distribution
573 isOK = static_cast<bool>(load >> b);
578 load.clear(ios::badbit | load.rdstate());
579 // this can mean, that the hypothesis is stored in old format
580 _distrType = DT_Regular;
581 _scaleFactor = scale_factor;
587 isOK = static_cast<bool>(load >> a);
590 _table.resize(a, 0.);
591 for ( size_t i=0; i < _table.size(); i++ )
593 isOK = static_cast<bool>(load >> b);
597 load.clear(ios::badbit | load.rdstate());
602 load.clear(ios::badbit | load.rdstate());
603 // this can mean, that the hypothesis is stored in old format
604 _distrType = DT_Regular;
605 _scaleFactor = scale_factor;
612 isOK = static_cast<bool>(load >> str);
617 load.clear(ios::badbit | load.rdstate());
618 // this can mean, that the hypothesis is stored in old format
619 _distrType = DT_Regular;
620 _scaleFactor = scale_factor;
629 if (_distrType == DT_TabFunc || _distrType == DT_ExprFunc)
631 isOK = static_cast<bool>(load >> a);
635 load.clear(ios::badbit | load.rdstate());
638 // load reversed edges IDs
640 isOK = static_cast<bool>(load >> intVal);
641 if ( isOK && _distrType != DT_Regular && intVal > 0 ) {
642 _edgeIDs.reserve( intVal );
643 for ( size_t i = 0; i < _edgeIDs.capacity() && isOK; i++) {
644 isOK = static_cast<bool>(load >> intVal);
645 if ( isOK ) _edgeIDs.push_back( intVal );
647 isOK = static_cast<bool>(load >> _objEntry);
653 //=============================================================================
657 //=============================================================================
659 ostream & operator <<(ostream & save, StdMeshers_NumberOfSegments & hyp)
661 return hyp.SaveTo( save );
664 //=============================================================================
668 //=============================================================================
670 istream & operator >>(istream & load, StdMeshers_NumberOfSegments & hyp)
672 return hyp.LoadFrom( load );
675 //================================================================================
677 * \brief Initialize number of segments by the mesh built on the geometry
678 * \param theMesh - the built mesh
679 * \param theShape - the geometry of interest
680 * \retval bool - true if parameter values have been successfully defined
682 //================================================================================
684 bool StdMeshers_NumberOfSegments::SetParametersByMesh(const SMESH_Mesh* theMesh,
685 const TopoDS_Shape& theShape)
687 if ( !theMesh || theShape.IsNull() )
690 _numberOfSegments = 0;
691 _distrType = DT_Regular;
694 TopTools_IndexedMapOfShape edgeMap;
695 TopExp::MapShapes( theShape, TopAbs_EDGE, edgeMap );
696 SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
697 for ( int i = 1; i <= edgeMap.Extent(); ++i )
699 // get current segment length
700 SMESHDS_SubMesh * eSubMesh = aMeshDS->MeshElements( edgeMap( i ));
701 if ( eSubMesh && eSubMesh->NbElements())
702 _numberOfSegments += eSubMesh->NbElements();
707 _numberOfSegments /= nbEdges;
709 if (_numberOfSegments == 0) _numberOfSegments = 1;
713 //================================================================================
715 * \brief Initialize my parameter values by default parameters.
716 * \retval bool - true if parameter values have been successfully defined
718 //================================================================================
720 bool StdMeshers_NumberOfSegments::SetParametersByDefaults(const TDefaults& dflts,
721 const SMESH_Mesh* /*theMesh*/)
723 return (_numberOfSegments = dflts._nbSegments );
726 //=============================================================================
730 //=============================================================================
732 void StdMeshers_NumberOfSegments::SetReversedEdges( std::vector<int>& ids )
734 if ( ids != _edgeIDs ) {
737 NotifySubMeshesHypothesisModification();