Salome HOME
c24e450e12c64c53719d4e87c8b1fb7af3e581ce
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_DistrPreview.cxx
1 // Copyright (C) 2007-2013  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
23 // File   : StdMeshersGUI_DistrPreview.cxx
24 // Author : Open CASCADE S.A.S.
25 // SMESH includes
26 //
27 #include "StdMeshersGUI_DistrPreview.h"
28
29 // Qwt includes
30 #include <qwt_plot_curve.h>
31 #include <qwt_plot_marker.h>
32 #include <qwt_plot_grid.h>
33 #include <qwt_symbol.h>
34 #include <qwt_legend.h>
35
36 // OCCT includes
37 #include <Expr_NamedUnknown.hxx>
38 #include <Expr_GeneralExpression.hxx>
39
40 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
41 #define NO_CAS_CATCH
42 #endif
43
44 #include <Standard_Failure.hxx>
45
46 #ifdef NO_CAS_CATCH
47 #include <Standard_ErrorHandler.hxx>
48 #endif
49
50 #ifdef WIN32
51 # include <algorithm>
52 #endif
53 #include <math.h>
54 #include <limits>
55
56 #include <Basics_Utils.hxx>
57
58 StdMeshersGUI_DistrPreview::StdMeshersGUI_DistrPreview( QWidget* p, StdMeshers::StdMeshers_NumberOfSegments_ptr h )
59 : QwtPlot( p ),
60   myPoints( 50 ),
61   myIsTable( false ),
62   myVars( 1, 1 ),
63   myValues( 1, 1 ),
64   myConv( CUT_NEGATIVE ),
65   myIsDone( true ),
66   myNbSeg( 1 )
67 {
68   Kernel_Utils::Localizer loc;
69   myHypo = StdMeshers::StdMeshers_NumberOfSegments::_duplicate( h );
70   myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
71   myDensity = new QwtPlotCurve( QString() );
72   myDensity->attach( this );
73   myDistr = new QwtPlotCurve( QString() );
74   myDistr->attach( this );
75   myMsg = new QwtPlotMarker();
76   myMsg->attach( this );
77   myMsg->setValue( 0.5, 0.5 );
78   QwtText mt = myMsg->label();
79   mt.setBackgroundPen( QPen( Qt::red, 1 ) );
80   QFont f = mt.font();
81   f.setPointSize( 14 ); //f.setBold( true );
82   mt.setFont( f );
83   myMsg->setLabel( mt );
84   myDensity->setPen( QPen( Qt::red, 1 ) );
85
86   QColor dc = Qt::blue;
87   myDistr->setPen( QPen( dc, 1 ) );
88   myDistr->setSymbol( QwtSymbol( QwtSymbol::XCross, QBrush( dc ), QPen( dc ), QSize( 5, 5 ) ) );
89
90   QwtLegend* l = legend();
91   if ( !l ) {
92     l = new QwtLegend( this );
93     l->setFrameStyle( QFrame::Box | QFrame::Sunken );
94   }
95   insertLegend( l, QwtPlot::BottomLegend );
96
97   enableAxis(QwtPlot::yLeft, false);
98   enableAxis(QwtPlot::yRight, true);
99   
100   QFont axisFont;
101   axisFont.setPointSize( 8 );
102   setAxisFont(QwtPlot::yRight, axisFont); 
103   setAxisFont(QwtPlot::xBottom, axisFont); 
104   
105   myDensity->setYAxis(QwtPlot::yRight);
106   myDistr->setYAxis(QwtPlot::yRight);
107   myMsg->setYAxis(QwtPlot::yRight);
108   myDensity->setTitle( tr( "SMESH_DENSITY_FUNC" ) );
109   myDistr->setTitle( tr( "SMESH_DISTR" ) );
110   
111   QwtPlotGrid* aGrid = new QwtPlotGrid();
112   QPen aMajPen = aGrid->majPen();
113   aMajPen.setStyle( Qt::DashLine );
114   aGrid->setPen( aMajPen );
115
116   aGrid->enableX( true );
117   aGrid->enableY( true );
118
119   aGrid->attach( this );
120 }
121
122 StdMeshersGUI_DistrPreview::~StdMeshersGUI_DistrPreview()
123 {
124 }
125
126 bool StdMeshersGUI_DistrPreview::isTableFunc() const
127 {
128   return myIsTable;
129 }
130
131 void StdMeshersGUI_DistrPreview::tableFunc( SMESH::double_array& f ) const
132 {
133   f = myTableFunc;
134 }
135
136 QString StdMeshersGUI_DistrPreview::function() const
137 {
138   return myFunction;
139 }
140
141 int StdMeshersGUI_DistrPreview::nbSeg() const
142 {
143   return myNbSeg;
144 }
145
146 int StdMeshersGUI_DistrPreview::pointsCount() const
147 {
148   return myPoints;
149 }
150
151 void StdMeshersGUI_DistrPreview::setConversion( Conversion conv, const bool upd )
152 {
153   myConv = conv;
154   if( upd )
155     update();
156 }
157
158 bool StdMeshersGUI_DistrPreview::setParams( const QString& func, const int nbSeg, const int points, const bool upd )
159 {
160   myIsTable = false;
161   myTableFunc = SMESH::double_array();
162   myFunction = func.isEmpty() ? "0" : func;
163   myPoints = points>0 ? points : 2;
164   myNbSeg = nbSeg>0 ? nbSeg : 1;
165   bool res = init( func );
166   if( upd )
167     update();
168   return res;
169 }
170
171 bool StdMeshersGUI_DistrPreview::setParams( const SMESH::double_array& f, const int nbSeg, const bool upd )
172 {
173   myIsTable = true;
174   myTableFunc = f;
175   if( myTableFunc.length()%2==1 )
176     myTableFunc.length( myTableFunc.length()-1 );
177
178   myFunction = "0";
179   myPoints = myTableFunc.length()/2;
180   myNbSeg = nbSeg>0 ? nbSeg : 1;
181
182   if( upd )
183     update();
184
185   return myTableFunc.length()>0;
186 }
187
188 bool StdMeshersGUI_DistrPreview::createTable( SMESH::double_array& func )
189 {
190   if( myExpr.IsNull() )
191   {
192     func.length( 0 );
193     return false;
194   }
195
196   const double xmin = 0.0, xmax = 1.0;
197
198   double d = (xmax-xmin)/double(myPoints-1);
199   func.length( 2*myPoints );
200   int err = 0;
201   for( int i=0, j=0; i<myPoints; j++ )
202   {
203     bool ok;
204     double t = xmin + d*j, f = funcValue( t, ok );
205     if( ok )
206     {
207       func[2*i] = t;
208       func[2*i+1] = f;
209       i++;
210     }
211     else
212       err++;
213   }
214   func.length( func.length()-2*err );
215   return err==0;
216 }
217
218 void StdMeshersGUI_DistrPreview::update()
219 {
220   Kernel_Utils::Localizer loc;
221   SMESH::double_array graph, distr;
222   if( isTableFunc() )
223   {
224     myIsDone = true;
225     graph = myTableFunc;
226   }
227   else
228     myIsDone = createTable( graph );
229
230   if( graph.length()>=2 )
231   {
232     StdMeshers::StdMeshers_NumberOfSegments_var h = 
233       StdMeshers::StdMeshers_NumberOfSegments::_narrow( myHypo );
234
235     if( !CORBA::is_nil( h.in() ) )
236     {
237       SMESH::double_array* arr = 0;
238       if( isTableFunc() )
239         arr = h->BuildDistributionTab( myTableFunc, myNbSeg, ( int )myConv );
240       else
241         arr = h->BuildDistributionExpr( myFunction.toLatin1().data(), myNbSeg, ( int )myConv );
242       if( arr )
243       {
244         distr = *arr;
245         delete arr;
246       }
247     }
248   }
249
250   bool correct = graph.length()>=2 && distr.length()>=2;
251   if( !correct )
252   {
253     showError();
254     return;
255   }
256   else
257   {
258     QwtText mt = myMsg->label();
259     mt.setText( QString() );
260     myMsg->setLabel( mt );
261   }
262
263   int size = graph.length()/2;
264   double* x = new double[size], *y = new double[size];
265   double min_x, max_x, min_y, max_y;
266   for( int i=0; i<size; i++ )
267   {
268     x[i] = graph[2*i];
269     y[i] = graph[2*i+1];
270     if( !convert( y[i] ) )
271     {
272       min_x = 0.0; max_x = 1.0; min_y = 0.0; max_y = 1.0;
273       delete[] x; delete[] y;
274       x = y = 0;
275       showError();
276       return;
277     }
278 #ifdef WIN32
279     if ( std::fabs(y[i]) >= HUGE_VAL)
280       y[i] = HUGE_VAL/100.;
281 #else
282     if ( isinf(y[i]))
283       y[i] = std::numeric_limits<double>::max()/100.;
284 #endif
285 //     if ( y[i] > 1e3 )
286 //       y[i] = 1e3;
287     if( i==0 || y[i]<min_y )
288       min_y = y[i];
289     if( i==0 || y[i]>max_y )
290       max_y = y[i];
291     if( i==0 || x[i]<min_x )
292       min_x = x[i];
293     if( i==0 || x[i]>max_x )
294       max_x = x[i];
295   }
296
297   setAxisScale( myDensity->xAxis(), min_x, max_x );
298   setAxisScale( myDensity->yAxis(),
299 #ifdef WIN32
300     min( 0.0, min_y ),
301     max( 0.0, max_y )
302 #else
303     std::min( 0.0, min_y ),
304     std::max( 0.0, max_y )
305 #endif
306     );
307   myDensity->setData( x, y, size );
308   if( x )
309     delete[] x;
310   if( y )
311     delete[] y;
312   x = y = 0;
313
314   size = distr.length();
315   x = new double[size];
316   y = new double[size];
317   for( int i=0; i<size; i++ )
318   {
319     x[i] = distr[i];
320     y[i] = 0;
321   }
322   myDistr->setData( x, y, size );
323   delete[] x;
324   delete[] y;
325   x = y = 0;
326
327   try {   
328 #ifdef NO_CAS_CATCH
329     OCC_CATCH_SIGNALS;
330 #endif
331     replot();
332   } catch(Standard_Failure) {
333     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
334   }
335 }
336
337 void StdMeshersGUI_DistrPreview::showError()
338 {
339   setAxisScale( myDensity->xAxis(), 0.0, 1.0 );
340   setAxisScale( myDensity->yAxis(), 0.0, 1.0 );
341   myDensity->setData( 0, 0, 0 );
342   myDistr->setData( 0, 0, 0 );
343   QwtText mt = myMsg->label();
344   mt.setText( tr( "SMESH_INVALID_FUNCTION" ) );
345   myMsg->setLabel( mt );
346   replot();
347 }
348
349 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
350 {
351   Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
352   if( !sub.IsNull() )
353     return sub->GetName()=="t";
354
355   bool res = true;
356   for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
357   {
358     Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
359     Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
360     if( !name.IsNull() )
361     {
362       if( name->GetName()!="t" )
363         res = false;
364     }
365     else
366       res = isCorrectArg( sub );
367   }
368   return res;
369 }
370
371 bool StdMeshersGUI_DistrPreview::init( const QString& str )
372 {
373   Kernel_Utils::Localizer loc;
374   bool parsed_ok = true;
375   try {
376 #ifdef NO_CAS_CATCH
377     OCC_CATCH_SIGNALS;
378 #endif
379     myExpr = ExprIntrp_GenExp::Create();
380     myExpr->Process( ( Standard_CString ) str.toLatin1().data() );
381   } catch(Standard_Failure) {
382     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
383     parsed_ok = false;
384   }
385
386   bool syntax = false, args = false;
387   if( parsed_ok && myExpr->IsDone() )
388   {
389     syntax = true;
390     args = isCorrectArg( myExpr->Expression() );
391   }
392
393   bool res = parsed_ok && syntax && args;
394   if( !res )
395     myExpr.Nullify();
396   return res;
397 }
398
399 double StdMeshersGUI_DistrPreview::funcValue( const double t, bool& ok )
400 {
401   if( myExpr.IsNull() )
402     return 0;
403
404   myValues.ChangeValue( 1 ) = t;
405
406   ok = true;
407   double res = calc( ok );
408
409   return res;
410 }
411
412 double StdMeshersGUI_DistrPreview::calc( bool& ok )
413 {
414   double res = 0.0;
415
416   ok = true;
417   try {   
418 #ifdef NO_CAS_CATCH
419     OCC_CATCH_SIGNALS;
420 #endif
421     res = myExpr->Expression()->Evaluate( myVars, myValues );
422   } catch(Standard_Failure) {
423     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
424     ok = false;
425     res = 0.0;
426   }
427
428   return res;
429 }
430
431 bool StdMeshersGUI_DistrPreview::isDone() const
432 {
433   return myIsDone;
434 }
435
436 bool StdMeshersGUI_DistrPreview::convert( double& v ) const
437 {
438   bool ok = true;
439   switch( myConv )
440   {
441   case EXPONENT:
442     {
443       try { 
444 #ifdef NO_CAS_CATCH
445         OCC_CATCH_SIGNALS;
446 #endif
447         // in StdMeshers_NumberOfSegments.cc
448         // const double PRECISION = 1e-7;
449         //
450         if(v < -7) v = -7.0;
451         v = pow( 10.0, v );
452       } catch(Standard_Failure) {
453         Handle(Standard_Failure) aFail = Standard_Failure::Caught();
454         v = 0.0;
455         ok = false;
456       }
457     }
458     break;
459
460   case CUT_NEGATIVE:
461     if( v<0 )
462       v = 0;
463     break;
464   }
465
466   return ok;
467 }