blob: 9932e1d1bc300917783e4aa9416dad6f938dbf54 [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
14struct SkDCubicPair {
15 const SkDCubic& first() const { return (const SkDCubic&) pts[0]; }
16 const SkDCubic& second() const { return (const SkDCubic&) pts[3]; }
17 SkDPoint pts[7];
18};
19
20struct SkDCubic {
caryclark54359292015-03-26 07:52:43 -070021 static const int kPointCount = 4;
22 static const int kPointLast = kPointCount - 1;
23 static const int kMaxIntersections = 9;
24
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +000025 enum SearchAxis {
26 kXAxis,
27 kYAxis
28 };
caryclark@google.com07393ca2013-04-08 11:47:37 +000029
caryclark54359292015-03-26 07:52:43 -070030 bool collapsed() const {
31 return fPts[0].approximatelyEqual(fPts[1]) && fPts[0].approximatelyEqual(fPts[2])
32 && fPts[0].approximatelyEqual(fPts[3]);
33 }
34
35 bool controlsInside() const {
36 SkDVector v01 = fPts[0] - fPts[1];
37 SkDVector v02 = fPts[0] - fPts[2];
38 SkDVector v03 = fPts[0] - fPts[3];
39 SkDVector v13 = fPts[1] - fPts[3];
40 SkDVector v23 = fPts[2] - fPts[3];
41 return v03.dot(v01) > 0 && v03.dot(v02) > 0 && v03.dot(v13) > 0 && v03.dot(v23) > 0;
42 }
43
44 static bool IsCubic() { return true; }
45
46 const SkDPoint& operator[](int n) const { SkASSERT(n >= 0 && n < kPointCount); return fPts[n]; }
47 SkDPoint& operator[](int n) { SkASSERT(n >= 0 && n < kPointCount); return fPts[n]; }
caryclark@google.com07393ca2013-04-08 11:47:37 +000048
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +000049 void align(int endIndex, int ctrlIndex, SkDPoint* dstPt) const;
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +000050 double binarySearch(double min, double max, double axisIntercept, SearchAxis xAxis) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +000051 double calcPrecision() const;
52 SkDCubicPair chopAt(double t) const;
53 bool clockwise() const;
54 static void Coefficients(const double* cubic, double* A, double* B, double* C, double* D);
caryclark54359292015-03-26 07:52:43 -070055 static bool ComplexBreak(const SkPoint pts[4], SkScalar* t);
56 int convexHull(char order[kPointCount]) const;
57 void dump() const; // callable from the debugger when the implementation code is linked in
58 void dumpID(int id) const;
59 void dumpInner() const;
caryclark@google.com07393ca2013-04-08 11:47:37 +000060 SkDVector dxdyAtT(double t) const;
61 bool endsAreExtremaInXOrY() const;
62 static int FindExtrema(double a, double b, double c, double d, double tValue[2]);
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +000063 int findInflections(double tValues[2]) const;
caryclark@google.comb3f09212013-04-17 15:49:16 +000064
caryclark54359292015-03-26 07:52:43 -070065 static int FindInflections(const SkPoint a[kPointCount], double tValues[2]) {
caryclark@google.comb3f09212013-04-17 15:49:16 +000066 SkDCubic cubic;
67 cubic.set(a);
68 return cubic.findInflections(tValues);
69 }
70
caryclark@google.com07393ca2013-04-08 11:47:37 +000071 int findMaxCurvature(double tValues[]) const;
caryclark54359292015-03-26 07:52:43 -070072 bool hullIntersects(const SkDCubic& c2, bool* isLinear) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +000073 bool isLinear(int startIndex, int endIndex) const;
74 bool monotonicInY() const;
caryclark54359292015-03-26 07:52:43 -070075 void otherPts(int index, const SkDPoint* o1Pts[kPointCount - 1]) const;
caryclark@google.com4fdbb222013-07-23 15:27:41 +000076 SkDPoint ptAtT(double t) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +000077 static int RootsReal(double A, double B, double C, double D, double t[3]);
78 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 +000079
80 int searchRoots(double extremes[6], int extrema, double axisIntercept,
81 SearchAxis xAxis, double* validRoots) const;
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +000082
caryclark54359292015-03-26 07:52:43 -070083 void set(const SkPoint pts[kPointCount]) {
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +000084 fPts[0] = pts[0];
85 fPts[1] = pts[1];
86 fPts[2] = pts[2];
87 fPts[3] = pts[3];
88 }
89
caryclark@google.com07393ca2013-04-08 11:47:37 +000090 SkDCubic subDivide(double t1, double t2) const;
caryclark@google.comb3f09212013-04-17 15:49:16 +000091
caryclark54359292015-03-26 07:52:43 -070092 static SkDCubic SubDivide(const SkPoint a[kPointCount], double t1, double t2) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000093 SkDCubic cubic;
94 cubic.set(a);
95 return cubic.subDivide(t1, t2);
96 }
caryclark@google.comb3f09212013-04-17 15:49:16 +000097
caryclark@google.com07393ca2013-04-08 11:47:37 +000098 void subDivide(const SkDPoint& a, const SkDPoint& d, double t1, double t2, SkDPoint p[2]) const;
99
caryclark54359292015-03-26 07:52:43 -0700100 static void SubDivide(const SkPoint pts[kPointCount], const SkDPoint& a, const SkDPoint& d, double t1,
caryclark@google.com07393ca2013-04-08 11:47:37 +0000101 double t2, SkDPoint p[2]) {
102 SkDCubic cubic;
103 cubic.set(pts);
104 cubic.subDivide(a, d, t1, t2, p);
105 }
106
107 SkDPoint top(double startT, double endT) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000108 SkDQuad toQuad() const;
caryclark@google.com570863f2013-09-16 15:55:01 +0000109
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +0000110 static const int gPrecisionUnit;
111
caryclark54359292015-03-26 07:52:43 -0700112 SkDPoint fPts[kPointCount];
caryclark@google.com07393ca2013-04-08 11:47:37 +0000113};
114
caryclark54359292015-03-26 07:52:43 -0700115/* Given the set [0, 1, 2, 3], and two of the four members, compute an XOR mask
116 that computes the other two. Note that:
117
118 one ^ two == 3 for (0, 3), (1, 2)
119 one ^ two < 3 for (0, 1), (0, 2), (1, 3), (2, 3)
120 3 - (one ^ two) is either 0, 1, or 2
121 1 >> (3 - (one ^ two)) is either 0 or 1
122thus:
123 returned == 2 for (0, 3), (1, 2)
124 returned == 3 for (0, 1), (0, 2), (1, 3), (2, 3)
125given that:
126 (0, 3) ^ 2 -> (2, 1) (1, 2) ^ 2 -> (3, 0)
127 (0, 1) ^ 3 -> (3, 2) (0, 2) ^ 3 -> (3, 1) (1, 3) ^ 3 -> (2, 0) (2, 3) ^ 3 -> (1, 0)
128*/
129inline int other_two(int one, int two) {
130 return 1 >> (3 - (one ^ two)) ^ 3;
131}
132
caryclark@google.com07393ca2013-04-08 11:47:37 +0000133#endif