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>
52 StdMeshersGUI_DistrPreview::StdMeshersGUI_DistrPreview( QWidget* p, StdMeshers::StdMeshers_NumberOfSegments_ptr h )
58 myConv( CUT_NEGATIVE ),
62 myHypo = StdMeshers::StdMeshers_NumberOfSegments::_duplicate( h );
63 myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
64 myDensity = new QwtPlotCurve( QString() );
65 myDensity->attach( this );
66 myDistr = new QwtPlotCurve( QString() );
67 myDistr->attach( this );
68 myMsg = new QwtPlotMarker();
69 myMsg->attach( this );
70 myMsg->setValue( 0.5, 0.5 );
71 QwtText mt = myMsg->label();
72 mt.setBackgroundPen( QPen( Qt::red, 1 ) );
74 f.setPointSize( 14 ); f.setBold( true );
76 myMsg->setLabel( mt );
77 myDensity->setPen( QPen( Qt::red, 1 ) );
80 myDistr->setPen( QPen( dc, 1 ) );
81 myDistr->setSymbol( QwtSymbol( QwtSymbol::XCross, QBrush( dc ), QPen( dc ), QSize( 5, 5 ) ) );
83 QwtLegend* l = legend();
85 l = new QwtLegend( this );
86 l->setFrameStyle( QFrame::Box | QFrame::Sunken );
88 insertLegend( l, QwtPlot::BottomLegend );
90 myDensity->setTitle( tr( "SMESH_DENSITY_FUNC" ) );
91 myDistr->setTitle( tr( "SMESH_DISTR" ) );
93 QwtPlotGrid* aGrid = new QwtPlotGrid();
94 QPen aMajPen = aGrid->majPen();
95 aMajPen.setStyle( Qt::DashLine );
96 aGrid->setPen( aMajPen );
98 aGrid->enableX( true );
99 aGrid->enableY( true );
101 aGrid->attach( this );
104 StdMeshersGUI_DistrPreview::~StdMeshersGUI_DistrPreview()
108 bool StdMeshersGUI_DistrPreview::isTableFunc() const
113 void StdMeshersGUI_DistrPreview::tableFunc( SMESH::double_array& f ) const
118 QString StdMeshersGUI_DistrPreview::function() const
123 int StdMeshersGUI_DistrPreview::nbSeg() const
128 int StdMeshersGUI_DistrPreview::pointsCount() const
133 void StdMeshersGUI_DistrPreview::setConversion( Conversion conv, const bool upd )
140 bool StdMeshersGUI_DistrPreview::setParams( const QString& func, const int nbSeg, const int points, const bool upd )
143 myTableFunc = SMESH::double_array();
144 myFunction = func.isEmpty() ? "0" : func;
145 myPoints = points>0 ? points : 2;
146 myNbSeg = nbSeg>0 ? nbSeg : 1;
147 bool res = init( func );
153 bool StdMeshersGUI_DistrPreview::setParams( const SMESH::double_array& f, const int nbSeg, const bool upd )
157 if( myTableFunc.length()%2==1 )
158 myTableFunc.length( myTableFunc.length()-1 );
161 myPoints = myTableFunc.length()/2;
162 myNbSeg = nbSeg>0 ? nbSeg : 1;
167 return myTableFunc.length()>0;
170 bool StdMeshersGUI_DistrPreview::createTable( SMESH::double_array& func )
172 if( myExpr.IsNull() )
178 const double xmin = 0.0, xmax = 1.0;
180 double d = (xmax-xmin)/double(myPoints-1);
181 func.length( 2*myPoints );
183 for( int i=0, j=0; i<myPoints; j++ )
186 double t = xmin + d*j, f = funcValue( t, ok );
196 func.length( func.length()-2*err );
200 void StdMeshersGUI_DistrPreview::update()
202 SMESH::double_array graph, distr;
209 myIsDone = createTable( graph );
211 if( graph.length()>=2 )
213 StdMeshers::StdMeshers_NumberOfSegments_var h =
214 StdMeshers::StdMeshers_NumberOfSegments::_narrow( myHypo );
216 if( !CORBA::is_nil( h.in() ) )
218 SMESH::double_array* arr = 0;
220 arr = h->BuildDistributionTab( myTableFunc, myNbSeg, ( int )myConv );
222 arr = h->BuildDistributionExpr( myFunction.toLatin1().data(), myNbSeg, ( int )myConv );
231 bool correct = graph.length()>=2 && distr.length()>=2;
239 QwtText mt = myMsg->label();
240 mt.setText( QString() );
241 myMsg->setLabel( mt );
244 int size = graph.length()/2;
245 double* x = new double[size], *y = new double[size];
246 double min_x, max_x, min_y, max_y;
247 for( int i=0; i<size; i++ )
251 if( !convert( y[i] ) )
253 min_x = 0.0; max_x = 1.0; min_y = 0.0; max_y = 1.0;
254 delete[] x; delete[] y;
259 if( i==0 || y[i]<min_y )
261 if( i==0 || y[i]>max_y )
263 if( i==0 || x[i]<min_x )
265 if( i==0 || x[i]>max_x )
269 setAxisScale( myDensity->xAxis(), min_x, max_x );
270 setAxisScale( myDensity->yAxis(),
275 std::min( 0.0, min_y ),
276 std::max( 0.0, max_y )
279 myDensity->setData( x, y, size );
286 size = distr.length();
287 x = new double[size];
288 y = new double[size];
289 for( int i=0; i<size; i++ )
294 myDistr->setData( x, y, size );
304 } catch(Standard_Failure) {
305 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
309 void StdMeshersGUI_DistrPreview::showError()
311 setAxisScale( myDensity->xAxis(), 0.0, 1.0 );
312 setAxisScale( myDensity->yAxis(), 0.0, 1.0 );
313 myDensity->setData( 0, 0, 0 );
314 myDistr->setData( 0, 0, 0 );
315 QwtText mt = myMsg->label();
316 mt.setText( tr( "SMESH_INVALID_FUNCTION" ) );
317 myMsg->setLabel( mt );
321 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
323 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
325 return sub->GetName()=="t";
328 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
330 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
331 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
334 if( name->GetName()!="t" )
338 res = isCorrectArg( sub );
343 bool StdMeshersGUI_DistrPreview::init( const QString& str )
345 bool parsed_ok = true;
350 myExpr = ExprIntrp_GenExp::Create();
351 myExpr->Process( ( Standard_CString ) str.toLatin1().data() );
352 } catch(Standard_Failure) {
353 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
357 bool syntax = false, args = false;
358 if( parsed_ok && myExpr->IsDone() )
361 args = isCorrectArg( myExpr->Expression() );
364 bool res = parsed_ok && syntax && args;
370 double StdMeshersGUI_DistrPreview::funcValue( const double t, bool& ok )
372 if( myExpr.IsNull() )
375 myValues.ChangeValue( 1 ) = t;
378 double res = calc( ok );
383 double StdMeshersGUI_DistrPreview::calc( bool& ok )
392 res = myExpr->Expression()->Evaluate( myVars, myValues );
393 } catch(Standard_Failure) {
394 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
402 bool StdMeshersGUI_DistrPreview::isDone() const
407 bool StdMeshersGUI_DistrPreview::convert( double& v ) const
418 // in StdMeshers_NumberOfSegments.cc
419 // const double PRECISION = 1e-7;
423 } catch(Standard_Failure) {
424 Handle(Standard_Failure) aFail = Standard_Failure::Caught();