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
21 #include "StdMeshersGUI_DistrPreview.h"
23 #include <Expr_NamedUnknown.hxx>
24 #include <Expr_GeneralExpression.hxx>
26 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
30 #include <Standard_Failure.hxx>
33 #include <Standard_ErrorHandler.hxx>
42 StdMeshersGUI_DistrPreview::StdMeshersGUI_DistrPreview( QWidget* p, StdMeshers::StdMeshers_NumberOfSegments_ptr h )
48 myConv( CUT_NEGATIVE ),
52 myHypo = StdMeshers::StdMeshers_NumberOfSegments::_duplicate( h );
53 myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
54 myDensity = insertCurve( QString() );
55 myDistr = insertCurve( QString() );
56 myMsg = insertMarker( new QwtPlotMarker( this ) );
57 setMarkerPos( myMsg, 0.5, 0.5 );
58 setMarkerLabelPen( myMsg, QPen( Qt::red, 1 ) );
59 QFont f = markerFont( myMsg );
62 setMarkerFont( myMsg, f );
63 setCurvePen( myDensity, QPen( Qt::red, 1 ) );
66 setCurvePen( myDistr, QPen( dc, 1 ) );
67 setCurveSymbol( myDistr, QwtSymbol( QwtSymbol::XCross, QBrush( dc ), QPen( dc ), QSize( 5, 5 ) ) );
68 setAutoLegend( true );
70 setLegendPos( Qwt::Bottom );
71 setCurveTitle( myDensity, tr( "SMESH_DENSITY_FUNC" ) );
72 setCurveTitle( myDistr, tr( "SMESH_DISTR" ) );
75 StdMeshersGUI_DistrPreview::~StdMeshersGUI_DistrPreview()
79 bool StdMeshersGUI_DistrPreview::isTableFunc() const
84 void StdMeshersGUI_DistrPreview::tableFunc( SMESH::double_array& f ) const
89 QString StdMeshersGUI_DistrPreview::function() const
94 int StdMeshersGUI_DistrPreview::nbSeg() const
99 int StdMeshersGUI_DistrPreview::pointsCount() const
104 void StdMeshersGUI_DistrPreview::setConversion( Conversion conv, const bool upd )
111 bool StdMeshersGUI_DistrPreview::setParams( const QString& func, const int nbSeg, const int points, const bool upd )
114 myTableFunc = SMESH::double_array();
115 myFunction = func.isEmpty() ? "0" : func;
116 myPoints = points>0 ? points : 2;
117 myNbSeg = nbSeg>0 ? nbSeg : 1;
118 bool res = init( func );
124 bool StdMeshersGUI_DistrPreview::setParams( const SMESH::double_array& f, const int nbSeg, const bool upd )
128 if( myTableFunc.length()%2==1 )
129 myTableFunc.length( myTableFunc.length()-1 );
132 myPoints = myTableFunc.length()/2;
133 myNbSeg = nbSeg>0 ? nbSeg : 1;
138 return myTableFunc.length()>0;
141 bool StdMeshersGUI_DistrPreview::createTable( SMESH::double_array& func )
143 if( myExpr.IsNull() )
149 const double xmin = 0.0, xmax = 1.0;
151 double d = (xmax-xmin)/double(myPoints-1);
152 func.length( 2*myPoints );
154 for( int i=0, j=0; i<myPoints; j++ )
157 double t = xmin + d*j, f = funcValue( t, ok );
167 func.length( func.length()-2*err );
171 void StdMeshersGUI_DistrPreview::update()
173 SMESH::double_array graph, distr;
180 myIsDone = createTable( graph );
182 if( graph.length()>=2 )
184 StdMeshers::StdMeshers_NumberOfSegments_var h =
185 StdMeshers::StdMeshers_NumberOfSegments::_narrow( myHypo );
187 if( !CORBA::is_nil( h.in() ) )
189 SMESH::double_array* arr = 0;
191 arr = h->BuildDistributionTab( myTableFunc, myNbSeg, ( int )myConv );
193 arr = h->BuildDistributionExpr( myFunction.latin1(), myNbSeg, ( int )myConv );
202 bool correct = graph.length()>=2 && distr.length()>=2;
209 setMarkerLabel( myMsg, QString() );
211 int size = graph.length()/2;
212 double* x = new double[size], *y = new double[size];
213 double min_x, max_x, min_y, max_y;
214 for( int i=0; i<size; i++ )
218 if( !convert( y[i] ) )
220 min_x = 0.0; max_x = 1.0; min_y = 0.0; max_y = 1.0;
221 delete[] x; delete[] y;
226 if( i==0 || y[i]<min_y )
228 if( i==0 || y[i]>max_y )
230 if( i==0 || x[i]<min_x )
232 if( i==0 || x[i]>max_x )
236 setAxisScale( curveXAxis( myDensity ), min_x, max_x );
237 setAxisScale( curveYAxis( myDensity ), min( 0.0, min_y ), max( 0.0, max_y ) );
238 setCurveData( myDensity, x, y, size );
245 size = distr.length();
246 x = new double[size];
247 y = new double[size];
248 for( int i=0; i<size; i++ )
253 setCurveData( myDistr, x, y, size );
263 } catch(Standard_Failure) {
264 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
268 void StdMeshersGUI_DistrPreview::showError()
270 setAxisScale( curveXAxis( myDensity ), 0.0, 1.0 );
271 setAxisScale( curveYAxis( myDensity ), 0.0, 1.0 );
272 setCurveData( myDensity, 0, 0, 0 );
273 setCurveData( myDistr, 0, 0, 0 );
274 setMarkerLabel( myMsg, tr( "SMESH_INVALID_FUNCTION" ) );
278 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
280 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
282 return sub->GetName()=="t";
285 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
287 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
288 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
291 if( name->GetName()!="t" )
295 res = isCorrectArg( sub );
300 bool StdMeshersGUI_DistrPreview::init( const QString& str )
302 bool parsed_ok = true;
307 myExpr = ExprIntrp_GenExp::Create();
308 myExpr->Process( ( Standard_CString ) str.latin1() );
309 } catch(Standard_Failure) {
310 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
314 bool syntax = false, args = false;
315 if( parsed_ok && myExpr->IsDone() )
318 args = isCorrectArg( myExpr->Expression() );
321 bool res = parsed_ok && syntax && args;
327 double StdMeshersGUI_DistrPreview::funcValue( const double t, bool& ok )
329 if( myExpr.IsNull() )
332 myValues.ChangeValue( 1 ) = t;
335 double res = calc( ok );
340 double StdMeshersGUI_DistrPreview::calc( bool& ok )
349 res = myExpr->Expression()->Evaluate( myVars, myValues );
350 } catch(Standard_Failure) {
351 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
359 bool StdMeshersGUI_DistrPreview::isDone() const
364 bool StdMeshersGUI_DistrPreview::convert( double& v ) const
375 // in StdMeshers_NumberOfSegments.cc
376 // const double PRECISION = 1e-7;
380 } catch(Standard_Failure) {
381 Handle(Standard_Failure) aFail = Standard_Failure::Caught();