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
23 #include "StdMeshersGUI_DistrPreview.h"
25 #include <Expr_NamedUnknown.hxx>
26 #include <Expr_GeneralExpression.hxx>
28 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
32 #include <Standard_Failure.hxx>
35 #include <Standard_ErrorHandler.hxx>
38 StdMeshersGUI_DistrPreview::StdMeshersGUI_DistrPreview( QWidget* p, StdMeshers::StdMeshers_NumberOfSegments_ptr h )
44 myConv( CUT_NEGATIVE ),
48 myHypo = StdMeshers::StdMeshers_NumberOfSegments::_duplicate( h );
49 myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
50 myDensity = insertCurve( QString() );
51 myDistr = insertCurve( QString() );
52 myMsg = insertMarker( new QwtPlotMarker( this ) );
53 setMarkerPos( myMsg, 0.5, 0.5 );
54 setMarkerLabelPen( myMsg, QPen( Qt::red, 1 ) );
55 QFont f = markerFont( myMsg );
58 setMarkerFont( myMsg, f );
59 setCurvePen( myDensity, QPen( Qt::red, 1 ) );
62 setCurvePen( myDistr, QPen( dc, 1 ) );
63 setCurveSymbol( myDistr, QwtSymbol( QwtSymbol::XCross, QBrush( dc ), QPen( dc ), QSize( 5, 5 ) ) );
64 setAutoLegend( true );
66 setLegendPos( Qwt::Bottom );
67 setCurveTitle( myDensity, tr( "SMESH_DENSITY_FUNC" ) );
68 setCurveTitle( myDistr, tr( "SMESH_DISTR" ) );
71 StdMeshersGUI_DistrPreview::~StdMeshersGUI_DistrPreview()
75 bool StdMeshersGUI_DistrPreview::isTableFunc() const
80 void StdMeshersGUI_DistrPreview::tableFunc( SMESH::double_array& f ) const
85 QString StdMeshersGUI_DistrPreview::function() const
90 int StdMeshersGUI_DistrPreview::nbSeg() const
95 int StdMeshersGUI_DistrPreview::pointsCount() const
100 void StdMeshersGUI_DistrPreview::setConversion( Conversion conv, const bool upd )
107 bool StdMeshersGUI_DistrPreview::setParams( const QString& func, const int nbSeg, const int points, const bool upd )
110 myTableFunc = SMESH::double_array();
111 myFunction = func.isEmpty() ? "0" : func;
112 myPoints = points>0 ? points : 2;
113 myNbSeg = nbSeg>0 ? nbSeg : 1;
114 bool res = init( func );
120 bool StdMeshersGUI_DistrPreview::setParams( const SMESH::double_array& f, const int nbSeg, const bool upd )
124 if( myTableFunc.length()%2==1 )
125 myTableFunc.length( myTableFunc.length()-1 );
128 myPoints = myTableFunc.length()/2;
129 myNbSeg = nbSeg>0 ? nbSeg : 1;
134 return myTableFunc.length()>0;
137 bool StdMeshersGUI_DistrPreview::createTable( SMESH::double_array& func )
139 if( myExpr.IsNull() )
145 const double xmin = 0.0, xmax = 1.0;
147 double d = (xmax-xmin)/double(myPoints-1);
148 func.length( 2*myPoints );
150 for( int i=0, j=0; i<myPoints; j++ )
153 double t = xmin + d*j, f = funcValue( t, ok );
163 func.length( func.length()-2*err );
167 void StdMeshersGUI_DistrPreview::update()
169 SMESH::double_array graph, distr;
176 myIsDone = createTable( graph );
178 if( graph.length()>=2 )
180 StdMeshers::StdMeshers_NumberOfSegments_var h =
181 StdMeshers::StdMeshers_NumberOfSegments::_narrow( myHypo );
183 if( !CORBA::is_nil( h.in() ) )
185 SMESH::double_array* arr = 0;
187 arr = h->BuildDistributionTab( myTableFunc, myNbSeg, ( int )myConv );
189 arr = h->BuildDistributionExpr( myFunction.latin1(), myNbSeg, ( int )myConv );
198 bool correct = graph.length()>=2 && distr.length()>=2;
205 setMarkerLabel( myMsg, QString() );
207 int size = graph.length()/2;
208 double* x = new double[size], *y = new double[size];
209 double min_x, max_x, min_y, max_y;
210 for( int i=0; i<size; i++ )
214 if( !convert( y[i] ) )
216 min_x = 0.0; max_x = 1.0; min_y = 0.0; max_y = 1.0;
217 delete[] x; delete[] y;
222 if( i==0 || y[i]<min_y )
224 if( i==0 || y[i]>max_y )
226 if( i==0 || x[i]<min_x )
228 if( i==0 || x[i]>max_x )
232 setAxisScale( curveXAxis( myDensity ), min_x, max_x );
233 setAxisScale( curveYAxis( myDensity ), min( 0.0, min_y ), max( 0.0, max_y ) );
234 setCurveData( myDensity, x, y, size );
241 size = distr.length();
242 x = new double[size];
243 y = new double[size];
244 for( int i=0; i<size; i++ )
249 setCurveData( myDistr, x, y, size );
259 } catch(Standard_Failure) {
260 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
264 void StdMeshersGUI_DistrPreview::showError()
266 setAxisScale( curveXAxis( myDensity ), 0.0, 1.0 );
267 setAxisScale( curveYAxis( myDensity ), 0.0, 1.0 );
268 setCurveData( myDensity, 0, 0, 0 );
269 setCurveData( myDistr, 0, 0, 0 );
270 setMarkerLabel( myMsg, tr( "SMESH_INVALID_FUNCTION" ) );
274 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
276 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
278 return sub->GetName()=="t";
281 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
283 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
284 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
287 if( name->GetName()!="t" )
291 res = isCorrectArg( sub );
296 bool StdMeshersGUI_DistrPreview::init( const QString& str )
298 bool parsed_ok = true;
303 myExpr = ExprIntrp_GenExp::Create();
304 myExpr->Process( ( Standard_CString ) str.latin1() );
305 } catch(Standard_Failure) {
306 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
310 bool syntax = false, args = false;
311 if( parsed_ok && myExpr->IsDone() )
314 args = isCorrectArg( myExpr->Expression() );
317 bool res = parsed_ok && syntax && args;
323 double StdMeshersGUI_DistrPreview::funcValue( const double t, bool& ok )
325 if( myExpr.IsNull() )
328 myValues.ChangeValue( 1 ) = t;
331 double res = calc( ok );
336 double StdMeshersGUI_DistrPreview::calc( bool& ok )
345 res = myExpr->Expression()->Evaluate( myVars, myValues );
346 } catch(Standard_Failure) {
347 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
355 bool StdMeshersGUI_DistrPreview::isDone() const
360 bool StdMeshersGUI_DistrPreview::convert( double& v ) const
371 // in StdMeshers_NumberOfSegments.cc
372 // const double PRECISION = 1e-7;
376 } catch(Standard_Failure) {
377 Handle(Standard_Failure) aFail = Standard_Failure::Caught();