2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkIntersections.h"
10 void 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));
16 int (SkIntersections::* const CurveVertical[])(const SkPoint[], SkScalar, SkScalar, SkScalar, bool) = {
18 &SkIntersections::verticalLine,
19 &SkIntersections::verticalQuad,
20 &SkIntersections::verticalCubic
23 int ( SkIntersections::* const CurveRay[])(const SkPoint[], const SkDLine&) = {
25 &SkIntersections::lineRay,
26 &SkIntersections::quadRay,
27 &SkIntersections::cubicRay
30 int SkIntersections::coincidentUsed() const {
31 if (!fIsCoincident[0]) {
32 SkASSERT(!fIsCoincident[1]);
36 SkDEBUGCODE(int count2 = 0;)
37 for (int index = 0; index < fUsed; ++index) {
38 if (fIsCoincident[0] & (1 << index)) {
42 if (fIsCoincident[1] & (1 << index)) {
47 SkASSERT(count == count2);
51 int SkIntersections::cubicRay(const SkPoint pts[4], const SkDLine& line) {
55 return intersectRay(cubic, line);
58 void SkIntersections::flip() {
59 for (int index = 0; index < fUsed; ++index) {
60 fT[1][index] = 1 - fT[1][index];
64 int SkIntersections::insert(double one, double two, const SkDPoint& pt) {
65 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
69 SkASSERT(fUsed <= 1 || fT[0][0] <= fT[0][1]);
71 for (index = 0; index < fUsed; ++index) {
72 double oldOne = fT[0][index];
73 double oldTwo = fT[1][index];
74 if (one == oldOne && two == oldTwo) {
77 if (more_roughly_equal(oldOne, one) && more_roughly_equal(oldTwo, two)) {
78 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))) {
89 if (pt.roughlyEqual(fPt[index])) {
90 SkDebugf("%s t=%1.9g pts roughly equal\n", __FUNCTION__, one);
93 if (fT[0][index] > one) {
98 SkASSERT(0); // FIXME : this error, if it is to be handled at runtime in release, must
99 // be propagated all the way back down to the caller, and return failure.
103 int remaining = fUsed - index;
105 memmove(&fPt[index + 1], &fPt[index], sizeof(fPt[0]) * remaining);
106 memmove(&fPt2[index + 1], &fPt2[index], sizeof(fPt2[0]) * remaining);
107 memmove(&fT[0][index + 1], &fT[0][index], sizeof(fT[0][0]) * remaining);
108 memmove(&fT[1][index + 1], &fT[1][index], sizeof(fT[1][0]) * remaining);
109 int clearMask = ~((1 << index) - 1);
110 fIsCoincident[0] += fIsCoincident[0] & clearMask;
111 fIsCoincident[1] += fIsCoincident[1] & clearMask;
120 void SkIntersections::insertNear(double one, double two, const SkDPoint& pt1, const SkDPoint& pt2) {
121 SkASSERT(one == 0 || one == 1);
122 SkASSERT(two == 0 || two == 1);
123 SkASSERT(pt1 != pt2);
124 SkASSERT(fNearlySame[(int) one]);
125 (void) insert(one, two, pt1);
126 fPt2[one ? fUsed - 1 : 0] = pt2;
129 void SkIntersections::insertCoincident(double one, double two, const SkDPoint& pt) {
130 int index = insertSwap(one, two, pt);
131 int bit = 1 << index;
132 fIsCoincident[0] |= bit;
133 fIsCoincident[1] |= bit;
136 int SkIntersections::lineRay(const SkPoint pts[2], const SkDLine& line) {
140 return intersectRay(l, line);
143 void SkIntersections::offset(int base, double start, double end) {
144 for (int index = base; index < fUsed; ++index) {
145 double val = fT[fSwap][index];
148 fT[fSwap][index] = val;
152 int SkIntersections::quadRay(const SkPoint pts[3], const SkDLine& line) {
156 return intersectRay(quad, line);
159 void SkIntersections::quickRemoveOne(int index, int replace) {
160 if (index < replace) {
161 fT[0][index] = fT[0][replace];
165 void SkIntersections::removeOne(int index) {
166 int remaining = --fUsed - index;
167 if (remaining <= 0) {
170 memmove(&fPt[index], &fPt[index + 1], sizeof(fPt[0]) * remaining);
171 memmove(&fPt2[index], &fPt2[index + 1], sizeof(fPt2[0]) * remaining);
172 memmove(&fT[0][index], &fT[0][index + 1], sizeof(fT[0][0]) * remaining);
173 memmove(&fT[1][index], &fT[1][index + 1], sizeof(fT[1][0]) * remaining);
174 SkASSERT(fIsCoincident[0] == 0);
175 int coBit = fIsCoincident[0] & (1 << index);
176 fIsCoincident[0] -= ((fIsCoincident[0] >> 1) & ~((1 << index) - 1)) + coBit;
177 SkASSERT(!(coBit ^ (fIsCoincident[1] & (1 << index))));
178 fIsCoincident[1] -= ((fIsCoincident[1] >> 1) & ~((1 << index) - 1)) + coBit;
181 void SkIntersections::swapPts() {
183 for (index = 0; index < fUsed; ++index) {
184 SkTSwap(fT[0][index], fT[1][index]);
188 int SkIntersections::verticalLine(const SkPoint a[2], SkScalar top, SkScalar bottom,
189 SkScalar x, bool flipped) {
192 return vertical(line, top, bottom, x, flipped);
195 int SkIntersections::verticalQuad(const SkPoint a[3], SkScalar top, SkScalar bottom,
196 SkScalar x, bool flipped) {
199 return vertical(quad, top, bottom, x, flipped);
202 int SkIntersections::verticalCubic(const SkPoint a[4], SkScalar top, SkScalar bottom,
203 SkScalar x, bool flipped) {
206 return vertical(cubic, top, bottom, x, flipped);