]> git.sesse.net Git - kdenlive/blobdiff - src/beziercurve/cubicbezierspline.cpp
Bezier Spline:
[kdenlive] / src / beziercurve / cubicbezierspline.cpp
index 0671f72ba05bf05780a5b5bbe64a969f07f05e3e..881d20af7e0e6bd8f53ce45daf38a3609114ecde 100644 (file)
@@ -18,8 +18,8 @@
 
 #include "cubicbezierspline.h"
 
-#include <KDebug>
 
+/** @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();
@@ -30,23 +30,8 @@ CubicBezierSpline::CubicBezierSpline(QObject* parent) :
         m_validSpline(false),
         m_precision(100)
 {
-    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) :
@@ -61,6 +46,7 @@ CubicBezierSpline& CubicBezierSpline::operator=(const CubicBezierSpline& spline)
 {
     m_precision = spline.m_precision;
     m_points = spline.m_points;
+    m_validSpline = false;
     return *this;
 }
 
@@ -79,11 +65,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)));
         }
     }
 
@@ -95,9 +77,9 @@ QString CubicBezierSpline::toString() const
 {
     QStringList spline;
     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(p.h1.x()).arg(p.h1.y())
+                                              .arg(p.p.x()).arg(p.p.y())
+                                              .arg(p.h2.x()).arg(p.h2.y());
     }
     return spline.join("|");
 }
@@ -108,7 +90,7 @@ int CubicBezierSpline::setPoint(int ix, const BPoint& point)
     keepSorted();
     validatePoints();
     m_validSpline = false;
-    return m_points.indexOf(point); // in case it changed
+    return indexOf(point); // in case it changed
 }
 
 QList <BPoint> CubicBezierSpline::points()
@@ -128,7 +110,7 @@ int CubicBezierSpline::addPoint(const BPoint& point)
     keepSorted();
     validatePoints();
     m_validSpline = false;
-    return m_points.indexOf(point);
+    return indexOf(point);
 }
 
 void CubicBezierSpline::setPrecision(int pre)
@@ -148,9 +130,6 @@ qreal CubicBezierSpline::value(qreal x, bool cont)
 {
     update();
 
-    //x = qBound(m_spline.constBegin().key(), x, m_spline.constEnd().key());
-    //kDebug() << "....x" << x<<"bounddown"<<m_spline.constBegin().key()<<"up"<<m_spline.constEnd().key();
-
     if (!cont)
         m_i = m_spline.constBegin();
     if (m_i != m_spline.constBegin())
@@ -216,7 +195,7 @@ void CubicBezierSpline::update()
                 << m_points.at(i+1).h1
                 << m_points.at(i+1).p;
 
-        int numberOfValues = (int)((points[3].x() - points[0].x()) * m_precision * 2);
+        int numberOfValues = (int)((points[3].x() - points[0].x()) * m_precision * 5);
         if (numberOfValues == 0)
             numberOfValues = 1;
         double step = 1 / (double)numberOfValues;
@@ -227,13 +206,21 @@ void CubicBezierSpline::update()
             t += step;
         }
     }
-    /*QMap<double, double>::const_iterator i = m_spline.constBegin();
-    kDebug()<<"////////////spline/////////////";
-    while (i != m_spline.constEnd()) {
-        kDebug() << i.key() << i.value();
-        ++i;
+}
+
+int CubicBezierSpline::indexOf(const BPoint& p)
+{
+    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);
     }
-    kDebug()<<"////////////spline/////////////end";*/
+    return -1;
 }
 
 #include "cubicbezierspline.moc"