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