File: Curve.cpp

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (258 lines) | stat: -rw-r--r-- 7,418 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
68
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
96
97
98
99
100
101
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
137
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/***************************************************************************
              Curve.cpp  -  curve consisting of points
                             -------------------
    begin                : Jan 20 2001
    copyright            : (C) 2001 by Thomas Eschenbacher
    email                : Thomas Eschenbacher <thomas.eschenbacher@gmx.de>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "config.h"

#include <math.h>

#include <algorithm>
#include <limits>

#include "libkwave/Curve.h"
#include "libkwave/Interpolation.h"
#include "libkwave/Parser.h"
#include "libkwave/String.h"

//***************************************************************************

QPointF Kwave::Curve::NoPoint(std::numeric_limits<qreal>::max(),
                              std::numeric_limits<qreal>::max());

//***************************************************************************
Kwave::Curve::Curve()
    :m_interpolation(Kwave::INTPOL_LINEAR)
{
}

//***************************************************************************
Kwave::Curve::Curve(const QString &command)
    :m_interpolation(Kwave::INTPOL_LINEAR)
{
    fromCommand(command);
}

//***************************************************************************
Kwave::Curve::~Curve()
{
    clear();
}

//***************************************************************************
void Kwave::Curve::fromCommand(const QString &command)
{
    clear();

    Kwave::Parser parse(command);
    QString t = parse.firstParam();
    setInterpolationType(m_interpolation.find(t));

    while (!parse.isDone()) {
        double x = parse.toDouble();
        if (parse.isDone()) break; // half point ?
        double y = parse.toDouble();
        append(Point(x, y));
    }
}

//***************************************************************************
QString Kwave::Curve::getCommand()
{
    QString cmd = _("curve(");
    cmd += m_interpolation.name(m_interpolation.type());

    foreach (const Point &p, *this) {
        QString par = _(",%1,%2");
        cmd += par.arg(p.x()).arg(p.y());
    }
    cmd += _(")");
    return cmd;
}

//***************************************************************************
Kwave::Interpolation &Kwave::Curve::interpolation()
{
    m_interpolation.prepareInterpolation(*this);
    return m_interpolation;
}

//***************************************************************************
QVector<double> Kwave::Curve::interpolation(unsigned int points)
{
    m_interpolation.prepareInterpolation(*this);
    return m_interpolation.interpolation(*this, points);
}

//***************************************************************************
void Kwave::Curve::setInterpolationType(Kwave::interpolation_t type)
{
    m_interpolation.setType(type);
}

//***************************************************************************
Kwave::interpolation_t Kwave::Curve::interpolationType()
{
    return m_interpolation.type();
}

//***************************************************************************
void Kwave::Curve::deletePoint(Point p, bool check)
{
    Iterator it(*this);
    if (!it.findNext(p)) return;
    if ((!check) || (it.hasPrevious() && it.hasNext()))
        it.remove();
}

//***************************************************************************
void Kwave::Curve::secondHalf()
{
    if (isEmpty()) return;

    QMutableListIterator<Point> it(*this);
    while (it.hasNext()) {
        Point &p = it.next();
        p.setX(0.5 + p.x() / 2.0);
    }

    insert(0.0, first().y());
}

//***************************************************************************
void Kwave::Curve::deleteSecondPoint()
{
    if (isEmpty()) return;

    Iterator it(*this);
    while (it.hasNext()) {
        it.next();
        it.remove();
    }
}

//***************************************************************************
void Kwave::Curve::insert(double x, double y)
{
    if ((x < 0.0) || (x > 1.0)) {
        qWarning("Curve::insert(%0.2f,%0.2f): out of range !",x,y);
        return;
    }

    append(Point(x, y));
    sort();
}

//***************************************************************************
void Kwave::Curve::firstHalf()
{
    if (isEmpty()) return;

    QMutableListIterator<Point> it(*this);
    while (it.hasNext()) {
        Point &p = it.next();
        p.setX(p.x() / 2.0);
    }
    append(Point(1.0, first().y()));
}

//****************************************************************************
void Kwave::Curve::VFlip()
{
    if (isEmpty()) return;

    QMutableListIterator<Point> it(*this);
    while (it.hasNext()) {
        Point &p = it.next();
        p.setY(1.0 - p.y());
    }
}

//***************************************************************************
void Kwave::Curve::HFlip()
{
    if (isEmpty()) return;

    // flip all x coordinates
    QMutableListIterator<Point> it(*this);
    while (it.hasNext()) {
        Point &p = it.next();
        p.setX(1.0 - p.x());
    }

    // reverse the order it the list
    sort();
}

//***************************************************************************
void Kwave::Curve::scaleFit(unsigned int range)
{
    double min = std::numeric_limits<double>::max();
    double max = std::numeric_limits<double>::min();

    Kwave::Interpolation interpolation(m_interpolation.type());

    QVector<double> y = interpolation.interpolation(*this, range);
    foreach (double yi, y) {
        if (yi > max) max = yi;
        if (yi < min) min = yi;
    }

    QMutableListIterator<Point> it(*this);
    while (it.hasNext()) {
        Point &p = it.next();
        p.ry() -= min;
        if (!qFuzzyCompare(max, min))
            p.ry() /= (max - min);
        else
            p.ry() = min;
    }

}

//***************************************************************************
Kwave::Curve::Point Kwave::Curve::findPoint(double px, double py, double tol)
{
    Point best = NoPoint;
    double min_dist = tol;

    QMutableListIterator<Point> it(*this);
    while (it.hasNext()) {
        Point &p = it.next();
        // use the length of the difference vector as criterium
        double dist = hypot(px - p.x(), py - p.y());
        if (dist < min_dist) {
            min_dist = dist;
            best = p;
        }
    }
    return best;
}

//***************************************************************************
static bool cmp(const Kwave::Curve::Point &a, const Kwave::Curve::Point &b)
{
    return (a.x() < b.x());
}

//***************************************************************************
void Kwave::Curve::sort()
{
    if (!isEmpty())
        std::sort(begin(), end(), cmp);
}

//***************************************************************************
//***************************************************************************