[3e4acc]: libkwave / Curve.cpp Maximize Restore History

Download this file

Curve.cpp    245 lines (201 with data), 6.4 kB

  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
/***************************************************************************
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 <float.h> // for FLT_MAX, DBL_MIN, DBL_MAX
#include <math.h>
#include <QtAlgorithms>
#include "Parser.h"
#include "Interpolation.h"
#include "Curve.h"
//***************************************************************************
QPointF Curve::NoPoint(FLT_MAX, FLT_MAX);
//***************************************************************************
Curve::Curve()
:m_interpolation(INTPOL_LINEAR)
{
}
//***************************************************************************
Curve::Curve(const QString &command)
:m_interpolation(INTPOL_LINEAR)
{
fromCommand(command);
}
//***************************************************************************
Curve::~Curve()
{
clear();
}
//***************************************************************************
void Curve::fromCommand(const QString &command)
{
clear();
Parser parse(command);
QString t = parse.firstParam();
setInterpolationType(m_interpolation.find(t));
double x, y;
while (!parse.isDone()) {
x = parse.toDouble();
if (parse.isDone()) break; // half point ?
y = parse.toDouble();
append(Point(x, y));
}
}
//***************************************************************************
QString Curve::getCommand()
{
QString cmd = "curve(";
cmd += m_interpolation.name(m_interpolation.type());
foreach (Point p, *this) {
QString par = ",%1,%2";
cmd += par.arg(p.x()).arg(p.y());
}
cmd += ")";
return cmd;
}
//***************************************************************************
Interpolation &Curve::interpolation()
{
m_interpolation.prepareInterpolation(*this);
return m_interpolation;
}
//***************************************************************************
QVector<double> Curve::interpolation(unsigned int points)
{
m_interpolation.prepareInterpolation(*this);
return m_interpolation.interpolation(*this, points);
}
//***************************************************************************
void Curve::setInterpolationType(interpolation_t type)
{
m_interpolation.setType(type);
}
//***************************************************************************
interpolation_t Curve::interpolationType()
{
return m_interpolation.type();
}
//***************************************************************************
void Curve::deletePoint(Point p, bool check)
{
Iterator it(*this);
if (!it.findNext(p)) return;
if ((!check) || (it.hasPrevious() && it.hasNext()))
it.remove();
}
//***************************************************************************
void Curve::secondHalf()
{
if (isEmpty()) return;
foreach (Point p, *this) {
p.setX(0.5 + p.x() / 2.0);
}
insert(0.0, first().y());
}
//***************************************************************************
void Curve::deleteSecondPoint()
{
if (isEmpty()) return;
Iterator it(*this);
while (it.hasNext()) {
it.next();
it.remove();
}
}
//***************************************************************************
void 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 Curve::firstHalf()
{
if (isEmpty()) return;
foreach (Point p, *this) {
p.setX(p.x() / 2.0);
}
append(Point(1.0, first().y()));
}
//****************************************************************************
void Curve::VFlip()
{
if (isEmpty()) return;
foreach (Point p, *this) {
p.setY(1.0 - p.y());
}
}
//***************************************************************************
void Curve::HFlip()
{
if (isEmpty()) return;
// flip all x coordinates
foreach (Point p, *this) {
p.setX(1.0 - p.x());
}
// reverse the order it the list
sort();
}
//***************************************************************************
void Curve::scaleFit(unsigned int range)
{
double min = DBL_MAX;
double max = DBL_MIN;
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;
}
foreach (Point p, *this) {
p.ry() -= min;
if (max != min)
p.ry() /= (max - min);
else
p.ry() = min;
}
}
//***************************************************************************
Curve::Point Curve::findPoint(double px, double py, double tol)
{
Point best = NoPoint;
double dist;
double min_dist = tol;
foreach (Point p, *this) {
// use the length of the difference vector as criterium
dist = hypot(px - p.x(), py - p.y());
if (dist < min_dist) {
min_dist = dist;
best = p;
}
}
return best;
}
//***************************************************************************
static bool compare_x(Curve::Point &a, Curve::Point &b)
{
return (a.x() < b.x());
}
//***************************************************************************
void Curve::sort()
{
qSort(begin(), end(), compare_x);
}
//***************************************************************************
//***************************************************************************