From 9df3b51082df8b53a0f1d910002cb4bf8d466fc8 Mon Sep 17 00:00:00 2001 From: Brenno Date: Mon, 1 Feb 2021 15:12:18 -0300 Subject: [PATCH] Removed unused class KeyframeBase Moved the functions IsPointBeforeX(), InterpolateLinearCurve() , InterpolateBezierCurve() and InterpolateBetween() from KeyFrameBase files to KeyFrame files --- src/CMakeLists.txt | 1 - src/Clip.cpp | 2 +- src/KeyFrame.cpp | 94 ++++++++++++++++++++++++++++++++------- src/KeyFrame.h | 17 +++++-- src/KeyFrameBase.cpp | 103 ------------------------------------------- src/KeyFrameBase.h | 85 ----------------------------------- 6 files changed, 92 insertions(+), 210 deletions(-) delete mode 100644 src/KeyFrameBase.cpp delete mode 100644 src/KeyFrameBase.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index adb7351a5..56e953707 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -86,7 +86,6 @@ set(OPENSHOT_SOURCES FrameMapper.cpp Json.cpp KeyFrame.cpp - KeyFrameBase.cpp TrackedObjectBase.cpp TrackedObjectBBox.cpp OpenShotVersion.cpp diff --git a/src/Clip.cpp b/src/Clip.cpp index 1ac5ac45d..146219330 100644 --- a/src/Clip.cpp +++ b/src/Clip.cpp @@ -1210,7 +1210,7 @@ void Clip::AddEffect(EffectBase* effect) // Sort effects sort_effects(); - // Add Tracker to Timeline + // Add Tracked Object to Timeline if (effect->info.has_tracked_object){ Timeline* parentTimeline = (Timeline *) ParentTimeline(); diff --git a/src/KeyFrame.cpp b/src/KeyFrame.cpp index 3b594a560..a178f2dad 100644 --- a/src/KeyFrame.cpp +++ b/src/KeyFrame.cpp @@ -31,35 +31,95 @@ #include "KeyFrame.h" #include "Exceptions.h" -#include // For assert() -#include // For std::cout -#include // For std::setprecision #include #include #include +#include // For assert() +#include // For std::cout +#include // For std::setprecision using namespace std; using namespace openshot; -namespace{ - template - int64_t SearchBetweenPoints(Point const & left, Point const & right, int64_t const current, Check check) { - int64_t start = left.co.X; - int64_t stop = right.co.X; - while (start < stop) { - int64_t const mid = (start + stop + 1) / 2; - double const value = InterpolateBetween(left, right, mid, 0.01); - if (check(round(value), current)) { - start = mid; - } else { - stop = mid - 1; +namespace openshot{ + + // Check if the X coordinate of a given Point is lower than a given value + bool IsPointBeforeX(Point const & p, double const x) { + return p.co.X < x; + } + + // Linear interpolation between two points + double InterpolateLinearCurve(Point const & left, Point const & right, double const target) { + double const diff_Y = right.co.Y - left.co.Y; + double const diff_X = right.co.X - left.co.X; + double const slope = diff_Y / diff_X; + return left.co.Y + slope * (target - left.co.X); + } + + // Bezier interpolation between two points + double InterpolateBezierCurve(Point const & left, Point const & right, double const target, double const allowed_error) { + double const X_diff = right.co.X - left.co.X; + double const Y_diff = right.co.Y - left.co.Y; + Coordinate const p0 = left.co; + Coordinate const p1 = Coordinate(p0.X + left.handle_right.X * X_diff, p0.Y + left.handle_right.Y * Y_diff); + Coordinate const p2 = Coordinate(p0.X + right.handle_left.X * X_diff, p0.Y + right.handle_left.Y * Y_diff); + Coordinate const p3 = right.co; + + double t = 0.5; + double t_step = 0.25; + do { + // Bernstein polynoms + double B[4] = {1, 3, 3, 1}; + double oneMinTExp = 1; + double tExp = 1; + for (int i = 0; i < 4; ++i, tExp *= t) { + B[i] *= tExp; + } + for (int i = 0; i < 4; ++i, oneMinTExp *= 1 - t) { + B[4 - i - 1] *= oneMinTExp; + } + double const x = p0.X * B[0] + p1.X * B[1] + p2.X * B[2] + p3.X * B[3]; + double const y = p0.Y * B[0] + p1.Y * B[1] + p2.Y * B[2] + p3.Y * B[3]; + if (fabs(target - x) < allowed_error) { + return y; } + if (x > target) { + t -= t_step; + } + else { + t += t_step; + } + t_step /= 2; + } while (true); + } + + // Interpolate two points using the right Point's interpolation method + double InterpolateBetween(Point const & left, Point const & right, double target, double allowed_error) { + assert(left.co.X < target); + assert(target <= right.co.X); + switch (right.interpolation) { + case CONSTANT: return left.co.Y; + case LINEAR: return InterpolateLinearCurve(left, right, target); + case BEZIER: return InterpolateBezierCurve(left, right, target, allowed_error); } - return start; } } - +template +int64_t SearchBetweenPoints(Point const & left, Point const & right, int64_t const current, Check check) { + int64_t start = left.co.X; + int64_t stop = right.co.X; + while (start < stop) { + int64_t const mid = (start + stop + 1) / 2; + double const value = InterpolateBetween(left, right, mid, 0.01); + if (check(round(value), current)) { + start = mid; + } else { + stop = mid - 1; + } + } + return start; +} // Constructor which sets the default point & coordinate at X=1 Keyframe::Keyframe(double value) { diff --git a/src/KeyFrame.h b/src/KeyFrame.h index 7306fd7bc..14dee7bf0 100644 --- a/src/KeyFrame.h +++ b/src/KeyFrame.h @@ -37,10 +37,21 @@ #include "Fraction.h" #include "Point.h" #include "Json.h" -#include "KeyFrameBase.h" namespace openshot { + /// Check if the X coordinate of a given Point is lower than a given value + bool IsPointBeforeX(Point const & p, double const x); + + /// Linear interpolation between two points + double InterpolateLinearCurve(Point const & left, Point const & right, double const target); + + /// Bezier interpolation between two points + double InterpolateBezierCurve(Point const & left, Point const & right, double const target, double const allowed_error); + + /// Interpolate two points using the right Point's interpolation method + double InterpolateBetween(Point const & left, Point const & right, double target, double allowed_error); + /** * @brief A Keyframe is a collection of Point instances, which is used to vary a number or property over time. * @@ -58,7 +69,7 @@ namespace openshot { * kf.PrintValues(); * \endcode */ - class Keyframe : public KeyframeBase { + class Keyframe { private: @@ -143,7 +154,7 @@ namespace openshot { /// Scale all points by a percentage (good for evenly lengthening or shortening an openshot::Keyframe) /// 1.0 = same size, 1.05 = 5% increase, etc... - void ScalePoints(double scale) override; + void ScalePoints(double scale); /// Replace an existing point with a new point void UpdatePoint(int64_t index, Point p); diff --git a/src/KeyFrameBase.cpp b/src/KeyFrameBase.cpp deleted file mode 100644 index 79f4f5281..000000000 --- a/src/KeyFrameBase.cpp +++ /dev/null @@ -1,103 +0,0 @@ -/** - * @file - * @brief Source file for the KeyframeBase class - * @author Jonathan Thomas - * - * @ref License - */ - -/* LICENSE - * - * Copyright (c) 2008-2019 OpenShot Studios, LLC - * . This file is part of - * OpenShot Library (libopenshot), an open-source project dedicated to - * delivering high quality video editing and animation solutions to the - * world. For more information visit . - * - * OpenShot Library (libopenshot) is free software: you can redistribute it - * and/or modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * OpenShot Library (libopenshot) is distributed in the hope that it will be - * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with OpenShot Library. If not, see . - */ - -#include "KeyFrameBase.h" -#include -#include -#include - - -namespace openshot{ - - // Check if the X coordinate of a given Point is lower than a given value - bool IsPointBeforeX(Point const & p, double const x) { - return p.co.X < x; - } - - // Linear interpolation between two points - double InterpolateLinearCurve(Point const & left, Point const & right, double const target) { - double const diff_Y = right.co.Y - left.co.Y; - double const diff_X = right.co.X - left.co.X; - double const slope = diff_Y / diff_X; - return left.co.Y + slope * (target - left.co.X); - } - - // Bezier interpolation between two points - double InterpolateBezierCurve(Point const & left, Point const & right, double const target, double const allowed_error) { - double const X_diff = right.co.X - left.co.X; - double const Y_diff = right.co.Y - left.co.Y; - Coordinate const p0 = left.co; - Coordinate const p1 = Coordinate(p0.X + left.handle_right.X * X_diff, p0.Y + left.handle_right.Y * Y_diff); - Coordinate const p2 = Coordinate(p0.X + right.handle_left.X * X_diff, p0.Y + right.handle_left.Y * Y_diff); - Coordinate const p3 = right.co; - - double t = 0.5; - double t_step = 0.25; - do { - // Bernstein polynoms - double B[4] = {1, 3, 3, 1}; - double oneMinTExp = 1; - double tExp = 1; - for (int i = 0; i < 4; ++i, tExp *= t) { - B[i] *= tExp; - } - for (int i = 0; i < 4; ++i, oneMinTExp *= 1 - t) { - B[4 - i - 1] *= oneMinTExp; - } - double const x = p0.X * B[0] + p1.X * B[1] + p2.X * B[2] + p3.X * B[3]; - double const y = p0.Y * B[0] + p1.Y * B[1] + p2.Y * B[2] + p3.Y * B[3]; - if (fabs(target - x) < allowed_error) { - return y; - } - if (x > target) { - t -= t_step; - } - else { - t += t_step; - } - t_step /= 2; - } while (true); - } - - // Interpolate two points using the right Point's interpolation method - double InterpolateBetween(Point const & left, Point const & right, double target, double allowed_error) { - assert(left.co.X < target); - assert(target <= right.co.X); - switch (right.interpolation) { - case CONSTANT: return left.co.Y; - case LINEAR: return InterpolateLinearCurve(left, right, target); - case BEZIER: return InterpolateBezierCurve(left, right, target, allowed_error); - } - } - - KeyframeBase::KeyframeBase(){ - - } -} \ No newline at end of file diff --git a/src/KeyFrameBase.h b/src/KeyFrameBase.h deleted file mode 100644 index 14b94c198..000000000 --- a/src/KeyFrameBase.h +++ /dev/null @@ -1,85 +0,0 @@ -/** - * @file - * @brief Header file for the KeyframeBase class - * @author Jonathan Thomas - * - * @ref License - */ - -/* LICENSE - * - * Copyright (c) 2008-2019 OpenShot Studios, LLC - * . This file is part of - * OpenShot Library (libopenshot), an open-source project dedicated to - * delivering high quality video editing and animation solutions to the - * world. For more information visit . - * - * OpenShot Library (libopenshot) is free software: you can redistribute it - * and/or modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * OpenShot Library (libopenshot) is distributed in the hope that it will be - * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with OpenShot Library. If not, see . - */ - -#ifndef OPENSHOT_KEYFRAMEBASE_H -#define OPENSHOT_KEYFRAMEBASE_H - -#include -#include -#include -#include -#include -#include -#include "Exceptions.h" -#include "Fraction.h" -#include "Coordinate.h" -#include "Point.h" -#include "Json.h" - - -namespace openshot { - - /// Check if the X coordinate of a given Point is lower than a given value - bool IsPointBeforeX(Point const & p, double const x); - - /// Linear interpolation between two points - double InterpolateLinearCurve(Point const & left, Point const & right, double const target); - - /// Bezier interpolation between two points - double InterpolateBezierCurve(Point const & left, Point const & right, double const target, double const allowed_error); - - /// Interpolate two points using the right Point's interpolation method - double InterpolateBetween(Point const & left, Point const & right, double target, double allowed_error); - - /** - * @brief This abstract class is the base class of all Keyframes. - * - * A Keyframe is a collection of Point instances, which is used to vary a number or property over time. - * - * Keyframes are used to animate and interpolate values of properties over time. For example, a single property - * can use a Keyframe instead of a constant value. Assume you want to slide an image (from left to right) over - * a video. You can create a Keyframe which will adjust the X value of the image over 100 frames (or however many - * frames the animation needs to last) from the value of 0 to 640. - */ - class KeyframeBase{ - - public: - - /// Blank constructor - KeyframeBase(); - - /// Scale all points by a percentage (good for evenly lengthening or shortening an openshot::Keyframe) - /// 1.0 = same size, 1.05 = 5% increase, etc... - virtual void ScalePoints(double scale) { return; }; - - }; -} // Namespace openshot - -#endif