kwave  18.07.70
Curve.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Curve.cpp - curve consisting of points
3  -------------------
4  begin : Jan 20 2001
5  copyright : (C) 2001 by Thomas Eschenbacher
6  email : Thomas Eschenbacher <thomas.eschenbacher@gmx.de>
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "config.h"
19 
20 #include <math.h>
21 
22 #include <algorithm>
23 #include <limits>
24 
25 #include "libkwave/Curve.h"
26 #include "libkwave/Interpolation.h"
27 #include "libkwave/Parser.h"
28 #include "libkwave/String.h"
29 
30 //***************************************************************************
31 
32 QPointF Kwave::Curve::NoPoint(std::numeric_limits<qreal>::max(),
33  std::numeric_limits<qreal>::max());
34 
35 //***************************************************************************
37  :m_interpolation(Kwave::INTPOL_LINEAR)
38 {
39 }
40 
41 //***************************************************************************
42 Kwave::Curve::Curve(const QString &command)
44 {
45  fromCommand(command);
46 }
47 
48 //***************************************************************************
50 {
51  clear();
52 }
53 
54 //***************************************************************************
55 void Kwave::Curve::fromCommand(const QString &command)
56 {
57  clear();
58 
59  Kwave::Parser parse(command);
60  QString t = parse.firstParam();
62 
63  while (!parse.isDone()) {
64  double x = parse.toDouble();
65  if (parse.isDone()) break; // half point ?
66  double y = parse.toDouble();
67  append(Point(x, y));
68  }
69 }
70 
71 //***************************************************************************
73 {
74  QString cmd = _("curve(");
76 
77  foreach (const Point &p, *this) {
78  QString par = _(",%1,%2");
79  cmd += par.arg(p.x()).arg(p.y());
80  }
81  cmd += _(")");
82  return cmd;
83 }
84 
85 //***************************************************************************
87 {
89  return m_interpolation;
90 }
91 
92 //***************************************************************************
93 QVector<double> Kwave::Curve::interpolation(unsigned int points)
94 {
96  return m_interpolation.interpolation(*this, points);
97 }
98 
99 //***************************************************************************
101 {
102  m_interpolation.setType(type);
103 }
104 
105 //***************************************************************************
107 {
108  return m_interpolation.type();
109 }
110 
111 //***************************************************************************
112 void Kwave::Curve::deletePoint(Point p, bool check)
113 {
114  Iterator it(*this);
115  if (!it.findNext(p)) return;
116  if ((!check) || (it.hasPrevious() && it.hasNext()))
117  it.remove();
118 }
119 
120 //***************************************************************************
122 {
123  if (isEmpty()) return;
124 
125  QMutableListIterator<Point> it(*this);
126  while (it.hasNext()) {
127  Point &p = it.next();
128  p.setX(0.5 + p.x() / 2.0);
129  }
130 
131  insert(0.0, first().y());
132 }
133 
134 //***************************************************************************
136 {
137  if (isEmpty()) return;
138 
139  Iterator it(*this);
140  while (it.hasNext()) {
141  it.next();
142  it.remove();
143  }
144 }
145 
146 //***************************************************************************
147 void Kwave::Curve::insert(double x, double y)
148 {
149  if ((x < 0.0) || (x > 1.0)) {
150  qWarning("Curve::insert(%0.2f,%0.2f): out of range !",x,y);
151  return;
152  }
153 
154  append(Point(x, y));
155  sort();
156 }
157 
158 //***************************************************************************
160 {
161  if (isEmpty()) return;
162 
163  QMutableListIterator<Point> it(*this);
164  while (it.hasNext()) {
165  Point &p = it.next();
166  p.setX(p.x() / 2.0);
167  }
168  append(Point(1.0, first().y()));
169 }
170 
171 //****************************************************************************
173 {
174  if (isEmpty()) return;
175 
176  QMutableListIterator<Point> it(*this);
177  while (it.hasNext()) {
178  Point &p = it.next();
179  p.setY(1.0 - p.y());
180  }
181 }
182 
183 //***************************************************************************
185 {
186  if (isEmpty()) return;
187 
188  // flip all x coordinates
189  QMutableListIterator<Point> it(*this);
190  while (it.hasNext()) {
191  Point &p = it.next();
192  p.setX(1.0 - p.x());
193  }
194 
195  // reverse the order it the list
196  sort();
197 }
198 
199 //***************************************************************************
200 void Kwave::Curve::scaleFit(unsigned int range)
201 {
202  double min = std::numeric_limits<double>::max();
203  double max = std::numeric_limits<double>::min();
204 
206 
207  QVector<double> y = interpolation.interpolation(*this, range);
208  foreach (double yi, y) {
209  if (yi > max) max = yi;
210  if (yi < min) min = yi;
211  }
212 
213  QMutableListIterator<Point> it(*this);
214  while (it.hasNext()) {
215  Point &p = it.next();
216  p.ry() -= min;
217  if (!qFuzzyCompare(max, min))
218  p.ry() /= (max - min);
219  else
220  p.ry() = min;
221  }
222 
223 }
224 
225 //***************************************************************************
226 Kwave::Curve::Point Kwave::Curve::findPoint(double px, double py, double tol)
227 {
228  Point best = NoPoint;
229  double min_dist = tol;
230 
231  QMutableListIterator<Point> it(*this);
232  while (it.hasNext()) {
233  Point &p = it.next();
234  // use the length of the difference vector as criterium
235  double dist = hypot(px - p.x(), py - p.y());
236  if (dist < min_dist) {
237  min_dist = dist;
238  best = p;
239  }
240  }
241  return best;
242 }
243 
244 //***************************************************************************
245 static bool cmp(const Kwave::Curve::Point &a, const Kwave::Curve::Point &b)
246 {
247  return (a.x() < b.x());
248 }
249 
250 //***************************************************************************
252 {
253  if (!isEmpty())
254  std::sort(begin(), end(), cmp);
255 }
256 
257 //***************************************************************************
258 //***************************************************************************
Kwave::interpolation_t type()
void scaleFit(unsigned int range=1024)
Definition: Curve.cpp:200
QPointF Point
Definition: Curve.h:48
Definition: App.h:33
static QPointF NoPoint
Definition: Curve.h:51
interpolation_t
Definition: Interpolation.h:34
QMutableListIterator< QPointF > Iterator
Definition: Curve.h:45
static QString name(Kwave::interpolation_t type)
void insert(double x, double y)
Definition: Curve.cpp:147
void secondHalf()
Definition: Curve.cpp:121
void fromCommand(const QString &command)
Definition: Curve.cpp:55
static bool cmp(const Kwave::Curve::Point &a, const Kwave::Curve::Point &b)
Definition: Curve.cpp:245
Kwave::Interpolation & interpolation()
Definition: Curve.cpp:86
void deleteSecondPoint()
Definition: Curve.cpp:135
bool prepareInterpolation(const Kwave::Curve &points)
Kwave::interpolation_t interpolationType()
Definition: Curve.cpp:106
void setInterpolationType(Kwave::interpolation_t type)
Definition: Curve.cpp:100
void VFlip()
Definition: Curve.cpp:172
QVector< double > interpolation(const Kwave::Curve &points, unsigned int len)
void firstHalf()
Definition: Curve.cpp:159
virtual ~Curve()
Definition: Curve.cpp:49
double toDouble()
Definition: Parser.cpp:262
Kwave::Interpolation m_interpolation
Definition: Curve.h:165
void deletePoint(Point p, bool check)
Definition: Curve.cpp:112
void HFlip()
Definition: Curve.cpp:184
QString getCommand()
Definition: Curve.cpp:72
const QString & firstParam()
Definition: Parser.cpp:168
#define _(m)
Definition: memcpy.c:66
Point findPoint(double x, double y, double tol=.05)
Definition: Curve.cpp:226
bool isDone() const
Definition: Parser.h:70
void sort()
Definition: Curve.cpp:251
static Kwave::interpolation_t find(const QString &name)
Definition: Interpolation.h:83
void setType(Kwave::interpolation_t t)