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