OpenShot Library | libopenshot 0.3.2
KeyFrame.cpp
Go to the documentation of this file.
1
9// Copyright (c) 2008-2019 OpenShot Studios, LLC
10//
11// SPDX-License-Identifier: LGPL-3.0-or-later
12
13#include "KeyFrame.h"
14#include "Exceptions.h"
15
16#include <algorithm> // For std::lower_bound, std::move_backward
17#include <functional> // For std::less, std::less_equal, etc…
18#include <utility> // For std::swap
19#include <numeric> // For std::accumulate
20#include <cassert> // For assert()
21#include <cmath> // For fabs, round
22#include <iostream> // For std::cout
23#include <iomanip> // For std::setprecision
24
25using namespace std;
26using namespace openshot;
27
28namespace openshot{
29
30 // Check if the X coordinate of a given Point is lower than a given value
31 bool IsPointBeforeX(Point const & p, double const x) {
32 return p.co.X < x;
33 }
34
35 // Linear interpolation between two points
36 double InterpolateLinearCurve(Point const & left, Point const & right, double const target) {
37 double const diff_Y = right.co.Y - left.co.Y;
38 double const diff_X = right.co.X - left.co.X;
39 double const slope = diff_Y / diff_X;
40 return left.co.Y + slope * (target - left.co.X);
41 }
42
43 // Bezier interpolation between two points
44 double InterpolateBezierCurve(Point const & left, Point const & right, double const target, double const allowed_error) {
45 double const X_diff = right.co.X - left.co.X;
46 double const Y_diff = right.co.Y - left.co.Y;
47 Coordinate const p0 = left.co;
48 Coordinate const p1 = Coordinate(p0.X + left.handle_right.X * X_diff, p0.Y + left.handle_right.Y * Y_diff);
49 Coordinate const p2 = Coordinate(p0.X + right.handle_left.X * X_diff, p0.Y + right.handle_left.Y * Y_diff);
50 Coordinate const p3 = right.co;
51
52 double t = 0.5;
53 double t_step = 0.25;
54 do {
55 // Bernstein polynoms
56 double B[4] = {1, 3, 3, 1};
57 double oneMinTExp = 1;
58 double tExp = 1;
59 for (int i = 0; i < 4; ++i, tExp *= t) {
60 B[i] *= tExp;
61 }
62 for (int i = 0; i < 4; ++i, oneMinTExp *= 1 - t) {
63 B[4 - i - 1] *= oneMinTExp;
64 }
65 double const x = p0.X * B[0] + p1.X * B[1] + p2.X * B[2] + p3.X * B[3];
66 double const y = p0.Y * B[0] + p1.Y * B[1] + p2.Y * B[2] + p3.Y * B[3];
67 if (fabs(target - x) < allowed_error) {
68 return y;
69 }
70 if (x > target) {
71 t -= t_step;
72 }
73 else {
74 t += t_step;
75 }
76 t_step /= 2;
77 } while (true);
78 }
79 // Interpolate two points using the right Point's interpolation method
80 double InterpolateBetween(Point const & left, Point const & right, double target, double allowed_error) {
81 // check if target is outside of the extremities poits
82 // This can occur when moving fast the play head
83 if(left.co.X > target){
84 return left.co.Y;
85 }
86 if(target > right.co.X){
87 return right.co.Y;
88 }
89 switch (right.interpolation) {
90 case CONSTANT: return left.co.Y;
91 case LINEAR: return InterpolateLinearCurve(left, right, target);
92 case BEZIER: return InterpolateBezierCurve(left, right, target, allowed_error);
93 default: return InterpolateLinearCurve(left, right, target);
94 }
95 }
96}
97
98template<typename Check>
99int64_t SearchBetweenPoints(Point const & left, Point const & right, int64_t const current, Check check) {
100 int64_t start = left.co.X;
101 int64_t stop = right.co.X;
102 while (start < stop) {
103 int64_t const mid = (start + stop + 1) / 2;
104 double const value = InterpolateBetween(left, right, mid, 0.01);
105 if (check(round(value), current)) {
106 start = mid;
107 } else {
108 stop = mid - 1;
109 }
110 }
111 return start;
112}
113
114// Constructor which sets the default point & coordinate at X=1
115Keyframe::Keyframe(double value) {
116 // Add initial point
117 AddPoint(Point(1, value));
118}
119
120// Constructor which takes a vector of Points
121Keyframe::Keyframe(const std::vector<openshot::Point>& points) : Points(points) {};
122
123// Destructor
125 Points.clear();
126 Points.shrink_to_fit();
127}
128
129// Add a new point on the key-frame. Each point has a primary coordinate,
130// a left handle, and a right handle.
132 // candidate is not less (greater or equal) than the new point in
133 // the X coordinate.
134 std::vector<Point>::iterator candidate =
135 std::lower_bound(begin(Points), end(Points), p.co.X, IsPointBeforeX);
136 if (candidate == end(Points)) {
137 // New point X is greater than all other points' X, add to
138 // back.
139 Points.push_back(p);
140 } else if ((*candidate).co.X == p.co.X) {
141 // New point is at same X coordinate as some point, overwrite
142 // point.
143 *candidate = p;
144 } else {
145 // New point needs to be inserted before candidate; thus move
146 // candidate and all following one to the right and insert new
147 // point then where candidate was.
148 size_t const candidate_index = candidate - begin(Points);
149 Points.push_back(p); // Make space; could also be a dummy point. INVALIDATES candidate!
150 std::move_backward(begin(Points) + candidate_index, end(Points) - 1, end(Points));
151 Points[candidate_index] = p;
152 }
153}
154
155// Add a new point on the key-frame, interpolate is optional (default: BEZIER)
156void Keyframe::AddPoint(double x, double y, InterpolationType interpolate)
157{
158 // Create a point
159 Point new_point(x, y, interpolate);
160
161 // Add the point
162 AddPoint(new_point);
163}
164
165// Get the index of a point by matching a coordinate
166int64_t Keyframe::FindIndex(Point p) const {
167 // loop through points, and find a matching coordinate
168 for (std::vector<Point>::size_type x = 0; x < Points.size(); x++) {
169 // Get each point
170 Point existing_point = Points[x];
171
172 // find a match
173 if (p.co.X == existing_point.co.X && p.co.Y == existing_point.co.Y) {
174 // Remove the matching point, and break out of loop
175 return x;
176 }
177 }
178
179 // no matching point found
180 throw OutOfBoundsPoint("Invalid point requested", -1, Points.size());
181}
182
183// Determine if point already exists
185 std::vector<Point>::const_iterator i =
186 std::lower_bound(begin(Points), end(Points), p.co.X, IsPointBeforeX);
187 return i != end(Points) && i->co.X == p.co.X;
188}
189
190// Get current point (or closest point) from the X coordinate (i.e. the frame number)
191Point Keyframe::GetClosestPoint(Point p, bool useLeft) const {
192 if (Points.size() == 0) {
193 return Point(-1, -1);
194 }
195
196 // Finds a point with an X coordinate which is "not less" (greater
197 // or equal) than the queried X coordinate.
198 std::vector<Point>::const_iterator candidate =
199 std::lower_bound(begin(Points), end(Points), p.co.X, IsPointBeforeX);
200
201 if (candidate == end(Points)) {
202 // All points are before the queried point.
203 //
204 // Note: Behavior the same regardless of useLeft!
205 return Points.back();
206 }
207 if (candidate == begin(Points)) {
208 // First point is greater or equal to the queried point.
209 //
210 // Note: Behavior the same regardless of useLeft!
211 return Points.front();
212 }
213 if (useLeft) {
214 return *(candidate - 1);
215 } else {
216 return *candidate;
217 }
218}
219
220// Get current point (or closest point to the right) from the X coordinate (i.e. the frame number)
222 return GetClosestPoint(p, false);
223}
224
225// Get previous point (if any)
227
228 // Lookup the index of this point
229 try {
230 int64_t index = FindIndex(p);
231
232 // If not the 1st point
233 if (index > 0)
234 return Points[index - 1];
235 else
236 return Points[0];
237
238 } catch (const OutOfBoundsPoint& e) {
239 // No previous point
240 return Point(-1, -1);
241 }
242}
243
244// Get max point (by Y coordinate)
246 Point maxPoint(-1, -1);
247
248 for (Point const & existing_point: Points) {
249 if (existing_point.co.Y >= maxPoint.co.Y) {
250 maxPoint = existing_point;
251 }
252 }
253
254 return maxPoint;
255}
256
257// Get the value at a specific index
258double Keyframe::GetValue(int64_t index) const {
259 if (Points.empty()) {
260 return 0;
261 }
262 std::vector<Point>::const_iterator candidate =
263 std::lower_bound(begin(Points), end(Points), static_cast<double>(index), IsPointBeforeX);
264
265 if (candidate == end(Points)) {
266 // index is behind last point
267 return Points.back().co.Y;
268 }
269 if (candidate == begin(Points)) {
270 // index is at or before first point
271 return Points.front().co.Y;
272 }
273 if (candidate->co.X == index) {
274 // index is directly on a point
275 return candidate->co.Y;
276 }
277 std::vector<Point>::const_iterator predecessor = candidate - 1;
278 return InterpolateBetween(*predecessor, *candidate, index, 0.01);
279}
280
281// Get the rounded INT value at a specific index
282int Keyframe::GetInt(int64_t index) const {
283 return int(round(GetValue(index)));
284}
285
286// Get the rounded INT value at a specific index
287int64_t Keyframe::GetLong(int64_t index) const {
288 return long(round(GetValue(index)));
289}
290
291// Get the direction of the curve at a specific index (increasing or decreasing)
292bool Keyframe::IsIncreasing(int index) const
293{
294 if (index <= 1) {
295 // Determine direction of frame 1 (and assume previous frames have same direction)
296 index = 1;
297 } else if (index >= GetLength()) {
298 // Determine direction of last valid frame # (and assume next frames have same direction)
299 index = GetLength() - 1;
300 }
301
302 // Get current index value
303 const double current_value = GetValue(index);
304
305 // Iterate from current index to next significant value change
306 int attempts = 1;
307 while (attempts < 600 && index + attempts <= GetLength()) {
308 // Get next value
309 const double next_value = GetValue(index + attempts);
310
311 // Is value significantly different
312 const double diff = next_value - current_value;
313 if (fabs(diff) > 0.0001) {
314 if (diff < 0.0) {
315 // Decreasing value found next
316 return false;
317 } else {
318 // Increasing value found next
319 return true;
320 }
321 }
322
323 // increment attempt
324 attempts++;
325 }
326
327 // If no next value found, assume increasing values
328 return true;
329}
330
331// Generate JSON string of this object
332std::string Keyframe::Json() const {
333
334 // Return formatted string
335 return JsonValue().toStyledString();
336}
337
338// Generate Json::Value for this object
339Json::Value Keyframe::JsonValue() const {
340
341 // Create root json object
342 Json::Value root;
343 root["Points"] = Json::Value(Json::arrayValue);
344
345 // loop through points
346 for (const auto existing_point : Points) {
347 root["Points"].append(existing_point.JsonValue());
348 }
349
350 // return JsonValue
351 return root;
352}
353
354// Load JSON string into this object
355void Keyframe::SetJson(const std::string value) {
356
357 // Parse JSON string into JSON objects
358 try
359 {
360 const Json::Value root = openshot::stringToJson(value);
361 // Set all values that match
362 SetJsonValue(root);
363 }
364 catch (const std::exception& e)
365 {
366 // Error parsing JSON (or missing keys)
367 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
368 }
369}
370
371// Load Json::Value into this object
372void Keyframe::SetJsonValue(const Json::Value root) {
373 // Clear existing points
374 Points.clear();
375 Points.shrink_to_fit();
376
377 if (!root["Points"].isNull())
378 // loop through points
379 for (const auto existing_point : root["Points"]) {
380 // Create Point
381 Point p;
382
383 // Load Json into Point
384 p.SetJsonValue(existing_point);
385
386 // Add Point to Keyframe
387 AddPoint(p);
388 }
389}
390
391// Get the change in Y value (from the previous Y value)
392double Keyframe::GetDelta(int64_t index) const {
393 if (index < 1) return 0.0;
394 if (index == 1 && !Points.empty()) return Points[0].co.Y;
395 if (index >= GetLength()) return 0.0;
396 return GetValue(index) - GetValue(index - 1);
397}
398
399// Get a point at a specific index
400Point const & Keyframe::GetPoint(int64_t index) const {
401 // Is index a valid point?
402 if (index >= 0 && index < (int64_t)Points.size())
403 return Points[index];
404 else
405 // Invalid index
406 throw OutOfBoundsPoint("Invalid point requested", index, Points.size());
407}
408
409// Get the number of values (i.e. coordinates on the X axis)
410int64_t Keyframe::GetLength() const {
411 if (Points.empty()) return 0;
412 if (Points.size() == 1) return 1;
413 return round(Points.back().co.X);
414}
415
416// Get the number of points (i.e. # of points)
417int64_t Keyframe::GetCount() const {
418
419 return Points.size();
420}
421
422// Remove a point by matching a coordinate
424 // loop through points, and find a matching coordinate
425 for (std::vector<Point>::size_type x = 0; x < Points.size(); x++) {
426 // Get each point
427 Point existing_point = Points[x];
428
429 // find a match
430 if (p.co.X == existing_point.co.X && p.co.Y == existing_point.co.Y) {
431 // Remove the matching point, and break out of loop
432 Points.erase(Points.begin() + x);
433 return;
434 }
435 }
436
437 // no matching point found
438 throw OutOfBoundsPoint("Invalid point requested", -1, Points.size());
439}
440
441// Remove a point by index
442void Keyframe::RemovePoint(int64_t index) {
443 // Is index a valid point?
444 if (index >= 0 && index < (int64_t)Points.size())
445 {
446 // Remove a specific point by index
447 Points.erase(Points.begin() + index);
448 }
449 else
450 // Invalid index
451 throw OutOfBoundsPoint("Invalid point requested", index, Points.size());
452}
453
454// Replace an existing point with a new point
455void Keyframe::UpdatePoint(int64_t index, Point p) {
456 // Remove matching point
457 RemovePoint(index);
458
459 // Add new point
460 AddPoint(p);
461}
462
463void Keyframe::PrintPoints(std::ostream* out) const {
464 *out << std::right << std::setprecision(4) << std::setfill(' ');
465 for (const auto& p : Points) {
466 *out << std::defaultfloat
467 << std::setw(6) << p.co.X
468 << std::setw(14) << std::fixed << p.co.Y
469 << '\n';
470 }
471 *out << std::flush;
472}
473
474void Keyframe::PrintValues(std::ostream* out) const {
475 // Column widths
476 std::vector<int> w{10, 12, 8, 11, 19};
477
478 *out << std::right << std::setfill(' ') << std::setprecision(4);
479 // Headings
480 *out << "│"
481 << std::setw(w[0]) << "Frame# (X)" << " │"
482 << std::setw(w[1]) << "Y Value" << " │"
483 << std::setw(w[2]) << "Delta Y" << " │ "
484 << std::setw(w[3]) << "Increasing?" << std::right
485 << "│\n";
486 // Divider
487 *out << "├───────────"
488 << "┼─────────────"
489 << "┼─────────"
490 << "┼────────────┤\n";
491
492 for (int64_t i = 1; i <= GetLength(); ++i) {
493 *out << "│"
494 << std::setw(w[0]-2) << std::defaultfloat << i
495 << (Contains(Point(i, 1)) ? " *" : " ") << " │"
496 << std::setw(w[1]) << std::fixed << GetValue(i) << " │"
497 << std::setw(w[2]) << std::defaultfloat << std::showpos
498 << GetDelta(i) << " │ " << std::noshowpos
499 << std::setw(w[3])
500 << (IsIncreasing(i) ? "true" : "false") << std::right << "│\n";
501 }
502 *out << " * = Keyframe point (non-interpolated)\n";
503 *out << std::flush;
504}
505
506
507// Scale all points by a percentage (good for evenly lengthening or shortening an openshot::Keyframe)
508// 1.0 = same size, 1.05 = 5% increase, etc...
509void Keyframe::ScalePoints(double scale)
510{
511 // TODO: What if scale is small so that two points land on the
512 // same X coordinate?
513 // TODO: What if scale < 0?
514
515 // Loop through each point (skipping the 1st point)
516 for (std::vector<Point>::size_type point_index = 1; point_index < Points.size(); point_index++) {
517 // Scale X value
518 Points[point_index].co.X = round(Points[point_index].co.X * scale);
519 }
520}
521
522// Flip all the points in this openshot::Keyframe (useful for reversing an effect or transition, etc...)
524 for (std::vector<Point>::size_type point_index = 0, reverse_index = Points.size() - 1; point_index < reverse_index; point_index++, reverse_index--) {
525 // Flip the points
526 using std::swap;
527 swap(Points[point_index].co.Y, Points[reverse_index].co.Y);
528 // TODO: check that this has the desired effect even with
529 // regards to handles!
530 }
531}
Header file for all Exception classes.
int64_t SearchBetweenPoints(Point const &left, Point const &right, int64_t const current, Check check)
Definition: KeyFrame.cpp:99
Header file for the Keyframe class.
A Cartesian coordinate (X, Y) used in the Keyframe animation system.
Definition: Coordinate.h:38
double X
The X value of the coordinate (usually representing the frame #)
Definition: Coordinate.h:40
double Y
The Y value of the coordinate (usually representing the value of the property being animated)
Definition: Coordinate.h:41
Exception for invalid JSON.
Definition: Exceptions.h:218
int64_t FindIndex(Point p) const
Get the index of a point by matching a coordinate.
Definition: KeyFrame.cpp:166
void RemovePoint(Point p)
Remove a point by matching a coordinate.
Definition: KeyFrame.cpp:423
void SetJson(const std::string value)
Load JSON string into this object.
Definition: KeyFrame.cpp:355
bool Contains(Point p) const
Does this keyframe contain a specific point.
Definition: KeyFrame.cpp:184
~Keyframe()
Destructor.
Definition: KeyFrame.cpp:124
void PrintValues(std::ostream *out=&std::cout) const
Print just the Y value of the point's primary coordinate.
Definition: KeyFrame.cpp:474
Point GetMaxPoint() const
Get max point (by Y coordinate)
Definition: KeyFrame.cpp:245
int GetInt(int64_t index) const
Get the rounded INT value at a specific index.
Definition: KeyFrame.cpp:282
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:372
void AddPoint(Point p)
Add a new point on the key-frame. Each point has a primary coordinate, a left handle,...
Definition: KeyFrame.cpp:131
double GetDelta(int64_t index) const
Get the change in Y value (from the previous Y value)
Definition: KeyFrame.cpp:392
Point const & GetPoint(int64_t index) const
Get a point at a specific index.
Definition: KeyFrame.cpp:400
int64_t GetLength() const
Definition: KeyFrame.cpp:410
void PrintPoints(std::ostream *out=&std::cout) const
Print a list of points.
Definition: KeyFrame.cpp:463
Point GetPreviousPoint(Point p) const
Get previous point (.
Definition: KeyFrame.cpp:226
int64_t GetLong(int64_t index) const
Get the rounded LONG value at a specific index.
Definition: KeyFrame.cpp:287
void UpdatePoint(int64_t index, Point p)
Replace an existing point with a new point.
Definition: KeyFrame.cpp:455
void ScalePoints(double scale)
Definition: KeyFrame.cpp:509
std::string Json() const
Generate JSON string of this object.
Definition: KeyFrame.cpp:332
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:258
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:339
void FlipPoints()
Flip all the points in this openshot::Keyframe (useful for reversing an effect or transition,...
Definition: KeyFrame.cpp:523
Keyframe()=default
Default constructor for the Keyframe class.
bool IsIncreasing(int index) const
Get the direction of the curve at a specific index (increasing or decreasing)
Definition: KeyFrame.cpp:292
int64_t GetCount() const
Get the number of points (i.e. # of points)
Definition: KeyFrame.cpp:417
Point GetClosestPoint(Point p) const
Get current point (or closest point to the right) from the X coordinate (i.e. the frame number)
Definition: KeyFrame.cpp:221
Exception for an out of bounds key-frame point.
Definition: Exceptions.h:325
A Point is the basic building block of a key-frame curve.
Definition: Point.h:64
Coordinate handle_left
This is the left handle coordinate (in percentages from 0 to 1)
Definition: Point.h:67
Coordinate co
This is the primary coordinate.
Definition: Point.h:66
InterpolationType interpolation
This is the interpolation mode.
Definition: Point.h:69
Coordinate handle_right
This is the right handle coordinate (in percentages from 0 to 1)
Definition: Point.h:68
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Point.cpp:104
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:29
double InterpolateLinearCurve(Point const &left, Point const &right, double const target)
Linear interpolation between two points.
Definition: KeyFrame.cpp:36
double InterpolateBezierCurve(Point const &left, Point const &right, double const target, double const allowed_error)
Bezier interpolation between two points.
Definition: KeyFrame.cpp:44
bool IsPointBeforeX(Point const &p, double const x)
Check if the X coordinate of a given Point is lower than a given value.
Definition: KeyFrame.cpp:31
double InterpolateBetween(Point const &left, Point const &right, double target, double allowed_error)
Interpolate two points using the right Point's interpolation method.
Definition: KeyFrame.cpp:80
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
InterpolationType
This controls how a Keyframe uses this point to interpolate between two points.
Definition: Point.h:28
@ CONSTANT
Constant curves jump from their previous position to a new one (with no interpolation).
Definition: Point.h:31
@ BEZIER
Bezier curves are quadratic curves, which create a smooth curve.
Definition: Point.h:29
@ LINEAR
Linear curves are angular, straight lines between two points.
Definition: Point.h:30