blob: 608ffe3b6d49722bbe7cbed022465a305ef1d61c [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
10int (SkIntersections::*CurveVertical[])(const SkPoint[], SkScalar, SkScalar, SkScalar, bool) = {
11 NULL,
12 &SkIntersections::verticalLine,
13 &SkIntersections::verticalQuad,
14 &SkIntersections::verticalCubic
15};
16
17int (SkIntersections::*CurveRay[])(const SkPoint[], const SkDLine&) = {
18 NULL,
19 NULL,
20 &SkIntersections::quadRay,
21 &SkIntersections::cubicRay
22};
23
24int SkIntersections::coincidentUsed() const {
25 if (!fIsCoincident[0]) {
caryclark@google.comcffbcc32013-06-04 17:59:42 +000026 SkASSERT(!fIsCoincident[1]);
caryclark@google.com07393ca2013-04-08 11:47:37 +000027 return 0;
28 }
29 int count = 0;
30 SkDEBUGCODE(int count2 = 0;)
31 for (int index = 0; index < fUsed; ++index) {
32 if (fIsCoincident[0] & (1 << index)) {
33 ++count;
34 }
35#ifdef SK_DEBUG
36 if (fIsCoincident[1] & (1 << index)) {
37 ++count2;
38 }
39#endif
40 }
41 SkASSERT(count == count2);
42 return count;
43}
44
45int SkIntersections::cubicRay(const SkPoint pts[4], const SkDLine& line) {
46 SkDCubic cubic;
47 cubic.set(pts);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000048 fMax = 3;
caryclark@google.com07393ca2013-04-08 11:47:37 +000049 return intersectRay(cubic, line);
50}
51
52void SkIntersections::flip() {
53 for (int index = 0; index < fUsed; ++index) {
54 fT[1][index] = 1 - fT[1][index];
55 }
56}
57
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000058int SkIntersections::insert(double one, double two, const SkDPoint& pt) {
caryclark@google.comb3f09212013-04-17 15:49:16 +000059 if (fIsCoincident[0] == 3 && between(fT[0][0], one, fT[0][1])) {
60 // For now, don't allow a mix of coincident and non-coincident intersections
61 return -1;
62 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000063 SkASSERT(fUsed <= 1 || fT[0][0] <= fT[0][1]);
64 int index;
65 for (index = 0; index < fUsed; ++index) {
66 double oldOne = fT[0][index];
67 double oldTwo = fT[1][index];
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000068 if (one == oldOne && two == oldTwo) {
69 return -1;
70 }
71 if (more_roughly_equal(oldOne, one) && more_roughly_equal(oldTwo, two)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000072 if ((precisely_zero(one) && !precisely_zero(oldOne))
73 || (precisely_equal(one, 1) && !precisely_equal(oldOne, 1))
74 || (precisely_zero(two) && !precisely_zero(oldTwo))
75 || (precisely_equal(two, 1) && !precisely_equal(oldTwo, 1))) {
76 fT[0][index] = one;
77 fT[1][index] = two;
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000078 fPt[index] = pt;
caryclark@google.com07393ca2013-04-08 11:47:37 +000079 }
80 return -1;
81 }
82 #if ONE_OFF_DEBUG
83 if (pt.roughlyEqual(fPt[index])) {
84 SkDebugf("%s t=%1.9g pts roughly equal\n", __FUNCTION__, one);
85 }
86 #endif
87 if (fT[0][index] > one) {
88 break;
89 }
90 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000091 if (fUsed >= fMax) {
92 SkASSERT(0); // FIXME : this error, if it is to be handled at runtime in release, must
93 // be propagated all the way back down to the caller, and return failure.
94 fUsed = 0;
95 return 0;
96 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000097 int remaining = fUsed - index;
98 if (remaining > 0) {
99 memmove(&fPt[index + 1], &fPt[index], sizeof(fPt[0]) * remaining);
100 memmove(&fT[0][index + 1], &fT[0][index], sizeof(fT[0][0]) * remaining);
101 memmove(&fT[1][index + 1], &fT[1][index], sizeof(fT[1][0]) * remaining);
caryclark@google.com570863f2013-09-16 15:55:01 +0000102 int clearMask = ~((1 << index) - 1);
103 fIsCoincident[0] += fIsCoincident[0] & clearMask;
104 fIsCoincident[1] += fIsCoincident[1] & clearMask;
105 fIsNear += fIsNear & clearMask;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000106 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000107 fPt[index] = pt;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000108 fT[0][index] = one;
109 fT[1][index] = two;
110 ++fUsed;
111 return index;
112}
113
caryclark@google.com570863f2013-09-16 15:55:01 +0000114void SkIntersections::insertNear(double one, double two, const SkDPoint& pt) {
115 int index = insert(one, two, pt);
116 if (index < 0) {
117 return;
118 }
119 fIsNear |= 1 << index;
120}
121
caryclark@google.com07393ca2013-04-08 11:47:37 +0000122void SkIntersections::insertCoincident(double one, double two, const SkDPoint& pt) {
123 int index = insertSwap(one, two, pt);
124 int bit = 1 << index;
125 fIsCoincident[0] |= bit;
126 fIsCoincident[1] |= bit;
127}
128
129void SkIntersections::offset(int base, double start, double end) {
130 for (int index = base; index < fUsed; ++index) {
131 double val = fT[fSwap][index];
132 val *= end - start;
133 val += start;
134 fT[fSwap][index] = val;
135 }
136}
137
138int SkIntersections::quadRay(const SkPoint pts[3], const SkDLine& line) {
139 SkDQuad quad;
140 quad.set(pts);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000141 fMax = 2;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000142 return intersectRay(quad, line);
143}
144
145void SkIntersections::quickRemoveOne(int index, int replace) {
146 if (index < replace) {
147 fT[0][index] = fT[0][replace];
148 }
149}
150
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000151#if 0
caryclark@google.com07393ca2013-04-08 11:47:37 +0000152void SkIntersections::remove(double one, double two, const SkDPoint& startPt,
153 const SkDPoint& endPt) {
154 for (int index = fUsed - 1; index >= 0; --index) {
155 if (!(fIsCoincident[0] & (1 << index)) && (between(one, fT[fSwap][index], two)
156 || startPt.approximatelyEqual(fPt[index])
157 || endPt.approximatelyEqual(fPt[index]))) {
158 SkASSERT(fUsed > 0);
159 removeOne(index);
160 }
161 }
162}
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000163#endif
caryclark@google.com07393ca2013-04-08 11:47:37 +0000164
165void SkIntersections::removeOne(int index) {
166 int remaining = --fUsed - index;
167 if (remaining <= 0) {
168 return;
169 }
170 memmove(&fPt[index], &fPt[index + 1], sizeof(fPt[0]) * remaining);
171 memmove(&fT[0][index], &fT[0][index + 1], sizeof(fT[0][0]) * remaining);
172 memmove(&fT[1][index], &fT[1][index + 1], sizeof(fT[1][0]) * remaining);
173 SkASSERT(fIsCoincident[0] == 0);
174 int coBit = fIsCoincident[0] & (1 << index);
175 fIsCoincident[0] -= ((fIsCoincident[0] >> 1) & ~((1 << index) - 1)) + coBit;
176 SkASSERT(!(coBit ^ (fIsCoincident[1] & (1 << index))));
177 fIsCoincident[1] -= ((fIsCoincident[1] >> 1) & ~((1 << index) - 1)) + coBit;
caryclark@google.com570863f2013-09-16 15:55:01 +0000178 fIsNear -= ((fIsNear >> 1) & ~((1 << index) - 1)) + (fIsNear & (1 << index));
caryclark@google.com07393ca2013-04-08 11:47:37 +0000179}
180
181void SkIntersections::swapPts() {
182 int index;
183 for (index = 0; index < fUsed; ++index) {
184 SkTSwap(fT[0][index], fT[1][index]);
185 }
186}
187
188int SkIntersections::verticalLine(const SkPoint a[2], SkScalar top, SkScalar bottom,
189 SkScalar x, bool flipped) {
190 SkDLine line;
191 line.set(a);
192 return vertical(line, top, bottom, x, flipped);
193}
194
195int SkIntersections::verticalQuad(const SkPoint a[3], SkScalar top, SkScalar bottom,
196 SkScalar x, bool flipped) {
197 SkDQuad quad;
198 quad.set(a);
199 return vertical(quad, top, bottom, x, flipped);
200}
201
202int SkIntersections::verticalCubic(const SkPoint a[4], SkScalar top, SkScalar bottom,
203 SkScalar x, bool flipped) {
204 SkDCubic cubic;
205 cubic.set(a);
206 return vertical(cubic, top, bottom, x, flipped);
207}