blob: bb757711154efde9c3135d4213aba678adcbb3ca [file] [log] [blame]
caryclark@google.comc6825902012-02-03 22:07:47 +00001#include "CurveIntersection.h"
2#include "CubicUtilities.h"
caryclark@google.com639df892012-01-10 21:46:10 +00003
4/* from http://tom.cs.byu.edu/~tom/papers/cvgip84.pdf 4.1
5 *
6 * This paper proves that Syvester's method can compute the implicit form of
7 * the quadratic from the parameterzied form.
8 *
9 * Given x = a*t*t*t + b*t*t + c*t + d (the parameterized form)
10 * y = e*t*t*t + f*t*t + g*t + h
11 *
12 * we want to find an equation of the implicit form:
13 *
14 * A*x^3 + B*x*x*y + C*x*y*y + D*y^3 + E*x*x + F*x*y + G*y*y + H*x + I*y + J = 0
15 *
16 * The implicit form can be expressed as a 6x6 determinant, as shown.
17 *
18 * The resultant obtained by Syvester's method is
19 *
20 * | a b c (d - x) 0 0 |
21 * | 0 a b c (d - x) 0 |
22 * | 0 0 a b c (d - x) |
23 * | e f g (h - y) 0 0 |
24 * | 0 e f g (h - y) 0 |
25 * | 0 0 e f g (h - y) |
26 *
27 * which, according to Mathematica, expands as shown below.
28 *
29 * Resultant[a*t^3 + b*t^2 + c*t + d - x, e*t^3 + f*t^2 + g*t + h - y, t]
30 *
31 * -d^3 e^3 + c d^2 e^2 f - b d^2 e f^2 + a d^2 f^3 - c^2 d e^2 g +
32 * 2 b d^2 e^2 g + b c d e f g - 3 a d^2 e f g - a c d f^2 g -
33 * b^2 d e g^2 + 2 a c d e g^2 + a b d f g^2 - a^2 d g^3 + c^3 e^2 h -
34 * 3 b c d e^2 h + 3 a d^2 e^2 h - b c^2 e f h + 2 b^2 d e f h +
35 * a c d e f h + a c^2 f^2 h - 2 a b d f^2 h + b^2 c e g h -
36 * 2 a c^2 e g h - a b d e g h - a b c f g h + 3 a^2 d f g h +
37 * a^2 c g^2 h - b^3 e h^2 + 3 a b c e h^2 - 3 a^2 d e h^2 +
38 * a b^2 f h^2 - 2 a^2 c f h^2 - a^2 b g h^2 + a^3 h^3 + 3 d^2 e^3 x -
39 * 2 c d e^2 f x + 2 b d e f^2 x - 2 a d f^3 x + c^2 e^2 g x -
40 * 4 b d e^2 g x - b c e f g x + 6 a d e f g x + a c f^2 g x +
41 * b^2 e g^2 x - 2 a c e g^2 x - a b f g^2 x + a^2 g^3 x +
42 * 3 b c e^2 h x - 6 a d e^2 h x - 2 b^2 e f h x - a c e f h x +
43 * 2 a b f^2 h x + a b e g h x - 3 a^2 f g h x + 3 a^2 e h^2 x -
44 * 3 d e^3 x^2 + c e^2 f x^2 - b e f^2 x^2 + a f^3 x^2 +
45 * 2 b e^2 g x^2 - 3 a e f g x^2 + 3 a e^2 h x^2 + e^3 x^3 -
46 * c^3 e^2 y + 3 b c d e^2 y - 3 a d^2 e^2 y + b c^2 e f y -
47 * 2 b^2 d e f y - a c d e f y - a c^2 f^2 y + 2 a b d f^2 y -
48 * b^2 c e g y + 2 a c^2 e g y + a b d e g y + a b c f g y -
49 * 3 a^2 d f g y - a^2 c g^2 y + 2 b^3 e h y - 6 a b c e h y +
50 * 6 a^2 d e h y - 2 a b^2 f h y + 4 a^2 c f h y + 2 a^2 b g h y -
51 * 3 a^3 h^2 y - 3 b c e^2 x y + 6 a d e^2 x y + 2 b^2 e f x y +
52 * a c e f x y - 2 a b f^2 x y - a b e g x y + 3 a^2 f g x y -
53 * 6 a^2 e h x y - 3 a e^2 x^2 y - b^3 e y^2 + 3 a b c e y^2 -
54 * 3 a^2 d e y^2 + a b^2 f y^2 - 2 a^2 c f y^2 - a^2 b g y^2 +
55 * 3 a^3 h y^2 + 3 a^2 e x y^2 - a^3 y^3
56 */
57
58enum {
caryclark@google.comc6825902012-02-03 22:07:47 +000059 xxx_coeff, // A
60 xxy_coeff, // B
61 xyy_coeff, // C
62 yyy_coeff, // D
caryclark@google.com639df892012-01-10 21:46:10 +000063 xx_coeff,
64 xy_coeff,
65 yy_coeff,
66 x_coeff,
67 y_coeff,
68 c_coeff,
69 coeff_count
70};
71
caryclark@google.comc6825902012-02-03 22:07:47 +000072#define USE_SYVESTER 0 // if 0, use control-point base parametric form
73#if USE_SYVESTER
74
caryclark@google.com639df892012-01-10 21:46:10 +000075// FIXME: factoring version unwritten
76// static bool straight_forward = true;
77
78/* from CubicParameterizationCode.cpp output:
79 * double A = e * e * e;
80 * double B = -3 * a * e * e;
81 * double C = 3 * a * a * e;
82 * double D = -a * a * a;
83 */
84static void calc_ABCD(double a, double e, double p[coeff_count]) {
85 double ee = e * e;
86 p[xxx_coeff] = e * ee;
87 p[xxy_coeff] = -3 * a * ee;
88 double aa = a * a;
89 p[xyy_coeff] = 3 * aa * e;
90 p[yyy_coeff] = -aa * a;
91}
92
93/* CubicParameterizationCode.cpp turns Mathematica output into C.
94 * Rather than edit the lines below, please edit the code there instead.
95 */
96// start of generated code
caryclark@google.comc6825902012-02-03 22:07:47 +000097static double calc_xx(double a, double b, double c, double d,
caryclark@google.com639df892012-01-10 21:46:10 +000098 double e, double f, double g, double h) {
99 return
100 -3 * d * e * e * e
101 + c * e * e * f
102 - b * e * f * f
103 + a * f * f * f
104 + 2 * b * e * e * g
105 - 3 * a * e * f * g
106 + 3 * a * e * e * h;
107}
108
caryclark@google.comc6825902012-02-03 22:07:47 +0000109static double calc_xy(double a, double b, double c, double d,
caryclark@google.com639df892012-01-10 21:46:10 +0000110 double e, double f, double g, double h) {
111 return
112 -3 * b * c * e * e
113 + 6 * a * d * e * e
114 + 2 * b * b * e * f
115 + a * c * e * f
116 - 2 * a * b * f * f
117 - a * b * e * g
118 + 3 * a * a * f * g
119 - 6 * a * a * e * h;
120}
121
caryclark@google.comc6825902012-02-03 22:07:47 +0000122static double calc_yy(double a, double b, double c, double d,
caryclark@google.com639df892012-01-10 21:46:10 +0000123 double e, double f, double g, double h) {
124 return
125 -b * b * b * e
126 + 3 * a * b * c * e
127 - 3 * a * a * d * e
128 + a * b * b * f
129 - 2 * a * a * c * f
130 - a * a * b * g
131 + 3 * a * a * a * h;
132}
133
caryclark@google.comc6825902012-02-03 22:07:47 +0000134static double calc_x(double a, double b, double c, double d,
caryclark@google.com639df892012-01-10 21:46:10 +0000135 double e, double f, double g, double h) {
136 return
137 3 * d * d * e * e * e
138 - 2 * c * d * e * e * f
139 + 2 * b * d * e * f * f
140 - 2 * a * d * f * f * f
141 + c * c * e * e * g
142 - 4 * b * d * e * e * g
143 - b * c * e * f * g
144 + 6 * a * d * e * f * g
145 + a * c * f * f * g
146 + b * b * e * g * g
147 - 2 * a * c * e * g * g
148 - a * b * f * g * g
149 + a * a * g * g * g
150 + 3 * b * c * e * e * h
151 - 6 * a * d * e * e * h
152 - 2 * b * b * e * f * h
153 - a * c * e * f * h
154 + 2 * a * b * f * f * h
155 + a * b * e * g * h
156 - 3 * a * a * f * g * h
157 + 3 * a * a * e * h * h;
158}
159
caryclark@google.comc6825902012-02-03 22:07:47 +0000160static double calc_y(double a, double b, double c, double d,
caryclark@google.com639df892012-01-10 21:46:10 +0000161 double e, double f, double g, double h) {
162 return
163 -c * c * c * e * e
164 + 3 * b * c * d * e * e
165 - 3 * a * d * d * e * e
166 + b * c * c * e * f
167 - 2 * b * b * d * e * f
168 - a * c * d * e * f
169 - a * c * c * f * f
170 + 2 * a * b * d * f * f
171 - b * b * c * e * g
172 + 2 * a * c * c * e * g
173 + a * b * d * e * g
174 + a * b * c * f * g
175 - 3 * a * a * d * f * g
176 - a * a * c * g * g
177 + 2 * b * b * b * e * h
178 - 6 * a * b * c * e * h
179 + 6 * a * a * d * e * h
180 - 2 * a * b * b * f * h
181 + 4 * a * a * c * f * h
182 + 2 * a * a * b * g * h
183 - 3 * a * a * a * h * h;
184}
185
caryclark@google.comc6825902012-02-03 22:07:47 +0000186static double calc_c(double a, double b, double c, double d,
caryclark@google.com639df892012-01-10 21:46:10 +0000187 double e, double f, double g, double h) {
188 return
189 -d * d * d * e * e * e
190 + c * d * d * e * e * f
191 - b * d * d * e * f * f
192 + a * d * d * f * f * f
193 - c * c * d * e * e * g
194 + 2 * b * d * d * e * e * g
195 + b * c * d * e * f * g
196 - 3 * a * d * d * e * f * g
197 - a * c * d * f * f * g
198 - b * b * d * e * g * g
199 + 2 * a * c * d * e * g * g
200 + a * b * d * f * g * g
201 - a * a * d * g * g * g
202 + c * c * c * e * e * h
203 - 3 * b * c * d * e * e * h
204 + 3 * a * d * d * e * e * h
205 - b * c * c * e * f * h
206 + 2 * b * b * d * e * f * h
207 + a * c * d * e * f * h
208 + a * c * c * f * f * h
209 - 2 * a * b * d * f * f * h
210 + b * b * c * e * g * h
211 - 2 * a * c * c * e * g * h
212 - a * b * d * e * g * h
213 - a * b * c * f * g * h
214 + 3 * a * a * d * f * g * h
215 + a * a * c * g * g * h
216 - b * b * b * e * h * h
217 + 3 * a * b * c * e * h * h
218 - 3 * a * a * d * e * h * h
219 + a * b * b * f * h * h
220 - 2 * a * a * c * f * h * h
221 - a * a * b * g * h * h
222 + a * a * a * h * h * h;
223}
224// end of generated code
225
caryclark@google.comc6825902012-02-03 22:07:47 +0000226#else
227
228/* more Mathematica generated code. This takes a different tack, starting with
229 the control-point based parametric formulas. The C code is unoptimized --
230 in this form, this is a proof of concept (since the other code didn't work)
231*/
232static double calc_c(double a, double b, double c, double d,
233 double e, double f, double g, double h) {
234 return
235d*d*d*e*e*e - 3*d*d*(3*c*e*e*f + 3*b*e*(-3*f*f + 2*e*g) + a*(9*f*f*f - 9*e*f*g + e*e*h)) -
236 h*(27*c*c*c*e*e - 27*c*c*(3*b*e*f - 3*a*f*f + 2*a*e*g) +
237 h*(-27*b*b*b*e + 27*a*b*b*f - 9*a*a*b*g + a*a*a*h) +
238 9*c*(9*b*b*e*g + a*b*(-9*f*g + 3*e*h) + a*a*(3*g*g - 2*f*h))) +
239 3*d*(9*c*c*e*e*g + 9*b*b*e*(3*g*g - 2*f*h) + 3*a*b*(-9*f*g*g + 6*f*f*h + e*g*h) +
240 a*a*(9*g*g*g - 9*f*g*h + e*h*h) + 3*c*(3*b*e*(-3*f*g + e*h) + a*(9*f*f*g - 6*e*g*g - e*f*h)))
241 ;
242}
243
244// - Power(e - 3*f + 3*g - h,3)*Power(x,3)
245static double calc_xxx(double e3f3gh) {
246 return -e3f3gh * e3f3gh * e3f3gh;
247}
248
249static double calc_y(double a, double b, double c, double d,
250 double e, double f, double g, double h) {
251 return
252+ 3*(6*b*d*d*e*e - d*d*d*e*e + 18*b*b*d*e*f - 18*b*d*d*e*f -
253 9*b*d*d*f*f - 54*b*b*d*e*g + 12*b*d*d*e*g - 27*b*b*d*g*g - 18*b*b*b*e*h + 18*b*b*d*e*h +
254 18*b*b*d*f*h + a*a*a*h*h - 9*b*b*b*h*h + 9*c*c*c*e*(e + 2*h) +
255 a*a*(-3*b*h*(2*g + h) + d*(-27*g*g + 9*g*h - h*(2*e + h) + 9*f*(g + h))) +
256 a*(9*b*b*h*(2*f + h) - 3*b*d*(6*f*f - 6*f*(3*g - 2*h) + g*(-9*g + h) + e*(g + h)) +
257 d*d*(e*e + 9*f*(3*f - g) + e*(-9*f - 9*g + 2*h))) -
258 9*c*c*(d*e*(e + 2*g) + 3*b*(f*h + e*(f + h)) + a*(-3*f*f - 6*f*h + 2*(g*h + e*(g + h)))) +
259 3*c*(d*d*e*(e + 2*f) + a*a*(3*g*g + 6*g*h - 2*h*(2*f + h)) + 9*b*b*(g*h + e*(g + h)) +
260 a*d*(-9*f*f - 18*f*g + 6*g*g + f*h + e*(f + 12*g + h)) +
261 b*(d*(-3*e*e + 9*f*g + e*(9*f + 9*g - 6*h)) + 3*a*(h*(2*e - 3*g + h) - 3*f*(g + h))))) // *y
262 ;
263}
264
265static double calc_yy(double a, double b, double c, double d,
266 double e, double f, double g, double h) {
267 return
268- 3*(18*c*c*c*e - 18*c*c*d*e + 6*c*d*d*e - d*d*d*e + 3*c*d*d*f - 9*c*c*d*g + a*a*a*h + 9*c*c*c*h -
269 9*b*b*b*(e + 2*h) - a*a*(d*(e - 9*f + 18*g - 7*h) + 3*c*(2*f - 6*g + h)) +
270 a*(-9*c*c*(2*e - 6*f + 2*g - h) + d*d*(-7*e + 18*f - 9*g + h) + 3*c*d*(7*e - 17*f + 3*g + h)) +
271 9*b*b*(3*c*(e + g + h) + a*(f + 2*h) - d*(e - 2*(f - 3*g + h))) -
272 3*b*(-(d*d*(e - 6*f + 2*g)) - 3*c*d*(e + 3*f + 3*g - h) + 9*c*c*(e + f + h) + a*a*(g + 2*h) +
273 a*(c*(-3*e + 9*f + 9*g + 3*h) + d*(e + 3*f - 17*g + 7*h)))) // *Power(y,2)
274 ;
275}
276
277// + Power(a - 3*b + 3*c - d,3)*Power(y,3)
278static double calc_yyy(double a3b3cd) {
279 return a3b3cd * a3b3cd * a3b3cd;
280}
281
282static double calc_xx(double a, double b, double c, double d,
283 double e, double f, double g, double h) {
284 return
285// + Power(x,2)*
286(-3*(-9*b*e*f*f + 9*a*f*f*f + 6*b*e*e*g - 9*a*e*f*g + 27*b*e*f*g - 27*a*f*f*g + 18*a*e*g*g - 54*b*e*g*g +
287 27*a*f*g*g + 27*b*f*g*g - 18*a*g*g*g + a*e*e*h - 9*b*e*e*h + 3*a*e*f*h + 9*b*e*f*h + 9*a*f*f*h -
288 18*b*f*f*h - 21*a*e*g*h + 51*b*e*g*h - 9*a*f*g*h - 27*b*f*g*h + 18*a*g*g*h + 7*a*e*h*h - 18*b*e*h*h - 3*a*f*h*h +
289 18*b*f*h*h - 6*a*g*h*h - 3*b*g*h*h + a*h*h*h +
290 3*c*(-9*f*f*(g - 2*h) + 3*g*g*h - f*h*(9*g + 2*h) + e*e*(f - 6*g + 6*h) +
291 e*(9*f*g + 6*g*g - 17*f*h - 3*g*h + 3*h*h)) -
292 d*(e*e*e + e*e*(-6*f - 3*g + 7*h) - 9*(2*f - g)*(f*f + g*g - f*(g + h)) +
293 e*(18*f*f + 9*g*g + 3*g*h + h*h - 3*f*(3*g + 7*h)))) )
294 ;
295}
296
297// + Power(x,2)*(3*(a - 3*b + 3*c - d)*Power(e - 3*f + 3*g - h,2)*y)
298static double calc_xxy(double a3b3cd, double e3f3gh) {
299 return 3 * a3b3cd * e3f3gh * e3f3gh;
300}
301
302static double calc_x(double a, double b, double c, double d,
303 double e, double f, double g, double h) {
304 return
305// + x*
306(-3*(27*b*b*e*g*g - 27*a*b*f*g*g + 9*a*a*g*g*g - 18*b*b*e*f*h + 18*a*b*f*f*h + 3*a*b*e*g*h -
307 27*b*b*e*g*h - 9*a*a*f*g*h + 27*a*b*f*g*h - 9*a*a*g*g*h + a*a*e*h*h - 9*a*b*e*h*h +
308 27*b*b*e*h*h + 6*a*a*f*h*h - 18*a*b*f*h*h - 9*b*b*f*h*h + 3*a*a*g*h*h +
309 6*a*b*g*h*h - a*a*h*h*h + 9*c*c*(e*e*(g - 3*h) - 3*f*f*h + e*(3*f + 2*g)*h) +
310 d*d*(e*e*e - 9*f*f*f + 9*e*f*(f + g) - e*e*(3*f + 6*g + h)) +
311 d*(-3*c*(-9*f*f*g + e*e*(2*f - 6*g - 3*h) + e*(9*f*g + 6*g*g + f*h)) +
312 a*(-18*f*f*f - 18*e*g*g + 18*g*g*g - 2*e*e*h + 3*e*g*h + 2*e*h*h + 9*f*f*(3*g + 2*h) +
313 3*f*(6*e*g - 9*g*g - e*h - 6*g*h)) - 3*b*(9*f*g*g + e*e*(4*g - 3*h) - 6*f*f*h -
314 e*(6*f*f + g*(18*g + h) - 3*f*(3*g + 4*h)))) +
315 3*c*(3*b*(e*e*h + 3*f*g*h - e*(3*f*g - 6*f*h + 6*g*h + h*h)) +
316 a*(9*f*f*(g - 2*h) + f*h*(-e + 9*g + 4*h) - 3*(2*g*g*h + e*(2*g*g - 4*g*h + h*h))))) )
317 ;
318}
319
320static double calc_xy(double a, double b, double c, double d,
321 double e, double f, double g, double h) {
322 return
323// + x*3*
324(-2*a*d*e*e - 7*d*d*e*e + 15*a*d*e*f + 21*d*d*e*f - 9*a*d*f*f - 18*d*d*f*f - 15*a*d*e*g -
325 3*d*d*e*g - 9*a*a*f*g + 9*d*d*f*g + 18*a*a*g*g + 9*a*d*g*g + 2*a*a*e*h - 2*d*d*e*h +
326 3*a*a*f*h + 15*a*d*f*h - 21*a*a*g*h - 15*a*d*g*h + 7*a*a*h*h + 2*a*d*h*h -
327 9*c*c*(2*e*e + 3*f*f + 3*f*h - 2*g*h + e*(-3*f - 4*g + h)) +
328 9*b*b*(3*g*g - 3*g*h + 2*h*(-2*f + h) + e*(-2*f + 3*g + h)) +
329 3*b*(3*c*(e*e + 3*e*(f - 3*g) + (9*f - 3*g - h)*h) + a*(6*f*f + e*g - 9*f*g - 9*g*g - 5*e*h + 9*f*h + 14*g*h - 7*h*h) +
330 d*(-e*e + 12*f*f - 27*f*g + e*(-9*f + 20*g - 5*h) + g*(9*g + h))) +
331 3*c*(a*(-(e*f) - 9*f*f + 27*f*g - 12*g*g + 5*e*h - 20*f*h + 9*g*h + h*h) +
332 d*(7*e*e + 9*f*f + 9*f*g - 6*g*g - f*h + e*(-14*f - 9*g + 5*h)))) // *y
333 ;
334}
335
336// - x*3*Power(a - 3*b + 3*c - d,2)*(e - 3*f + 3*g - h)*Power(y,2)
337static double calc_xyy(double a3b3cd, double e3f3gh) {
338 return -3 * a3b3cd * a3b3cd * e3f3gh;
339}
340
341#endif
342
caryclark@google.com639df892012-01-10 21:46:10 +0000343static double (*calc_proc[])(double a, double b, double c, double d,
344 double e, double f, double g, double h) = {
caryclark@google.comc6825902012-02-03 22:07:47 +0000345 calc_xx, calc_xy, calc_yy, calc_x, calc_y, calc_c
caryclark@google.com639df892012-01-10 21:46:10 +0000346};
347
caryclark@google.comc6825902012-02-03 22:07:47 +0000348#if USE_SYVESTER
caryclark@google.com639df892012-01-10 21:46:10 +0000349/* Control points to parametric coefficients
350 s = 1 - t
caryclark@google.com27accef2012-01-25 18:57:23 +0000351 Attt + 3Btts + 3Ctss + Dsss ==
caryclark@google.com639df892012-01-10 21:46:10 +0000352 Attt + 3B(1 - t)tt + 3C(1 - t)(t - tt) + D(1 - t)(1 - 2t + tt) ==
353 Attt + 3B(tt - ttt) + 3C(t - tt - tt + ttt) + D(1-2t+tt-t+2tt-ttt) ==
354 Attt + 3Btt - 3Bttt + 3Ct - 6Ctt + 3Cttt + D - 3Dt + 3Dtt - Dttt ==
355 D + (3C - 3D)t + (3B - 6C + 3D)tt + (A - 3B + 3C - D)ttt
356 a = A - 3*B + 3*C - D
357 b = 3*B - 6*C + 3*D
358 c = 3*C - 3*D
359 d = D
360 */
caryclark@google.com27accef2012-01-25 18:57:23 +0000361
362 /* http://www.algorithmist.net/bezier3.html
363 p = 3 * A
364 q = 3 * B
365 r = 3 * C
366 a = A
367 b = q - p
368 c = p - 2 * q + r
369 d = D - A + q - r
370
371 B(t) = a + t * (b + t * (c + t * d))
372
373 so
374
375 B(t) = a + t*b + t*t*(c + t*d)
376 = a + t*b + t*t*c + t*t*t*d
377 */
caryclark@google.com639df892012-01-10 21:46:10 +0000378static void set_abcd(const double* cubic, double& a, double& b, double& c,
379 double& d) {
380 a = cubic[0]; // a = A
381 b = 3 * cubic[2]; // b = 3*B (compute rest of b lazily)
382 c = 3 * cubic[4]; // c = 3*C (compute rest of c lazily)
383 d = cubic[6]; // d = D
384 a += -b + c - d; // a = A - 3*B + 3*C - D
385}
386
387static void calc_bc(const double d, double& b, double& c) {
388 b -= 3 * c; // b = 3*B - 3*C
389 c -= 3 * d; // c = 3*C - 3*D
390 b -= c; // b = 3*B - 6*C + 3*D
391}
392
caryclark@google.com27accef2012-01-25 18:57:23 +0000393static void alt_set_abcd(const double* cubic, double& a, double& b, double& c,
394 double& d) {
395 a = cubic[0];
396 double p = 3 * a;
397 double q = 3 * cubic[2];
398 double r = 3 * cubic[4];
399 b = q - p;
400 c = p - 2 * q + r;
401 d = cubic[6] - a + q - r;
402}
403
404const bool try_alt = true;
405
caryclark@google.comc6825902012-02-03 22:07:47 +0000406#else
407
408static void calc_ABCD(double a, double b, double c, double d,
409 double e, double f, double g, double h,
410 double p[coeff_count]) {
411 double a3b3cd = a - 3 * (b - c) - d;
412 double e3f3gh = e - 3 * (f - g) - h;
413 p[xxx_coeff] = calc_xxx(e3f3gh);
414 p[xxy_coeff] = calc_xxy(a3b3cd, e3f3gh);
415 p[xyy_coeff] = calc_xyy(a3b3cd, e3f3gh);
416 p[yyy_coeff] = calc_yyy(a3b3cd);
417}
418#endif
419
caryclark@google.com639df892012-01-10 21:46:10 +0000420bool implicit_matches(const Cubic& one, const Cubic& two) {
421 double p1[coeff_count]; // a'xxx , b'xxy , c'xyy , d'xx , e'xy , f'yy, etc.
422 double p2[coeff_count];
caryclark@google.comc6825902012-02-03 22:07:47 +0000423#if USE_SYVESTER
caryclark@google.com639df892012-01-10 21:46:10 +0000424 double a1, b1, c1, d1;
caryclark@google.com27accef2012-01-25 18:57:23 +0000425 if (try_alt)
426 alt_set_abcd(&one[0].x, a1, b1, c1, d1);
427 else
428 set_abcd(&one[0].x, a1, b1, c1, d1);
caryclark@google.com639df892012-01-10 21:46:10 +0000429 double e1, f1, g1, h1;
caryclark@google.com27accef2012-01-25 18:57:23 +0000430 if (try_alt)
431 alt_set_abcd(&one[0].y, e1, f1, g1, h1);
432 else
433 set_abcd(&one[0].y, e1, f1, g1, h1);
caryclark@google.com639df892012-01-10 21:46:10 +0000434 calc_ABCD(a1, e1, p1);
435 double a2, b2, c2, d2;
caryclark@google.com27accef2012-01-25 18:57:23 +0000436 if (try_alt)
437 alt_set_abcd(&two[0].x, a2, b2, c2, d2);
438 else
439 set_abcd(&two[0].x, a2, b2, c2, d2);
caryclark@google.com639df892012-01-10 21:46:10 +0000440 double e2, f2, g2, h2;
caryclark@google.com27accef2012-01-25 18:57:23 +0000441 if (try_alt)
442 alt_set_abcd(&two[0].y, e2, f2, g2, h2);
443 else
444 set_abcd(&two[0].y, e2, f2, g2, h2);
caryclark@google.com639df892012-01-10 21:46:10 +0000445 calc_ABCD(a2, e2, p2);
caryclark@google.comc6825902012-02-03 22:07:47 +0000446#else
447 double a1 = one[0].x;
448 double b1 = one[1].x;
449 double c1 = one[2].x;
450 double d1 = one[3].x;
451 double e1 = one[0].y;
452 double f1 = one[1].y;
453 double g1 = one[2].y;
454 double h1 = one[3].y;
455 calc_ABCD(a1, b1, c1, d1, e1, f1, g1, h1, p1);
456 double a2 = two[0].x;
457 double b2 = two[1].x;
458 double c2 = two[2].x;
459 double d2 = two[3].x;
460 double e2 = two[0].y;
461 double f2 = two[1].y;
462 double g2 = two[2].y;
463 double h2 = two[3].y;
464 calc_ABCD(a2, b2, c2, d2, e2, f2, g2, h2, p2);
465#endif
caryclark@google.com639df892012-01-10 21:46:10 +0000466 int first = 0;
467 for (int index = 0; index < coeff_count; ++index) {
caryclark@google.comc6825902012-02-03 22:07:47 +0000468#if USE_SYVESTER
caryclark@google.com27accef2012-01-25 18:57:23 +0000469 if (!try_alt && index == xx_coeff) {
caryclark@google.com639df892012-01-10 21:46:10 +0000470 calc_bc(d1, b1, c1);
471 calc_bc(h1, f1, g1);
472 calc_bc(d2, b2, c2);
473 calc_bc(h2, f2, g2);
474 }
caryclark@google.comc6825902012-02-03 22:07:47 +0000475#endif
caryclark@google.com639df892012-01-10 21:46:10 +0000476 if (index >= xx_coeff) {
477 int procIndex = index - xx_coeff;
478 p1[index] = (*calc_proc[procIndex])(a1, b1, c1, d1, e1, f1, g1, h1);
479 p2[index] = (*calc_proc[procIndex])(a2, b2, c2, d2, e2, f2, g2, h2);
480 }
481 if (approximately_zero(p1[index]) || approximately_zero(p2[index])) {
482 first += first == index;
483 continue;
484 }
485 if (first == index) {
486 continue;
487 }
488 if (!approximately_equal(p1[index] * p2[first],
489 p1[first] * p2[index])) {
490 return false;
491 }
492 }
493 return true;
494}
495
496static double tangent(const double* cubic, double t) {
497 double a, b, c, d;
caryclark@google.comc6825902012-02-03 22:07:47 +0000498#if USE_SYVESTER
caryclark@google.com639df892012-01-10 21:46:10 +0000499 set_abcd(cubic, a, b, c, d);
500 calc_bc(d, b, c);
caryclark@google.comc6825902012-02-03 22:07:47 +0000501#else
502 coefficients(cubic, a, b, c, d);
503#endif
caryclark@google.com639df892012-01-10 21:46:10 +0000504 return 3 * a * t * t + 2 * b * t + c;
505}
506
507void tangent(const Cubic& cubic, double t, _Point& result) {
508 result.x = tangent(&cubic[0].x, t);
509 result.y = tangent(&cubic[0].y, t);
510}
511
caryclark@google.com27accef2012-01-25 18:57:23 +0000512// unit test to return and validate parametric coefficients
513#include "CubicParameterization_TestUtility.cpp"
514
515