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 
25 using namespace std;
26 using namespace openshot;
27 
28 namespace 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 
98 template<typename Check>
99 int64_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
115 Keyframe::Keyframe(double value) {
116  // Add initial point
117  AddPoint(Point(1, value));
118 }
119 
120 // Constructor which takes a vector of Points
121 Keyframe::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)
156 void 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
166 int64_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
184 bool Keyframe::Contains(Point p) const {
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)
191 Point 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
258 double 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
282 int Keyframe::GetInt(int64_t index) const {
283  return int(round(GetValue(index)));
284 }
285 
286 // Get the rounded INT value at a specific index
287 int64_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)
292 bool 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
332 std::string Keyframe::Json() const {
333 
334  // Return formatted string
335  return JsonValue().toStyledString();
336 }
337 
338 // Generate Json::Value for this object
339 Json::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
355 void 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
372 void Keyframe::SetJsonValue(const Json::Value root) {
373  // Clear existing points
374  Points.clear();
375  Points.shrink_to_fit();
376 
377  if (root.isObject() && !root["Points"].isNull()) {
378  // loop through points in JSON Object
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  } else if (root.isNumeric()) {
390  // Create Point from Numeric value
391  Point p(root.asFloat());
392 
393  // Add Point to Keyframe
394  AddPoint(p);
395  }
396 }
397 
398 // Get the change in Y value (from the previous Y value)
399 double Keyframe::GetDelta(int64_t index) const {
400  if (index < 1) return 0.0;
401  if (index == 1 && !Points.empty()) return Points[0].co.Y;
402  if (index >= GetLength()) return 0.0;
403  return GetValue(index) - GetValue(index - 1);
404 }
405 
406 // Get a point at a specific index
407 Point const & Keyframe::GetPoint(int64_t index) const {
408  // Is index a valid point?
409  if (index >= 0 && index < (int64_t)Points.size())
410  return Points[index];
411  else
412  // Invalid index
413  throw OutOfBoundsPoint("Invalid point requested", index, Points.size());
414 }
415 
416 // Get the number of values (i.e. coordinates on the X axis)
417 int64_t Keyframe::GetLength() const {
418  if (Points.empty()) return 0;
419  if (Points.size() == 1) return 1;
420  return round(Points.back().co.X);
421 }
422 
423 // Get the number of points (i.e. # of points)
424 int64_t Keyframe::GetCount() const {
425 
426  return Points.size();
427 }
428 
429 // Remove a point by matching a coordinate
431  // loop through points, and find a matching coordinate
432  for (std::vector<Point>::size_type x = 0; x < Points.size(); x++) {
433  // Get each point
434  Point existing_point = Points[x];
435 
436  // find a match
437  if (p.co.X == existing_point.co.X && p.co.Y == existing_point.co.Y) {
438  // Remove the matching point, and break out of loop
439  Points.erase(Points.begin() + x);
440  return;
441  }
442  }
443 
444  // no matching point found
445  throw OutOfBoundsPoint("Invalid point requested", -1, Points.size());
446 }
447 
448 // Remove a point by index
449 void Keyframe::RemovePoint(int64_t index) {
450  // Is index a valid point?
451  if (index >= 0 && index < (int64_t)Points.size())
452  {
453  // Remove a specific point by index
454  Points.erase(Points.begin() + index);
455  }
456  else
457  // Invalid index
458  throw OutOfBoundsPoint("Invalid point requested", index, Points.size());
459 }
460 
461 // Replace an existing point with a new point
462 void Keyframe::UpdatePoint(int64_t index, Point p) {
463  // Remove matching point
464  RemovePoint(index);
465 
466  // Add new point
467  AddPoint(p);
468 }
469 
470 void Keyframe::PrintPoints(std::ostream* out) const {
471  *out << std::right << std::setprecision(4) << std::setfill(' ');
472  for (const auto& p : Points) {
473  *out << std::defaultfloat
474  << std::setw(6) << p.co.X
475  << std::setw(14) << std::fixed << p.co.Y
476  << '\n';
477  }
478  *out << std::flush;
479 }
480 
481 void Keyframe::PrintValues(std::ostream* out) const {
482  // Column widths
483  std::vector<int> w{10, 12, 8, 11, 19};
484 
485  *out << std::right << std::setfill(' ') << std::setprecision(4);
486  // Headings
487  *out << "│"
488  << std::setw(w[0]) << "Frame# (X)" << " │"
489  << std::setw(w[1]) << "Y Value" << " │"
490  << std::setw(w[2]) << "Delta Y" << " │ "
491  << std::setw(w[3]) << "Increasing?" << std::right
492  << "│\n";
493  // Divider
494  *out << "├───────────"
495  << "┼─────────────"
496  << "┼─────────"
497  << "┼────────────┤\n";
498 
499  for (int64_t i = 1; i <= GetLength(); ++i) {
500  *out << "│"
501  << std::setw(w[0]-2) << std::defaultfloat << i
502  << (Contains(Point(i, 1)) ? " *" : " ") << " │"
503  << std::setw(w[1]) << std::fixed << GetValue(i) << " │"
504  << std::setw(w[2]) << std::defaultfloat << std::showpos
505  << GetDelta(i) << " │ " << std::noshowpos
506  << std::setw(w[3])
507  << (IsIncreasing(i) ? "true" : "false") << std::right << "│\n";
508  }
509  *out << " * = Keyframe point (non-interpolated)\n";
510  *out << std::flush;
511 }
512 
513 
514 // Scale all points by a percentage (good for evenly lengthening or shortening an openshot::Keyframe)
515 // 1.0 = same size, 1.05 = 5% increase, etc...
516 void Keyframe::ScalePoints(double scale)
517 {
518  // TODO: What if scale is small so that two points land on the
519  // same X coordinate?
520  // TODO: What if scale < 0?
521 
522  // Loop through each point (skipping the 1st point)
523  for (std::vector<Point>::size_type point_index = 1; point_index < Points.size(); point_index++) {
524  // Scale X value
525  Points[point_index].co.X = round(Points[point_index].co.X * scale);
526  }
527 }
528 
529 // Flip all the points in this openshot::Keyframe (useful for reversing an effect or transition, etc...)
531  for (std::vector<Point>::size_type point_index = 0, reverse_index = Points.size() - 1; point_index < reverse_index; point_index++, reverse_index--) {
532  // Flip the points
533  using std::swap;
534  swap(Points[point_index].co.Y, Points[reverse_index].co.Y);
535  // TODO: check that this has the desired effect even with
536  // regards to handles!
537  }
538 }
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
openshot::Keyframe::IsIncreasing
bool IsIncreasing(int index) const
Get the direction of the curve at a specific index (increasing or decreasing)
Definition: KeyFrame.cpp:292
openshot::Coordinate::Y
double Y
The Y value of the coordinate (usually representing the value of the property being animated)
Definition: Coordinate.h:41
openshot::Keyframe::GetLong
int64_t GetLong(int64_t index) const
Get the rounded LONG value at a specific index.
Definition: KeyFrame.cpp:287
SearchBetweenPoints
int64_t SearchBetweenPoints(Point const &left, Point const &right, int64_t const current, Check check)
Definition: KeyFrame.cpp:99
openshot::InterpolateLinearCurve
double InterpolateLinearCurve(Point const &left, Point const &right, double const target)
Linear interpolation between two points.
Definition: KeyFrame.cpp:36
openshot::Keyframe::FindIndex
int64_t FindIndex(Point p) const
Get the index of a point by matching a coordinate.
Definition: KeyFrame.cpp:166
openshot::Keyframe::RemovePoint
void RemovePoint(Point p)
Remove a point by matching a coordinate.
Definition: KeyFrame.cpp:430
openshot::Point::interpolation
InterpolationType interpolation
This is the interpolation mode.
Definition: Point.h:69
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::Point::co
Coordinate co
This is the primary coordinate.
Definition: Point.h:66
openshot::Keyframe::Json
std::string Json() const
Generate JSON string of this object.
Definition: KeyFrame.cpp:332
openshot::Keyframe::GetDelta
double GetDelta(int64_t index) const
Get the change in Y value (from the previous Y value)
Definition: KeyFrame.cpp:399
openshot::Keyframe::GetMaxPoint
Point GetMaxPoint() const
Get max point (by Y coordinate)
Definition: KeyFrame.cpp:245
openshot::Keyframe::~Keyframe
~Keyframe()
Destructor.
Definition: KeyFrame.cpp:124
openshot::IsPointBeforeX
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
openshot::Keyframe::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:372
openshot::Keyframe::Contains
bool Contains(Point p) const
Does this keyframe contain a specific point.
Definition: KeyFrame.cpp:184
KeyFrame.h
Header file for the Keyframe class.
openshot::Keyframe::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:339
openshot::Keyframe::AddPoint
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
openshot::OutOfBoundsPoint
Exception for an out of bounds key-frame point.
Definition: Exceptions.h:324
openshot::InterpolateBezierCurve
double InterpolateBezierCurve(Point const &left, Point const &right, double const target, double const allowed_error)
Bezier interpolation between two points.
Definition: KeyFrame.cpp:44
openshot::InterpolateBetween
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
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:217
openshot::Keyframe::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition: KeyFrame.cpp:355
openshot::Point::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Point.cpp:104
openshot::Keyframe::PrintPoints
void PrintPoints(std::ostream *out=&std::cout) const
Print a list of points.
Definition: KeyFrame.cpp:470
openshot::Keyframe::PrintValues
void PrintValues(std::ostream *out=&std::cout) const
Print just the Y value of the point's primary coordinate.
Definition: KeyFrame.cpp:481
openshot::Keyframe::UpdatePoint
void UpdatePoint(int64_t index, Point p)
Replace an existing point with a new point.
Definition: KeyFrame.cpp:462
openshot::Keyframe::GetPoint
const Point & GetPoint(int64_t index) const
Get a point at a specific index.
Definition: KeyFrame.cpp:407
openshot::LINEAR
@ LINEAR
Linear curves are angular, straight lines between two points.
Definition: Point.h:30
openshot::Keyframe::GetClosestPoint
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
openshot::Keyframe::GetLength
int64_t GetLength() const
Definition: KeyFrame.cpp:417
openshot::Keyframe::GetInt
int GetInt(int64_t index) const
Get the rounded INT value at a specific index.
Definition: KeyFrame.cpp:282
openshot::Keyframe::GetCount
int64_t GetCount() const
Get the number of points (i.e. # of points)
Definition: KeyFrame.cpp:424
openshot::Point::handle_left
Coordinate handle_left
This is the left handle coordinate (in percentages from 0 to 1)
Definition: Point.h:67
openshot::CONSTANT
@ CONSTANT
Constant curves jump from their previous position to a new one (with no interpolation).
Definition: Point.h:31
openshot::Keyframe::ScalePoints
void ScalePoints(double scale)
Definition: KeyFrame.cpp:516
openshot::Keyframe::GetPreviousPoint
Point GetPreviousPoint(Point p) const
Get previous point (.
Definition: KeyFrame.cpp:226
openshot::InterpolationType
InterpolationType
This controls how a Keyframe uses this point to interpolate between two points.
Definition: Point.h:28
openshot::Point::handle_right
Coordinate handle_right
This is the right handle coordinate (in percentages from 0 to 1)
Definition: Point.h:68
openshot::Keyframe::FlipPoints
void FlipPoints()
Flip all the points in this openshot::Keyframe (useful for reversing an effect or transition,...
Definition: KeyFrame.cpp:530
openshot::BEZIER
@ BEZIER
Bezier curves are quadratic curves, which create a smooth curve.
Definition: Point.h:29
openshot::Coordinate
A Cartesian coordinate (X, Y) used in the Keyframe animation system.
Definition: Coordinate.h:38
openshot::Point
A Point is the basic building block of a key-frame curve.
Definition: Point.h:64
openshot::Coordinate::X
double X
The X value of the coordinate (usually representing the frame #)
Definition: Coordinate.h:40
Exceptions.h
Header file for all Exception classes.
openshot::Keyframe::GetValue
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:258