michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #include "SkIntersections.h" michael@0: #include "SkPathOpsLine.h" michael@0: michael@0: /* Determine the intersection point of two lines. This assumes the lines are not parallel, michael@0: and that that the lines are infinite. michael@0: From http://en.wikipedia.org/wiki/Line-line_intersection michael@0: */ michael@0: SkDPoint SkIntersections::Line(const SkDLine& a, const SkDLine& b) { michael@0: double axLen = a[1].fX - a[0].fX; michael@0: double ayLen = a[1].fY - a[0].fY; michael@0: double bxLen = b[1].fX - b[0].fX; michael@0: double byLen = b[1].fY - b[0].fY; michael@0: double denom = byLen * axLen - ayLen * bxLen; michael@0: SkASSERT(denom); michael@0: double term1 = a[1].fX * a[0].fY - a[1].fY * a[0].fX; michael@0: double term2 = b[1].fX * b[0].fY - b[1].fY * b[0].fX; michael@0: SkDPoint p; michael@0: p.fX = (term1 * bxLen - axLen * term2) / denom; michael@0: p.fY = (term1 * byLen - ayLen * term2) / denom; michael@0: return p; michael@0: } michael@0: michael@0: void SkIntersections::cleanUpCoincidence() { michael@0: SkASSERT(fUsed == 2); michael@0: // both t values are good michael@0: bool startMatch = fT[0][0] == 0 && (fT[1][0] == 0 || fT[1][0] == 1); michael@0: bool endMatch = fT[0][1] == 1 && (fT[1][1] == 0 || fT[1][1] == 1); michael@0: if (startMatch || endMatch) { michael@0: removeOne(startMatch); michael@0: return; michael@0: } michael@0: // either t value is good michael@0: bool pStartMatch = fT[0][0] == 0 || fT[1][0] == 0 || fT[1][0] == 1; michael@0: bool pEndMatch = fT[0][1] == 1 || fT[1][1] == 0 || fT[1][1] == 1; michael@0: removeOne(pStartMatch || !pEndMatch); michael@0: } michael@0: michael@0: void SkIntersections::cleanUpParallelLines(bool parallel) { michael@0: while (fUsed > 2) { michael@0: removeOne(1); michael@0: } michael@0: if (fUsed == 2 && !parallel) { michael@0: bool startMatch = fT[0][0] == 0 || fT[1][0] == 0 || fT[1][0] == 1; michael@0: bool endMatch = fT[0][1] == 1 || fT[1][1] == 0 || fT[1][1] == 1; michael@0: if ((!startMatch && !endMatch) || approximately_equal(fT[0][0], fT[0][1])) { michael@0: SkASSERT(startMatch || endMatch); michael@0: removeOne(endMatch); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkIntersections::computePoints(const SkDLine& line, int used) { michael@0: fPt[0] = line.ptAtT(fT[0][0]); michael@0: if ((fUsed = used) == 2) { michael@0: fPt[1] = line.ptAtT(fT[0][1]); michael@0: } michael@0: } michael@0: michael@0: int SkIntersections::intersectRay(const SkDLine& a, const SkDLine& b) { michael@0: fMax = 2; michael@0: SkDVector aLen = a[1] - a[0]; michael@0: SkDVector bLen = b[1] - b[0]; michael@0: /* Slopes match when denom goes to zero: michael@0: axLen / ayLen == bxLen / byLen michael@0: (ayLen * byLen) * axLen / ayLen == (ayLen * byLen) * bxLen / byLen michael@0: byLen * axLen == ayLen * bxLen michael@0: byLen * axLen - ayLen * bxLen == 0 ( == denom ) michael@0: */ michael@0: double denom = bLen.fY * aLen.fX - aLen.fY * bLen.fX; michael@0: SkDVector ab0 = a[0] - b[0]; michael@0: double numerA = ab0.fY * bLen.fX - bLen.fY * ab0.fX; michael@0: double numerB = ab0.fY * aLen.fX - aLen.fY * ab0.fX; michael@0: numerA /= denom; michael@0: numerB /= denom; michael@0: int used; michael@0: if (!approximately_zero(denom)) { michael@0: fT[0][0] = numerA; michael@0: fT[1][0] = numerB; michael@0: used = 1; michael@0: } else { michael@0: /* See if the axis intercepts match: michael@0: ay - ax * ayLen / axLen == by - bx * ayLen / axLen michael@0: axLen * (ay - ax * ayLen / axLen) == axLen * (by - bx * ayLen / axLen) michael@0: axLen * ay - ax * ayLen == axLen * by - bx * ayLen michael@0: */ michael@0: if (!AlmostEqualUlps(aLen.fX * a[0].fY - aLen.fY * a[0].fX, michael@0: aLen.fX * b[0].fY - aLen.fY * b[0].fX)) { michael@0: return fUsed = 0; michael@0: } michael@0: // there's no great answer for intersection points for coincident rays, but return something michael@0: fT[0][0] = fT[1][0] = 0; michael@0: fT[1][0] = fT[1][1] = 1; michael@0: used = 2; michael@0: } michael@0: computePoints(a, used); michael@0: return fUsed; michael@0: } michael@0: michael@0: // note that this only works if both lines are neither horizontal nor vertical michael@0: int SkIntersections::intersect(const SkDLine& a, const SkDLine& b) { michael@0: fMax = 3; // note that we clean up so that there is no more than two in the end michael@0: // see if end points intersect the opposite line michael@0: double t; michael@0: for (int iA = 0; iA < 2; ++iA) { michael@0: if ((t = b.exactPoint(a[iA])) >= 0) { michael@0: insert(iA, t, a[iA]); michael@0: } michael@0: } michael@0: for (int iB = 0; iB < 2; ++iB) { michael@0: if ((t = a.exactPoint(b[iB])) >= 0) { michael@0: insert(t, iB, b[iB]); michael@0: } michael@0: } michael@0: /* Determine the intersection point of two line segments michael@0: Return FALSE if the lines don't intersect michael@0: from: http://paulbourke.net/geometry/lineline2d/ */ michael@0: double axLen = a[1].fX - a[0].fX; michael@0: double ayLen = a[1].fY - a[0].fY; michael@0: double bxLen = b[1].fX - b[0].fX; michael@0: double byLen = b[1].fY - b[0].fY; michael@0: /* Slopes match when denom goes to zero: michael@0: axLen / ayLen == bxLen / byLen michael@0: (ayLen * byLen) * axLen / ayLen == (ayLen * byLen) * bxLen / byLen michael@0: byLen * axLen == ayLen * bxLen michael@0: byLen * axLen - ayLen * bxLen == 0 ( == denom ) michael@0: */ michael@0: double axByLen = axLen * byLen; michael@0: double ayBxLen = ayLen * bxLen; michael@0: // detect parallel lines the same way here and in SkOpAngle operator < michael@0: // so that non-parallel means they are also sortable michael@0: bool unparallel = fAllowNear ? NotAlmostEqualUlps(axByLen, ayBxLen) michael@0: : NotAlmostDequalUlps(axByLen, ayBxLen); michael@0: if (unparallel && fUsed == 0) { michael@0: double ab0y = a[0].fY - b[0].fY; michael@0: double ab0x = a[0].fX - b[0].fX; michael@0: double numerA = ab0y * bxLen - byLen * ab0x; michael@0: double numerB = ab0y * axLen - ayLen * ab0x; michael@0: double denom = axByLen - ayBxLen; michael@0: if (between(0, numerA, denom) && between(0, numerB, denom)) { michael@0: fT[0][0] = numerA / denom; michael@0: fT[1][0] = numerB / denom; michael@0: computePoints(a, 1); michael@0: } michael@0: } michael@0: if (fAllowNear || !unparallel) { michael@0: for (int iA = 0; iA < 2; ++iA) { michael@0: if ((t = b.nearPoint(a[iA])) >= 0) { michael@0: insert(iA, t, a[iA]); michael@0: } michael@0: } michael@0: for (int iB = 0; iB < 2; ++iB) { michael@0: if ((t = a.nearPoint(b[iB])) >= 0) { michael@0: insert(t, iB, b[iB]); michael@0: } michael@0: } michael@0: } michael@0: cleanUpParallelLines(!unparallel); michael@0: SkASSERT(fUsed <= 2); michael@0: return fUsed; michael@0: } michael@0: michael@0: static int horizontal_coincident(const SkDLine& line, double y) { michael@0: double min = line[0].fY; michael@0: double max = line[1].fY; michael@0: if (min > max) { michael@0: SkTSwap(min, max); michael@0: } michael@0: if (min > y || max < y) { michael@0: return 0; michael@0: } michael@0: if (AlmostEqualUlps(min, max) && max - min < fabs(line[0].fX - line[1].fX)) { michael@0: return 2; michael@0: } michael@0: return 1; michael@0: } michael@0: michael@0: static double horizontal_intercept(const SkDLine& line, double y) { michael@0: return SkPinT((y - line[0].fY) / (line[1].fY - line[0].fY)); michael@0: } michael@0: michael@0: int SkIntersections::horizontal(const SkDLine& line, double y) { michael@0: fMax = 2; michael@0: int horizontalType = horizontal_coincident(line, y); michael@0: if (horizontalType == 1) { michael@0: fT[0][0] = horizontal_intercept(line, y); michael@0: } else if (horizontalType == 2) { michael@0: fT[0][0] = 0; michael@0: fT[0][1] = 1; michael@0: } michael@0: return fUsed = horizontalType; michael@0: } michael@0: michael@0: int SkIntersections::horizontal(const SkDLine& line, double left, double right, michael@0: double y, bool flipped) { michael@0: fMax = 2; michael@0: // see if end points intersect the opposite line michael@0: double t; michael@0: const SkDPoint leftPt = { left, y }; michael@0: if ((t = line.exactPoint(leftPt)) >= 0) { michael@0: insert(t, (double) flipped, leftPt); michael@0: } michael@0: if (left != right) { michael@0: const SkDPoint rightPt = { right, y }; michael@0: if ((t = line.exactPoint(rightPt)) >= 0) { michael@0: insert(t, (double) !flipped, rightPt); michael@0: } michael@0: for (int index = 0; index < 2; ++index) { michael@0: if ((t = SkDLine::ExactPointH(line[index], left, right, y)) >= 0) { michael@0: insert((double) index, flipped ? 1 - t : t, line[index]); michael@0: } michael@0: } michael@0: } michael@0: int result = horizontal_coincident(line, y); michael@0: if (result == 1 && fUsed == 0) { michael@0: fT[0][0] = horizontal_intercept(line, y); michael@0: double xIntercept = line[0].fX + fT[0][0] * (line[1].fX - line[0].fX); michael@0: if (between(left, xIntercept, right)) { michael@0: fT[1][0] = (xIntercept - left) / (right - left); michael@0: if (flipped) { michael@0: // OPTIMIZATION: ? instead of swapping, pass original line, use [1].fX - [0].fX michael@0: for (int index = 0; index < result; ++index) { michael@0: fT[1][index] = 1 - fT[1][index]; michael@0: } michael@0: } michael@0: fPt[0].fX = xIntercept; michael@0: fPt[0].fY = y; michael@0: fUsed = 1; michael@0: } michael@0: } michael@0: if (fAllowNear || result == 2) { michael@0: if ((t = line.nearPoint(leftPt)) >= 0) { michael@0: insert(t, (double) flipped, leftPt); michael@0: } michael@0: if (left != right) { michael@0: const SkDPoint rightPt = { right, y }; michael@0: if ((t = line.nearPoint(rightPt)) >= 0) { michael@0: insert(t, (double) !flipped, rightPt); michael@0: } michael@0: for (int index = 0; index < 2; ++index) { michael@0: if ((t = SkDLine::NearPointH(line[index], left, right, y)) >= 0) { michael@0: insert((double) index, flipped ? 1 - t : t, line[index]); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: cleanUpParallelLines(result == 2); michael@0: return fUsed; michael@0: } michael@0: michael@0: static int vertical_coincident(const SkDLine& line, double x) { michael@0: double min = line[0].fX; michael@0: double max = line[1].fX; michael@0: if (min > max) { michael@0: SkTSwap(min, max); michael@0: } michael@0: if (!precisely_between(min, x, max)) { michael@0: return 0; michael@0: } michael@0: if (AlmostEqualUlps(min, max)) { michael@0: return 2; michael@0: } michael@0: return 1; michael@0: } michael@0: michael@0: static double vertical_intercept(const SkDLine& line, double x) { michael@0: return SkPinT((x - line[0].fX) / (line[1].fX - line[0].fX)); michael@0: } michael@0: michael@0: int SkIntersections::vertical(const SkDLine& line, double x) { michael@0: fMax = 2; michael@0: int verticalType = vertical_coincident(line, x); michael@0: if (verticalType == 1) { michael@0: fT[0][0] = vertical_intercept(line, x); michael@0: } else if (verticalType == 2) { michael@0: fT[0][0] = 0; michael@0: fT[0][1] = 1; michael@0: } michael@0: return fUsed = verticalType; michael@0: } michael@0: michael@0: int SkIntersections::vertical(const SkDLine& line, double top, double bottom, michael@0: double x, bool flipped) { michael@0: fMax = 2; michael@0: // see if end points intersect the opposite line michael@0: double t; michael@0: SkDPoint topPt = { x, top }; michael@0: if ((t = line.exactPoint(topPt)) >= 0) { michael@0: insert(t, (double) flipped, topPt); michael@0: } michael@0: if (top != bottom) { michael@0: SkDPoint bottomPt = { x, bottom }; michael@0: if ((t = line.exactPoint(bottomPt)) >= 0) { michael@0: insert(t, (double) !flipped, bottomPt); michael@0: } michael@0: for (int index = 0; index < 2; ++index) { michael@0: if ((t = SkDLine::ExactPointV(line[index], top, bottom, x)) >= 0) { michael@0: insert((double) index, flipped ? 1 - t : t, line[index]); michael@0: } michael@0: } michael@0: } michael@0: int result = vertical_coincident(line, x); michael@0: if (result == 1 && fUsed == 0) { michael@0: fT[0][0] = vertical_intercept(line, x); michael@0: double yIntercept = line[0].fY + fT[0][0] * (line[1].fY - line[0].fY); michael@0: if (between(top, yIntercept, bottom)) { michael@0: fT[1][0] = (yIntercept - top) / (bottom - top); michael@0: if (flipped) { michael@0: // OPTIMIZATION: instead of swapping, pass original line, use [1].fY - [0].fY michael@0: for (int index = 0; index < result; ++index) { michael@0: fT[1][index] = 1 - fT[1][index]; michael@0: } michael@0: } michael@0: fPt[0].fX = x; michael@0: fPt[0].fY = yIntercept; michael@0: fUsed = 1; michael@0: } michael@0: } michael@0: if (fAllowNear || result == 2) { michael@0: if ((t = line.nearPoint(topPt)) >= 0) { michael@0: insert(t, (double) flipped, topPt); michael@0: } michael@0: if (top != bottom) { michael@0: SkDPoint bottomPt = { x, bottom }; michael@0: if ((t = line.nearPoint(bottomPt)) >= 0) { michael@0: insert(t, (double) !flipped, bottomPt); michael@0: } michael@0: for (int index = 0; index < 2; ++index) { michael@0: if ((t = SkDLine::NearPointV(line[index], top, bottom, x)) >= 0) { michael@0: insert((double) index, flipped ? 1 - t : t, line[index]); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: cleanUpParallelLines(result == 2); michael@0: return fUsed; michael@0: } michael@0: michael@0: // from http://www.bryceboe.com/wordpress/wp-content/uploads/2006/10/intersect.py michael@0: // 4 subs, 2 muls, 1 cmp michael@0: static bool ccw(const SkDPoint& A, const SkDPoint& B, const SkDPoint& C) { michael@0: return (C.fY - A.fY) * (B.fX - A.fX) > (B.fY - A.fY) * (C.fX - A.fX); michael@0: } michael@0: michael@0: // 16 subs, 8 muls, 6 cmps michael@0: bool SkIntersections::Test(const SkDLine& a, const SkDLine& b) { michael@0: return ccw(a[0], b[0], b[1]) != ccw(a[1], b[0], b[1]) michael@0: && ccw(a[0], a[1], b[0]) != ccw(a[0], a[1], b[1]); michael@0: }