1 // Copyright (C) 2007-2020 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 StdMeshers : implementation of point distribution algorithm
24 // File : StdMeshers_Distribution.cxx
25 // Author : Alexandre SOLOVYOV
29 #include "StdMeshers_Distribution.hxx"
31 #include <math_GaussSingleIntegration.hxx>
32 #include <utilities.h>
34 #include <Standard_Failure.hxx>
35 #include <Expr_NamedUnknown.hxx>
36 #include <Standard_ErrorHandler.hxx>
40 namespace StdMeshers {
42 Function::Function( const int conv )
51 bool Function::value( const double, double& f ) const
58 } catch(Standard_Failure&) {
63 else if( myConv==1 && f<0.0 )
69 FunctionIntegral::FunctionIntegral( const Function* f, const double st )
71 myFunc( const_cast<Function*>( f ) ),
76 FunctionIntegral::~FunctionIntegral()
80 bool FunctionIntegral::value( const double t, double& f ) const
82 f = myFunc ? myFunc->integral( myStart, t ) : 0;
83 return myFunc!=0 && Function::value( t, f );
86 double FunctionIntegral::integral( const double, const double ) const
91 FunctionTable::FunctionTable( const std::vector<double>& data, const int conv )
97 FunctionTable::~FunctionTable()
101 bool FunctionTable::value( const double t, double& f ) const
104 if( !findBounds( t, i1, i2 ) )
108 f = myData[ 2*i1+1 ];
109 Function::value( t, f );
114 x1 = myData[2*i1], y1 = myData[2*i1+1],
115 x2 = myData[2*i2], y2 = myData[2*i2+1];
117 Function::value( x1, y1 );
118 Function::value( x2, y2 );
120 f = y1 + ( y2-y1 ) * ( t-x1 ) / ( x2-x1 );
124 double FunctionTable::integral( const int i ) const
126 if ( i >= 0 && i < (int)myData.size()-1 )
127 return integral( i, myData[2*(i+1)] - myData[2*i] );
132 double FunctionTable::integral( const int i, const double d ) const
134 double f1,f2, res = 0.0;
135 if( value( myData[2*i]+d, f1 ) )
136 if(!value(myData[2*i], f2)) {
138 Function::value( 1, f2 );
140 res = (f2+f1) * d / 2.0;
144 double FunctionTable::integral( const double a, const double b ) const
146 int x1s, x1f, x2s, x2f;
147 findBounds( a, x1s, x1f );
148 findBounds( b, x2s, x2f );
150 for( int i=x1s; i<x2s; i++ )
152 J-=integral( x1s, a-myData[2*x1s] );
153 J+=integral( x2s, b-myData[2*x2s] );
157 bool FunctionTable::findBounds( const double x, int& x_ind_1, int& x_ind_2 ) const
159 int n = myData.size() / 2;
160 if( n==0 || x<myData[0] )
162 x_ind_1 = x_ind_2 = 0;
166 for( int i=0; i<n-1; i++ )
167 if( myData[2*i]<=x && x<myData[2*(i+1)] )
175 return ( fabs( x - myData[2*x_ind_2] ) < 1.e-10 );
178 FunctionExpr::FunctionExpr( const char* str, const int conv )
186 myExpr = ExprIntrp_GenExp::Create();
187 myExpr->Process( ( Standard_CString )str );
188 } catch(Standard_Failure&) {
192 if( !ok || !myExpr->IsDone() )
195 myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
198 FunctionExpr::~FunctionExpr()
202 Standard_Boolean FunctionExpr::Value( const Standard_Real T, Standard_Real& F )
205 Standard_Boolean res = value( T, f );
210 bool FunctionExpr::value( const double t, double& f ) const
212 if( myExpr.IsNull() )
215 ( ( TColStd_Array1OfReal& )myValues ).ChangeValue( 1 ) = t;
219 f = myExpr->Expression()->Evaluate( myVars, myValues );
220 } catch(Standard_Failure&) {
225 ok = Function::value( t, f ) && ok;
229 double FunctionExpr::integral( const double a, const double b ) const
234 math_GaussSingleIntegration _int
235 ( *static_cast<math_Function*>( const_cast<FunctionExpr*> (this) ), a, b, 20 );
238 } catch(Standard_Failure&) {
240 MESSAGE( "Exception in integral calculating" );
245 double dihotomySolve( Function& f, const double val, const double _start, const double _fin, const double eps, bool& ok )
247 double start = _start, fin = _fin, start_val, fin_val; bool ok1, ok2;
248 ok1 = f.value( start, start_val );
249 ok2 = f.value( fin, fin_val );
257 bool start_pos = start_val>=val, fin_pos = fin_val>=val;
260 while( fin-start>eps )
262 double mid = ( start+fin )/2.0, mid_val;
263 ok = f.value( mid, mid_val );
268 //sprintf( buf, "start=%f\nfin=%f\nmid_val=%f\n", float( start ), float( fin ), float( mid_val ) );
271 bool mid_pos = mid_val>=val;
272 if( start_pos!=mid_pos )
277 else if( fin_pos!=mid_pos )
288 return (start+fin)/2.0;
291 bool buildDistribution( const TCollection_AsciiString& f, const int conv, const double start, const double end,
292 const int nbSeg, vector<double>& data, const double eps )
294 FunctionExpr F( f.ToCString(), conv );
295 return buildDistribution( F, start, end, nbSeg, data, eps );
298 bool buildDistribution( const std::vector<double>& f, const int conv, const double start, const double end,
299 const int nbSeg, vector<double>& data, const double eps )
301 FunctionTable F( f, conv );
302 return buildDistribution( F, start, end, nbSeg, data, eps );
305 bool buildDistribution( const Function& func, const double start, const double end, const int nbSeg,
306 vector<double>& data, const double eps )
311 data.resize( nbSeg+1 );
313 double J = func.integral( start, end ) / nbSeg;
318 //MESSAGE( "distribution:" );
320 for( int i=1; i<nbSeg; i++ )
322 FunctionIntegral f_int( &func, data[i-1] );
323 data[i] = dihotomySolve( f_int, J, data[i-1], end, eps, ok );
324 //sprintf( buf, "%f\n", float( data[i] ) );