-
Notifications
You must be signed in to change notification settings - Fork 0
/
bezier.h
executable file
·296 lines (249 loc) · 9.69 KB
/
bezier.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#ifndef BEZIER_H
#define BEZIER_H
#include <cstddef>
#include <ostream>
#include <vector>
#include <functional>
#include <cmath>
#include <iostream>
namespace bezier {
namespace constants {
const int NUM_OF_CUBIC_BEZIER_NODES = 4;
} // bezier::constants
class Point2d;
namespace types {
using point_2d = Point2d;
using real_t = double;
using node_index_t = unsigned int;
} // bezier::types
namespace details {
const types::real_t ARC = 4 * (sqrt(2) - 1) / 3;
types::real_t degToRad(types::real_t deg) {
return deg * M_PI / 180;
}
using curve_t = std::function<types::point_2d(types::node_index_t)>;
using oneDimArray_t = std::vector<bool>;
using twoDimArray_t = std::vector<std::vector<bool>>;
} // bezier::details
class Point2d {
public:
Point2d(types::real_t X, types::real_t Y) : X(X), Y(Y) {};
// A linear transformation to a given point.
Point2d(const Point2d &point,
types::real_t a11, types::real_t a12,
types::real_t a21, types::real_t a22) :
X(point.X * a11 + point.Y * a12),
Y(point.X * a21 + point.Y * a22) {};
Point2d operator+(const Point2d &other) const {
return Point2d{this->X + other.X, this->Y + other.Y};
}
Point2d operator*(const types::real_t scalar) const {
return Point2d{this->X * scalar, this->Y * scalar};
}
bool operator==(const Point2d &other) const {
return (this->X == other.X && this->Y == other.Y);
}
std::ostream &operator<<(std::ostream &ostr) const {
ostr << "(" << this->X << ", " << this->Y << ")";
return ostr;
}
const types::real_t X;
const types::real_t Y;
};
Point2d operator*(const types::real_t scalar, const Point2d &point) {
return point * scalar;
}
types::point_2d nodes(const types::point_2d p0,
const types::point_2d p1,
const types::point_2d p2,
const types::point_2d p3,
types::node_index_t index) {
return (index == 0 ? p0 :
(index == 1 ? p1 :
(index == 2 ? p2 :
(index == 3 ? p3 :
throw std::out_of_range(
"a curve node index is out of range")))));
}
details::curve_t Cup() {
static auto cup =
[](types::node_index_t index) {
return nodes(types::point_2d(-1, 1),
types::point_2d(-1, -1),
types::point_2d(1, -1),
types::point_2d(1, 1),
index);
};
return cup;
}
details::curve_t Cap() {
static auto cap =
[](types::node_index_t index) {
return nodes(types::point_2d(-1, -1),
types::point_2d(-1, 1),
types::point_2d(1, 1),
types::point_2d(1, -1),
index);
};
return cap;
}
details::curve_t ConvexArc() {
static auto convexArc =
[](types::node_index_t index) {
return nodes(types::point_2d(0, 1),
types::point_2d(details::ARC, 1),
types::point_2d(1, details::ARC),
types::point_2d(1, 0),
index);
};
return convexArc;
}
details::curve_t ConcaveArc() {
static auto concaveArc =
[](types::node_index_t index) {
return nodes(types::point_2d(0, 1),
types::point_2d(0, 1 - details::ARC),
types::point_2d(1 - details::ARC, 0),
types::point_2d(1, 0),
index);
};
return concaveArc;
}
details::curve_t LineSegment(
types::point_2d p, types::point_2d q) {
auto lineSegment =
[p, q](types::node_index_t index) {
return nodes(p, p, q, q, index);
};
return lineSegment;
}
details::curve_t MovePoint(
const details::curve_t &f,
types::node_index_t index_no,
types::real_t x,
types::real_t y) {
auto movePoint =
[f, index_no, x, y](types::node_index_t index) {
return (index == index_no
? f(index) + types::point_2d(x, y)
: f(index));
};
return movePoint;
}
details::curve_t Rotate(
const details::curve_t &f,
types::real_t a) {
auto rotate =
[f, a](types::node_index_t index) {
return types::point_2d(f(index),
cos(details::degToRad(a)),
-sin(details::degToRad(a)),
sin(details::degToRad(a)),
cos(details::degToRad(a)));
};
return rotate;
}
details::curve_t Scale(
const details::curve_t &f,
types::real_t x,
types::real_t y) {
auto scale =
[f, x, y](types::node_index_t index) {
return types::point_2d(f(index),
x, 0,
0, y);
};
return scale;
}
details::curve_t Translate(
const details::curve_t &f,
types::real_t x,
types::real_t y) {
auto translate =
[f, x, y](types::node_index_t index) {
return f(index) + types::point_2d(x, y);
};
return translate;
}
details::curve_t Concatenate(
const details::curve_t &f1,
const details::curve_t &f2) {
auto concatenate =
[f1, f2](types::node_index_t index) {
return (index < 4 ? f1(index) : f2(index - 4));
};
return concatenate;
}
template<typename... Arg>
details::curve_t Concatenate(
details::curve_t f1, Arg... args) {
return Concatenate(f1, Concatenate(args...));
}
class P3CurvePlotter {
public:
explicit P3CurvePlotter(
const details::curve_t &f,
size_t numberOfSegments = 1,
size_t resolution = 80) :
resolution(resolution) {
belongsToPlot = details::twoDimArray_t
(resolution, details::oneDimArray_t(resolution, false));
// Rounded up.
size_t pointsPerSegment =
(resolution * resolution + numberOfSegments - 1)
/ numberOfSegments;
types::real_t t;
size_t x_index, y_index;
for (size_t sg_no = 1; sg_no <= numberOfSegments; sg_no++) {
for (size_t i = 0; i <= pointsPerSegment; i++) {
t = 1.0 * i / pointsPerSegment;
types::point_2d p = this->operator()(f, t, sg_no);
// First shift by vector (1, 1).
x_index = (size_t) ((p.X + 1)
* ((types::real_t) resolution / 2));
y_index = (size_t) ((p.Y + 1)
* ((types::real_t) resolution / 2));
if (x_index < 0 || x_index >= resolution
|| y_index < 0 || y_index >= resolution) {
continue;
}
// Printing a vector is done with lines from last to first.
belongsToPlot[(resolution - 1) - y_index][x_index] = true;
}
}
}
void Print(std::ostream &os = std::cout,
const char fb = '*',
const char bg = ' ') const {
for (const auto &row : belongsToPlot) {
for (auto element : row) {
if (element) {
os << fb;
} else {
os << bg;
}
}
os << std::endl;
}
}
types::point_2d operator()(const details::curve_t &f,
types::real_t t,
size_t segmentNumber) const {
const types::point_2d p0 = f(0 + (segmentNumber - 1) * 4);
const types::point_2d p1 = f(1 + (segmentNumber - 1) * 4);
const types::point_2d p2 = f(2 + (segmentNumber - 1) * 4);
const types::point_2d p3 = f(3 + (segmentNumber - 1) * 4);
types::point_2d b0 = (((types::real_t) 1.0 - t) * p0) + (t * p1);
types::point_2d b1 = (((types::real_t) 1.0 - t) * p1) + (t * p2);
types::point_2d b2 = (((types::real_t) 1.0 - t) * p2) + (t * p3);
types::point_2d b0_2 = (((types::real_t) 1.0 - t) * b0) + (t * b1);
types::point_2d b1_2 = (((types::real_t) 1.0 - t) * b1) + (t * b2);
types::point_2d b0_3 = (((types::real_t) 1.0 - t) * b0_2) + (t * b1_2);
return b0_3;
}
private:
size_t resolution;
details::twoDimArray_t belongsToPlot;
};
} // bezier
#endif // BEZIER_H