-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedges.h
280 lines (211 loc) · 6.43 KB
/
edges.h
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
/*
* Copyright 2023 Jade Keegan
*/
#ifndef edges_DEFINED
#define edges_DEFINED
#include "include/GCanvas.h"
#include "include/GRect.h"
#include "include/GColor.h"
#include "include/GBitmap.h"
#include "path.h"
#include "helpers.h"
#include <deque>
struct Edge {
// m = (x1-x0)/(y1-y0)
float m, b;
int top, bottom;
float currX;
int wind;
};
Edge makeEdge(GPoint p0, GPoint p1, int wind) {
if (p0.y > p1.y) {
std::swap(p0, p1);
}
int top = myRound(p0.y);
int bottom = myRound(p1.y);
if (top == bottom) {
// return an invalid edge
return {-1, -1, -1, -1, -1, 0 };
}
const float m = (p1.x - p0.x) / (p1.y - p0.y);
const float b = p0.x - (p0.y * m);
return { .m=m, .b=b, .top=top, .bottom=bottom, .currX = m * ((float) top + 0.5f) + b, .wind=(wind ? 1 : -1) };
}
// clip edge
std::vector<Edge> clipEdges(int bottom, int right, GPoint p0, GPoint p1) {
std::vector<Edge> edges;
bool wind;
// make p0 = top pt, p1 = bottom pt
if (p0.y == p1.y) {
return edges;
}
if (p0.y > p1.y) {
std::swap(p0, p1);
wind = false;
} else {
wind = true;
}
// if vertically above/below canvas
if (p1.y <= 0 || p0.y >= bottom) {
return edges;
}
const float m = (p1.x - p0.x) / (p1.y - p0.y);
const float b = p0.x - (p0.y * m);
// clip top
if (p0.y < 0) {
p0.x = b;
p0.y = 0;
}
// clip bottom
if (p1.y > bottom) {
p1.x = m * bottom + b;
p1.y = bottom;
}
// clip sides
if (p0.x > p1.x) {
std::swap(p0, p1);
}
// left project
if (p1.x <= 0) { // not on canvas, left project
Edge edge = makeEdge({0, p0.y}, {0, p1.y}, wind);
if (edge.top != -1) {
edges.push_back(edge);
}
return edges;
}
// right project
if (p0.x >= right) { // not on canvas, right project
Edge edge = makeEdge({right, p0.y}, {right, p1.y}, wind);
if (edge.top != -1) {
edges.push_back(edge);
}
return edges;
}
// left straddle
if (p0.x < 0) { // left point straddling
float newY = (b / m) * -1;
Edge edge = makeEdge({0, p0.y}, {0, newY}, wind);
p0 = {0, newY};
if (edge.top != -1) {
edges.push_back(edge);
}
}
// right straddle
if (p1.x > right) {
float newY = (right-b) / m;
Edge edge = makeEdge({right, p1.y}, {right, newY}, wind);
p1 = {(float) right, newY};
if (edge.top != -1) {
edges.push_back(edge);
}
}
Edge edge = makeEdge(p0, p1, wind);
if (edge.top != -1) {
edges.push_back(edge);
}
return edges;
}
std::vector<Edge> buildEdges(GBitmap device, int count, const GPoint points[]) {
// clip points
int dBottom = device.height();
int dRight = device.width();
std::vector<Edge> edges;
// build edges
for (int i=0; i<count; i++) {
GPoint p0 = points[i];
GPoint p1 = points[(i+1) % count];
std::vector<Edge> new_edges = clipEdges(dBottom, dRight, p0, p1);
if (new_edges.size() > 0) {
edges.insert(edges.end(), new_edges.begin(), new_edges.end());
}
}
return edges;
}
int numQuadSegments(GPoint pts[3]) {
GPoint E = (pts[0] - 2 * pts[1] + pts[2]) * 0.25f; // (A - 2B + C) / 4
float magnitude = sqrt(E.x * E.x + E.y * E.y);
// (int)ceil(sqrt(|E|/tolerance))
return (int) ceil(sqrt(magnitude * 4)); // tolerance = 0.25
}
int numCubicSegments(GPoint pts[4]) {
GPoint E0 = pts[0] - 2 * pts[1] + pts[2]; // A - 2B + C
GPoint E1 = pts[1] - 2 * pts[2] + pts[3]; // B - 2C + D
float Ex = std::max(abs(E0.x), abs(E1.x));
float Ey = std::max(abs(E0.y), abs(E1.y));
float magnitude = sqrt(Ex * Ex + Ey * Ey);
// (int)ceil(sqrt((3*|E|)/(4*tolerance)));
return (int) ceil(sqrt(magnitude * 3)); // tolerance = 0.25
}
std::vector<Edge> buildPathEdges(GPath path, int width, int height) {
std::vector<Edge> edges;
GPoint pts[GPath::kMaxNextPoints];
GPath::Edger edger(path);
GPath::Verb v;
while ((v = edger.next(pts)) != GPath::kDone) {
std::vector<Edge> new_edges;
int num_segments;
GPoint p0, p1;
float t;
switch (v) {
case GPath::kLine:
new_edges = clipEdges(height, width, pts[0], pts[1]);
if (new_edges.size() > 0) {
edges.insert(edges.end(), new_edges.begin(), new_edges.end());
}
break;
case GPath::kQuad:
// optimize to use chopQuad where f'(t)y = 0 (derivative of f(t) where change in y = 0)
num_segments = numQuadSegments(pts);
p0 = pts[0];
for (int i=1; i < num_segments; i++) {
t = i * (1.f/num_segments);
p1 = { getQuadCurvePoint(pts[0].x, pts[1].x, pts[2].x, t).ABC,
getQuadCurvePoint(pts[0].y, pts[1].y, pts[2].y, t).ABC };
new_edges = clipEdges(height, width, p0, p1);
if (new_edges.size() > 0) {
edges.insert(edges.end(), new_edges.begin(), new_edges.end());
}
p0 = p1;
}
new_edges = clipEdges(height, width, p0, pts[2]);
if (new_edges.size() > 0) {
edges.insert(edges.end(), new_edges.begin(), new_edges.end());
}
break;
case GPath::kCubic:
num_segments = numCubicSegments(pts);
p0 = pts[0];
for (int i=1; i < num_segments; i++) {
t = i * (1.f/num_segments);
p1 = { getCubicCurvePoint(pts[0].x, pts[1].x, pts[2].x, pts[3].x, t).ABCD,
getCubicCurvePoint(pts[0].y, pts[1].y, pts[2].y, pts[3].y, t).ABCD };
new_edges = clipEdges(height, width, p0, p1);
if (new_edges.size() > 0) {
edges.insert(edges.end(), new_edges.begin(), new_edges.end());
}
p0 = p1;
}
new_edges = clipEdges(height, width, p0, pts[3]);
if (new_edges.size() > 0) {
edges.insert(edges.end(), new_edges.begin(), new_edges.end());
}
break;
}
}
return edges;
}
bool isValidEdge(Edge e, int y) {
return e.top <= y && e.bottom > y;
}
// check if e0 > e1
bool compareEdges(Edge e0, Edge e1) {
return (e0.top < e1.top) ? true:
(e1.top < e0.top) ? false:
(e0.currX < e1.currX) ? true:
(e1.currX < e0.currX) ? false:
(e0.m < e1.m);
}
bool compareEdgesX(Edge e0, Edge e1) {
return e0.currX < e1.currX;
}
#endif