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