blob: 4f043fdf88f65e1514d811fb712a63795877a312 [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +00001/*
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
caryclark@google.comcffbcc32013-06-04 17:59:42 +000011#include "SkPath.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000012#include "SkPathOpsPoint.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000013
caryclark4209dcb2016-10-20 10:11:27 -070014struct SkDCubicPair;
caryclark@google.com07393ca2013-04-08 11:47:37 +000015
16struct SkDCubic {
caryclark54359292015-03-26 07:52:43 -070017 static const int kPointCount = 4;
18 static const int kPointLast = kPointCount - 1;
19 static const int kMaxIntersections = 9;
20
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +000021 enum SearchAxis {
22 kXAxis,
23 kYAxis
24 };
caryclark@google.com07393ca2013-04-08 11:47:37 +000025
caryclark54359292015-03-26 07:52:43 -070026 bool collapsed() const {
27 return fPts[0].approximatelyEqual(fPts[1]) && fPts[0].approximatelyEqual(fPts[2])
28 && fPts[0].approximatelyEqual(fPts[3]);
29 }
30
31 bool controlsInside() const {
32 SkDVector v01 = fPts[0] - fPts[1];
33 SkDVector v02 = fPts[0] - fPts[2];
34 SkDVector v03 = fPts[0] - fPts[3];
35 SkDVector v13 = fPts[1] - fPts[3];
36 SkDVector v23 = fPts[2] - fPts[3];
37 return v03.dot(v01) > 0 && v03.dot(v02) > 0 && v03.dot(v13) > 0 && v03.dot(v23) > 0;
38 }
39
caryclarked0935a2015-10-22 07:23:52 -070040 static bool IsConic() { return false; }
caryclark54359292015-03-26 07:52:43 -070041
42 const SkDPoint& operator[](int n) const { SkASSERT(n >= 0 && n < kPointCount); return fPts[n]; }
43 SkDPoint& operator[](int n) { SkASSERT(n >= 0 && n < kPointCount); return fPts[n]; }
caryclark@google.com07393ca2013-04-08 11:47:37 +000044
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +000045 void align(int endIndex, int ctrlIndex, SkDPoint* dstPt) const;
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +000046 double binarySearch(double min, double max, double axisIntercept, SearchAxis xAxis) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +000047 double calcPrecision() const;
48 SkDCubicPair chopAt(double t) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +000049 static void Coefficients(const double* cubic, double* A, double* B, double* C, double* D);
Cary Clark7eb01e02016-12-08 14:36:32 -050050 static int ComplexBreak(const SkPoint pts[4], SkScalar* t);
caryclark54359292015-03-26 07:52:43 -070051 int convexHull(char order[kPointCount]) const;
caryclark1049f122015-04-20 08:31:59 -070052
53 void debugInit() {
54 sk_bzero(fPts, sizeof(fPts));
55 }
56
caryclarka35ab3e2016-10-20 08:32:18 -070057 void debugSet(const SkDPoint* pts);
58
caryclark54359292015-03-26 07:52:43 -070059 void dump() const; // callable from the debugger when the implementation code is linked in
60 void dumpID(int id) const;
61 void dumpInner() const;
caryclark@google.com07393ca2013-04-08 11:47:37 +000062 SkDVector dxdyAtT(double t) const;
63 bool endsAreExtremaInXOrY() const;
caryclarkaec25102015-04-29 08:28:30 -070064 static int FindExtrema(const double src[], double tValue[2]);
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +000065 int findInflections(double tValues[2]) const;
caryclark@google.comb3f09212013-04-17 15:49:16 +000066
caryclark54359292015-03-26 07:52:43 -070067 static int FindInflections(const SkPoint a[kPointCount], double tValues[2]) {
caryclark@google.comb3f09212013-04-17 15:49:16 +000068 SkDCubic cubic;
caryclark624637c2015-05-11 07:21:27 -070069 return cubic.set(a).findInflections(tValues);
caryclark@google.comb3f09212013-04-17 15:49:16 +000070 }
71
caryclark@google.com07393ca2013-04-08 11:47:37 +000072 int findMaxCurvature(double tValues[]) const;
caryclarka35ab3e2016-10-20 08:32:18 -070073
74#ifdef SK_DEBUG
75 SkOpGlobalState* globalState() const { return fDebugGlobalState; }
76#endif
77
caryclark54359292015-03-26 07:52:43 -070078 bool hullIntersects(const SkDCubic& c2, bool* isLinear) const;
caryclark1049f122015-04-20 08:31:59 -070079 bool hullIntersects(const SkDConic& c, bool* isLinear) const;
80 bool hullIntersects(const SkDQuad& c2, bool* isLinear) const;
81 bool hullIntersects(const SkDPoint* pts, int ptCount, bool* isLinear) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +000082 bool isLinear(int startIndex, int endIndex) const;
Cary Clark0a671982018-10-11 12:16:49 -040083 static int maxIntersections() { return kMaxIntersections; }
caryclarkaec25102015-04-29 08:28:30 -070084 bool monotonicInX() const;
caryclark@google.com07393ca2013-04-08 11:47:37 +000085 bool monotonicInY() const;
caryclark54359292015-03-26 07:52:43 -070086 void otherPts(int index, const SkDPoint* o1Pts[kPointCount - 1]) const;
Cary Clark0a671982018-10-11 12:16:49 -040087 static int pointCount() { return kPointCount; }
88 static int pointLast() { return kPointLast; }
caryclark@google.com4fdbb222013-07-23 15:27:41 +000089 SkDPoint ptAtT(double t) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +000090 static int RootsReal(double A, double B, double C, double D, double t[3]);
91 static int RootsValidT(const double A, const double B, const double C, double D, double s[3]);
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +000092
93 int searchRoots(double extremes[6], int extrema, double axisIntercept,
94 SearchAxis xAxis, double* validRoots) const;
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +000095
Cary Clark7eb01e02016-12-08 14:36:32 -050096 bool toFloatPoints(SkPoint* ) const;
reeddc308852015-04-30 07:47:13 -070097 /**
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
caryclarka35ab3e2016-10-20 08:32:18 -0700108// add debug only global pointer so asserts can be skipped by fuzzers
109 const SkDCubic& set(const SkPoint pts[kPointCount]
110 SkDEBUGPARAMS(SkOpGlobalState* state = nullptr)) {
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +0000111 fPts[0] = pts[0];
112 fPts[1] = pts[1];
113 fPts[2] = pts[2];
114 fPts[3] = pts[3];
caryclarka35ab3e2016-10-20 08:32:18 -0700115 SkDEBUGCODE(fDebugGlobalState = state);
caryclark1049f122015-04-20 08:31:59 -0700116 return *this;
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +0000117 }
118
caryclark@google.com07393ca2013-04-08 11:47:37 +0000119 SkDCubic subDivide(double t1, double t2) const;
Cary Clark0a671982018-10-11 12:16:49 -0400120 void subDivide(double t1, double t2, SkDCubic* c) const { *c = this->subDivide(t1, t2); }
caryclark@google.comb3f09212013-04-17 15:49:16 +0000121
caryclark54359292015-03-26 07:52:43 -0700122 static SkDCubic SubDivide(const SkPoint a[kPointCount], double t1, double t2) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000123 SkDCubic cubic;
caryclark624637c2015-05-11 07:21:27 -0700124 return cubic.set(a).subDivide(t1, t2);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000125 }
caryclark@google.comb3f09212013-04-17 15:49:16 +0000126
caryclark@google.com07393ca2013-04-08 11:47:37 +0000127 void subDivide(const SkDPoint& a, const SkDPoint& d, double t1, double t2, SkDPoint p[2]) const;
128
caryclark54359292015-03-26 07:52:43 -0700129 static void SubDivide(const SkPoint pts[kPointCount], const SkDPoint& a, const SkDPoint& d, double t1,
caryclark@google.com07393ca2013-04-08 11:47:37 +0000130 double t2, SkDPoint p[2]) {
131 SkDCubic cubic;
caryclark624637c2015-05-11 07:21:27 -0700132 cubic.set(pts).subDivide(a, d, t1, t2, p);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000133 }
134
caryclark624637c2015-05-11 07:21:27 -0700135 double top(const SkDCubic& dCurve, double startT, double endT, SkDPoint*topPt) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000136 SkDQuad toQuad() const;
caryclark@google.com570863f2013-09-16 15:55:01 +0000137
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +0000138 static const int gPrecisionUnit;
caryclark54359292015-03-26 07:52:43 -0700139 SkDPoint fPts[kPointCount];
caryclarka35ab3e2016-10-20 08:32:18 -0700140 SkDEBUGCODE(SkOpGlobalState* fDebugGlobalState);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000141};
142
caryclark54359292015-03-26 07:52:43 -0700143/* Given the set [0, 1, 2, 3], and two of the four members, compute an XOR mask
144 that computes the other two. Note that:
145
146 one ^ two == 3 for (0, 3), (1, 2)
147 one ^ two < 3 for (0, 1), (0, 2), (1, 3), (2, 3)
148 3 - (one ^ two) is either 0, 1, or 2
149 1 >> (3 - (one ^ two)) is either 0 or 1
150thus:
151 returned == 2 for (0, 3), (1, 2)
152 returned == 3 for (0, 1), (0, 2), (1, 3), (2, 3)
153given that:
154 (0, 3) ^ 2 -> (2, 1) (1, 2) ^ 2 -> (3, 0)
155 (0, 1) ^ 3 -> (3, 2) (0, 2) ^ 3 -> (3, 1) (1, 3) ^ 3 -> (2, 0) (2, 3) ^ 3 -> (1, 0)
156*/
157inline int other_two(int one, int two) {
158 return 1 >> (3 - (one ^ two)) ^ 3;
159}
160
caryclark4209dcb2016-10-20 10:11:27 -0700161struct SkDCubicPair {
162 const SkDCubic first() const {
163#ifdef SK_DEBUG
164 SkDCubic result;
165 result.debugSet(&pts[0]);
166 return result;
167#else
168 return (const SkDCubic&) pts[0];
169#endif
170 }
171 const SkDCubic second() const {
172#ifdef SK_DEBUG
173 SkDCubic result;
174 result.debugSet(&pts[3]);
175 return result;
176#else
177 return (const SkDCubic&) pts[3];
178#endif
179 }
180 SkDPoint pts[7];
181};
182
Cary Clark0a671982018-10-11 12:16:49 -0400183#if PATH_OP_COMPILE_FOR_SIZE
184
185#include "SkArenaAlloc.h"
186#include "SkPathOpsTCurve.h"
187
188class SkTCubic : public SkTCurve {
189public:
190 SkDCubic fCubic;
191
192 SkTCubic() {}
193
194 SkTCubic(const SkDCubic& c)
195 : fCubic(c) {
196 }
197
198 ~SkTCubic() override {}
199
200 const SkDPoint& operator[](int n) const override { return fCubic[n]; }
201 SkDPoint& operator[](int n) override { return fCubic[n]; }
202
203 bool collapsed() const override { return fCubic.collapsed(); }
204 bool controlsInside() const override { return fCubic.controlsInside(); }
205 void debugInit() override { return fCubic.debugInit(); }
206 SkDVector dxdyAtT(double t) const override { return fCubic.dxdyAtT(t); }
207#ifdef SK_DEBUG
208 SkOpGlobalState* globalState() const override { return fCubic.globalState(); }
209#endif
210 bool hullIntersects(const SkDQuad& quad, bool* isLinear) const override;
211 bool hullIntersects(const SkDConic& conic, bool* isLinear) const override;
212
213 bool hullIntersects(const SkDCubic& cubic, bool* isLinear) const override {
214 return cubic.hullIntersects(fCubic, isLinear);
215 }
216
217 bool hullIntersects(const SkTCurve& curve, bool* isLinear) const override {
218 return curve.hullIntersects(fCubic, isLinear);
219 }
220
221 int intersectRay(SkIntersections* i, const SkDLine& line) const override;
222 bool IsConic() const override { return false; }
223 SkTCurve* make(SkArenaAlloc& heap) const override { return heap.make<SkTCubic>(); }
224
225 int maxIntersections() const override { return SkDCubic::kMaxIntersections; }
226
227 void otherPts(int oddMan, const SkDPoint* endPt[2]) const override {
228 fCubic.otherPts(oddMan, endPt);
229 }
230
231 int pointCount() const override { return SkDCubic::kPointCount; }
232 int pointLast() const override { return SkDCubic::kPointLast; }
233 SkDPoint ptAtT(double t) const override { return fCubic.ptAtT(t); }
234 void setBounds(SkDRect* ) const override;
235
236 void subDivide(double t1, double t2, SkTCurve* curve) const override {
237 ((SkTCubic*) curve)->fCubic = fCubic.subDivide(t1, t2);
238 }
239};
240
241#endif // PATH_OP_COMPILE_FOR_SIZE
242
caryclark@google.com07393ca2013-04-08 11:47:37 +0000243#endif