1 // SMESH StdMeshers : implementaion of point distribution algorithm
3 // Copyright (C) 2003 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
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 <Standard_ErrorHandler.hxx>
37 Function::Function( const int conv )
46 bool Function::value( const double, double& f ) const
52 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
57 catch(Standard_Failure)
59 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
64 else if( myConv==1 && f<0.0 )
70 FunctionIntegral::FunctionIntegral( const Function* f, const double st )
72 myFunc( const_cast<Function*>( f ) ),
77 FunctionIntegral::~FunctionIntegral()
81 bool FunctionIntegral::value( const double t, double& f ) const
83 f = myFunc ? myFunc->integral( myStart, t ) : 0;
84 return myFunc!=0 && Function::value( t, f );
87 double FunctionIntegral::integral( const double, const double ) const
92 FunctionTable::FunctionTable( const std::vector<double>& data, const int conv )
98 FunctionTable::~FunctionTable()
102 bool FunctionTable::value( const double t, double& f ) const
105 if( !findBounds( t, i1, i2 ) )
109 x1 = myData[2*i1], y1 = myData[2*i1+1],
110 x2 = myData[2*i2], y2 = myData[2*i2+1];
112 Function::value( x1, y1 );
113 Function::value( x2, y2 );
115 f = y1 + ( y2-y1 ) * ( t-x1 ) / ( x2-x1 );
119 double FunctionTable::integral( const int i ) const
121 if( i>=0 && i<myData.size()-1 )
122 return integral( i, myData[2*(i+1)]-myData[2*i] );
127 double FunctionTable::integral( const int i, const double d ) const
129 double f1,f2, res = 0.0;
130 if( value( myData[2*i]+d, f1 ) )
131 if(!value(myData[2*i], f2))
133 res = (f2+f1) * d / 2.0;
137 double FunctionTable::integral( const double a, const double b ) const
139 int x1s, x1f, x2s, x2f;
140 findBounds( a, x1s, x1f );
141 findBounds( b, x2s, x2f );
143 for( int i=x1s; i<x2s; i++ )
145 J-=integral( x1s, a-myData[2*x1s] );
146 J+=integral( x2s, b-myData[2*x2s] );
150 bool FunctionTable::findBounds( const double x, int& x_ind_1, int& x_ind_2 ) const
152 int n = myData.size() / 2;
153 if( n==0 || x<myData[0] )
155 x_ind_1 = x_ind_2 = 0;
159 for( int i=0; i<n-1; i++ )
160 if( myData[2*i]<=x && x<=myData[2*(i+1)] )
171 FunctionExpr::FunctionExpr( const char* str, const int conv )
178 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
181 myExpr = ExprIntrp_GenExp::Create();
182 myExpr->Process( ( Standard_CString )str );
184 catch(Standard_Failure)
186 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
190 if( !ok || !myExpr->IsDone() )
193 myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
196 FunctionExpr::~FunctionExpr()
200 Standard_Boolean FunctionExpr::Value( Standard_Real T, Standard_Real& F )
203 Standard_Boolean res = value( T, f );
208 bool FunctionExpr::value( const double t, double& f ) const
210 if( myExpr.IsNull() )
213 ( ( TColStd_Array1OfReal& )myValues ).ChangeValue( 1 ) = t;
216 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
219 f = myExpr->Expression()->Evaluate( myVars, myValues );
221 catch(Standard_Failure) {
222 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
227 ok = Function::value( t, f ) && ok;
231 double FunctionExpr::integral( const double a, const double b ) const
235 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
238 math_GaussSingleIntegration _int( ( math_Function& )*this, a, b, 20 );
242 catch(Standard_Failure)
245 MESSAGE( "Exception in integral calculating" );
250 double dihotomySolve( Function& f, const double val, const double _start, const double _fin, const double eps, bool& ok )
252 double start = _start, fin = _fin, start_val, fin_val; bool ok1, ok2;
253 ok1 = f.value( start, start_val );
254 ok2 = f.value( fin, fin_val );
262 bool start_pos = start_val>=val, fin_pos = fin_val>=val;
265 while( fin-start>eps )
267 double mid = ( start+fin )/2.0, mid_val;
268 ok = f.value( mid, mid_val );
273 //sprintf( buf, "start=%f\nfin=%f\nmid_val=%f\n", float( start ), float( fin ), float( mid_val ) );
276 bool mid_pos = mid_val>=val;
277 if( start_pos!=mid_pos )
282 else if( fin_pos!=mid_pos )
293 return (start+fin)/2.0;
296 bool buildDistribution( const TCollection_AsciiString& f, const int conv, const double start, const double end,
297 const int nbSeg, vector<double>& data, const double eps )
299 FunctionExpr F( f.ToCString(), conv );
300 return buildDistribution( F, start, end, nbSeg, data, eps );
303 bool buildDistribution( const std::vector<double>& f, const int conv, const double start, const double end,
304 const int nbSeg, vector<double>& data, const double eps )
306 FunctionTable F( f, conv );
307 return buildDistribution( F, start, end, nbSeg, data, eps );
310 bool buildDistribution( const Function& func, const double start, const double end, const int nbSeg,
311 vector<double>& data, const double eps )
316 data.resize( nbSeg+1 );
318 double J = func.integral( start, end ) / nbSeg;
323 //MESSAGE( "distribution:" );
325 for( int i=1; i<nbSeg; i++ )
327 FunctionIntegral f_int( &func, data[i-1] );
328 data[i] = dihotomySolve( f_int, J, data[i-1], end, eps, ok );
329 //sprintf( buf, "%f\n", float( data[i] ) );