1 // Copyright (C) 2007-2008 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
22 #include "StdMeshersGUI_DistrPreview.h"
24 #include <Expr_NamedUnknown.hxx>
25 #include <Expr_GeneralExpression.hxx>
27 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
31 #include <Standard_Failure.hxx>
34 #include <Standard_ErrorHandler.hxx>
43 StdMeshersGUI_DistrPreview::StdMeshersGUI_DistrPreview( QWidget* p, StdMeshers::StdMeshers_NumberOfSegments_ptr h )
49 myConv( CUT_NEGATIVE ),
53 myHypo = StdMeshers::StdMeshers_NumberOfSegments::_duplicate( h );
54 myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
55 myDensity = insertCurve( QString() );
56 myDistr = insertCurve( QString() );
57 myMsg = insertMarker( new QwtPlotMarker( this ) );
58 setMarkerPos( myMsg, 0.5, 0.5 );
59 setMarkerLabelPen( myMsg, QPen( Qt::red, 1 ) );
60 QFont f = markerFont( myMsg );
63 setMarkerFont( myMsg, f );
64 setCurvePen( myDensity, QPen( Qt::red, 1 ) );
67 setCurvePen( myDistr, QPen( dc, 1 ) );
68 setCurveSymbol( myDistr, QwtSymbol( QwtSymbol::XCross, QBrush( dc ), QPen( dc ), QSize( 5, 5 ) ) );
69 setAutoLegend( true );
71 setLegendPos( Qwt::Bottom );
72 setCurveTitle( myDensity, tr( "SMESH_DENSITY_FUNC" ) );
73 setCurveTitle( myDistr, tr( "SMESH_DISTR" ) );
76 StdMeshersGUI_DistrPreview::~StdMeshersGUI_DistrPreview()
80 bool StdMeshersGUI_DistrPreview::isTableFunc() const
85 void StdMeshersGUI_DistrPreview::tableFunc( SMESH::double_array& f ) const
90 QString StdMeshersGUI_DistrPreview::function() const
95 int StdMeshersGUI_DistrPreview::nbSeg() const
100 int StdMeshersGUI_DistrPreview::pointsCount() const
105 void StdMeshersGUI_DistrPreview::setConversion( Conversion conv, const bool upd )
112 bool StdMeshersGUI_DistrPreview::setParams( const QString& func, const int nbSeg, const int points, const bool upd )
115 myTableFunc = SMESH::double_array();
116 myFunction = func.isEmpty() ? "0" : func;
117 myPoints = points>0 ? points : 2;
118 myNbSeg = nbSeg>0 ? nbSeg : 1;
119 bool res = init( func );
125 bool StdMeshersGUI_DistrPreview::setParams( const SMESH::double_array& f, const int nbSeg, const bool upd )
129 if( myTableFunc.length()%2==1 )
130 myTableFunc.length( myTableFunc.length()-1 );
133 myPoints = myTableFunc.length()/2;
134 myNbSeg = nbSeg>0 ? nbSeg : 1;
139 return myTableFunc.length()>0;
142 bool StdMeshersGUI_DistrPreview::createTable( SMESH::double_array& func )
144 if( myExpr.IsNull() )
150 const double xmin = 0.0, xmax = 1.0;
152 double d = (xmax-xmin)/double(myPoints-1);
153 func.length( 2*myPoints );
155 for( int i=0, j=0; i<myPoints; j++ )
158 double t = xmin + d*j, f = funcValue( t, ok );
168 func.length( func.length()-2*err );
172 void StdMeshersGUI_DistrPreview::update()
174 SMESH::double_array graph, distr;
181 myIsDone = createTable( graph );
183 if( graph.length()>=2 )
185 StdMeshers::StdMeshers_NumberOfSegments_var h =
186 StdMeshers::StdMeshers_NumberOfSegments::_narrow( myHypo );
188 if( !CORBA::is_nil( h.in() ) )
190 SMESH::double_array* arr = 0;
192 arr = h->BuildDistributionTab( myTableFunc, myNbSeg, ( int )myConv );
194 arr = h->BuildDistributionExpr( myFunction.latin1(), myNbSeg, ( int )myConv );
203 bool correct = graph.length()>=2 && distr.length()>=2;
210 setMarkerLabel( myMsg, QString() );
212 int size = graph.length()/2;
213 double* x = new double[size], *y = new double[size];
214 double min_x, max_x, min_y, max_y;
215 for( int i=0; i<size; i++ )
219 if( !convert( y[i] ) )
221 min_x = 0.0; max_x = 1.0; min_y = 0.0; max_y = 1.0;
222 delete[] x; delete[] y;
227 if( i==0 || y[i]<min_y )
229 if( i==0 || y[i]>max_y )
231 if( i==0 || x[i]<min_x )
233 if( i==0 || x[i]>max_x )
237 setAxisScale( curveXAxis( myDensity ), min_x, max_x );
238 setAxisScale( curveYAxis( myDensity ), min( 0.0, min_y ), max( 0.0, max_y ) );
239 setCurveData( myDensity, x, y, size );
246 size = distr.length();
247 x = new double[size];
248 y = new double[size];
249 for( int i=0; i<size; i++ )
254 setCurveData( myDistr, x, y, size );
264 } catch(Standard_Failure) {
265 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
269 void StdMeshersGUI_DistrPreview::showError()
271 setAxisScale( curveXAxis( myDensity ), 0.0, 1.0 );
272 setAxisScale( curveYAxis( myDensity ), 0.0, 1.0 );
273 setCurveData( myDensity, 0, 0, 0 );
274 setCurveData( myDistr, 0, 0, 0 );
275 setMarkerLabel( myMsg, tr( "SMESH_INVALID_FUNCTION" ) );
279 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
281 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
283 return sub->GetName()=="t";
286 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
288 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
289 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
292 if( name->GetName()!="t" )
296 res = isCorrectArg( sub );
301 bool StdMeshersGUI_DistrPreview::init( const QString& str )
303 bool parsed_ok = true;
308 myExpr = ExprIntrp_GenExp::Create();
309 myExpr->Process( ( Standard_CString ) str.latin1() );
310 } catch(Standard_Failure) {
311 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
315 bool syntax = false, args = false;
316 if( parsed_ok && myExpr->IsDone() )
319 args = isCorrectArg( myExpr->Expression() );
322 bool res = parsed_ok && syntax && args;
328 double StdMeshersGUI_DistrPreview::funcValue( const double t, bool& ok )
330 if( myExpr.IsNull() )
333 myValues.ChangeValue( 1 ) = t;
336 double res = calc( ok );
341 double StdMeshersGUI_DistrPreview::calc( bool& ok )
350 res = myExpr->Expression()->Evaluate( myVars, myValues );
351 } catch(Standard_Failure) {
352 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
360 bool StdMeshersGUI_DistrPreview::isDone() const
365 bool StdMeshersGUI_DistrPreview::convert( double& v ) const
376 // in StdMeshers_NumberOfSegments.cc
377 // const double PRECISION = 1e-7;
381 } catch(Standard_Failure) {
382 Handle(Standard_Failure) aFail = Standard_Failure::Caught();