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