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