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