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