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