bool SkXRayCrossesLine(const SkXRay& pt, const SkPoint pts[2]) { // Determine quick discards. // Consider query line going exactly through point 0 to not // intersect, for symmetry with SkXRayCrossesMonotonicCubic. if (pt.fY == pts[0].fY) return false; if (pt.fY < pts[0].fY && pt.fY < pts[1].fY) return false; if (pt.fY > pts[0].fY && pt.fY > pts[1].fY) return false; if (pt.fX > pts[0].fX && pt.fX > pts[1].fX) return false; // Determine degenerate cases if (SkScalarNearlyZero(pts[0].fY - pts[1].fY)) return false; if (SkScalarNearlyZero(pts[0].fX - pts[1].fX)) // We've already determined the query point lies within the // vertical range of the line segment. return pt.fX <= pts[0].fX; // Full line segment evaluation SkScalar delta_y = pts[1].fY - pts[0].fY; SkScalar delta_x = pts[1].fX - pts[0].fX; SkScalar slope = SkScalarDiv(delta_y, delta_x); SkScalar b = pts[0].fY - SkScalarMul(slope, pts[0].fX); // Solve for x coordinate at y = pt.fY SkScalar x = SkScalarDiv(pt.fY - b, slope); return pt.fX <= x; }
/** * For the purposes of drawing bitmaps, if a matrix is "almost" translate * go ahead and treat it as if it were, so that subsequent code can go fast. */ static bool just_trans_general(const SkMatrix& matrix) { SkASSERT(matrix_only_scale_translate(matrix)); const SkScalar tol = SK_Scalar1 / 32768; return SkScalarNearlyZero(matrix[SkMatrix::kMScaleX] - SK_Scalar1, tol) && SkScalarNearlyZero(matrix[SkMatrix::kMScaleY] - SK_Scalar1, tol); }
static AngleType Dot2AngleType(SkScalar dot) { // need more precise fixed normalization // SkASSERT(SkScalarAbs(dot) <= SK_Scalar1 + SK_ScalarNearlyZero); if (dot >= 0) // shallow or line return SkScalarNearlyZero(SK_Scalar1 - dot) ? kNearlyLine_AngleType : kShallow_AngleType; else // sharp or 180 return SkScalarNearlyZero(SK_Scalar1 + dot) ? kNearly180_AngleType : kSharp_AngleType; }
SkColor SkHSVToColor(U8CPU a, const SkScalar hsv[3]) { SkASSERT(hsv); SkScalar s = SkScalarPin(hsv[1], 0, 1); SkScalar v = SkScalarPin(hsv[2], 0, 1); U8CPU v_byte = SkScalarRoundToInt(v * 255); if (SkScalarNearlyZero(s)) { // shade of gray return SkColorSetARGB(a, v_byte, v_byte, v_byte); } SkScalar hx = (hsv[0] < 0 || hsv[0] >= SkIntToScalar(360)) ? 0 : hsv[0]/60; SkScalar w = SkScalarFloorToScalar(hx); SkScalar f = hx - w; unsigned p = SkScalarRoundToInt((SK_Scalar1 - s) * v * 255); unsigned q = SkScalarRoundToInt((SK_Scalar1 - (s * f)) * v * 255); unsigned t = SkScalarRoundToInt((SK_Scalar1 - (s * (SK_Scalar1 - f))) * v * 255); unsigned r, g, b; SkASSERT((unsigned)(w) < 6); switch ((unsigned)(w)) { case 0: r = v_byte; g = t; b = p; break; case 1: r = q; g = v_byte; b = p; break; case 2: r = p; g = v_byte; b = t; break; case 3: r = p; g = q; b = v_byte; break; case 4: r = t; g = p; b = v_byte; break; default: r = v_byte; g = p; b = q; break; } return SkColorSetARGB(a, r, g, b); }
static bool just_trans_general(const SkMatrix& matrix) { SkASSERT(matrix_only_scale_translate(matrix)); if (matrix.getType() & SkMatrix::kScale_Mask) { const SkScalar tol = SK_Scalar1 / 32768; if (!SkScalarNearlyZero(matrix[SkMatrix::kMScaleX] - SK_Scalar1, tol)) { return false; } if (!SkScalarNearlyZero(matrix[SkMatrix::kMScaleY] - SK_Scalar1, tol)) { return false; } } // if we got here, treat us as either kTranslate_Mask or identity return true; }
// Finds affine and persp such that in = affine * persp. // but it returns the inverse of perspective matrix. static bool split_perspective(const SkMatrix in, SkMatrix* affine, SkMatrix* perspectiveInverse) { const SkScalar p2 = in[SkMatrix::kMPersp2]; if (SkScalarNearlyZero(p2)) { return false; } const SkScalar zero = SkIntToScalar(0); const SkScalar one = SkIntToScalar(1); const SkScalar sx = in[SkMatrix::kMScaleX]; const SkScalar kx = in[SkMatrix::kMSkewX]; const SkScalar tx = in[SkMatrix::kMTransX]; const SkScalar ky = in[SkMatrix::kMSkewY]; const SkScalar sy = in[SkMatrix::kMScaleY]; const SkScalar ty = in[SkMatrix::kMTransY]; const SkScalar p0 = in[SkMatrix::kMPersp0]; const SkScalar p1 = in[SkMatrix::kMPersp1]; // Perspective matrix would be: // 1 0 0 // 0 1 0 // p0 p1 p2 // But we need the inverse of persp. perspectiveInverse->setAll(one, zero, zero, zero, one, zero, -p0/p2, -p1/p2, 1/p2); affine->setAll(sx - p0 * tx / p2, kx - p1 * tx / p2, tx / p2, ky - p0 * ty / p2, sy - p1 * ty / p2, ty / p2, zero, zero, one); return true; }
static IntersectionType intersection(const SkPoint& p1, const SkPoint& p2, const SkPoint& p3, const SkPoint& p4, SkPoint& res) { // Store the values for fast access and easy // equations-to-code conversion SkScalar x1 = p1.x(), x2 = p2.x(), x3 = p3.x(), x4 = p4.x(); SkScalar y1 = p1.y(), y2 = p2.y(), y3 = p3.y(), y4 = p4.y(); SkScalar d = SkScalarMul(x1 - x2, y3 - y4) - SkScalarMul(y1 - y2, x3 - x4); // If d is zero, there is no intersection if (SkScalarNearlyZero(d)) { return kNone_IntersectionType; } // Get the x and y SkScalar pre = SkScalarMul(x1, y2) - SkScalarMul(y1, x2), post = SkScalarMul(x3, y4) - SkScalarMul(y3, x4); // Compute the point of intersection res.set(SkScalarDiv(SkScalarMul(pre, x3 - x4) - SkScalarMul(x1 - x2, post), d), SkScalarDiv(SkScalarMul(pre, y3 - y4) - SkScalarMul(y1 - y2, post), d)); // Check if the x and y coordinates are within both lines return (res.x() < GrMin(x1, x2) || res.x() > GrMax(x1, x2) || res.x() < GrMin(x3, x4) || res.x() > GrMax(x3, x4) || res.y() < GrMin(y1, y2) || res.y() > GrMax(y1, y2) || res.y() < GrMin(y3, y4) || res.y() > GrMax(y3, y4)) ? kOut_IntersectionType : kIn_IntersectionType; }
bool SkPathContainsPoint(const SkPath& originalPath, const FloatPoint& point, SkPath::FillType ft) { SkRect bounds = originalPath.getBounds(); // We can immediately return false if the point is outside the bounding // rect. We don't use bounds.contains() here, since it would exclude // points on the right and bottom edges of the bounding rect, and we want // to include them. SkScalar fX = SkFloatToScalar(point.x()); SkScalar fY = SkFloatToScalar(point.y()); if (fX < bounds.fLeft || fX > bounds.fRight || fY < bounds.fTop || fY > bounds.fBottom) return false; // Scale the path to a large size before hit testing for two reasons: // 1) Skia has trouble with coordinates close to the max signed 16-bit values, so we scale larger paths down. // TODO: when Skia is patched to work properly with large values, this will not be necessary. // 2) Skia does not support analytic hit testing, so we scale paths up to do raster hit testing with subpixel accuracy. // 3) Scale the x/y axis separately so an extreme large/small scale factor on one axis won't // ruin the resolution of the other axis. SkScalar biggestCoordX = std::max(bounds.fRight, -bounds.fLeft); SkScalar biggestCoordY = std::max(bounds.fBottom, -bounds.fTop); if (SkScalarNearlyZero(biggestCoordX) || SkScalarNearlyZero(biggestCoordY)) return false; biggestCoordX = std::max(biggestCoordX, std::fabs(fX) + 1); biggestCoordY = std::max(biggestCoordY, std::fabs(fY) + 1); const SkScalar kMaxCoordinate = SkIntToScalar(1 << 15); SkScalar scaleX = kMaxCoordinate / biggestCoordX; SkScalar scaleY = kMaxCoordinate / biggestCoordY; SkRegion rgn; SkRegion clip; SkMatrix m; SkPath scaledPath(originalPath); scaledPath.setFillType(ft); m.setScale(scaleX, scaleY); scaledPath.transform(m, 0); int x = static_cast<int>(floorf(0.5f + point.x() * scaleX)); int y = static_cast<int>(floorf(0.5f + point.y() * scaleY)); clip.setRect(x - 1, y - 1, x + 1, y + 1); return rgn.setPath(scaledPath, clip); }
void onSetData(const GrGLSLProgramDataManager& pdman, const GrFragmentProcessor& p) override { INHERITED::onSetData(pdman, p); const TwoPointConicalEffect& effect = p.cast<TwoPointConicalEffect>(); // kRadialType should imply |r1 - r0| = 1 (after our transformation) SkASSERT(effect.getType() == Type::kStrip || SkScalarNearlyZero(SkTAbs(effect.diffRadius()) - 1)); pdman.set1f(fParamUni, effect.getType() == Type::kRadial ? effect.r0() : effect.r0() * effect.r0()); }
// return X coordinate of intersection with horizontal line at Y static SkScalar sect_with_horizontal(const SkPoint src[2], SkScalar Y) { SkScalar dy = src[1].fY - src[0].fY; if (SkScalarNearlyZero(dy)) { return SkScalarAve(src[0].fX, src[1].fX); } else { return src[0].fX + SkScalarMulDiv(Y - src[0].fY, src[1].fX - src[0].fX, dy); } }
// return Y coordinate of intersection with vertical line at X static SkScalar sect_with_vertical(const SkPoint src[2], SkScalar X) { SkScalar dx = src[1].fX - src[0].fX; if (SkScalarNearlyZero(dx)) { return SkScalarAve(src[0].fY, src[1].fY); } else { return src[0].fY + SkScalarMulDiv(X - src[0].fX, src[1].fY - src[0].fY, dx); } }
static void test_conic_tangents(skiatest::Reporter* reporter) { SkPoint pts[] = { { 10, 20}, {10, 20}, {20, 30}, { 10, 20}, {15, 25}, {20, 30}, { 10, 20}, {20, 30}, {20, 30} }; int count = (int) SK_ARRAY_COUNT(pts) / 3; for (int index = 0; index < count; ++index) { SkConic conic(&pts[index * 3], 0.707f); SkVector start = conic.evalTangentAt(0); SkVector mid = conic.evalTangentAt(.5f); SkVector end = conic.evalTangentAt(1); REPORTER_ASSERT(reporter, start.fX && start.fY); REPORTER_ASSERT(reporter, mid.fX && mid.fY); REPORTER_ASSERT(reporter, end.fX && end.fY); REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid))); REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end))); } }
static bool just_trans_general(const SkMatrix& matrix) { SkMatrix::TypeMask mask = matrix.getType(); if (mask & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)) { return false; } if (mask & SkMatrix::kScale_Mask) { const SkScalar tol = SK_Scalar1 / 32768; if (!SkScalarNearlyZero(matrix[SkMatrix::kMScaleX] - SK_Scalar1, tol)) { return false; } if (!SkScalarNearlyZero(matrix[SkMatrix::kMScaleY] - SK_Scalar1, tol)) { return false; } } // if we got here, treat us as either kTranslate_Mask or identity return true; }
// Computes perpDot for point compared to segment. // A positive value means the point is to the left of the segment, // negative is to the right, 0 is collinear. static int compute_side(const SkPoint& s0, const SkPoint& s1, const SkPoint& p) { SkVector v0 = s1 - s0; SkVector v1 = p - s0; SkScalar perpDot = v0.cross(v1); if (!SkScalarNearlyZero(perpDot)) { return ((perpDot > 0) ? 1 : -1); } return 0; }
/* Solve coeff(t) == 0, returning the number of roots that lie withing 0 < t < 1. coeff[0]t^3 + coeff[1]t^2 + coeff[2]t + coeff[3] Eliminates repeated roots (so that all tValues are distinct, and are always in increasing order. */ static int solve_cubic_polynomial(const SkFP coeff[4], SkScalar tValues[3]) { #ifndef SK_SCALAR_IS_FLOAT return 0; // this is not yet implemented for software float #endif if (SkScalarNearlyZero(coeff[0])) // we're just a quadratic { return SkFindUnitQuadRoots(coeff[1], coeff[2], coeff[3], tValues); } SkFP a, b, c, Q, R; { SkASSERT(coeff[0] != 0); SkFP inva = SkFPInvert(coeff[0]); a = SkFPMul(coeff[1], inva); b = SkFPMul(coeff[2], inva); c = SkFPMul(coeff[3], inva); } Q = SkFPDivInt(SkFPSub(SkFPMul(a,a), SkFPMulInt(b, 3)), 9); // R = (2*a*a*a - 9*a*b + 27*c) / 54; R = SkFPMulInt(SkFPMul(SkFPMul(a, a), a), 2); R = SkFPSub(R, SkFPMulInt(SkFPMul(a, b), 9)); R = SkFPAdd(R, SkFPMulInt(c, 27)); R = SkFPDivInt(R, 54); SkFP Q3 = SkFPMul(SkFPMul(Q, Q), Q); SkFP R2MinusQ3 = SkFPSub(SkFPMul(R,R), Q3); SkFP adiv3 = SkFPDivInt(a, 3); SkScalar* roots = tValues; SkScalar r; if (SkFPLT(R2MinusQ3, 0)) // we have 3 real roots { #ifdef SK_SCALAR_IS_FLOAT float theta = sk_float_acos(R / sk_float_sqrt(Q3)); float neg2RootQ = -2 * sk_float_sqrt(Q); r = neg2RootQ * sk_float_cos(theta/3) - adiv3; if (is_unit_interval(r)) *roots++ = r; r = neg2RootQ * sk_float_cos((theta + 2*SK_ScalarPI)/3) - adiv3; if (is_unit_interval(r)) *roots++ = r; r = neg2RootQ * sk_float_cos((theta - 2*SK_ScalarPI)/3) - adiv3; if (is_unit_interval(r)) *roots++ = r; SkDEBUGCODE(test_collaps_duplicates();)
static void test_cubic_tangents(skiatest::Reporter* reporter) { SkPoint pts[] = { { 10, 20}, {10, 20}, {20, 30}, {30, 40}, { 10, 20}, {15, 25}, {20, 30}, {30, 40}, { 10, 20}, {20, 30}, {30, 40}, {30, 40}, }; int count = (int) SK_ARRAY_COUNT(pts) / 4; for (int index = 0; index < count; ++index) { SkConic conic(&pts[index * 3], 0.707f); SkVector start, mid, end; SkEvalCubicAt(&pts[index * 4], 0, nullptr, &start, nullptr); SkEvalCubicAt(&pts[index * 4], .5f, nullptr, &mid, nullptr); SkEvalCubicAt(&pts[index * 4], 1, nullptr, &end, nullptr); REPORTER_ASSERT(reporter, start.fX && start.fY); REPORTER_ASSERT(reporter, mid.fX && mid.fY); REPORTER_ASSERT(reporter, end.fX && end.fY); REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid))); REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end))); } }
static void center_of_mass(const SegmentArray& segments, SkPoint* c) { SkScalar area = 0; SkPoint center = {0, 0}; int count = segments.count(); SkPoint p0 = {0, 0}; if (count > 2) { // We translate the polygon so that the first point is at the origin. // This avoids some precision issues with small area polygons far away // from the origin. p0 = segments[0].endPt(); SkPoint pi; SkPoint pj; // the first and last iteration of the below loop would compute // zeros since the starting / ending point is (0,0). So instead we start // at i=1 and make the last iteration i=count-2. pj = segments[1].endPt() - p0; for (int i = 1; i < count - 1; ++i) { pi = pj; const SkPoint pj = segments[i + 1].endPt() - p0; SkScalar t = SkScalarMul(pi.fX, pj.fY) - SkScalarMul(pj.fX, pi.fY); area += t; center.fX += (pi.fX + pj.fX) * t; center.fY += (pi.fY + pj.fY) * t; } } // If the poly has no area then we instead return the average of // its points. if (SkScalarNearlyZero(area)) { SkPoint avg; avg.set(0, 0); for (int i = 0; i < count; ++i) { const SkPoint& pt = segments[i].endPt(); avg.fX += pt.fX; avg.fY += pt.fY; } SkScalar denom = SK_Scalar1 / count; avg.scale(denom); *c = avg; } else { area *= 3; area = SkScalarDiv(SK_Scalar1, area); center.fX = SkScalarMul(center.fX, area); center.fY = SkScalarMul(center.fY, area); // undo the translate of p0 to the origin. *c = center + p0; } SkASSERT(!SkScalarIsNaN(c->fX) && !SkScalarIsNaN(c->fY)); }
// return Y coordinate of intersection with vertical line at X static SkScalar sect_with_vertical(const SkPoint src[2], SkScalar X) { SkScalar dx = src[1].fX - src[0].fX; if (SkScalarNearlyZero(dx)) { return SkScalarAve(src[0].fY, src[1].fY); } else { // need the extra precision so we don't compute a value that exceeds // our original limits double X0 = src[0].fX; double Y0 = src[0].fY; double X1 = src[1].fX; double Y1 = src[1].fY; double result = Y0 + ((double)X - X0) * (Y1 - Y0) / (X1 - X0); return (float)result; } }
void SkLinearGradient:: LinearGradient4fContext::shadeSpanInternal(int x, int y, dstType dst[], int count, float bias0, float bias1) const { SkPoint pt; fDstToPosProc(fDstToPos, x + SK_ScalarHalf, y + SK_ScalarHalf, &pt); const SkScalar fx = pinFx<tileMode>(pt.x()); const SkScalar dx = fDstToPos.getScaleX(); LinearIntervalProcessor<dstType, premul, tileMode> proc(fIntervals->begin(), fIntervals->end() - 1, this->findInterval(fx), fx, dx, SkScalarNearlyZero(dx * count)); Sk4f bias4f0(bias0), bias4f1(bias1); while (count > 0) { // What we really want here is SkTPin(advance, 1, count) // but that's a significant perf hit for >> stops; investigate. const int n = SkScalarTruncToInt( SkTMin<SkScalar>(proc.currentAdvance() + 1, SkIntToScalar(count))); // The current interval advance can be +inf (e.g. when reaching // the clamp mode end intervals) - when that happens, we expect to // a) consume all remaining count in one swoop // b) return a zero color gradient SkASSERT(SkScalarIsFinite(proc.currentAdvance()) || (n == count && proc.currentRampIsZero())); if (proc.currentRampIsZero()) { DstTraits<dstType, premul>::store(proc.currentColor(), dst, n); } else { ramp<dstType, premul>(proc.currentColor(), proc.currentColorGrad(), dst, n, bias4f0, bias4f1); } proc.advance(SkIntToScalar(n)); count -= n; dst += n; if (n & 1) { SkTSwap(bias4f0, bias4f1); } } }
static SkXfermode* Create(SkScalar k1, SkScalar k2, SkScalar k3, SkScalar k4, bool enforcePMColor) { if (SkScalarNearlyZero(k1) && SkScalarNearlyEqual(k2, SK_Scalar1) && SkScalarNearlyZero(k3) && SkScalarNearlyZero(k4)) { return SkXfermode::Create(SkXfermode::kSrc_Mode); } else if (SkScalarNearlyZero(k1) && SkScalarNearlyZero(k2) && SkScalarNearlyEqual(k3, SK_Scalar1) && SkScalarNearlyZero(k4)) { return SkXfermode::Create(SkXfermode::kDst_Mode); } return new SkArithmeticMode_scalar(k1, k2, k3, k4, enforcePMColor); }
bool SkPathContainsPoint(SkPath* originalPath, const FloatPoint& point, SkPath::FillType ft) { SkRect bounds = originalPath->getBounds(); // We can immediately return false if the point is outside the bounding // rect. We don't use bounds.contains() here, since it would exclude // points on the right and bottom edges of the bounding rect, and we want // to include them. SkScalar fX = SkFloatToScalar(point.x()); SkScalar fY = SkFloatToScalar(point.y()); if (fX < bounds.fLeft || fX > bounds.fRight || fY < bounds.fTop || fY > bounds.fBottom) return false; // Scale the path to a large size before hit testing for two reasons: // 1) Skia has trouble with coordinates close to the max signed 16-bit values, so we scale larger paths down. // TODO: when Skia is patched to work properly with large values, this will not be necessary. // 2) Skia does not support analytic hit testing, so we scale paths up to do raster hit testing with subpixel accuracy. SkScalar biggestCoord = std::max(std::max(std::max(bounds.fRight, bounds.fBottom), -bounds.fLeft), -bounds.fTop); if (SkScalarNearlyZero(biggestCoord)) return false; biggestCoord = std::max(std::max(biggestCoord, fX + 1), fY + 1); const SkScalar kMaxCoordinate = SkIntToScalar(1 << 15); SkScalar scale = SkScalarDiv(kMaxCoordinate, biggestCoord); SkRegion rgn; SkRegion clip; SkMatrix m; SkPath scaledPath; SkPath::FillType originalFillType = originalPath->getFillType(); originalPath->setFillType(ft); m.setScale(scale, scale); originalPath->transform(m, &scaledPath); int x = static_cast<int>(floorf(0.5f + point.x() * scale)); int y = static_cast<int>(floorf(0.5f + point.y() * scale)); clip.setRect(x - 1, y - 1, x + 1, y + 1); bool contains = rgn.setPath(scaledPath, clip); originalPath->setFillType(originalFillType); return contains; }
// return X coordinate of intersection with horizontal line at Y static SkScalar sect_with_horizontal(const SkPoint src[2], SkScalar Y) { SkScalar dy = src[1].fY - src[0].fY; if (SkScalarNearlyZero(dy)) { return SkScalarAve(src[0].fX, src[1].fX); } else { // need the extra precision so we don't compute a value that exceeds // our original limits double X0 = src[0].fX; double Y0 = src[0].fY; double X1 = src[1].fX; double Y1 = src[1].fY; double result = X0 + ((double)Y - Y0) * (X1 - X0) / (Y1 - Y0); // The computed X value might still exceed [X0..X1] due to quantum flux // when the doubles were added and subtracted, so we have to pin the // answer :( return (float)pin_unsorted(result, X0, X1); } }
// Compute the intersection 'p' between segments s0 and s1, if any. // 's' is the parametric value for the intersection along 's0' & 't' is the same for 's1'. // Returns false if there is no intersection. static bool compute_intersection(const InsetSegment& s0, const InsetSegment& s1, SkPoint* p, SkScalar* s, SkScalar* t) { SkVector v0 = s0.fP1 - s0.fP0; SkVector v1 = s1.fP1 - s1.fP0; SkScalar perpDot = v0.cross(v1); if (SkScalarNearlyZero(perpDot)) { // segments are parallel // check if endpoints are touching if (s0.fP1.equalsWithinTolerance(s1.fP0)) { *p = s0.fP1; *s = SK_Scalar1; *t = 0; return true; } if (s1.fP1.equalsWithinTolerance(s0.fP0)) { *p = s1.fP1; *s = 0; *t = SK_Scalar1; return true; } return false; } SkVector d = s1.fP0 - s0.fP0; SkScalar localS = d.cross(v1) / perpDot; if (localS < 0 || localS > SK_Scalar1) { return false; } SkScalar localT = d.cross(v0) / perpDot; if (localT < 0 || localT > SK_Scalar1) { return false; } v0 *= localS; *p = s0.fP0 + v0; *s = localS; *t = localT; return true; }
/* Solve coeff(t) == 0, returning the number of roots that lie withing 0 < t < 1. coeff[0]t^3 + coeff[1]t^2 + coeff[2]t + coeff[3] Eliminates repeated roots (so that all tValues are distinct, and are always in increasing order. */ static int solve_cubic_poly(const SkScalar coeff[4], SkScalar tValues[3]) { if (SkScalarNearlyZero(coeff[0])) { // we're just a quadratic return SkFindUnitQuadRoots(coeff[1], coeff[2], coeff[3], tValues); } SkScalar a, b, c, Q, R; { SkASSERT(coeff[0] != 0); SkScalar inva = SkScalarInvert(coeff[0]); a = coeff[1] * inva; b = coeff[2] * inva; c = coeff[3] * inva; } Q = (a*a - b*3) / 9; R = (2*a*a*a - 9*a*b + 27*c) / 54; SkScalar Q3 = Q * Q * Q; SkScalar R2MinusQ3 = R * R - Q3; SkScalar adiv3 = a / 3; SkScalar* roots = tValues; SkScalar r; if (R2MinusQ3 < 0) { // we have 3 real roots SkScalar theta = SkScalarACos(R / SkScalarSqrt(Q3)); SkScalar neg2RootQ = -2 * SkScalarSqrt(Q); r = neg2RootQ * SkScalarCos(theta/3) - adiv3; if (is_unit_interval(r)) { *roots++ = r; } r = neg2RootQ * SkScalarCos((theta + 2*SK_ScalarPI)/3) - adiv3; if (is_unit_interval(r)) { *roots++ = r; } r = neg2RootQ * SkScalarCos((theta - 2*SK_ScalarPI)/3) - adiv3; if (is_unit_interval(r)) { *roots++ = r; } SkDEBUGCODE(test_collaps_duplicates();)
// return X coordinate of intersection with horizontal line at Y static SkScalar sect_with_horizontal(const SkPoint src[2], SkScalar Y) { SkScalar dy = src[1].fY - src[0].fY; if (SkScalarNearlyZero(dy)) { return SkScalarAve(src[0].fX, src[1].fX); } else { #ifdef SK_SCALAR_IS_FLOAT // need the extra precision so we don't compute a value that exceeds // our original limits double X0 = src[0].fX; double Y0 = src[0].fY; double X1 = src[1].fX; double Y1 = src[1].fY; double result = X0 + ((double)Y - Y0) * (X1 - X0) / (Y1 - Y0); return (float)result; #else return src[0].fX + SkScalarMulDiv(Y - src[0].fY, src[1].fX - src[0].fX, dy); #endif } }
bool SkSetPoly3To3(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]) { const SkPoint& srcAve = src[0]; const SkPoint& dstAve = dst[0]; SkScalar srcOP[4], dstOP[4]; computeOuterProduct(srcOP, src, srcAve, src, srcAve); computeOuterProduct(dstOP, src, srcAve, dst, dstAve); SkScalar det = SkScalarMul(srcOP[0], srcOP[3]) - SkScalarMul(srcOP[1], srcOP[2]); // need SkScalarNearlyZeroSquared for this (to match Chrome's fix) if (SkScalarNearlyZero(det)) { return false; } SkScalar invDet = SkScalarInvert(det); // now compute invDet * [srcOP]T * [dstOP] // scale and transpose const SkScalar srcOP0 = SkScalarMul( srcOP[3], invDet); const SkScalar srcOP1 = SkScalarMul(-srcOP[1], invDet); const SkScalar srcOP2 = SkScalarMul(-srcOP[2], invDet); const SkScalar srcOP3 = SkScalarMul( srcOP[0], invDet); matrix->reset(); matrix->setScaleX(dot(srcOP0, srcOP1, dstOP[0], dstOP[2])); matrix->setSkewX( dot(srcOP2, srcOP3, dstOP[0], dstOP[2])); matrix->setSkewY (dot(srcOP0, srcOP1, dstOP[1], dstOP[3])); matrix->setScaleY(dot(srcOP2, srcOP3, dstOP[1], dstOP[3])); matrix->setTranslateX(dstAve.fX - dot(srcAve.fX, srcAve.fY, matrix->getScaleX(), matrix->getSkewX())); matrix->setTranslateY(dstAve.fY - dot(srcAve.fX, srcAve.fY, matrix->getSkewY(), matrix->getScaleY())); return true; }
static void test_matrix_decomposition(skiatest::Reporter* reporter) { SkMatrix mat; SkPoint rotation1, scale, rotation2; const float kRotation0 = 15.5f; const float kRotation1 = -50.f; const float kScale0 = 5000.f; const float kScale1 = 0.001f; // identity mat.reset(); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); // make sure it doesn't crash if we pass in NULLs REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, NULL, NULL, NULL)); // rotation only mat.setRotate(kRotation0); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); // uniform scale only mat.setScale(kScale0, kScale0); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); // anisotropic scale only mat.setScale(kScale1, kScale0); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); // rotation then uniform scale mat.setRotate(kRotation1); mat.postScale(kScale0, kScale0); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); // uniform scale then rotation mat.setScale(kScale0, kScale0); mat.postRotate(kRotation1); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); // rotation then uniform scale+reflection mat.setRotate(kRotation0); mat.postScale(kScale1, -kScale1); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); // uniform scale+reflection, then rotate mat.setScale(kScale0, -kScale0); mat.postRotate(kRotation1); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); // rotation then anisotropic scale mat.setRotate(kRotation1); mat.postScale(kScale1, kScale0); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); // rotation then anisotropic scale mat.setRotate(90); mat.postScale(kScale1, kScale0); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); // anisotropic scale then rotation mat.setScale(kScale1, kScale0); mat.postRotate(kRotation0); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); // anisotropic scale then rotation mat.setScale(kScale1, kScale0); mat.postRotate(90); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); // rotation, uniform scale, then different rotation mat.setRotate(kRotation1); mat.postScale(kScale0, kScale0); mat.postRotate(kRotation0); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); // rotation, anisotropic scale, then different rotation mat.setRotate(kRotation0); mat.postScale(kScale1, kScale0); mat.postRotate(kRotation1); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); // rotation, anisotropic scale + reflection, then different rotation mat.setRotate(kRotation0); mat.postScale(-kScale1, kScale0); mat.postRotate(kRotation1); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); // try some random matrices SkRandom rand; for (int m = 0; m < 1000; ++m) { SkScalar rot0 = rand.nextRangeF(-180, 180); SkScalar sx = rand.nextRangeF(-3000.f, 3000.f); SkScalar sy = rand.nextRangeF(-3000.f, 3000.f); SkScalar rot1 = rand.nextRangeF(-180, 180); mat.setRotate(rot0); mat.postScale(sx, sy); mat.postRotate(rot1); if (SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)) { REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); } else { // if the matrix is degenerate, the basis vectors should be near-parallel or near-zero SkScalar perpdot = mat[SkMatrix::kMScaleX]*mat[SkMatrix::kMScaleY] - mat[SkMatrix::kMSkewX]*mat[SkMatrix::kMSkewY]; REPORTER_ASSERT(reporter, SkScalarNearlyZero(perpdot)); } } // translation shouldn't affect this mat.postTranslate(-1000.f, 1000.f); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); // perspective shouldn't affect this mat[SkMatrix::kMPersp0] = 12.f; mat[SkMatrix::kMPersp1] = 4.f; mat[SkMatrix::kMPersp2] = 1872.f; REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2)); // degenerate matrices // mostly zero entries mat.reset(); mat[SkMatrix::kMScaleX] = 0.f; REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); mat.reset(); mat[SkMatrix::kMScaleY] = 0.f; REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); mat.reset(); // linearly dependent entries mat[SkMatrix::kMScaleX] = 1.f; mat[SkMatrix::kMSkewX] = 2.f; mat[SkMatrix::kMSkewY] = 4.f; mat[SkMatrix::kMScaleY] = 8.f; REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)); }
static void test_matrix_decomposition(skiatest::Reporter* reporter) { SkMatrix mat; SkScalar rotation0, scaleX, scaleY, rotation1; const float kRotation0 = 15.5f; const float kRotation1 = -50.f; const float kScale0 = 5000.f; const float kScale1 = 0.001f; // identity mat.reset(); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation0)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, SK_Scalar1)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, SK_Scalar1)); REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1)); // make sure it doesn't crash if we pass in NULLs REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, NULL, NULL, NULL, NULL)); // rotation only mat.setRotate(kRotation0); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation0))); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, SK_Scalar1)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, SK_Scalar1)); REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1)); // uniform scale only mat.setScale(kScale0, kScale0); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation0)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale0)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0)); REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1)); // anisotropic scale only mat.setScale(kScale1, kScale0); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation0)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale1)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0)); REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1)); // rotation then uniform scale mat.setRotate(kRotation1); mat.postScale(kScale0, kScale0); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation1))); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale0)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0)); REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1)); // uniform scale then rotation mat.setScale(kScale0, kScale0); mat.postRotate(kRotation1); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation1))); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale0)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0)); REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1)); // rotation then uniform scale+reflection mat.setRotate(kRotation0); mat.postScale(kScale1, -kScale1); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation0))); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale1)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, -kScale1)); REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1)); // uniform scale+reflection, then rotate mat.setScale(kScale0, -kScale0); mat.postRotate(kRotation1); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(-kRotation1))); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale0)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, -kScale0)); REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1)); // rotation then anisotropic scale mat.setRotate(kRotation1); mat.postScale(kScale1, kScale0); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation1))); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale1)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0)); REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1)); // anisotropic scale then rotation mat.setScale(kScale1, kScale0); mat.postRotate(kRotation0); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation0)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale1)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation1, SkDegreesToRadians(kRotation0))); // rotation, uniform scale, then different rotation mat.setRotate(kRotation1); mat.postScale(kScale0, kScale0); mat.postRotate(kRotation0); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation0 + kRotation1))); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale0)); REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0)); REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1)); // rotation, anisotropic scale, then different rotation mat.setRotate(kRotation0); mat.postScale(kScale1, kScale0); mat.postRotate(kRotation1); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); // Because of the shear/skew we won't get the same results, so we need to multiply it out. // Generating the matrices requires doing a radian-to-degree calculation, then degree-to-radian // calculation (in setRotate()), which adds error, so this just computes the matrix elements // directly. SkScalar c0; SkScalar s0 = SkScalarSinCos(rotation0, &c0); SkScalar c1; SkScalar s1 = SkScalarSinCos(rotation1, &c1); // We do a relative check here because large scale factors cause problems with an absolute check REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX], scaleX*c0*c1 - scaleY*s0*s1)); REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX], -scaleX*s0*c1 - scaleY*c0*s1)); REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY], scaleX*c0*s1 + scaleY*s0*c1)); REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY], -scaleX*s0*s1 + scaleY*c0*c1)); // try some random matrices SkMWCRandom rand; for (int m = 0; m < 1000; ++m) { SkScalar rot0 = rand.nextRangeF(-SK_ScalarPI, SK_ScalarPI); SkScalar sx = rand.nextRangeF(-3000.f, 3000.f); SkScalar sy = rand.nextRangeF(-3000.f, 3000.f); SkScalar rot1 = rand.nextRangeF(-SK_ScalarPI, SK_ScalarPI); mat.setRotate(rot0); mat.postScale(sx, sy); mat.postRotate(rot1); if (SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)) { SkScalar c0; SkScalar s0 = SkScalarSinCos(rotation0, &c0); SkScalar c1; SkScalar s1 = SkScalarSinCos(rotation1, &c1); REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX], scaleX*c0*c1 - scaleY*s0*s1)); REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX], -scaleX*s0*c1 - scaleY*c0*s1)); REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY], scaleX*c0*s1 + scaleY*s0*c1)); REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY], -scaleX*s0*s1 + scaleY*c0*c1)); } else { // if the matrix is degenerate, the basis vectors should be near-parallel or near-zero SkScalar perpdot = mat[SkMatrix::kMScaleX]*mat[SkMatrix::kMScaleY] - mat[SkMatrix::kMSkewX]*mat[SkMatrix::kMSkewY]; REPORTER_ASSERT(reporter, SkScalarNearlyZero(perpdot)); } } // translation shouldn't affect this mat.postTranslate(-1000.f, 1000.f); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); s0 = SkScalarSinCos(rotation0, &c0); s1 = SkScalarSinCos(rotation1, &c1); REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX], scaleX*c0*c1 - scaleY*s0*s1)); REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX], -scaleX*s0*c1 - scaleY*c0*s1)); REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY], scaleX*c0*s1 + scaleY*s0*c1)); REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY], -scaleX*s0*s1 + scaleY*c0*c1)); // perspective shouldn't affect this mat[SkMatrix::kMPersp0] = 12.f; mat[SkMatrix::kMPersp1] = 4.f; mat[SkMatrix::kMPersp2] = 1872.f; REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); s0 = SkScalarSinCos(rotation0, &c0); s1 = SkScalarSinCos(rotation1, &c1); REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX], scaleX*c0*c1 - scaleY*s0*s1)); REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX], -scaleX*s0*c1 - scaleY*c0*s1)); REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY], scaleX*c0*s1 + scaleY*s0*c1)); REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY], -scaleX*s0*s1 + scaleY*c0*c1)); // rotation, anisotropic scale + reflection, then different rotation mat.setRotate(kRotation0); mat.postScale(-kScale1, kScale0); mat.postRotate(kRotation1); REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); s0 = SkScalarSinCos(rotation0, &c0); s1 = SkScalarSinCos(rotation1, &c1); REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX], scaleX*c0*c1 - scaleY*s0*s1)); REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX], -scaleX*s0*c1 - scaleY*c0*s1)); REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY], scaleX*c0*s1 + scaleY*s0*c1)); REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY], -scaleX*s0*s1 + scaleY*c0*c1)); // degenerate matrices // mostly zero entries mat.reset(); mat[SkMatrix::kMScaleX] = 0.f; REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); mat.reset(); mat[SkMatrix::kMScaleY] = 0.f; REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); mat.reset(); // linearly dependent entries mat[SkMatrix::kMScaleX] = 1.f; mat[SkMatrix::kMSkewX] = 2.f; mat[SkMatrix::kMSkewY] = 4.f; mat[SkMatrix::kMScaleY] = 8.f; REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)); }
void SkPathStroker::cubicTo(const SkPoint& pt1, const SkPoint& pt2, const SkPoint& pt3) { bool degenerateAB = SkPath::IsLineDegenerate(fPrevPt, pt1); bool degenerateBC = SkPath::IsLineDegenerate(pt1, pt2); bool degenerateCD = SkPath::IsLineDegenerate(pt2, pt3); if (degenerateAB + degenerateBC + degenerateCD >= 2) { this->lineTo(pt3); return; } SkVector normalAB, unitAB, normalCD, unitCD; // find the first tangent (which might be pt1 or pt2 { const SkPoint* nextPt = &pt1; if (degenerateAB) nextPt = &pt2; this->preJoinTo(*nextPt, &normalAB, &unitAB, false); } { SkPoint pts[4], tmp[13]; int i, count; SkVector n, u; SkScalar tValues[3]; pts[0] = fPrevPt; pts[1] = pt1; pts[2] = pt2; pts[3] = pt3; #if 1 count = SkChopCubicAtMaxCurvature(pts, tmp, tValues); #else count = 1; memcpy(tmp, pts, 4 * sizeof(SkPoint)); #endif n = normalAB; u = unitAB; for (i = 0; i < count; i++) { this->cubic_to(&tmp[i * 3], n, u, &normalCD, &unitCD, kMaxCubicSubdivide); if (i == count - 1) { break; } n = normalCD; u = unitCD; } // check for too pinchy for (i = 1; i < count; i++) { SkPoint p; SkVector v, c; SkEvalCubicAt(pts, tValues[i - 1], &p, &v, &c); SkScalar dot = SkPoint::DotProduct(c, c); v.scale(SkScalarInvert(dot)); if (SkScalarNearlyZero(v.fX) && SkScalarNearlyZero(v.fY)) { fExtra.addCircle(p.fX, p.fY, fRadius, SkPath::kCW_Direction); } } } this->postJoinTo(pt3, normalCD, unitCD); }
bool SkXRayCrossesMonotonicCubic(const SkXRay& pt, const SkPoint cubic[4]) { // Find the minimum and maximum y of the extrema, which are the // first and last points since this cubic is monotonic SkScalar min_y = SkMinScalar(cubic[0].fY, cubic[3].fY); SkScalar max_y = SkMaxScalar(cubic[0].fY, cubic[3].fY); if (pt.fY == cubic[0].fY || pt.fY < min_y || pt.fY > max_y) { // The query line definitely does not cross the curve return false; } SkScalar min_x = SkMinScalar( SkMinScalar( SkMinScalar(cubic[0].fX, cubic[1].fX), cubic[2].fX), cubic[3].fX); if (pt.fX < min_x) { // The query line definitely crosses the curve return true; } SkScalar max_x = SkMaxScalar( SkMaxScalar( SkMaxScalar(cubic[0].fX, cubic[1].fX), cubic[2].fX), cubic[3].fX); if (pt.fX > max_x) { // The query line definitely does not cross the curve return false; } // Do a binary search to find the parameter value which makes y as // close as possible to the query point. See whether the query // line's origin is to the left of the associated x coordinate. // kMaxIter is chosen as the number of mantissa bits for a float, // since there's no way we are going to get more precision by // iterating more times than that. const int kMaxIter = 23; SkPoint eval; int iter = 0; SkScalar upper_t; SkScalar lower_t; // Need to invert direction of t parameter if cubic goes up // instead of down if (cubic[3].fY > cubic[0].fY) { upper_t = SK_Scalar1; lower_t = SkFloatToScalar(0); } else { upper_t = SkFloatToScalar(0); lower_t = SK_Scalar1; } do { SkScalar t = SkScalarAve(upper_t, lower_t); SkEvalCubicAt(cubic, t, &eval, NULL, NULL); if (pt.fY > eval.fY) { lower_t = t; } else { upper_t = t; } } while (++iter < kMaxIter && !SkScalarNearlyZero(eval.fY - pt.fY)); if (pt.fX <= eval.fX) { return true; } return false; }