1 /***************************************************************************
2                   Curve.h  -  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 #ifndef CURVE_H
19 #define CURVE_H
20 
21 #include "config.h"
22 
23 #include <QtGlobal>
24 #include <QList>
25 #include <QListIterator>
26 #include <QMutableListIterator>
27 #include <QObject>
28 #include <QPointF>
29 #include <QString>
30 #include <QVector>
31 #include <QtGlobal>
32 
33 #include "libkwave/Interpolation.h"
34 
35 namespace Kwave
36 {
37     class Q_DECL_EXPORT Curve: public QList<QPointF>
38     {
39     public:
40 
41 	/** Iterator */
42 	typedef QListIterator<QPointF> ConstIterator;
43 
44 	/** Iterator */
45 	typedef QMutableListIterator<QPointF> Iterator;
46 
47 	/** class used for the points */
48 	typedef QPointF Point;
49 
50 	/** used for the "invalid" point */
51 	static QPointF NoPoint;
52 
53 	/**
54 	 * Default constructor, creates an empty curve.
55 	 */
56 	Curve();
57 
58 	/**
59 	 * Constructor, creates a curve from a command string.
60 	 * @param command string with parameters
61 	 */
62 	explicit Curve(const QString &command);
63 
64 	/** Destructor */
65 	virtual ~Curve();
66 
67 	/** Moves all current points into the left half */
68 	void firstHalf();
69 
70 	/** Moves all current points into the right half */
71 	void secondHalf();
72 
73 	/**
74 	 * Removes and deletes a point from the curve. Note that after this
75 	 * call the passed point is no longer valid!
76 	 * @param p point to be deleted
77 	 * @param check if true, the last or first point will not be deleted
78 	 */
79 	void deletePoint(Point p, bool check);
80 
81 	/**
82 	 * Deletes every second point.
83 	 */
84 	void deleteSecondPoint();
85 
86 	/**
87 	 * Flips/mirrors the curve horizontally (x-axis).
88 	 */
89 	void HFlip();
90 
91 	/**
92 	 * Flips/mirrors the curve vertically (y-axis).
93 	 */
94 	void VFlip();
95 
96 	/**
97 	 * Scales the curve vertically to fit into a range of (+/- range/2)
98 	 * on the y-axis.
99 	 * @param range the size range to use for scaling
100 	 */
101 	void scaleFit(unsigned int range = 1024);
102 
103 	/**
104 	 * Creates a new point and inserts it into the curve. The new
105 	 * point will be sorted in by it's x coordinate.
106 	 * @param x coordinate on the x axis, should be [0...+1.0]
107 	 * @param y coordinate on the y axis, should be [0...+1.0]
108 	 */
109 	void insert(double x, double y);
110 
111 	/**
112 	 * Searches for a point at given coordinates with a definable
113 	 * tolerance.
114 	 * @param x coordinate on the x axis
115 	 * @param y coordinate on the y axis
116 	 * @param tol tolerance for x and y direction, absolute value
117 	 * @return pointer to the found point or "NoPoint" if nothing found.
118 	 */
119 	Point findPoint(double x, double y, double tol = .05);
120 
121 	/**
122 	 * Sets a curve from a command string. Opposite of getCommand().
123 	 * @param command a string that contains the interpolation
124 	 *        type and pairs of x/y coordinates.
125 	 */
126 	void fromCommand(const QString &command);
127 
128 	/**
129 	 * Returns a command string out of the curve points and
130 	 * interpolation type.
131 	 */
132 	QString getCommand();
133 
134 	/**
135 	 * Returns the interpolation type.
136 	 */
137 	Kwave::interpolation_t interpolationType();
138 
139 	/**
140 	 * Sets a new interpolation type.
141 	 * @param type the new interpolation type
142 	 */
143 	void setInterpolationType(Kwave::interpolation_t type);
144 
145 	/** Returns a reference to the Interpolation object itself */
146 	Kwave::Interpolation &interpolation();
147 
148 	/**
149 	 * Returns an array of points, calculated out of the
150 	 * current interpolation parameters.
151 	 * @param points number of points
152 	 * @return Array of interpolated values or null if the
153 	 *         number of points was zero or the curve was empty.
154 	 */
155 	QVector<double> interpolation(unsigned int points);
156 
157     protected:
158 
159 	/** sorts the list by ascending x coordinate */
160 	void sort();
161 
162     private:
163 
164 	/** interpolation object */
165 	Kwave::Interpolation m_interpolation;
166 
167     };
168 }
169 
170 #endif /* CURVE_H */
171 
172 //***************************************************************************
173 //***************************************************************************
174