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>
36 StdMeshersGUI_DistrPreview::StdMeshersGUI_DistrPreview( QWidget* p, StdMeshers::StdMeshers_NumberOfSegments_ptr h )
42 myConv( CUT_NEGATIVE ),
46 myHypo = StdMeshers::StdMeshers_NumberOfSegments::_duplicate( h );
47 myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
48 myDensity = insertCurve( QString() );
49 myDistr = insertCurve( QString() );
50 myMsg = insertMarker( new QwtPlotMarker( this ) );
51 setMarkerPos( myMsg, 0.5, 0.5 );
52 setMarkerLabelPen( myMsg, QPen( Qt::red, 1 ) );
53 QFont f = markerFont( myMsg );
56 setMarkerFont( myMsg, f );
57 setCurvePen( myDensity, QPen( Qt::red, 1 ) );
60 setCurvePen( myDistr, QPen( dc, 1 ) );
61 setCurveSymbol( myDistr, QwtSymbol( QwtSymbol::XCross, QBrush( dc ), QPen( dc ), QSize( 5, 5 ) ) );
62 setAutoLegend( true );
64 setLegendPos( Qwt::Bottom );
65 setCurveTitle( myDensity, tr( "SMESH_DENSITY_FUNC" ) );
66 setCurveTitle( myDistr, tr( "SMESH_DISTR" ) );
69 StdMeshersGUI_DistrPreview::~StdMeshersGUI_DistrPreview()
73 bool StdMeshersGUI_DistrPreview::isTableFunc() const
78 void StdMeshersGUI_DistrPreview::tableFunc( SMESH::double_array& f ) const
83 QString StdMeshersGUI_DistrPreview::function() const
88 int StdMeshersGUI_DistrPreview::nbSeg() const
93 int StdMeshersGUI_DistrPreview::pointsCount() const
98 void StdMeshersGUI_DistrPreview::setConversion( Conversion conv, const bool upd )
105 bool StdMeshersGUI_DistrPreview::setParams( const QString& func, const int nbSeg, const int points, const bool upd )
108 myTableFunc = SMESH::double_array();
109 myFunction = func.isEmpty() ? "0" : func;
110 myPoints = points>0 ? points : 2;
111 myNbSeg = nbSeg>0 ? nbSeg : 1;
112 bool res = init( func );
118 bool StdMeshersGUI_DistrPreview::setParams( const SMESH::double_array& f, const int nbSeg, const bool upd )
122 if( myTableFunc.length()%2==1 )
123 myTableFunc.length( myTableFunc.length()-1 );
126 myPoints = myTableFunc.length()/2;
127 myNbSeg = nbSeg>0 ? nbSeg : 1;
132 return myTableFunc.length()>0;
135 bool StdMeshersGUI_DistrPreview::createTable( SMESH::double_array& func )
137 if( myExpr.IsNull() )
143 const double xmin = 0.0, xmax = 1.0;
145 double d = (xmax-xmin)/double(myPoints-1);
146 func.length( 2*myPoints );
148 for( int i=0, j=0; i<myPoints; j++ )
151 double t = xmin + d*j, f = funcValue( t, ok );
161 func.length( func.length()-2*err );
165 void StdMeshersGUI_DistrPreview::update()
167 SMESH::double_array graph, distr;
174 myIsDone = createTable( graph );
176 if( graph.length()>=2 )
178 StdMeshers::StdMeshers_NumberOfSegments_var h =
179 StdMeshers::StdMeshers_NumberOfSegments::_narrow( myHypo );
181 if( !CORBA::is_nil( h.in() ) )
183 SMESH::double_array* arr = 0;
185 arr = h->BuildDistributionTab( myTableFunc, myNbSeg, ( int )myConv );
187 arr = h->BuildDistributionExpr( myFunction.latin1(), myNbSeg, ( int )myConv );
196 bool correct = graph.length()>=2 && distr.length()>=2;
203 setMarkerLabel( myMsg, QString() );
205 int size = graph.length()/2;
206 double* x = new double[size], *y = new double[size];
207 double min_x, max_x, min_y, max_y;
208 for( int i=0; i<size; i++ )
212 if( !convert( y[i] ) )
214 min_x = 0.0; max_x = 1.0; min_y = 0.0; max_y = 1.0;
215 delete[] x; delete[] y;
220 if( i==0 || y[i]<min_y )
222 if( i==0 || y[i]>max_y )
224 if( i==0 || x[i]<min_x )
226 if( i==0 || x[i]>max_x )
230 setAxisScale( curveXAxis( myDensity ), min_x, max_x );
231 setAxisScale( curveYAxis( myDensity ), min( 0.0, min_y ), max( 0.0, max_y ) );
232 setCurveData( myDensity, x, y, size );
239 size = distr.length();
240 x = new double[size];
241 y = new double[size];
242 for( int i=0; i<size; i++ )
247 setCurveData( myDistr, x, y, size );
257 } catch(Standard_Failure) {
258 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
262 void StdMeshersGUI_DistrPreview::showError()
264 setAxisScale( curveXAxis( myDensity ), 0.0, 1.0 );
265 setAxisScale( curveYAxis( myDensity ), 0.0, 1.0 );
266 setCurveData( myDensity, 0, 0, 0 );
267 setCurveData( myDistr, 0, 0, 0 );
268 setMarkerLabel( myMsg, tr( "SMESH_INVALID_FUNCTION" ) );
272 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
274 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
276 return sub->GetName()=="t";
279 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
281 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
282 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
285 if( name->GetName()!="t" )
289 res = isCorrectArg( sub );
294 bool StdMeshersGUI_DistrPreview::init( const QString& str )
296 bool parsed_ok = true;
301 myExpr = ExprIntrp_GenExp::Create();
302 myExpr->Process( ( Standard_CString ) str.latin1() );
303 } catch(Standard_Failure) {
304 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
308 bool syntax = false, args = false;
309 if( parsed_ok && myExpr->IsDone() )
312 args = isCorrectArg( myExpr->Expression() );
315 bool res = parsed_ok && syntax && args;
321 double StdMeshersGUI_DistrPreview::funcValue( const double t, bool& ok )
323 if( myExpr.IsNull() )
326 myValues.ChangeValue( 1 ) = t;
329 double res = calc( ok );
334 double StdMeshersGUI_DistrPreview::calc( bool& ok )
343 res = myExpr->Expression()->Evaluate( myVars, myValues );
344 } catch(Standard_Failure) {
345 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
353 bool StdMeshersGUI_DistrPreview::isDone() const
358 bool StdMeshersGUI_DistrPreview::convert( double& v ) const
369 // in StdMeshers_NumberOfSegments.cc
370 // const double PRECISION = 1e-7;
374 } catch(Standard_Failure) {
375 Handle(Standard_Failure) aFail = Standard_Failure::Caught();