// FIXME: if flat measure is sufficiently large, then probably the quartic solution failed
static void relaxed_is_linear(const SkDQuad& q1, const SkDQuad& q2, SkIntersections* i) {
    double m1 = flat_measure(q1);
    double m2 = flat_measure(q2);
#if DEBUG_FLAT_QUADS
    double min = SkTMin(m1, m2);
    if (min > 5) {
        SkDebugf("%s maybe not flat enough.. %1.9g\n", __FUNCTION__, min);
    }
#endif
    i->reset();
    const SkDQuad& rounder = m2 < m1 ? q1 : q2;
    const SkDQuad& flatter = m2 < m1 ? q2 : q1;
    bool subDivide = false;
    is_linear_inner(flatter, 0, 1, rounder, 0, 1, i, &subDivide);
    if (subDivide) {
        SkDQuadPair pair = flatter.chopAt(0.5);
        SkIntersections firstI, secondI;
        relaxed_is_linear(pair.first(), rounder, &firstI);
        for (int index = 0; index < firstI.used(); ++index) {
            i->insert(firstI[0][index] * 0.5, firstI[1][index], firstI.pt(index));
        }
        relaxed_is_linear(pair.second(), rounder, &secondI);
        for (int index = 0; index < secondI.used(); ++index) {
            i->insert(0.5 + secondI[0][index] * 0.5, secondI[1][index], secondI.pt(index));
        }
    }
    if (m2 < m1) {
        i->swapPts();
    }
}
Beispiel #2
0
// FIXME: if flat measure is sufficiently large, then probably the quartic solution failed
// avoid imprecision incurred with chopAt
static void relaxed_is_linear(const SkDQuad* q1, double s1, double e1, const SkDQuad* q2,
        double s2, double e2, SkIntersections* i) {
    double m1 = flat_measure(*q1);
    double m2 = flat_measure(*q2);
    i->reset();
    const SkDQuad* rounder, *flatter;
    double sf, midf, ef, sr, er;
    if (m2 < m1) {
        rounder = q1;
        sr = s1;
        er = e1;
        flatter = q2;
        sf = s2;
        midf = (s2 + e2) / 2;
        ef = e2;
    } else {
        rounder = q2;
        sr = s2;
        er = e2;
        flatter = q1;
        sf = s1;
        midf = (s1 + e1) / 2;
        ef = e1;
    }
    bool subDivide = false;
    is_linear_inner(*flatter, sf, ef, *rounder, sr, er, i, &subDivide);
    if (subDivide) {
        relaxed_is_linear(flatter, sf, midf, rounder, sr, er, i);
        relaxed_is_linear(flatter, midf, ef, rounder, sr, er, i);
    }
    if (m2 < m1) {
        i->swapPts();
    }
}
// FIXME ? should this measure both and then use the quad that is the flattest as the line?
static bool is_linear(const SkDQuad& q1, const SkDQuad& q2, SkIntersections* i) {
    double measure = flat_measure(q1);
    // OPTIMIZE: (get rid of sqrt) use approximately_zero
    if (!approximately_zero_sqrt(measure)) {
        return false;
    }
    return is_linear_inner(q1, 0, 1, q2, 0, 1, i, NULL);
}
static bool is_linear_inner(const SkDQuad& q1, double t1s, double t1e, const SkDQuad& q2,
                            double t2s, double t2e, SkIntersections* i, bool* subDivide) {
    SkDQuad hull = q1.subDivide(t1s, t1e);
    SkDLine line = {{hull[2], hull[0]}};
    const SkDLine* testLines[] = { &line, (const SkDLine*) &hull[0], (const SkDLine*) &hull[1] };
    const size_t kTestCount = SK_ARRAY_COUNT(testLines);
    SkSTArray<kTestCount * 2, double, true> tsFound;
    for (size_t index = 0; index < kTestCount; ++index) {
        SkIntersections rootTs;
        rootTs.allowNear(false);
        int roots = rootTs.intersect(q2, *testLines[index]);
        for (int idx2 = 0; idx2 < roots; ++idx2) {
            double t = rootTs[0][idx2];
#ifdef SK_DEBUG
            SkDPoint qPt = q2.ptAtT(t);
            SkDPoint lPt = testLines[index]->ptAtT(rootTs[1][idx2]);
            SkASSERT(qPt.approximatelyEqual(lPt));
#endif
            if (approximately_negative(t - t2s) || approximately_positive(t - t2e)) {
                continue;
            }
            tsFound.push_back(rootTs[0][idx2]);
        }
    }
    int tCount = tsFound.count();
    if (tCount <= 0) {
        return true;
    }
    double tMin, tMax;
    if (tCount == 1) {
        tMin = tMax = tsFound[0];
    } else {
        SkASSERT(tCount > 1);
        SkTQSort<double>(tsFound.begin(), tsFound.end() - 1);
        tMin = tsFound[0];
        tMax = tsFound[tsFound.count() - 1];
    }
    SkDPoint end = q2.ptAtT(t2s);
    bool startInTriangle = hull.pointInHull(end);
    if (startInTriangle) {
        tMin = t2s;
    }
    end = q2.ptAtT(t2e);
    bool endInTriangle = hull.pointInHull(end);
    if (endInTriangle) {
        tMax = t2e;
    }
    int split = 0;
    SkDVector dxy1, dxy2;
    if (tMin != tMax || tCount > 2) {
        dxy2 = q2.dxdyAtT(tMin);
        for (int index = 1; index < tCount; ++index) {
            dxy1 = dxy2;
            dxy2 = q2.dxdyAtT(tsFound[index]);
            double dot = dxy1.dot(dxy2);
            if (dot < 0) {
                split = index - 1;
                break;
            }
        }
    }
    if (split == 0) {  // there's one point
        if (add_intercept(q1, q2, tMin, tMax, i, subDivide)) {
            return true;
        }
        i->swap();
        return is_linear_inner(q2, tMin, tMax, q1, t1s, t1e, i, subDivide);
    }
    // At this point, we have two ranges of t values -- treat each separately at the split
    bool result;
    if (add_intercept(q1, q2, tMin, tsFound[split - 1], i, subDivide)) {
        result = true;
    } else {
        i->swap();
        result = is_linear_inner(q2, tMin, tsFound[split - 1], q1, t1s, t1e, i, subDivide);
    }
    if (add_intercept(q1, q2, tsFound[split], tMax, i, subDivide)) {
        result = true;
    } else {
        i->swap();
        result |= is_linear_inner(q2, tsFound[split], tMax, q1, t1s, t1e, i, subDivide);
    }
    return result;
}