| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 | 
							- /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 
-  * Qwt Widget Library
 
-  * Copyright (C) 1997   Josef Wilgen
 
-  * Copyright (C) 2002   Uwe Rathmann
 
-  *
 
-  * This library is free software; you can redistribute it and/or
 
-  * modify it under the terms of the Qwt License, Version 1.0
 
-  *****************************************************************************/
 
- #include "qwt_curve_fitter.h"
 
- #include "qwt_math.h"
 
- #include "qwt_spline.h"
 
- #include <qstack.h>
 
- #include <qvector.h>
 
- #if QT_VERSION < 0x040601
 
- #define qFabs(x) ::fabs(x)
 
- #endif
 
- //! Constructor
 
- QwtCurveFitter::QwtCurveFitter()
 
- {
 
- }
 
- //! Destructor
 
- QwtCurveFitter::~QwtCurveFitter()
 
- {
 
- }
 
- class QwtSplineCurveFitter::PrivateData
 
- {
 
- public:
 
-     PrivateData():
 
-         fitMode( QwtSplineCurveFitter::Auto ),
 
-         splineSize( 250 )
 
-     {
 
-     }
 
-     QwtSpline spline;
 
-     QwtSplineCurveFitter::FitMode fitMode;
 
-     int splineSize;
 
- };
 
- //! Constructor
 
- QwtSplineCurveFitter::QwtSplineCurveFitter()
 
- {
 
-     d_data = new PrivateData;
 
- }
 
- //! Destructor
 
- QwtSplineCurveFitter::~QwtSplineCurveFitter()
 
- {
 
-     delete d_data;
 
- }
 
- /*!
 
-   Select the algorithm used for building the spline
 
-   \param mode Mode representing a spline algorithm
 
-   \sa fitMode()
 
- */
 
- void QwtSplineCurveFitter::setFitMode( FitMode mode )
 
- {
 
-     d_data->fitMode = mode;
 
- }
 
- /*!
 
-   \return Mode representing a spline algorithm
 
-   \sa setFitMode()
 
- */
 
- QwtSplineCurveFitter::FitMode QwtSplineCurveFitter::fitMode() const
 
- {
 
-     return d_data->fitMode;
 
- }
 
- /*!
 
-   Assign a spline
 
-   \param spline Spline
 
-   \sa spline()
 
- */
 
- void QwtSplineCurveFitter::setSpline( const QwtSpline &spline )
 
- {
 
-     d_data->spline = spline;
 
-     d_data->spline.reset();
 
- }
 
- /*!
 
-   \return Spline
 
-   \sa setSpline()
 
- */
 
- const QwtSpline &QwtSplineCurveFitter::spline() const
 
- {
 
-     return d_data->spline;
 
- }
 
- /*!
 
-   \return Spline
 
-   \sa setSpline()
 
- */
 
- QwtSpline &QwtSplineCurveFitter::spline()
 
- {
 
-     return d_data->spline;
 
- }
 
- /*!
 
-    Assign a spline size ( has to be at least 10 points )
 
-    \param splineSize Spline size
 
-    \sa splineSize()
 
- */
 
- void QwtSplineCurveFitter::setSplineSize( int splineSize )
 
- {
 
-     d_data->splineSize = qMax( splineSize, 10 );
 
- }
 
- /*!
 
-   \return Spline size
 
-   \sa setSplineSize()
 
- */
 
- int QwtSplineCurveFitter::splineSize() const
 
- {
 
-     return d_data->splineSize;
 
- }
 
- /*!
 
-   Find a curve which has the best fit to a series of data points
 
-   \param points Series of data points
 
-   \return Curve points
 
- */
 
- QPolygonF QwtSplineCurveFitter::fitCurve( const QPolygonF &points ) const
 
- {
 
-     const int size = ( int )points.size();
 
-     if ( size <= 2 )
 
-         return points;
 
-     FitMode fitMode = d_data->fitMode;
 
-     if ( fitMode == Auto )
 
-     {
 
-         fitMode = Spline;
 
-         const QPointF *p = points.data();
 
-         for ( int i = 1; i < size; i++ )
 
-         {
 
-             if ( p[i].x() <= p[i-1].x() )
 
-             {
 
-                 fitMode = ParametricSpline;
 
-                 break;
 
-             }
 
-         };
 
-     }
 
-     if ( fitMode == ParametricSpline )
 
-         return fitParametric( points );
 
-     else
 
-         return fitSpline( points );
 
- }
 
