/
path.cc
325 lines (266 loc) · 7.35 KB
/
path.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/**
* This file is part of the "clip" project
* Copyright (c) 2018 Paul Asmuth
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <iostream>
#include "path.h"
namespace clip {
double PathData::operator[](size_t idx) const {
return coefficients[idx];
}
Path::Path() {}
Path::Path(const PathData* data, size_t size) {
data_.reserve(size);
for (size_t i = 0; i < size; ++i) {
data_.emplace_back(data[i]);
}
}
void Path::moveTo(double x, double y) {
PathData d;
d.command = PathCommand::MOVE_TO;
d.coefficients[0] = x;
d.coefficients[1] = y;
data_.emplace_back(std::move(d));
}
void Path::moveTo(const Point& p) {
PathData d;
d.command = PathCommand::MOVE_TO;
d.coefficients[0] = p.x;
d.coefficients[1] = p.y;
data_.emplace_back(std::move(d));
}
void Path::lineTo(double x, double y) {
PathData d;
d.command = PathCommand::LINE_TO;
d.coefficients[0] = x;
d.coefficients[1] = y;
data_.emplace_back(std::move(d));
}
void Path::lineTo(const Point& p) {
PathData d;
d.command = PathCommand::LINE_TO;
d.coefficients[0] = p.x;
d.coefficients[1] = p.y;
data_.emplace_back(std::move(d));
}
void Path::quadraticCurveTo(double cx, double cy, double x, double y) {
PathData d;
d.command = PathCommand::QUADRATIC_CURVE_TO;
d.coefficients[0] = cx;
d.coefficients[1] = cy;
d.coefficients[2] = x;
d.coefficients[3] = y;
data_.emplace_back(std::move(d));
}
void Path::cubicCurveTo(double c1x, double c1y, double c2x, double c2y, double x, double y) {
PathData d;
d.command = PathCommand::CUBIC_CURVE_TO;
d.coefficients[0] = c1x;
d.coefficients[1] = c1y;
d.coefficients[2] = c2x;
d.coefficients[3] = c2y;
d.coefficients[4] = x;
d.coefficients[5] = y;
data_.emplace_back(std::move(d));
}
void Path::closePath() {
PathData d;
d.command = PathCommand::CLOSE;
data_.emplace_back(std::move(d));
}
const PathData& Path::operator[](size_t idx) const {
return data_[idx];
}
PathData& Path::operator[](size_t idx) {
return data_[idx];
}
const PathData* Path::data() const {
return &*data_.data();
}
PathData* Path::data() {
return &*data_.data();
}
const PathData* Path::begin() const {
return &*data_.begin();
}
PathData* Path::begin() {
return &*data_.begin();
}
const PathData* Path::end() const {
return &*data_.end();
}
PathData* Path::end() {
return &*data_.end();
}
size_t Path::size() const {
return data_.size();
}
bool Path::empty() const {
return data_.size() == 0;
}
Polygon2 path_to_polygon_simple(const Path& path) {
Polygon2 poly;
for (size_t i = 0; i < path.size(); ++i) {
const auto& cmd = path[i];
switch (cmd.command) {
case PathCommand::MOVE_TO:
case PathCommand::LINE_TO:
poly.vertices.emplace_back(cmd[0], cmd[1]);
break;
case PathCommand::QUADRATIC_CURVE_TO:
poly.vertices.emplace_back(cmd[2], cmd[3]);
break;
case PathCommand::CUBIC_CURVE_TO:
poly.vertices.emplace_back(cmd[4], cmd[5]);
break;
case PathCommand::CLOSE:
continue;
}
}
return poly;
}
Path path_from_polygon(const Polygon2& poly) {
Path p;
if (poly.vertices.empty()) {
return p;
}
p.moveTo(poly.vertices[0].x, poly.vertices[0].y);
for (size_t i = 1; i < poly.vertices.size(); ++i) {
p.lineTo(poly.vertices[i].x, poly.vertices[i].y);
}
p.closePath();
return p;
}
Path path_from_poly2(const Poly2& poly) {
Path p;
path_add_poly_ring(&p, poly.boundary);
for (const auto& r : poly.holes) {
path_add_poly_ring(&p, r);
}
return p;
}
Path path_transform(const Path& path, const mat3& transform) {
Path path_out;
for (const auto& cmd : path) {
switch (cmd.command) {
case PathCommand::MOVE_TO: {
auto p0 = mul(transform, {cmd[0], cmd[1], 1.0});
path_out.moveTo(p0.x, p0.y);
break;
}
case PathCommand::LINE_TO: {
auto p0 = mul(transform, {cmd[0], cmd[1], 1.0});
path_out.lineTo(p0.x, p0.y);
break;
}
case PathCommand::QUADRATIC_CURVE_TO: {
auto p0 = mul(transform, {cmd[0], cmd[1], 1.0});
auto p1 = mul(transform, {cmd[2], cmd[3], 1.0});
path_out.quadraticCurveTo(p0.x, p0.y, p1.x, p1.y);
break;
}
case PathCommand::CUBIC_CURVE_TO: {
auto p0 = mul(transform, {cmd[0], cmd[1], 1.0});
auto p1 = mul(transform, {cmd[2], cmd[3], 1.0});
auto p2 = mul(transform, {cmd[4], cmd[5], 1.0});
path_out.cubicCurveTo(p0.x, p0.y, p1.x, p1.y, p2.x, p2.y);
break;
}
case PathCommand::CLOSE: {
path_out.closePath();
break;
}
}
}
return path_out;
}
void path_add_line(Path* path, vec2 from, vec2 to) {
path->moveTo(from.x, from.y);
path->lineTo(to.x, to.y);
}
void path_add_rectangle(Path* path, vec2 origin, vec2 size) {
path->moveTo(
origin.x + size.x / 2,
origin.y + size.y / 2);
path->lineTo(
origin.x + size.x / 2,
origin.y - size.y / 2);
path->lineTo(
origin.x - size.x / 2,
origin.y - size.y / 2);
path->lineTo(
origin.x - size.x / 2,
origin.y + size.y / 2);
path->closePath();
}
void path_add_rectangle(Path* path, const Rectangle& box) {
path->moveTo(box.x, box.y);
path->lineTo(box.x, box.y + box.h);
path->lineTo(box.x + box.w, box.y + box.h);
path->lineTo(box.x + box.w, box.y);
path->closePath();
}
void path_add_circle(Path* path, vec2 origin, double radius) {
const double control_point_distance = 0.552284749831;
path->moveTo(
origin.x,
origin.y - radius);
path->cubicCurveTo(
origin.x - radius * control_point_distance,
origin.y - radius,
origin.x - radius,
origin.y - radius * control_point_distance,
origin.x - radius,
origin.y);
path->cubicCurveTo(
origin.x - radius,
origin.y + radius * control_point_distance,
origin.x - radius * control_point_distance,
origin.y + radius,
origin.x,
origin.y + radius);
path->cubicCurveTo(
origin.x + radius * control_point_distance,
origin.y + radius,
origin.x + radius,
origin.y + radius * control_point_distance,
origin.x + radius,
origin.y);
path->cubicCurveTo(
origin.x + radius,
origin.y - radius * control_point_distance,
origin.x + radius * control_point_distance,
origin.y - radius,
origin.x,
origin.y - radius);
path->closePath();
}
void path_add_poly_line(Path* path, const PolyLine2& line) {
if (line.vertices.empty()) {
return;
}
path->moveTo(line.vertices[0].x, line.vertices[0].y);
for (size_t i = 1; i < line.vertices.size(); ++i) {
path->lineTo(line.vertices[i].x, line.vertices[i].y);
}
}
void path_add_poly_ring(Path* path, const PolyLine2& ring) {
if (ring.vertices.empty()) {
return;
}
path->moveTo(ring.vertices[0].x, ring.vertices[0].y);
for (size_t i = 1; i < ring.vertices.size(); ++i) {
path->lineTo(ring.vertices[i].x, ring.vertices[i].y);
}
path->closePath();
}
} // namespace clip