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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : StdMeshers_Distribution.cxx
25 // Author : Alexandre SOLOVYOV
29 #include "StdMeshers_Distribution.hxx"
30 #include <CASCatch_CatchSignals.hxx>
31 #include <CASCatch_Failure.hxx>
32 #include <CASCatch_ErrorHandler.hxx>
34 #include <math_GaussSingleIntegration.hxx>
35 #include <utilities.h>
37 Function::Function( const int conv )
46 bool Function::value( const double, double& f ) const
48 CASCatch_CatchSignals aCatchSignals;
49 aCatchSignals.Activate();
58 CASCatch_CATCH(CASCatch_Failure)
60 aCatchSignals.Deactivate();
61 Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();
66 else if( myConv==1 && f<0.0 )
72 FunctionIntegral::FunctionIntegral( const Function* f, const double st )
74 myFunc( const_cast<Function*>( f ) ),
79 FunctionIntegral::~FunctionIntegral()
83 bool FunctionIntegral::value( const double t, double& f ) const
85 f = myFunc ? myFunc->integral( myStart, t ) : 0;
86 return myFunc!=0 && Function::value( t, f );
89 double FunctionIntegral::integral( const double, const double ) const
94 FunctionTable::FunctionTable( const std::vector<double>& data, const int conv )
100 FunctionTable::~FunctionTable()
104 bool FunctionTable::value( const double t, double& f ) const
107 if( !findBounds( t, i1, i2 ) )
111 x1 = myData[2*i1], y1 = myData[2*i1+1],
112 x2 = myData[2*i2], y2 = myData[2*i2+1];
114 Function::value( x1, y1 );
115 Function::value( x2, y2 );
117 f = y1 + ( y2-y1 ) * ( t-x1 ) / ( x2-x1 );
121 double FunctionTable::integral( const int i ) const
123 if( i>=0 && i<myData.size()-1 )
124 return integral( i, myData[2*(i+1)]-myData[2*i] );
129 double FunctionTable::integral( const int i, const double d ) const
132 if( value( myData[2*i]+d, f ) )
133 res = ( myData[2*i+1] + f ) / 2.0 * d;
138 double FunctionTable::integral( const double a, const double b ) const
140 int x1s, x1f, x2s, x2f;
141 findBounds( a, x1s, x1f );
142 findBounds( b, x2s, x2f );
144 for( int i=x1s; i<x2s; i++ )
146 J-=integral( x1s, a-myData[2*x1s] );
147 J+=integral( x2s, b-myData[2*x2s] );
151 bool FunctionTable::findBounds( const double x, int& x_ind_1, int& x_ind_2 ) const
153 int n = myData.size();
154 if( n==0 || x<myData[0] )
156 x_ind_1 = x_ind_2 = 0;
160 for( int i=0; i<n-1; i++ )
161 if( myData[2*i]<=x && x<=myData[2*(i+1)] )
172 FunctionExpr::FunctionExpr( const char* str, const int conv )
177 CASCatch_CatchSignals aCatchSignals;
178 aCatchSignals.Activate();
183 myExpr = ExprIntrp_GenExp::Create();
184 myExpr->Process( ( Standard_CString )str );
186 CASCatch_CATCH(CASCatch_Failure)
188 aCatchSignals.Deactivate();
189 Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();
192 aCatchSignals.Deactivate();
194 if( !ok || !myExpr->IsDone() )
197 myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
200 FunctionExpr::~FunctionExpr()
204 Standard_Boolean FunctionExpr::Value( Standard_Real T, Standard_Real& F )
207 Standard_Boolean res = value( T, f );
212 bool FunctionExpr::value( const double t, double& f ) const
214 if( myExpr.IsNull() )
217 OSD::SetSignal( true );
218 CASCatch_CatchSignals aCatchSignals;
219 aCatchSignals.Activate();
221 ( ( TColStd_Array1OfReal& )myValues ).ChangeValue( 1 ) = t;
224 f = myExpr->Expression()->Evaluate( myVars, myValues );
226 CASCatch_CATCH(CASCatch_Failure) {
227 aCatchSignals.Deactivate();
228 Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();
233 aCatchSignals.Deactivate();
234 ok = Function::value( t, f ) && ok;
238 double FunctionExpr::integral( const double a, const double b ) const
243 math_GaussSingleIntegration _int( ( math_Function& )*this, a, b, 20 );
247 CASCatch_CATCH(CASCatch_Failure)
250 MESSAGE( "Exception in integral calculating" );
255 double dihotomySolve( Function& f, const double val, const double _start, const double _fin, const double eps, bool& ok )
257 double start = _start, fin = _fin, start_val, fin_val; bool ok1, ok2;
258 ok1 = f.value( start, start_val );
259 ok2 = f.value( fin, fin_val );
267 bool start_pos = start_val>=val, fin_pos = fin_val>=val;
270 while( fin-start>eps )
272 double mid = ( start+fin )/2.0, mid_val;
273 ok = f.value( mid, mid_val );
278 //sprintf( buf, "start=%f\nfin=%f\nmid_val=%f\n", float( start ), float( fin ), float( mid_val ) );
281 bool mid_pos = mid_val>=val;
282 if( start_pos!=mid_pos )
287 else if( fin_pos!=mid_pos )
298 return (start+fin)/2.0;
301 bool buildDistribution( const TCollection_AsciiString& f, const int conv, const double start, const double end,
302 const int nbSeg, vector<double>& data, const double eps )
304 FunctionExpr F( f.ToCString(), conv );
305 return buildDistribution( F, start, end, nbSeg, data, eps );
308 bool buildDistribution( const std::vector<double>& f, const int conv, const double start, const double end,
309 const int nbSeg, vector<double>& data, const double eps )
311 FunctionTable F( f, conv );
312 return buildDistribution( F, start, end, nbSeg, data, eps );
315 bool buildDistribution( const Function& func, const double start, const double end, const int nbSeg,
316 vector<double>& data, const double eps )
321 data.resize( nbSeg+1 );
323 double J = func.integral( start, end ) / nbSeg;
328 //MESSAGE( "distribution:" );
330 for( int i=1; i<nbSeg; i++ )
332 FunctionIntegral f_int( &func, data[i-1] );
333 data[i] = dihotomySolve( f_int, J, data[i-1], end, eps, ok );
334 //sprintf( buf, "%f\n", float( data[i] ) );