- QPolygonF QwtSplineCurveFitter::fitSpline( const QPolygonF &points ) const
 
- {
 
-     d_data->spline.setPoints( points );
 
-     if ( !d_data->spline.isValid() )
 
-         return points;
 
-     QPolygonF fittedPoints( d_data->splineSize );
 
-     const double x1 = points[0].x();
 
-     const double x2 = points[int( points.size() - 1 )].x();
 
-     const double dx = x2 - x1;
 
-     const double delta = dx / ( d_data->splineSize - 1 );
 
-     for ( int i = 0; i < d_data->splineSize; i++ )
 
-     {
 
-         QPointF &p = fittedPoints[i];
 
-         const double v = x1 + i * delta;
 
-         const double sv = d_data->spline.value( v );
 
-         p.setX( qRound( v ) );
 
-         p.setY( qRound( sv ) );
 
-     }
 
-     d_data->spline.reset();
 
-     return fittedPoints;
 
- }
 
- QPolygonF QwtSplineCurveFitter::fitParametric( const QPolygonF &points ) const
 
- {
 
-     int i;
 
-     const int size = points.size();
 
-     QPolygonF fittedPoints( d_data->splineSize );
 
-     QPolygonF splinePointsX( size );
 
-     QPolygonF splinePointsY( size );
 
-     const QPointF *p = points.data();
 
-     QPointF *spX = splinePointsX.data();
 
-     QPointF *spY = splinePointsY.data();
 
-     double param = 0.0;
 
-     for ( i = 0; i < size; i++ )
 
-     {
 
-         const double x = p[i].x();
 
-         const double y = p[i].y();
 
-         if ( i > 0 )
 
-         {
 
-             const double delta = qSqrt( qwtSqr( x - spX[i-1].y() )
 
-                       + qwtSqr( y - spY[i-1].y() ) );
 
-             param += qMax( delta, 1.0 );
 
-         }
 
-         spX[i].setX( param );
 
-         spX[i].setY( x );
 
-         spY[i].setX( param );
 
-         spY[i].setY( y );
 
-     }
 
-     d_data->spline.setPoints( splinePointsX );
 
-     if ( !d_data->spline.isValid() )
 
-         return points;
 
-     const double deltaX =
 
-         splinePointsX[size - 1].x() / ( d_data->splineSize - 1 );
 
-     for ( i = 0; i < d_data->splineSize; i++ )
 
-     {
 
-         const double dtmp = i * deltaX;
 
-         fittedPoints[i].setX( qRound( d_data->spline.value( dtmp ) ) );
 
-     }
 
-     d_data->spline.setPoints( splinePointsY );
 
-     if ( !d_data->spline.isValid() )
 
-         return points;
 
-     const double deltaY =
 
-         splinePointsY[size - 1].x() / ( d_data->splineSize - 1 );
 
-     for ( i = 0; i < d_data->splineSize; i++ )
 
-     {
 
-         const double dtmp = i * deltaY;
 
-         fittedPoints[i].setY( qRound( d_data->spline.value( dtmp ) ) );
 
-     }
 
-     return fittedPoints;
 
- }
 
- class QwtWeedingCurveFitter::PrivateData
 
- {
 
- public:
 
-     PrivateData():
 
-         tolerance( 1.0 )
 
-     {
 
-     }
 
-     double tolerance;
 
- };
 
- class QwtWeedingCurveFitter::Line
 
- {
 
- public:
 
-     Line( int i1 = 0, int i2 = 0 ):
 
-         from( i1 ),
 
-         to( i2 )
 
-     {
 
-     }
 
-     int from;
 
-     int to;
 
- };
 
- /*!
 
-    Constructor
 
-    \param tolerance Tolerance
 
-    \sa setTolerance(), tolerance()
 
- */
 
- QwtWeedingCurveFitter::QwtWeedingCurveFitter( double tolerance )
 
- {
 
-     d_data = new PrivateData;
 
-     setTolerance( tolerance );
 
- }
 
- //! Destructor
 
- QwtWeedingCurveFitter::~QwtWeedingCurveFitter()
 
