qwt_curve_fitter.cpp 10.1 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
5
 *
pixhawk's avatar
pixhawk committed
6 7 8 9
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

Bryant's avatar
Bryant committed
10
#include "qwt_curve_fitter.h"
pixhawk's avatar
pixhawk committed
11 12
#include "qwt_math.h"
#include "qwt_spline.h"
Bryant's avatar
Bryant committed
13 14 15 16 17 18
#include <qstack.h>
#include <qvector.h>

#if QT_VERSION < 0x040601
#define qFabs(x) ::fabs(x)
#endif
pixhawk's avatar
pixhawk committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

//! Constructor
QwtCurveFitter::QwtCurveFitter()
{
}

//! Destructor
QwtCurveFitter::~QwtCurveFitter()
{
}

class QwtSplineCurveFitter::PrivateData
{
public:
    PrivateData():
Bryant's avatar
Bryant committed
34 35 36
        fitMode( QwtSplineCurveFitter::Auto ),
        splineSize( 250 )
    {
pixhawk's avatar
pixhawk committed
37 38 39 40 41 42 43
    }

    QwtSpline spline;
    QwtSplineCurveFitter::FitMode fitMode;
    int splineSize;
};

Bryant's avatar
Bryant committed
44
//! Constructor
pixhawk's avatar
pixhawk committed
45 46 47 48 49
QwtSplineCurveFitter::QwtSplineCurveFitter()
{
    d_data = new PrivateData;
}

Bryant's avatar
Bryant committed
50
//! Destructor
pixhawk's avatar
pixhawk committed
51 52 53 54 55
QwtSplineCurveFitter::~QwtSplineCurveFitter()
{
    delete d_data;
}

Bryant's avatar
Bryant committed
56 57 58 59 60 61 62
/*!
  Select the algorithm used for building the spline

  \param mode Mode representing a spline algorithm
  \sa fitMode()
*/
void QwtSplineCurveFitter::setFitMode( FitMode mode )
pixhawk's avatar
pixhawk committed
63 64 65 66
{
    d_data->fitMode = mode;
}

Bryant's avatar
Bryant committed
67 68 69 70
/*!
  \return Mode representing a spline algorithm
  \sa setFitMode()
*/
pixhawk's avatar
pixhawk committed
71 72 73 74 75
QwtSplineCurveFitter::FitMode QwtSplineCurveFitter::fitMode() const
{
    return d_data->fitMode;
}

Bryant's avatar
Bryant committed
76 77 78 79 80 81 82
/*!
  Assign a spline

  \param spline Spline
  \sa spline()
*/
void QwtSplineCurveFitter::setSpline( const QwtSpline &spline )
pixhawk's avatar
pixhawk committed
83 84 85 86 87
{
    d_data->spline = spline;
    d_data->spline.reset();
}

Bryant's avatar
Bryant committed
88 89 90 91
/*!
  \return Spline
  \sa setSpline()
*/
pixhawk's avatar
pixhawk committed
92 93 94 95 96
const QwtSpline &QwtSplineCurveFitter::spline() const
{
    return d_data->spline;
}

Bryant's avatar
Bryant committed
97 98 99 100
/*!
  \return Spline
  \sa setSpline()
*/
101
QwtSpline &QwtSplineCurveFitter::spline()
pixhawk's avatar
pixhawk committed
102 103 104 105
{
    return d_data->spline;
}

Bryant's avatar
Bryant committed
106 107 108 109 110 111 112
/*!
   Assign a spline size ( has to be at least 10 points )

   \param splineSize Spline size
   \sa splineSize()
*/
void QwtSplineCurveFitter::setSplineSize( int splineSize )
pixhawk's avatar
pixhawk committed
113
{
Bryant's avatar
Bryant committed
114
    d_data->splineSize = qMax( splineSize, 10 );
pixhawk's avatar
pixhawk committed
115 116
}

Bryant's avatar
Bryant committed
117 118 119 120
/*!
  \return Spline size
  \sa setSplineSize()
*/
pixhawk's avatar
pixhawk committed
121 122 123 124 125
int QwtSplineCurveFitter::splineSize() const
{
    return d_data->splineSize;
}

