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_symbol.h>
31 #include <qwt_legend.h>
34 #include <Expr_NamedUnknown.hxx>
35 #include <Expr_GeneralExpression.hxx>
37 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
41 #include <Standard_Failure.hxx>
44 #include <Standard_ErrorHandler.hxx>
47 StdMeshersGUI_DistrPreview::StdMeshersGUI_DistrPreview( QWidget* p, StdMeshers::StdMeshers_NumberOfSegments_ptr h )
53 myConv( CUT_NEGATIVE ),
57 myHypo = StdMeshers::StdMeshers_NumberOfSegments::_duplicate( h );
58 myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
59 myDensity = new QwtPlotCurve( QString() );
60 myDensity->attach( this );
61 myDistr = new QwtPlotCurve( QString() );
62 myDistr->attach( this );
63 myMsg = new QwtPlotMarker();
64 myMsg->attach( this );
65 myMsg->setValue( 0.5, 0.5 );
66 QwtText mt = myMsg->label();
67 mt.setBackgroundPen( QPen( Qt::red, 1 ) );
69 f.setPointSize( 14 ); f.setBold( true );
71 myMsg->setLabel( mt );
72 myDensity->setPen( QPen( Qt::red, 1 ) );
75 myDistr->setPen( QPen( dc, 1 ) );
76 myDistr->setSymbol( QwtSymbol( QwtSymbol::XCross, QBrush( dc ), QPen( dc ), QSize( 5, 5 ) ) );
78 QwtLegend* l = legend();
80 l = new QwtLegend( this );
81 l->setFrameStyle( QFrame::Box | QFrame::Sunken );
83 insertLegend( l, QwtPlot::BottomLegend );
85 myDensity->setTitle( tr( "SMESH_DENSITY_FUNC" ) );
86 myDistr->setTitle( tr( "SMESH_DISTR" ) );
89 StdMeshersGUI_DistrPreview::~StdMeshersGUI_DistrPreview()
93 bool StdMeshersGUI_DistrPreview::isTableFunc() const
98 void StdMeshersGUI_DistrPreview::tableFunc( SMESH::double_array& f ) const
103 QString StdMeshersGUI_DistrPreview::function() const
108 int StdMeshersGUI_DistrPreview::nbSeg() const
113 int StdMeshersGUI_DistrPreview::pointsCount() const
118 void StdMeshersGUI_DistrPreview::setConversion( Conversion conv, const bool upd )
125 bool StdMeshersGUI_DistrPreview::setParams( const QString& func, const int nbSeg, const int points, const bool upd )
128 myTableFunc = SMESH::double_array();
129 myFunction = func.isEmpty() ? "0" : func;
130 myPoints = points>0 ? points : 2;
131 myNbSeg = nbSeg>0 ? nbSeg : 1;
132 bool res = init( func );
138 bool StdMeshersGUI_DistrPreview::setParams( const SMESH::double_array& f, const int nbSeg, const bool upd )
142 if( myTableFunc.length()%2==1 )
143 myTableFunc.length( myTableFunc.length()-1 );
146 myPoints = myTableFunc.length()/2;
147 myNbSeg = nbSeg>0 ? nbSeg : 1;
152 return myTableFunc.length()>0;
155 bool StdMeshersGUI_DistrPreview::createTable( SMESH::double_array& func )
157 if( myExpr.IsNull() )
163 const double xmin = 0.0, xmax = 1.0;
165 double d = (xmax-xmin)/double(myPoints-1);
166 func.length( 2*myPoints );
168 for( int i=0, j=0; i<myPoints; j++ )
171 double t = xmin + d*j, f = funcValue( t, ok );
181 func.length( func.length()-2*err );
185 void StdMeshersGUI_DistrPreview::update()
187 SMESH::double_array graph, distr;
194 myIsDone = createTable( graph );
196 if( graph.length()>=2 )
198 StdMeshers::StdMeshers_NumberOfSegments_var h =
199 StdMeshers::StdMeshers_NumberOfSegments::_narrow( myHypo );
201 if( !CORBA::is_nil( h.in() ) )
203 SMESH::double_array* arr = 0;
205 arr = h->BuildDistributionTab( myTableFunc, myNbSeg, ( int )myConv );
207 arr = h->BuildDistributionExpr( myFunction.toLatin1().data(), myNbSeg, ( int )myConv );
216 bool correct = graph.length()>=2 && distr.length()>=2;
224 QwtText mt = myMsg->label();
225 mt.setText( QString() );
226 myMsg->setLabel( mt );
229 int size = graph.length()/2;
230 double* x = new double[size], *y = new double[size];
231 double min_x, max_x, min_y, max_y;
232 for( int i=0; i<size; i++ )
236 if( !convert( y[i] ) )
238 min_x = 0.0; max_x = 1.0; min_y = 0.0; max_y = 1.0;
239 delete[] x; delete[] y;
244 if( i==0 || y[i]<min_y )
246 if( i==0 || y[i]>max_y )
248 if( i==0 || x[i]<min_x )
250 if( i==0 || x[i]>max_x )
254 setAxisScale( myDensity->xAxis(), min_x, max_x );
255 setAxisScale( myDensity->yAxis(), std::min( 0.0, min_y ), std::max( 0.0, max_y ) );
256 myDensity->setData( x, y, size );
263 size = distr.length();
264 x = new double[size];
265 y = new double[size];
266 for( int i=0; i<size; i++ )
271 myDistr->setData( x, y, size );
281 } catch(Standard_Failure) {
282 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
286 void StdMeshersGUI_DistrPreview::showError()
288 setAxisScale( myDensity->xAxis(), 0.0, 1.0 );
289 setAxisScale( myDensity->yAxis(), 0.0, 1.0 );
290 myDensity->setData( 0, 0, 0 );
291 myDistr->setData( 0, 0, 0 );
292 QwtText mt = myMsg->label();
293 mt.setText( tr( "SMESH_INVALID_FUNCTION" ) );
294 myMsg->setLabel( mt );
298 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
300 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
302 return sub->GetName()=="t";
305 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
307 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
308 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
311 if( name->GetName()!="t" )
315 res = isCorrectArg( sub );
320 bool StdMeshersGUI_DistrPreview::init( const QString& str )
322 bool parsed_ok = true;
327 myExpr = ExprIntrp_GenExp::Create();
328 myExpr->Process( ( Standard_CString ) str.toLatin1().data() );
329 } catch(Standard_Failure) {
330 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
334 bool syntax = false, args = false;
335 if( parsed_ok && myExpr->IsDone() )
338 args = isCorrectArg( myExpr->Expression() );
341 bool res = parsed_ok && syntax && args;
347 double StdMeshersGUI_DistrPreview::funcValue( const double t, bool& ok )
349 if( myExpr.IsNull() )
352 myValues.ChangeValue( 1 ) = t;
355 double res = calc( ok );
360 double StdMeshersGUI_DistrPreview::calc( bool& ok )
369 res = myExpr->Expression()->Evaluate( myVars, myValues );
370 } catch(Standard_Failure) {
371 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
379 bool StdMeshersGUI_DistrPreview::isDone() const
384 bool StdMeshersGUI_DistrPreview::convert( double& v ) const
395 // in StdMeshers_NumberOfSegments.cc
396 // const double PRECISION = 1e-7;
400 } catch(Standard_Failure) {
401 Handle(Standard_Failure) aFail = Standard_Failure::Caught();