Debian Codegraph @

third_party/spiro/ppedit/bezctx_hittest.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 "zmisc.h"
22 #include "bezctx.h"
23 #include "bezctx_hittest.h"
24 #include <math.h>
25
26 typedef struct {
27 bezctx base;
28 double x, y;
29
30 double x0, y0;
31 int knot_idx;
32
33 int knot_idx_min;
34 double r_min;
35 } bezctx_hittest;
36
37 static void
38 bezctx_hittest_moveto(bezctx *z, double x, double y, int is_open) {
39 bezctx_hittest *bc = (bezctx_hittest *)z;
40
41 bc->x0 = x;
42 bc->y0 = y;
43 }
44
45 static void
46 bezctx_hittest_lineto(bezctx *z, double x, double y) {
47 bezctx_hittest *bc = (bezctx_hittest *)z;
48 double x0 = bc->x0;
49 double y0 = bc->y0;
50 double dx = x - x0;
51 double dy = y - y0;
52 double dotp = (bc->x - x0) * dx + (bc->y - y0) * dy;
53 double lin_dotp = dx * dx + dy * dy;
54 double r_min, r;
55
56 r = hypot(bc->x - x0, bc->y - y0);
57 r_min = r;
58 r = hypot(bc->x - x, bc->y - y);
59 if (r < r_min) r_min = r;
60
61 if (dotp >= 0 && dotp <= lin_dotp) {
62 double norm = (bc->x - x0) * dy - (bc->y - y0) * dx;
63 r = fabs(norm / sqrt(lin_dotp));
64 if (r < r_min) r_min = r;
65 }
66
67 if (r_min < bc->r_min) {
68 bc->r_min = r_min;
69 bc->knot_idx_min = bc->knot_idx;
70 }
71
72 bc->x0 = x;
73 bc->y0 = y;
74 }
75
76 #define cube(x) ((x) * (x) * (x))
77
78 static double
79 my_cbrt(double x)
80 {
81 if (x >= 0)
82 return pow(x, 1.0 / 3.0);
83 else
84 return -pow(-x, 1.0 / 3.0);
85 }
86
87 /**
88 * Give real roots to eqn c0 + c1 * x + c2 * x^2 + c3 * x^3 == 0.
89 * Return value is number of roots found.
90 **/
91 static int
92 solve_cubic(double c0, double c1, double c2, double c3, double root[3])
93 {
94 double p, q, r, a, b, Q, x0;
95
96 p = c2 / c3;
97 q = c1 / c3;
98 r = c0 / c3;
99 a = (3 * q - p * p) / 3;
100 b = (2 * cube(p) - 9 * p * q + 27 * r) / 27;
101 Q = b * b / 4 + cube(a) / 27;
102 x0 = p / 3;
103 if (Q > 0) {
104 double sQ = sqrt(Q);
105 double t1 = my_cbrt(-b/2 + sQ) + my_cbrt(-b/2 - sQ);
106 root[0] = t1 - x0;
107 return 1;
108 } else if (Q == 0) {
109 double t1 = my_cbrt(b / 2);
110 double x1 = t1 - x0;
111 root[0] = x1;
112 root[1] = x1;
113 root[2] = -2 * t1 - x0;
114 return 3;
115 } else {
116 double sQ = sqrt(-Q);
117 double rho = hypot(-b/2, sQ);
118 double th = atan2(sQ, -b/2);
119 double cbrho = my_cbrt(rho);
120 double c = cos(th / 3);
121 double s = sin(th / 3);
122 double sqr3 = sqrt(3);
123 root[0] = 2 * cbrho * c - x0;
124 root[1] = -cbrho * (c + sqr3 * s) - x0;
125 root[2] = -cbrho * (c - sqr3 * s) - x0;
126 return 3;
127 }
128 }
129
130
131 static double
132 dist_to_quadratic(double x, double y,
133 double x0, double y0,
134 double x1, double y1,
135 double x2, double y2)
136 {
137 double u0, u1, t0, t1, t2, c0, c1, c2, c3;
138 double roots[3];
139 int n_roots;
140 double ts[4];
141 int n_ts;
142 int i;
143 double minerr = 0;
144
145 u0 = x1 - x0;
146 u1 = x0 - 2 * x1 + x2;
147 t0 = x0 - x;
148 t1 = 2 * u0;
149 t2 = u1;
150 c0 = t0 * u0;
151 c1 = t1 * u0 + t0 * u1;
152 c2 = t2 * u0 + t1 * u1;
153 c3 = t2 * u1;
154
155 u0 = y1 - y0;
156 u1 = y0 - 2 * y1 + y2;
157 t0 = y0 - y;
158 t1 = 2 * u0;
159 t2 = u1;
160 c0 += t0 * u0;
161 c1 += t1 * u0 + t0 * u1;
162 c2 += t2 * u0 + t1 * u1;
163 c3 += t2 * u1;
164
165 n_roots = solve_cubic(c0, c1, c2, c3, roots);
166 n_ts = 0;
167 for (i = 0; i < n_roots; i++) {
168 double t = roots[i];
169 if (t > 0 && t < 1)
170 ts[n_ts++] = t;
171 }
172 if (n_ts < n_roots) {
173 ts[n_ts++] = 0;
174 ts[n_ts++] = 1;
175 }
176 for (i = 0; i < n_ts; i++) {
177 double t = ts[i];
178 double xa = x0 * (1 - t) * (1 - t) + 2 * x1 * (1 - t) * t + x2 * t * t;
179 double ya = y0 * (1 - t) * (1 - t) + 2 * y1 * (1 - t) * t + y2 * t * t;
180 double err = hypot(xa - x, ya - y);
181 if (i == 0 || err < minerr) {
182 minerr = err;
183 }
184 }
185 return minerr;
186 }
187
188 static void
189 bezctx_hittest_quadto(bezctx *z, double x1, double y1, double x2, double y2)
190 {
191 bezctx_hittest *bc = (bezctx_hittest *)z;
192 double r = dist_to_quadratic(bc->x, bc->y,
193 bc->x0, bc->y0, x1, y1, x2, y2);
194
195 if (r < bc->r_min) {
196 bc->r_min = r;
197 bc->knot_idx_min = bc->knot_idx;
198 }
199 bc->x0 = x2;
200 bc->y0 = y2;
201 }
202
203 static void
204 bezctx_hittest_curveto(bezctx *z, double x1, double y1, double x2, double y2,
205 double x3, double y3)
206 {
207 bezctx_hittest *bc = (bezctx_hittest *)z;
208 double x0 = bc->x0;
209 double y0 = bc->y0;
210 int n_subdiv = 32;
211 int i;
212 double xq2, yq2;
213
214 /* todo: subdivide to quadratics rather than lines */
215 for (i = 0; i < n_subdiv; i++) {
216 double t = (1. / n_subdiv) * (i + 1);
217 double mt = 1 - t;
218
219 xq2 = x0 * mt * mt * mt + 3 * x1 * mt * t * t + 3 * x2 * mt * mt * t +
220 x3 * t * t * t;
221 yq2 = y0 * mt * mt * mt + 3 * y1 * mt * t * t + 3 * y2 * mt * mt * t +
222 y3 * t * t * t;
223 bezctx_hittest_lineto(z, xq2, yq2);
224 }
225 }
226
227 static void
228 bezctx_hittest_mark_knot(bezctx *z, int knot_idx) {
229 bezctx_hittest *bc = (bezctx_hittest *)z;
230
231 bc->knot_idx = knot_idx;
232 }
233
234 bezctx *
235 new_bezctx_hittest(double x, double y) {
236 bezctx_hittest *result = znew(bezctx_hittest, 1);
237
238 result->base.moveto = bezctx_hittest_moveto;
239 result->base.lineto = bezctx_hittest_lineto;
240 result->base.quadto = bezctx_hittest_quadto;
241 result->base.curveto = bezctx_hittest_curveto;
242 result->base.mark_knot = bezctx_hittest_mark_knot;
243 result->x = x;
244 result->y = y;
245 result->knot_idx_min = -1;
246 result->r_min = 1e12;
247 return &result->base;
248 }
249
250 double
251 bezctx_hittest_report(bezctx *z, int *p_knot_idx)
252 {
253 bezctx_hittest *bc = (bezctx_hittest *)z;
254 double r_min = bc->r_min;
255
256 if (p_knot_idx)
257 *p_knot_idx = bc->knot_idx_min;
258
259 zfree(z);
260 return r_min;
261 }