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