From 3740ca045450e31377d5f217ae0407e8b79c167d Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Fri, 19 Oct 2012 22:11:22 -0500 Subject: [PATCH] Added some new methods on the keyframe class, and added some unit tests for the keyframes. --- include/KeyFrame.h | 7 +++++++ src/Clip.cpp | 17 +--------------- src/KeyFrame.cpp | 44 ++++++++++++++++++++++++++++++++++++++++ tests/KeyFrame_Tests.cpp | 30 +++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 16 deletions(-) diff --git a/include/KeyFrame.h b/include/KeyFrame.h index ec1a274c..2d4bbee0 100644 --- a/include/KeyFrame.h +++ b/include/KeyFrame.h @@ -11,6 +11,7 @@ #include #include #include "Exceptions.h" +#include "Fraction.h" #include "Coordinate.h" #include "Point.h" @@ -95,6 +96,12 @@ namespace openshot { /// Get the rounded INT value at a specific index int GetInt(int index); + /// Get the direction of the curve at a specific index (increasing or decreasing) + bool IsIncreasing(int index); + + /// Get the fraction that represents how many times this value is repeated in the curve + Fraction GetRepeatFraction(int index); + /// Get a point at a specific index Point& GetPoint(int index) throw(OutOfBoundsPoint); diff --git a/src/Clip.cpp b/src/Clip.cpp index c56b7b8d..32c4b8b1 100644 --- a/src/Clip.cpp +++ b/src/Clip.cpp @@ -208,27 +208,12 @@ void Clip::apply_time_mapped_frame(tr1::shared_ptr frame, int frame_numbe resampler = new AudioResampler(); // Get new frame number - int new_frame_number = time.GetValue(frame_number); + int new_frame_number = time.GetInt(frame_number); // Get previous and next values (if any) int previous_value = time.GetValue(frame_number - 1); int next_value = time.GetValue(frame_number + 1); - // walk the curve (for 100 X coordinates), and try and detect direction - bool reverse = false; - for (int index = frame_number; index < frame_number + 100; index++) - { - if (time.GetValue(index) > new_frame_number) - { - // reverse time - reverse = true; - break; - } - else if (time.GetValue(index) < new_frame_number) - // forward time - break; - } - // difference between previous frame and current frame int previous_diff = abs(new_frame_number - previous_value); diff --git a/src/KeyFrame.cpp b/src/KeyFrame.cpp index 040209ad..da7d5426 100644 --- a/src/KeyFrame.cpp +++ b/src/KeyFrame.cpp @@ -190,6 +190,50 @@ int Keyframe::GetInt(int index) return 0; } +// Get the direction of the curve at a specific index (increasing or decreasing) +bool Keyframe::IsIncreasing(int index) +{ + // Check if it needs to be processed + if (needs_update) + Process(); + + // Is index a valid point? + if (index >= 0 && index < Values.size()) + // Return value + return int(round(Values[index].increasing)); + else if (index < 0 && Values.size() > 0) + // Return the minimum value + return int(round(Values[0].increasing)); + else if (index >= Values.size() && Values.size() > 0) + // return the maximum value + return int(round(Values[Values.size() - 1].increasing)); + else + // return a blank coordinate (0,0) + return true; +} + +// Get the fraction that represents how many times this value is repeated in the curve +Fraction Keyframe::GetRepeatFraction(int index) +{ + // Check if it needs to be processed + if (needs_update) + Process(); + + // Is index a valid point? + if (index >= 0 && index < Values.size()) + // Return value + return Values[index].repeated; + else if (index < 0 && Values.size() > 0) + // Return the minimum value + return Values[0].repeated; + else if (index >= Values.size() && Values.size() > 0) + // return the maximum value + return Values[Values.size() - 1].repeated; + else + // return a blank coordinate (0,0) + return Fraction(1,1); +} + // Get a point at a specific index Point& Keyframe::GetPoint(int index) throw(OutOfBoundsPoint) { // Is index a valid point? diff --git a/tests/KeyFrame_Tests.cpp b/tests/KeyFrame_Tests.cpp index 63deaaea..3a751490 100644 --- a/tests/KeyFrame_Tests.cpp +++ b/tests/KeyFrame_Tests.cpp @@ -162,3 +162,33 @@ TEST(Keyframe_GetValue_For_Constant_Curve_3_Points) // Check the expected number of values CHECK_EQUAL(kf.Values.size(), 51); } + +TEST(Keyframe_Check_Direction_and_Repeat_Fractions) +{ + // Create a keyframe curve with 2 points + Keyframe kf; + kf.AddPoint(1, 500); + kf.AddPoint(400, 100); + kf.AddPoint(500, 500); + + // Spot check values from the curve + CHECK_EQUAL(kf.GetInt(1), 500); + CHECK_EQUAL(kf.IsIncreasing(1), false); + CHECK_EQUAL(kf.GetRepeatFraction(1).num, 1); + CHECK_EQUAL(kf.GetRepeatFraction(1).den, 10); + + CHECK_EQUAL(kf.GetInt(24), 497); + CHECK_EQUAL(kf.IsIncreasing(24), false); + CHECK_EQUAL(kf.GetRepeatFraction(24).num, 2); + CHECK_EQUAL(kf.GetRepeatFraction(24).den, 4); + + CHECK_EQUAL(kf.GetInt(390), 101); + CHECK_EQUAL(kf.IsIncreasing(390), false); + CHECK_EQUAL(kf.GetRepeatFraction(390).num, 8); + CHECK_EQUAL(kf.GetRepeatFraction(390).den, 8); + + CHECK_EQUAL(kf.GetInt(391), 100); + CHECK_EQUAL(kf.IsIncreasing(391), true); + CHECK_EQUAL(kf.GetRepeatFraction(391).num, 1); + CHECK_EQUAL(kf.GetRepeatFraction(391).den, 12); +}