Salome HOME
Consider OCCT version
[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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
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 <Standard_ErrorHandler.hxx>
36
37 Function::Function( const int conv )
38 : myConv( conv )
39 {
40 }
41
42 Function::~Function()
43 {
44 }
45
46 bool Function::value( const double, double& f ) const
47 {
48   bool ok = true;
49   if( myConv==0 )
50   {
51     try {
52 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
53       OCC_CATCH_SIGNALS;
54 #endif
55       f = pow( 10, f );
56     }
57     catch(Standard_Failure)
58     {
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   double
109     x1 = myData[2*i1], y1 = myData[2*i1+1],
110     x2 = myData[2*i2], y2 = myData[2*i2+1];
111
112   Function::value( x1, y1 );
113   Function::value( x2, y2 );
114   
115   f = y1 + ( y2-y1 ) * ( t-x1 ) / ( x2-x1 );
116   return true;
117 }
118
119 double FunctionTable::integral( const int i ) const
120 {
121   if( i>=0 && i<myData.size()-1 )
122     return integral( i, myData[2*(i+1)]-myData[2*i] );
123   else
124     return 0;
125 }
126
127 double FunctionTable::integral( const int i, const double d ) const
128 {
129   double f1,f2, res = 0.0;
130   if( value( myData[2*i]+d, f1 ) )
131     if(!value(myData[2*i], f2))
132       f2 = myData[2*i+1];
133   res = (f2+f1) * d / 2.0;
134   return res;
135 }
136
137 double FunctionTable::integral( const double a, const double b ) const
138 {
139   int x1s, x1f, x2s, x2f;
140   findBounds( a, x1s, x1f );
141   findBounds( b, x2s, x2f );
142   double J = 0;
143   for( int i=x1s; i<x2s; i++ )
144     J+=integral( i );
145   J-=integral( x1s, a-myData[2*x1s] );
146   J+=integral( x2s, b-myData[2*x2s] );
147   return J;
148 }
149
150 bool FunctionTable::findBounds( const double x, int& x_ind_1, int& x_ind_2 ) const
151 {
152   int n = myData.size() / 2;
153   if( n==0 || x<myData[0] )
154   {
155     x_ind_1 = x_ind_2 = 0;
156     return false;
157   }
158
159   for( int i=0; i<n-1; i++ )
160     if( myData[2*i]<=x && x<=myData[2*(i+1)] )
161     {
162       x_ind_1 = i;
163       x_ind_2 = i+1;
164       return true;
165     }
166   x_ind_1 = n-1;
167   x_ind_2 = n-1;
168   return false;
169 }
170
171 FunctionExpr::FunctionExpr( const char* str, const int conv )
172 : Function( conv ),
173   myVars( 1, 1 ),
174   myValues( 1, 1 )
175 {
176   bool ok = true;
177   try {
178 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
179     OCC_CATCH_SIGNALS;
180 #endif
181     myExpr = ExprIntrp_GenExp::Create();
182     myExpr->Process( ( Standard_CString )str );
183   }
184   catch(Standard_Failure)
185   {
186     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
187     ok = false;
188   }
189
190   if( !ok || !myExpr->IsDone() )
191     myExpr.Nullify();
192
193   myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
194 }
195
196 FunctionExpr::~FunctionExpr()
197 {
198 }
199
200 Standard_Boolean FunctionExpr::Value( Standard_Real T, Standard_Real& F )
201 {
202   double f;
203   Standard_Boolean res = value( T, f );
204   F = f;
205   return res;
206 }
207
208 bool FunctionExpr::value( const double t, double& f ) const
209 {
210   if( myExpr.IsNull() )
211     return false;
212
213   ( ( TColStd_Array1OfReal& )myValues ).ChangeValue( 1 ) = t;
214   bool ok = true;
215   try {
216 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
217     OCC_CATCH_SIGNALS;
218 #endif
219     f = myExpr->Expression()->Evaluate( myVars, myValues );
220   }
221   catch(Standard_Failure) {
222     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
223     f = 0.0;
224     ok = false;
225   }
226
227   ok = Function::value( t, f ) && ok;
228   return ok;
229 }
230
231 double FunctionExpr::integral( const double a, const double b ) const
232 {
233   double res = 0.0;
234   try {
235 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
236     OCC_CATCH_SIGNALS;
237 #endif
238     math_GaussSingleIntegration _int( ( math_Function& )*this, a, b, 20 );
239     if( _int.IsDone() )
240       res = _int.Value();
241   }
242   catch(Standard_Failure)
243   {
244     res = 0.0;
245     MESSAGE( "Exception in integral calculating" );
246   }
247   return res;
248 }
249
250 double dihotomySolve( Function& f, const double val, const double _start, const double _fin, const double eps, bool& ok )
251 {
252   double start = _start, fin = _fin, start_val, fin_val; bool ok1, ok2;
253   ok1 = f.value( start, start_val );
254   ok2 = f.value( fin, fin_val );
255
256   if( !ok1 || !ok2 )
257   {
258     ok = false;
259     return 0.0;
260   }
261
262   bool start_pos = start_val>=val, fin_pos = fin_val>=val;
263   ok = true;
264   
265   while( fin-start>eps )
266   {
267     double mid = ( start+fin )/2.0, mid_val;
268     ok = f.value( mid, mid_val );
269     if( !ok )
270       return 0.0;
271
272     //char buf[1024];
273     //sprintf( buf, "start=%f\nfin=%f\nmid_val=%f\n", float( start ), float( fin ), float( mid_val ) );
274     //MESSAGE( buf );
275
276     bool mid_pos = mid_val>=val;
277     if( start_pos!=mid_pos )
278     {
279       fin_pos = mid_pos;
280       fin = mid;
281     }
282     else if( fin_pos!=mid_pos )
283     {
284       start_pos = mid_pos;
285       start = mid;
286     }
287     else
288     {
289       ok = false;
290       break;
291     }
292   }
293   return (start+fin)/2.0;
294 }
295
296 bool buildDistribution( const TCollection_AsciiString& f, const int conv, const double start, const double end,
297                         const int nbSeg, vector<double>& data, const double eps )
298 {
299   FunctionExpr F( f.ToCString(), conv );
300   return buildDistribution( F, start, end, nbSeg, data, eps );
301 }
302
303 bool buildDistribution( const std::vector<double>& f, const int conv, const double start, const double end,
304                         const int nbSeg, vector<double>& data, const double eps )
305 {
306   FunctionTable F( f, conv );
307   return buildDistribution( F, start, end, nbSeg, data, eps );
308 }
309
310 bool buildDistribution( const Function& func, const double start, const double end, const int nbSeg,
311                         vector<double>& data, const double eps )
312 {
313   if( nbSeg<=0 )
314     return false;
315
316   data.resize( nbSeg+1 );
317   data[0] = start;
318   double J = func.integral( start, end ) / nbSeg;
319   if( J<1E-10 )
320     return false;
321
322   bool ok;
323   //MESSAGE( "distribution:" );
324   //char buf[1024];
325   for( int i=1; i<nbSeg; i++ )
326   {
327     FunctionIntegral f_int( &func, data[i-1] );
328     data[i] = dihotomySolve( f_int, J, data[i-1], end, eps, ok );
329     //sprintf( buf, "%f\n", float( data[i] ) );
330     //MESSAGE( buf );
331     if( !ok )
332       return false;
333   }
334
335   data[nbSeg] = end;
336   return true;
337 }