OpenShot Library | libopenshot  0.2.4
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() {
112 
113  // Return formatted string
114  return JsonValue().toStyledString();
115 }
116 
117 // Generate Json::JsonValue for this object
118 Json::Value Point::JsonValue() {
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(std::string value) {
136 
137  // Parse JSON string into JSON objects
138  Json::Value root;
139  Json::CharReaderBuilder rbuilder;
140  Json::CharReader* reader(rbuilder.newCharReader());
141 
142  std::string errors;
143  bool success = reader->parse( value.c_str(),
144  value.c_str() + value.size(), &root, &errors );
145  delete reader;
146 
147  if (!success)
148  // Raise exception
149  throw InvalidJSON("JSON could not be parsed (or is invalid)");
150 
151  try
152  {
153  // Set all values that match
154  SetJsonValue(root);
155  }
156  catch (const std::exception& e)
157  {
158  // Error parsing JSON (or missing keys)
159  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
160  }
161 }
162 
163 // Load Json::JsonValue into this object
164 void Point::SetJsonValue(Json::Value root) {
165 
166  if (!root["co"].isNull())
167  co.SetJsonValue(root["co"]); // update coordinate
168  if (!root["handle_left"].isNull())
169  handle_left.SetJsonValue(root["handle_left"]); // update coordinate
170  if (!root["handle_right"].isNull())
171  handle_right.SetJsonValue(root["handle_right"]); // update coordinate
172  if (!root["interpolation"].isNull())
173  interpolation = (InterpolationType) root["interpolation"].asInt();
174  if (!root["handle_type"].isNull())
175  handle_type = (HandleType) root["handle_type"].asInt();
176 
177 }
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
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Point.cpp:118
This class represents a Cartesian coordinate (X, Y) used in the Keyframe animation system...
Definition: Coordinate.h:55
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Coordinate.cpp:101
Bezier curves are quadratic curves, which create a smooth curve.
Definition: Point.h:47
void SetJson(std::string value)
Load JSON string into this object.
Definition: Point.cpp:135
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
HandleType handle_type
This is the handle mode.
Definition: Point.h:88
std::string Json()
Get and Set JSON methods.
Definition: Point.cpp:111
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.
Coordinate co
This is the primary coordinate.
Definition: Point.h:84
Exception for invalid JSON.
Definition: Exceptions.h:205
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Point.cpp:164
void Initialize_Handles()
Definition: Point.cpp:93
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Coordinate.cpp:55
Constant curves jump from their previous position to a new one (with no interpolation).
Definition: Point.h:49