void CubicToQuads(const SkDCubic& cubic, double precision, SkTArray<SkDQuad, true>& quads) { SkTArray<double, true> ts; toQuadraticTs(&cubic, precision, &ts); if (ts.count() <= 0) { SkDQuad quad = cubic.toQuad(); quads.push_back(quad); return; } double tStart = 0; for (int i1 = 0; i1 <= ts.count(); ++i1) { const double tEnd = i1 < ts.count() ? ts[i1] : 1; SkDRect bounds; bounds.setBounds(cubic); SkDCubic part = cubic.subDivide(tStart, tEnd); SkDQuad quad = part.toQuad(); if (quad[1].fX < bounds.fLeft) { quad[1].fX = bounds.fLeft; } else if (quad[1].fX > bounds.fRight) { quad[1].fX = bounds.fRight; } if (quad[1].fY < bounds.fTop) { quad[1].fY = bounds.fTop; } else if (quad[1].fY > bounds.fBottom) { quad[1].fY = bounds.fBottom; } quads.push_back(quad); tStart = tEnd; } }
// FIXME: cache keep the bounds and/or precision with the caller? double SkDCubic::calcPrecision() const { SkDRect dRect; dRect.setBounds(*this); // OPTIMIZATION: just use setRawBounds ? double width = dRect.fRight - dRect.fLeft; double height = dRect.fBottom - dRect.fTop; return (width > height ? width : height) / gPrecisionUnit; }
static double maxDist(const SkDQuad& quad) { SkDRect bounds; bounds.setBounds(quad); SkDVector corner[4] = { { bounds.fLeft - quad[0].fX, bounds.fTop - quad[0].fY }, { bounds.fRight - quad[0].fX, bounds.fTop - quad[0].fY }, { bounds.fLeft - quad[0].fX, bounds.fBottom - quad[0].fY }, { bounds.fRight - quad[0].fX, bounds.fBottom - quad[0].fY } }; double max = 0; for (unsigned index = 0; index < SK_ARRAY_COUNT(corner); ++index) { max = SkTMax(max, corner[index].length()); } return max; }
static void writeFrames() { const int scale = 5; for (int index = 0; index < (int) SK_ARRAY_COUNT(frameSizes); ++index) { SkDRect bounds; bool boundsSet = false; int frameSize = frameSizes[index]; for (int fIndex = 0; fIndex < frameSize; ++fIndex) { const SkDConic& dC = frames[index][fIndex]; SkDConic dConic = {{{ {dC.fPts[0].fX * scale, dC.fPts[0].fY * scale }, {dC.fPts[1].fX * scale, dC.fPts[1].fY * scale }, {dC.fPts[2].fX * scale, dC.fPts[2].fY * scale }}}, dC.fWeight }; SkDRect dBounds; dBounds.setBounds(dConic); if (!boundsSet) { bounds = dBounds; boundsSet = true; } else { bounds.add((SkDPoint&) dBounds.fLeft); bounds.add((SkDPoint&) dBounds.fRight); } } bounds.fLeft -= 10; bounds.fTop -= 10; bounds.fRight += 10; bounds.fBottom += 10; SkBitmap bitmap; bitmap.tryAllocPixels(SkImageInfo::MakeN32Premul( SkScalarRoundToInt(SkDoubleToScalar(bounds.width())), SkScalarRoundToInt(SkDoubleToScalar(bounds.height())))); SkCanvas canvas(bitmap); SkPaint paint; paint.setAntiAlias(true); paint.setStyle(SkPaint::kStroke_Style); canvas.translate(SkDoubleToScalar(-bounds.fLeft), SkDoubleToScalar(-bounds.fTop)); canvas.drawColor(SK_ColorWHITE); for (int fIndex = 0; fIndex < frameSize; ++fIndex) { const SkDConic& dC = frames[index][fIndex]; SkDConic dConic = {{{ {dC.fPts[0].fX * scale, dC.fPts[0].fY * scale }, {dC.fPts[1].fX * scale, dC.fPts[1].fY * scale }, {dC.fPts[2].fX * scale, dC.fPts[2].fY * scale }}}, dC.fWeight }; SkPath path; path.moveTo(dConic.fPts[0].asSkPoint()); path.conicTo(dConic.fPts[1].asSkPoint(), dConic.fPts[2].asSkPoint(), dConic.fWeight); if (fIndex < 2) { paint.setARGB(0x80, 0xFF, 0, 0); } else { paint.setARGB(0x80, 0, 0, 0xFF); } canvas.drawPath(path, paint); } SkString filename("c:\\Users\\caryclark\\Documents\\"); filename.appendf("f%d.png", index); SkImageEncoder::EncodeFile(filename.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100); } }
static void writeDPng(const SkDConic& dC, const char* name) { const int scale = 5; SkDConic dConic = {{{ {dC.fPts[0].fX * scale, dC.fPts[0].fY * scale }, {dC.fPts[1].fX * scale, dC.fPts[1].fY * scale }, {dC.fPts[2].fX * scale, dC.fPts[2].fY * scale }}}, dC.fWeight }; SkBitmap bitmap; SkDRect bounds; bounds.setBounds(dConic); bounds.fLeft -= 10; bounds.fTop -= 10; bounds.fRight += 10; bounds.fBottom += 10; bitmap.tryAllocPixels(SkImageInfo::MakeN32Premul( SkScalarRoundToInt(SkDoubleToScalar(bounds.width())), SkScalarRoundToInt(SkDoubleToScalar(bounds.height())))); SkCanvas canvas(bitmap); SkPaint paint; paint.setAntiAlias(true); paint.setStyle(SkPaint::kStroke_Style); canvas.translate(SkDoubleToScalar(-bounds.fLeft), SkDoubleToScalar(-bounds.fTop)); canvas.drawColor(SK_ColorWHITE); SkPath path; path.moveTo(dConic.fPts[0].asSkPoint()); path.conicTo(dConic.fPts[1].asSkPoint(), dConic.fPts[2].asSkPoint(), dConic.fWeight); paint.setARGB(0x80, 0xFF, 0, 0); canvas.drawPath(path, paint); path.reset(); const int chops = 2; for (int tIndex = 0; tIndex < chops; ++tIndex) { SkDConic chopped = dConic.subDivide(tIndex / (double) chops, (tIndex + 1) / (double) chops); path.moveTo(chopped.fPts[0].asSkPoint()); path.conicTo(chopped.fPts[1].asSkPoint(), chopped.fPts[2].asSkPoint(), chopped.fWeight); } paint.setARGB(0x80, 0, 0, 0xFF); canvas.drawPath(path, paint); SkString filename("c:\\Users\\caryclark\\Documents\\"); filename.appendf("%s.png", name); SkImageEncoder::EncodeFile(filename.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100); }
// intersect the end of the cubic with the other. Try lines from the end to control and opposite // end to determine range of t on opposite cubic. static void intersectEnd(const SkDCubic& cubic1, bool start, const SkDCubic& cubic2, const SkDRect& bounds2, SkIntersections& i) { SkDLine line; int t1Index = start ? 0 : 3; line[0] = cubic1[t1Index]; // don't bother if the two cubics are connnected SkTDArray<double> tVals; // OPTIMIZE: replace with hard-sized array for (int index = 0; index < 4; ++index) { if (index == t1Index) { continue; } SkDVector dxy1 = cubic1[index] - line[0]; dxy1 /= SkDCubic::gPrecisionUnit; line[1] = line[0] + dxy1; SkDRect lineBounds; lineBounds.setBounds(line); if (!bounds2.intersects(&lineBounds)) { continue; } SkIntersections local; if (!local.intersect(cubic2, line)) { continue; } for (int idx2 = 0; idx2 < local.used(); ++idx2) { double foundT = local[0][idx2]; if (approximately_less_than_zero(foundT) || approximately_greater_than_one(foundT)) { continue; } if (local.pt(idx2).approximatelyEqual(line[0])) { if (i.swapped()) { // FIXME: insert should respect swap i.insert(foundT, start ? 0 : 1, line[0]); } else { i.insert(start ? 0 : 1, foundT, line[0]); } } else { *tVals.append() = local[0][idx2]; } } } if (tVals.count() == 0) { return; } QSort<double>(tVals.begin(), tVals.end() - 1); double tMin1 = start ? 0 : 1 - LINE_FRACTION; double tMax1 = start ? LINE_FRACTION : 1; int tIdx = 0; do { int tLast = tIdx; while (tLast + 1 < tVals.count() && roughly_equal(tVals[tLast + 1], tVals[tIdx])) { ++tLast; } double tMin2 = SkTMax<double>(tVals[tIdx] - LINE_FRACTION, 0.0); double tMax2 = SkTMin<double>(tVals[tLast] + LINE_FRACTION, 1.0); int lastUsed = i.used(); intersect(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i); if (lastUsed == i.used()) { tMin2 = SkTMax<double>(tVals[tIdx] - (1.0 / SkDCubic::gPrecisionUnit), 0.0); tMax2 = SkTMin<double>(tVals[tLast] + (1.0 / SkDCubic::gPrecisionUnit), 1.0); intersect(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i); } tIdx = tLast + 1; } while (tIdx < tVals.count()); return; }
void SkIntersections::cubicNearEnd(const SkDCubic& cubic1, bool start, const SkDCubic& cubic2, const SkDRect& bounds2) { SkDLine line; int t1Index = start ? 0 : 3; double testT = (double) !start; // don't bother if the two cubics are connnected static const int kPointsInCubic = 4; // FIXME: move to DCubic, replace '4' with this static const int kMaxLineCubicIntersections = 3; SkSTArray<(kMaxLineCubicIntersections - 1) * kMaxLineCubicIntersections, double, true> tVals; line[0] = cubic1[t1Index]; // this variant looks for intersections with the end point and lines parallel to other points for (int index = 0; index < kPointsInCubic; ++index) { if (index == t1Index) { continue; } SkDVector dxy1 = cubic1[index] - line[0]; dxy1 /= SkDCubic::gPrecisionUnit; line[1] = line[0] + dxy1; SkDRect lineBounds; lineBounds.setBounds(line); if (!bounds2.intersects(&lineBounds)) { continue; } SkIntersections local; if (!local.intersect(cubic2, line)) { continue; } for (int idx2 = 0; idx2 < local.used(); ++idx2) { double foundT = local[0][idx2]; if (approximately_less_than_zero(foundT) || approximately_greater_than_one(foundT)) { continue; } if (local.pt(idx2).approximatelyEqual(line[0])) { if (swapped()) { // FIXME: insert should respect swap insert(foundT, testT, line[0]); } else { insert(testT, foundT, line[0]); } } else { tVals.push_back(foundT); } } } if (tVals.count() == 0) { return; } SkTQSort<double>(tVals.begin(), tVals.end() - 1); double tMin1 = start ? 0 : 1 - LINE_FRACTION; double tMax1 = start ? LINE_FRACTION : 1; int tIdx = 0; do { int tLast = tIdx; while (tLast + 1 < tVals.count() && roughly_equal(tVals[tLast + 1], tVals[tIdx])) { ++tLast; } double tMin2 = SkTMax(tVals[tIdx] - LINE_FRACTION, 0.0); double tMax2 = SkTMin(tVals[tLast] + LINE_FRACTION, 1.0); int lastUsed = used(); if (start ? tMax1 < tMin2 : tMax2 < tMin1) { ::intersect(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, *this); } if (lastUsed == used()) { tMin2 = SkTMax(tVals[tIdx] - (1.0 / SkDCubic::gPrecisionUnit), 0.0); tMax2 = SkTMin(tVals[tLast] + (1.0 / SkDCubic::gPrecisionUnit), 1.0); if (start ? tMax1 < tMin2 : tMax2 < tMin1) { ::intersect(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, *this); } } tIdx = tLast + 1; } while (tIdx < tVals.count()); return; }