michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #include "SkDQuadImplicit.h" michael@0: michael@0: /* from http://tom.cs.byu.edu/~tom/papers/cvgip84.pdf 4.1 michael@0: * michael@0: * This paper proves that Syvester's method can compute the implicit form of michael@0: * the quadratic from the parameterized form. michael@0: * michael@0: * Given x = a*t*t + b*t + c (the parameterized form) michael@0: * y = d*t*t + e*t + f michael@0: * michael@0: * we want to find an equation of the implicit form: michael@0: * michael@0: * A*x*x + B*x*y + C*y*y + D*x + E*y + F = 0 michael@0: * michael@0: * The implicit form can be expressed as a 4x4 determinant, as shown. michael@0: * michael@0: * The resultant obtained by Syvester's method is michael@0: * michael@0: * | a b (c - x) 0 | michael@0: * | 0 a b (c - x) | michael@0: * | d e (f - y) 0 | michael@0: * | 0 d e (f - y) | michael@0: * michael@0: * which expands to michael@0: * michael@0: * d*d*x*x + -2*a*d*x*y + a*a*y*y michael@0: * + (-2*c*d*d + b*e*d - a*e*e + 2*a*f*d)*x michael@0: * + (-2*f*a*a + e*b*a - d*b*b + 2*d*c*a)*y michael@0: * + michael@0: * | a b c 0 | michael@0: * | 0 a b c | == 0. michael@0: * | d e f 0 | michael@0: * | 0 d e f | michael@0: * michael@0: * Expanding the constant determinant results in michael@0: * michael@0: * | a b c | | b c 0 | michael@0: * a*| e f 0 | + d*| a b c | == michael@0: * | d e f | | d e f | michael@0: * michael@0: * 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) michael@0: * michael@0: */ michael@0: michael@0: // use the tricky arithmetic path, but leave the original to compare just in case michael@0: static bool straight_forward = false; michael@0: michael@0: SkDQuadImplicit::SkDQuadImplicit(const SkDQuad& q) { michael@0: double a, b, c; michael@0: SkDQuad::SetABC(&q[0].fX, &a, &b, &c); michael@0: double d, e, f; michael@0: SkDQuad::SetABC(&q[0].fY, &d, &e, &f); michael@0: // compute the implicit coefficients michael@0: if (straight_forward) { // 42 muls, 13 adds michael@0: fP[kXx_Coeff] = d * d; michael@0: fP[kXy_Coeff] = -2 * a * d; michael@0: fP[kYy_Coeff] = a * a; michael@0: fP[kX_Coeff] = -2*c*d*d + b*e*d - a*e*e + 2*a*f*d; michael@0: fP[kY_Coeff] = -2*f*a*a + e*b*a - d*b*b + 2*d*c*a; michael@0: fP[kC_Coeff] = a*(a*f*f + c*e*e - c*f*d - b*e*f) michael@0: + d*(b*b*f + c*c*d - c*a*f - c*e*b); michael@0: } else { // 26 muls, 11 adds michael@0: double aa = a * a; michael@0: double ad = a * d; michael@0: double dd = d * d; michael@0: fP[kXx_Coeff] = dd; michael@0: fP[kXy_Coeff] = -2 * ad; michael@0: fP[kYy_Coeff] = aa; michael@0: double be = b * e; michael@0: double bde = be * d; michael@0: double cdd = c * dd; michael@0: double ee = e * e; michael@0: fP[kX_Coeff] = -2*cdd + bde - a*ee + 2*ad*f; michael@0: double aaf = aa * f; michael@0: double abe = a * be; michael@0: double ac = a * c; michael@0: double bb_2ac = b*b - 2*ac; michael@0: fP[kY_Coeff] = -2*aaf + abe - d*bb_2ac; michael@0: fP[kC_Coeff] = aaf*f + ac*ee + d*f*bb_2ac - abe*f + c*cdd - c*bde; michael@0: } michael@0: } michael@0: michael@0: /* Given a pair of quadratics, determine their parametric coefficients. michael@0: * If the scaled coefficients are nearly equal, then the part of the quadratics michael@0: * may be coincident. michael@0: * OPTIMIZATION -- since comparison short-circuits on no match, michael@0: * lazily compute the coefficients, comparing the easiest to compute first. michael@0: * xx and yy first; then xy; and so on. michael@0: */ michael@0: bool SkDQuadImplicit::match(const SkDQuadImplicit& p2) const { michael@0: int first = 0; michael@0: for (int index = 0; index <= kC_Coeff; ++index) { michael@0: if (approximately_zero(fP[index]) && approximately_zero(p2.fP[index])) { michael@0: first += first == index; michael@0: continue; michael@0: } michael@0: if (first == index) { michael@0: continue; michael@0: } michael@0: if (!AlmostDequalUlps(fP[index] * p2.fP[first], fP[first] * p2.fP[index])) { michael@0: return false; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool SkDQuadImplicit::Match(const SkDQuad& quad1, const SkDQuad& quad2) { michael@0: SkDQuadImplicit i1(quad1); // a'xx , b'xy , c'yy , d'x , e'y , f michael@0: SkDQuadImplicit i2(quad2); michael@0: return i1.match(i2); michael@0: }