OpenShot Library | libopenshot  0.2.7
Point.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for Point class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC
12  * <http://www.openshotstudios.com/>. This file is part of
13  * OpenShot Library (libopenshot), an open-source project dedicated to
14  * delivering high quality video editing and animation solutions to the
15  * world. For more information visit <http://www.openshot.org/>.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #include "Point.h"
32 #include "Exceptions.h"
33 
34 using namespace std;
35 using namespace openshot;
36 
37 // Default constructor
38 Point::Point() : Point::Point(Coordinate(1, 0), BEZIER, AUTO) {};
39 
40 // Constructor which creates a single coordinate at X=1
41 Point::Point(float y) : Point::Point(Coordinate(1, y), CONSTANT, AUTO) {};
42 
43 // Constructor which creates a Bezier curve with point at (x, y)
44 Point::Point(float x, float y) : Point::Point(Coordinate(x, y), BEZIER, AUTO) {};
45 
46 // Constructor which also creates a Point, setting X,Y, and interpolation.
48  : Point::Point(Coordinate(x, y), interpolation, AUTO) {};
49 
50 
51 // Direct Coordinate-accepting constructors
53 
55  : Point::Point(co, interpolation, AUTO) {};
56 
58  co(co), interpolation(interpolation), handle_type(handle_type) {
59  // set handles
61 }
62 
64  // initialize left and right handles (in percentages from 0 to 1)
65  // default to a smooth curve
66  Initialize_LeftHandle(0.5, 1.0);
67  Initialize_RightHandle(0.5, 0.0);
68 }
69 
70 void Point::Initialize_LeftHandle(float x, float y) {
71  // initialize left handle (in percentages from 0 to 1)
72  handle_left = Coordinate(x, y);
73 }
74 
75 void Point::Initialize_RightHandle(float x, float y) {
76  // initialize right handle (in percentages from 0 to 1)
77  handle_right = Coordinate(x, y);
78 }
79 
80 // Generate JSON string of this object
81 std::string Point::Json() const {
82 
83  // Return formatted string
84  return JsonValue().toStyledString();
85 }
86 
87 // Generate Json::Value for this object
88 Json::Value Point::JsonValue() const {
89 
90  // Create root json object
91  Json::Value root;
92  root["co"] = co.JsonValue();
93  if (interpolation == BEZIER) {
94  root["handle_left"] = handle_left.JsonValue();
95  root["handle_right"] = handle_right.JsonValue();
96  root["handle_type"] = handle_type;
97  }
98  root["interpolation"] = interpolation;
99 
100  // return JsonValue
101  return root;
102 }
103 
104 // Load JSON string into this object
105 void Point::SetJson(const std::string value) {
106 
107  // Parse JSON string into JSON objects
108  try
109  {
110  const Json::Value root = openshot::stringToJson(value);
111  // Set all values that match
112  SetJsonValue(root);
113  }
114  catch (const std::exception& e)
115  {
116  // Error parsing JSON (or missing keys)
117  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
118  }
119 }
120 
121 // Load Json::Value into this object
122 void Point::SetJsonValue(const Json::Value root) {
123 
124  if (!root["co"].isNull())
125  co.SetJsonValue(root["co"]); // update coordinate
126  if (!root["handle_left"].isNull())
127  handle_left.SetJsonValue(root["handle_left"]); // update coordinate
128  if (!root["handle_right"].isNull())
129  handle_right.SetJsonValue(root["handle_right"]); // update coordinate
130  if (!root["interpolation"].isNull())
131  interpolation = (InterpolationType) root["interpolation"].asInt();
132  if (!root["handle_type"].isNull())
133  handle_type = (HandleType) root["handle_type"].asInt();
134 
135 }
void Initialize_LeftHandle(float x, float y)
Set the left handle to a percent of the primary coordinate (0 to 1)
Definition: Point.cpp:70
This class represents a Cartesian coordinate (X, Y) used in the Keyframe animation system...
Definition: Coordinate.h:54
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Point.cpp:122
Bezier curves are quadratic curves, which create a smooth curve.
Definition: Point.h:47
InterpolationType interpolation
This is the interpolation mode.
Definition: Point.h:87
Header file for Point class.
Coordinate handle_right
This is the right handle coordinate (in percentages from 0 to 1)
Definition: Point.h:86
STL namespace.
void Initialize_RightHandle(float x, float y)
Set the right handle to a percent of the primary coordinate (0 to 1)
Definition: Point.cpp:75
Coordinate handle_left
This is the left handle coordinate (in percentages from 0 to 1)
Definition: Point.h:85
A Point is the basic building block of a key-frame curve.
Definition: Point.h:82
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:34
HandleType handle_type
This is the handle mode.
Definition: Point.h:88
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: Coordinate.cpp:54
Header file for all Exception classes.
std::string Json() const
Generate JSON string of this object.
Definition: Point.cpp:81
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: Point.cpp:88
HandleType
When BEZIER interpolation is used, the point&#39;s left and right handles are used to influence the direc...
Definition: Point.h:59
Point()
Default constructor (defaults to 1,0)
Definition: Point.cpp:38
Automatically adjust the handles to achieve the smoothest curve.
Definition: Point.h:60
InterpolationType
This controls how a Keyframe uses this point to interpolate between two points.
Definition: Point.h:46
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:46
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Coordinate.cpp:88
Coordinate co
This is the primary coordinate.
Definition: Point.h:84
Exception for invalid JSON.
Definition: Exceptions.h:205
void SetJson(const std::string value)
Load JSON string into this object.
Definition: Point.cpp:105
void Initialize_Handles()
Definition: Point.cpp:63
Constant curves jump from their previous position to a new one (with no interpolation).
Definition: Point.h:49