blob: 2e8a5db3f762a8fe07ae7f662e0e419f8133be73 [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;
caryclarkaec25102015-04-29 08:28:30 -070083 bool monotonicInX() const;
caryclark@google.com07393ca2013-04-08 11:47:37 +000084 bool monotonicInY() const;
caryclark54359292015-03-26 07:52:43 -070085 void otherPts(int index, const SkDPoint* o1Pts[kPointCount - 1]) const;
caryclark@google.com4fdbb222013-07-23 15:27:41 +000086 SkDPoint ptAtT(double t) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +000087 static int RootsReal(double A, double B, double C, double D, double t[3]);
88 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 +000089
90 int searchRoots(double extremes[6], int extrema, double axisIntercept,
91 SearchAxis xAxis, double* validRoots) const;
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +000092
Cary Clark7eb01e02016-12-08 14:36:32 -050093 bool toFloatPoints(SkPoint* ) const;
reeddc308852015-04-30 07:47:13 -070094 /**
95 * Return the number of valid roots (0 < root < 1) for this cubic intersecting the
96 * specified horizontal line.
97 */
98 int horizontalIntersect(double yIntercept, double roots[3]) const;
99 /**
100 * Return the number of valid roots (0 < root < 1) for this cubic intersecting the
101 * specified vertical line.
102 */
103 int verticalIntersect(double xIntercept, double roots[3]) const;
104
caryclarka35ab3e2016-10-20 08:32:18 -0700105// add debug only global pointer so asserts can be skipped by fuzzers
106 const SkDCubic& set(const SkPoint pts[kPointCount]
107 SkDEBUGPARAMS(SkOpGlobalState* state = nullptr)) {
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +0000108 fPts[0] = pts[0];
109 fPts[1] = pts[1];
110 fPts[2] = pts[2];
111 fPts[3] = pts[3];
caryclarka35ab3e2016-10-20 08:32:18 -0700112 SkDEBUGCODE(fDebugGlobalState = state);
caryclark1049f122015-04-20 08:31:59 -0700113 return *this;
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +0000114 }
115
caryclark@google.com07393ca2013-04-08 11:47:37 +0000116 SkDCubic subDivide(double t1, double t2) const;
caryclark@google.comb3f09212013-04-17 15:49:16 +0000117
caryclark54359292015-03-26 07:52:43 -0700118 static SkDCubic SubDivide(const SkPoint a[kPointCount], double t1, double t2) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000119 SkDCubic cubic;
caryclark624637c2015-05-11 07:21:27 -0700120 return cubic.set(a).subDivide(t1, t2);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000121 }
caryclark@google.comb3f09212013-04-17 15:49:16 +0000122
caryclark@google.com07393ca2013-04-08 11:47:37 +0000123 void subDivide(const SkDPoint& a, const SkDPoint& d, double t1, double t2, SkDPoint p[2]) const;
124
caryclark54359292015-03-26 07:52:43 -0700125 static void SubDivide(const SkPoint pts[kPointCount], const SkDPoint& a, const SkDPoint& d, double t1,
caryclark@google.com07393ca2013-04-08 11:47:37 +0000126 double t2, SkDPoint p[2]) {
127 SkDCubic cubic;
caryclark624637c2015-05-11 07:21:27 -0700128 cubic.set(pts).subDivide(a, d, t1, t2, p);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000129 }
130
caryclark624637c2015-05-11 07:21:27 -0700131 double top(const SkDCubic& dCurve, double startT, double endT, SkDPoint*topPt) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000132 SkDQuad toQuad() const;
caryclark@google.com570863f2013-09-16 15:55:01 +0000133
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +0000134 static const int gPrecisionUnit;
caryclark54359292015-03-26 07:52:43 -0700135 SkDPoint fPts[kPointCount];
caryclarka35ab3e2016-10-20 08:32:18 -0700136 SkDEBUGCODE(SkOpGlobalState* fDebugGlobalState);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000137};
138
caryclark54359292015-03-26 07:52:43 -0700139/* 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
146thus:
147 returned == 2 for (0, 3), (1, 2)
148 returned == 3 for (0, 1), (0, 2), (1, 3), (2, 3)
149given 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*/
153inline int other_two(int one, int two) {
154 return 1 >> (3 - (one ^ two)) ^ 3;
155}
156
caryclark4209dcb2016-10-20 10:11:27 -0700157struct SkDCubicPair {
158 const SkDCubic first() const {
159#ifdef SK_DEBUG
160 SkDCubic result;
161 result.debugSet(&pts[0]);
162 return result;
163#else
164 return (const SkDCubic&) pts[0];
165#endif
166 }
167 const SkDCubic second() const {
168#ifdef SK_DEBUG
169 SkDCubic result;
170 result.debugSet(&pts[3]);
171 return result;
172#else
173 return (const SkDCubic&) pts[3];
174#endif
175 }
176 SkDPoint pts[7];
177};
178
caryclark@google.com07393ca2013-04-08 11:47:37 +0000179#endif