Bryant's avatar
Bryant committed
126 127 128 129 130 131 132
/*!
  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
pixhawk's avatar
pixhawk committed
133
{
Bryant's avatar
Bryant committed
134
    const int size = points.size();
pixhawk's avatar
pixhawk committed
135 136 137 138
    if ( size <= 2 )
        return points;

    FitMode fitMode = d_data->fitMode;
Bryant's avatar
Bryant committed
139 140
    if ( fitMode == Auto )
    {
pixhawk's avatar
pixhawk committed
141 142
        fitMode = Spline;

Bryant's avatar
Bryant committed
143 144 145 146 147
        const QPointF *p = points.data();
        for ( int i = 1; i < size; i++ )
        {
            if ( p[i].x() <= p[i-1].x() )
            {
pixhawk's avatar
pixhawk committed
148 149 150 151 152 153 154
                fitMode = ParametricSpline;
                break;
            }
        };
    }

    if ( fitMode == ParametricSpline )
Bryant's avatar
Bryant committed
155
        return fitParametric( points );
pixhawk's avatar
pixhawk committed
156
    else
Bryant's avatar
Bryant committed
157
        return fitSpline( points );
pixhawk's avatar
pixhawk committed
158 159
}

Bryant's avatar
Bryant committed
160
QPolygonF QwtSplineCurveFitter::fitSpline( const QPolygonF &points ) const
pixhawk's avatar
pixhawk committed
161
{
Bryant's avatar
Bryant committed
162
    d_data->spline.setPoints( points );
pixhawk's avatar
pixhawk committed
163 164 165
    if ( !d_data->spline.isValid() )
        return points;

Bryant's avatar
Bryant committed
166
    QPolygonF fittedPoints( d_data->splineSize );
pixhawk's avatar
pixhawk committed
167 168

    const double x1 = points[0].x();
Bryant's avatar
Bryant committed
169
    const double x2 = points[int( points.size() - 1 )].x();
pixhawk's avatar
pixhawk committed
170
    const double dx = x2 - x1;
Bryant's avatar
Bryant committed
171
    const double delta = dx / ( d_data->splineSize - 1 );
pixhawk's avatar
pixhawk committed
172

Bryant's avatar
Bryant committed
173 174 175
    for ( int i = 0; i < d_data->splineSize; i++ )
    {
        QPointF &p = fittedPoints[i];
pixhawk's avatar
pixhawk committed
176 177

        const double v = x1 + i * delta;
Bryant's avatar
Bryant committed
178
        const double sv = d_data->spline.value( v );
pixhawk's avatar
pixhawk committed
179

Bryant's avatar
Bryant committed
180 181
        p.setX( v );
        p.setY( sv );
pixhawk's avatar
pixhawk committed
182 183 184 185 186 187
    }
    d_data->spline.reset();

    return fittedPoints;
}

Bryant's avatar
Bryant committed
188
QPolygonF QwtSplineCurveFitter::fitParametric( const QPolygonF &points ) const
pixhawk's avatar
pixhawk committed
189 190 191 192
{
    int i;
    const int size = points.size();

Bryant's avatar
Bryant committed
193 194 195
    QPolygonF fittedPoints( d_data->splineSize );
    QPolygonF splinePointsX( size );
    QPolygonF splinePointsY( size );
pixhawk's avatar
pixhawk committed
196

Bryant's avatar
Bryant committed
197 198 199
    const QPointF *p = points.data();
    QPointF *spX = splinePointsX.data();
    QPointF *spY = splinePointsY.data();
pixhawk's avatar
pixhawk committed
200 201

    double param = 0.0;
Bryant's avatar
Bryant committed
202 203
    for ( i = 0; i < size; i++ )
    {
pixhawk's avatar
pixhawk committed
204 205
        const double x = p[i].x();
        const double y = p[i].y();
Bryant's avatar
Bryant committed
206 207 208 209 210
        if ( i > 0 )
        {
            const double delta = qSqrt( qwtSqr( x - spX[i-1].y() )
                      + qwtSqr( y - spY[i-1].y() ) );
            param += qMax( delta, 1.0 );
pixhawk's avatar
pixhawk committed
211
        }
Bryant's avatar
Bryant committed
212 213 214 215
        spX[i].setX( param );
        spX[i].setY( x );
        spY[i].setX( param );
        spY[i].setY( y );
pixhawk's avatar
pixhawk committed
216 217
    }

Bryant's avatar
Bryant committed
218
    d_data->spline.setPoints( splinePointsX );
pixhawk's avatar
pixhawk committed
219 220 221 222
    if ( !d_data->spline.isValid() )
        return points;

    const double deltaX =
Bryant's avatar
Bryant committed
223 224 225
        splinePointsX[size - 1].x() / ( d_data->splineSize - 1 );
    for ( i = 0; i < d_data->splineSize; i++ )
    {
pixhawk's avatar
pixhawk committed
226
        const double dtmp = i * deltaX;
Bryant's avatar
Bryant committed
227
        fittedPoints[i].setX( d_data->spline.value( dtmp ) );
pixhawk's avatar
pixhawk committed
228 229
    }

Bryant's avatar
Bryant committed
230
    d_data->spline.setPoints( splinePointsY );
pixhawk's avatar
pixhawk committed
231 232 233 234
    if ( !d_data->spline.isValid() )
        return points;

    const double deltaY =
Bryant's avatar
Bryant committed
235 236 237
        splinePointsY[size - 1].x() / ( d_data->splineSize - 1 );
    for ( i = 0; i < d_data->splineSize; i++ )
    {
pixhawk's avatar
pixhawk committed
238
        const double dtmp = i * deltaY;
Bryant's avatar
Bryant committed
239
        fittedPoints[i].setY( d_data->spline.value( dtmp ) );
pixhawk's avatar
pixhawk committed
240 241 242 243
    }

    return fittedPoints;
}
Bryant's avatar
Bryant committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

class QwtWeedingCurveFitter::PrivateData
{
public:
    PrivateData():
        tolerance( 1.0 ),
        chunkSize( 0 )
    {
    }

    double tolerance;
    uint chunkSize;
};

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 acceptable
 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;
}

/*!
 Limit the number of points passed to a run of the algorithm

 The runtime of the Douglas Peucker algorithm increases non linear
 with the number of points. For a chunk size > 0 the polygon
 is split into pieces passed to the algorithm one by one.

 \param numPoints Maximum for the number of points passed to the algorithm

 \sa chunkSize()
*/
void QwtWeedingCurveFitter::setChunkSize( uint numPoints )
{
    if ( numPoints > 0 )
        numPoints = qMax( numPoints, 3U );

    d_data->chunkSize = numPoints;
}

