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 SkPathOpsQuad_DEFINED |
| 9 | #define SkPathOpsQuad_DEFINED |
| 10 | |
Cary Clark | 8762fb6 | 2018-10-16 16:06:24 -0400 | [diff] [blame] | 11 | #include "SkArenaAlloc.h" |
| 12 | #include "SkPathOpsTCurve.h" |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 13 | |
caryclark | 03b03ca | 2015-04-23 09:13:37 -0700 | [diff] [blame] | 14 | struct SkOpCurve; |
| 15 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 16 | struct SkDQuadPair { |
| 17 | const SkDQuad& first() const { return (const SkDQuad&) pts[0]; } |
| 18 | const SkDQuad& second() const { return (const SkDQuad&) pts[2]; } |
| 19 | SkDPoint pts[5]; |
| 20 | }; |
| 21 | |
| 22 | struct SkDQuad { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 23 | static const int kPointCount = 3; |
| 24 | static const int kPointLast = kPointCount - 1; |
| 25 | static const int kMaxIntersections = 4; |
| 26 | |
| 27 | SkDPoint fPts[kPointCount]; |
| 28 | |
| 29 | bool collapsed() const { |
| 30 | return fPts[0].approximatelyEqual(fPts[1]) && fPts[0].approximatelyEqual(fPts[2]); |
| 31 | } |
| 32 | |
| 33 | bool controlsInside() const { |
| 34 | SkDVector v01 = fPts[0] - fPts[1]; |
| 35 | SkDVector v02 = fPts[0] - fPts[2]; |
| 36 | SkDVector v12 = fPts[1] - fPts[2]; |
| 37 | return v02.dot(v01) > 0 && v02.dot(v12) > 0; |
| 38 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 39 | |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 40 | void debugInit() { |
| 41 | sk_bzero(fPts, sizeof(fPts)); |
| 42 | } |
| 43 | |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 44 | void debugSet(const SkDPoint* pts); |
| 45 | |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 46 | SkDQuad flip() const { |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 47 | SkDQuad result = {{fPts[2], fPts[1], fPts[0]} SkDEBUGPARAMS(fDebugGlobalState) }; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 48 | return result; |
| 49 | } |
| 50 | |
caryclark | ed0935a | 2015-10-22 07:23:52 -0700 | [diff] [blame] | 51 | static bool IsConic() { return false; } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 52 | |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 53 | const SkDQuad& set(const SkPoint pts[kPointCount] |
| 54 | SkDEBUGPARAMS(SkOpGlobalState* state = nullptr)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 55 | fPts[0] = pts[0]; |
| 56 | fPts[1] = pts[1]; |
| 57 | fPts[2] = pts[2]; |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 58 | SkDEBUGCODE(fDebugGlobalState = state); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 59 | return *this; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 60 | } |
| 61 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 62 | const SkDPoint& operator[](int n) const { SkASSERT(n >= 0 && n < kPointCount); return fPts[n]; } |
| 63 | 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] | 64 | |
| 65 | static int AddValidTs(double s[], int realRoots, double* t); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 66 | void align(int endIndex, SkDPoint* dstPt) const; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 67 | SkDQuadPair chopAt(double t) const; |
| 68 | SkDVector dxdyAtT(double t) const; |
caryclark | aec2510 | 2015-04-29 08:28:30 -0700 | [diff] [blame] | 69 | static int FindExtrema(const double src[], double tValue[1]); |
caryclark | 0449bcf | 2016-02-09 13:25:45 -0800 | [diff] [blame] | 70 | |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 71 | #ifdef SK_DEBUG |
| 72 | SkOpGlobalState* globalState() const { return fDebugGlobalState; } |
| 73 | #endif |
| 74 | |
caryclark | 0449bcf | 2016-02-09 13:25:45 -0800 | [diff] [blame] | 75 | /** |
| 76 | * Return the number of valid roots (0 < root < 1) for this cubic intersecting the |
| 77 | * specified horizontal line. |
| 78 | */ |
| 79 | int horizontalIntersect(double yIntercept, double roots[2]) const; |
| 80 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 81 | bool hullIntersects(const SkDQuad& , bool* isLinear) const; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 82 | bool hullIntersects(const SkDConic& , bool* isLinear) const; |
| 83 | bool hullIntersects(const SkDCubic& , bool* isLinear) const; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 84 | bool isLinear(int startIndex, int endIndex) const; |
Cary Clark | 0a67198 | 2018-10-11 12:16:49 -0400 | [diff] [blame] | 85 | static int maxIntersections() { return kMaxIntersections; } |
caryclark | aec2510 | 2015-04-29 08:28:30 -0700 | [diff] [blame] | 86 | bool monotonicInX() const; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 87 | bool monotonicInY() const; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 88 | void otherPts(int oddMan, const SkDPoint* endPt[2]) const; |
Cary Clark | 0a67198 | 2018-10-11 12:16:49 -0400 | [diff] [blame] | 89 | static int pointCount() { return kPointCount; } |
| 90 | static int pointLast() { return kPointLast; } |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 91 | SkDPoint ptAtT(double t) const; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 92 | static int RootsReal(double A, double B, double C, double t[2]); |
| 93 | static int RootsValidT(const double A, const double B, const double C, double s[2]); |
| 94 | static void SetABC(const double* quad, double* a, double* b, double* c); |
| 95 | SkDQuad subDivide(double t1, double t2) const; |
Cary Clark | 0a67198 | 2018-10-11 12:16:49 -0400 | [diff] [blame] | 96 | void subDivide(double t1, double t2, SkDQuad* quad) const { *quad = this->subDivide(t1, t2); } |
| 97 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 98 | static SkDQuad SubDivide(const SkPoint a[kPointCount], double t1, double t2) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 99 | SkDQuad quad; |
| 100 | quad.set(a); |
| 101 | return quad.subDivide(t1, t2); |
| 102 | } |
| 103 | SkDPoint subDivide(const SkDPoint& a, const SkDPoint& c, double t1, double t2) const; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 104 | static SkDPoint SubDivide(const SkPoint pts[kPointCount], const SkDPoint& a, const SkDPoint& c, |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 105 | double t1, double t2) { |
| 106 | SkDQuad quad; |
| 107 | quad.set(pts); |
| 108 | return quad.subDivide(a, c, t1, t2); |
| 109 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 110 | |
caryclark | 0449bcf | 2016-02-09 13:25:45 -0800 | [diff] [blame] | 111 | /** |
| 112 | * Return the number of valid roots (0 < root < 1) for this cubic intersecting the |
| 113 | * specified vertical line. |
| 114 | */ |
| 115 | int verticalIntersect(double xIntercept, double roots[2]) const; |
| 116 | |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 117 | SkDCubic debugToCubic() const; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 118 | // utilities callable by the user from the debugger when the implementation code is linked in |
| 119 | void dump() const; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 120 | void dumpID(int id) const; |
| 121 | void dumpInner() const; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 122 | |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 123 | SkDEBUGCODE(SkOpGlobalState* fDebugGlobalState); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
Cary Clark | 0a67198 | 2018-10-11 12:16:49 -0400 | [diff] [blame] | 126 | |
| 127 | class SkTQuad : public SkTCurve { |
| 128 | public: |
| 129 | SkDQuad fQuad; |
| 130 | |
| 131 | SkTQuad() {} |
| 132 | |
| 133 | SkTQuad(const SkDQuad& q) |
| 134 | : fQuad(q) { |
| 135 | } |
| 136 | |
| 137 | ~SkTQuad() override {} |
| 138 | |
| 139 | const SkDPoint& operator[](int n) const override { return fQuad[n]; } |
| 140 | SkDPoint& operator[](int n) override { return fQuad[n]; } |
| 141 | |
| 142 | bool collapsed() const override { return fQuad.collapsed(); } |
| 143 | bool controlsInside() const override { return fQuad.controlsInside(); } |
| 144 | void debugInit() override { return fQuad.debugInit(); } |
Cary Clark | 8762fb6 | 2018-10-16 16:06:24 -0400 | [diff] [blame] | 145 | #if DEBUG_T_SECT |
| 146 | void dumpID(int id) const override { return fQuad.dumpID(id); } |
| 147 | #endif |
Cary Clark | 0a67198 | 2018-10-11 12:16:49 -0400 | [diff] [blame] | 148 | SkDVector dxdyAtT(double t) const override { return fQuad.dxdyAtT(t); } |
| 149 | #ifdef SK_DEBUG |
| 150 | SkOpGlobalState* globalState() const override { return fQuad.globalState(); } |
| 151 | #endif |
| 152 | |
| 153 | bool hullIntersects(const SkDQuad& quad, bool* isLinear) const override { |
| 154 | return quad.hullIntersects(fQuad, isLinear); |
| 155 | } |
| 156 | |
| 157 | bool hullIntersects(const SkDConic& conic, bool* isLinear) const override; |
| 158 | bool hullIntersects(const SkDCubic& cubic, bool* isLinear) const override; |
| 159 | |
| 160 | bool hullIntersects(const SkTCurve& curve, bool* isLinear) const override { |
| 161 | return curve.hullIntersects(fQuad, isLinear); |
| 162 | } |
| 163 | |
| 164 | int intersectRay(SkIntersections* i, const SkDLine& line) const override; |
| 165 | bool IsConic() const override { return false; } |
| 166 | SkTCurve* make(SkArenaAlloc& heap) const override { return heap.make<SkTQuad>(); } |
| 167 | |
| 168 | int maxIntersections() const override { return SkDQuad::kMaxIntersections; } |
| 169 | |
| 170 | void otherPts(int oddMan, const SkDPoint* endPt[2]) const override { |
| 171 | fQuad.otherPts(oddMan, endPt); |
| 172 | } |
| 173 | |
| 174 | int pointCount() const override { return SkDQuad::kPointCount; } |
| 175 | int pointLast() const override { return SkDQuad::kPointLast; } |
| 176 | SkDPoint ptAtT(double t) const override { return fQuad.ptAtT(t); } |
| 177 | void setBounds(SkDRect* ) const override; |
| 178 | |
| 179 | void subDivide(double t1, double t2, SkTCurve* curve) const override { |
| 180 | ((SkTQuad*) curve)->fQuad = fQuad.subDivide(t1, t2); |
| 181 | } |
| 182 | }; |
| 183 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 184 | #endif |