1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/pathops/SkDQuadImplicit.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,117 @@ 1.4 +/* 1.5 + * Copyright 2012 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 +#include "SkDQuadImplicit.h" 1.11 + 1.12 +/* from http://tom.cs.byu.edu/~tom/papers/cvgip84.pdf 4.1 1.13 + * 1.14 + * This paper proves that Syvester's method can compute the implicit form of 1.15 + * the quadratic from the parameterized form. 1.16 + * 1.17 + * Given x = a*t*t + b*t + c (the parameterized form) 1.18 + * y = d*t*t + e*t + f 1.19 + * 1.20 + * we want to find an equation of the implicit form: 1.21 + * 1.22 + * A*x*x + B*x*y + C*y*y + D*x + E*y + F = 0 1.23 + * 1.24 + * The implicit form can be expressed as a 4x4 determinant, as shown. 1.25 + * 1.26 + * The resultant obtained by Syvester's method is 1.27 + * 1.28 + * | a b (c - x) 0 | 1.29 + * | 0 a b (c - x) | 1.30 + * | d e (f - y) 0 | 1.31 + * | 0 d e (f - y) | 1.32 + * 1.33 + * which expands to 1.34 + * 1.35 + * d*d*x*x + -2*a*d*x*y + a*a*y*y 1.36 + * + (-2*c*d*d + b*e*d - a*e*e + 2*a*f*d)*x 1.37 + * + (-2*f*a*a + e*b*a - d*b*b + 2*d*c*a)*y 1.38 + * + 1.39 + * | a b c 0 | 1.40 + * | 0 a b c | == 0. 1.41 + * | d e f 0 | 1.42 + * | 0 d e f | 1.43 + * 1.44 + * Expanding the constant determinant results in 1.45 + * 1.46 + * | a b c | | b c 0 | 1.47 + * a*| e f 0 | + d*| a b c | == 1.48 + * | d e f | | d e f | 1.49 + * 1.50 + * 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) 1.51 + * 1.52 + */ 1.53 + 1.54 +// use the tricky arithmetic path, but leave the original to compare just in case 1.55 +static bool straight_forward = false; 1.56 + 1.57 +SkDQuadImplicit::SkDQuadImplicit(const SkDQuad& q) { 1.58 + double a, b, c; 1.59 + SkDQuad::SetABC(&q[0].fX, &a, &b, &c); 1.60 + double d, e, f; 1.61 + SkDQuad::SetABC(&q[0].fY, &d, &e, &f); 1.62 + // compute the implicit coefficients 1.63 + if (straight_forward) { // 42 muls, 13 adds 1.64 + fP[kXx_Coeff] = d * d; 1.65 + fP[kXy_Coeff] = -2 * a * d; 1.66 + fP[kYy_Coeff] = a * a; 1.67 + fP[kX_Coeff] = -2*c*d*d + b*e*d - a*e*e + 2*a*f*d; 1.68 + fP[kY_Coeff] = -2*f*a*a + e*b*a - d*b*b + 2*d*c*a; 1.69 + fP[kC_Coeff] = a*(a*f*f + c*e*e - c*f*d - b*e*f) 1.70 + + d*(b*b*f + c*c*d - c*a*f - c*e*b); 1.71 + } else { // 26 muls, 11 adds 1.72 + double aa = a * a; 1.73 + double ad = a * d; 1.74 + double dd = d * d; 1.75 + fP[kXx_Coeff] = dd; 1.76 + fP[kXy_Coeff] = -2 * ad; 1.77 + fP[kYy_Coeff] = aa; 1.78 + double be = b * e; 1.79 + double bde = be * d; 1.80 + double cdd = c * dd; 1.81 + double ee = e * e; 1.82 + fP[kX_Coeff] = -2*cdd + bde - a*ee + 2*ad*f; 1.83 + double aaf = aa * f; 1.84 + double abe = a * be; 1.85 + double ac = a * c; 1.86 + double bb_2ac = b*b - 2*ac; 1.87 + fP[kY_Coeff] = -2*aaf + abe - d*bb_2ac; 1.88 + fP[kC_Coeff] = aaf*f + ac*ee + d*f*bb_2ac - abe*f + c*cdd - c*bde; 1.89 + } 1.90 +} 1.91 + 1.92 + /* Given a pair of quadratics, determine their parametric coefficients. 1.93 + * If the scaled coefficients are nearly equal, then the part of the quadratics 1.94 + * may be coincident. 1.95 + * OPTIMIZATION -- since comparison short-circuits on no match, 1.96 + * lazily compute the coefficients, comparing the easiest to compute first. 1.97 + * xx and yy first; then xy; and so on. 1.98 + */ 1.99 +bool SkDQuadImplicit::match(const SkDQuadImplicit& p2) const { 1.100 + int first = 0; 1.101 + for (int index = 0; index <= kC_Coeff; ++index) { 1.102 + if (approximately_zero(fP[index]) && approximately_zero(p2.fP[index])) { 1.103 + first += first == index; 1.104 + continue; 1.105 + } 1.106 + if (first == index) { 1.107 + continue; 1.108 + } 1.109 + if (!AlmostDequalUlps(fP[index] * p2.fP[first], fP[first] * p2.fP[index])) { 1.110 + return false; 1.111 + } 1.112 + } 1.113 + return true; 1.114 +} 1.115 + 1.116 +bool SkDQuadImplicit::Match(const SkDQuad& quad1, const SkDQuad& quad2) { 1.117 + SkDQuadImplicit i1(quad1); // a'xx , b'xy , c'yy , d'x , e'y , f 1.118 + SkDQuadImplicit i2(quad2); 1.119 + return i1.match(i2); 1.120 +}