]> git.sesse.net Git - kdenlive/blob - src/beziercurve/cubicbezierspline.cpp
175aed213494107ef0414af18a36cf1ad8613201
[kdenlive] / src / beziercurve / cubicbezierspline.cpp
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 #include "cubicbezierspline.h"
20
21
22 /** @brief For sorting a Bezier spline. Whether a is before b. */
23 static bool pointLessThan(const BPoint &a, const BPoint &b)
24 {
25     return a.p.x() < b.p.x();
26 }
27
28 CubicBezierSpline::CubicBezierSpline(QObject* parent) :
29         QObject(parent)
30 {
31     m_points.append(BPoint(QPointF(0, 0), QPointF(0, 0), QPointF(.1, .1)));
32     m_points.append(BPoint(QPointF(.9, .9), QPointF(1, 1), QPointF(1, 1)));
33 }
34
35 CubicBezierSpline::CubicBezierSpline(const CubicBezierSpline& spline, QObject* parent) :
36         QObject(parent)
37 {
38     m_points = spline.m_points;
39 }
40
41 CubicBezierSpline& CubicBezierSpline::operator=(const CubicBezierSpline& spline)
42 {
43     m_points = spline.m_points;
44     return *this;
45 }
46
47 void CubicBezierSpline::fromString(const QString& spline)
48 {
49     m_points.clear();
50
51     QStringList bpoints = spline.split('|');
52     foreach(const QString &bpoint, bpoints) {
53         QStringList points = bpoint.split('#');
54         QList <QPointF> values;
55         foreach(const QString &point, points) {
56             QStringList xy = point.split(';');
57             if (xy.count() == 2)
58                 values.append(QPointF(xy.at(0).toDouble(), xy.at(1).toDouble()));
59         }
60         if (values.count() == 3) {
61             m_points.append(BPoint(values.at(0), values.at(1), values.at(2)));
62         }
63     }
64
65     keepSorted();
66     validatePoints();
67 }
68
69 QString CubicBezierSpline::toString() const
70 {
71     QStringList spline;
72     foreach(const BPoint &p, m_points) {
73         spline << QString("%1;%2#%3;%4#%5;%6").arg(p.h1.x()).arg(p.h1.y())
74                                               .arg(p.p.x()).arg(p.p.y())
75                                               .arg(p.h2.x()).arg(p.h2.y());
76     }
77     return spline.join("|");
78 }
79
80 int CubicBezierSpline::setPoint(int ix, const BPoint& point)
81 {
82     m_points[ix] = point;
83     keepSorted();
84     validatePoints();
85     return indexOf(point); // in case it changed
86 }
87
88 QList <BPoint> CubicBezierSpline::points()
89 {
90     return m_points;
91 }
92
93 void CubicBezierSpline::removePoint(int ix)
94 {
95     m_points.removeAt(ix);
96 }
97
98 int CubicBezierSpline::addPoint(const BPoint& point)
99 {
100     m_points.append(point);
101     keepSorted();
102     validatePoints();
103     return indexOf(point);
104 }
105
106 BPoint CubicBezierSpline::getPoint(int ix, int normalisedWidth, int normalisedHeight, bool invertHeight)
107 {
108     BPoint p = m_points.at(ix);
109     for (int i = 0; i < 3; ++i) {
110         p[i].rx() *= normalisedWidth;
111         p[i].ry() *= normalisedHeight;
112         if (invertHeight)
113             p[i].ry() = normalisedHeight - p[i].y();
114     }
115     return p;
116 }
117
118 void CubicBezierSpline::validatePoints()
119 {
120     BPoint p1, p2;
121     for (int i = 0; i < m_points.count() - 1; ++i) {
122         p1 = m_points.at(i);
123         p2 = m_points.at(i+1);
124         p1.h2.setX(qBound(p1.p.x(), p1.h2.x(), p2.p.x()));
125         p2.h1.setX(qBound(p1.p.x(), p2.h1.x(), p2.p.x()));
126         m_points[i] = p1;
127         m_points[i+1] = p2;
128     }
129 }
130
131 void CubicBezierSpline::keepSorted()
132 {
133     qSort(m_points.begin(), m_points.end(), pointLessThan);
134 }
135
136 int CubicBezierSpline::indexOf(const BPoint& p)
137 {
138     if (m_points.indexOf(p) == -1) {
139         // point changed during validation process
140         for (int i = 0; i < m_points.count(); ++i) {
141             // this condition should be sufficient, too
142             if (m_points.at(i).p == p.p)
143                 return i;
144         }
145     } else {
146         return m_points.indexOf(p);
147     }
148     return -1;
149 }
150
151 #include "cubicbezierspline.moc"