Salome HOME
a723fdbef364eae9767b5705ef39c31204aa05fd
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_DistrPreview.cxx
1 //  Copyright (C) 2007-2010  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   myDensity->setTitle( tr( "SMESH_DENSITY_FUNC" ) );
98   myDistr->setTitle( tr( "SMESH_DISTR" ) );
99   
100   QwtPlotGrid* aGrid = new QwtPlotGrid();
101   QPen aMajPen = aGrid->majPen();
102   aMajPen.setStyle( Qt::DashLine );
103   aGrid->setPen( aMajPen );
104
105   aGrid->enableX( true );
106   aGrid->enableY( true );
107
108   aGrid->attach( this );
109 }
110
111 StdMeshersGUI_DistrPreview::~StdMeshersGUI_DistrPreview()
112 {
113 }
114
115 bool StdMeshersGUI_DistrPreview::isTableFunc() const
116 {
117   return myIsTable;
118 }
119
120 void StdMeshersGUI_DistrPreview::tableFunc( SMESH::double_array& f ) const
121 {
122   f = myTableFunc;
123 }
124
125 QString StdMeshersGUI_DistrPreview::function() const
126 {
127   return myFunction;
128 }
129
130 int StdMeshersGUI_DistrPreview::nbSeg() const
131 {
132   return myNbSeg;
133 }
134
135 int StdMeshersGUI_DistrPreview::pointsCount() const
136 {
137   return myPoints;
138 }
139
140 void StdMeshersGUI_DistrPreview::setConversion( Conversion conv, const bool upd )
141 {
142   myConv = conv;
143   if( upd )
144     update();
145 }
146
147 bool StdMeshersGUI_DistrPreview::setParams( const QString& func, const int nbSeg, const int points, const bool upd )
148 {
149   myIsTable = false;
150   myTableFunc = SMESH::double_array();
151   myFunction = func.isEmpty() ? "0" : func;
152   myPoints = points>0 ? points : 2;
153   myNbSeg = nbSeg>0 ? nbSeg : 1;
154   bool res = init( func );
155   if( upd )
156     update();
157   return res;
158 }
159
160 bool StdMeshersGUI_DistrPreview::setParams( const SMESH::double_array& f, const int nbSeg, const bool upd )
161 {
162   myIsTable = true;
163   myTableFunc = f;
164   if( myTableFunc.length()%2==1 )
165     myTableFunc.length( myTableFunc.length()-1 );
166
167   myFunction = "0";
168   myPoints = myTableFunc.length()/2;
169   myNbSeg = nbSeg>0 ? nbSeg : 1;
170
171   if( upd )
172     update();
173
174   return myTableFunc.length()>0;
175 }
176
177 bool StdMeshersGUI_DistrPreview::createTable( SMESH::double_array& func )
178 {
179   if( myExpr.IsNull() )
180   {
181     func.length( 0 );
182     return false;
183   }
184
185   const double xmin = 0.0, xmax = 1.0;
186
187   double d = (xmax-xmin)/double(myPoints-1);
188   func.length( 2*myPoints );
189   int err = 0;
190   for( int i=0, j=0; i<myPoints; j++ )
191   {
192     bool ok;
193     double t = xmin + d*j, f = funcValue( t, ok );
194     if( ok )
195     {
196       func[2*i] = t;
197       func[2*i+1] = f;
198       i++;
199     }
200     else
201       err++;
202   }
203   func.length( func.length()-2*err );
204   return err==0;
205 }
206
207 void StdMeshersGUI_DistrPreview::update()
208 {
209   Kernel_Utils::Localizer loc;
210   SMESH::double_array graph, distr;
211   if( isTableFunc() )
212   {
213     myIsDone = true;
214     graph = myTableFunc;
215   }
216   else
217     myIsDone = createTable( graph );
218
219   if( graph.length()>=2 )
220   {
221     StdMeshers::StdMeshers_NumberOfSegments_var h = 
222       StdMeshers::StdMeshers_NumberOfSegments::_narrow( myHypo );
223
224     if( !CORBA::is_nil( h.in() ) )
225     {
226       SMESH::double_array* arr = 0;
227       if( isTableFunc() )
228         arr = h->BuildDistributionTab( myTableFunc, myNbSeg, ( int )myConv );
229       else
230         arr = h->BuildDistributionExpr( myFunction.toLatin1().data(), myNbSeg, ( int )myConv );
231       if( arr )
232       {
233         distr = *arr;
234         delete arr;
235       }
236     }
237   }
238
239   bool correct = graph.length()>=2 && distr.length()>=2;
240   if( !correct )
241   {
242     showError();
243     return;
244   }
245   else
246   {
247     QwtText mt = myMsg->label();
248     mt.setText( QString() );
249     myMsg->setLabel( mt );
250   }
251
252   int size = graph.length()/2;
253   double* x = new double[size], *y = new double[size];
254   double min_x, max_x, min_y, max_y;
255   for( int i=0; i<size; i++ )
256   {
257     x[i] = graph[2*i];
258     y[i] = graph[2*i+1];
259     if( !convert( y[i] ) )
260     {
261       min_x = 0.0; max_x = 1.0; min_y = 0.0; max_y = 1.0;
262       delete[] x; delete[] y;
263       x = y = 0;
264       showError();
265       return;
266     }
267     if ( isinf(y[i]))
268       y[i] = std::numeric_limits<double>::max()/100.;
269 //     if ( y[i] > 1e3 )
270 //       y[i] = 1e3;
271     if( i==0 || y[i]<min_y )
272       min_y = y[i];
273     if( i==0 || y[i]>max_y )
274       max_y = y[i];
275     if( i==0 || x[i]<min_x )
276       min_x = x[i];
277     if( i==0 || x[i]>max_x )
278       max_x = x[i];
279   }
280
281   setAxisScale( myDensity->xAxis(), min_x, max_x );
282   setAxisScale( myDensity->yAxis(),
283 #ifdef WIN32
284     min( 0.0, min_y ),
285     max( 0.0, max_y )
286 #else
287     std::min( 0.0, min_y ),
288     std::max( 0.0, max_y )
289 #endif
290     );
291   myDensity->setData( x, y, size );
292   if( x )
293     delete[] x;
294   if( y )
295     delete[] y;
296   x = y = 0;
297
298   size = distr.length();
299   x = new double[size];
300   y = new double[size];
301   for( int i=0; i<size; i++ )
302   {
303     x[i] = distr[i];
304     y[i] = 0;
305   }
306   myDistr->setData( x, y, size );
307   delete[] x;
308   delete[] y;
309   x = y = 0;
310
311   try {   
312 #ifdef NO_CAS_CATCH
313     OCC_CATCH_SIGNALS;
314 #endif
315     replot();
316   } catch(Standard_Failure) {
317     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
318   }
319 }
320
321 void StdMeshersGUI_DistrPreview::showError()
322 {
323   setAxisScale( myDensity->xAxis(), 0.0, 1.0 );
324   setAxisScale( myDensity->yAxis(), 0.0, 1.0 );
325   myDensity->setData( 0, 0, 0 );
326   myDistr->setData( 0, 0, 0 );
327   QwtText mt = myMsg->label();
328   mt.setText( tr( "SMESH_INVALID_FUNCTION" ) );
329   myMsg->setLabel( mt );
330   replot();
331 }
332
333 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
334 {
335   Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
336   if( !sub.IsNull() )
337     return sub->GetName()=="t";
338
339   bool res = true;
340   for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
341   {
342     Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
343     Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
344     if( !name.IsNull() )
345     {
346       if( name->GetName()!="t" )
347         res = false;
348     }
349     else
350       res = isCorrectArg( sub );
351   }
352   return res;
353 }
354
355 bool StdMeshersGUI_DistrPreview::init( const QString& str )
356 {
357   Kernel_Utils::Localizer loc;
358   bool parsed_ok = true;
359   try {
360 #ifdef NO_CAS_CATCH
361     OCC_CATCH_SIGNALS;
362 #endif
363     myExpr = ExprIntrp_GenExp::Create();
364     myExpr->Process( ( Standard_CString ) str.toLatin1().data() );
365   } catch(Standard_Failure) {
366     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
367     parsed_ok = false;
368   }
369
370   bool syntax = false, args = false;
371   if( parsed_ok && myExpr->IsDone() )
372   {
373     syntax = true;
374     args = isCorrectArg( myExpr->Expression() );
375   }
376
377   bool res = parsed_ok && syntax && args;
378   if( !res )
379     myExpr.Nullify();
380   return res;
381 }
382
383 double StdMeshersGUI_DistrPreview::funcValue( const double t, bool& ok )
384 {
385   if( myExpr.IsNull() )
386     return 0;
387
388   myValues.ChangeValue( 1 ) = t;
389
390   ok = true;
391   double res = calc( ok );
392
393   return res;
394 }
395
396 double StdMeshersGUI_DistrPreview::calc( bool& ok )
397 {
398   double res = 0.0;
399
400   ok = true;
401   try {   
402 #ifdef NO_CAS_CATCH
403     OCC_CATCH_SIGNALS;
404 #endif
405     res = myExpr->Expression()->Evaluate( myVars, myValues );
406   } catch(Standard_Failure) {
407     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
408     ok = false;
409     res = 0.0;
410   }
411
412   return res;
413 }
414
415 bool StdMeshersGUI_DistrPreview::isDone() const
416 {
417   return myIsDone;
418 }
419
420 bool StdMeshersGUI_DistrPreview::convert( double& v ) const
421 {
422   bool ok = true;
423   switch( myConv )
424   {
425   case EXPONENT:
426     {
427       try { 
428 #ifdef NO_CAS_CATCH
429         OCC_CATCH_SIGNALS;
430 #endif
431         // in StdMeshers_NumberOfSegments.cc
432         // const double PRECISION = 1e-7;
433         //
434         if(v < -7) v = -7.0;
435         v = pow( 10.0, v );
436       } catch(Standard_Failure) {
437         Handle(Standard_Failure) aFail = Standard_Failure::Caught();
438         v = 0.0;
439         ok = false;
440       }
441     }
442     break;
443
444   case CUT_NEGATIVE:
445     if( v<0 )
446       v = 0;
447     break;
448   }
449
450   return ok;
451 }