Index: tests/PathOpsTestCommon.cpp |
diff --git a/tests/PathOpsTestCommon.cpp b/tests/PathOpsTestCommon.cpp |
index 60a12ee56ec87afcb606e05a6916283502b5e23b..f1cba8ed8b2c8d07e79bbdfe5325b240d026f6ab 100644 |
--- a/tests/PathOpsTestCommon.cpp |
+++ b/tests/PathOpsTestCommon.cpp |
@@ -9,11 +9,129 @@ |
#include "SkPathOpsCubic.h" |
#include "SkPathOpsLine.h" |
#include "SkPathOpsQuad.h" |
-#include "SkPathOpsTriangle.h" |
+#include "SkReduceOrder.h" |
+#include "SkTSort.h" |
+ |
+static double calc_t_div(const SkDCubic& cubic, double precision, double start) { |
+ const double adjust = sqrt(3.) / 36; |
+ SkDCubic sub; |
+ const SkDCubic* cPtr; |
+ if (start == 0) { |
+ cPtr = &cubic; |
+ } else { |
+ // OPTIMIZE: special-case half-split ? |
+ sub = cubic.subDivide(start, 1); |
+ cPtr = ⊂ |
+ } |
+ const SkDCubic& c = *cPtr; |
+ double dx = c[3].fX - 3 * (c[2].fX - c[1].fX) - c[0].fX; |
+ double dy = c[3].fY - 3 * (c[2].fY - c[1].fY) - c[0].fY; |
+ double dist = sqrt(dx * dx + dy * dy); |
+ double tDiv3 = precision / (adjust * dist); |
+ double t = SkDCubeRoot(tDiv3); |
+ if (start > 0) { |
+ t = start + (1 - start) * t; |
+ } |
+ return t; |
+} |
+ |
+static bool add_simple_ts(const SkDCubic& cubic, double precision, SkTArray<double, true>* ts) { |
+ double tDiv = calc_t_div(cubic, precision, 0); |
+ if (tDiv >= 1) { |
+ return true; |
+ } |
+ if (tDiv >= 0.5) { |
+ ts->push_back(0.5); |
+ return true; |
+ } |
+ return false; |
+} |
+ |
+static void addTs(const SkDCubic& cubic, double precision, double start, double end, |
+ SkTArray<double, true>* ts) { |
+ double tDiv = calc_t_div(cubic, precision, 0); |
+ double parts = ceil(1.0 / tDiv); |
+ for (double index = 0; index < parts; ++index) { |
+ double newT = start + (index / parts) * (end - start); |
+ if (newT > 0 && newT < 1) { |
+ ts->push_back(newT); |
+ } |
+ } |
+} |
+ |
+static void toQuadraticTs(const SkDCubic* cubic, double precision, SkTArray<double, true>* ts) { |
+ SkReduceOrder reducer; |
+ int order = reducer.reduce(*cubic, SkReduceOrder::kAllow_Quadratics); |
+ if (order < 3) { |
+ return; |
+ } |
+ double inflectT[5]; |
+ int inflections = cubic->findInflections(inflectT); |
+ SkASSERT(inflections <= 2); |
+ if (!cubic->endsAreExtremaInXOrY()) { |
+ inflections += cubic->findMaxCurvature(&inflectT[inflections]); |
+ SkASSERT(inflections <= 5); |
+ } |
+ SkTQSort<double>(inflectT, &inflectT[inflections - 1]); |
+ // OPTIMIZATION: is this filtering common enough that it needs to be pulled out into its |
+ // own subroutine? |
+ while (inflections && approximately_less_than_zero(inflectT[0])) { |
+ memmove(inflectT, &inflectT[1], sizeof(inflectT[0]) * --inflections); |
+ } |
+ int start = 0; |
+ int next = 1; |
+ while (next < inflections) { |
+ if (!approximately_equal(inflectT[start], inflectT[next])) { |
+ ++start; |
+ ++next; |
+ continue; |
+ } |
+ memmove(&inflectT[start], &inflectT[next], sizeof(inflectT[0]) * (--inflections - start)); |
+ } |
+ |
+ while (inflections && approximately_greater_than_one(inflectT[inflections - 1])) { |
+ --inflections; |
+ } |
+ SkDCubicPair pair; |
+ if (inflections == 1) { |
+ pair = cubic->chopAt(inflectT[0]); |
+ int orderP1 = reducer.reduce(pair.first(), SkReduceOrder::kNo_Quadratics); |
+ if (orderP1 < 2) { |
+ --inflections; |
+ } else { |
+ int orderP2 = reducer.reduce(pair.second(), SkReduceOrder::kNo_Quadratics); |
+ if (orderP2 < 2) { |
+ --inflections; |
+ } |
+ } |
+ } |
+ if (inflections == 0 && add_simple_ts(*cubic, precision, ts)) { |
+ return; |
+ } |
+ if (inflections == 1) { |
+ pair = cubic->chopAt(inflectT[0]); |
+ addTs(pair.first(), precision, 0, inflectT[0], ts); |
+ addTs(pair.second(), precision, inflectT[0], 1, ts); |
+ return; |
+ } |
+ if (inflections > 1) { |
+ SkDCubic part = cubic->subDivide(0, inflectT[0]); |
+ addTs(part, precision, 0, inflectT[0], ts); |
+ int last = inflections - 1; |
+ for (int idx = 0; idx < last; ++idx) { |
+ part = cubic->subDivide(inflectT[idx], inflectT[idx + 1]); |
+ addTs(part, precision, inflectT[idx], inflectT[idx + 1], ts); |
+ } |
+ part = cubic->subDivide(inflectT[last], 1); |
+ addTs(part, precision, inflectT[last], 1, ts); |
+ return; |
+ } |
+ addTs(*cubic, precision, 0, 1, ts); |
+} |
void CubicToQuads(const SkDCubic& cubic, double precision, SkTArray<SkDQuad, true>& quads) { |
SkTArray<double, true> ts; |
- cubic.toQuadraticTs(precision, &ts); |
+ toQuadraticTs(&cubic, precision, &ts); |
if (ts.count() <= 0) { |
SkDQuad quad = cubic.toQuad(); |
quads.push_back(quad); |
@@ -180,15 +298,6 @@ bool ValidQuad(const SkDQuad& quad) { |
return true; |
} |
-bool ValidTriangle(const SkDTriangle& triangle) { |
- for (int index = 0; index < 3; ++index) { |
- if (!ValidPoint(triangle.fPts[index])) { |
- return false; |
- } |
- } |
- return true; |
-} |
- |
bool ValidVector(const SkDVector& v) { |
if (SkDoubleIsNaN(v.fX)) { |
return false; |