X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fbeziercurve%2Fcubicbezierspline.cpp;h=13b1b73e082137f6aaed07ac1e761bef95c03653;hb=e74ed82c3ebc6b5b86106632f5dabdc524ac695a;hp=0671f72ba05bf05780a5b5bbe64a969f07f05e3e;hpb=ceec2ca1bcbdeda3820e4fd5451f1042e94b1671;p=kdenlive diff --git a/src/beziercurve/cubicbezierspline.cpp b/src/beziercurve/cubicbezierspline.cpp index 0671f72b..13b1b73e 100644 --- a/src/beziercurve/cubicbezierspline.cpp +++ b/src/beziercurve/cubicbezierspline.cpp @@ -17,49 +17,29 @@ ***************************************************************************/ #include "cubicbezierspline.h" - #include +/** @brief For sorting a Bezier spline. Whether a is before b. */ static bool pointLessThan(const BPoint &a, const BPoint &b) { return a.p.x() < b.p.x(); } CubicBezierSpline::CubicBezierSpline(QObject* parent) : - QObject(parent), - m_validSpline(false), - m_precision(100) + QObject(parent) { - BPoint start; - start.p.setX(0); - start.p.setY(0); - start.h1.setX(0); - start.h1.setY(0); - start.h2.setX(0.1); - start.h2.setY(0.1); - m_points.append(start); - - BPoint end; - end.p.setX(1); - end.p.setY(1); - end.h1.setX(0.9); - end.h1.setY(0.9); - end.h2.setX(1); - end.h2.setY(1); - m_points.append(end); + m_points.append(BPoint(QPointF(0, 0), QPointF(0, 0), QPointF(.1, .1))); + m_points.append(BPoint(QPointF(.9, .9), QPointF(1, 1), QPointF(1, 1))); } CubicBezierSpline::CubicBezierSpline(const CubicBezierSpline& spline, QObject* parent) : QObject(parent) { - m_precision = spline.m_precision; m_points = spline.m_points; - m_validSpline = false; } CubicBezierSpline& CubicBezierSpline::operator=(const CubicBezierSpline& spline) { - m_precision = spline.m_precision; m_points = spline.m_points; return *this; } @@ -67,7 +47,6 @@ CubicBezierSpline& CubicBezierSpline::operator=(const CubicBezierSpline& spline) void CubicBezierSpline::fromString(const QString& spline) { m_points.clear(); - m_validSpline = false; QStringList bpoints = spline.split('|'); foreach(const QString &bpoint, bpoints) { @@ -79,11 +58,7 @@ void CubicBezierSpline::fromString(const QString& spline) values.append(QPointF(xy.at(0).toDouble(), xy.at(1).toDouble())); } if (values.count() == 3) { - BPoint bp; - bp.h1 = values.at(0); - bp.p = values.at(1); - bp.h2 = values.at(2); - m_points.append(bp); + m_points.append(BPoint(values.at(0), values.at(1), values.at(2))); } } @@ -94,10 +69,11 @@ void CubicBezierSpline::fromString(const QString& spline) QString CubicBezierSpline::toString() const { QStringList spline; + QLocale locale; foreach(const BPoint &p, m_points) { - spline << (QString::number(p.h1.x()) + ";" + QString::number(p.h1.y()) - + "#" + QString::number(p.p.x()) + ";" + QString::number(p.p.y()) - + "#" + QString::number(p.h2.x()) + ";" + QString::number(p.h2.y())); + spline << QString("%1;%2#%3;%4#%5;%6").arg(locale.toString(p.h1.x())).arg(locale.toString(p.h1.y())) + .arg(locale.toString(p.p.x())).arg(locale.toString(p.p.y())) + .arg(locale.toString(p.h2.x())).arg(locale.toString(p.h2.y())); } return spline.join("|"); } @@ -107,8 +83,7 @@ int CubicBezierSpline::setPoint(int ix, const BPoint& point) m_points[ix] = point; keepSorted(); validatePoints(); - m_validSpline = false; - return m_points.indexOf(point); // in case it changed + return indexOf(point); // in case it changed } QList CubicBezierSpline::points() @@ -119,7 +94,6 @@ QList CubicBezierSpline::points() void CubicBezierSpline::removePoint(int ix) { m_points.removeAt(ix); - m_validSpline = false; } int CubicBezierSpline::addPoint(const BPoint& point) @@ -127,46 +101,19 @@ int CubicBezierSpline::addPoint(const BPoint& point) m_points.append(point); keepSorted(); validatePoints(); - m_validSpline = false; - return m_points.indexOf(point); -} - -void CubicBezierSpline::setPrecision(int pre) -{ - if (pre != m_precision) { - m_precision = pre; - m_validSpline = false; - } + return indexOf(point); } -int CubicBezierSpline::getPrecision() +BPoint CubicBezierSpline::getPoint(int ix, int normalisedWidth, int normalisedHeight, bool invertHeight) { - return m_precision; -} - -qreal CubicBezierSpline::value(qreal x, bool cont) -{ - update(); - - //x = qBound(m_spline.constBegin().key(), x, m_spline.constEnd().key()); - //kDebug() << "....x" << x<<"bounddown"< diff) - break; - - diff = qAbs(x - m_i.key()); - y = m_i.value(); - ++m_i; + BPoint p = m_points.at(ix); + for (int i = 0; i < 3; ++i) { + p[i].rx() *= normalisedWidth; + p[i].ry() *= normalisedHeight; + if (invertHeight) + p[i].ry() = normalisedHeight - p[i].y(); } - return qBound((qreal)0.0, y, (qreal)1.0); + return p; } void CubicBezierSpline::validatePoints() @@ -187,53 +134,19 @@ void CubicBezierSpline::keepSorted() qSort(m_points.begin(), m_points.end(), pointLessThan); } -QPointF CubicBezierSpline::point(double t, const QList< QPointF >& points) -{ - // coefficients from Bernstein basis polynomial of degree 3 - double c1 = (1-t) * (1-t) * (1-t); - double c2 = 3 * t * (1-t) * (1-t); - double c3 = 3 * t * t * (1-t); - double c4 = t * t * t; - - return QPointF(points[0].x()*c1 + points[1].x()*c2 + points[2].x()*c3 + points[3].x()*c4, - points[0].y()*c1 + points[1].y()*c2 + points[2].y()*c3 + points[3].y()*c4); -} - -void CubicBezierSpline::update() +int CubicBezierSpline::indexOf(const BPoint& p) { - if (m_validSpline) - return; - - m_validSpline = true; - m_spline.clear(); - - QList points; - QPointF p; - for (int i = 0; i < m_points.count() - 1; ++i) { - points.clear(); - points << m_points.at(i).p - << m_points.at(i).h2 - << m_points.at(i+1).h1 - << m_points.at(i+1).p; - - int numberOfValues = (int)((points[3].x() - points[0].x()) * m_precision * 2); - if (numberOfValues == 0) - numberOfValues = 1; - double step = 1 / (double)numberOfValues; - double t = 0; - while (t <= 1) { - p = point(t, points); - m_spline.insert(p.x(), p.y()); - t += step; + if (m_points.indexOf(p) == -1) { + // point changed during validation process + for (int i = 0; i < m_points.count(); ++i) { + // this condition should be sufficient, too + if (m_points.at(i).p == p.p) + return i; } + } else { + return m_points.indexOf(p); } - /*QMap::const_iterator i = m_spline.constBegin(); - kDebug()<<"////////////spline/////////////"; - while (i != m_spline.constEnd()) { - kDebug() << i.key() << i.value(); - ++i; - } - kDebug()<<"////////////spline/////////////end";*/ + return -1; } #include "cubicbezierspline.moc"