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