| 1 |
// -*- related-file-name: "../include/lcdf/bezier.hh" -*- |
| 2 |
|
| 3 |
/* bezier.{cc,hh} -- cubic Bezier curves |
| 4 |
* |
| 5 |
* Copyright (c) 1998-2023 Eddie Kohler |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify it |
| 8 |
* under the terms of the GNU General Public License as published by the Free |
| 9 |
* Software Foundation; either version 2 of the License, or (at your option) |
| 10 |
* any later version. This program is distributed in the hope that it will be |
| 11 |
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
| 13 |
* Public License for more details. |
| 14 |
*/ |
| 15 |
|
| 16 |
#ifdef HAVE_CONFIG_H |
| 17 |
# include <config.h> |
| 18 |
#endif |
| 19 |
#include <lcdf/bezier.hh> |
| 20 |
|
| 21 |
// |
| 22 |
// bounding box |
| 23 |
// |
| 24 |
|
| 25 |
void |
| 26 |
Bezier::make_bb() const noexcept |
| 27 |
{ |
| 28 |
_bb = 0; |
| 29 |
for (int i = 1; i < 4; i++) { |
| 30 |
if (_p[i].x > bb_right_x()) |
| 31 |
_bb = (_bb & ~0x03) | (i << 0); |
| 32 |
else if (_p[i].x < bb_left_x()) |
| 33 |
_bb = (_bb & ~0x0C) | (i << 2); |
| 34 |
if (_p[i].y > bb_top_x()) |
| 35 |
_bb = (_bb & ~0x30) | (i << 4); |
| 36 |
else if (_p[i].y < bb_bottom_x()) |
| 37 |
_bb = (_bb & ~0xC0) | (i << 6); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
// |
| 43 |
// is_flat, eval |
| 44 |
// |
| 45 |
|
| 46 |
bool |
| 47 |
Bezier::is_flat(double t) const noexcept |
| 48 |
{ |
| 49 |
return (_p[2].on_segment(_p[0], _p[3], t) |
| 50 |
&& _p[1].on_segment(_p[0], _p[3], t)); |
| 51 |
} |
| 52 |
|
| 53 |
static Point |
| 54 |
eval_bezier(Point *b_in, int degree, double u) |
| 55 |
{ |
| 56 |
assert(degree < 4); |
| 57 |
Point b[4]; |
| 58 |
for (int i = 0; i <= degree; i++) |
| 59 |
b[i] = b_in[i]; |
| 60 |
|
| 61 |
double m = 1.0 - u; |
| 62 |
for (int i = 1; i <= degree; i++) |
| 63 |
for (int j = 0; j <= degree - i; j++) |
| 64 |
b[j] = b[j]*m + b[j+1]*u; |
| 65 |
return b[0]; |
| 66 |
} |
| 67 |
|
| 68 |
Point |
| 69 |
Bezier::eval(double u) const noexcept |
| 70 |
{ |
| 71 |
Bezier b = *this; |
| 72 |
double m = 1.0 - u; |
| 73 |
for (int i = 1; i < 4; i++) |
| 74 |
for (int j = 0; j < 4 - i; j++) |
| 75 |
b._p[j] = m * b._p[j] + u * b._p[j+1]; |
| 76 |
return b._p[0]; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
// |
| 81 |
// halve |
| 82 |
// |
| 83 |
|
| 84 |
void |
| 85 |
Bezier::halve(Bezier &l, Bezier &r) const noexcept |
| 86 |
{ |
| 87 |
Point half = Point::midpoint(_p[1], _p[2]); |
| 88 |
l._p[0] = _p[0]; |
| 89 |
l._p[1] = Point::midpoint(_p[0], _p[1]); |
| 90 |
l._p[2] = Point::midpoint(l._p[1], half); |
| 91 |
r._p[3] = _p[3]; |
| 92 |
r._p[2] = Point::midpoint(_p[2], _p[3]); |
| 93 |
r._p[1] = Point::midpoint(r._p[2], half); |
| 94 |
r._p[0] = l._p[3] = Point::midpoint(l._p[2], r._p[1]); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
// |
| 99 |
// hit testing |
| 100 |
// |
| 101 |
|
| 102 |
bool |
| 103 |
Bezier::in_bb(const Point &p, double tolerance) const noexcept |
| 104 |
{ |
| 105 |
ensure_bb(); |
| 106 |
if (bb_right() + tolerance < p.x |
| 107 |
|| bb_left() - tolerance > p.x |
| 108 |
|| bb_top() + tolerance < p.y |
| 109 |
|| bb_bottom() - tolerance > p.y) |
| 110 |
return false; |
| 111 |
else |
| 112 |
return true; |
| 113 |
} |
| 114 |
|
| 115 |
double |
| 116 |
Bezier::hit_recurse(const Point &p, double tolerance, double leftd, |
| 117 |
double rightd, double leftt, double rightt) const noexcept |
| 118 |
{ |
| 119 |
Bezier left, right; |
| 120 |
double middled, resultt; |
| 121 |
|
| 122 |
if (is_flat(tolerance)) { |
| 123 |
if (p.on_segment(_p[0], _p[3], tolerance)) |
| 124 |
return (leftt + rightt) / 2; |
| 125 |
else |
| 126 |
return -1; |
| 127 |
} |
| 128 |
|
| 129 |
if (leftd < tolerance * tolerance) |
| 130 |
return leftt; |
| 131 |
if (rightd < tolerance * tolerance) |
| 132 |
return rightt; |
| 133 |
|
| 134 |
if (!in_bb(p, tolerance)) |
| 135 |
return -1; |
| 136 |
|
| 137 |
halve(left, right); |
| 138 |
middled = (right._p[0] - p).squared_length(); |
| 139 |
resultt = left.hit_recurse |
| 140 |
(p, tolerance, leftd, middled, leftt, (leftt + rightt) / 2); |
| 141 |
if (resultt >= 0) |
| 142 |
return resultt; |
| 143 |
|
| 144 |
return right.hit_recurse |
| 145 |
(p, tolerance, middled, rightd, (leftt + rightt) / 2, rightt); |
| 146 |
} |
| 147 |
|
| 148 |
bool |
| 149 |
Bezier::hit(const Point &p, double tolerance) const noexcept |
| 150 |
{ |
| 151 |
double leftd = (_p[0] - p).squared_length(); |
| 152 |
double rightd = (_p[3] - p).squared_length(); |
| 153 |
double resultt = hit_recurse(p, tolerance, leftd, rightd, 0, 1); |
| 154 |
return resultt >= 0; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
// |
| 159 |
// segmentize to list of points |
| 160 |
// |
| 161 |
// uses recursive subdivision |
| 162 |
// |
| 163 |
|
| 164 |
void |
| 165 |
Bezier::segmentize(Vector<Point> &v, bool first) const |
| 166 |
{ |
| 167 |
if (is_flat(0.5)) { |
| 168 |
if (first) |
| 169 |
v.push_back(_p[0]); |
| 170 |
v.push_back(_p[3]); |
| 171 |
} else { |
| 172 |
Bezier left, right; |
| 173 |
halve(left, right); |
| 174 |
left.segmentize(v, first); |
| 175 |
right.segmentize(v, false); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
// |
| 181 |
// curve fitting |
| 182 |
// |
| 183 |
// code after Philip J. Schneider's algorithm described, with code, in the |
| 184 |
// first Graphics Gems |
| 185 |
// |
| 186 |
|
| 187 |
static void |
| 188 |
chord_length_parameterize(const Point *d, int nd, Vector<double> &result) |
| 189 |
{ |
| 190 |
assert(result.size() == 0); |
| 191 |
result.reserve(nd); |
| 192 |
result.push_back(0); |
| 193 |
for (int i = 1; i < nd; i++) |
| 194 |
result.push_back(result.back() + Point::distance(d[i-1], d[i])); |
| 195 |
double last_dist = result.back(); |
| 196 |
for (int i = 1; i < nd; i++) |
| 197 |
result[i] /= last_dist; |
| 198 |
} |
| 199 |
|
| 200 |
static inline double |
| 201 |
B0(double u) |
| 202 |
{ |
| 203 |
double m = 1.0 - u; |
| 204 |
return m*m*m; |
| 205 |
} |
| 206 |
|
| 207 |
static inline double |
| 208 |
B1(double u) |
| 209 |
{ |
| 210 |
double m = 1.0 - u; |
| 211 |
return 3*m*m*u; |
| 212 |
} |
| 213 |
|
| 214 |
static inline double |
| 215 |
B2(double u) |
| 216 |
{ |
| 217 |
double m = 1.0 - u; |
| 218 |
return 3*m*u*u; |
| 219 |
} |
| 220 |
|
| 221 |
static inline double |
| 222 |
B3(double u) |
| 223 |
{ |
| 224 |
return u*u*u; |
| 225 |
} |
| 226 |
|
| 227 |
static Bezier |
| 228 |
generate_bezier(const Point *d, int nd, const Vector<double> ¶meters, |
| 229 |
const Point &left_tangent, const Point &right_tangent) |
| 230 |
{ |
| 231 |
Point *a0 = new Point[nd]; |
| 232 |
Point *a1 = new Point[nd]; |
| 233 |
|
| 234 |
for (int i = 0; i < nd; i++) { |
| 235 |
a0[i] = left_tangent * B1(parameters[i]); |
| 236 |
a1[i] = right_tangent * B2(parameters[i]); |
| 237 |
} |
| 238 |
|
| 239 |
double c[2][2], x[2]; |
| 240 |
c[0][0] = c[0][1] = c[1][0] = c[1][1] = x[0] = x[1] = 0.0; |
| 241 |
|
| 242 |
int last = nd - 1; |
| 243 |
for (int i = 0; i < nd; i++) { |
| 244 |
c[0][0] += Point::dot(a0[i], a0[i]); |
| 245 |
c[0][1] += Point::dot(a0[i], a1[i]); |
| 246 |
c[1][1] += Point::dot(a1[i], a1[i]); |
| 247 |
|
| 248 |
Point tmp = d[i] - (d[0] * (B0(parameters[i]) + B1(parameters[i])) |
| 249 |
+ d[last] * (B2(parameters[i]) + B3(parameters[i]))); |
| 250 |
x[0] += Point::dot(a0[i], tmp); |
| 251 |
x[1] += Point::dot(a1[i], tmp); |
| 252 |
} |
| 253 |
c[1][0] = c[0][1]; |
| 254 |
|
| 255 |
// compute determinants |
| 256 |
double det_c0_c1 = c[0][0]*c[1][1] - c[1][0]*c[0][1]; |
| 257 |
double det_c0_x = c[0][0]*x[1] - c[0][1]*x[0]; |
| 258 |
double det_x_c1 = x[0]*c[1][1] - x[1]*c[0][1]; |
| 259 |
|
| 260 |
// finally, derive alpha values |
| 261 |
if (det_c0_c1 == 0.0) |
| 262 |
det_c0_c1 = c[0][0]*c[1][1] * 10e-12; |
| 263 |
double alpha_l = det_x_c1 / det_c0_c1; |
| 264 |
double alpha_r = det_c0_x / det_c0_c1; |
| 265 |
|
| 266 |
// if alpha negative, use the Wu/Barsky heuristic |
| 267 |
if (alpha_l < 0.0 || alpha_r < 0.0) { |
| 268 |
double distance = Point::distance(d[0], d[last]) / 3; |
| 269 |
return Bezier(d[0], d[0] + left_tangent*distance, |
| 270 |
d[last] + right_tangent*distance, d[last]); |
| 271 |
} else |
| 272 |
return Bezier(d[0], d[0] + left_tangent*alpha_l, |
| 273 |
d[last] + right_tangent*alpha_r, d[last]); |
| 274 |
} |
| 275 |
|
| 276 |
static double |
| 277 |
newton_raphson_root_find(const Bezier &b, const Point &p, double u) |
| 278 |
{ |
| 279 |
const Point *b_pts = b.points(); |
| 280 |
|
| 281 |
Point b_det[3]; |
| 282 |
for (int i = 0; i < 3; i++) |
| 283 |
b_det[i] = (b_pts[i+1] - b_pts[i]) * 3; |
| 284 |
|
| 285 |
Point b_det_det[2]; |
| 286 |
for (int i = 0; i < 2; i++) |
| 287 |
b_det_det[i] = (b_det[i+1] - b_det[i]) * 2; |
| 288 |
|
| 289 |
Point b_u = b.eval(u); |
| 290 |
Point b_det_u = eval_bezier(b_det, 2, u); |
| 291 |
Point b_det_det_u = eval_bezier(b_det_det, 1, u); |
| 292 |
|
| 293 |
double numerator = Point::dot(b_u - p, b_det_u); |
| 294 |
double denominator = Point::dot(b_det_u, b_det_u) + |
| 295 |
Point::dot(b_u - p, b_det_det_u); |
| 296 |
|
| 297 |
return u - numerator/denominator; |
| 298 |
} |
| 299 |
|
| 300 |
static void |
| 301 |
reparameterize(const Point *d, int nd, Vector<double> ¶meters, |
| 302 |
const Bezier &b) |
| 303 |
{ |
| 304 |
for (int i = 0; i < nd; i++) |
| 305 |
parameters[i] = newton_raphson_root_find(b, d[i], parameters[i]); |
| 306 |
} |
| 307 |
|
| 308 |
static double |
| 309 |
compute_max_error(const Point *d, int nd, const Bezier &b, |
| 310 |
const Vector<double> ¶meters, int *split_point) |
| 311 |
{ |
| 312 |
*split_point = nd/2; |
| 313 |
double max_dist = 0.0; |
| 314 |
for (int i = 1; i < nd - 1; i++) { |
| 315 |
double dist = (b.eval(parameters[i]) - d[i]).squared_length(); |
| 316 |
if (dist >= max_dist) { |
| 317 |
max_dist = dist; |
| 318 |
*split_point = i; |
| 319 |
} |
| 320 |
} |
| 321 |
return max_dist; |
| 322 |
} |
| 323 |
|
| 324 |
static void |
| 325 |
fit0(const Point *d, int nd, Point left_tangent, Point right_tangent, |
| 326 |
double error, Vector<Bezier> &result) |
| 327 |
{ |
| 328 |
// Use a heuristic for small regions (only two points) |
| 329 |
if (nd == 2) { |
| 330 |
double dist = Point::distance(d[0], d[1]) / 3; |
| 331 |
result.push_back(Bezier(d[0], |
| 332 |
d[0] + dist*left_tangent, |
| 333 |
d[1] + dist*right_tangent, |
| 334 |
d[1])); |
| 335 |
return; |
| 336 |
} |
| 337 |
|
| 338 |
// Parameterize points and attempt to fit curve |
| 339 |
Vector<double> parameters; |
| 340 |
chord_length_parameterize(d, nd, parameters); |
| 341 |
Bezier b = generate_bezier(d, nd, parameters, left_tangent, right_tangent); |
| 342 |
|
| 343 |
// find max error |
| 344 |
int split_point; |
| 345 |
double max_error = compute_max_error(d, nd, b, parameters, &split_point); |
| 346 |
if (max_error < error) { |
| 347 |
result.push_back(b); |
| 348 |
return; |
| 349 |
} |
| 350 |
|
| 351 |
// if error not too large, try iteration and reparameterization |
| 352 |
if (max_error < error*error) |
| 353 |
for (int i = 0; i < 4; i++) { |
| 354 |
reparameterize(d, nd, parameters, b); |
| 355 |
b = generate_bezier(d, nd, parameters, left_tangent, right_tangent); |
| 356 |
max_error = compute_max_error(d, nd, b, parameters, &split_point); |
| 357 |
if (max_error < error) { |
| 358 |
result.push_back(b); |
| 359 |
return; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
// fitting failed -- split at max error point and fit again |
| 364 |
Point center_tangent = ((d[split_point-1] - d[split_point+1])/2).normal(); |
| 365 |
fit0(d, split_point+1, left_tangent, center_tangent, error, result); |
| 366 |
fit0(d+split_point, nd-split_point, -center_tangent, right_tangent, error, result); |
| 367 |
} |
| 368 |
|
| 369 |
void |
| 370 |
Bezier::fit(const Vector<Point> &points, double error, Vector<Bezier> &result) |
| 371 |
{ |
| 372 |
int npoints = points.size(); |
| 373 |
Point left_tangent = (points[1] - points[0]).normal(); |
| 374 |
Point right_tangent = (points[npoints-2] - points[npoints-1]).normal(); |
| 375 |
fit0(&points[0], npoints, left_tangent, right_tangent, error, result); |
| 376 |
} |