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 
8 #ifndef SkPathOpsQuad_DEFINED
9 #define SkPathOpsQuad_DEFINED
10 
11 #include "SkPathOpsPoint.h"
12 
13 struct SkOpCurve;
14 
15 struct SkDQuadPair {
firstSkDQuadPair16     const SkDQuad& first() const { return (const SkDQuad&) pts[0]; }
secondSkDQuadPair17     const SkDQuad& second() const { return (const SkDQuad&) pts[2]; }
18     SkDPoint pts[5];
19 };
20 
21 struct SkDQuad {
22     static const int kPointCount = 3;
23     static const int kPointLast = kPointCount - 1;
24     static const int kMaxIntersections = 4;
25 
26     SkDPoint fPts[kPointCount];
27 
collapsedSkDQuad28     bool collapsed() const {
29         return fPts[0].approximatelyEqual(fPts[1]) && fPts[0].approximatelyEqual(fPts[2]);
30     }
31 
controlsInsideSkDQuad32     bool controlsInside() const {
33         SkDVector v01 = fPts[0] - fPts[1];
34         SkDVector v02 = fPts[0] - fPts[2];
35         SkDVector v12 = fPts[1] - fPts[2];
36         return v02.dot(v01) > 0 && v02.dot(v12) > 0;
37     }
38 
debugInitSkDQuad39     void debugInit() {
40         sk_bzero(fPts, sizeof(fPts));
41     }
42 
flipSkDQuad43     SkDQuad flip() const {
44         SkDQuad result = {{fPts[2], fPts[1], fPts[0]}};
45         return result;
46     }
47 
IsConicSkDQuad48     static bool IsConic() { return false; }
49 
setSkDQuad50     const SkDQuad& set(const SkPoint pts[kPointCount]) {
51         fPts[0] = pts[0];
52         fPts[1] = pts[1];
53         fPts[2] = pts[2];
54         return *this;
55     }
56 
57     const SkDPoint& operator[](int n) const { SkASSERT(n >= 0 && n < kPointCount); return fPts[n]; }
58     SkDPoint& operator[](int n) { SkASSERT(n >= 0 && n < kPointCount); return fPts[n]; }
59 
60     static int AddValidTs(double s[], int realRoots, double* t);
61     void align(int endIndex, SkDPoint* dstPt) const;
62     SkDQuadPair chopAt(double t) const;
63     SkDVector dxdyAtT(double t) const;
64     static int FindExtrema(const double src[], double tValue[1]);
65 
66     /**
67      *  Return the number of valid roots (0 < root < 1) for this cubic intersecting the
68      *  specified horizontal line.
69      */
70     int horizontalIntersect(double yIntercept, double roots[2]) const;
71 
72     bool hullIntersects(const SkDQuad& , bool* isLinear) const;
73     bool hullIntersects(const SkDConic& , bool* isLinear) const;
74     bool hullIntersects(const SkDCubic& , bool* isLinear) const;
75     bool isLinear(int startIndex, int endIndex) const;
76     bool monotonicInX() const;
77     bool monotonicInY() const;
78     void otherPts(int oddMan, const SkDPoint* endPt[2]) const;
79     SkDPoint ptAtT(double t) const;
80     static int RootsReal(double A, double B, double C, double t[2]);
81     static int RootsValidT(const double A, const double B, const double C, double s[2]);
82     static void SetABC(const double* quad, double* a, double* b, double* c);
83     SkDQuad subDivide(double t1, double t2) const;
SubDivideSkDQuad84     static SkDQuad SubDivide(const SkPoint a[kPointCount], double t1, double t2) {
85         SkDQuad quad;
86         quad.set(a);
87         return quad.subDivide(t1, t2);
88     }
89     SkDPoint subDivide(const SkDPoint& a, const SkDPoint& c, double t1, double t2) const;
SubDivideSkDQuad90     static SkDPoint SubDivide(const SkPoint pts[kPointCount], const SkDPoint& a, const SkDPoint& c,
91                               double t1, double t2) {
92         SkDQuad quad;
93         quad.set(pts);
94         return quad.subDivide(a, c, t1, t2);
95     }
96 
97     /**
98      *  Return the number of valid roots (0 < root < 1) for this cubic intersecting the
99      *  specified vertical line.
100      */
101     int verticalIntersect(double xIntercept, double roots[2]) const;
102 
103     SkDCubic debugToCubic() const;
104     // utilities callable by the user from the debugger when the implementation code is linked in
105     void dump() const;
106     void dumpID(int id) const;
107     void dumpInner() const;
108 
109 private:
110 //  static double Tangent(const double* quadratic, double t);  // uncalled
111 };
112 
113 #endif
114