Salome HOME
Integrate improvement PAL13615 from V3_2_0_maintainance
[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     Function::value( t, f );
123     return true;
124   }
125       
126   double
127     x1 = myData[2*i1], y1 = myData[2*i1+1],
128     x2 = myData[2*i2], y2 = myData[2*i2+1];
129
130   Function::value( x1, y1 );
131   Function::value( x2, y2 );
132   
133   f = y1 + ( y2-y1 ) * ( t-x1 ) / ( x2-x1 );
134   return true;
135 }
136
137 double FunctionTable::integral( const int i ) const
138 {
139   if( i>=0 && i<myData.size()-1 )
140     return integral( i, myData[2*(i+1)]-myData[2*i] );
141   else
142     return 0;
143 }
144
145 double FunctionTable::integral( const int i, const double d ) const
146 {
147   double f1,f2, res = 0.0;
148   if( value( myData[2*i]+d, f1 ) )
149     if(!value(myData[2*i], f2)) {
150       f2 = myData[2*i+1];
151       Function::value( 1, f2 );
152     }
153   res = (f2+f1) * d / 2.0;
154   return res;
155 }
156
157 double FunctionTable::integral( const double a, const double b ) const
158 {
159   int x1s, x1f, x2s, x2f;
160   findBounds( a, x1s, x1f );
161   findBounds( b, x2s, x2f );
162   double J = 0;
163   for( int i=x1s; i<x2s; i++ )
164     J+=integral( i );
165   J-=integral( x1s, a-myData[2*x1s] );
166   J+=integral( x2s, b-myData[2*x2s] );
167   return J;
168 }
169
170 bool FunctionTable::findBounds( const double x, int& x_ind_1, int& x_ind_2 ) const
171 {
172   int n = myData.size() / 2;
173   if( n==0 || x<myData[0] )
174   {
175     x_ind_1 = x_ind_2 = 0;
176     return false;
177   }
178
179   for( int i=0; i<n-1; i++ )
180     if( myData[2*i]<=x && x<myData[2*(i+1)] )
181     {
182       x_ind_1 = i;
183       x_ind_2 = i+1;
184       return true;
185     }
186   x_ind_1 = n-1;
187   x_ind_2 = n-1;
188   return ( fabs( x - myData[2*x_ind_2] ) < 1.e-10 );
189 }
190
191 FunctionExpr::FunctionExpr( const char* str, const int conv )
192 : Function( conv ),
193   myVars( 1, 1 ),
194   myValues( 1, 1 )
195 {
196   bool ok = true;
197 #ifdef NO_CAS_CATCH
198   try {
199     OCC_CATCH_SIGNALS;
200 #else
201   CASCatch_TRY {
202 #endif
203     myExpr = ExprIntrp_GenExp::Create();
204     myExpr->Process( ( Standard_CString )str );
205 #ifdef NO_CAS_CATCH
206   } catch(Standard_Failure) {
207 #else
208   } CASCatch_CATCH(Standard_Failure) {
209 #endif
210     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
211     ok = false;
212   }
213
214   if( !ok || !myExpr->IsDone() )
215     myExpr.Nullify();
216
217   myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
218 }
219
220 FunctionExpr::~FunctionExpr()
221 {
222 }
223
224 Standard_Boolean FunctionExpr::Value( const Standard_Real T, Standard_Real& F )
225 {
226   double f;
227   Standard_Boolean res = value( T, f );
228   F = f;
229   return res;
230 }
231
232 bool FunctionExpr::value( const double t, double& f ) const
233 {
234   if( myExpr.IsNull() )
235     return false;
236
237   ( ( TColStd_Array1OfReal& )myValues ).ChangeValue( 1 ) = t;
238   bool ok = true;
239 #ifdef NO_CAS_CATCH
240   try {
241     OCC_CATCH_SIGNALS;
242 #else
243   CASCatch_TRY {
244 #endif
245     f = myExpr->Expression()->Evaluate( myVars, myValues );
246 #ifdef NO_CAS_CATCH
247   } catch(Standard_Failure) {
248 #else
249   } CASCatch_CATCH(Standard_Failure) {
250 #endif
251     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
252     f = 0.0;
253     ok = false;
254   }
255
256   ok = Function::value( t, f ) && ok;
257   return ok;
258 }
259
260 double FunctionExpr::integral( const double a, const double b ) const
261 {
262   double res = 0.0;
263 #ifdef NO_CAS_CATCH
264   try {
265     OCC_CATCH_SIGNALS;
266 #else
267   CASCatch_TRY {
268 #endif
269     math_GaussSingleIntegration _int
270       ( *static_cast<math_Function*>( const_cast<FunctionExpr*> (this) ), a, b, 20 );
271     if( _int.IsDone() )
272       res = _int.Value();
273 #ifdef NO_CAS_CATCH
274   } catch(Standard_Failure) {
275 #else
276   } CASCatch_CATCH(Standard_Failure) {
277 #endif
278     res = 0.0;
279     MESSAGE( "Exception in integral calculating" );
280   }
281   return res;
282 }
283
284 double dihotomySolve( Function& f, const double val, const double _start, const double _fin, const double eps, bool& ok )
285 {
286   double start = _start, fin = _fin, start_val, fin_val; bool ok1, ok2;
287   ok1 = f.value( start, start_val );
288   ok2 = f.value( fin, fin_val );
289
290   if( !ok1 || !ok2 )
291   {
292     ok = false;
293     return 0.0;
294   }
295
296   bool start_pos = start_val>=val, fin_pos = fin_val>=val;
297   ok = true;
298   
299   while( fin-start>eps )
300   {
301     double mid = ( start+fin )/2.0, mid_val;
302     ok = f.value( mid, mid_val );
303     if( !ok )
304       return 0.0;
305
306     //char buf[1024];
307     //sprintf( buf, "start=%f\nfin=%f\nmid_val=%f\n", float( start ), float( fin ), float( mid_val ) );
308     //MESSAGE( buf );
309
310     bool mid_pos = mid_val>=val;
311     if( start_pos!=mid_pos )
312     {
313       fin_pos = mid_pos;
314       fin = mid;
315     }
316     else if( fin_pos!=mid_pos )
317     {
318       start_pos = mid_pos;
319       start = mid;
320     }
321     else
322     {
323       ok = false;
324       break;
325     }
326   }
327   return (start+fin)/2.0;
328 }
329
330 bool buildDistribution( const TCollection_AsciiString& f, const int conv, const double start, const double end,
331                         const int nbSeg, vector<double>& data, const double eps )
332 {
333   FunctionExpr F( f.ToCString(), conv );
334   return buildDistribution( F, start, end, nbSeg, data, eps );
335 }
336
337 bool buildDistribution( const std::vector<double>& f, const int conv, const double start, const double end,
338                         const int nbSeg, vector<double>& data, const double eps )
339 {
340   FunctionTable F( f, conv );
341   return buildDistribution( F, start, end, nbSeg, data, eps );
342 }
343
344 bool buildDistribution( const Function& func, const double start, const double end, const int nbSeg,
345                         vector<double>& data, const double eps )
346 {
347   if( nbSeg<=0 )
348     return false;
349
350   data.resize( nbSeg+1 );
351   data[0] = start;
352   double J = func.integral( start, end ) / nbSeg;
353   if( J<1E-10 )
354     return false;
355
356   bool ok;
357   //MESSAGE( "distribution:" );
358   //char buf[1024];
359   for( int i=1; i<nbSeg; i++ )
360   {
361     FunctionIntegral f_int( &func, data[i-1] );
362     data[i] = dihotomySolve( f_int, J, data[i-1], end, eps, ok );
363     //sprintf( buf, "%f\n", float( data[i] ) );
364     //MESSAGE( buf );
365     if( !ok )
366       return false;
367   }
368
369   data[nbSeg] = end;
370   return true;
371 }