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 SkPathOpsCubic_DEFINED
9 #define SkPathOpsCubic_DEFINED
10
11 #include "SkPath.h"
12 #include "SkPathOpsPoint.h"
13
14 struct SkDCubicPair {
firstSkDCubicPair15 const SkDCubic& first() const { return (const SkDCubic&) pts[0]; }
secondSkDCubicPair16 const SkDCubic& second() const { return (const SkDCubic&) pts[3]; }
17 SkDPoint pts[7];
18 };
19
20 struct SkDCubic {
21 static const int kPointCount = 4;
22 static const int kPointLast = kPointCount - 1;
23 static const int kMaxIntersections = 9;
24
25 enum SearchAxis {
26 kXAxis,
27 kYAxis
28 };
29
30 enum CubicType {
31 kUnsplit_SkDCubicType,
32 kSplitAtLoop_SkDCubicType,
33 kSplitAtInflection_SkDCubicType,
34 kSplitAtMaxCurvature_SkDCubicType,
35 };
36
collapsedSkDCubic37 bool collapsed() const {
38 return fPts[0].approximatelyEqual(fPts[1]) && fPts[0].approximatelyEqual(fPts[2])
39 && fPts[0].approximatelyEqual(fPts[3]);
40 }
41
controlsInsideSkDCubic42 bool controlsInside() const {
43 SkDVector v01 = fPts[0] - fPts[1];
44 SkDVector v02 = fPts[0] - fPts[2];
45 SkDVector v03 = fPts[0] - fPts[3];
46 SkDVector v13 = fPts[1] - fPts[3];
47 SkDVector v23 = fPts[2] - fPts[3];
48 return v03.dot(v01) > 0 && v03.dot(v02) > 0 && v03.dot(v13) > 0 && v03.dot(v23) > 0;
49 }
50
IsCubicSkDCubic51 static bool IsCubic() { return true; }
52
53 const SkDPoint& operator[](int n) const { SkASSERT(n >= 0 && n < kPointCount); return fPts[n]; }
54 SkDPoint& operator[](int n) { SkASSERT(n >= 0 && n < kPointCount); return fPts[n]; }
55
56 void align(int endIndex, int ctrlIndex, SkDPoint* dstPt) const;
57 double binarySearch(double min, double max, double axisIntercept, SearchAxis xAxis) const;
58 double calcPrecision() const;
59 SkDCubicPair chopAt(double t) const;
60 static void Coefficients(const double* cubic, double* A, double* B, double* C, double* D);
61 static bool ComplexBreak(const SkPoint pts[4], SkScalar* t, CubicType* cubicType);
62 int convexHull(char order[kPointCount]) const;
63
debugInitSkDCubic64 void debugInit() {
65 sk_bzero(fPts, sizeof(fPts));
66 }
67
68 void dump() const; // callable from the debugger when the implementation code is linked in
69 void dumpID(int id) const;
70 void dumpInner() const;
71 SkDVector dxdyAtT(double t) const;
72 bool endsAreExtremaInXOrY() const;
73 static int FindExtrema(const double src[], double tValue[2]);
74 int findInflections(double tValues[2]) const;
75
FindInflectionsSkDCubic76 static int FindInflections(const SkPoint a[kPointCount], double tValues[2]) {
77 SkDCubic cubic;
78 return cubic.set(a).findInflections(tValues);
79 }
80
81 int findMaxCurvature(double tValues[]) const;
82 bool hullIntersects(const SkDCubic& c2, bool* isLinear) const;
83 bool hullIntersects(const SkDConic& c, bool* isLinear) const;
84 bool hullIntersects(const SkDQuad& c2, bool* isLinear) const;
85 bool hullIntersects(const SkDPoint* pts, int ptCount, bool* isLinear) const;
86 bool isLinear(int startIndex, int endIndex) const;
87 bool monotonicInX() const;
88 bool monotonicInY() const;
89 void otherPts(int index, const SkDPoint* o1Pts[kPointCount - 1]) const;
90 SkDPoint ptAtT(double t) const;
91 static int RootsReal(double A, double B, double C, double D, double t[3]);
92 static int RootsValidT(const double A, const double B, const double C, double D, double s[3]);
93
94 int searchRoots(double extremes[6], int extrema, double axisIntercept,
95 SearchAxis xAxis, double* validRoots) const;
96
97 /**
98 * Return the number of valid roots (0 < root < 1) for this cubic intersecting the
99 * specified horizontal line.
100 */
101 int horizontalIntersect(double yIntercept, double roots[3]) const;
102 /**
103 * Return the number of valid roots (0 < root < 1) for this cubic intersecting the
104 * specified vertical line.
105 */
106 int verticalIntersect(double xIntercept, double roots[3]) const;
107
setSkDCubic108 const SkDCubic& set(const SkPoint pts[kPointCount]) {
109 fPts[0] = pts[0];
110 fPts[1] = pts[1];
111 fPts[2] = pts[2];
112 fPts[3] = pts[3];
113 return *this;
114 }
115
116 SkDCubic subDivide(double t1, double t2) const;
117
SubDivideSkDCubic118 static SkDCubic SubDivide(const SkPoint a[kPointCount], double t1, double t2) {
119 SkDCubic cubic;
120 return cubic.set(a).subDivide(t1, t2);
121 }
122
123 void subDivide(const SkDPoint& a, const SkDPoint& d, double t1, double t2, SkDPoint p[2]) const;
124
SubDivideSkDCubic125 static void SubDivide(const SkPoint pts[kPointCount], const SkDPoint& a, const SkDPoint& d, double t1,
126 double t2, SkDPoint p[2]) {
127 SkDCubic cubic;
128 cubic.set(pts).subDivide(a, d, t1, t2, p);
129 }
130
131 double top(const SkDCubic& dCurve, double startT, double endT, SkDPoint*topPt) const;
132 SkDQuad toQuad() const;
133
134 static const int gPrecisionUnit;
135
136 SkDPoint fPts[kPointCount];
137 };
138
139 /* Given the set [0, 1, 2, 3], and two of the four members, compute an XOR mask
140 that computes the other two. Note that:
141
142 one ^ two == 3 for (0, 3), (1, 2)
143 one ^ two < 3 for (0, 1), (0, 2), (1, 3), (2, 3)
144 3 - (one ^ two) is either 0, 1, or 2
145 1 >> (3 - (one ^ two)) is either 0 or 1
146 thus:
147 returned == 2 for (0, 3), (1, 2)
148 returned == 3 for (0, 1), (0, 2), (1, 3), (2, 3)
149 given that:
150 (0, 3) ^ 2 -> (2, 1) (1, 2) ^ 2 -> (3, 0)
151 (0, 1) ^ 3 -> (3, 2) (0, 2) ^ 3 -> (3, 1) (1, 3) ^ 3 -> (2, 0) (2, 3) ^ 3 -> (1, 0)
152 */
other_two(int one,int two)153 inline int other_two(int one, int two) {
154 return 1 >> (3 - (one ^ two)) ^ 3;
155 }
156
157 #endif
158