blob: 76441aa4d5c1fb1269c3079a1717254a4cc08c8b [file] [log] [blame]
caryclark@google.com9e49fb62012-08-27 14:11:33 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
caryclark@google.comc6825902012-02-03 22:07:47 +00007#include "CurveIntersection.h"
caryclark@google.com235f56a2012-09-14 14:19:30 +00008#include "QuadraticParameterization.h"
caryclark@google.com639df892012-01-10 21:46:10 +00009#include "QuadraticUtilities.h"
10
11/* from http://tom.cs.byu.edu/~tom/papers/cvgip84.pdf 4.1
12 *
rmistry@google.comd6176b02012-08-23 18:14:13 +000013 * This paper proves that Syvester's method can compute the implicit form of
caryclark@google.comc6825902012-02-03 22:07:47 +000014 * the quadratic from the parameterized form.
caryclark@google.com639df892012-01-10 21:46:10 +000015 *
16 * Given x = a*t*t + b*t + c (the parameterized form)
17 * y = d*t*t + e*t + f
18 *
19 * we want to find an equation of the implicit form:
20 *
21 * A*x*x + B*x*y + C*y*y + D*x + E*y + F = 0
22 *
23 * The implicit form can be expressed as a 4x4 determinant, as shown.
24 *
25 * The resultant obtained by Syvester's method is
26 *
27 * | a b (c - x) 0 |
28 * | 0 a b (c - x) |
29 * | d e (f - y) 0 |
30 * | 0 d e (f - y) |
31 *
32 * which expands to
33 *
34 * d*d*x*x + -2*a*d*x*y + a*a*y*y
35 * + (-2*c*d*d + b*e*d - a*e*e + 2*a*f*d)*x
36 * + (-2*f*a*a + e*b*a - d*b*b + 2*d*c*a)*y
37 * +
38 * | a b c 0 |
39 * | 0 a b c | == 0.
40 * | d e f 0 |
41 * | 0 d e f |
42 *
43 * Expanding the constant determinant results in
44 *
45 * | a b c | | b c 0 |
46 * a*| e f 0 | + d*| a b c | ==
47 * | d e f | | d e f |
48 *
49 * a*(a*f*f + c*e*e - c*f*d - b*e*f) + d*(b*b*f + c*c*d - c*a*f - c*e*b)
50 *
51 */
52
caryclark@google.com639df892012-01-10 21:46:10 +000053
54static bool straight_forward = true;
55
caryclark@google.com235f56a2012-09-14 14:19:30 +000056QuadImplicitForm::QuadImplicitForm(const Quadratic& q) {
caryclark@google.com639df892012-01-10 21:46:10 +000057 double a, b, c;
58 set_abc(&q[0].x, a, b, c);
59 double d, e, f;
60 set_abc(&q[0].y, d, e, f);
61 // compute the implicit coefficients
62 if (straight_forward) { // 42 muls, 13 adds
63 p[xx_coeff] = d * d;
64 p[xy_coeff] = -2 * a * d;
65 p[yy_coeff] = a * a;
66 p[x_coeff] = -2*c*d*d + b*e*d - a*e*e + 2*a*f*d;
67 p[y_coeff] = -2*f*a*a + e*b*a - d*b*b + 2*d*c*a;
68 p[c_coeff] = a*(a*f*f + c*e*e - c*f*d - b*e*f)
69 + d*(b*b*f + c*c*d - c*a*f - c*e*b);
70 } else { // 26 muls, 11 adds
71 double aa = a * a;
72 double ad = a * d;
73 double dd = d * d;
74 p[xx_coeff] = dd;
75 p[xy_coeff] = -2 * ad;
76 p[yy_coeff] = aa;
77 double be = b * e;
78 double bde = be * d;
79 double cdd = c * dd;
80 double ee = e * e;
81 p[x_coeff] = -2*cdd + bde - a*ee + 2*ad*f;
82 double aaf = aa * f;
83 double abe = a * be;
84 double ac = a * c;
85 double bb_2ac = b*b - 2*ac;
86 p[y_coeff] = -2*aaf + abe - d*bb_2ac;
87 p[c_coeff] = aaf*f + ac*ee + d*f*bb_2ac - abe*f + c*cdd - c*bde;
88 }
89}
90
91 /* Given a pair of quadratics, determine their parametric coefficients.
92 * If the scaled coefficients are nearly equal, then the part of the quadratics
93 * may be coincident.
94 * FIXME: optimization -- since comparison short-circuits on no match,
95 * lazily compute the coefficients, comparing the easiest to compute first.
96 * xx and yy first; then xy; and so on.
97 */
caryclark@google.com235f56a2012-09-14 14:19:30 +000098bool QuadImplicitForm::implicit_match(const QuadImplicitForm& p2) const {
caryclark@google.com639df892012-01-10 21:46:10 +000099 int first = 0;
100 for (int index = 0; index < coeff_count; ++index) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000101 if (approximately_zero(p[index]) && approximately_zero(p2.p[index])) {
caryclark@google.com639df892012-01-10 21:46:10 +0000102 first += first == index;
103 continue;
104 }
105 if (first == index) {
106 continue;
107 }
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000108 if (!AlmostEqualUlps(p[index] * p2.p[first], p[first] * p2.p[index])) {
caryclark@google.com639df892012-01-10 21:46:10 +0000109 return false;
110 }
111 }
112 return true;
113}
114
caryclark@google.com235f56a2012-09-14 14:19:30 +0000115bool implicit_matches(const Quadratic& quad1, const Quadratic& quad2) {
116 QuadImplicitForm i1(quad1); // a'xx , b'xy , c'yy , d'x , e'y , f
117 QuadImplicitForm i2(quad2);
118 return i1.implicit_match(i2);
119}
120
caryclark@google.com639df892012-01-10 21:46:10 +0000121static double tangent(const double* quadratic, double t) {
122 double a, b, c;
123 set_abc(quadratic, a, b, c);
124 return 2 * a * t + b;
125}
126
127void tangent(const Quadratic& quadratic, double t, _Point& result) {
128 result.x = tangent(&quadratic[0].x, t);
129 result.y = tangent(&quadratic[0].y, t);
130}
131
caryclark@google.com235f56a2012-09-14 14:19:30 +0000132
133
caryclark@google.com27accef2012-01-25 18:57:23 +0000134// unit test to return and validate parametric coefficients
135#include "QuadraticParameterization_TestUtility.cpp"