/*!
  
  \return Maximum for the number of points passed to a run 
          of the algorithm - or 0, when unlimited
  \sa setChunkSize()
*/
uint QwtWeedingCurveFitter::chunkSize() const
{
    return d_data->chunkSize;
}

/*!
  \param points Series of data points
  \return Curve points
*/
QPolygonF QwtWeedingCurveFitter::fitCurve( const QPolygonF &points ) const
{
    QPolygonF fittedPoints;

    if ( d_data->chunkSize == 0 )
    {
        fittedPoints = simplify( points );
    }
    else
    {
        for ( int i = 0; i < points.size(); i += d_data->chunkSize )
        {
            const QPolygonF p = points.mid( i, d_data->chunkSize );
            fittedPoints += simplify( p );
        }
    }

    return fittedPoints;
}

QPolygonF QwtWeedingCurveFitter::simplify( const QPolygonF &points ) const
{
    const double toleranceSqr = d_data->tolerance * d_data->tolerance;

    QStack<Line> stack;
    stack.reserve( 500 );

    const QPointF *p = points.data();
    const int nPoints = points.size();

    QVector<bool> usePoint( nPoints, false );

    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 maxDistSqr = 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();

            double distToSegmentSqr;
            if ( fromVecX * unitVecX + fromVecY * unitVecY < 0.0 )
            {
                distToSegmentSqr = fromVecX * fromVecX + fromVecY * fromVecY;
            }
            else
            {
                const double toVecX = p[i].x() - p[r.to].x();
                const double toVecY = p[i].y() - p[r.to].y();
                const double toVecLength = toVecX * toVecX + toVecY * toVecY;

                const double s = toVecX * ( -unitVecX ) + toVecY * ( -unitVecY );
                if ( s < 0.0 )
                {
                    distToSegmentSqr = toVecLength;
                }
                else
                {
                    distToSegmentSqr = qFabs( toVecLength - s * s );
                }
            }

            if ( maxDistSqr < distToSegmentSqr )
            {
                maxDistSqr = distToSegmentSqr;
                nVertexIndexMaxDistance = i;
            }
        }
        if ( maxDistSqr <= toleranceSqr )
        {
            usePoint[r.from] = true;
            usePoint[r.to] = true;
        }
        else
        {
            stack.push( Line( r.from, nVertexIndexMaxDistance ) );
            stack.push( Line( nVertexIndexMaxDistance, r.to ) );
        }
    }

    QPolygonF stripped;
    for ( int i = 0; i < nPoints; i++ )
    {
        if ( usePoint[i] )
            stripped += p[i];
    }

    return stripped;
}