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