OpenShot Library | libopenshot  0.3.0
KeyFrame.h
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 #ifndef OPENSHOT_KEYFRAME_H
14 #define OPENSHOT_KEYFRAME_H
15 
16 #include <iostream>
17 #include <vector>
18 
19 #include "Fraction.h"
20 #include "Point.h"
21 #include "Json.h"
22 
23 namespace openshot {
24 
26  bool IsPointBeforeX(Point const & p, double const x);
27 
29  double InterpolateLinearCurve(Point const & left, Point const & right, double const target);
30 
32  double InterpolateBezierCurve(Point const & left, Point const & right, double const target, double const allowed_error);
33 
35  double InterpolateBetween(Point const & left, Point const & right, double target, double allowed_error);
36 
54  class Keyframe {
55 
56 
57  private:
58  std::vector<Point> Points;
59 
60  public:
62  Keyframe() = default;
63 
65  Keyframe(double value);
66 
68  Keyframe(const std::vector<openshot::Point>& points);
69 
71  ~Keyframe();
72 
74  void AddPoint(Point p);
75 
77  void AddPoint(double x, double y, InterpolationType interpolate=BEZIER);
78 
80  bool Contains(Point p) const;
81 
83  void FlipPoints();
84 
86  int64_t FindIndex(Point p) const;
87 
89  double GetValue(int64_t index) const;
90 
92  int GetInt(int64_t index) const;
93 
95  int64_t GetLong(int64_t index) const;
96 
98  Fraction GetRepeatFraction(int64_t index) const;
99 
101  double GetDelta(int64_t index) const;
102 
104  Point const & GetPoint(int64_t index) const;
105 
107  Point GetClosestPoint(Point p) const;
108 
111  Point GetClosestPoint(Point p, bool useLeft) const;
112 
114  Point GetPreviousPoint(Point p) const;
115 
117  Point GetMaxPoint() const;
118 
119  // Get the number of values (i.e. coordinates on the X axis)
120  int64_t GetLength() const;
121 
123  int64_t GetCount() const;
124 
126  bool IsIncreasing(int index) const;
127 
128  // Get and Set JSON methods
129  std::string Json() const;
130  Json::Value JsonValue() const;
131  void SetJson(const std::string value);
132  void SetJsonValue(const Json::Value root);
133 
135  void RemovePoint(Point p);
136 
138  void RemovePoint(int64_t index);
139 
142  void ScalePoints(double scale);
143 
145  void UpdatePoint(int64_t index, Point p);
146 
148  void PrintPoints(std::ostream* out=&std::cout) const;
149 
151  void PrintValues(std::ostream* out=&std::cout) const;
152 
153  };
154 
155 }
156 
157 #endif
double GetDelta(int64_t index) const
Get the change in Y value (from the previous Y value)
Definition: KeyFrame.cpp:482
Header file for Fraction class.
void PrintValues(std::ostream *out=&std::cout) const
Print just the Y value of the point&#39;s primary coordinate.
Definition: KeyFrame.cpp:564
Point GetMaxPoint() const
Get max point (by Y coordinate)
Definition: KeyFrame.cpp:245
void FlipPoints()
Flip all the points in this openshot::Keyframe (useful for reversing an effect or transition...
Definition: KeyFrame.cpp:617
Point const & GetPoint(int64_t index) const
Get a point at a specific index.
Definition: KeyFrame.cpp:490
int64_t GetLong(int64_t index) const
Get the rounded LONG value at a specific index.
Definition: KeyFrame.cpp:287
void PrintPoints(std::ostream *out=&std::cout) const
Print a list of points.
Definition: KeyFrame.cpp:553
Bezier curves are quadratic curves, which create a smooth curve.
Definition: Point.h:29
Header file for Point class.
void ScalePoints(double scale)
Definition: KeyFrame.cpp:603
A Point is the basic building block of a key-frame curve.
Definition: Point.h:64
Point GetPreviousPoint(Point p) const
Get previous point (.
Definition: KeyFrame.cpp:226
bool IsIncreasing(int index) const
Get the direction of the curve at a specific index (increasing or decreasing)
Definition: KeyFrame.cpp:292
bool Contains(Point p) const
Does this keyframe contain a specific point.
Definition: KeyFrame.cpp:184
double InterpolateBezierCurve(Point const &left, Point const &right, double const target, double const allowed_error)
Bezier interpolation between two points.
Definition: KeyFrame.cpp:44
void UpdatePoint(int64_t index, Point p)
Replace an existing point with a new point.
Definition: KeyFrame.cpp:545
int GetInt(int64_t index) const
Get the rounded INT value at a specific index.
Definition: KeyFrame.cpp:282
double InterpolateLinearCurve(Point const &left, Point const &right, double const target)
Linear interpolation between two points.
Definition: KeyFrame.cpp:36
void AddPoint(Point p)
Add a new point on the key-frame. Each point has a primary coordinate, a left handle, and a right handle.
Definition: KeyFrame.cpp:131
Keyframe()=default
Default constructor for the Keyframe class.
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
void RemovePoint(Point p)
Remove a point by matching a coordinate.
Definition: KeyFrame.cpp:513
Header file for JSON class.
This class represents a fraction.
Definition: Fraction.h:30
double InterpolateBetween(Point const &left, Point const &right, double target, double allowed_error)
Interpolate two points using the right Point&#39;s interpolation method.
Definition: KeyFrame.cpp:80
InterpolationType
This controls how a Keyframe uses this point to interpolate between two points.
Definition: Point.h:28
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
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:358
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:325
int64_t FindIndex(Point p) const
Get the index of a point by matching a coordinate.
Definition: KeyFrame.cpp:166
int64_t GetCount() const
Get the number of points (i.e. # of points)
Definition: KeyFrame.cpp:507
double GetValue(int64_t index) const
Get the value at a specific index.
Definition: KeyFrame.cpp:258
~Keyframe()
Destructor.
Definition: KeyFrame.cpp:124
void SetJson(const std::string value)
Load JSON string into this object.
Definition: KeyFrame.cpp:341
Fraction GetRepeatFraction(int64_t index) const
Get the fraction that represents how many times this value is repeated in the curve.
Definition: KeyFrame.cpp:379
A Keyframe is a collection of Point instances, which is used to vary a number or property over time...
Definition: KeyFrame.h:54
int64_t GetLength() const
Definition: KeyFrame.cpp:500
std::string Json() const
Generate JSON string of this object.
Definition: KeyFrame.cpp:318