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