+29
-24
@@ -28,7 +28,10 @@ namespace olive {
|
||||
|
||||
double Bezier::QuadraticXtoT(double x, double a, double b, double c)
|
||||
{
|
||||
return (a - b + qSqrt(a*x + c*x - 2*b*x + qPow(b, 2) - a*c))/(a - 2*b + c);
|
||||
// Clamp to prevent infinite loop
|
||||
x = clamp(x, a, c);
|
||||
|
||||
return CalculateTFromX(false, x, a, b, c, 0);
|
||||
}
|
||||
|
||||
double Bezier::QuadraticTtoY(double a, double b, double c, double t)
|
||||
@@ -36,31 +39,12 @@ double Bezier::QuadraticTtoY(double a, double b, double c, double t)
|
||||
return qPow(1.0 - t, 2)*a + 2*(1.0 - t)*t*b + qPow(t, 2)*c;
|
||||
}
|
||||
|
||||
double Bezier::CubicXtoT(double x_target, double a, double b, double c, double d)
|
||||
double Bezier::CubicXtoT(double x, double a, double b, double c, double d)
|
||||
{
|
||||
const double tolerance = 0.0001;
|
||||
// Clamp to prevent infinite loop
|
||||
x = clamp(x, a, d);
|
||||
|
||||
// Clamp to prevent deadlocks
|
||||
x_target = clamp(x_target, a, d);
|
||||
|
||||
double lower = 0.0;
|
||||
double upper = 1.0;
|
||||
|
||||
double percent = 0.5;
|
||||
double x = CubicTtoY(a, b, c, d, percent);
|
||||
|
||||
while (qAbs(x_target - x) > tolerance) {
|
||||
if (x_target > x) {
|
||||
lower = percent;
|
||||
} else {
|
||||
upper = percent;
|
||||
}
|
||||
|
||||
percent = (upper + lower) * 0.5;
|
||||
x = CubicTtoY(a, b, c, d, percent);
|
||||
}
|
||||
|
||||
return percent;
|
||||
return CalculateTFromX(true, x, a, b, c, d);
|
||||
}
|
||||
|
||||
double Bezier::CubicTtoY(double a, double b, double c, double d, double t)
|
||||
@@ -68,4 +52,25 @@ double Bezier::CubicTtoY(double a, double b, double c, double d, double t)
|
||||
return qPow(1.0 - t, 3)*a + 3*qPow(1.0 - t, 2)*t*b + 3*(1.0 - t)*qPow(t, 2)*c + qPow(t, 3)*d;
|
||||
}
|
||||
|
||||
double Bezier::CalculateTFromX(bool cubic, double x, double a, double b, double c, double d)
|
||||
{
|
||||
double bottom = 0.0;
|
||||
double top = 1.0;
|
||||
|
||||
while (true) {
|
||||
double mid = (bottom + top) * 0.5;
|
||||
double test = cubic ? CubicTtoY(a, b, c, d, mid) : QuadraticTtoY(a, b, c, mid);
|
||||
|
||||
if (qFuzzyCompare(test, x)) {
|
||||
return mid;
|
||||
} else if (x > test) {
|
||||
bottom = mid;
|
||||
} else {
|
||||
top = mid;
|
||||
}
|
||||
}
|
||||
|
||||
return qSNaN();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+17
-1
@@ -21,6 +21,8 @@
|
||||
#ifndef BEZIER_H
|
||||
#define BEZIER_H
|
||||
|
||||
#include <QPointF>
|
||||
|
||||
#include "common/define.h"
|
||||
|
||||
namespace olive {
|
||||
@@ -32,9 +34,23 @@ public:
|
||||
|
||||
static double QuadraticTtoY(double a, double b, double c, double t);
|
||||
|
||||
static double CubicXtoT(double x_target, double a, double b, double c, double d);
|
||||
static double QuadraticXtoY(double x, const QPointF &a, const QPointF &b, const QPointF &c)
|
||||
{
|
||||
return QuadraticTtoY(a.y(), b.y(), c.y(), QuadraticXtoT(x, a.x(), b.x(), c.x()));
|
||||
}
|
||||
|
||||
static double CubicXtoT(double x, double a, double b, double c, double d);
|
||||
|
||||
static double CubicTtoY(double a, double b, double c, double d, double t);
|
||||
|
||||
static double CubicXtoY(double x, const QPointF &a, const QPointF &b, const QPointF &c, const QPointF &d)
|
||||
{
|
||||
return CubicTtoY(a.y(), b.y(), c.y(), d.y(), CubicXtoT(x, a.x(), b.x(), c.x(), d.x()));
|
||||
}
|
||||
|
||||
private:
|
||||
static double CalculateTFromX(bool cubic, double x, double a, double b, double c, double d);
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
+15
-27
@@ -607,46 +607,34 @@ QVariant Node::GetSplitValueAtTimeOnTrack(const QString &input, const rational &
|
||||
}
|
||||
|
||||
if (before->type() == NodeKeyframe::kBezier && after->type() == NodeKeyframe::kBezier) {
|
||||
|
||||
// Perform a cubic bezier with two control points
|
||||
|
||||
double t = Bezier::CubicXtoT(time.toDouble(),
|
||||
before->time().toDouble(),
|
||||
before->time().toDouble() + before->valid_bezier_control_out().x(),
|
||||
after->time().toDouble() + after->valid_bezier_control_in().x(),
|
||||
after->time().toDouble());
|
||||
|
||||
double y = Bezier::CubicTtoY(before_val,
|
||||
before_val + before->valid_bezier_control_out().y(),
|
||||
after_val + after->valid_bezier_control_in().y(),
|
||||
after_val,
|
||||
t);
|
||||
|
||||
interpolated = y;
|
||||
interpolated = Bezier::CubicXtoY(time.toDouble(),
|
||||
QPointF(before->time().toDouble(), before_val),
|
||||
QPointF(before->time().toDouble() + before->valid_bezier_control_out().x(), before_val + before->valid_bezier_control_out().y()),
|
||||
QPointF(after->time().toDouble() + after->valid_bezier_control_in().x(), after_val + after->valid_bezier_control_in().y()),
|
||||
QPointF(after->time().toDouble(), after_val));
|
||||
|
||||
} else if (before->type() == NodeKeyframe::kBezier || after->type() == NodeKeyframe::kBezier) {
|
||||
// Perform a quadratic bezier with only one control point
|
||||
|
||||
QPointF control_point;
|
||||
double control_point_time;
|
||||
double control_point_value;
|
||||
|
||||
if (before->type() == NodeKeyframe::kBezier) {
|
||||
control_point = before->valid_bezier_control_out();
|
||||
control_point_time = before->time().toDouble() + control_point.x();
|
||||
control_point_value = before_val + control_point.y();
|
||||
control_point.setX(control_point.x() + before->time().toDouble());
|
||||
control_point.setY(control_point.y() + before_val);
|
||||
} else {
|
||||
control_point = after->valid_bezier_control_in();
|
||||
control_point_time = after->time().toDouble() + control_point.x();
|
||||
control_point_value = after_val + control_point.y();
|
||||
control_point.setX(control_point.x() + after->time().toDouble());
|
||||
control_point.setY(control_point.y() + after_val);
|
||||
}
|
||||
|
||||
// Generate T from time values - used to determine bezier progress
|
||||
double t = Bezier::QuadraticXtoT(time.toDouble(), before->time().toDouble(), control_point_time, after->time().toDouble());
|
||||
|
||||
// Generate value using T
|
||||
double y = Bezier::QuadraticTtoY(before_val, control_point_value, after_val, t);
|
||||
|
||||
interpolated = y;
|
||||
// Interpolate value using quadratic beziers
|
||||
interpolated = Bezier::QuadraticXtoY(time.toDouble(),
|
||||
QPointF(before->time().toDouble(), before_val),
|
||||
control_point,
|
||||
QPointF(after->time().toDouble(), after_val));
|
||||
|
||||
} else {
|
||||
// To have arrived here, the keyframes must both be linear
|
||||
|
||||
Reference in New Issue
Block a user