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