- {
 
-     delete d_data;
 
- }
 
- /*!
 
-  Assign the tolerance
 
-  The tolerance is the maximum distance, that is accaptable
 
-  between the original curve and the smoothed curve.
 
-  Increasing the tolerance will reduce the number of the
 
-  resulting points.
 
-  \param tolerance Tolerance
 
-  \sa tolerance()
 
- */
 
- void QwtWeedingCurveFitter::setTolerance( double tolerance )
 
- {
 
-     d_data->tolerance = qMax( tolerance, 0.0 );
 
- }
 
- /*!
 
-   \return Tolerance
 
-   \sa setTolerance()
 
- */
 
- double QwtWeedingCurveFitter::tolerance() const
 
- {
 
-     return d_data->tolerance;
 
- }
 
- /*!
 
-   \param points Series of data points
 
-   \return Curve points
 
- */
 
- QPolygonF QwtWeedingCurveFitter::fitCurve( const QPolygonF &points ) const
 
- {
 
-     QStack<Line> stack;
 
-     stack.reserve( 500 );
 
-     const QPointF *p = points.data();
 
-     const int nPoints = points.size();
 
-     QVector<bool> usePoint( nPoints, false );
 
-     double distToSegment;
 
-     stack.push( Line( 0, nPoints - 1 ) );
 
-     while ( !stack.isEmpty() )
 
-     {
 
-         const Line r = stack.pop();
 
-         // initialize line segment
 
-         const double vecX = p[r.to].x() - p[r.from].x();
 
-         const double vecY = p[r.to].y() - p[r.from].y();
 
-         const double vecLength = qSqrt( vecX * vecX + vecY * vecY );
 
-         const double unitVecX = ( vecLength != 0.0 ) ? vecX / vecLength : 0.0;
 
-         const double unitVecY = ( vecLength != 0.0 ) ? vecY / vecLength : 0.0;
 
-         double maxDist = 0.0;
 
-         int nVertexIndexMaxDistance = r.from + 1;
 
-         for ( int i = r.from + 1; i < r.to; i++ )
 
-         {
 
-             //compare to anchor
 
-             const double fromVecX = p[i].x() - p[r.from].x();
 
-             const double fromVecY = p[i].y() - p[r.from].y();
 
-             const double fromVecLength =
 
-                 qSqrt( fromVecX * fromVecX + fromVecY * fromVecY );
 
-             if ( fromVecX * unitVecX + fromVecY * unitVecY < 0.0 )
 
-             {
 
-                 distToSegment = fromVecLength;
 
-             }
 
-             if ( fromVecX * unitVecX + fromVecY * unitVecY < 0.0 )
 
-             {
 
-                 distToSegment = fromVecLength;
 
-             }
 
-             else
 
-             {
 
-                 const double toVecX = p[i].x() - p[r.to].x();
 
-                 const double toVecY = p[i].y() - p[r.to].y();
 
-                 const double toVecLength = qSqrt( toVecX * toVecX + toVecY * toVecY );
 
-                 const double s = toVecX * ( -unitVecX ) + toVecY * ( -unitVecY );
 
-                 if ( s < 0.0 )
 
-                     distToSegment = toVecLength;
 
-                 else
 
-                 {
 
-                     distToSegment = qSqrt( qFabs( toVecLength * toVecLength - s * s ) );
 
-                 }
 
-             }
 
-             if ( maxDist < distToSegment )
 
-             {
 
-                 maxDist = distToSegment;
 
-                 nVertexIndexMaxDistance = i;
 
-             }
 
-         }
 
-         if ( maxDist <= d_data->tolerance )
 
-         {
 
-             usePoint[r.from] = true;
 
-             usePoint[r.to] = true;
 
-         }
 
-         else
 
-         {
 
-             stack.push( Line( r.from, nVertexIndexMaxDistance ) );
 
-             stack.push( Line( nVertexIndexMaxDistance, r.to ) );
 
-         }
 
-     }
 
-     int cnt = 0;
 
-     QPolygonF stripped( nPoints );
 
-     for ( int i = 0; i < nPoints; i++ )
 
-     {
 
-         if ( usePoint[i] )
 
-             stripped[cnt++] = p[i];
 
-     }
 
-     stripped.resize( cnt );
 
-     return stripped;
 
- }
 
 
  |