Debian Codegraph @

third_party/spiro/ppedit/cornu.c

raw symbols SCIP
1 /*
2 ppedit - A pattern plate editor for Spiro splines.
3 Copyright (C) 2007 Raph Levien
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301, USA.
19
20 */
21 #include <math.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include "bezctx_intf.h"
25
26 /* The computation of fresnel integrals is adapted from: */
27
28 /*
29 Cephes Math Library Release 2.1: December, 1988
30 Copyright 1984, 1987, 1988 by Stephen L. Moshier
31 Direct inquiries to 30 Frost Street, Cambridge, MA 02140
32 */
33
34 double polevl( double x, double coef[], int N )
35 {
36 double ans;
37 int i;
38 double *p;
39
40 p = coef;
41 ans = *p++;
42 i = N;
43
44 do
45 ans = ans * x + *p++;
46 while( --i );
47
48 return( ans );
49 }
50
51 /* p1evl() */
52 /* N
53 * Evaluate polynomial when coefficient of x is 1.0.
54 * Otherwise same as polevl.
55 */
56
57 double p1evl( double x, double coef[], int N )
58 {
59 double ans;
60 double *p;
61 int i;
62
63 p = coef;
64 ans = x + *p++;
65 i = N-1;
66
67 do
68 ans = ans * x + *p++;
69 while( --i );
70
71 return( ans );
72 }
73
74 static double sn[6] = {
75 -2.99181919401019853726E3,
76 7.08840045257738576863E5,
77 -6.29741486205862506537E7,
78 2.54890880573376359104E9,
79 -4.42979518059697779103E10,
80 3.18016297876567817986E11,
81 };
82 static double sd[6] = {
83 /* 1.00000000000000000000E0,*/
84 2.81376268889994315696E2,
85 4.55847810806532581675E4,
86 5.17343888770096400730E6,
87 4.19320245898111231129E8,
88 2.24411795645340920940E10,
89 6.07366389490084639049E11,
90 };
91
92 static double cn[6] = {
93 -4.98843114573573548651E-8,
94 9.50428062829859605134E-6,
95 -6.45191435683965050962E-4,
96 1.88843319396703850064E-2,
97 -2.05525900955013891793E-1,
98 9.99999999999999998822E-1,
99 };
100 static double cd[7] = {
101 3.99982968972495980367E-12,
102 9.15439215774657478799E-10,
103 1.25001862479598821474E-7,
104 1.22262789024179030997E-5,
105 8.68029542941784300606E-4,
106 4.12142090722199792936E-2,
107 1.00000000000000000118E0,
108 };
109 static double fn[10] = {
110 4.21543555043677546506E-1,
111 1.43407919780758885261E-1,
112 1.15220955073585758835E-2,
113 3.45017939782574027900E-4,
114 4.63613749287867322088E-6,
115 3.05568983790257605827E-8,
116 1.02304514164907233465E-10,
117 1.72010743268161828879E-13,
118 1.34283276233062758925E-16,
119 3.76329711269987889006E-20,
120 };
121 static double fd[10] = {
122 /* 1.00000000000000000000E0,*/
123 7.51586398353378947175E-1,
124 1.16888925859191382142E-1,
125 6.44051526508858611005E-3,
126 1.55934409164153020873E-4,
127 1.84627567348930545870E-6,
128 1.12699224763999035261E-8,
129 3.60140029589371370404E-11,
130 5.88754533621578410010E-14,
131 4.52001434074129701496E-17,
132 1.25443237090011264384E-20,
133 };
134 static double gn[11] = {
135 5.04442073643383265887E-1,
136 1.97102833525523411709E-1,
137 1.87648584092575249293E-2,
138 6.84079380915393090172E-4,
139 1.15138826111884280931E-5,
140 9.82852443688422223854E-8,
141 4.45344415861750144738E-10,
142 1.08268041139020870318E-12,
143 1.37555460633261799868E-15,
144 8.36354435630677421531E-19,
145 1.86958710162783235106E-22,
146 };
147 static double gd[11] = {
148 /* 1.00000000000000000000E0,*/
149 1.47495759925128324529E0,
150 3.37748989120019970451E-1,
151 2.53603741420338795122E-2,
152 8.14679107184306179049E-4,
153 1.27545075667729118702E-5,
154 1.04314589657571990585E-7,
155 4.60680728146520428211E-10,
156 1.10273215066240270757E-12,
157 1.38796531259578871258E-15,
158 8.39158816283118707363E-19,
159 1.86958710162783236342E-22,
160 };
161
162 #ifndef M_PI
163 #define M_PI 3.14159265358979323846 /* pi */
164 #endif
165 #ifndef M_PI_2
166 #define M_PI_2 1.57079632679489661923 /* pi/2 */
167 #endif
168
169 int fresnl( xxa, ssa, cca )
170 double xxa, *ssa, *cca;
171 {
172 double f, g, cc, ss, c, s, t, u;
173 double x, x2;
174
175 x = fabs(xxa);
176 x2 = x * x;
177 if( x2 < 2.5625 )
178 {
179 t = x2 * x2;
180 ss = x * x2 * polevl( t, sn, 5)/p1evl( t, sd, 6 );
181 cc = x * polevl( t, cn, 5)/polevl(t, cd, 6 );
182 goto done;
183 }
184
185
186
187
188
189 #if 0
190 /* Note by RLL: the cutoff here seems low to me; perhaps it should be
191 eliminated altogether. */
192 if( x > 36974.0 )
193 {
194 cc = 0.5;
195 ss = 0.5;
196 goto done;
197 }
198 #endif
199
200 /* Asymptotic power series auxiliary functions
201 * for large argument
202 */
203 x2 = x * x;
204 t = M_PI * x2;
205 u = 1.0/(t * t);
206 t = 1.0/t;
207 f = 1.0 - u * polevl( u, fn, 9)/p1evl(u, fd, 10);
208 g = t * polevl( u, gn, 10)/p1evl(u, gd, 11);
209
210 t = M_PI_2 * x2;
211 c = cos(t);
212 s = sin(t);
213 t = M_PI * x;
214 cc = 0.5 + (f * s - g * c)/t;
215 ss = 0.5 - (f * c + g * s)/t;
216
217 done:
218 if( xxa < 0.0 )
219 {
220 cc = -cc;
221 ss = -ss;
222 }
223
224 *cca = cc;
225 *ssa = ss;
226 return(0);
227 }
228
229 /*
230 End section adapted from Cephes math library. The following code is
231 by Raph Levien.
232 */
233
234 void eval_cornu(double t, double *ps, double *pc)
235 {
236 double s, c;
237 double rspio2 = 0.7978845608028653; /* 1 / sqrt(pi/2) */
238 double spio2 = 1.2533141373155; /* sqrt(pi/2) */
239
240 fresnl(t * rspio2, &s, &c);
241 *ps = s * spio2;
242 *pc = c * spio2;
243 }
244
245 double mod_2pi(double th) {
246 double u = th * (1 / (2 * M_PI));
247 return 2 * M_PI * (u - floor(u + 0.5));
248 }
249
250 void fit_cornu_half(double th0, double th1,
251 double *pt0, double *pt1,
252 double *pk0, double *pk1)
253 {
254 int i;
255 const int max_iter = 21;
256 double t0, t1, t_m;
257 double tl, tr, t_est;
258 double s0, c0, s1, c1;
259 /* This implementation uses bisection, which is simple but almost
260 certainly not the fastest converging. If time is of the essence,
261 use something like Newton-Raphson. */
262
263 if (fabs(th0 + th1) < 1e-6) {
264 th0 += 1e-6;
265 th1 += 1e-6;
266 }
267 t_est = 0.29112 * (th1 + th0) / sqrt(th1 - th0);
268 tl = t_est * .9;
269 tr = t_est * 2;
270 for (i = 0; i < max_iter; i++) {
271 double dt;
272 double chord_th;
273
274 t_m = .5 * (tl + tr);
275 dt = (th0 + th1) / (4 * t_m);
276 t0 = t_m - dt;
277 t1 = t_m + dt;
278 eval_cornu(t0, &s0, &c0);
279 eval_cornu(t1, &s1, &c1);
280 chord_th = atan2(s1 - s0, c1 - c0);
281 if (mod_2pi(chord_th - t0 * t0 - th0) < 0)
282 tl = t_m;
283 else
284 tr = t_m;
285 }
286 *pt0 = t0;
287 *pt1 = t1;
288 if (pk0 || pk1) {
289 double chordlen = hypot(s1 - s0, c1 - c0);
290 if (pk0) *pk0 = t0 * chordlen;
291 if (pk1) *pk1 = t1 * chordlen;
292 }
293 }
294
295 /* Most of the time, this should give a fairly tight, yet conservative,
296 (meaning it won't underestimate) approximation of the maximum error
297 between a Cornu spiral segment and its quadratic Bezier fit.
298 */
299 double
300 est_cornu_error(double t0, double t1)
301 {
302 double t, u, est;
303
304 if (t0 < 0 || t1 < 0) {
305 t0 = -t0;
306 t1 = -t1;
307 }
308 if (t1 < 0) {
309 fprintf(stderr, "unexpected t1 sign\n");
310 }
311 if (t1 < t0) {
312 double tmp = t0;
313 t0 = t1;
314 t1 = tmp;
315 }
316 if (fabs(t0) < 1e-9) {
317 est = t1 * t1 * t1;
318 est *= .017256 - .0059 - est * t1;
319 } else {
320 t = t1 - t0;
321 t *= t;
322 t *= t;
323 est = t * fabs(t0 + t1 - 1.22084) / (t0 + t1);
324 u = t0 + t1 + .6;
325 u = u * u * u;
326 est *= .014 * (.6 * u + 1);
327 est += t * (t1 - t0) * .004;
328 }
329 return est;
330 }
331
332 void
333 affine_pt(const double aff[6], double x, double y, double *px, double *py) {
334 *px = x * aff[0] + y * aff[2] + aff[4];
335 *py = x * aff[1] + y * aff[3] + aff[5];
336 }
337
338 void
339 affine_multiply(double dst[6], const double src1[6], const double src2[6])
340 {
341 double d0, d1, d2, d3, d4, d5;
342 d0 = src1[0] * src2[0] + src1[1] * src2[2];
343 d1 = src1[0] * src2[1] + src1[1] * src2[3];
344 d2 = src1[2] * src2[0] + src1[3] * src2[2];
345 d3 = src1[2] * src2[1] + src1[3] * src2[3];
346 d4 = src1[4] * src2[0] + src1[5] * src2[2] + src2[4];
347 d5 = src1[4] * src2[1] + src1[5] * src2[3] + src2[5];
348 dst[0] = d0;
349 dst[1] = d1;
350 dst[2] = d2;
351 dst[3] = d3;
352 dst[4] = d4;
353 dst[5] = d5;
354 }
355
356 void
357 fit_quadratic(double x0, double y0, double th0,
358 double x1, double y1, double th1,
359 double quad[6]) {
360 double th;
361 double s0, c0, s1, c1;
362 double det, s, c;
363
364 th = atan2(y1 - y0, x1 - x0);
365 s0 = sin(th0 - th);
366 c0 = cos(th0 - th);
367 s1 = sin(th - th1);
368 c1 = cos(th - th1);
369 det = 1 / (s0 * c1 + s1 * c0);
370 s = s0 * s1 * det;
371 c = c0 * s1 * det;
372 quad[0] = x0;
373 quad[1] = y0;
374 quad[2] = x0 + (x1 - x0) * c - (y1 - y0) * s;
375 quad[3] = y0 + (y1 - y0) * c + (x1 - x0) * s;
376 quad[4] = x1;
377 quad[5] = y1;
378 }
379
380 void
381 cornu_seg_to_quad(double t0, double t1, const double aff[6], bezctx *bc)
382 {
383 double x0, y0;
384 double x1, y1;
385 double th0 = t0 * t0;
386 double th1 = t1 * t1;
387 double quad[6];
388 double qx1, qy1, qx2, qy2;
389
390 eval_cornu(t0, &y0, &x0);
391 eval_cornu(t1, &y1, &x1);
392 fit_quadratic(x0, y0, th0, x1, y1, th1, quad);
393 affine_pt(aff, quad[2], quad[3], &qx1, &qy1);
394 affine_pt(aff, quad[4], quad[5], &qx2, &qy2);
395 bezctx_quadto(bc, qx1, qy1, qx2, qy2);
396 }
397
398 void
399 cornu_seg_to_bpath(double t0, double t1, const double aff[6],
400 bezctx *bc, double tol)
401 {
402 double tm;
403
404 if ((t0 < 0 && t1 > 0) || (t1 < 0 && t0 > 0))
405 tm = 0;
406 else {
407 if (fabs(t0 * t0 - t1 * t1) < 1.5 &&
408 est_cornu_error(t0, t1) < tol) {
409 cornu_seg_to_quad(t0, t1, aff, bc);
410 return;
411 }
412 #if 0
413 if (fabs(t0 - t1) < 1e-6) {
414 printf("DIVERGENCE!\007\n");
415 return;
416
417 }
418 #endif
419 tm = (t0 + t1) * .5;
420 }
421
422 cornu_seg_to_bpath(t0, tm, aff, bc, tol);
423 cornu_seg_to_bpath(tm, t1, aff, bc, tol);
424 }
425
426 void
427 cornu_to_bpath(const double xs[], const double ys[], const double ths[], int n,
428 bezctx *bc, double tol, int closed, int kt0, int n_kt)
429 {
430 int i;
431
432 for (i = 0; i < n - 1 + closed; i++) {
433 double x0 = xs[i], y0 = ys[i];
434 int ip1 = (i + 1) % n;
435 double x1 = xs[ip1], y1 = ys[ip1];
436 double th = atan2(y1 - y0, x1 - x0);
437 double th0 = mod_2pi(ths[i] - th);
438 double th1 = mod_2pi(th - ths[ip1]);
439 double t0, t1;
440 double s0, c0, s1, c1;
441 double chord_th, chordlen, rot, scale;
442 double aff[6], aff2[6];
443 double flip = -1;
444
445 th1 += 1e-6;
446 if (th1 < th0) {
447 double tmp = th0;
448 th0 = th1;
449 th1 = tmp;
450 flip = 1;
451 }
452 fit_cornu_half(th0, th1, &t0, &t1, NULL, NULL);
453 if (flip == 1) {
454 double tmp = t0;
455 t0 = t1;
456 t1 = tmp;
457 }
458 eval_cornu(t0, &s0, &c0);
459 s0 *= flip;
460 eval_cornu(t1, &s1, &c1);
461 s1 *= flip;
462 chord_th = atan2(s1 - s0, c1 - c0);
463 chordlen = hypot(s1 - s0, c1 - c0);
464 rot = th - chord_th;
465 scale = hypot(y1 - y0, x1 - x0) / chordlen;
466 aff[0] = 1;
467 aff[1] = 0;
468 aff[2] = 0;
469 aff[3] = flip;
470 aff[4] = -c0;
471 aff[5] = -s0;
472 aff2[0] = scale * cos(rot);
473 aff2[1] = scale * sin(rot);
474 aff2[2] = -aff2[1];
475 aff2[3] = aff2[0];
476 aff2[4] = x0;
477 aff2[5] = y0;
478 affine_multiply(aff, aff, aff2);
479 bezctx_mark_knot(bc, (kt0 + i) % n_kt);
480 cornu_seg_to_bpath(t0, t1, aff, bc, tol / scale);
481 }
482 }
483
484 /* fit arc to pts (0, 0), (x, y), and (1, 0), return th tangent to
485 arc at (x, y) */
486 double fit_arc(double x, double y) {
487 return atan2(y - 2 * x * y, y * y + x - x * x);
488 }
489
490 void
491 local_ths(const double xs[], const double ys[], double ths[], int n,
492 int closed)
493 {
494 int i;
495
496 for (i = 1 - closed; i < n - 1 + closed; i++) {
497 int im1 = (i + n - 1) % n;
498 double x0 = xs[im1];
499 double y0 = ys[im1];
500 double x1 = xs[i];
501 double y1 = ys[i];
502 int ip1 = (i + 1) % n;
503 double x2 = xs[ip1];
504 double y2 = ys[ip1];
505 double dx = x2 - x0;
506 double dy = y2 - y0;
507 double ir2 = dx * dx + dy * dy;
508 double x = ((x1 - x0) * dx + (y1 - y0) * dy) / ir2;
509 double y = ((y1 - y0) * dx - (x1 - x0) * dy) / ir2;
510 if (dx == 0.0 && dy == 0.0)
511 ths[i] = 0.0;
512 else
513 ths[i] = fit_arc(x, y) + atan2(dy, dx);
514 }
515 }
516
517 void
518 endpoint_ths(const double xs[], const double ys[], double ths[], int n)
519 {
520 ths[0] = 2 * atan2(ys[1] - ys[0], xs[1] - xs[0]) - ths[1];
521 ths[n - 1] = 2 * atan2(ys[n - 1] - ys[n - 2], xs[n - 1] - xs[n-2]) - ths[n - 2];
522 }
523
524 void
525 tweak_ths(const double xs[], const double ys[], double ths[], int n,
526 double delt, int closed)
527 {
528 double *dks = (double *)malloc(sizeof(double) * n);
529 int i;
530 double first_k0, last_k1;
531
532 for (i = 0; i < n - 1 + closed; i++) {
533 double x0 = xs[i];
534 double y0 = ys[i];
535 int ip1 = (i + 1) % n;
536 double x1 = xs[ip1];
537 double y1 = ys[ip1];
538 double th, th0, th1;
539 double t0, t1, k0, k1;
540 double s0, c0, s1, c1;
541 double scale;
542 double flip = -1;
543
544 if (x0 == x1 && y0 == y1) {
545 #ifdef VERBOSE
546 printf("Overlapping points (i=%d)\n", i);
547 #endif
548 /* Very naughty, casting off the constness like this. */
549 ((double*) xs)[i] = x1 = x1 + 1e-6;
550 }
551
552 th = atan2(y1 - y0, x1 - x0);
553 th0 = mod_2pi(ths[i] - th);
554 th1 = mod_2pi(th - ths[ip1]);
555
556 th1 += 1e-6;
557 if (th1 < th0) {
558 double tmp = th0;
559 th0 = th1;
560 th1 = tmp;
561 flip = 1;
562 }
563 fit_cornu_half(th0, th1, &t0, &t1, &k0, &k1);
564 if (flip == 1) {
565 double tmp = t0;
566 t0 = t1;
567 t1 = tmp;
568
569 tmp = k0;
570 k0 = k1;
571 k1 = tmp;
572 }
573 eval_cornu(t0, &s0, &c0);
574 eval_cornu(t1, &s1, &c1);
575 scale = 1 / hypot(y1 - y0, x1 - x0);
576 k0 *= scale;
577 k1 *= scale;
578 if (i > 0) dks[i] = k0 - last_k1;
579 else first_k0 = k0;
580 last_k1 = k1;
581 }
582 if (closed)
583 dks[0] = first_k0 - last_k1;
584 for (i = 1 - closed; i < n - 1 + closed; i++) {
585 int im1 = (i + n - 1) % n;
586 double x0 = xs[im1];
587 double y0 = ys[im1];
588 double x1 = xs[i];
589 double y1 = ys[i];
590 int ip1 = (i + 1) % n;
591 double x2 = xs[ip1];
592 double y2 = ys[ip1];
593 double chord1 = hypot(x1 - x0, y1 - y0);
594 double chord2 = hypot(x2 - x1, y2 - y1);
595 ths[i] -= delt * dks[i] * chord1 * chord2 / (chord1 + chord2);
596 }
597 free(dks);
598 }
599
600 void test_cornu(void)
601 {
602 #if 0
603 int i;
604 for (i = -10; i < 100; i++) {
605 double t = 36974 * 1.2533141373155 + i * .1;
606 double s, c;
607 eval_cornu(t, &s, &c);
608 printf("%g %g %g\n", t, s, c);
609 }
610 #else
611 double t0, t1;
612 fit_cornu_half(0, 1, &t0, &t1, 0, 0);
613 printf("%g %g\n", t0, t1);
614 #endif
615 }