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