Back to index

salome-smesh  6.5.0
StdMeshersGUI_DistrPreview.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File   : StdMeshersGUI_DistrPreview.cxx
00024 // Author : Open CASCADE S.A.S.
00025 // SMESH includes
00026 //
00027 #include "StdMeshersGUI_DistrPreview.h"
00028 
00029 // Qwt includes
00030 #include <qwt_plot_curve.h>
00031 #include <qwt_plot_marker.h>
00032 #include <qwt_plot_grid.h>
00033 #include <qwt_symbol.h>
00034 #include <qwt_legend.h>
00035 
00036 // OCCT includes
00037 #include <Expr_NamedUnknown.hxx>
00038 #include <Expr_GeneralExpression.hxx>
00039 
00040 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
00041 #define NO_CAS_CATCH
00042 #endif
00043 
00044 #include <Standard_Failure.hxx>
00045 
00046 #ifdef NO_CAS_CATCH
00047 #include <Standard_ErrorHandler.hxx>
00048 #endif
00049 
00050 #ifdef WIN32
00051 # include <algorithm>
00052 #endif
00053 #include <math.h>
00054 #include <limits>
00055 
00056 #include <Basics_Utils.hxx>
00057 
00058 StdMeshersGUI_DistrPreview::StdMeshersGUI_DistrPreview( QWidget* p, StdMeshers::StdMeshers_NumberOfSegments_ptr h )
00059 : QwtPlot( p ),
00060   myPoints( 50 ),
00061   myIsTable( false ),
00062   myVars( 1, 1 ),
00063   myValues( 1, 1 ),
00064   myConv( CUT_NEGATIVE ),
00065   myIsDone( true ),
00066   myNbSeg( 1 )
00067 {
00068   Kernel_Utils::Localizer loc;
00069   myHypo = StdMeshers::StdMeshers_NumberOfSegments::_duplicate( h );
00070   myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
00071   myDensity = new QwtPlotCurve( QString() );
00072   myDensity->attach( this );
00073   myDistr = new QwtPlotCurve( QString() );
00074   myDistr->attach( this );
00075   myMsg = new QwtPlotMarker();
00076   myMsg->attach( this );
00077   myMsg->setValue( 0.5, 0.5 );
00078   QwtText mt = myMsg->label();
00079   mt.setBackgroundPen( QPen( Qt::red, 1 ) );
00080   QFont f = mt.font();
00081   f.setPointSize( 14 ); //f.setBold( true );
00082   mt.setFont( f );
00083   myMsg->setLabel( mt );
00084   myDensity->setPen( QPen( Qt::red, 1 ) );
00085 
00086   QColor dc = Qt::blue;
00087   myDistr->setPen( QPen( dc, 1 ) );
00088   myDistr->setSymbol( QwtSymbol( QwtSymbol::XCross, QBrush( dc ), QPen( dc ), QSize( 5, 5 ) ) );
00089 
00090   QwtLegend* l = legend();
00091   if ( !l ) {
00092     l = new QwtLegend( this );
00093     l->setFrameStyle( QFrame::Box | QFrame::Sunken );
00094   }
00095   insertLegend( l, QwtPlot::BottomLegend );
00096 
00097   enableAxis(QwtPlot::yLeft, false);
00098   enableAxis(QwtPlot::yRight, true);
00099   
00100   QFont axisFont;
00101   axisFont.setPointSize( 8 );
00102   setAxisFont(QwtPlot::yRight, axisFont); 
00103   setAxisFont(QwtPlot::xBottom, axisFont); 
00104   
00105   myDensity->setYAxis(QwtPlot::yRight);
00106   myDistr->setYAxis(QwtPlot::yRight);
00107   myMsg->setYAxis(QwtPlot::yRight);
00108   myDensity->setTitle( tr( "SMESH_DENSITY_FUNC" ) );
00109   myDistr->setTitle( tr( "SMESH_DISTR" ) );
00110   
00111   QwtPlotGrid* aGrid = new QwtPlotGrid();
00112   QPen aMajPen = aGrid->majPen();
00113   aMajPen.setStyle( Qt::DashLine );
00114   aGrid->setPen( aMajPen );
00115 
00116   aGrid->enableX( true );
00117   aGrid->enableY( true );
00118 
00119   aGrid->attach( this );
00120 }
00121 
00122 StdMeshersGUI_DistrPreview::~StdMeshersGUI_DistrPreview()
00123 {
00124 }
00125 
00126 bool StdMeshersGUI_DistrPreview::isTableFunc() const
00127 {
00128   return myIsTable;
00129 }
00130 
00131 void StdMeshersGUI_DistrPreview::tableFunc( SMESH::double_array& f ) const
00132 {
00133   f = myTableFunc;
00134 }
00135 
00136 QString StdMeshersGUI_DistrPreview::function() const
00137 {
00138   return myFunction;
00139 }
00140 
00141 int StdMeshersGUI_DistrPreview::nbSeg() const
00142 {
00143   return myNbSeg;
00144 }
00145 
00146 int StdMeshersGUI_DistrPreview::pointsCount() const
00147 {
00148   return myPoints;
00149 }
00150 
00151 void StdMeshersGUI_DistrPreview::setConversion( Conversion conv, const bool upd )
00152 {
00153   myConv = conv;
00154   if( upd )
00155     update();
00156 }
00157 
00158 bool StdMeshersGUI_DistrPreview::setParams( const QString& func, const int nbSeg, const int points, const bool upd )
00159 {
00160   myIsTable = false;
00161   myTableFunc = SMESH::double_array();
00162   myFunction = func.isEmpty() ? "0" : func;
00163   myPoints = points>0 ? points : 2;
00164   myNbSeg = nbSeg>0 ? nbSeg : 1;
00165   bool res = init( func );
00166   if( upd )
00167     update();
00168   return res;
00169 }
00170 
00171 bool StdMeshersGUI_DistrPreview::setParams( const SMESH::double_array& f, const int nbSeg, const bool upd )
00172 {
00173   myIsTable = true;
00174   myTableFunc = f;
00175   if( myTableFunc.length()%2==1 )
00176     myTableFunc.length( myTableFunc.length()-1 );
00177 
00178   myFunction = "0";
00179   myPoints = myTableFunc.length()/2;
00180   myNbSeg = nbSeg>0 ? nbSeg : 1;
00181 
00182   if( upd )
00183     update();
00184 
00185   return myTableFunc.length()>0;
00186 }
00187 
00188 bool StdMeshersGUI_DistrPreview::createTable( SMESH::double_array& func )
00189 {
00190   if( myExpr.IsNull() )
00191   {
00192     func.length( 0 );
00193     return false;
00194   }
00195 
00196   const double xmin = 0.0, xmax = 1.0;
00197 
00198   double d = (xmax-xmin)/double(myPoints-1);
00199   func.length( 2*myPoints );
00200   int err = 0;
00201   for( int i=0, j=0; i<myPoints; j++ )
00202   {
00203     bool ok;
00204     double t = xmin + d*j, f = funcValue( t, ok );
00205     if( ok )
00206     {
00207       func[2*i] = t;
00208       func[2*i+1] = f;
00209       i++;
00210     }
00211     else
00212       err++;
00213   }
00214   func.length( func.length()-2*err );
00215   return err==0;
00216 }
00217 
00218 void StdMeshersGUI_DistrPreview::update()
00219 {
00220   Kernel_Utils::Localizer loc;
00221   SMESH::double_array graph, distr;
00222   if( isTableFunc() )
00223   {
00224     myIsDone = true;
00225     graph = myTableFunc;
00226   }
00227   else
00228     myIsDone = createTable( graph );
00229 
00230   if( graph.length()>=2 )
00231   {
00232     StdMeshers::StdMeshers_NumberOfSegments_var h = 
00233       StdMeshers::StdMeshers_NumberOfSegments::_narrow( myHypo );
00234 
00235     if( !CORBA::is_nil( h.in() ) )
00236     {
00237       SMESH::double_array* arr = 0;
00238       if( isTableFunc() )
00239         arr = h->BuildDistributionTab( myTableFunc, myNbSeg, ( int )myConv );
00240       else
00241         arr = h->BuildDistributionExpr( myFunction.toLatin1().data(), myNbSeg, ( int )myConv );
00242       if( arr )
00243       {
00244         distr = *arr;
00245         delete arr;
00246       }
00247     }
00248   }
00249 
00250   bool correct = graph.length()>=2 && distr.length()>=2;
00251   if( !correct )
00252   {
00253     showError();
00254     return;
00255   }
00256   else
00257   {
00258     QwtText mt = myMsg->label();
00259     mt.setText( QString() );
00260     myMsg->setLabel( mt );
00261   }
00262 
00263   int size = graph.length()/2;
00264   double* x = new double[size], *y = new double[size];
00265   double min_x, max_x, min_y, max_y;
00266   for( int i=0; i<size; i++ )
00267   {
00268     x[i] = graph[2*i];
00269     y[i] = graph[2*i+1];
00270     if( !convert( y[i] ) )
00271     {
00272       min_x = 0.0; max_x = 1.0; min_y = 0.0; max_y = 1.0;
00273       delete[] x; delete[] y;
00274       x = y = 0;
00275       showError();
00276       return;
00277     }
00278 #ifdef WIN32
00279     if ( std::fabs(y[i]) >= HUGE_VAL)
00280       y[i] = HUGE_VAL/100.;
00281 #else
00282     if ( isinf(y[i]))
00283       y[i] = std::numeric_limits<double>::max()/100.;
00284 #endif
00285 //     if ( y[i] > 1e3 )
00286 //       y[i] = 1e3;
00287     if( i==0 || y[i]<min_y )
00288       min_y = y[i];
00289     if( i==0 || y[i]>max_y )
00290       max_y = y[i];
00291     if( i==0 || x[i]<min_x )
00292       min_x = x[i];
00293     if( i==0 || x[i]>max_x )
00294       max_x = x[i];
00295   }
00296 
00297   setAxisScale( myDensity->xAxis(), min_x, max_x );
00298   setAxisScale( myDensity->yAxis(),
00299 #ifdef WIN32
00300     min( 0.0, min_y ),
00301     max( 0.0, max_y )
00302 #else
00303     std::min( 0.0, min_y ),
00304     std::max( 0.0, max_y )
00305 #endif
00306     );
00307   myDensity->setData( x, y, size );
00308   if( x )
00309     delete[] x;
00310   if( y )
00311     delete[] y;
00312   x = y = 0;
00313 
00314   size = distr.length();
00315   x = new double[size];
00316   y = new double[size];
00317   for( int i=0; i<size; i++ )
00318   {
00319     x[i] = distr[i];
00320     y[i] = 0;
00321   }
00322   myDistr->setData( x, y, size );
00323   delete[] x;
00324   delete[] y;
00325   x = y = 0;
00326 
00327   try {   
00328 #ifdef NO_CAS_CATCH
00329     OCC_CATCH_SIGNALS;
00330 #endif
00331     replot();
00332   } catch(Standard_Failure) {
00333     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
00334   }
00335 }
00336 
00337 void StdMeshersGUI_DistrPreview::showError()
00338 {
00339   setAxisScale( myDensity->xAxis(), 0.0, 1.0 );
00340   setAxisScale( myDensity->yAxis(), 0.0, 1.0 );
00341   myDensity->setData( 0, 0, 0 );
00342   myDistr->setData( 0, 0, 0 );
00343   QwtText mt = myMsg->label();
00344   mt.setText( tr( "SMESH_INVALID_FUNCTION" ) );
00345   myMsg->setLabel( mt );
00346   replot();
00347 }
00348 
00349 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
00350 {
00351   Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
00352   if( !sub.IsNull() )
00353     return sub->GetName()=="t";
00354 
00355   bool res = true;
00356   for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
00357   {
00358     Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
00359     Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
00360     if( !name.IsNull() )
00361     {
00362       if( name->GetName()!="t" )
00363         res = false;
00364     }
00365     else
00366       res = isCorrectArg( sub );
00367   }
00368   return res;
00369 }
00370 
00371 bool StdMeshersGUI_DistrPreview::init( const QString& str )
00372 {
00373   Kernel_Utils::Localizer loc;
00374   bool parsed_ok = true;
00375   try {
00376 #ifdef NO_CAS_CATCH
00377     OCC_CATCH_SIGNALS;
00378 #endif
00379     myExpr = ExprIntrp_GenExp::Create();
00380     myExpr->Process( ( Standard_CString ) str.toLatin1().data() );
00381   } catch(Standard_Failure) {
00382     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
00383     parsed_ok = false;
00384   }
00385 
00386   bool syntax = false, args = false;
00387   if( parsed_ok && myExpr->IsDone() )
00388   {
00389     syntax = true;
00390     args = isCorrectArg( myExpr->Expression() );
00391   }
00392 
00393   bool res = parsed_ok && syntax && args;
00394   if( !res )
00395     myExpr.Nullify();
00396   return res;
00397 }
00398 
00399 double StdMeshersGUI_DistrPreview::funcValue( const double t, bool& ok )
00400 {
00401   if( myExpr.IsNull() )
00402     return 0;
00403 
00404   myValues.ChangeValue( 1 ) = t;
00405 
00406   ok = true;
00407   double res = calc( ok );
00408 
00409   return res;
00410 }
00411 
00412 double StdMeshersGUI_DistrPreview::calc( bool& ok )
00413 {
00414   double res = 0.0;
00415 
00416   ok = true;
00417   try {   
00418 #ifdef NO_CAS_CATCH
00419     OCC_CATCH_SIGNALS;
00420 #endif
00421     res = myExpr->Expression()->Evaluate( myVars, myValues );
00422   } catch(Standard_Failure) {
00423     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
00424     ok = false;
00425     res = 0.0;
00426   }
00427 
00428   return res;
00429 }
00430 
00431 bool StdMeshersGUI_DistrPreview::isDone() const
00432 {
00433   return myIsDone;
00434 }
00435 
00436 bool StdMeshersGUI_DistrPreview::convert( double& v ) const
00437 {
00438   bool ok = true;
00439   switch( myConv )
00440   {
00441   case EXPONENT:
00442     {
00443       try { 
00444 #ifdef NO_CAS_CATCH
00445         OCC_CATCH_SIGNALS;
00446 #endif
00447         // in StdMeshers_NumberOfSegments.cc
00448         // const double PRECISION = 1e-7;
00449         //
00450         if(v < -7) v = -7.0;
00451         v = pow( 10.0, v );
00452       } catch(Standard_Failure) {
00453         Handle(Standard_Failure) aFail = Standard_Failure::Caught();
00454         v = 0.0;
00455         ok = false;
00456       }
00457     }
00458     break;
00459 
00460   case CUT_NEGATIVE:
00461     if( v<0 )
00462       v = 0;
00463     break;
00464   }
00465 
00466   return ok;
00467 }