]> git.sesse.net Git - kdenlive/blob - src/beziercurve/cubicbezierspline.h
Bezier spline editor: Fix inaccurate placement of handles
[kdenlive] / src / beziercurve / cubicbezierspline.h
1 /***************************************************************************
2  *   Copyright (C) 2010 by Till Theato (root@ttill.de)                     *
3  *   This file is part of Kdenlive (www.kdenlive.org).                     *
4  *                                                                         *
5  *   Kdenlive is free software: you can redistribute it and/or modify      *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation, either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   Kdenlive is distributed in the hope that it will be useful,           *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with Kdenlive.  If not, see <http://www.gnu.org/licenses/>.     *
17  ***************************************************************************/
18
19 #ifndef CUBICBEZIERSPLINE_H
20 #define CUBICBEZIERSPLINE_H
21
22 #include "bpoint.h"
23
24 #include <QtCore>
25
26
27 class CubicBezierSpline : public QObject
28 {
29     Q_OBJECT
30
31 public:
32     CubicBezierSpline(QObject* parent = 0);
33     CubicBezierSpline(const CubicBezierSpline &spline, QObject* parent = 0);
34     CubicBezierSpline& operator=(const CubicBezierSpline &spline);
35
36     /** @brief Loads the points from the string @param spline.
37      *
38      * x, y values have to be separated with a ';'
39      * handles and points with a '#'
40      * and the nodes with a '|'
41      * So that you get: h1x;h1y#px;py#h2x;h2y|h1x;h1y#... */
42     void fromString(const QString &spline);
43     /** @brief Returns the points stoed in a string.
44      *
45      * x, y values have are separated with a ';'
46      * handles and points with a '#'
47      * and the nodes with a '|'
48      * So that you get: h1x;h1y#px;py#h2x;h2y|h1x;h1y#... */
49     QString toString() const;
50
51     /** @brief Returns a list of the points defining the spline. */
52     QList <BPoint> points();
53     /** @brief Finds the closest point in the pre-calculated spline to @param x.
54      * @param x x value
55      * @param cont (default = false) Whether to start searching at the previously requested x and skip all values before it.
56                                      This makes requesting a lot of increasing x's faster. */
57     qreal value(qreal x, bool cont = false);
58
59     /** @brief Sets the point at index @param ix to @param point and returns its index (it might have changed during validation). */
60     int setPoint(int ix, const BPoint &point);
61     /** @brief Adds @param point and returns its index. */
62     int addPoint(const BPoint &point);
63     /** @brief Removes the point at @param ix. */
64     void removePoint(int ix);
65
66     /** @brief Sets the precision to @param pre.
67      *
68      * The precision influences the number of points that are calculated for the spline:
69      * number of values = precision * 10 */
70     void setPrecision(int pre);
71     int getPrecision();
72
73 private:
74     QPointF point(double t, const QList<QPointF> &points);
75     void validatePoints();
76     void keepSorted();
77     void update();
78     int indexOf(const BPoint &p);
79
80     QList <BPoint> m_points;
81     /** x, y pairs */
82     QMap <double, double> m_spline;
83     /** iterator used when searching for a value in in continue mode (see @function value). */
84     QMap <double, double>::const_iterator m_i;
85     /** Whether the spline represents the points and the precision. */
86     bool m_validSpline;
87     int m_precision;
88 };
89
90 #endif