1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : StdMeshersGUI_DistrPreview.cxx
24 // Author : Open CASCADE S.A.S.
27 #include "StdMeshersGUI_DistrPreview.h"
30 #include <qwt_plot_curve.h>
31 #include <qwt_plot_marker.h>
32 #include <qwt_plot_grid.h>
33 #include <qwt_symbol.h>
34 #include <qwt_legend.h>
37 #include <Expr_NamedUnknown.hxx>
38 #include <Expr_GeneralExpression.hxx>
40 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
44 #include <Standard_Failure.hxx>
47 #include <Standard_ErrorHandler.hxx>
56 #include <Basics_Utils.hxx>
58 StdMeshersGUI_DistrPreview::StdMeshersGUI_DistrPreview( QWidget* p, StdMeshers::StdMeshers_NumberOfSegments_ptr h )
64 myConv( CUT_NEGATIVE ),
68 Kernel_Utils::Localizer loc;
69 myHypo = StdMeshers::StdMeshers_NumberOfSegments::_duplicate( h );
70 myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
71 myDensity = new QwtPlotCurve( QString() );
72 myDensity->attach( this );
73 myDistr = new QwtPlotCurve( QString() );
74 myDistr->attach( this );
75 myMsg = new QwtPlotMarker();
76 myMsg->attach( this );
77 myMsg->setValue( 0.5, 0.5 );
78 QwtText mt = myMsg->label();
79 mt.setBackgroundPen( QPen( Qt::red, 1 ) );
81 f.setPointSize( 14 ); f.setBold( true );
83 myMsg->setLabel( mt );
84 myDensity->setPen( QPen( Qt::red, 1 ) );
87 myDistr->setPen( QPen( dc, 1 ) );
88 myDistr->setSymbol( QwtSymbol( QwtSymbol::XCross, QBrush( dc ), QPen( dc ), QSize( 5, 5 ) ) );
90 QwtLegend* l = legend();
92 l = new QwtLegend( this );
93 l->setFrameStyle( QFrame::Box | QFrame::Sunken );
95 insertLegend( l, QwtPlot::BottomLegend );
97 myDensity->setTitle( tr( "SMESH_DENSITY_FUNC" ) );
98 myDistr->setTitle( tr( "SMESH_DISTR" ) );
100 QwtPlotGrid* aGrid = new QwtPlotGrid();
101 QPen aMajPen = aGrid->majPen();
102 aMajPen.setStyle( Qt::DashLine );
103 aGrid->setPen( aMajPen );
105 aGrid->enableX( true );
106 aGrid->enableY( true );
108 aGrid->attach( this );
111 StdMeshersGUI_DistrPreview::~StdMeshersGUI_DistrPreview()
115 bool StdMeshersGUI_DistrPreview::isTableFunc() const
120 void StdMeshersGUI_DistrPreview::tableFunc( SMESH::double_array& f ) const
125 QString StdMeshersGUI_DistrPreview::function() const
130 int StdMeshersGUI_DistrPreview::nbSeg() const
135 int StdMeshersGUI_DistrPreview::pointsCount() const
140 void StdMeshersGUI_DistrPreview::setConversion( Conversion conv, const bool upd )
147 bool StdMeshersGUI_DistrPreview::setParams( const QString& func, const int nbSeg, const int points, const bool upd )
150 myTableFunc = SMESH::double_array();
151 myFunction = func.isEmpty() ? "0" : func;
152 myPoints = points>0 ? points : 2;
153 myNbSeg = nbSeg>0 ? nbSeg : 1;
154 bool res = init( func );
160 bool StdMeshersGUI_DistrPreview::setParams( const SMESH::double_array& f, const int nbSeg, const bool upd )
164 if( myTableFunc.length()%2==1 )
165 myTableFunc.length( myTableFunc.length()-1 );
168 myPoints = myTableFunc.length()/2;
169 myNbSeg = nbSeg>0 ? nbSeg : 1;
174 return myTableFunc.length()>0;
177 bool StdMeshersGUI_DistrPreview::createTable( SMESH::double_array& func )
179 if( myExpr.IsNull() )
185 const double xmin = 0.0, xmax = 1.0;
187 double d = (xmax-xmin)/double(myPoints-1);
188 func.length( 2*myPoints );
190 for( int i=0, j=0; i<myPoints; j++ )
193 double t = xmin + d*j, f = funcValue( t, ok );
203 func.length( func.length()-2*err );
207 void StdMeshersGUI_DistrPreview::update()
209 Kernel_Utils::Localizer loc;
210 SMESH::double_array graph, distr;
217 myIsDone = createTable( graph );
219 if( graph.length()>=2 )
221 StdMeshers::StdMeshers_NumberOfSegments_var h =
222 StdMeshers::StdMeshers_NumberOfSegments::_narrow( myHypo );
224 if( !CORBA::is_nil( h.in() ) )
226 SMESH::double_array* arr = 0;
228 arr = h->BuildDistributionTab( myTableFunc, myNbSeg, ( int )myConv );
230 arr = h->BuildDistributionExpr( myFunction.toLatin1().data(), myNbSeg, ( int )myConv );
239 bool correct = graph.length()>=2 && distr.length()>=2;
247 QwtText mt = myMsg->label();
248 mt.setText( QString() );
249 myMsg->setLabel( mt );
252 int size = graph.length()/2;
253 double* x = new double[size], *y = new double[size];
254 double min_x, max_x, min_y, max_y;
255 for( int i=0; i<size; i++ )
259 if( !convert( y[i] ) )
261 min_x = 0.0; max_x = 1.0; min_y = 0.0; max_y = 1.0;
262 delete[] x; delete[] y;
268 if ( std::fabs(y[i]) >= HUGE_VAL)
269 y[i] = HUGE_VAL/100.;
272 y[i] = std::numeric_limits<double>::max()/100.;
276 if( i==0 || y[i]<min_y )
278 if( i==0 || y[i]>max_y )
280 if( i==0 || x[i]<min_x )
282 if( i==0 || x[i]>max_x )
286 setAxisScale( myDensity->xAxis(), min_x, max_x );
287 setAxisScale( myDensity->yAxis(),
292 std::min( 0.0, min_y ),
293 std::max( 0.0, max_y )
296 myDensity->setData( x, y, size );
303 size = distr.length();
304 x = new double[size];
305 y = new double[size];
306 for( int i=0; i<size; i++ )
311 myDistr->setData( x, y, size );
321 } catch(Standard_Failure) {
322 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
326 void StdMeshersGUI_DistrPreview::showError()
328 setAxisScale( myDensity->xAxis(), 0.0, 1.0 );
329 setAxisScale( myDensity->yAxis(), 0.0, 1.0 );
330 myDensity->setData( 0, 0, 0 );
331 myDistr->setData( 0, 0, 0 );
332 QwtText mt = myMsg->label();
333 mt.setText( tr( "SMESH_INVALID_FUNCTION" ) );
334 myMsg->setLabel( mt );
338 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
340 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
342 return sub->GetName()=="t";
345 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
347 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
348 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
351 if( name->GetName()!="t" )
355 res = isCorrectArg( sub );
360 bool StdMeshersGUI_DistrPreview::init( const QString& str )
362 Kernel_Utils::Localizer loc;
363 bool parsed_ok = true;
368 myExpr = ExprIntrp_GenExp::Create();
369 myExpr->Process( ( Standard_CString ) str.toLatin1().data() );
370 } catch(Standard_Failure) {
371 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
375 bool syntax = false, args = false;
376 if( parsed_ok && myExpr->IsDone() )
379 args = isCorrectArg( myExpr->Expression() );
382 bool res = parsed_ok && syntax && args;
388 double StdMeshersGUI_DistrPreview::funcValue( const double t, bool& ok )
390 if( myExpr.IsNull() )
393 myValues.ChangeValue( 1 ) = t;
396 double res = calc( ok );
401 double StdMeshersGUI_DistrPreview::calc( bool& ok )
410 res = myExpr->Expression()->Evaluate( myVars, myValues );
411 } catch(Standard_Failure) {
412 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
420 bool StdMeshersGUI_DistrPreview::isDone() const
425 bool StdMeshersGUI_DistrPreview::convert( double& v ) const
436 // in StdMeshers_NumberOfSegments.cc
437 // const double PRECISION = 1e-7;
441 } catch(Standard_Failure) {
442 Handle(Standard_Failure) aFail = Standard_Failure::Caught();