summaryrefslogtreecommitdiff
path: root/Build/source/texk/lcdf-typetools/lcdf-typetools-src/liblcdf/bezier.cc
blob: c296a4b4aa375543c5abffd895440bd13f3feca0 (plain)
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// -*- related-file-name: "../include/lcdf/bezier.hh" -*-

/* bezier.{cc,hh} -- cubic Bezier curves
 *
 * Copyright (c) 1998-2019 Eddie Kohler
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version. This program is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <lcdf/bezier.hh>

//
// bounding box
//

void
Bezier::make_bb() const noexcept
{
    _bb = 0;
    for (int i = 1; i < 4; i++) {
	if (_p[i].x > bb_right_x())
	    _bb = (_bb & ~0x03) | (i << 0);
	else if (_p[i].x < bb_left_x())
	    _bb = (_bb & ~0x0C) | (i << 2);
	if (_p[i].y > bb_top_x())
	    _bb = (_bb & ~0x30) | (i << 4);
	else if (_p[i].y < bb_bottom_x())
	    _bb = (_bb & ~0xC0) | (i << 6);
    }
}


//
// is_flat, eval
//

bool
Bezier::is_flat(double t) const noexcept
{
    return (_p[2].on_segment(_p[0], _p[3], t)
	    && _p[1].on_segment(_p[0], _p[3], t));
}

static Point
eval_bezier(Point *b_in, int degree, double u)
{
    assert(degree < 4);
    Point b[4];
    for (int i = 0; i <= degree; i++)
	b[i] = b_in[i];

    double m = 1.0 - u;
    for (int i = 1; i <= degree; i++)
	for (int j = 0; j <= degree - i; j++)
	    b[j] = b[j]*m + b[j+1]*u;
    return b[0];
}

Point
Bezier::eval(double u) const noexcept
{
    Bezier b = *this;
    double m = 1.0 - u;
    for (int i = 1; i < 4; i++)
	for (int j = 0; j < 4 - i; j++)
	    b._p[j] = m * b._p[j] + u * b._p[j+1];
    return b._p[0];
}


//
// halve
//

void
Bezier::halve(Bezier &l, Bezier &r) const noexcept
{
    Point half = Point::midpoint(_p[1], _p[2]);
    l._p[0] = _p[0];
    l._p[1] = Point::midpoint(_p[0], _p[1]);
    l._p[2] = Point::midpoint(l._p[1], half);
    r._p[3] = _p[3];
    r._p[2] = Point::midpoint(_p[2], _p[3]);
    r._p[1] = Point::midpoint(r._p[2], half);
    r._p[0] = l._p[3] = Point::midpoint(l._p[2], r._p[1]);
}


//
// hit testing
//

bool
Bezier::in_bb(const Point &p, double tolerance) const noexcept
{
    ensure_bb();
    if (bb_right() + tolerance < p.x
	|| bb_left() - tolerance > p.x
	|| bb_top() + tolerance < p.y
	|| bb_bottom() - tolerance > p.y)
	return false;
    else
	return true;
}

double
Bezier::hit_recurse(const Point &p, double tolerance, double leftd,
		    double rightd, double leftt, double rightt) const noexcept
{
    Bezier left, right;
    double middled, resultt;

    if (is_flat(tolerance)) {
	if (p.on_segment(_p[0], _p[3], tolerance))
	    return (leftt + rightt) / 2;
	else
	    return -1;
    }

    if (leftd < tolerance * tolerance)
	return leftt;
    if (rightd < tolerance * tolerance)
	return rightt;

    if (!in_bb(p, tolerance))
	return -1;

    halve(left, right);
    middled = (right._p[0] - p).squared_length();
    resultt = left.hit_recurse
	(p, tolerance, leftd, middled, leftt, (leftt + rightt) / 2);
    if (resultt >= 0)
	return resultt;

    return right.hit_recurse
	(p, tolerance, middled, rightd, (leftt + rightt) / 2, rightt);
}

bool
Bezier::hit(const Point &p, double tolerance) const noexcept
{
    double leftd = (_p[0] - p).squared_length();
    double rightd = (_p[3] - p).squared_length();
    double resultt = hit_recurse(p, tolerance, leftd, rightd, 0, 1);
    return resultt >= 0;
}


//
// segmentize to list of points
//
// uses recursive subdivision
//

void
Bezier::segmentize(Vector<Point> &v, bool first) const
{
    if (is_flat(0.5)) {
	if (first)
	    v.push_back(_p[0]);
	v.push_back(_p[3]);
    } else {
	Bezier left, right;
	halve(left, right);
	left.segmentize(v, first);
	right.segmentize(v, false);
    }
}


//
// curve fitting
//
// code after Philip J. Schneider's algorithm described, with code, in the
// first Graphics Gems
//

static void
chord_length_parameterize(const Point *d, int nd, Vector<double> &result)
{
    assert(result.size() == 0);
    result.reserve(nd);
    result.push_back(0);
    for (int i = 1; i < nd; i++)
	result.push_back(result.back() + Point::distance(d[i-1], d[i]));
    double last_dist = result.back();
    for (int i = 1; i < nd; i++)
	result[i] /= last_dist;
}

static inline double
B0(double u)
{
    double m = 1.0 - u;
    return m*m*m;
}

static inline double
B1(double u)
{
    double m = 1.0 - u;
    return 3*m*m*u;
}

static inline double
B2(double u)
{
    double m = 1.0 - u;
    return 3*m*u*u;
}

static inline double
B3(double u)
{
    return u*u*u;
}

static Bezier
generate_bezier(const Point *d, int nd, const Vector<double> &parameters,
		const Point &left_tangent, const Point &right_tangent)
{
    Point *a0 = new Point[nd];
    Point *a1 = new Point[nd];

    for (int i = 0; i < nd; i++) {
	a0[i] = left_tangent * B1(parameters[i]);
	a1[i] = right_tangent * B2(parameters[i]);
    }

    double c[2][2], x[2];
    c[0][0] = c[0][1] = c[1][0] = c[1][1] = x[0] = x[1] = 0.0;

    int last = nd - 1;
    for (int i = 0; i < nd; i++) {
	c[0][0] += Point::dot(a0[i], a0[i]);
	c[0][1] += Point::dot(a0[i], a1[i]);
	c[1][1] += Point::dot(a1[i], a1[i]);

	Point tmp = d[i] - (d[0] * (B0(parameters[i]) + B1(parameters[i]))
			    + d[last] * (B2(parameters[i]) + B3(parameters[i])));
	x[0] += Point::dot(a0[i], tmp);
	x[1] += Point::dot(a1[i], tmp);
    }
    c[1][0] = c[0][1];

    // compute determinants
    double det_c0_c1 = c[0][0]*c[1][1] - c[1][0]*c[0][1];
    double det_c0_x = c[0][0]*x[1] - c[0][1]*x[0];
    double det_x_c1 = x[0]*c[1][1] - x[1]*c[0][1];

    // finally, derive alpha values
    if (det_c0_c1 == 0.0)
	det_c0_c1 = c[0][0]*c[1][1] * 10e-12;
    double alpha_l = det_x_c1 / det_c0_c1;
    double alpha_r = det_c0_x / det_c0_c1;

    // if alpha negative, use the Wu/Barsky heuristic
    if (alpha_l < 0.0 || alpha_r < 0.0) {
	double distance = Point::distance(d[0], d[last]) / 3;
	return Bezier(d[0], d[0] + left_tangent*distance,
		      d[last] + right_tangent*distance, d[last]);
    } else
	return Bezier(d[0], d[0] + left_tangent*alpha_l,
		      d[last] + right_tangent*alpha_r, d[last]);
}

static double
newton_raphson_root_find(const Bezier &b, const Point &p, double u)
{
    const Point *b_pts = b.points();

    Point b_det[3];
    for (int i = 0; i < 3; i++)
	b_det[i] = (b_pts[i+1] - b_pts[i]) * 3;

    Point b_det_det[2];
    for (int i = 0; i < 2; i++)
	b_det_det[i] = (b_det[i+1] - b_det[i]) * 2;

    Point b_u = b.eval(u);
    Point b_det_u = eval_bezier(b_det, 2, u);
    Point b_det_det_u = eval_bezier(b_det_det, 1, u);

    double numerator = Point::dot(b_u - p, b_det_u);
    double denominator = Point::dot(b_det_u, b_det_u) +
	Point::dot(b_u - p, b_det_det_u);

    return u - numerator/denominator;
}

static void
reparameterize(const Point *d, int nd, Vector<double> &parameters,
	       const Bezier &b)
{
    for (int i = 0; i < nd; i++)
	parameters[i] = newton_raphson_root_find(b, d[i], parameters[i]);
}

static double
compute_max_error(const Point *d, int nd, const Bezier &b,
		  const Vector<double> &parameters, int *split_point)
{
    *split_point = nd/2;
    double max_dist = 0.0;
    for (int i = 1; i < nd - 1; i++) {
	double dist = (b.eval(parameters[i]) - d[i]).squared_length();
	if (dist >= max_dist) {
	    max_dist = dist;
	    *split_point = i;
	}
    }
    return max_dist;
}

static void
fit0(const Point *d, int nd, Point left_tangent, Point right_tangent,
     double error, Vector<Bezier> &result)
{
    // Use a heuristic for small regions (only two points)
    if (nd == 2) {
	double dist = Point::distance(d[0], d[1]) / 3;
	result.push_back(Bezier(d[0],
				d[0] + dist*left_tangent,
				d[1] + dist*right_tangent,
				d[1]));
	return;
    }

    // Parameterize points and attempt to fit curve
    Vector<double> parameters;
    chord_length_parameterize(d, nd, parameters);
    Bezier b = generate_bezier(d, nd, parameters, left_tangent, right_tangent);

    // find max error
    int split_point;
    double max_error = compute_max_error(d, nd, b, parameters, &split_point);
    if (max_error < error) {
	result.push_back(b);
	return;
    }

    // if error not too large, try iteration and reparameterization
    if (max_error < error*error)
	for (int i = 0; i < 4; i++) {
	    reparameterize(d, nd, parameters, b);
	    b = generate_bezier(d, nd, parameters, left_tangent, right_tangent);
	    max_error = compute_max_error(d, nd, b, parameters, &split_point);
	    if (max_error < error) {
		result.push_back(b);
		return;
	    }
	}

    // fitting failed -- split at max error point and fit again
    Point center_tangent = ((d[split_point-1] - d[split_point+1])/2).normal();
    fit0(d, split_point+1, left_tangent, center_tangent, error, result);
    fit0(d+split_point, nd-split_point, -center_tangent, right_tangent, error, result);
}

void
Bezier::fit(const Vector<Point> &points, double error, Vector<Bezier> &result)
{
    int npoints = points.size();
    Point left_tangent = (points[1] - points[0]).normal();
    Point right_tangent = (points[npoints-2] - points[npoints-1]).normal();
    fit0(&points[0], npoints, left_tangent, right_tangent, error, result);
}