blob: 0e8cd27593f39f2c4e9178b4184fe8cd2d174e65 [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 SkPathOpsQuad_DEFINED
9#define SkPathOpsQuad_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/SkArenaAlloc.h"
12#include "src/pathops/SkPathOpsTCurve.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000013
caryclark03b03ca2015-04-23 09:13:37 -070014struct SkOpCurve;
15
caryclark@google.com07393ca2013-04-08 11:47:37 +000016struct 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
22struct SkDQuad {
caryclark54359292015-03-26 07:52:43 -070023 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.com07393ca2013-04-08 11:47:37 +000039
caryclark1049f122015-04-20 08:31:59 -070040 void debugInit() {
41 sk_bzero(fPts, sizeof(fPts));
42 }
43
caryclarka35ab3e2016-10-20 08:32:18 -070044 void debugSet(const SkDPoint* pts);
45
caryclark@google.comcffbcc32013-06-04 17:59:42 +000046 SkDQuad flip() const {
caryclarka35ab3e2016-10-20 08:32:18 -070047 SkDQuad result = {{fPts[2], fPts[1], fPts[0]} SkDEBUGPARAMS(fDebugGlobalState) };
caryclark@google.comcffbcc32013-06-04 17:59:42 +000048 return result;
49 }
50
caryclarked0935a2015-10-22 07:23:52 -070051 static bool IsConic() { return false; }
caryclark54359292015-03-26 07:52:43 -070052
caryclarka35ab3e2016-10-20 08:32:18 -070053 const SkDQuad& set(const SkPoint pts[kPointCount]
54 SkDEBUGPARAMS(SkOpGlobalState* state = nullptr)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000055 fPts[0] = pts[0];
56 fPts[1] = pts[1];
57 fPts[2] = pts[2];
caryclarka35ab3e2016-10-20 08:32:18 -070058 SkDEBUGCODE(fDebugGlobalState = state);
caryclark1049f122015-04-20 08:31:59 -070059 return *this;
caryclark@google.com07393ca2013-04-08 11:47:37 +000060 }
61
caryclark54359292015-03-26 07:52:43 -070062 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.com07393ca2013-04-08 11:47:37 +000064
65 static int AddValidTs(double s[], int realRoots, double* t);
caryclark@google.comcffbcc32013-06-04 17:59:42 +000066 void align(int endIndex, SkDPoint* dstPt) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +000067 SkDQuadPair chopAt(double t) const;
68 SkDVector dxdyAtT(double t) const;
caryclarkaec25102015-04-29 08:28:30 -070069 static int FindExtrema(const double src[], double tValue[1]);
caryclark0449bcf2016-02-09 13:25:45 -080070
caryclarka35ab3e2016-10-20 08:32:18 -070071#ifdef SK_DEBUG
72 SkOpGlobalState* globalState() const { return fDebugGlobalState; }
73#endif
74
caryclark0449bcf2016-02-09 13:25:45 -080075 /**
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
caryclark54359292015-03-26 07:52:43 -070081 bool hullIntersects(const SkDQuad& , bool* isLinear) const;
caryclark1049f122015-04-20 08:31:59 -070082 bool hullIntersects(const SkDConic& , bool* isLinear) const;
83 bool hullIntersects(const SkDCubic& , bool* isLinear) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +000084 bool isLinear(int startIndex, int endIndex) const;
Cary Clark0a671982018-10-11 12:16:49 -040085 static int maxIntersections() { return kMaxIntersections; }
caryclarkaec25102015-04-29 08:28:30 -070086 bool monotonicInX() const;
caryclark@google.com07393ca2013-04-08 11:47:37 +000087 bool monotonicInY() const;
caryclark54359292015-03-26 07:52:43 -070088 void otherPts(int oddMan, const SkDPoint* endPt[2]) const;
Cary Clark0a671982018-10-11 12:16:49 -040089 static int pointCount() { return kPointCount; }
90 static int pointLast() { return kPointLast; }
caryclark@google.com4fdbb222013-07-23 15:27:41 +000091 SkDPoint ptAtT(double t) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +000092 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 Clark0a671982018-10-11 12:16:49 -040096 void subDivide(double t1, double t2, SkDQuad* quad) const { *quad = this->subDivide(t1, t2); }
97
caryclark54359292015-03-26 07:52:43 -070098 static SkDQuad SubDivide(const SkPoint a[kPointCount], double t1, double t2) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000099 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;
caryclark54359292015-03-26 07:52:43 -0700104 static SkDPoint SubDivide(const SkPoint pts[kPointCount], const SkDPoint& a, const SkDPoint& c,
caryclark@google.com07393ca2013-04-08 11:47:37 +0000105 double t1, double t2) {
106 SkDQuad quad;
107 quad.set(pts);
108 return quad.subDivide(a, c, t1, t2);
109 }
caryclark@google.com570863f2013-09-16 15:55:01 +0000110
caryclark0449bcf2016-02-09 13:25:45 -0800111 /**
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
caryclark624637c2015-05-11 07:21:27 -0700117 SkDCubic debugToCubic() const;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000118 // utilities callable by the user from the debugger when the implementation code is linked in
119 void dump() const;
caryclark54359292015-03-26 07:52:43 -0700120 void dumpID(int id) const;
121 void dumpInner() const;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000122
caryclarka35ab3e2016-10-20 08:32:18 -0700123 SkDEBUGCODE(SkOpGlobalState* fDebugGlobalState);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000124};
125
Cary Clark0a671982018-10-11 12:16:49 -0400126
127class SkTQuad : public SkTCurve {
128public:
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 Clark8762fb62018-10-16 16:06:24 -0400145#if DEBUG_T_SECT
146 void dumpID(int id) const override { return fQuad.dumpID(id); }
147#endif
Cary Clark0a671982018-10-11 12:16:49 -0400148 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.com07393ca2013-04-08 11:47:37 +0000184#endif