blob: e9875cf69df37cfc29617c50a71065b2f73177ec [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#include "SkIntersections.h"
9
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +000010void SkIntersections::append(const SkIntersections& i) {
11 for (int index = 0; index < i.fUsed; ++index) {
12 insert(i[0][index], i[1][index], i.pt(index));
13 }
14}
15
caryclark2e403812014-08-25 06:53:04 -070016int (SkIntersections::* const CurveVertical[])(const SkPoint[], SkScalar, SkScalar, SkScalar, bool) = {
caryclark@google.com07393ca2013-04-08 11:47:37 +000017 NULL,
18 &SkIntersections::verticalLine,
19 &SkIntersections::verticalQuad,
20 &SkIntersections::verticalCubic
21};
22
caryclark2e403812014-08-25 06:53:04 -070023int ( SkIntersections::* const CurveRay[])(const SkPoint[], const SkDLine&) = {
caryclark@google.com07393ca2013-04-08 11:47:37 +000024 NULL,
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +000025 &SkIntersections::lineRay,
caryclark@google.com07393ca2013-04-08 11:47:37 +000026 &SkIntersections::quadRay,
27 &SkIntersections::cubicRay
28};
29
30int SkIntersections::coincidentUsed() const {
31 if (!fIsCoincident[0]) {
caryclark@google.comcffbcc32013-06-04 17:59:42 +000032 SkASSERT(!fIsCoincident[1]);
caryclark@google.com07393ca2013-04-08 11:47:37 +000033 return 0;
34 }
35 int count = 0;
36 SkDEBUGCODE(int count2 = 0;)
37 for (int index = 0; index < fUsed; ++index) {
38 if (fIsCoincident[0] & (1 << index)) {
39 ++count;
40 }
41#ifdef SK_DEBUG
42 if (fIsCoincident[1] & (1 << index)) {
43 ++count2;
44 }
45#endif
46 }
47 SkASSERT(count == count2);
48 return count;
49}
50
51int SkIntersections::cubicRay(const SkPoint pts[4], const SkDLine& line) {
52 SkDCubic cubic;
53 cubic.set(pts);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000054 fMax = 3;
caryclark@google.com07393ca2013-04-08 11:47:37 +000055 return intersectRay(cubic, line);
56}
57
58void SkIntersections::flip() {
59 for (int index = 0; index < fUsed; ++index) {
60 fT[1][index] = 1 - fT[1][index];
61 }
62}
63
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000064int SkIntersections::insert(double one, double two, const SkDPoint& pt) {
caryclark@google.comb3f09212013-04-17 15:49:16 +000065 if (fIsCoincident[0] == 3 && between(fT[0][0], one, fT[0][1])) {
66 // For now, don't allow a mix of coincident and non-coincident intersections
67 return -1;
68 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000069 SkASSERT(fUsed <= 1 || fT[0][0] <= fT[0][1]);
70 int index;
71 for (index = 0; index < fUsed; ++index) {
72 double oldOne = fT[0][index];
73 double oldTwo = fT[1][index];
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000074 if (one == oldOne && two == oldTwo) {
75 return -1;
76 }
77 if (more_roughly_equal(oldOne, one) && more_roughly_equal(oldTwo, two)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000078 if ((precisely_zero(one) && !precisely_zero(oldOne))
79 || (precisely_equal(one, 1) && !precisely_equal(oldOne, 1))
80 || (precisely_zero(two) && !precisely_zero(oldTwo))
81 || (precisely_equal(two, 1) && !precisely_equal(oldTwo, 1))) {
caryclark65f55312014-11-13 06:58:52 -080082 SkASSERT(one >= 0 && one <= 1);
83 SkASSERT(two >= 0 && two <= 1);
caryclark@google.com07393ca2013-04-08 11:47:37 +000084 fT[0][index] = one;
85 fT[1][index] = two;
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000086 fPt[index] = pt;
caryclark@google.com07393ca2013-04-08 11:47:37 +000087 }
88 return -1;
89 }
90 #if ONE_OFF_DEBUG
91 if (pt.roughlyEqual(fPt[index])) {
92 SkDebugf("%s t=%1.9g pts roughly equal\n", __FUNCTION__, one);
93 }
94 #endif
95 if (fT[0][index] > one) {
96 break;
97 }
98 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000099 if (fUsed >= fMax) {
100 SkASSERT(0); // FIXME : this error, if it is to be handled at runtime in release, must
101 // be propagated all the way back down to the caller, and return failure.
102 fUsed = 0;
103 return 0;
104 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000105 int remaining = fUsed - index;
106 if (remaining > 0) {
107 memmove(&fPt[index + 1], &fPt[index], sizeof(fPt[0]) * remaining);
caryclarkdac1d172014-06-17 05:15:38 -0700108 memmove(&fPt2[index + 1], &fPt2[index], sizeof(fPt2[0]) * remaining);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000109 memmove(&fT[0][index + 1], &fT[0][index], sizeof(fT[0][0]) * remaining);
110 memmove(&fT[1][index + 1], &fT[1][index], sizeof(fT[1][0]) * remaining);
caryclark@google.com570863f2013-09-16 15:55:01 +0000111 int clearMask = ~((1 << index) - 1);
112 fIsCoincident[0] += fIsCoincident[0] & clearMask;
113 fIsCoincident[1] += fIsCoincident[1] & clearMask;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000114 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000115 fPt[index] = pt;
caryclark65f55312014-11-13 06:58:52 -0800116 SkASSERT(one >= 0 && one <= 1);
117 SkASSERT(two >= 0 && two <= 1);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000118 fT[0][index] = one;
119 fT[1][index] = two;
120 ++fUsed;
121 return index;
122}
123
caryclarkdac1d172014-06-17 05:15:38 -0700124void SkIntersections::insertNear(double one, double two, const SkDPoint& pt1, const SkDPoint& pt2) {
125 SkASSERT(one == 0 || one == 1);
126 SkASSERT(two == 0 || two == 1);
127 SkASSERT(pt1 != pt2);
128 SkASSERT(fNearlySame[(int) one]);
129 (void) insert(one, two, pt1);
130 fPt2[one ? fUsed - 1 : 0] = pt2;
131}
132
caryclark@google.com07393ca2013-04-08 11:47:37 +0000133void SkIntersections::insertCoincident(double one, double two, const SkDPoint& pt) {
134 int index = insertSwap(one, two, pt);
135 int bit = 1 << index;
136 fIsCoincident[0] |= bit;
137 fIsCoincident[1] |= bit;
138}
139
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000140int SkIntersections::lineRay(const SkPoint pts[2], const SkDLine& line) {
141 SkDLine l;
142 l.set(pts);
143 fMax = 2;
144 return intersectRay(l, line);
145}
146
caryclark@google.com07393ca2013-04-08 11:47:37 +0000147void SkIntersections::offset(int base, double start, double end) {
148 for (int index = base; index < fUsed; ++index) {
149 double val = fT[fSwap][index];
150 val *= end - start;
151 val += start;
152 fT[fSwap][index] = val;
153 }
154}
155
156int SkIntersections::quadRay(const SkPoint pts[3], const SkDLine& line) {
157 SkDQuad quad;
158 quad.set(pts);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000159 fMax = 2;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000160 return intersectRay(quad, line);
161}
162
163void SkIntersections::quickRemoveOne(int index, int replace) {
164 if (index < replace) {
165 fT[0][index] = fT[0][replace];
166 }
167}
168
caryclark@google.com07393ca2013-04-08 11:47:37 +0000169void SkIntersections::removeOne(int index) {
170 int remaining = --fUsed - index;
171 if (remaining <= 0) {
172 return;
173 }
174 memmove(&fPt[index], &fPt[index + 1], sizeof(fPt[0]) * remaining);
caryclarkdac1d172014-06-17 05:15:38 -0700175 memmove(&fPt2[index], &fPt2[index + 1], sizeof(fPt2[0]) * remaining);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000176 memmove(&fT[0][index], &fT[0][index + 1], sizeof(fT[0][0]) * remaining);
177 memmove(&fT[1][index], &fT[1][index + 1], sizeof(fT[1][0]) * remaining);
caryclark65f55312014-11-13 06:58:52 -0800178// SkASSERT(fIsCoincident[0] == 0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000179 int coBit = fIsCoincident[0] & (1 << index);
180 fIsCoincident[0] -= ((fIsCoincident[0] >> 1) & ~((1 << index) - 1)) + coBit;
181 SkASSERT(!(coBit ^ (fIsCoincident[1] & (1 << index))));
182 fIsCoincident[1] -= ((fIsCoincident[1] >> 1) & ~((1 << index) - 1)) + coBit;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000183}
184
185void SkIntersections::swapPts() {
186 int index;
187 for (index = 0; index < fUsed; ++index) {
188 SkTSwap(fT[0][index], fT[1][index]);
189 }
190}
191
192int SkIntersections::verticalLine(const SkPoint a[2], SkScalar top, SkScalar bottom,
193 SkScalar x, bool flipped) {
194 SkDLine line;
195 line.set(a);
196 return vertical(line, top, bottom, x, flipped);
197}
198
199int SkIntersections::verticalQuad(const SkPoint a[3], SkScalar top, SkScalar bottom,
200 SkScalar x, bool flipped) {
201 SkDQuad quad;
202 quad.set(a);
203 return vertical(quad, top, bottom, x, flipped);
204}
205
206int SkIntersections::verticalCubic(const SkPoint a[4], SkScalar top, SkScalar bottom,
207 SkScalar x, bool flipped) {
208 SkDCubic cubic;
209 cubic.set(a);
210 return vertical(cubic, top, bottom, x, flipped);
211}