OpenShot Library | libopenshot  0.2.5
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 "../include/Point.h"
32 
33 using namespace std;
34 using namespace openshot;
35 
36 // Default constructor (defaults to 1,0)
37 Point::Point() : interpolation(BEZIER), handle_type(AUTO)
38 {
39  // set new coorinate
40  co = Coordinate(1, 0);
41 
42  // set handles
44 }
45 
46 // Constructor which creates a single coordinate at X=1
47 Point::Point(float y) :
49  // set new coorinate
50  co = Coordinate(1, y);
51 
52  // set handles
54 }
55 
56 Point::Point(float x, float y) :
58  // set new coorinate
59  co = Coordinate(x, y);
60 
61  // set handles
63 }
64 
65 // Constructor which also creates a Point and sets the X,Y, and interpolation of the Point.
67  handle_type(AUTO), interpolation(interpolation) {
68  // set new coorinate
69  co = Coordinate(x, y);
70 
71  // set handles
73 }
74 
77  // set handles
79 }
80 
82  co(co), interpolation(interpolation), handle_type(AUTO) {
83  // set handles
85 }
86 
88  co(co), interpolation(interpolation), handle_type(handle_type) {
89  // set handles
91 }
92 
94  // initialize left and right handles (in percentages from 0 to 1)
95  // default to a smooth curve
96  Initialize_LeftHandle(0.5, 1.0);
97  Initialize_RightHandle(0.5, 0.0);
98 }
99 
100 void Point::Initialize_LeftHandle(float x, float y) {
101  // initialize left handle (in percentages from 0 to 1)
102  handle_left = Coordinate(x, y);
103 }
104 
105 void Point::Initialize_RightHandle(float x, float y) {
106  // initialize right handle (in percentages from 0 to 1)
107  handle_right = Coordinate(x, y);
108 }
109 
110 // Generate JSON string of this object
111 std::string Point::Json() const {
112 
113  // Return formatted string
114  return JsonValue().toStyledString();
115 }
116 
117 // Generate Json::Value for this object
118 Json::Value Point::JsonValue() const {
119 
120  // Create root json object
121  Json::Value root;
122  root["co"] = co.JsonValue();
123  if (interpolation == BEZIER) {
124  root["handle_left"] = handle_left.JsonValue();
125  root["handle_right"] = handle_right.JsonValue();
126  root["handle_type"] = handle_type;
127  }
128  root["interpolation"] = interpolation;
129 
130  // return JsonValue
131  return root;
132 }
133 
134 // Load JSON string into this object
135 void Point::SetJson(const std::string value) {
136 
137  // Parse JSON string into JSON objects
138  try
139  {
140  const Json::Value root = openshot::stringToJson(value);
141  // Set all values that match
142  SetJsonValue(root);
143  }
144  catch (const std::exception& e)
145  {
146  // Error parsing JSON (or missing keys)
147  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
148  }
149 }
150 
151 // Load Json::Value into this object
152 void Point::SetJsonValue(const Json::Value root) {
153 
154  if (!root["co"].isNull())
155  co.SetJsonValue(root["co"]); // update coordinate
156  if (!root["handle_left"].isNull())
157  handle_left.SetJsonValue(root["handle_left"]); // update coordinate
158  if (!root["handle_right"].isNull())
159  handle_right.SetJsonValue(root["handle_right"]); // update coordinate
160  if (!root["interpolation"].isNull())
161  interpolation = (InterpolationType) root["interpolation"].asInt();
162  if (!root["handle_type"].isNull())
163  handle_type = (HandleType) root["handle_type"].asInt();
164 
165 }
void Initialize_LeftHandle(float x, float y)
Set the left handle to a percent of the primary coordinate (0 to 1)
Definition: Point.cpp:100
This class represents a Cartesian coordinate (X, Y) used in the Keyframe animation system...
Definition: Coordinate.h:55
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Point.cpp:152
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
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:105
Coordinate handle_left
This is the left handle coordinate (in percentages from 0 to 1)
Definition: Point.h:85
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:33
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:55
std::string Json() const
Get and Set JSON methods.
Definition: Point.cpp:111
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: Point.cpp:118
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:37
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.
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Coordinate.cpp:89
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:135
void Initialize_Handles()
Definition: Point.cpp:93
Constant curves jump from their previous position to a new one (with no interpolation).
Definition: Point.h:49