caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkPath.h" |
Ben Wagner | 729a23f | 2019-05-17 16:29:34 -0400 | [diff] [blame] | 12 | #include "src/core/SkArenaAlloc.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/pathops/SkPathOpsTCurve.h" |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 14 | |
caryclark | 4209dcb | 2016-10-20 10:11:27 -0700 | [diff] [blame] | 15 | struct SkDCubicPair; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 16 | |
| 17 | struct SkDCubic { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 18 | static const int kPointCount = 4; |
| 19 | static const int kPointLast = kPointCount - 1; |
| 20 | static const int kMaxIntersections = 9; |
| 21 | |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 22 | enum SearchAxis { |
| 23 | kXAxis, |
| 24 | kYAxis |
| 25 | }; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 26 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 27 | bool collapsed() const { |
| 28 | return fPts[0].approximatelyEqual(fPts[1]) && fPts[0].approximatelyEqual(fPts[2]) |
| 29 | && fPts[0].approximatelyEqual(fPts[3]); |
| 30 | } |
| 31 | |
| 32 | bool controlsInside() const { |
| 33 | SkDVector v01 = fPts[0] - fPts[1]; |
| 34 | SkDVector v02 = fPts[0] - fPts[2]; |
| 35 | SkDVector v03 = fPts[0] - fPts[3]; |
| 36 | SkDVector v13 = fPts[1] - fPts[3]; |
| 37 | SkDVector v23 = fPts[2] - fPts[3]; |
| 38 | return v03.dot(v01) > 0 && v03.dot(v02) > 0 && v03.dot(v13) > 0 && v03.dot(v23) > 0; |
| 39 | } |
| 40 | |
caryclark | ed0935a | 2015-10-22 07:23:52 -0700 | [diff] [blame] | 41 | static bool IsConic() { return false; } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 42 | |
| 43 | const SkDPoint& operator[](int n) const { SkASSERT(n >= 0 && n < kPointCount); return fPts[n]; } |
| 44 | SkDPoint& operator[](int n) { SkASSERT(n >= 0 && n < kPointCount); return fPts[n]; } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 45 | |
skia.committer@gmail.com | 8f6ef40 | 2013-06-05 07:01:06 +0000 | [diff] [blame] | 46 | void align(int endIndex, int ctrlIndex, SkDPoint* dstPt) const; |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 47 | double binarySearch(double min, double max, double axisIntercept, SearchAxis xAxis) const; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 48 | double calcPrecision() const; |
| 49 | SkDCubicPair chopAt(double t) const; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 50 | static void Coefficients(const double* cubic, double* A, double* B, double* C, double* D); |
Cary Clark | 7eb01e0 | 2016-12-08 14:36:32 -0500 | [diff] [blame] | 51 | static int ComplexBreak(const SkPoint pts[4], SkScalar* t); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 52 | int convexHull(char order[kPointCount]) const; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 53 | |
| 54 | void debugInit() { |
| 55 | sk_bzero(fPts, sizeof(fPts)); |
| 56 | } |
| 57 | |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 58 | void debugSet(const SkDPoint* pts); |
| 59 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 60 | void dump() const; // callable from the debugger when the implementation code is linked in |
| 61 | void dumpID(int id) const; |
| 62 | void dumpInner() const; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 63 | SkDVector dxdyAtT(double t) const; |
| 64 | bool endsAreExtremaInXOrY() const; |
caryclark | aec2510 | 2015-04-29 08:28:30 -0700 | [diff] [blame] | 65 | static int FindExtrema(const double src[], double tValue[2]); |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 66 | int findInflections(double tValues[2]) const; |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 67 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 68 | static int FindInflections(const SkPoint a[kPointCount], double tValues[2]) { |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 69 | SkDCubic cubic; |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 70 | return cubic.set(a).findInflections(tValues); |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 71 | } |
| 72 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 73 | int findMaxCurvature(double tValues[]) const; |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 74 | |
| 75 | #ifdef SK_DEBUG |
| 76 | SkOpGlobalState* globalState() const { return fDebugGlobalState; } |
| 77 | #endif |
| 78 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 79 | bool hullIntersects(const SkDCubic& c2, bool* isLinear) const; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 80 | bool hullIntersects(const SkDConic& c, bool* isLinear) const; |
| 81 | bool hullIntersects(const SkDQuad& c2, bool* isLinear) const; |
| 82 | bool hullIntersects(const SkDPoint* pts, int ptCount, bool* isLinear) const; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 83 | bool isLinear(int startIndex, int endIndex) const; |
Cary Clark | 0a67198 | 2018-10-11 12:16:49 -0400 | [diff] [blame] | 84 | static int maxIntersections() { return kMaxIntersections; } |
caryclark | aec2510 | 2015-04-29 08:28:30 -0700 | [diff] [blame] | 85 | bool monotonicInX() const; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 86 | bool monotonicInY() const; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 87 | void otherPts(int index, const SkDPoint* o1Pts[kPointCount - 1]) const; |
Cary Clark | 0a67198 | 2018-10-11 12:16:49 -0400 | [diff] [blame] | 88 | static int pointCount() { return kPointCount; } |
| 89 | static int pointLast() { return kPointLast; } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 90 | SkDPoint ptAtT(double t) const; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 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]); |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 93 | |
| 94 | int searchRoots(double extremes[6], int extrema, double axisIntercept, |
| 95 | SearchAxis xAxis, double* validRoots) const; |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 96 | |
Cary Clark | 7eb01e0 | 2016-12-08 14:36:32 -0500 | [diff] [blame] | 97 | bool toFloatPoints(SkPoint* ) const; |
reed | dc30885 | 2015-04-30 07:47:13 -0700 | [diff] [blame] | 98 | /** |
| 99 | * Return the number of valid roots (0 < root < 1) for this cubic intersecting the |
| 100 | * specified horizontal line. |
| 101 | */ |
| 102 | int horizontalIntersect(double yIntercept, double roots[3]) const; |
| 103 | /** |
| 104 | * Return the number of valid roots (0 < root < 1) for this cubic intersecting the |
| 105 | * specified vertical line. |
| 106 | */ |
| 107 | int verticalIntersect(double xIntercept, double roots[3]) const; |
| 108 | |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 109 | // add debug only global pointer so asserts can be skipped by fuzzers |
| 110 | const SkDCubic& set(const SkPoint pts[kPointCount] |
| 111 | SkDEBUGPARAMS(SkOpGlobalState* state = nullptr)) { |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 112 | fPts[0] = pts[0]; |
| 113 | fPts[1] = pts[1]; |
| 114 | fPts[2] = pts[2]; |
| 115 | fPts[3] = pts[3]; |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 116 | SkDEBUGCODE(fDebugGlobalState = state); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 117 | return *this; |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 118 | } |
| 119 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 120 | SkDCubic subDivide(double t1, double t2) const; |
Cary Clark | 0a67198 | 2018-10-11 12:16:49 -0400 | [diff] [blame] | 121 | void subDivide(double t1, double t2, SkDCubic* c) const { *c = this->subDivide(t1, t2); } |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 122 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 123 | static SkDCubic SubDivide(const SkPoint a[kPointCount], double t1, double t2) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 124 | SkDCubic cubic; |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 125 | return cubic.set(a).subDivide(t1, t2); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 126 | } |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 127 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 128 | void subDivide(const SkDPoint& a, const SkDPoint& d, double t1, double t2, SkDPoint p[2]) const; |
| 129 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 130 | static void SubDivide(const SkPoint pts[kPointCount], const SkDPoint& a, const SkDPoint& d, double t1, |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 131 | double t2, SkDPoint p[2]) { |
| 132 | SkDCubic cubic; |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 133 | cubic.set(pts).subDivide(a, d, t1, t2, p); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 134 | } |
| 135 | |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 136 | double top(const SkDCubic& dCurve, double startT, double endT, SkDPoint*topPt) const; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 137 | SkDQuad toQuad() const; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 138 | |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 139 | static const int gPrecisionUnit; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 140 | SkDPoint fPts[kPointCount]; |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 141 | SkDEBUGCODE(SkOpGlobalState* fDebugGlobalState); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 144 | /* Given the set [0, 1, 2, 3], and two of the four members, compute an XOR mask |
| 145 | that computes the other two. Note that: |
| 146 | |
| 147 | one ^ two == 3 for (0, 3), (1, 2) |
| 148 | one ^ two < 3 for (0, 1), (0, 2), (1, 3), (2, 3) |
| 149 | 3 - (one ^ two) is either 0, 1, or 2 |
| 150 | 1 >> (3 - (one ^ two)) is either 0 or 1 |
| 151 | thus: |
| 152 | returned == 2 for (0, 3), (1, 2) |
| 153 | returned == 3 for (0, 1), (0, 2), (1, 3), (2, 3) |
| 154 | given that: |
| 155 | (0, 3) ^ 2 -> (2, 1) (1, 2) ^ 2 -> (3, 0) |
| 156 | (0, 1) ^ 3 -> (3, 2) (0, 2) ^ 3 -> (3, 1) (1, 3) ^ 3 -> (2, 0) (2, 3) ^ 3 -> (1, 0) |
| 157 | */ |
| 158 | inline int other_two(int one, int two) { |
| 159 | return 1 >> (3 - (one ^ two)) ^ 3; |
| 160 | } |
| 161 | |
caryclark | 4209dcb | 2016-10-20 10:11:27 -0700 | [diff] [blame] | 162 | struct SkDCubicPair { |
| 163 | const SkDCubic first() const { |
| 164 | #ifdef SK_DEBUG |
| 165 | SkDCubic result; |
| 166 | result.debugSet(&pts[0]); |
| 167 | return result; |
| 168 | #else |
| 169 | return (const SkDCubic&) pts[0]; |
| 170 | #endif |
| 171 | } |
| 172 | const SkDCubic second() const { |
| 173 | #ifdef SK_DEBUG |
| 174 | SkDCubic result; |
| 175 | result.debugSet(&pts[3]); |
| 176 | return result; |
| 177 | #else |
| 178 | return (const SkDCubic&) pts[3]; |
| 179 | #endif |
| 180 | } |
| 181 | SkDPoint pts[7]; |
| 182 | }; |
| 183 | |
Cary Clark | 0a67198 | 2018-10-11 12:16:49 -0400 | [diff] [blame] | 184 | class SkTCubic : public SkTCurve { |
| 185 | public: |
| 186 | SkDCubic fCubic; |
| 187 | |
| 188 | SkTCubic() {} |
| 189 | |
| 190 | SkTCubic(const SkDCubic& c) |
| 191 | : fCubic(c) { |
| 192 | } |
| 193 | |
| 194 | ~SkTCubic() override {} |
| 195 | |
| 196 | const SkDPoint& operator[](int n) const override { return fCubic[n]; } |
| 197 | SkDPoint& operator[](int n) override { return fCubic[n]; } |
| 198 | |
| 199 | bool collapsed() const override { return fCubic.collapsed(); } |
| 200 | bool controlsInside() const override { return fCubic.controlsInside(); } |
| 201 | void debugInit() override { return fCubic.debugInit(); } |
Cary Clark | 8762fb6 | 2018-10-16 16:06:24 -0400 | [diff] [blame] | 202 | #if DEBUG_T_SECT |
| 203 | void dumpID(int id) const override { return fCubic.dumpID(id); } |
| 204 | #endif |
Cary Clark | 0a67198 | 2018-10-11 12:16:49 -0400 | [diff] [blame] | 205 | SkDVector dxdyAtT(double t) const override { return fCubic.dxdyAtT(t); } |
| 206 | #ifdef SK_DEBUG |
| 207 | SkOpGlobalState* globalState() const override { return fCubic.globalState(); } |
| 208 | #endif |
| 209 | bool hullIntersects(const SkDQuad& quad, bool* isLinear) const override; |
| 210 | bool hullIntersects(const SkDConic& conic, bool* isLinear) const override; |
| 211 | |
| 212 | bool hullIntersects(const SkDCubic& cubic, bool* isLinear) const override { |
| 213 | return cubic.hullIntersects(fCubic, isLinear); |
| 214 | } |
| 215 | |
| 216 | bool hullIntersects(const SkTCurve& curve, bool* isLinear) const override { |
| 217 | return curve.hullIntersects(fCubic, isLinear); |
| 218 | } |
| 219 | |
| 220 | int intersectRay(SkIntersections* i, const SkDLine& line) const override; |
| 221 | bool IsConic() const override { return false; } |
| 222 | SkTCurve* make(SkArenaAlloc& heap) const override { return heap.make<SkTCubic>(); } |
| 223 | |
| 224 | int maxIntersections() const override { return SkDCubic::kMaxIntersections; } |
| 225 | |
| 226 | void otherPts(int oddMan, const SkDPoint* endPt[2]) const override { |
| 227 | fCubic.otherPts(oddMan, endPt); |
| 228 | } |
| 229 | |
| 230 | int pointCount() const override { return SkDCubic::kPointCount; } |
| 231 | int pointLast() const override { return SkDCubic::kPointLast; } |
| 232 | SkDPoint ptAtT(double t) const override { return fCubic.ptAtT(t); } |
| 233 | void setBounds(SkDRect* ) const override; |
| 234 | |
| 235 | void subDivide(double t1, double t2, SkTCurve* curve) const override { |
| 236 | ((SkTCubic*) curve)->fCubic = fCubic.subDivide(t1, t2); |
| 237 | } |
| 238 | }; |
| 239 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 240 | #endif |