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