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