gfx/skia/trunk/src/pathops/SkDCubicToQuads.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 http://stackoverflow.com/questions/2009160/how-do-i-convert-the-2-control-points-of-a-cubic-curve-to-the-single-control-poi
michael@0 3 */
michael@0 4
michael@0 5 /*
michael@0 6 Let's call the control points of the cubic Q0..Q3 and the control points of the quadratic P0..P2.
michael@0 7 Then for degree elevation, the equations are:
michael@0 8
michael@0 9 Q0 = P0
michael@0 10 Q1 = 1/3 P0 + 2/3 P1
michael@0 11 Q2 = 2/3 P1 + 1/3 P2
michael@0 12 Q3 = P2
michael@0 13 In your case you have Q0..Q3 and you're solving for P0..P2. There are two ways to compute P1 from
michael@0 14 the equations above:
michael@0 15
michael@0 16 P1 = 3/2 Q1 - 1/2 Q0
michael@0 17 P1 = 3/2 Q2 - 1/2 Q3
michael@0 18 If this is a degree-elevated cubic, then both equations will give the same answer for P1. Since
michael@0 19 it's likely not, your best bet is to average them. So,
michael@0 20
michael@0 21 P1 = -1/4 Q0 + 3/4 Q1 + 3/4 Q2 - 1/4 Q3
michael@0 22
michael@0 23
michael@0 24 SkDCubic defined by: P1/2 - anchor points, C1/C2 control points
michael@0 25 |x| is the euclidean norm of x
michael@0 26 mid-point approx of cubic: a quad that shares the same anchors with the cubic and has the
michael@0 27 control point at C = (3·C2 - P2 + 3·C1 - P1)/4
michael@0 28
michael@0 29 Algorithm
michael@0 30
michael@0 31 pick an absolute precision (prec)
michael@0 32 Compute the Tdiv as the root of (cubic) equation
michael@0 33 sqrt(3)/18 · |P2 - 3·C2 + 3·C1 - P1|/2 · Tdiv ^ 3 = prec
michael@0 34 if Tdiv < 0.5 divide the cubic at Tdiv. First segment [0..Tdiv] can be approximated with by a
michael@0 35 quadratic, with a defect less than prec, by the mid-point approximation.
michael@0 36 Repeat from step 2 with the second resulted segment (corresponding to 1-Tdiv)
michael@0 37 0.5<=Tdiv<1 - simply divide the cubic in two. The two halves can be approximated by the mid-point
michael@0 38 approximation
michael@0 39 Tdiv>=1 - the entire cubic can be approximated by the mid-point approximation
michael@0 40
michael@0 41 confirmed by (maybe stolen from)
michael@0 42 http://www.caffeineowl.com/graphics/2d/vectorial/cubic2quad01.html
michael@0 43 // maybe in turn derived from http://www.cccg.ca/proceedings/2004/36.pdf
michael@0 44 // also stored at http://www.cis.usouthal.edu/~hain/general/Publications/Bezier/bezier%20cccg04%20paper.pdf
michael@0 45
michael@0 46 */
michael@0 47
michael@0 48 #include "SkPathOpsCubic.h"
michael@0 49 #include "SkPathOpsLine.h"
michael@0 50 #include "SkPathOpsQuad.h"
michael@0 51 #include "SkReduceOrder.h"
michael@0 52 #include "SkTArray.h"
michael@0 53 #include "SkTSort.h"
michael@0 54
michael@0 55 #define USE_CUBIC_END_POINTS 1
michael@0 56
michael@0 57 static double calc_t_div(const SkDCubic& cubic, double precision, double start) {
michael@0 58 const double adjust = sqrt(3.) / 36;
michael@0 59 SkDCubic sub;
michael@0 60 const SkDCubic* cPtr;
michael@0 61 if (start == 0) {
michael@0 62 cPtr = &cubic;
michael@0 63 } else {
michael@0 64 // OPTIMIZE: special-case half-split ?
michael@0 65 sub = cubic.subDivide(start, 1);
michael@0 66 cPtr = &sub;
michael@0 67 }
michael@0 68 const SkDCubic& c = *cPtr;
michael@0 69 double dx = c[3].fX - 3 * (c[2].fX - c[1].fX) - c[0].fX;
michael@0 70 double dy = c[3].fY - 3 * (c[2].fY - c[1].fY) - c[0].fY;
michael@0 71 double dist = sqrt(dx * dx + dy * dy);
michael@0 72 double tDiv3 = precision / (adjust * dist);
michael@0 73 double t = SkDCubeRoot(tDiv3);
michael@0 74 if (start > 0) {
michael@0 75 t = start + (1 - start) * t;
michael@0 76 }
michael@0 77 return t;
michael@0 78 }
michael@0 79
michael@0 80 SkDQuad SkDCubic::toQuad() const {
michael@0 81 SkDQuad quad;
michael@0 82 quad[0] = fPts[0];
michael@0 83 const SkDPoint fromC1 = {(3 * fPts[1].fX - fPts[0].fX) / 2, (3 * fPts[1].fY - fPts[0].fY) / 2};
michael@0 84 const SkDPoint fromC2 = {(3 * fPts[2].fX - fPts[3].fX) / 2, (3 * fPts[2].fY - fPts[3].fY) / 2};
michael@0 85 quad[1].fX = (fromC1.fX + fromC2.fX) / 2;
michael@0 86 quad[1].fY = (fromC1.fY + fromC2.fY) / 2;
michael@0 87 quad[2] = fPts[3];
michael@0 88 return quad;
michael@0 89 }
michael@0 90
michael@0 91 static bool add_simple_ts(const SkDCubic& cubic, double precision, SkTArray<double, true>* ts) {
michael@0 92 double tDiv = calc_t_div(cubic, precision, 0);
michael@0 93 if (tDiv >= 1) {
michael@0 94 return true;
michael@0 95 }
michael@0 96 if (tDiv >= 0.5) {
michael@0 97 ts->push_back(0.5);
michael@0 98 return true;
michael@0 99 }
michael@0 100 return false;
michael@0 101 }
michael@0 102
michael@0 103 static void addTs(const SkDCubic& cubic, double precision, double start, double end,
michael@0 104 SkTArray<double, true>* ts) {
michael@0 105 double tDiv = calc_t_div(cubic, precision, 0);
michael@0 106 double parts = ceil(1.0 / tDiv);
michael@0 107 for (double index = 0; index < parts; ++index) {
michael@0 108 double newT = start + (index / parts) * (end - start);
michael@0 109 if (newT > 0 && newT < 1) {
michael@0 110 ts->push_back(newT);
michael@0 111 }
michael@0 112 }
michael@0 113 }
michael@0 114
michael@0 115 // flavor that returns T values only, deferring computing the quads until they are needed
michael@0 116 // FIXME: when called from recursive intersect 2, this could take the original cubic
michael@0 117 // and do a more precise job when calling chop at and sub divide by computing the fractional ts.
michael@0 118 // it would still take the prechopped cubic for reduce order and find cubic inflections
michael@0 119 void SkDCubic::toQuadraticTs(double precision, SkTArray<double, true>* ts) const {
michael@0 120 SkReduceOrder reducer;
michael@0 121 int order = reducer.reduce(*this, SkReduceOrder::kAllow_Quadratics);
michael@0 122 if (order < 3) {
michael@0 123 return;
michael@0 124 }
michael@0 125 double inflectT[5];
michael@0 126 int inflections = findInflections(inflectT);
michael@0 127 SkASSERT(inflections <= 2);
michael@0 128 if (!endsAreExtremaInXOrY()) {
michael@0 129 inflections += findMaxCurvature(&inflectT[inflections]);
michael@0 130 SkASSERT(inflections <= 5);
michael@0 131 }
michael@0 132 SkTQSort<double>(inflectT, &inflectT[inflections - 1]);
michael@0 133 // OPTIMIZATION: is this filtering common enough that it needs to be pulled out into its
michael@0 134 // own subroutine?
michael@0 135 while (inflections && approximately_less_than_zero(inflectT[0])) {
michael@0 136 memmove(inflectT, &inflectT[1], sizeof(inflectT[0]) * --inflections);
michael@0 137 }
michael@0 138 int start = 0;
michael@0 139 int next = 1;
michael@0 140 while (next < inflections) {
michael@0 141 if (!approximately_equal(inflectT[start], inflectT[next])) {
michael@0 142 ++start;
michael@0 143 ++next;
michael@0 144 continue;
michael@0 145 }
michael@0 146 memmove(&inflectT[start], &inflectT[next], sizeof(inflectT[0]) * (--inflections - start));
michael@0 147 }
michael@0 148
michael@0 149 while (inflections && approximately_greater_than_one(inflectT[inflections - 1])) {
michael@0 150 --inflections;
michael@0 151 }
michael@0 152 SkDCubicPair pair;
michael@0 153 if (inflections == 1) {
michael@0 154 pair = chopAt(inflectT[0]);
michael@0 155 int orderP1 = reducer.reduce(pair.first(), SkReduceOrder::kNo_Quadratics);
michael@0 156 if (orderP1 < 2) {
michael@0 157 --inflections;
michael@0 158 } else {
michael@0 159 int orderP2 = reducer.reduce(pair.second(), SkReduceOrder::kNo_Quadratics);
michael@0 160 if (orderP2 < 2) {
michael@0 161 --inflections;
michael@0 162 }
michael@0 163 }
michael@0 164 }
michael@0 165 if (inflections == 0 && add_simple_ts(*this, precision, ts)) {
michael@0 166 return;
michael@0 167 }
michael@0 168 if (inflections == 1) {
michael@0 169 pair = chopAt(inflectT[0]);
michael@0 170 addTs(pair.first(), precision, 0, inflectT[0], ts);
michael@0 171 addTs(pair.second(), precision, inflectT[0], 1, ts);
michael@0 172 return;
michael@0 173 }
michael@0 174 if (inflections > 1) {
michael@0 175 SkDCubic part = subDivide(0, inflectT[0]);
michael@0 176 addTs(part, precision, 0, inflectT[0], ts);
michael@0 177 int last = inflections - 1;
michael@0 178 for (int idx = 0; idx < last; ++idx) {
michael@0 179 part = subDivide(inflectT[idx], inflectT[idx + 1]);
michael@0 180 addTs(part, precision, inflectT[idx], inflectT[idx + 1], ts);
michael@0 181 }
michael@0 182 part = subDivide(inflectT[last], 1);
michael@0 183 addTs(part, precision, inflectT[last], 1, ts);
michael@0 184 return;
michael@0 185 }
michael@0 186 addTs(*this, precision, 0, 1, ts);
michael@0 187 }

mercurial