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