qwt_curve_fitter.cpp 5.16 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 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * 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_math.h"
#include "qwt_spline.h"
#include "qwt_curve_fitter.h"

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

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

class QwtSplineCurveFitter::PrivateData
{
public:
    PrivateData():
        fitMode(QwtSplineCurveFitter::Auto),
29
        splineSize(250) {
pixhawk's avatar
pixhawk committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    }

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

QwtSplineCurveFitter::QwtSplineCurveFitter()
{
    d_data = new PrivateData;
}

QwtSplineCurveFitter::~QwtSplineCurveFitter()
{
    delete d_data;
}

void QwtSplineCurveFitter::setFitMode(FitMode mode)
{
    d_data->fitMode = mode;
}

QwtSplineCurveFitter::FitMode QwtSplineCurveFitter::fitMode() const
{
    return d_data->fitMode;
}

void QwtSplineCurveFitter::setSpline(const QwtSpline &spline)
{
    d_data->spline = spline;
    d_data->spline.reset();
}

const QwtSpline &QwtSplineCurveFitter::spline() const
{
    return d_data->spline;
}

68
QwtSpline &QwtSplineCurveFitter::spline()
pixhawk's avatar
pixhawk committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
{
    return d_data->spline;
}

void QwtSplineCurveFitter::setSplineSize(int splineSize)
{
    d_data->splineSize = qwtMax(splineSize, 10);
}

int QwtSplineCurveFitter::splineSize() const
{
    return d_data->splineSize;
}

#if QT_VERSION < 0x040000
QwtArray<QwtDoublePoint> QwtSplineCurveFitter::fitCurve(
    const QwtArray<QwtDoublePoint> & points) const
#else
QPolygonF QwtSplineCurveFitter::fitCurve(
    const QPolygonF &points) const
#endif
{
    const int size = (int)points.size();
    if ( size <= 2 )
        return points;

    FitMode fitMode = d_data->fitMode;
96
    if ( fitMode == Auto ) {
pixhawk's avatar
pixhawk committed
97 98 99
        fitMode = Spline;

        const QwtDoublePoint *p = points.data();
100 101
        for ( int i = 1; i < size; i++ ) {
            if ( p[i].x() <= p[i-1].x() ) {
pixhawk's avatar
pixhawk committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
                fitMode = ParametricSpline;
                break;
            }
        };
    }

    if ( fitMode == ParametricSpline )
        return fitParametric(points);
    else
        return fitSpline(points);
}

#if QT_VERSION < 0x040000
QwtArray<QwtDoublePoint> QwtSplineCurveFitter::fitSpline(
    const QwtArray<QwtDoublePoint> &points) const
#else
QPolygonF QwtSplineCurveFitter::fitSpline(
    const QPolygonF &points) const
#endif
{
    d_data->spline.setPoints(points);
    if ( !d_data->spline.isValid() )
        return points;

#if QT_VERSION < 0x040000
    QwtArray<QwtDoublePoint> fittedPoints(d_data->splineSize);
#else
    QPolygonF fittedPoints(d_data->splineSize);
#endif

    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);

137
    for (int i = 0; i < d_data->splineSize; i++) {
pixhawk's avatar
pixhawk committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
        QwtDoublePoint &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;
}

#if QT_VERSION < 0x040000
QwtArray<QwtDoublePoint> QwtSplineCurveFitter::fitParametric(
    const QwtArray<QwtDoublePoint> &points) const
#else
QPolygonF QwtSplineCurveFitter::fitParametric(
    const QPolygonF &points) const
#endif
{
    int i;
    const int size = points.size();

#if QT_VERSION < 0x040000
    QwtArray<QwtDoublePoint> fittedPoints(d_data->splineSize);
    QwtArray<QwtDoublePoint> splinePointsX(size);
    QwtArray<QwtDoublePoint> splinePointsY(size);
#else
    QPolygonF fittedPoints(d_data->splineSize);
    QPolygonF splinePointsX(size);
    QPolygonF splinePointsY(size);
#endif

    const QwtDoublePoint *p = points.data();
    QwtDoublePoint *spX = splinePointsX.data();
    QwtDoublePoint *spY = splinePointsY.data();

    double param = 0.0;
177
    for (i = 0; i < size; i++) {
pixhawk's avatar
pixhawk committed
178 179
        const double x = p[i].x();
        const double y = p[i].y();
180
        if ( i > 0 ) {
pixhawk's avatar
pixhawk committed
181
            const double delta = sqrt( qwtSqr(x - spX[i-1].y())
182
                                       + qwtSqr( y - spY[i-1].y() ) );
pixhawk's avatar
pixhawk committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196
            param += qwtMax(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);
197
    for (i = 0; i < d_data->splineSize; i++) {
pixhawk's avatar
pixhawk committed
198 199 200 201 202 203 204 205 206 207
        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);
208
    for (i = 0; i < d_data->splineSize; i++) {
pixhawk's avatar
pixhawk committed
209 210 211 212 213 214
        const double dtmp = i * deltaY;
        fittedPoints[i].setY(qRound(d_data->spline.value(dtmp)));
    }

    return fittedPoints;
}