OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 #include "PathOpsTestCommon.h" | 7 #include "PathOpsTestCommon.h" |
8 #include "SkPathOpsBounds.h" | 8 #include "SkPathOpsBounds.h" |
9 #include "SkPathOpsCubic.h" | 9 #include "SkPathOpsCubic.h" |
10 #include "SkPathOpsLine.h" | 10 #include "SkPathOpsLine.h" |
11 #include "SkPathOpsQuad.h" | 11 #include "SkPathOpsQuad.h" |
12 #include "SkPathOpsTriangle.h" | 12 #include "SkReduceOrder.h" |
| 13 #include "SkTSort.h" |
| 14 |
| 15 static double calc_t_div(const SkDCubic& cubic, double precision, double start)
{ |
| 16 const double adjust = sqrt(3.) / 36; |
| 17 SkDCubic sub; |
| 18 const SkDCubic* cPtr; |
| 19 if (start == 0) { |
| 20 cPtr = &cubic; |
| 21 } else { |
| 22 // OPTIMIZE: special-case half-split ? |
| 23 sub = cubic.subDivide(start, 1); |
| 24 cPtr = ⊂ |
| 25 } |
| 26 const SkDCubic& c = *cPtr; |
| 27 double dx = c[3].fX - 3 * (c[2].fX - c[1].fX) - c[0].fX; |
| 28 double dy = c[3].fY - 3 * (c[2].fY - c[1].fY) - c[0].fY; |
| 29 double dist = sqrt(dx * dx + dy * dy); |
| 30 double tDiv3 = precision / (adjust * dist); |
| 31 double t = SkDCubeRoot(tDiv3); |
| 32 if (start > 0) { |
| 33 t = start + (1 - start) * t; |
| 34 } |
| 35 return t; |
| 36 } |
| 37 |
| 38 static bool add_simple_ts(const SkDCubic& cubic, double precision, SkTArray<doub
le, true>* ts) { |
| 39 double tDiv = calc_t_div(cubic, precision, 0); |
| 40 if (tDiv >= 1) { |
| 41 return true; |
| 42 } |
| 43 if (tDiv >= 0.5) { |
| 44 ts->push_back(0.5); |
| 45 return true; |
| 46 } |
| 47 return false; |
| 48 } |
| 49 |
| 50 static void addTs(const SkDCubic& cubic, double precision, double start, double
end, |
| 51 SkTArray<double, true>* ts) { |
| 52 double tDiv = calc_t_div(cubic, precision, 0); |
| 53 double parts = ceil(1.0 / tDiv); |
| 54 for (double index = 0; index < parts; ++index) { |
| 55 double newT = start + (index / parts) * (end - start); |
| 56 if (newT > 0 && newT < 1) { |
| 57 ts->push_back(newT); |
| 58 } |
| 59 } |
| 60 } |
| 61 |
| 62 static void toQuadraticTs(const SkDCubic* cubic, double precision, SkTArray<doub
le, true>* ts) { |
| 63 SkReduceOrder reducer; |
| 64 int order = reducer.reduce(*cubic, SkReduceOrder::kAllow_Quadratics); |
| 65 if (order < 3) { |
| 66 return; |
| 67 } |
| 68 double inflectT[5]; |
| 69 int inflections = cubic->findInflections(inflectT); |
| 70 SkASSERT(inflections <= 2); |
| 71 if (!cubic->endsAreExtremaInXOrY()) { |
| 72 inflections += cubic->findMaxCurvature(&inflectT[inflections]); |
| 73 SkASSERT(inflections <= 5); |
| 74 } |
| 75 SkTQSort<double>(inflectT, &inflectT[inflections - 1]); |
| 76 // OPTIMIZATION: is this filtering common enough that it needs to be pulled
out into its |
| 77 // own subroutine? |
| 78 while (inflections && approximately_less_than_zero(inflectT[0])) { |
| 79 memmove(inflectT, &inflectT[1], sizeof(inflectT[0]) * --inflections); |
| 80 } |
| 81 int start = 0; |
| 82 int next = 1; |
| 83 while (next < inflections) { |
| 84 if (!approximately_equal(inflectT[start], inflectT[next])) { |
| 85 ++start; |
| 86 ++next; |
| 87 continue; |
| 88 } |
| 89 memmove(&inflectT[start], &inflectT[next], sizeof(inflectT[0]) * (--infl
ections - start)); |
| 90 } |
| 91 |
| 92 while (inflections && approximately_greater_than_one(inflectT[inflections -
1])) { |
| 93 --inflections; |
| 94 } |
| 95 SkDCubicPair pair; |
| 96 if (inflections == 1) { |
| 97 pair = cubic->chopAt(inflectT[0]); |
| 98 int orderP1 = reducer.reduce(pair.first(), SkReduceOrder::kNo_Quadratics
); |
| 99 if (orderP1 < 2) { |
| 100 --inflections; |
| 101 } else { |
| 102 int orderP2 = reducer.reduce(pair.second(), SkReduceOrder::kNo_Quadr
atics); |
| 103 if (orderP2 < 2) { |
| 104 --inflections; |
| 105 } |
| 106 } |
| 107 } |
| 108 if (inflections == 0 && add_simple_ts(*cubic, precision, ts)) { |
| 109 return; |
| 110 } |
| 111 if (inflections == 1) { |
| 112 pair = cubic->chopAt(inflectT[0]); |
| 113 addTs(pair.first(), precision, 0, inflectT[0], ts); |
| 114 addTs(pair.second(), precision, inflectT[0], 1, ts); |
| 115 return; |
| 116 } |
| 117 if (inflections > 1) { |
| 118 SkDCubic part = cubic->subDivide(0, inflectT[0]); |
| 119 addTs(part, precision, 0, inflectT[0], ts); |
| 120 int last = inflections - 1; |
| 121 for (int idx = 0; idx < last; ++idx) { |
| 122 part = cubic->subDivide(inflectT[idx], inflectT[idx + 1]); |
| 123 addTs(part, precision, inflectT[idx], inflectT[idx + 1], ts); |
| 124 } |
| 125 part = cubic->subDivide(inflectT[last], 1); |
| 126 addTs(part, precision, inflectT[last], 1, ts); |
| 127 return; |
| 128 } |
| 129 addTs(*cubic, precision, 0, 1, ts); |
| 130 } |
13 | 131 |
14 void CubicToQuads(const SkDCubic& cubic, double precision, SkTArray<SkDQuad, tru
e>& quads) { | 132 void CubicToQuads(const SkDCubic& cubic, double precision, SkTArray<SkDQuad, tru
e>& quads) { |
15 SkTArray<double, true> ts; | 133 SkTArray<double, true> ts; |
16 cubic.toQuadraticTs(precision, &ts); | 134 toQuadraticTs(&cubic, precision, &ts); |
17 if (ts.count() <= 0) { | 135 if (ts.count() <= 0) { |
18 SkDQuad quad = cubic.toQuad(); | 136 SkDQuad quad = cubic.toQuad(); |
19 quads.push_back(quad); | 137 quads.push_back(quad); |
20 return; | 138 return; |
21 } | 139 } |
22 double tStart = 0; | 140 double tStart = 0; |
23 for (int i1 = 0; i1 <= ts.count(); ++i1) { | 141 for (int i1 = 0; i1 <= ts.count(); ++i1) { |
24 const double tEnd = i1 < ts.count() ? ts[i1] : 1; | 142 const double tEnd = i1 < ts.count() ? ts[i1] : 1; |
25 SkDCubic part = cubic.subDivide(tStart, tEnd); | 143 SkDCubic part = cubic.subDivide(tStart, tEnd); |
26 SkDQuad quad = part.toQuad(); | 144 SkDQuad quad = part.toQuad(); |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 | 291 |
174 bool ValidQuad(const SkDQuad& quad) { | 292 bool ValidQuad(const SkDQuad& quad) { |
175 for (int index = 0; index < 3; ++index) { | 293 for (int index = 0; index < 3; ++index) { |
176 if (!ValidPoint(quad[index])) { | 294 if (!ValidPoint(quad[index])) { |
177 return false; | 295 return false; |
178 } | 296 } |
179 } | 297 } |
180 return true; | 298 return true; |
181 } | 299 } |
182 | 300 |
183 bool ValidTriangle(const SkDTriangle& triangle) { | |
184 for (int index = 0; index < 3; ++index) { | |
185 if (!ValidPoint(triangle.fPts[index])) { | |
186 return false; | |
187 } | |
188 } | |
189 return true; | |
190 } | |
191 | |
192 bool ValidVector(const SkDVector& v) { | 301 bool ValidVector(const SkDVector& v) { |
193 if (SkDoubleIsNaN(v.fX)) { | 302 if (SkDoubleIsNaN(v.fX)) { |
194 return false; | 303 return false; |
195 } | 304 } |
196 return !SkDoubleIsNaN(v.fY); | 305 return !SkDoubleIsNaN(v.fY); |
197 } | 306 } |
OLD | NEW |