Salome HOME
Merge remote branch 'origin/V7_dev'
[modules/smesh.git] / src / StdMeshers / StdMeshers_Distribution.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 #include <Expr_NamedUnknown.hxx>
40
41 #ifdef NO_CAS_CATCH
42 #include <Standard_ErrorHandler.hxx>
43 #endif
44
45 using namespace std;
46
47 namespace StdMeshers {
48
49 Function::Function( const int conv )
50 : myConv( conv )
51 {
52 }
53
54 Function::~Function()
55 {
56 }
57
58 bool Function::value( const double, double& f ) const
59 {
60   bool ok = true;
61   if (myConv == 0) {
62     try {
63 #ifdef NO_CAS_CATCH
64       OCC_CATCH_SIGNALS;
65 #endif
66       f = pow( 10., f );
67     } catch(Standard_Failure) {
68       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
69       f = 0.0;
70       ok = false;
71     }
72   }
73   else if( myConv==1 && f<0.0 )
74     f = 0.0;
75
76   return ok;
77 }
78
79 FunctionIntegral::FunctionIntegral( const Function* f, const double st )
80 : Function( -1 ),
81   myFunc( const_cast<Function*>( f ) ),
82   myStart( st )
83 {
84 }
85
86 FunctionIntegral::~FunctionIntegral()
87 {
88 }
89
90 bool FunctionIntegral::value( const double t, double& f ) const
91 {
92   f = myFunc ? myFunc->integral( myStart, t ) : 0;
93   return myFunc!=0 && Function::value( t, f );
94 }
95
96 double FunctionIntegral::integral( const double, const double ) const
97 {
98   return 0;
99 }
100
101 FunctionTable::FunctionTable( const std::vector<double>& data, const int conv )
102 : Function( conv )
103 {
104   myData = data;
105 }
106
107 FunctionTable::~FunctionTable()
108 {
109 }
110
111 bool FunctionTable::value( const double t, double& f ) const
112 {
113   int i1, i2;
114   if( !findBounds( t, i1, i2 ) )
115     return false;
116
117   if( i1==i2 ) {
118     f = myData[ 2*i1+1 ];
119     Function::value( t, f );
120     return true;
121   }
122       
123   double
124     x1 = myData[2*i1], y1 = myData[2*i1+1],
125     x2 = myData[2*i2], y2 = myData[2*i2+1];
126
127   Function::value( x1, y1 );
128   Function::value( x2, y2 );
129   
130   f = y1 + ( y2-y1 ) * ( t-x1 ) / ( x2-x1 );
131   return true;
132 }
133
134 double FunctionTable::integral( const int i ) const
135 {
136   if ( i >= 0  &&  i < (int)myData.size()-1 )
137     return integral( i, myData[2*(i+1)] - myData[2*i] );
138   else
139     return 0;
140 }
141
142 double FunctionTable::integral( const int i, const double d ) const
143 {
144   double f1,f2, res = 0.0;
145   if( value( myData[2*i]+d, f1 ) )
146     if(!value(myData[2*i], f2)) {
147       f2 = myData[2*i+1];
148       Function::value( 1, f2 );
149     }
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   try {
195 #ifdef NO_CAS_CATCH
196     OCC_CATCH_SIGNALS;
197 #endif
198     myExpr = ExprIntrp_GenExp::Create();
199     myExpr->Process( ( Standard_CString )str );
200   } catch(Standard_Failure) {
201     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
202     ok = false;
203   }
204
205   if( !ok || !myExpr->IsDone() )
206     myExpr.Nullify();
207
208   myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
209 }
210
211 FunctionExpr::~FunctionExpr()
212 {
213 }
214
215 Standard_Boolean FunctionExpr::Value( const Standard_Real T, Standard_Real& F )
216 {
217   double f;
218   Standard_Boolean res = value( T, f );
219   F = f;
220   return res;
221 }
222
223 bool FunctionExpr::value( const double t, double& f ) const
224 {
225   if( myExpr.IsNull() )
226     return false;
227
228   ( ( TColStd_Array1OfReal& )myValues ).ChangeValue( 1 ) = t;
229   bool ok = true;
230   try {
231 #ifdef NO_CAS_CATCH
232     OCC_CATCH_SIGNALS;
233 #endif
234     f = myExpr->Expression()->Evaluate( myVars, myValues );
235   } catch(Standard_Failure) {
236     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
237     f = 0.0;
238     ok = false;
239   }
240
241   ok = Function::value( t, f ) && ok;
242   return ok;
243 }
244
245 double FunctionExpr::integral( const double a, const double b ) const
246 {
247   double res = 0.0;
248   try {
249 #ifdef NO_CAS_CATCH
250     OCC_CATCH_SIGNALS;
251 #endif
252     math_GaussSingleIntegration _int
253       ( *static_cast<math_Function*>( const_cast<FunctionExpr*> (this) ), a, b, 20 );
254     if( _int.IsDone() )
255       res = _int.Value();
256   } catch(Standard_Failure) {
257     res = 0.0;
258     MESSAGE( "Exception in integral calculating" );
259   }
260   return res;
261 }
262
263 double dihotomySolve( Function& f, const double val, const double _start, const double _fin, const double eps, bool& ok )
264 {
265   double start = _start, fin = _fin, start_val, fin_val; bool ok1, ok2;
266   ok1 = f.value( start, start_val );
267   ok2 = f.value( fin, fin_val );
268
269   if( !ok1 || !ok2 )
270   {
271     ok = false;
272     return 0.0;
273   }
274
275   bool start_pos = start_val>=val, fin_pos = fin_val>=val;
276   ok = true;
277   
278   while( fin-start>eps )
279   {
280     double mid = ( start+fin )/2.0, mid_val;
281     ok = f.value( mid, mid_val );
282     if( !ok )
283       return 0.0;
284
285     //char buf[1024];
286     //sprintf( buf, "start=%f\nfin=%f\nmid_val=%f\n", float( start ), float( fin ), float( mid_val ) );
287     //MESSAGE( buf );
288
289     bool mid_pos = mid_val>=val;
290     if( start_pos!=mid_pos )
291     {
292       fin_pos = mid_pos;
293       fin = mid;
294     }
295     else if( fin_pos!=mid_pos )
296     {
297       start_pos = mid_pos;
298       start = mid;
299     }
300     else
301     {
302       ok = false;
303       break;
304     }
305   }
306   return (start+fin)/2.0;
307 }
308
309 bool buildDistribution( const TCollection_AsciiString& f, const int conv, const double start, const double end,
310                         const int nbSeg, vector<double>& data, const double eps )
311 {
312   FunctionExpr F( f.ToCString(), conv );
313   return buildDistribution( F, start, end, nbSeg, data, eps );
314 }
315
316 bool buildDistribution( const std::vector<double>& f, const int conv, const double start, const double end,
317                         const int nbSeg, vector<double>& data, const double eps )
318 {
319   FunctionTable F( f, conv );
320   return buildDistribution( F, start, end, nbSeg, data, eps );
321 }
322
323 bool buildDistribution( const Function& func, const double start, const double end, const int nbSeg,
324                         vector<double>& data, const double eps )
325 {
326   if( nbSeg<=0 )
327     return false;
328
329   data.resize( nbSeg+1 );
330   data[0] = start;
331   double J = func.integral( start, end ) / nbSeg;
332   if( J<1E-10 )
333     return false;
334
335   bool ok;
336   //MESSAGE( "distribution:" );
337   //char buf[1024];
338   for( int i=1; i<nbSeg; i++ )
339   {
340     FunctionIntegral f_int( &func, data[i-1] );
341     data[i] = dihotomySolve( f_int, J, data[i-1], end, eps, ok );
342     //sprintf( buf, "%f\n", float( data[i] ) );
343     //MESSAGE( buf );
344     if( !ok )
345       return false;
346   }
347
348   data[nbSeg] = end;
349   return true;
350 }
351 }