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