Salome HOME
86efdee66fe1bacd10aaed1da9cf258094f9a00d
[modules/smesh.git] / src / StdMeshers / StdMeshers_Distribution.cxx
1 //  SMESH StdMeshers : implementaion of point distribution algorithm
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : StdMeshers_Distribution.cxx
25 //  Author : Alexandre SOLOVYOV
26 //  Module : SMESH
27 //  $Header$
28
29 #include "StdMeshers_Distribution.hxx"
30
31 #include <math_GaussSingleIntegration.hxx>
32 #include <utilities.h>
33
34 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
35 #define NO_CAS_CATCH
36 #endif
37
38 #include <Standard_Failure.hxx>
39
40 #ifdef NO_CAS_CATCH
41 #include <Standard_ErrorHandler.hxx>
42 #else
43 #include "CASCatch.hxx"
44 #endif
45
46 Function::Function( const int conv )
47 : myConv( conv )
48 {
49 }
50
51 Function::~Function()
52 {
53 }
54
55 bool Function::value( const double, double& f ) const
56 {
57   bool ok = true;
58   if (myConv == 0) {
59 #ifdef NO_CAS_CATCH
60     try {
61       OCC_CATCH_SIGNALS;
62 #else
63     CASCatch_TRY {
64 #endif
65       f = pow( 10, f );
66 #ifdef NO_CAS_CATCH
67     } catch(Standard_Failure) {
68 #else
69     } CASCatch_CATCH(Standard_Failure) {
70 #endif
71       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
72       f = 0.0;
73       ok = false;
74     }
75   }
76   else if( myConv==1 && f<0.0 )
77     f = 0.0;
78
79   return ok;
80 }
81
82 FunctionIntegral::FunctionIntegral( const Function* f, const double st )
83 : Function( -1 ),
84   myFunc( const_cast<Function*>( f ) ),
85   myStart( st )
86 {
87 }
88
89 FunctionIntegral::~FunctionIntegral()
90 {
91 }
92
93 bool FunctionIntegral::value( const double t, double& f ) const
94 {
95   f = myFunc ? myFunc->integral( myStart, t ) : 0;
96   return myFunc!=0 && Function::value( t, f );
97 }
98
99 double FunctionIntegral::integral( const double, const double ) const
100 {
101   return 0;
102 }
103
104 FunctionTable::FunctionTable( const std::vector<double>& data, const int conv )
105 : Function( conv )
106 {
107   myData = data;
108 }
109
110 FunctionTable::~FunctionTable()
111 {
112 }
113
114 bool FunctionTable::value( const double t, double& f ) const
115 {
116   int i1, i2;
117   if( !findBounds( t, i1, i2 ) )
118     return false;
119
120   if( i1==i2 ) {
121     f = myData[ 2*i1+1 ];
122     return true;
123   }
124       
125   double
126     x1 = myData[2*i1], y1 = myData[2*i1+1],
127     x2 = myData[2*i2], y2 = myData[2*i2+1];
128
129   Function::value( x1, y1 );
130   Function::value( x2, y2 );
131   
132   f = y1 + ( y2-y1 ) * ( t-x1 ) / ( x2-x1 );
133   return true;
134 }
135
136 double FunctionTable::integral( const int i ) const
137 {
138   if( i>=0 && i<myData.size()-1 )
139     return integral( i, myData[2*(i+1)]-myData[2*i] );
140   else
141     return 0;
142 }
143
144 double FunctionTable::integral( const int i, const double d ) const
145 {
146   double f1,f2, res = 0.0;
147   if( value( myData[2*i]+d, f1 ) )
148     if(!value(myData[2*i], f2))
149       f2 = myData[2*i+1];
150   res = (f2+f1) * d / 2.0;
151   return res;
152 }
153
154 double FunctionTable::integral( const double a, const double b ) const
155 {
156   int x1s, x1f, x2s, x2f;
157   findBounds( a, x1s, x1f );
158   findBounds( b, x2s, x2f );
159   double J = 0;
160   for( int i=x1s; i<x2s; i++ )
161     J+=integral( i );
162   J-=integral( x1s, a-myData[2*x1s] );
163   J+=integral( x2s, b-myData[2*x2s] );
164   return J;
165 }
166
167 bool FunctionTable::findBounds( const double x, int& x_ind_1, int& x_ind_2 ) const
168 {
169   int n = myData.size() / 2;
170   if( n==0 || x<myData[0] )
171   {
172     x_ind_1 = x_ind_2 = 0;
173     return false;
174   }
175
176   for( int i=0; i<n-1; i++ )
177     if( myData[2*i]<=x && x<myData[2*(i+1)] )
178     {
179       x_ind_1 = i;
180       x_ind_2 = i+1;
181       return true;
182     }
183   x_ind_1 = n-1;
184   x_ind_2 = n-1;
185   return ( fabs( x - myData[2*x_ind_2] ) < 1.e-10 );
186 }
187
188 FunctionExpr::FunctionExpr( const char* str, const int conv )
189 : Function( conv ),
190   myVars( 1, 1 ),
191   myValues( 1, 1 )
192 {
193   bool ok = true;
194 #ifdef NO_CAS_CATCH
195   try {
196     OCC_CATCH_SIGNALS;
197 #else
198   CASCatch_TRY {
199 #endif
200     myExpr = ExprIntrp_GenExp::Create();
201     myExpr->Process( ( Standard_CString )str );
202 #ifdef NO_CAS_CATCH
203   } catch(Standard_Failure) {
204 #else
205   } CASCatch_CATCH(Standard_Failure) {
206 #endif
207     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
208     ok = false;
209   }
210
211   if( !ok || !myExpr->IsDone() )
212     myExpr.Nullify();
213
214   myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
215 }
216
217 FunctionExpr::~FunctionExpr()
218 {
219 }
220
221 Standard_Boolean FunctionExpr::Value( Standard_Real T, Standard_Real& F )
222 {
223   double f;
224   Standard_Boolean res = value( T, f );
225   F = f;
226   return res;
227 }
228
229 bool FunctionExpr::value( const double t, double& f ) const
230 {
231   if( myExpr.IsNull() )
232     return false;
233
234   ( ( TColStd_Array1OfReal& )myValues ).ChangeValue( 1 ) = t;
235   bool ok = true;
236 #ifdef NO_CAS_CATCH
237   try {
238     OCC_CATCH_SIGNALS;
239 #else
240   CASCatch_TRY {
241 #endif
242     f = myExpr->Expression()->Evaluate( myVars, myValues );
243 #ifdef NO_CAS_CATCH
244   } catch(Standard_Failure) {
245 #else
246   } CASCatch_CATCH(Standard_Failure) {
247 #endif
248     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
249     f = 0.0;
250     ok = false;
251   }
252
253   ok = Function::value( t, f ) && ok;
254   return ok;
255 }
256
257 double FunctionExpr::integral( const double a, const double b ) const
258 {
259   double res = 0.0;
260 #ifdef NO_CAS_CATCH
261   try {
262     OCC_CATCH_SIGNALS;
263 #else
264   CASCatch_TRY {
265 #endif
266     math_GaussSingleIntegration _int
267       ( *static_cast<math_Function*>( const_cast<FunctionExpr*> (this) ), a, b, 20 );
268     if( _int.IsDone() )
269       res = _int.Value();
270 #ifdef NO_CAS_CATCH
271   } catch(Standard_Failure) {
272 #else
273   } CASCatch_CATCH(Standard_Failure) {
274 #endif
275     res = 0.0;
276     MESSAGE( "Exception in integral calculating" );
277   }
278   return res;
279 }
280
281 double dihotomySolve( Function& f, const double val, const double _start, const double _fin, const double eps, bool& ok )
282 {
283   double start = _start, fin = _fin, start_val, fin_val; bool ok1, ok2;
284   ok1 = f.value( start, start_val );
285   ok2 = f.value( fin, fin_val );
286
287   if( !ok1 || !ok2 )
288   {
289     ok = false;
290     return 0.0;
291   }
292
293   bool start_pos = start_val>=val, fin_pos = fin_val>=val;
294   ok = true;
295   
296   while( fin-start>eps )
297   {
298     double mid = ( start+fin )/2.0, mid_val;
299     ok = f.value( mid, mid_val );
300     if( !ok )
301       return 0.0;
302
303     //char buf[1024];
304     //sprintf( buf, "start=%f\nfin=%f\nmid_val=%f\n", float( start ), float( fin ), float( mid_val ) );
305     //MESSAGE( buf );
306
307     bool mid_pos = mid_val>=val;
308     if( start_pos!=mid_pos )
309     {
310       fin_pos = mid_pos;
311       fin = mid;
312     }
313     else if( fin_pos!=mid_pos )
314     {
315       start_pos = mid_pos;
316       start = mid;
317     }
318     else
319     {
320       ok = false;
321       break;
322     }
323   }
324   return (start+fin)/2.0;
325 }
326
327 bool buildDistribution( const TCollection_AsciiString& f, const int conv, const double start, const double end,
328                         const int nbSeg, vector<double>& data, const double eps )
329 {
330   FunctionExpr F( f.ToCString(), conv );
331   return buildDistribution( F, start, end, nbSeg, data, eps );
332 }
333
334 bool buildDistribution( const std::vector<double>& f, const int conv, const double start, const double end,
335                         const int nbSeg, vector<double>& data, const double eps )
336 {
337   FunctionTable F( f, conv );
338   return buildDistribution( F, start, end, nbSeg, data, eps );
339 }
340
341 bool buildDistribution( const Function& func, const double start, const double end, const int nbSeg,
342                         vector<double>& data, const double eps )
343 {
344   if( nbSeg<=0 )
345     return false;
346
347   data.resize( nbSeg+1 );
348   data[0] = start;
349   double J = func.integral( start, end ) / nbSeg;
350   if( J<1E-10 )
351     return false;
352
353   bool ok;
354   //MESSAGE( "distribution:" );
355   //char buf[1024];
356   for( int i=1; i<nbSeg; i++ )
357   {
358     FunctionIntegral f_int( &func, data[i-1] );
359     data[i] = dihotomySolve( f_int, J, data[i-1], end, eps, ok );
360     //sprintf( buf, "%f\n", float( data[i] ) );
361     //MESSAGE( buf );
362     if( !ok )
363       return false;
364   }
365
366   data[nbSeg] = end;
367   return true;
368 }