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