2 #include "StdMeshersGUI_DistrPreview.h"
3 #include "CASCatch.hxx"
5 #include <Expr_NamedUnknown.hxx>
6 #include <Expr_GeneralExpression.hxx>
8 StdMeshersGUI_DistrPreview::StdMeshersGUI_DistrPreview( QWidget* p, StdMeshers::StdMeshers_NumberOfSegments_ptr h )
14 myConv( CUT_NEGATIVE ),
18 myHypo = StdMeshers::StdMeshers_NumberOfSegments::_duplicate( h );
19 myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
20 myDensity = insertCurve( QString() );
21 myDistr = insertCurve( QString() );
22 myMsg = insertMarker( new QwtPlotMarker( this ) );
23 setMarkerPos( myMsg, 0.5, 0.5 );
24 setMarkerLabelPen( myMsg, QPen( Qt::red, 1 ) );
25 QFont f = markerFont( myMsg );
28 setMarkerFont( myMsg, f );
29 setCurvePen( myDensity, QPen( Qt::red, 1 ) );
32 setCurvePen( myDistr, QPen( dc, 1 ) );
33 setCurveSymbol( myDistr, QwtSymbol( QwtSymbol::XCross, QBrush( dc ), QPen( dc ), QSize( 5, 5 ) ) );
34 setAutoLegend( true );
36 setLegendPos( Qwt::Bottom );
37 setCurveTitle( myDensity, tr( "SMESH_DENSITY_FUNC" ) );
38 setCurveTitle( myDistr, tr( "SMESH_DISTR" ) );
41 StdMeshersGUI_DistrPreview::~StdMeshersGUI_DistrPreview()
45 bool StdMeshersGUI_DistrPreview::isTableFunc() const
50 void StdMeshersGUI_DistrPreview::tableFunc( SMESH::double_array& f ) const
55 QString StdMeshersGUI_DistrPreview::function() const
60 int StdMeshersGUI_DistrPreview::nbSeg() const
65 int StdMeshersGUI_DistrPreview::pointsCount() const
70 void StdMeshersGUI_DistrPreview::setConversion( Conversion conv, const bool upd )
77 bool StdMeshersGUI_DistrPreview::setParams( const QString& func, const int nbSeg, const int points, const bool upd )
80 myTableFunc = SMESH::double_array();
81 myFunction = func.isEmpty() ? "0" : func;
82 myPoints = points>0 ? points : 2;
83 myNbSeg = nbSeg>0 ? nbSeg : 1;
84 bool res = init( func );
90 bool StdMeshersGUI_DistrPreview::setParams( const SMESH::double_array& f, const int nbSeg, const bool upd )
94 if( myTableFunc.length()%2==1 )
95 myTableFunc.length( myTableFunc.length()-1 );
98 myPoints = myTableFunc.length()/2;
99 myNbSeg = nbSeg>0 ? nbSeg : 1;
104 return myTableFunc.length()>0;
107 bool StdMeshersGUI_DistrPreview::createTable( SMESH::double_array& func )
109 if( myExpr.IsNull() )
115 const double xmin = 0.0, xmax = 1.0;
117 double d = (xmax-xmin)/double(myPoints-1);
118 func.length( 2*myPoints );
120 for( int i=0, j=0; i<myPoints; j++ )
123 double t = xmin + d*j, f = funcValue( t, ok );
133 func.length( func.length()-2*err );
137 void StdMeshersGUI_DistrPreview::update()
139 SMESH::double_array graph, distr;
146 myIsDone = createTable( graph );
148 if( graph.length()>=2 )
150 StdMeshers::StdMeshers_NumberOfSegments_var h =
151 StdMeshers::StdMeshers_NumberOfSegments::_narrow( myHypo );
153 if( !CORBA::is_nil( h.in() ) )
155 SMESH::double_array* arr = 0;
157 arr = h->BuildDistributionTab( myTableFunc, myNbSeg, ( int )myConv );
159 arr = h->BuildDistributionExpr( myFunction.latin1(), myNbSeg, ( int )myConv );
168 bool correct = graph.length()>=2 && distr.length()>=2;
175 setMarkerLabel( myMsg, QString() );
177 int size = graph.length()/2;
178 double* x = new double[size], *y = new double[size];
179 double min_x, max_x, min_y, max_y;
180 for( int i=0; i<size; i++ )
184 if( !convert( y[i] ) )
186 min_x = 0.0; max_x = 1.0; min_y = 0.0; max_y = 1.0;
187 delete[] x; delete[] y;
192 if( i==0 || y[i]<min_y )
194 if( i==0 || y[i]>max_y )
196 if( i==0 || x[i]<min_x )
198 if( i==0 || x[i]>max_x )
202 setAxisScale( curveXAxis( myDensity ), min_x, max_x );
203 setAxisScale( curveYAxis( myDensity ), min( 0.0, min_y ), max( 0.0, max_y ) );
204 setCurveData( myDensity, x, y, size );
211 size = distr.length();
212 x = new double[size];
213 y = new double[size];
214 for( int i=0; i<size; i++ )
219 setCurveData( myDistr, x, y, size );
228 CASCatch_CATCH(Standard_Failure)
230 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
234 void StdMeshersGUI_DistrPreview::showError()
236 setAxisScale( curveXAxis( myDensity ), 0.0, 1.0 );
237 setAxisScale( curveYAxis( myDensity ), 0.0, 1.0 );
238 setCurveData( myDensity, 0, 0, 0 );
239 setCurveData( myDistr, 0, 0, 0 );
240 setMarkerLabel( myMsg, tr( "SMESH_INVALID_FUNCTION" ) );
244 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
246 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
248 return sub->GetName()=="t";
251 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
253 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
254 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
257 if( name->GetName()!="t" )
261 res = isCorrectArg( sub );
266 bool StdMeshersGUI_DistrPreview::init( const QString& str )
268 bool parsed_ok = true;
271 myExpr = ExprIntrp_GenExp::Create();
272 myExpr->Process( ( Standard_CString ) str.latin1() );
274 CASCatch_CATCH(Standard_Failure)
276 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
280 bool syntax = false, args = false;
281 if( parsed_ok && myExpr->IsDone() )
284 args = isCorrectArg( myExpr->Expression() );
287 bool res = parsed_ok && syntax && args;
293 double StdMeshersGUI_DistrPreview::funcValue( const double t, bool& ok )
295 if( myExpr.IsNull() )
298 myValues.ChangeValue( 1 ) = t;
301 double res = calc( ok );
306 double StdMeshersGUI_DistrPreview::calc( bool& ok )
312 res = myExpr->Expression()->Evaluate( myVars, myValues );
314 CASCatch_CATCH(Standard_Failure) {
315 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
323 bool StdMeshersGUI_DistrPreview::isDone() const
328 bool StdMeshersGUI_DistrPreview::convert( double& v ) const
337 // in StdMeshers_NumberOfSegments.cc
338 // const double PRECISION = 1e-7;
343 CASCatch_CATCH(Standard_Failure)
345 Handle(Standard_Failure) aFail = Standard_Failure::Caught();