From 90db687598b34306dcc4f87aac07154b6ea3d470 Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Tue, 15 Dec 2015 18:12:24 -0600 Subject: [PATCH] Fixed bug in RemovePoint, and improved the AddPoint KeyFrame method to remove a previous, duplicate point (based on co.X value). --- src/KeyFrame.cpp | 8 +++++++- tests/KeyFrame_Tests.cpp | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/KeyFrame.cpp b/src/KeyFrame.cpp index ab58096e..2c2e9161 100644 --- a/src/KeyFrame.cpp +++ b/src/KeyFrame.cpp @@ -73,6 +73,12 @@ void Keyframe::AddPoint(Point p) { // mark as dirty needs_update = true; + // Check for duplicate point (and remove it) + Point closest = GetClosestPoint(p); + if (closest.co.X == p.co.X) + // Remove existing point + RemovePoint(closest); + // Add point at correct spot Points.push_back(p); @@ -464,7 +470,7 @@ void Keyframe::RemovePoint(Point p) throw(OutOfBoundsPoint) { if (p.co.X == existing_point.co.X && p.co.Y == existing_point.co.Y) { // Remove the matching point, and break out of loop Points.erase(Points.begin() + x); - break; + return; } } diff --git a/tests/KeyFrame_Tests.cpp b/tests/KeyFrame_Tests.cpp index 33ba2bdd..4d970465 100644 --- a/tests/KeyFrame_Tests.cpp +++ b/tests/KeyFrame_Tests.cpp @@ -321,3 +321,16 @@ TEST(Keyframe_Flip_Keyframe) CHECK_CLOSE(2.0f, kf.GetValue(50), 0.01); CHECK_CLOSE(10.0f, kf.GetValue(100), 0.01); } + +TEST(Keyframe_Remove_Duplicate_Point) +{ + // Create a keyframe curve with 2 points + Keyframe kf; + kf.AddPoint(1, 0.0); + kf.AddPoint(1, 1.0); + kf.AddPoint(1, 2.0); + + // Spot check values from the curve + CHECK_EQUAL(kf.GetLength(), 1); + CHECK_CLOSE(kf.GetPoint(0).co.Y, 2.0, 0.01); +} \ No newline at end of file