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
Point()38 Point::Point() : Point::Point(Coordinate(1, 0), BEZIER, AUTO) {};
39 
40 // Constructor which creates a single coordinate at X=1
Point(float y)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)
Point(float x,float 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.
Point(float x,float y,InterpolationType interpolation)47 Point::Point(float x, float y, InterpolationType interpolation)
48 	: Point::Point(Coordinate(x, y), interpolation, AUTO) {};
49 
50 
51 // Direct Coordinate-accepting constructors
Point(const Coordinate & co)52 Point::Point(const Coordinate& co) : Point::Point(co, BEZIER, AUTO) {};
53 
Point(const Coordinate & co,InterpolationType interpolation)54 Point::Point(const Coordinate& co, InterpolationType interpolation)
55 	: Point::Point(co, interpolation, AUTO) {};
56 
Point(const Coordinate & co,InterpolationType interpolation,HandleType handle_type)57 Point::Point(const Coordinate& co, InterpolationType interpolation, HandleType handle_type) :
58 	co(co), interpolation(interpolation), handle_type(handle_type) {
59 	// set handles
60 	Initialize_Handles();
61 }
62 
Initialize_Handles()63 void Point::Initialize_Handles() {
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 
Initialize_LeftHandle(float x,float y)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 
Initialize_RightHandle(float x,float y)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
Json() const81 std::string Point::Json() const {
82 
83 	// Return formatted string
84 	return JsonValue().toStyledString();
85 }
86 
87 // Generate Json::Value for this object
JsonValue() const88 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
SetJson(const std::string value)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
SetJsonValue(const Json::Value root)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 }
136