gfx/skia/trunk/src/pathops/SkDQuadImplicit.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright 2012 Google Inc.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7 #include "SkDQuadImplicit.h"
michael@0 8
michael@0 9 /* from http://tom.cs.byu.edu/~tom/papers/cvgip84.pdf 4.1
michael@0 10 *
michael@0 11 * This paper proves that Syvester's method can compute the implicit form of
michael@0 12 * the quadratic from the parameterized form.
michael@0 13 *
michael@0 14 * Given x = a*t*t + b*t + c (the parameterized form)
michael@0 15 * y = d*t*t + e*t + f
michael@0 16 *
michael@0 17 * we want to find an equation of the implicit form:
michael@0 18 *
michael@0 19 * A*x*x + B*x*y + C*y*y + D*x + E*y + F = 0
michael@0 20 *
michael@0 21 * The implicit form can be expressed as a 4x4 determinant, as shown.
michael@0 22 *
michael@0 23 * The resultant obtained by Syvester's method is
michael@0 24 *
michael@0 25 * | a b (c - x) 0 |
michael@0 26 * | 0 a b (c - x) |
michael@0 27 * | d e (f - y) 0 |
michael@0 28 * | 0 d e (f - y) |
michael@0 29 *
michael@0 30 * which expands to
michael@0 31 *
michael@0 32 * d*d*x*x + -2*a*d*x*y + a*a*y*y
michael@0 33 * + (-2*c*d*d + b*e*d - a*e*e + 2*a*f*d)*x
michael@0 34 * + (-2*f*a*a + e*b*a - d*b*b + 2*d*c*a)*y
michael@0 35 * +
michael@0 36 * | a b c 0 |
michael@0 37 * | 0 a b c | == 0.
michael@0 38 * | d e f 0 |
michael@0 39 * | 0 d e f |
michael@0 40 *
michael@0 41 * Expanding the constant determinant results in
michael@0 42 *
michael@0 43 * | a b c | | b c 0 |
michael@0 44 * a*| e f 0 | + d*| a b c | ==
michael@0 45 * | d e f | | d e f |
michael@0 46 *
michael@0 47 * 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 48 *
michael@0 49 */
michael@0 50
michael@0 51 // use the tricky arithmetic path, but leave the original to compare just in case
michael@0 52 static bool straight_forward = false;
michael@0 53
michael@0 54 SkDQuadImplicit::SkDQuadImplicit(const SkDQuad& q) {
michael@0 55 double a, b, c;
michael@0 56 SkDQuad::SetABC(&q[0].fX, &a, &b, &c);
michael@0 57 double d, e, f;
michael@0 58 SkDQuad::SetABC(&q[0].fY, &d, &e, &f);
michael@0 59 // compute the implicit coefficients
michael@0 60 if (straight_forward) { // 42 muls, 13 adds
michael@0 61 fP[kXx_Coeff] = d * d;
michael@0 62 fP[kXy_Coeff] = -2 * a * d;
michael@0 63 fP[kYy_Coeff] = a * a;
michael@0 64 fP[kX_Coeff] = -2*c*d*d + b*e*d - a*e*e + 2*a*f*d;
michael@0 65 fP[kY_Coeff] = -2*f*a*a + e*b*a - d*b*b + 2*d*c*a;
michael@0 66 fP[kC_Coeff] = a*(a*f*f + c*e*e - c*f*d - b*e*f)
michael@0 67 + d*(b*b*f + c*c*d - c*a*f - c*e*b);
michael@0 68 } else { // 26 muls, 11 adds
michael@0 69 double aa = a * a;
michael@0 70 double ad = a * d;
michael@0 71 double dd = d * d;
michael@0 72 fP[kXx_Coeff] = dd;
michael@0 73 fP[kXy_Coeff] = -2 * ad;
michael@0 74 fP[kYy_Coeff] = aa;
michael@0 75 double be = b * e;
michael@0 76 double bde = be * d;
michael@0 77 double cdd = c * dd;
michael@0 78 double ee = e * e;
michael@0 79 fP[kX_Coeff] = -2*cdd + bde - a*ee + 2*ad*f;
michael@0 80 double aaf = aa * f;
michael@0 81 double abe = a * be;
michael@0 82 double ac = a * c;
michael@0 83 double bb_2ac = b*b - 2*ac;
michael@0 84 fP[kY_Coeff] = -2*aaf + abe - d*bb_2ac;
michael@0 85 fP[kC_Coeff] = aaf*f + ac*ee + d*f*bb_2ac - abe*f + c*cdd - c*bde;
michael@0 86 }
michael@0 87 }
michael@0 88
michael@0 89 /* Given a pair of quadratics, determine their parametric coefficients.
michael@0 90 * If the scaled coefficients are nearly equal, then the part of the quadratics
michael@0 91 * may be coincident.
michael@0 92 * OPTIMIZATION -- since comparison short-circuits on no match,
michael@0 93 * lazily compute the coefficients, comparing the easiest to compute first.
michael@0 94 * xx and yy first; then xy; and so on.
michael@0 95 */
michael@0 96 bool SkDQuadImplicit::match(const SkDQuadImplicit& p2) const {
michael@0 97 int first = 0;
michael@0 98 for (int index = 0; index <= kC_Coeff; ++index) {
michael@0 99 if (approximately_zero(fP[index]) && approximately_zero(p2.fP[index])) {
michael@0 100 first += first == index;
michael@0 101 continue;
michael@0 102 }
michael@0 103 if (first == index) {
michael@0 104 continue;
michael@0 105 }
michael@0 106 if (!AlmostDequalUlps(fP[index] * p2.fP[first], fP[first] * p2.fP[index])) {
michael@0 107 return false;
michael@0 108 }
michael@0 109 }
michael@0 110 return true;
michael@0 111 }
michael@0 112
michael@0 113 bool SkDQuadImplicit::Match(const SkDQuad& quad1, const SkDQuad& quad2) {
michael@0 114 SkDQuadImplicit i1(quad1); // a'xx , b'xy , c'yy , d'x , e'y , f
michael@0 115 SkDQuadImplicit i2(quad2);
michael@0 116 return i1.match(i2);
michael@0 117 }

mercurial