Kale
Loading...
Searching...
No Matches
Path.hpp
Go to the documentation of this file.
1/*
2 Copyright 2022 Rishi Challa
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#pragma once
18
22
23#include <vector>
24#include <ostream>
25#include <utility>
26
27#include <nlohmann/json.hpp>
28
29namespace Kale {
30
35 using JSON = nlohmann::json;
36
43
47 class Path {
48 private:
49
54 void roundCorners(const std::vector<std::pair<Vector2f, float>>& lines);
55
56 public:
57
61 std::vector<CubicBezier> beziers;
62
66 Path();
67
72 Path(size_t n);
73
78 Path(const std::vector<CubicBezier>& beziers);
79
85 Path(const std::vector<Vector2f>& lines, float cornerRadius);
86
93 Path(const std::vector<std::pair<Vector2f, float>>& lines);
94
99 Rect getBoundingBox() const;
100
105 void operator+=(const Path& other);
106
111 Path operator*(float value) const;
112
119 void moveTo(Vector2f pos);
120
126 void lineTo(Vector2f pos);
127
135 void bezierTo(Vector2f controlPoint1, Vector2f controlPoint2, Vector2f end);
136
144 void arcTo(Vector2f end, Vector2f center, bool clockwise);
145
150 void closePath();
151
152 };
153
157 std::ostream& operator<<(std::ostream& os, const CubicBezier& bezier);
158
162 std::ostream& operator<<(std::ostream& os, const Path& path);
163
167 inline void from_json(const JSON& j, CubicBezier& p) {
168 p.start = j["P0"].get<Vector2f>();
169 p.controlPoint1 = j["P1"].get<Vector2f>();
170 p.controlPoint2 = j["P2"].get<Vector2f>();
171 p.end = j["P3"].get<Vector2f>();
172 }
173
177 inline void to_json(JSON& j, const CubicBezier& p) {
178 j = {{"P0", p.start}, {"P1", p.controlPoint1}, {"P2", p.controlPoint2}, {"P3", p.end}};
179 }
180
184 inline void from_json(const JSON& j, Path& p) {
185 p.beziers = j.get<std::vector<CubicBezier>>();
186 }
187
191 inline void to_json(JSON& j, const Path& p) {
192 j = JSON(p.beziers);
193 }
194
195}
void bezierTo(Vector2f controlPoint1, Vector2f controlPoint2, Vector2f end)
Definition Path.cpp:216
void moveTo(Vector2f pos)
Definition Path.cpp:192
void roundCorners(const std::vector< std::pair< Vector2f, float > > &lines)
Definition Path.cpp:75
void closePath()
Definition Path.cpp:272
void operator+=(const Path &other)
Definition Path.cpp:161
void arcTo(Vector2f end, Vector2f center, bool clockwise)
Definition Path.cpp:230
void lineTo(Vector2f pos)
Definition Path.cpp:202
Path operator*(float value) const
Definition Path.cpp:175
std::vector< CubicBezier > beziers
Definition Path.hpp:61
Rect getBoundingBox() const
Definition Path.cpp:141
nlohmann::json JSON
Definition Scene.hpp:39
void to_json(JSON &j, const Matrix< w, h, T > &p)
Definition Matrix.hpp:547
void from_json(const JSON &j, Matrix< w, h, T > &p)
Definition Matrix.hpp:539
std::ostream & operator<<(std::ostream &os, const Kale::Matrix< w, h, T > &mat)
Definition Matrix.hpp:524
Vector2f start
Definition Path.hpp:41
Vector2f controlPoint1
Definition Path.hpp:41
Vector2f end
Definition Path.hpp:41
Vector2f controlPoint2
Definition Path.hpp:41