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