1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : StdMeshersGUI_DistrPreview.cxx
21 // Author : Open CASCADE S.A.S.
25 #include "StdMeshersGUI_DistrPreview.h"
28 #include <qwt_plot_curve.h>
29 #include <qwt_plot_marker.h>
30 #include <qwt_plot_grid.h>
31 #include <qwt_symbol.h>
32 #include <qwt_legend.h>
35 #include <Expr_NamedUnknown.hxx>
36 #include <Expr_GeneralExpression.hxx>
38 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
42 #include <Standard_Failure.hxx>
45 #include <Standard_ErrorHandler.hxx>
48 StdMeshersGUI_DistrPreview::StdMeshersGUI_DistrPreview( QWidget* p, StdMeshers::StdMeshers_NumberOfSegments_ptr h )
54 myConv( CUT_NEGATIVE ),
58 myHypo = StdMeshers::StdMeshers_NumberOfSegments::_duplicate( h );
59 myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
60 myDensity = new QwtPlotCurve( QString() );
61 myDensity->attach( this );
62 myDistr = new QwtPlotCurve( QString() );
63 myDistr->attach( this );
64 myMsg = new QwtPlotMarker();
65 myMsg->attach( this );
66 myMsg->setValue( 0.5, 0.5 );
67 QwtText mt = myMsg->label();
68 mt.setBackgroundPen( QPen( Qt::red, 1 ) );
70 f.setPointSize( 14 ); f.setBold( true );
72 myMsg->setLabel( mt );
73 myDensity->setPen( QPen( Qt::red, 1 ) );
76 myDistr->setPen( QPen( dc, 1 ) );
77 myDistr->setSymbol( QwtSymbol( QwtSymbol::XCross, QBrush( dc ), QPen( dc ), QSize( 5, 5 ) ) );
79 QwtLegend* l = legend();
81 l = new QwtLegend( this );
82 l->setFrameStyle( QFrame::Box | QFrame::Sunken );
84 insertLegend( l, QwtPlot::BottomLegend );
86 myDensity->setTitle( tr( "SMESH_DENSITY_FUNC" ) );
87 myDistr->setTitle( tr( "SMESH_DISTR" ) );
89 QwtPlotGrid* aGrid = new QwtPlotGrid();
90 QPen aMajPen = aGrid->majPen();
91 aMajPen.setStyle( Qt::DashLine );
92 aGrid->setPen( aMajPen );
94 aGrid->enableX( true );
95 aGrid->enableY( true );
97 aGrid->attach( this );
100 StdMeshersGUI_DistrPreview::~StdMeshersGUI_DistrPreview()
104 bool StdMeshersGUI_DistrPreview::isTableFunc() const
109 void StdMeshersGUI_DistrPreview::tableFunc( SMESH::double_array& f ) const
114 QString StdMeshersGUI_DistrPreview::function() const
119 int StdMeshersGUI_DistrPreview::nbSeg() const
124 int StdMeshersGUI_DistrPreview::pointsCount() const
129 void StdMeshersGUI_DistrPreview::setConversion( Conversion conv, const bool upd )
136 bool StdMeshersGUI_DistrPreview::setParams( const QString& func, const int nbSeg, const int points, const bool upd )
139 myTableFunc = SMESH::double_array();
140 myFunction = func.isEmpty() ? "0" : func;
141 myPoints = points>0 ? points : 2;
142 myNbSeg = nbSeg>0 ? nbSeg : 1;
143 bool res = init( func );
149 bool StdMeshersGUI_DistrPreview::setParams( const SMESH::double_array& f, const int nbSeg, const bool upd )
153 if( myTableFunc.length()%2==1 )
154 myTableFunc.length( myTableFunc.length()-1 );
157 myPoints = myTableFunc.length()/2;
158 myNbSeg = nbSeg>0 ? nbSeg : 1;
163 return myTableFunc.length()>0;
166 bool StdMeshersGUI_DistrPreview::createTable( SMESH::double_array& func )
168 if( myExpr.IsNull() )
174 const double xmin = 0.0, xmax = 1.0;
176 double d = (xmax-xmin)/double(myPoints-1);
177 func.length( 2*myPoints );
179 for( int i=0, j=0; i<myPoints; j++ )
182 double t = xmin + d*j, f = funcValue( t, ok );
192 func.length( func.length()-2*err );
196 void StdMeshersGUI_DistrPreview::update()
198 SMESH::double_array graph, distr;
205 myIsDone = createTable( graph );
207 if( graph.length()>=2 )
209 StdMeshers::StdMeshers_NumberOfSegments_var h =
210 StdMeshers::StdMeshers_NumberOfSegments::_narrow( myHypo );
212 if( !CORBA::is_nil( h.in() ) )
214 SMESH::double_array* arr = 0;
216 arr = h->BuildDistributionTab( myTableFunc, myNbSeg, ( int )myConv );
218 arr = h->BuildDistributionExpr( myFunction.toLatin1().data(), myNbSeg, ( int )myConv );
227 bool correct = graph.length()>=2 && distr.length()>=2;
235 QwtText mt = myMsg->label();
236 mt.setText( QString() );
237 myMsg->setLabel( mt );
240 int size = graph.length()/2;
241 double* x = new double[size], *y = new double[size];
242 double min_x, max_x, min_y, max_y;
243 for( int i=0; i<size; i++ )
247 if( !convert( y[i] ) )
249 min_x = 0.0; max_x = 1.0; min_y = 0.0; max_y = 1.0;
250 delete[] x; delete[] y;
255 if( i==0 || y[i]<min_y )
257 if( i==0 || y[i]>max_y )
259 if( i==0 || x[i]<min_x )
261 if( i==0 || x[i]>max_x )
265 setAxisScale( myDensity->xAxis(), min_x, max_x );
266 setAxisScale( myDensity->yAxis(), std::min( 0.0, min_y ), std::max( 0.0, max_y ) );
267 myDensity->setData( x, y, size );
274 size = distr.length();
275 x = new double[size];
276 y = new double[size];
277 for( int i=0; i<size; i++ )
282 myDistr->setData( x, y, size );
292 } catch(Standard_Failure) {
293 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
297 void StdMeshersGUI_DistrPreview::showError()
299 setAxisScale( myDensity->xAxis(), 0.0, 1.0 );
300 setAxisScale( myDensity->yAxis(), 0.0, 1.0 );
301 myDensity->setData( 0, 0, 0 );
302 myDistr->setData( 0, 0, 0 );
303 QwtText mt = myMsg->label();
304 mt.setText( tr( "SMESH_INVALID_FUNCTION" ) );
305 myMsg->setLabel( mt );
309 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
311 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
313 return sub->GetName()=="t";
316 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
318 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
319 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
322 if( name->GetName()!="t" )
326 res = isCorrectArg( sub );
331 bool StdMeshersGUI_DistrPreview::init( const QString& str )
333 bool parsed_ok = true;
338 myExpr = ExprIntrp_GenExp::Create();
339 myExpr->Process( ( Standard_CString ) str.toLatin1().data() );
340 } catch(Standard_Failure) {
341 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
345 bool syntax = false, args = false;
346 if( parsed_ok && myExpr->IsDone() )
349 args = isCorrectArg( myExpr->Expression() );
352 bool res = parsed_ok && syntax && args;
358 double StdMeshersGUI_DistrPreview::funcValue( const double t, bool& ok )
360 if( myExpr.IsNull() )
363 myValues.ChangeValue( 1 ) = t;
366 double res = calc( ok );
371 double StdMeshersGUI_DistrPreview::calc( bool& ok )
380 res = myExpr->Expression()->Evaluate( myVars, myValues );
381 } catch(Standard_Failure) {
382 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
390 bool StdMeshersGUI_DistrPreview::isDone() const
395 bool StdMeshersGUI_DistrPreview::convert( double& v ) const
406 // in StdMeshers_NumberOfSegments.cc
407 // const double PRECISION = 1e-7;
411 } catch(Standard_Failure) {
412 Handle(Standard_Failure) aFail = Standard_Failure::Caught();