caryclark@google.com | 9e49fb6 | 2012-08-27 14:11:33 +0000 | [diff] [blame] | 1 | /* |
| 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.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 7 | #include "CurveIntersection.h" |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 8 | #include "QuadraticParameterization.h" |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 9 | #include "QuadraticUtilities.h" |
| 10 | |
| 11 | /* from http://tom.cs.byu.edu/~tom/papers/cvgip84.pdf 4.1 |
| 12 | * |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 13 | * This paper proves that Syvester's method can compute the implicit form of |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 14 | * the quadratic from the parameterized form. |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 15 | * |
| 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.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 53 | |
| 54 | static bool straight_forward = true; |
| 55 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 56 | QuadImplicitForm::QuadImplicitForm(const Quadratic& q) { |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 57 | 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.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 98 | bool QuadImplicitForm::implicit_match(const QuadImplicitForm& p2) const { |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 99 | int first = 0; |
| 100 | for (int index = 0; index < coeff_count; ++index) { |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 101 | if (approximately_zero(p[index]) && approximately_zero(p2.p[index])) { |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 102 | first += first == index; |
| 103 | continue; |
| 104 | } |
| 105 | if (first == index) { |
| 106 | continue; |
| 107 | } |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 108 | if (!AlmostEqualUlps(p[index] * p2.p[first], p[first] * p2.p[index])) { |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 109 | return false; |
| 110 | } |
| 111 | } |
| 112 | return true; |
| 113 | } |
| 114 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 115 | bool 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.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 121 | static 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 | |
| 127 | void 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.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 132 | |
| 133 | |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 134 | // unit test to return and validate parametric coefficients |
| 135 | #include "QuadraticParameterization_TestUtility.cpp" |