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 "SkOpAngle.h" michael@0: #include "SkOpSegment.h" michael@0: #include "SkPathOpsCurve.h" michael@0: #include "SkTSort.h" michael@0: michael@0: #if DEBUG_ANGLE michael@0: #include "SkString.h" michael@0: michael@0: static const char funcName[] = "SkOpSegment::operator<"; michael@0: static const int bugChar = strlen(funcName) + 1; michael@0: #endif michael@0: michael@0: /* Angles are sorted counterclockwise. The smallest angle has a positive x and the smallest michael@0: positive y. The largest angle has a positive x and a zero y. */ michael@0: michael@0: #if DEBUG_ANGLE michael@0: static bool CompareResult(SkString* bugOut, const char* append, bool compare) { michael@0: bugOut->appendf("%s", append); michael@0: bugOut->writable_str()[bugChar] = "><"[compare]; michael@0: SkDebugf("%s\n", bugOut->c_str()); michael@0: return compare; michael@0: } michael@0: michael@0: #define COMPARE_RESULT(append, compare) CompareResult(&bugOut, append, compare) michael@0: #else michael@0: #define COMPARE_RESULT(append, compare) compare michael@0: #endif michael@0: michael@0: bool SkOpAngle::calcSlop(double x, double y, double rx, double ry, bool* result) const{ michael@0: double absX = fabs(x); michael@0: double absY = fabs(y); michael@0: double length = absX < absY ? absX / 2 + absY : absX + absY / 2; michael@0: int exponent; michael@0: (void) frexp(length, &exponent); michael@0: double epsilon = ldexp(FLT_EPSILON, exponent); michael@0: SkPath::Verb verb = fSegment->verb(); michael@0: SkASSERT(verb == SkPath::kQuad_Verb || verb == SkPath::kCubic_Verb); michael@0: // FIXME: the quad and cubic factors are made up ; determine actual values michael@0: double slop = verb == SkPath::kQuad_Verb ? 4 * epsilon : 512 * epsilon; michael@0: double xSlop = slop; michael@0: double ySlop = x * y < 0 ? -xSlop : xSlop; // OPTIMIZATION: use copysign / _copysign ? michael@0: double x1 = x - xSlop; michael@0: double y1 = y + ySlop; michael@0: double x_ry1 = x1 * ry; michael@0: double rx_y1 = rx * y1; michael@0: *result = x_ry1 < rx_y1; michael@0: double x2 = x + xSlop; michael@0: double y2 = y - ySlop; michael@0: double x_ry2 = x2 * ry; michael@0: double rx_y2 = rx * y2; michael@0: bool less2 = x_ry2 < rx_y2; michael@0: return *result == less2; michael@0: } michael@0: michael@0: /* michael@0: for quads and cubics, set up a parameterized line (e.g. LineParameters ) michael@0: for points [0] to [1]. See if point [2] is on that line, or on one side michael@0: or the other. If it both quads' end points are on the same side, choose michael@0: the shorter tangent. If the tangents are equal, choose the better second michael@0: tangent angle michael@0: michael@0: FIXME: maybe I could set up LineParameters lazily michael@0: */ michael@0: bool SkOpAngle::operator<(const SkOpAngle& rh) const { // this/lh: left-hand; rh: right-hand michael@0: double y = dy(); michael@0: double ry = rh.dy(); michael@0: #if DEBUG_ANGLE michael@0: SkString bugOut; michael@0: bugOut.printf("%s _ id=%d segId=%d tStart=%1.9g tEnd=%1.9g" michael@0: " | id=%d segId=%d tStart=%1.9g tEnd=%1.9g ", funcName, michael@0: fID, fSegment->debugID(), fSegment->t(fStart), fSegment->t(fEnd), michael@0: rh.fID, rh.fSegment->debugID(), rh.fSegment->t(rh.fStart), rh.fSegment->t(rh.fEnd)); michael@0: #endif michael@0: double y_ry = y * ry; michael@0: if (y_ry < 0) { // if y's are opposite signs, we can do a quick return michael@0: return COMPARE_RESULT("1 y * ry < 0", y < 0); michael@0: } michael@0: // at this point, both y's must be the same sign, or one (or both) is zero michael@0: double x = dx(); michael@0: double rx = rh.dx(); michael@0: if (x * rx < 0) { // if x's are opposite signs, use y to determine first or second half michael@0: if (y < 0 && ry < 0) { // if y's are negative, lh x is smaller if positive michael@0: return COMPARE_RESULT("2 x_rx < 0 && y < 0 ...", x > 0); michael@0: } michael@0: if (y >= 0 && ry >= 0) { // if y's are zero or positive, lh x is smaller if negative michael@0: return COMPARE_RESULT("3 x_rx < 0 && y >= 0 ...", x < 0); michael@0: } michael@0: SkASSERT((y == 0) ^ (ry == 0)); // if one y is zero and one is negative, neg y is smaller michael@0: return COMPARE_RESULT("4 x_rx < 0 && y == 0 ...", y < 0); michael@0: } michael@0: // at this point, both x's must be the same sign, or one (or both) is zero michael@0: if (y_ry == 0) { // if either y is zero michael@0: if (y + ry < 0) { // if the other y is less than zero, it must be smaller michael@0: return COMPARE_RESULT("5 y_ry == 0 && y + ry < 0", y < 0); michael@0: } michael@0: if (y + ry > 0) { // if a y is greater than zero and an x is positive, non zero is smaller michael@0: return COMPARE_RESULT("6 y_ry == 0 && y + ry > 0", (x + rx > 0) ^ (y == 0)); michael@0: } michael@0: // at this point, both y's are zero, so lines are coincident or one is degenerate michael@0: SkASSERT(x * rx != 0); // and a degenerate line should haven't gotten this far michael@0: } michael@0: // see if either curve can be lengthened before trying the tangent michael@0: if (fSegment->other(fEnd) != rh.fSegment // tangents not absolutely identical michael@0: && rh.fSegment->other(rh.fEnd) != fSegment michael@0: && y != -DBL_EPSILON michael@0: && ry != -DBL_EPSILON) { // and not intersecting michael@0: SkOpAngle longer = *this; michael@0: SkOpAngle rhLonger = rh; michael@0: if ((longer.lengthen(rh) | rhLonger.lengthen(*this)) // lengthen both michael@0: && (fUnorderable || !longer.fUnorderable) michael@0: && (rh.fUnorderable || !rhLonger.fUnorderable)) { michael@0: #if DEBUG_ANGLE michael@0: bugOut.prepend(" "); michael@0: #endif michael@0: return COMPARE_RESULT("10 longer.lengthen(rh) ...", longer < rhLonger); michael@0: } michael@0: } michael@0: SkPath::Verb verb = fSegment->verb(); michael@0: SkPath::Verb rVerb = rh.fSegment->verb(); michael@0: if (y_ry != 0) { // if they aren't coincident, look for a stable cross product michael@0: // at this point, y's are the same sign, neither is zero michael@0: // and x's are the same sign, or one (or both) is zero michael@0: double x_ry = x * ry; michael@0: double rx_y = rx * y; michael@0: if (!fComputed && !rh.fComputed) { michael@0: if (!SkDLine::NearRay(x, y, rx, ry) && x_ry != rx_y) { michael@0: return COMPARE_RESULT("7 !fComputed && !rh.fComputed", x_ry < rx_y); michael@0: } michael@0: if (fSide2 == 0 && rh.fSide2 == 0) { michael@0: return COMPARE_RESULT("7a !fComputed && !rh.fComputed", x_ry < rx_y); michael@0: } michael@0: } else { michael@0: // if the vector was a result of subdividing a curve, see if it is stable michael@0: bool sloppy1 = x_ry < rx_y; michael@0: bool sloppy2 = !sloppy1; michael@0: if ((!fComputed || calcSlop(x, y, rx, ry, &sloppy1)) michael@0: && (!rh.fComputed || rh.calcSlop(rx, ry, x, y, &sloppy2)) michael@0: && sloppy1 != sloppy2) { michael@0: return COMPARE_RESULT("8 CalcSlop(x, y ...", sloppy1); michael@0: } michael@0: } michael@0: } michael@0: if (fSide2 * rh.fSide2 == 0) { // one is zero michael@0: #if DEBUG_ANGLE michael@0: if (fSide2 == rh.fSide2 && y_ry) { // both is zero; coincidence was undetected michael@0: SkDebugf("%s coincidence!\n", __FUNCTION__); michael@0: } michael@0: #endif michael@0: return COMPARE_RESULT("9a fSide2 * rh.fSide2 == 0 ...", fSide2 < rh.fSide2); michael@0: } michael@0: // at this point, the initial tangent line is nearly coincident michael@0: // see if edges curl away from each other michael@0: if (fSide * rh.fSide < 0 && (!approximately_zero(fSide) || !approximately_zero(rh.fSide))) { michael@0: return COMPARE_RESULT("9b fSide * rh.fSide < 0 ...", fSide < rh.fSide); michael@0: } michael@0: if (fUnsortable || rh.fUnsortable) { michael@0: // even with no solution, return a stable sort michael@0: return COMPARE_RESULT("11 fUnsortable || rh.fUnsortable", this < &rh); michael@0: } michael@0: if ((verb == SkPath::kLine_Verb && approximately_zero(y) && approximately_zero(x)) michael@0: || (rVerb == SkPath::kLine_Verb michael@0: && approximately_zero(ry) && approximately_zero(rx))) { michael@0: // See general unsortable comment below. This case can happen when michael@0: // one line has a non-zero change in t but no change in x and y. michael@0: fUnsortable = true; michael@0: return COMPARE_RESULT("12 verb == SkPath::kLine_Verb ...", this < &rh); michael@0: } michael@0: if (fSegment->isTiny(this) || rh.fSegment->isTiny(&rh)) { michael@0: fUnsortable = true; michael@0: return COMPARE_RESULT("13 verb == fSegment->isTiny(this) ...", this < &rh); michael@0: } michael@0: SkASSERT(verb >= SkPath::kQuad_Verb); michael@0: SkASSERT(rVerb >= SkPath::kQuad_Verb); michael@0: // FIXME: until I can think of something better, project a ray from the michael@0: // end of the shorter tangent to midway between the end points michael@0: // through both curves and use the resulting angle to sort michael@0: // FIXME: some of this setup can be moved to set() if it works, or cached if it's expensive michael@0: double len = fTangentPart.normalSquared(); michael@0: double rlen = rh.fTangentPart.normalSquared(); michael@0: SkDLine ray; michael@0: SkIntersections i, ri; michael@0: int roots, rroots; michael@0: bool flip = false; michael@0: bool useThis; michael@0: bool leftLessThanRight = fSide > 0; michael@0: do { michael@0: useThis = (len < rlen) ^ flip; michael@0: const SkDCubic& part = useThis ? fCurvePart : rh.fCurvePart; michael@0: SkPath::Verb partVerb = useThis ? verb : rVerb; michael@0: ray[0] = partVerb == SkPath::kCubic_Verb && part[0].approximatelyEqual(part[1]) ? michael@0: part[2] : part[1]; michael@0: ray[1] = SkDPoint::Mid(part[0], part[SkPathOpsVerbToPoints(partVerb)]); michael@0: SkASSERT(ray[0] != ray[1]); michael@0: roots = (i.*CurveRay[SkPathOpsVerbToPoints(verb)])(fSegment->pts(), ray); michael@0: rroots = (ri.*CurveRay[SkPathOpsVerbToPoints(rVerb)])(rh.fSegment->pts(), ray); michael@0: } while ((roots == 0 || rroots == 0) && (flip ^= true)); michael@0: if (roots == 0 || rroots == 0) { michael@0: // FIXME: we don't have a solution in this case. The interim solution michael@0: // is to mark the edges as unsortable, exclude them from this and michael@0: // future computations, and allow the returned path to be fragmented michael@0: fUnsortable = true; michael@0: return COMPARE_RESULT("roots == 0 || rroots == 0", this < &rh); michael@0: } michael@0: SkASSERT(fSide != 0 && rh.fSide != 0); michael@0: if (fSide * rh.fSide < 0) { michael@0: fUnsortable = true; michael@0: return COMPARE_RESULT("14 fSide * rh.fSide < 0", this < &rh); michael@0: } michael@0: SkDPoint lLoc; michael@0: double best = SK_ScalarInfinity; michael@0: #if DEBUG_SORT michael@0: SkDebugf("lh=%d rh=%d use-lh=%d ray={{%1.9g,%1.9g}, {%1.9g,%1.9g}} %c\n", michael@0: fSegment->debugID(), rh.fSegment->debugID(), useThis, ray[0].fX, ray[0].fY, michael@0: ray[1].fX, ray[1].fY, "-+"[fSide > 0]); michael@0: #endif michael@0: for (int index = 0; index < roots; ++index) { michael@0: SkDPoint loc = i.pt(index); michael@0: SkDVector dxy = loc - ray[0]; michael@0: double dist = dxy.lengthSquared(); michael@0: #if DEBUG_SORT michael@0: SkDebugf("best=%1.9g dist=%1.9g loc={%1.9g,%1.9g} dxy={%1.9g,%1.9g}\n", michael@0: best, dist, loc.fX, loc.fY, dxy.fX, dxy.fY); michael@0: #endif michael@0: if (best > dist) { michael@0: lLoc = loc; michael@0: best = dist; michael@0: } michael@0: } michael@0: flip = false; michael@0: SkDPoint rLoc; michael@0: for (int index = 0; index < rroots; ++index) { michael@0: rLoc = ri.pt(index); michael@0: SkDVector dxy = rLoc - ray[0]; michael@0: double dist = dxy.lengthSquared(); michael@0: #if DEBUG_SORT michael@0: SkDebugf("best=%1.9g dist=%1.9g %c=(fSide < 0) rLoc={%1.9g,%1.9g} dxy={%1.9g,%1.9g}\n", michael@0: best, dist, "><"[fSide < 0], rLoc.fX, rLoc.fY, dxy.fX, dxy.fY); michael@0: #endif michael@0: if (best > dist) { michael@0: flip = true; michael@0: break; michael@0: } michael@0: } michael@0: if (flip) { michael@0: leftLessThanRight = !leftLessThanRight; michael@0: } michael@0: return COMPARE_RESULT("15 leftLessThanRight", leftLessThanRight); michael@0: } michael@0: michael@0: bool SkOpAngle::isHorizontal() const { michael@0: return dy() == 0 && fSegment->verb() == SkPath::kLine_Verb; michael@0: } michael@0: michael@0: // lengthen cannot cross opposite angle michael@0: bool SkOpAngle::lengthen(const SkOpAngle& opp) { michael@0: if (fSegment->other(fEnd) == opp.fSegment) { michael@0: return false; michael@0: } michael@0: // FIXME: make this a while loop instead and make it as large as possible? michael@0: int newEnd = fEnd; michael@0: if (fStart < fEnd ? ++newEnd < fSegment->count() : --newEnd >= 0) { michael@0: fEnd = newEnd; michael@0: setSpans(); michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: void SkOpAngle::set(const SkOpSegment* segment, int start, int end) { michael@0: fSegment = segment; michael@0: fStart = start; michael@0: fEnd = end; michael@0: setSpans(); michael@0: } michael@0: michael@0: void SkOpAngle::setSpans() { michael@0: fUnorderable = fSegment->isTiny(this); michael@0: fLastMarked = NULL; michael@0: fUnsortable = false; michael@0: const SkPoint* pts = fSegment->pts(); michael@0: if (fSegment->verb() != SkPath::kLine_Verb) { michael@0: fComputed = fSegment->subDivide(fStart, fEnd, &fCurvePart); michael@0: fSegment->subDivide(fStart, fStart < fEnd ? fSegment->count() - 1 : 0, &fCurveHalf); michael@0: } michael@0: // FIXME: slight errors in subdivision cause sort trouble later on. As an experiment, try michael@0: // rounding the curve part to float precision here michael@0: // fCurvePart.round(fSegment->verb()); michael@0: switch (fSegment->verb()) { michael@0: case SkPath::kLine_Verb: { michael@0: SkASSERT(fStart != fEnd); michael@0: fCurvePart[0].set(pts[fStart > fEnd]); michael@0: fCurvePart[1].set(pts[fStart < fEnd]); michael@0: fComputed = false; michael@0: // OPTIMIZATION: for pure line compares, we never need fTangentPart.c michael@0: fTangentPart.lineEndPoints(*SkTCast(&fCurvePart)); michael@0: fSide = 0; michael@0: fSide2 = 0; michael@0: } break; michael@0: case SkPath::kQuad_Verb: { michael@0: fSide2 = -fTangentHalf.quadPart(*SkTCast(&fCurveHalf)); michael@0: SkDQuad& quad = *SkTCast(&fCurvePart); michael@0: fTangentPart.quadEndPoints(quad); michael@0: fSide = -fTangentPart.pointDistance(fCurvePart[2]); // not normalized -- compare sign only michael@0: if (fComputed && dx() > 0 && approximately_zero(dy())) { michael@0: SkDCubic origCurve; // can't use segment's curve in place since it may be flipped michael@0: int last = fSegment->count() - 1; michael@0: fSegment->subDivide(fStart < fEnd ? 0 : last, fStart < fEnd ? last : 0, &origCurve); michael@0: SkLineParameters origTan; michael@0: origTan.quadEndPoints(*SkTCast(&origCurve)); michael@0: if (origTan.dx() <= 0 michael@0: || (dy() != origTan.dy() && dy() * origTan.dy() <= 0)) { // signs match? michael@0: fUnorderable = true; michael@0: return; michael@0: } michael@0: } michael@0: } break; michael@0: case SkPath::kCubic_Verb: { michael@0: double startT = fSegment->t(fStart); michael@0: fSide2 = -fTangentHalf.cubicPart(fCurveHalf); michael@0: fTangentPart.cubicEndPoints(fCurvePart); michael@0: double testTs[4]; michael@0: // OPTIMIZATION: keep inflections precomputed with cubic segment? michael@0: int testCount = SkDCubic::FindInflections(pts, testTs); michael@0: double endT = fSegment->t(fEnd); michael@0: double limitT = endT; michael@0: int index; michael@0: for (index = 0; index < testCount; ++index) { michael@0: if (!between(startT, testTs[index], limitT)) { michael@0: testTs[index] = -1; michael@0: } michael@0: } michael@0: testTs[testCount++] = startT; michael@0: testTs[testCount++] = endT; michael@0: SkTQSort(testTs, &testTs[testCount - 1]); michael@0: double bestSide = 0; michael@0: int testCases = (testCount << 1) - 1; michael@0: index = 0; michael@0: while (testTs[index] < 0) { michael@0: ++index; michael@0: } michael@0: index <<= 1; michael@0: for (; index < testCases; ++index) { michael@0: int testIndex = index >> 1; michael@0: double testT = testTs[testIndex]; michael@0: if (index & 1) { michael@0: testT = (testT + testTs[testIndex + 1]) / 2; michael@0: } michael@0: // OPTIMIZE: could avoid call for t == startT, endT michael@0: SkDPoint pt = dcubic_xy_at_t(pts, testT); michael@0: double testSide = fTangentPart.pointDistance(pt); michael@0: if (fabs(bestSide) < fabs(testSide)) { michael@0: bestSide = testSide; michael@0: } michael@0: } michael@0: fSide = -bestSide; // compare sign only michael@0: SkASSERT(fSide == 0 || fSide2 != 0); michael@0: if (fComputed && dx() > 0 && approximately_zero(dy())) { michael@0: SkDCubic origCurve; // can't use segment's curve in place since it may be flipped michael@0: int last = fSegment->count() - 1; michael@0: fSegment->subDivide(fStart < fEnd ? 0 : last, fStart < fEnd ? last : 0, &origCurve); michael@0: SkDCubicPair split = origCurve.chopAt(startT); michael@0: SkLineParameters splitTan; michael@0: splitTan.cubicEndPoints(fStart < fEnd ? split.second() : split.first()); michael@0: if (splitTan.dx() <= 0) { michael@0: fUnorderable = true; michael@0: fUnsortable = fSegment->isTiny(this); michael@0: return; michael@0: } michael@0: // if one is < 0 and the other is >= 0 michael@0: if (dy() * splitTan.dy() < 0) { michael@0: fUnorderable = true; michael@0: fUnsortable = fSegment->isTiny(this); michael@0: return; michael@0: } michael@0: } michael@0: } break; michael@0: default: michael@0: SkASSERT(0); michael@0: } michael@0: if ((fUnsortable = approximately_zero(dx()) && approximately_zero(dy()))) { michael@0: return; michael@0: } michael@0: if (fSegment->verb() == SkPath::kLine_Verb) { michael@0: return; michael@0: } michael@0: SkASSERT(fStart != fEnd); michael@0: int smaller = SkMin32(fStart, fEnd); michael@0: int larger = SkMax32(fStart, fEnd); michael@0: while (smaller < larger && fSegment->span(smaller).fTiny) { michael@0: ++smaller; michael@0: } michael@0: if (precisely_equal(fSegment->span(smaller).fT, fSegment->span(larger).fT)) { michael@0: #if DEBUG_UNSORTABLE michael@0: SkPoint iPt = fSegment->xyAtT(fStart); michael@0: SkPoint ePt = fSegment->xyAtT(fEnd); michael@0: SkDebugf("%s all tiny unsortable [%d] (%1.9g,%1.9g) [%d] (%1.9g,%1.9g)\n", __FUNCTION__, michael@0: fStart, iPt.fX, iPt.fY, fEnd, ePt.fX, ePt.fY); michael@0: #endif michael@0: fUnsortable = true; michael@0: return; michael@0: } michael@0: fUnsortable = fStart < fEnd ? fSegment->span(smaller).fUnsortableStart michael@0: : fSegment->span(larger).fUnsortableEnd; michael@0: #if DEBUG_UNSORTABLE michael@0: if (fUnsortable) { michael@0: SkPoint iPt = fSegment->xyAtT(smaller); michael@0: SkPoint ePt = fSegment->xyAtT(larger); michael@0: SkDebugf("%s unsortable [%d] (%1.9g,%1.9g) [%d] (%1.9g,%1.9g)\n", __FUNCTION__, michael@0: smaller, iPt.fX, iPt.fY, fEnd, ePt.fX, ePt.fY); michael@0: } michael@0: #endif michael@0: return; michael@0: } michael@0: michael@0: #ifdef SK_DEBUG michael@0: void SkOpAngle::dump() const { michael@0: const SkOpSpan& spanStart = fSegment->span(fStart); michael@0: const SkOpSpan& spanEnd = fSegment->span(fEnd); michael@0: const SkOpSpan& spanMin = fStart < fEnd ? spanStart : spanEnd; michael@0: SkDebugf("id=%d (%1.9g,%1.9g) start=%d (%1.9g) end=%d (%1.9g) sumWind=", michael@0: fSegment->debugID(), fSegment->xAtT(fStart), fSegment->yAtT(fStart), michael@0: fStart, spanStart.fT, fEnd, spanEnd.fT); michael@0: SkPathOpsDebug::WindingPrintf(spanMin.fWindSum); michael@0: SkDebugf(" oppWind="); michael@0: SkPathOpsDebug::WindingPrintf(spanMin.fOppSum), michael@0: SkDebugf(" done=%d\n", spanMin.fDone); michael@0: } michael@0: #endif