1 /***************************************************************************
2 * Copyright (C) 2010 by Till Theato (root@ttill.de) *
3 * This file is part of Kdenlive (www.kdenlive.org). *
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. *
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. *
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 ***************************************************************************/
19 #include "cubicbezierspline.h"
23 /** @brief For sorting a Bezier spline. Whether a is before b. */
24 static bool pointLessThan(const BPoint &a, const BPoint &b)
26 return a.p.x() < b.p.x();
29 CubicBezierSpline::CubicBezierSpline(QObject* parent) :
41 m_points.append(start);
53 CubicBezierSpline::CubicBezierSpline(const CubicBezierSpline& spline, QObject* parent) :
56 m_precision = spline.m_precision;
57 m_points = spline.m_points;
58 m_validSpline = false;
61 CubicBezierSpline& CubicBezierSpline::operator=(const CubicBezierSpline& spline)
63 m_precision = spline.m_precision;
64 m_points = spline.m_points;
65 m_validSpline = false;
69 void CubicBezierSpline::fromString(const QString& spline)
72 m_validSpline = false;
74 QStringList bpoints = spline.split('|');
75 foreach(const QString &bpoint, bpoints) {
76 QStringList points = bpoint.split('#');
77 QList <QPointF> values;
78 foreach(const QString &point, points) {
79 QStringList xy = point.split(';');
81 values.append(QPointF(xy.at(0).toDouble(), xy.at(1).toDouble()));
83 if (values.count() == 3) {
96 QString CubicBezierSpline::toString() const
99 foreach(const BPoint &p, m_points) {
100 spline << (QString::number(p.h1.x()) + ";" + QString::number(p.h1.y())
101 + "#" + QString::number(p.p.x()) + ";" + QString::number(p.p.y())
102 + "#" + QString::number(p.h2.x()) + ";" + QString::number(p.h2.y()));
104 return spline.join("|");
107 int CubicBezierSpline::setPoint(int ix, const BPoint& point)
109 m_points[ix] = point;
112 m_validSpline = false;
113 return indexOf(point); // in case it changed
116 QList <BPoint> CubicBezierSpline::points()
121 void CubicBezierSpline::removePoint(int ix)
123 m_points.removeAt(ix);
124 m_validSpline = false;
127 int CubicBezierSpline::addPoint(const BPoint& point)
129 m_points.append(point);
132 m_validSpline = false;
133 return indexOf(point);
136 void CubicBezierSpline::setPrecision(int pre)
138 if (pre != m_precision) {
140 m_validSpline = false;
144 int CubicBezierSpline::getPrecision()
149 qreal CubicBezierSpline::value(qreal x, bool cont)
154 m_i = m_spline.constBegin();
155 if (m_i != m_spline.constBegin())
158 double diff = qAbs(x - m_i.key());
159 double y = m_i.value();
160 while (m_i != m_spline.constEnd()) {
161 if (qAbs(x - m_i.key()) > diff)
164 diff = qAbs(x - m_i.key());
168 return qBound((qreal)0.0, y, (qreal)1.0);
171 void CubicBezierSpline::validatePoints()
174 for (int i = 0; i < m_points.count() - 1; ++i) {
176 p2 = m_points.at(i+1);
177 p1.h2.setX(qBound(p1.p.x(), p1.h2.x(), p2.p.x()));
178 p2.h1.setX(qBound(p1.p.x(), p2.h1.x(), p2.p.x()));
184 void CubicBezierSpline::keepSorted()
186 qSort(m_points.begin(), m_points.end(), pointLessThan);
189 QPointF CubicBezierSpline::point(double t, const QList< QPointF >& points)
191 // coefficients from Bernstein basis polynomial of degree 3
192 double c1 = (1-t) * (1-t) * (1-t);
193 double c2 = 3 * t * (1-t) * (1-t);
194 double c3 = 3 * t * t * (1-t);
195 double c4 = t * t * t;
197 return QPointF(points[0].x()*c1 + points[1].x()*c2 + points[2].x()*c3 + points[3].x()*c4,
198 points[0].y()*c1 + points[1].y()*c2 + points[2].y()*c3 + points[3].y()*c4);
201 void CubicBezierSpline::update()
206 m_validSpline = true;
209 QList <QPointF> points;
211 for (int i = 0; i < m_points.count() - 1; ++i) {
213 points << m_points.at(i).p
215 << m_points.at(i+1).h1
216 << m_points.at(i+1).p;
218 int numberOfValues = (int)((points[3].x() - points[0].x()) * m_precision * 5);
219 if (numberOfValues == 0)
221 double step = 1 / (double)numberOfValues;
224 p = point(t, points);
225 m_spline.insert(p.x(), p.y());
231 int CubicBezierSpline::indexOf(const BPoint& p)
233 if (m_points.indexOf(p) == -1) {
234 // point changed during validation process
235 for (int i = 0; i < m_points.count(); ++i) {
236 // this condition should be sufficient, too
237 if (m_points.at(i).p == p.p)
241 return m_points.indexOf(p);
246 #include "cubicbezierspline.moc"