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