caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 7 | #include "Simplify.h" |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 8 | |
| 9 | #undef SkASSERT |
| 10 | #define SkASSERT(cond) while (!(cond)) { sk_throw(); } |
| 11 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 12 | // Terminology: |
| 13 | // A Path contains one of more Contours |
| 14 | // A Contour is made up of Segment array |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 15 | // A Segment is described by a Verb and a Point array with 2, 3, or 4 points |
| 16 | // A Verb is one of Line, Quad(ratic), or Cubic |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 17 | // A Segment contains a Span array |
| 18 | // A Span is describes a portion of a Segment using starting and ending T |
| 19 | // T values range from 0 to 1, where 0 is the first Point in the Segment |
| 20 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 21 | // FIXME: remove once debugging is complete |
| 22 | #if 0 // set to 1 for no debugging whatsoever |
| 23 | |
| 24 | //const bool gxRunTestsInOneThread = false; |
| 25 | |
| 26 | #define DEBUG_ADD_INTERSECTING_TS 0 |
| 27 | #define DEBUG_BRIDGE 0 |
| 28 | #define DEBUG_DUMP 0 |
| 29 | |
| 30 | #else |
| 31 | |
| 32 | //const bool gRunTestsInOneThread = true; |
| 33 | |
| 34 | #define DEBUG_ADD_INTERSECTING_TS 1 |
| 35 | #define DEBUG_BRIDGE 1 |
| 36 | #define DEBUG_DUMP 1 |
| 37 | |
| 38 | #endif |
| 39 | |
| 40 | #if DEBUG_DUMP |
| 41 | static const char* kLVerbStr[] = {"", "line", "quad", "cubic"}; |
| 42 | static const char* kUVerbStr[] = {"", "Line", "Quad", "Cubic"}; |
| 43 | static int gContourID; |
| 44 | static int gSegmentID; |
| 45 | #endif |
| 46 | |
| 47 | static int LineIntersect(const SkPoint a[2], const SkPoint b[2], |
| 48 | Intersections& intersections) { |
| 49 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
| 50 | const _Line bLine = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}}; |
| 51 | return intersect(aLine, bLine, intersections.fT[0], intersections.fT[1]); |
| 52 | } |
| 53 | |
| 54 | static int QuadLineIntersect(const SkPoint a[3], const SkPoint b[2], |
| 55 | Intersections& intersections) { |
| 56 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}}; |
| 57 | const _Line bLine = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}}; |
| 58 | intersect(aQuad, bLine, intersections); |
| 59 | return intersections.fUsed; |
| 60 | } |
| 61 | |
| 62 | static int CubicLineIntersect(const SkPoint a[2], const SkPoint b[3], |
| 63 | Intersections& intersections) { |
| 64 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}, |
| 65 | {a[3].fX, a[3].fY}}; |
| 66 | const _Line bLine = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}}; |
| 67 | return intersect(aCubic, bLine, intersections.fT[0], intersections.fT[1]); |
| 68 | } |
| 69 | |
| 70 | static int QuadIntersect(const SkPoint a[3], const SkPoint b[3], |
| 71 | Intersections& intersections) { |
| 72 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}}; |
| 73 | const Quadratic bQuad = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}, {b[2].fX, b[2].fY}}; |
| 74 | intersect(aQuad, bQuad, intersections); |
| 75 | return intersections.fUsed; |
| 76 | } |
| 77 | |
| 78 | static int CubicIntersect(const SkPoint a[4], const SkPoint b[4], |
| 79 | Intersections& intersections) { |
| 80 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}, |
| 81 | {a[3].fX, a[3].fY}}; |
| 82 | const Cubic bCubic = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}, {b[2].fX, b[2].fY}, |
| 83 | {b[3].fX, b[3].fY}}; |
| 84 | intersect(aCubic, bCubic, intersections); |
| 85 | return intersections.fUsed; |
| 86 | } |
| 87 | |
| 88 | static int HLineIntersect(const SkPoint a[2], SkScalar left, SkScalar right, |
| 89 | SkScalar y, bool flipped, Intersections& intersections) { |
| 90 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
| 91 | return horizontalIntersect(aLine, left, right, y, flipped, intersections); |
| 92 | } |
| 93 | |
| 94 | static int VLineIntersect(const SkPoint a[2], SkScalar left, SkScalar right, |
| 95 | SkScalar y, bool flipped, Intersections& intersections) { |
| 96 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
| 97 | return verticalIntersect(aLine, left, right, y, flipped, intersections); |
| 98 | } |
| 99 | |
| 100 | static int HQuadIntersect(const SkPoint a[3], SkScalar left, SkScalar right, |
| 101 | SkScalar y, bool flipped, Intersections& intersections) { |
| 102 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}}; |
| 103 | return horizontalIntersect(aQuad, left, right, y, flipped, intersections); |
| 104 | } |
| 105 | |
| 106 | static int VQuadIntersect(const SkPoint a[3], SkScalar left, SkScalar right, |
| 107 | SkScalar y, bool flipped, Intersections& intersections) { |
| 108 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}}; |
| 109 | return verticalIntersect(aQuad, left, right, y, flipped, intersections); |
| 110 | } |
| 111 | |
| 112 | static int HCubicIntersect(const SkPoint a[4], SkScalar left, SkScalar right, |
| 113 | SkScalar y, bool flipped, Intersections& intersections) { |
| 114 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}, |
| 115 | {a[3].fX, a[3].fY}}; |
| 116 | return horizontalIntersect(aCubic, left, right, y, flipped, intersections); |
| 117 | } |
| 118 | |
| 119 | static int VCubicIntersect(const SkPoint a[4], SkScalar left, SkScalar right, |
| 120 | SkScalar y, bool flipped, Intersections& intersections) { |
| 121 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}, |
| 122 | {a[3].fX, a[3].fY}}; |
| 123 | return verticalIntersect(aCubic, left, right, y, flipped, intersections); |
| 124 | } |
| 125 | |
| 126 | static void LineXYAtT(const SkPoint a[2], double t, SkPoint* out) { |
| 127 | const _Line line = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
| 128 | double x, y; |
| 129 | xy_at_t(line, t, x, y); |
| 130 | out->fX = SkDoubleToScalar(x); |
| 131 | out->fY = SkDoubleToScalar(y); |
| 132 | } |
| 133 | |
| 134 | static void QuadXYAtT(const SkPoint a[3], double t, SkPoint* out) { |
| 135 | const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}}; |
| 136 | double x, y; |
| 137 | xy_at_t(quad, t, x, y); |
| 138 | out->fX = SkDoubleToScalar(x); |
| 139 | out->fY = SkDoubleToScalar(y); |
| 140 | } |
| 141 | |
| 142 | static void CubicXYAtT(const SkPoint a[4], double t, SkPoint* out) { |
| 143 | const Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}, |
| 144 | {a[3].fX, a[3].fY}}; |
| 145 | double x, y; |
| 146 | xy_at_t(cubic, t, x, y); |
| 147 | out->fX = SkDoubleToScalar(x); |
| 148 | out->fY = SkDoubleToScalar(y); |
| 149 | } |
| 150 | |
| 151 | static void (* const SegmentXYAtT[])(const SkPoint [], double , SkPoint* ) = { |
| 152 | NULL, |
| 153 | LineXYAtT, |
| 154 | QuadXYAtT, |
| 155 | CubicXYAtT |
| 156 | }; |
| 157 | |
| 158 | static SkScalar LineXAtT(const SkPoint a[2], double t) { |
| 159 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
| 160 | double x; |
| 161 | xy_at_t(aLine, t, x, *(double*) 0); |
| 162 | return SkDoubleToScalar(x); |
| 163 | } |
| 164 | |
| 165 | static SkScalar QuadXAtT(const SkPoint a[3], double t) { |
| 166 | const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}}; |
| 167 | double x; |
| 168 | xy_at_t(quad, t, x, *(double*) 0); |
| 169 | return SkDoubleToScalar(x); |
| 170 | } |
| 171 | |
| 172 | static SkScalar CubicXAtT(const SkPoint a[4], double t) { |
| 173 | const Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}, |
| 174 | {a[3].fX, a[3].fY}}; |
| 175 | double x; |
| 176 | xy_at_t(cubic, t, x, *(double*) 0); |
| 177 | return SkDoubleToScalar(x); |
| 178 | } |
| 179 | |
| 180 | static SkScalar (* const SegmentXAtT[])(const SkPoint [], double ) = { |
| 181 | NULL, |
| 182 | LineXAtT, |
| 183 | QuadXAtT, |
| 184 | CubicXAtT |
| 185 | }; |
| 186 | |
| 187 | static SkScalar LineYAtT(const SkPoint a[2], double t) { |
| 188 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
| 189 | double y; |
| 190 | xy_at_t(aLine, t, *(double*) 0, y); |
| 191 | return SkDoubleToScalar(y); |
| 192 | } |
| 193 | |
| 194 | static SkScalar QuadYAtT(const SkPoint a[3], double t) { |
| 195 | const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}}; |
| 196 | double y; |
| 197 | xy_at_t(quad, t, *(double*) 0, y); |
| 198 | return SkDoubleToScalar(y); |
| 199 | } |
| 200 | |
| 201 | static SkScalar CubicYAtT(const SkPoint a[4], double t) { |
| 202 | const Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}, |
| 203 | {a[3].fX, a[3].fY}}; |
| 204 | double y; |
| 205 | xy_at_t(cubic, t, *(double*) 0, y); |
| 206 | return SkDoubleToScalar(y); |
| 207 | } |
| 208 | |
| 209 | static SkScalar (* const SegmentYAtT[])(const SkPoint [], double ) = { |
| 210 | NULL, |
| 211 | LineYAtT, |
| 212 | QuadYAtT, |
| 213 | CubicYAtT |
| 214 | }; |
| 215 | |
| 216 | static void LineSubDivide(const SkPoint a[2], double startT, double endT, |
| 217 | SkPoint sub[2]) { |
| 218 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
| 219 | _Line dst; |
| 220 | sub_divide(aLine, startT, endT, dst); |
| 221 | sub[0].fX = SkDoubleToScalar(dst[0].x); |
| 222 | sub[0].fY = SkDoubleToScalar(dst[0].y); |
| 223 | sub[1].fX = SkDoubleToScalar(dst[1].x); |
| 224 | sub[1].fY = SkDoubleToScalar(dst[1].y); |
| 225 | } |
| 226 | |
| 227 | static void QuadSubDivide(const SkPoint a[3], double startT, double endT, |
| 228 | SkPoint sub[3]) { |
| 229 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 230 | {a[2].fX, a[2].fY}}; |
| 231 | Quadratic dst; |
| 232 | sub_divide(aQuad, startT, endT, dst); |
| 233 | sub[0].fX = SkDoubleToScalar(dst[0].x); |
| 234 | sub[0].fY = SkDoubleToScalar(dst[0].y); |
| 235 | sub[1].fX = SkDoubleToScalar(dst[1].x); |
| 236 | sub[1].fY = SkDoubleToScalar(dst[1].y); |
| 237 | sub[2].fX = SkDoubleToScalar(dst[2].x); |
| 238 | sub[2].fY = SkDoubleToScalar(dst[2].y); |
| 239 | } |
| 240 | |
| 241 | static void CubicSubDivide(const SkPoint a[4], double startT, double endT, |
| 242 | SkPoint sub[4]) { |
| 243 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 244 | {a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}}; |
| 245 | Cubic dst; |
| 246 | sub_divide(aCubic, startT, endT, dst); |
| 247 | sub[0].fX = SkDoubleToScalar(dst[0].x); |
| 248 | sub[0].fY = SkDoubleToScalar(dst[0].y); |
| 249 | sub[1].fX = SkDoubleToScalar(dst[1].x); |
| 250 | sub[1].fY = SkDoubleToScalar(dst[1].y); |
| 251 | sub[2].fX = SkDoubleToScalar(dst[2].x); |
| 252 | sub[2].fY = SkDoubleToScalar(dst[2].y); |
| 253 | sub[3].fX = SkDoubleToScalar(dst[3].x); |
| 254 | sub[3].fY = SkDoubleToScalar(dst[3].y); |
| 255 | } |
| 256 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 257 | static void (* const SegmentSubDivide[])(const SkPoint [], double , double , |
| 258 | SkPoint []) = { |
| 259 | NULL, |
| 260 | LineSubDivide, |
| 261 | QuadSubDivide, |
| 262 | CubicSubDivide |
| 263 | }; |
| 264 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 265 | static void QuadSubBounds(const SkPoint a[3], double startT, double endT, |
| 266 | SkRect& bounds) { |
| 267 | SkPoint dst[3]; |
| 268 | QuadSubDivide(a, startT, endT, dst); |
| 269 | bounds.fLeft = bounds.fRight = dst[0].fX; |
| 270 | bounds.fTop = bounds.fBottom = dst[0].fY; |
| 271 | for (int index = 1; index < 3; ++index) { |
| 272 | bounds.growToInclude(dst[index].fX, dst[index].fY); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | static void CubicSubBounds(const SkPoint a[4], double startT, double endT, |
| 277 | SkRect& bounds) { |
| 278 | SkPoint dst[4]; |
| 279 | CubicSubDivide(a, startT, endT, dst); |
| 280 | bounds.fLeft = bounds.fRight = dst[0].fX; |
| 281 | bounds.fTop = bounds.fBottom = dst[0].fY; |
| 282 | for (int index = 1; index < 4; ++index) { |
| 283 | bounds.growToInclude(dst[index].fX, dst[index].fY); |
| 284 | } |
| 285 | } |
| 286 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 287 | static SkPath::Verb QuadReduceOrder(const SkPoint a[3], |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 288 | SkTDArray<SkPoint>& reducePts) { |
| 289 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 290 | {a[2].fX, a[2].fY}}; |
| 291 | Quadratic dst; |
| 292 | int order = reduceOrder(aQuad, dst); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 293 | if (order == 3) { |
| 294 | return SkPath::kQuad_Verb; |
| 295 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 296 | for (int index = 0; index < order; ++index) { |
| 297 | SkPoint* pt = reducePts.append(); |
| 298 | pt->fX = SkDoubleToScalar(dst[index].x); |
| 299 | pt->fY = SkDoubleToScalar(dst[index].y); |
| 300 | } |
| 301 | return (SkPath::Verb) (order - 1); |
| 302 | } |
| 303 | |
| 304 | static SkPath::Verb CubicReduceOrder(const SkPoint a[4], |
| 305 | SkTDArray<SkPoint>& reducePts) { |
| 306 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 307 | {a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}}; |
| 308 | Cubic dst; |
| 309 | int order = reduceOrder(aCubic, dst, kReduceOrder_QuadraticsAllowed); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 310 | if (order == 4) { |
| 311 | return SkPath::kCubic_Verb; |
| 312 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 313 | for (int index = 0; index < order; ++index) { |
| 314 | SkPoint* pt = reducePts.append(); |
| 315 | pt->fX = SkDoubleToScalar(dst[index].x); |
| 316 | pt->fY = SkDoubleToScalar(dst[index].y); |
| 317 | } |
| 318 | return (SkPath::Verb) (order - 1); |
| 319 | } |
| 320 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 321 | static bool QuadIsLinear(const SkPoint a[3]) { |
| 322 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 323 | {a[2].fX, a[2].fY}}; |
| 324 | return isLinear(aQuad, 0, 2); |
| 325 | } |
| 326 | |
| 327 | static bool CubicIsLinear(const SkPoint a[4]) { |
| 328 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 329 | {a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}}; |
| 330 | return isLinear(aCubic, 0, 3); |
| 331 | } |
| 332 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 333 | static SkScalar LineLeftMost(const SkPoint a[2], double startT, double endT) { |
| 334 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
| 335 | double x[2]; |
| 336 | xy_at_t(aLine, startT, x[0], *(double*) 0); |
| 337 | xy_at_t(aLine, endT, x[0], *(double*) 0); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 338 | return startT < endT ? (float) startT : (float) endT; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | static SkScalar QuadLeftMost(const SkPoint a[3], double startT, double endT) { |
| 342 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 343 | {a[2].fX, a[2].fY}}; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 344 | return (float) leftMostT(aQuad, startT, endT); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | static SkScalar CubicLeftMost(const SkPoint a[4], double startT, double endT) { |
| 348 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 349 | {a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}}; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 350 | return (float) leftMostT(aCubic, startT, endT); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | static SkScalar (* const SegmentLeftMost[])(const SkPoint [], double , double) = { |
| 354 | NULL, |
| 355 | LineLeftMost, |
| 356 | QuadLeftMost, |
| 357 | CubicLeftMost |
| 358 | }; |
| 359 | |
| 360 | static bool IsCoincident(const SkPoint a[2], const SkPoint& above, |
| 361 | const SkPoint& below) { |
| 362 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
| 363 | const _Line bLine = {{above.fX, above.fY}, {below.fX, below.fY}}; |
| 364 | return implicit_matches_ulps(aLine, bLine, 32); |
| 365 | } |
| 366 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 367 | class Segment; |
| 368 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 369 | // sorting angles |
| 370 | // given angles of {dx dy ddx ddy dddx dddy} sort them |
| 371 | class Angle { |
| 372 | public: |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 373 | // FIXME: this is bogus for quads and cubics |
| 374 | // if the quads and cubics' line from end pt to ctrl pt are coincident, |
| 375 | // there's no obvious way to determine the curve ordering from the |
| 376 | // derivatives alone. In particular, if one quadratic's coincident tangent |
| 377 | // is longer than the other curve, the final control point can place the |
| 378 | // longer curve on either side of the shorter one. |
| 379 | // Using Bezier curve focus http://cagd.cs.byu.edu/~tom/papers/bezclip.pdf |
| 380 | // may provide some help, but nothing has been figured out yet. |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 381 | bool operator<(const Angle& rh) const { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 382 | if ((fDy < 0) ^ (rh.fDy < 0)) { |
| 383 | return fDy < 0; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 384 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 385 | if (fDy == 0 && rh.fDy == 0 && fDx != rh.fDx) { |
| 386 | return fDx < rh.fDx; |
| 387 | } |
| 388 | SkScalar cmp = fDx * rh.fDy - rh.fDx * fDy; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 389 | if (cmp) { |
| 390 | return cmp < 0; |
| 391 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 392 | if ((fDDy < 0) ^ (rh.fDDy < 0)) { |
| 393 | return fDDy < 0; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 394 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 395 | if (fDDy == 0 && rh.fDDy == 0 && fDDx != rh.fDDx) { |
| 396 | return fDDx < rh.fDDx; |
| 397 | } |
| 398 | cmp = fDDx * rh.fDDy - rh.fDDx * fDDy; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 399 | if (cmp) { |
| 400 | return cmp < 0; |
| 401 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 402 | if ((fDDDy < 0) ^ (rh.fDDDy < 0)) { |
| 403 | return fDDDy < 0; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 404 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 405 | if (fDDDy == 0 && rh.fDDDy == 0) { |
| 406 | return fDDDx < rh.fDDDx; |
| 407 | } |
| 408 | return fDDDx * rh.fDDDy < rh.fDDDx * fDDDy; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 409 | } |
| 410 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 411 | int end() const { |
| 412 | return fEnd; |
| 413 | } |
| 414 | |
| 415 | // since all angles share a point, this needs to know which point |
| 416 | // is the common origin, i.e., whether the center is at pts[0] or pts[verb] |
| 417 | // practically, this should only be called by addAngle |
| 418 | void set(const SkPoint* pts, SkPath::Verb verb, const Segment* segment, |
| 419 | int start, int end, bool coincident) { |
| 420 | SkASSERT(start != end); |
| 421 | fSegment = segment; |
| 422 | fStart = start; |
| 423 | fEnd = end; |
| 424 | fCoincident = coincident; |
| 425 | fDx = pts[1].fX - pts[0].fX; // b - a |
| 426 | fDy = pts[1].fY - pts[0].fY; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 427 | if (verb == SkPath::kLine_Verb) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 428 | fDDx = fDDy = fDDDx = fDDDy = 0; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 429 | return; |
| 430 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 431 | fDDx = pts[2].fX - pts[1].fX - fDx; // a - 2b + c |
| 432 | fDDy = pts[2].fY - pts[1].fY - fDy; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 433 | if (verb == SkPath::kQuad_Verb) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 434 | fDDDx = fDDDy = 0; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 435 | return; |
| 436 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 437 | fDDDx = pts[3].fX + 3 * (pts[1].fX - pts[2].fX) - pts[0].fX; |
| 438 | fDDDy = pts[3].fY + 3 * (pts[1].fY - pts[2].fY) - pts[0].fY; |
| 439 | } |
| 440 | |
| 441 | // noncoincident quads/cubics may have the same initial angle |
| 442 | // as lines, so must sort by derivatives as well |
| 443 | // if flatness turns out to be a reasonable way to sort, use the below: |
| 444 | void setFlat(const SkPoint* pts, SkPath::Verb verb, const Segment* segment, |
| 445 | int start, int end, bool coincident) { |
| 446 | fSegment = segment; |
| 447 | fStart = start; |
| 448 | fEnd = end; |
| 449 | fCoincident = coincident; |
| 450 | fDx = pts[1].fX - pts[0].fX; // b - a |
| 451 | fDy = pts[1].fY - pts[0].fY; |
| 452 | if (verb == SkPath::kLine_Verb) { |
| 453 | fDDx = fDDy = fDDDx = fDDDy = 0; |
| 454 | return; |
| 455 | } |
| 456 | if (verb == SkPath::kQuad_Verb) { |
| 457 | int uplsX = FloatAsInt(pts[2].fX - pts[1].fY - fDx); |
| 458 | int uplsY = FloatAsInt(pts[2].fY - pts[1].fY - fDy); |
| 459 | int larger = std::max(abs(uplsX), abs(uplsY)); |
| 460 | int shift = 0; |
| 461 | double flatT; |
| 462 | SkPoint ddPt; // FIXME: get rid of copy (change fDD_ to point) |
| 463 | LineParameters implicitLine; |
| 464 | _Line tangent = {{pts[0].fX, pts[0].fY}, {pts[1].fX, pts[1].fY}}; |
| 465 | implicitLine.lineEndPoints(tangent); |
| 466 | implicitLine.normalize(); |
| 467 | while (larger > UlpsEpsilon * 1024) { |
| 468 | larger >>= 2; |
| 469 | ++shift; |
| 470 | flatT = 0.5 / (1 << shift); |
| 471 | QuadXYAtT(pts, flatT, &ddPt); |
| 472 | _Point _pt = {ddPt.fX, ddPt.fY}; |
| 473 | double distance = implicitLine.pointDistance(_pt); |
| 474 | if (approximately_zero(distance)) { |
| 475 | SkDebugf("%s ulps too small %1.9g\n", __FUNCTION__, distance); |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | flatT = 0.5 / (1 << shift); |
| 480 | QuadXYAtT(pts, flatT, &ddPt); |
| 481 | fDDx = ddPt.fX - pts[0].fX; |
| 482 | fDDy = ddPt.fY - pts[0].fY; |
| 483 | SkASSERT(fDDx != 0 || fDDy != 0); |
| 484 | fDDDx = fDDDy = 0; |
| 485 | return; |
| 486 | } |
| 487 | SkASSERT(0); // FIXME: add cubic case |
| 488 | } |
| 489 | |
| 490 | const Segment* segment() const { |
| 491 | return fSegment; |
| 492 | } |
| 493 | |
| 494 | int sign() const { |
| 495 | int result = fStart - fEnd >> 31 | 1; |
| 496 | SkASSERT(result == fStart < fEnd ? -1 : 1); |
| 497 | return result; |
| 498 | } |
| 499 | |
| 500 | int start() const { |
| 501 | return fStart; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | private: |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 505 | SkScalar fDx; |
| 506 | SkScalar fDy; |
| 507 | SkScalar fDDx; |
| 508 | SkScalar fDDy; |
| 509 | SkScalar fDDDx; |
| 510 | SkScalar fDDDy; |
| 511 | const Segment* fSegment; |
| 512 | int fStart; |
| 513 | int fEnd; |
| 514 | bool fCoincident; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 515 | }; |
| 516 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 517 | static void sortAngles(SkTDArray<Angle>& angles, SkTDArray<Angle*>& angleList) { |
| 518 | int angleCount = angles.count(); |
| 519 | int angleIndex; |
| 520 | angleList.setReserve(angleCount); |
| 521 | for (angleIndex = 0; angleIndex < angleCount; ++angleIndex) { |
| 522 | *angleList.append() = &angles[angleIndex]; |
| 523 | } |
| 524 | QSort<Angle>(angleList.begin(), angleList.end() - 1); |
| 525 | } |
| 526 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 527 | // Bounds, unlike Rect, does not consider a vertical line to be empty. |
| 528 | struct Bounds : public SkRect { |
| 529 | static bool Intersects(const Bounds& a, const Bounds& b) { |
| 530 | return a.fLeft <= b.fRight && b.fLeft <= a.fRight && |
| 531 | a.fTop <= b.fBottom && b.fTop <= a.fBottom; |
| 532 | } |
| 533 | |
| 534 | bool isEmpty() { |
| 535 | return fLeft > fRight || fTop > fBottom |
| 536 | || fLeft == fRight && fTop == fBottom |
| 537 | || isnan(fLeft) || isnan(fRight) |
| 538 | || isnan(fTop) || isnan(fBottom); |
| 539 | } |
| 540 | |
| 541 | void setCubicBounds(const SkPoint a[4]) { |
| 542 | _Rect dRect; |
| 543 | Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 544 | {a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}}; |
| 545 | dRect.setBounds(cubic); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 546 | set((float) dRect.left, (float) dRect.top, (float) dRect.right, |
| 547 | (float) dRect.bottom); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | void setQuadBounds(const SkPoint a[3]) { |
| 551 | const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 552 | {a[2].fX, a[2].fY}}; |
| 553 | _Rect dRect; |
| 554 | dRect.setBounds(quad); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 555 | set((float) dRect.left, (float) dRect.top, (float) dRect.right, |
| 556 | (float) dRect.bottom); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 557 | } |
| 558 | }; |
| 559 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 560 | struct Span { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 561 | double fT; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 562 | Segment* fOther; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 563 | double fOtherT; // value at fOther[fOtherIndex].fT |
| 564 | int fOtherIndex; // can't be used during intersection |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 565 | int fWinding; // accumulated from contours surrounding this one |
| 566 | // OPTIMIZATION: done needs only 2 bits (values are -1, 0, 1) |
| 567 | int fDone; // set when t to t+fDone is processed |
| 568 | // OPTIMIZATION: done needs only 2 bits (values are -1, 0, 1) |
| 569 | int fCoincident; // -1 start of coincidence, 0 no coincidence, 1 end |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 570 | }; |
| 571 | |
| 572 | class Segment { |
| 573 | public: |
| 574 | Segment() { |
| 575 | #if DEBUG_DUMP |
| 576 | fID = ++gSegmentID; |
| 577 | #endif |
| 578 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 579 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 580 | void addAngle(SkTDArray<Angle>& angles, int start, int end, |
| 581 | bool coincident) const { |
| 582 | SkASSERT(start != end); |
| 583 | SkPoint edge[4]; |
| 584 | (*SegmentSubDivide[fVerb])(fPts, fTs[start].fT, fTs[end].fT, edge); |
| 585 | Angle* angle = angles.append(); |
| 586 | angle->set(edge, fVerb, this, start, end, coincident); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 587 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 588 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 589 | void addCubic(const SkPoint pts[4]) { |
| 590 | init(pts, SkPath::kCubic_Verb); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 591 | fBounds.setCubicBounds(pts); |
| 592 | } |
| 593 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 594 | void addLine(const SkPoint pts[2]) { |
| 595 | init(pts, SkPath::kLine_Verb); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 596 | fBounds.set(pts, 2); |
| 597 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 598 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 599 | // add 2 to edge or out of range values to get T extremes |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 600 | void addOtherT(int index, double otherT, int otherIndex) { |
| 601 | Span& span = fTs[index]; |
| 602 | span.fOtherT = otherT; |
| 603 | span.fOtherIndex = otherIndex; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 604 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 605 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 606 | void addQuad(const SkPoint pts[3]) { |
| 607 | init(pts, SkPath::kQuad_Verb); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 608 | fBounds.setQuadBounds(pts); |
| 609 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 610 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 611 | int addT(double newT, Segment& other, int coincident) { |
| 612 | // FIXME: in the pathological case where there is a ton of intercepts, |
| 613 | // binary search? |
| 614 | int insertedAt = -1; |
| 615 | Span* span; |
| 616 | size_t tCount = fTs.count(); |
| 617 | double delta; |
| 618 | for (size_t idx2 = 0; idx2 < tCount; ++idx2) { |
| 619 | // OPTIMIZATION: if there are three or more identical Ts, then |
| 620 | // the fourth and following could be further insertion-sorted so |
| 621 | // that all the edges are clockwise or counterclockwise. |
| 622 | // This could later limit segment tests to the two adjacent |
| 623 | // neighbors, although it doesn't help with determining which |
| 624 | // circular direction to go in. |
| 625 | if (newT <= fTs[idx2].fT) { |
| 626 | insertedAt = idx2; |
| 627 | span = fTs.insert(idx2); |
| 628 | goto finish; |
| 629 | } |
| 630 | } |
| 631 | insertedAt = tCount; |
| 632 | span = fTs.append(); |
| 633 | finish: |
| 634 | span->fT = newT; |
| 635 | span->fOther = &other; |
| 636 | span->fWinding = 1; |
| 637 | span->fDone = 0; |
| 638 | span->fCoincident = coincident; |
| 639 | fCoincident |= coincident; |
| 640 | return insertedAt; |
| 641 | } |
| 642 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 643 | void addTwoAngles(int start, int end, const SkPoint& endLoc, |
| 644 | const Span* endSpan, bool startCo, SkTDArray<Angle>& angles) const { |
| 645 | // add edge leading into junction |
| 646 | addAngle(angles, end, start, startCo); |
| 647 | // add edge leading away from junction |
| 648 | bool coincident; |
| 649 | int step = start < end ? 1 : -1; |
| 650 | int tIndex = nextSpan(end, step, endLoc, endSpan, NULL, coincident); |
| 651 | if (tIndex >= 0) { |
| 652 | lastSpan(tIndex, step, endLoc, endSpan, coincident); |
| 653 | addAngle(angles, end, tIndex, coincident); |
| 654 | } |
| 655 | } |
| 656 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 657 | const Bounds& bounds() const { |
| 658 | return fBounds; |
| 659 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 660 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 661 | void buildAngles(int index, int last, int step, const SkPoint& loc, |
| 662 | SkTDArray<Angle>& angles) const { |
| 663 | SkASSERT(index - last != 0); |
| 664 | SkASSERT((index - last < 0) ^ (step < 0)); |
| 665 | int end = last + step; |
| 666 | do { |
| 667 | Span* span = &fTs[index]; |
| 668 | Segment* other = span->fOther; |
| 669 | if (other->fDone) { |
| 670 | continue; |
| 671 | } |
| 672 | // if there is only one live crossing, and no coincidence, continue |
| 673 | // in the same direction |
| 674 | // if there is coincidence, the only choice may be to reverse direction |
| 675 | // find edge on either side of intersection |
| 676 | int oIndex = span->fOtherIndex; |
| 677 | Span* otherSpan = &other->fTs[oIndex]; |
| 678 | SkASSERT(otherSpan->fOther == this); |
| 679 | // if done == -1, prior span has already been processed |
| 680 | bool otherCo; |
| 681 | int localStep = step; |
| 682 | int next = other->nextSpan(oIndex, localStep, loc, otherSpan, |
| 683 | NULL, otherCo); |
| 684 | if (next < 0) { |
| 685 | localStep = -step; |
| 686 | next = other->nextSpan(oIndex, localStep, loc, otherSpan, |
| 687 | NULL, otherCo); |
| 688 | } |
| 689 | other->lastSpan(next, localStep, loc, otherSpan, otherCo); |
| 690 | // add candidate into and away from junction |
| 691 | other->addTwoAngles(next, oIndex, loc, span, otherCo, angles); |
| 692 | |
| 693 | } while ((index += step) != end); |
| 694 | } |
| 695 | |
| 696 | // figure out if the segment's ascending T goes clockwise or not |
| 697 | // not enough context to write this as shown |
| 698 | // instead, add all segments meeting at the top |
| 699 | // sort them using buildAngleList |
| 700 | // find the first in the sort |
| 701 | // see if ascendingT goes to top |
| 702 | bool clockwise(int tIndex) const { |
| 703 | SkASSERT(0); // incomplete |
| 704 | return false; |
| 705 | } |
| 706 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 707 | bool done() const { |
| 708 | return fDone; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 709 | } |
| 710 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 711 | int findCoincidentEnd(int start) const { |
| 712 | int tCount = fTs.count(); |
| 713 | SkASSERT(start < tCount); |
| 714 | const Span& span = fTs[start]; |
| 715 | SkASSERT(span.fCoincident); |
| 716 | for (int index = start + 1; index < tCount; ++index) { |
| 717 | const Span& match = fTs[index]; |
| 718 | if (match.fOther == span.fOther) { |
| 719 | SkASSERT(match.fCoincident); |
| 720 | return index; |
| 721 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 722 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 723 | SkASSERT(0); // should never get here |
| 724 | return -1; |
| 725 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 726 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 727 | // start is the index of the beginning T of this edge |
| 728 | // it is guaranteed to have an end which describes a non-zero length (?) |
| 729 | // winding -1 means ccw, 1 means cw |
| 730 | // step is in/out -1 or 1 |
| 731 | // spanIndex is returned |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 732 | Segment* findNext(int start, int winding, int& step, int& spanIndex) const { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 733 | SkASSERT(step == 1 || step == -1); |
| 734 | int count = fTs.count(); |
| 735 | SkASSERT(step > 0 ? start < count - 1 : start > 0); |
| 736 | Span* startSpan = &fTs[start]; |
| 737 | // FIXME: |
| 738 | // since Ts can be stepped either way, done markers must be careful |
| 739 | // not to assume that segment was only ascending in T. This shouldn't |
| 740 | // be a problem unless pathologically a segment can be partially |
| 741 | // ascending and partially descending -- maybe quads/cubic can do this? |
| 742 | startSpan->fDone = step; |
| 743 | SkPoint startLoc; // OPTIMIZATION: store this in the t span? |
| 744 | xyAtT(startSpan->fT, &startLoc); |
| 745 | SkPoint endLoc; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 746 | bool startCo; |
| 747 | int end = nextSpan(start, step, startLoc, startSpan, &endLoc, startCo); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 748 | |
| 749 | // if we hit the end looking for span end, is that always an error? |
| 750 | SkASSERT(step > 0 ? end + 1 < count : end - 1 >= 0); |
| 751 | |
| 752 | // preflight for coincidence -- if present, it may change winding |
| 753 | // considerations and whether reversed edges can be followed |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 754 | int last = lastSpan(end, step, startLoc, startSpan, startCo); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 755 | |
| 756 | // Discard opposing direction candidates if no coincidence was found. |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 757 | Span* endSpan = &fTs[end]; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 758 | int candidateCount = abs(last - end); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 759 | Segment* other; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 760 | if (candidateCount == 1) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 761 | SkASSERT(!startCo); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 762 | // move in winding direction until edge in correct direction |
| 763 | // balance wrong direction edges before finding correct one |
| 764 | // this requres that the intersection is angularly sorted |
| 765 | // for a single intersection, special case -- choose the opposite |
| 766 | // edge that steps the same |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 767 | other = endSpan->fOther; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 768 | SkASSERT(!other->fDone); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 769 | spanIndex = endSpan->fOtherIndex; |
| 770 | SkASSERT(step < 0 ? spanIndex > 0 |
| 771 | : spanIndex < other->fTs.count() - 1); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 772 | return other; |
| 773 | } |
| 774 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 775 | // more than one viable candidate -- measure angles to find best |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 776 | SkTDArray<Angle> angles; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 777 | SkASSERT(end - start != 0); |
| 778 | SkASSERT((end - start < 0) ^ (step < 0)); |
| 779 | addTwoAngles(start, end, endLoc, endSpan, startCo, angles); |
| 780 | buildAngles(end, last, step, endLoc, angles); |
| 781 | SkTDArray<Angle*> sorted; |
| 782 | sortAngles(angles, sorted); |
| 783 | // find the starting edge |
| 784 | int startIndex = -1; |
| 785 | int angleCount = angles.count(); |
| 786 | int angleIndex; |
| 787 | const Angle* angle; |
| 788 | for (angleIndex = 0; angleIndex < angleCount; ++angleIndex) { |
| 789 | angle = sorted[angleIndex]; |
| 790 | if (angle->segment() == this && angle->start() == end && |
| 791 | angle->end() == start) { |
| 792 | startIndex = angleIndex; |
| 793 | break; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 794 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 795 | } |
| 796 | SkASSERT(startIndex >= 0); |
| 797 | winding += angle->sign(); |
| 798 | int nextIndex = startIndex; |
| 799 | const Angle* nextAngle; |
| 800 | do { |
| 801 | if (++nextIndex == angleCount) { |
| 802 | nextIndex = 0; |
| 803 | } |
| 804 | SkASSERT(nextIndex != startIndex); // should never wrap around |
| 805 | nextAngle = sorted[nextIndex]; |
| 806 | // OPTIMIZATION: Figure out all connections, given the initial |
| 807 | // winding info (e.g., accumulate winding in span for reuse) |
| 808 | winding -= nextAngle->sign(); |
| 809 | } while (winding); |
| 810 | // FIXME: get rid of cast |
| 811 | return const_cast<Segment*>(nextAngle->segment()); |
| 812 | |
| 813 | // so the span needs to contain the pairing info found here |
| 814 | // this should include the winding computed for the edge, and |
| 815 | // what edge it connects to, and whether it is discarded |
| 816 | // (maybe discarded == abs(winding) > 1) ? |
| 817 | // only need derivatives for duration of sorting, add a new struct |
| 818 | // for pairings, remove extra spans that have zero length and |
| 819 | // reference an unused other |
| 820 | // for coincident, the last span on the other may be marked done |
| 821 | // (always?) |
| 822 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 823 | // if loop is exhausted, contour may be closed. |
| 824 | // FIXME: pass in close point so we can check for closure |
| 825 | |
| 826 | // given a segment, and a sense of where 'inside' is, return the next |
| 827 | // segment. If this segment has an intersection, or ends in multiple |
| 828 | // segments, find the mate that continues the outside. |
| 829 | // note that if there are multiples, but no coincidence, we can limit |
| 830 | // choices to connections in the correct direction |
| 831 | |
| 832 | // mark found segments as done |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | void findTooCloseToCall(int winding) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 836 | int count = fTs.count(); |
| 837 | if (count < 3) { // require t=0, x, 1 at minimum |
| 838 | return; |
| 839 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 840 | int matchIndex = 0; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 841 | int moCount; |
| 842 | Span* match; |
| 843 | Segment* mOther; |
| 844 | do { |
| 845 | match = &fTs[matchIndex]; |
| 846 | mOther = match->fOther; |
| 847 | moCount = mOther->fTs.count(); |
| 848 | } while (moCount >= 3 || ++matchIndex < count - 1); // require t=0, x, 1 at minimum |
| 849 | SkPoint matchPt; |
| 850 | // OPTIMIZATION: defer matchPt until qualifying toCount is found? |
| 851 | xyAtT(match->fT, &matchPt); |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 852 | // look for a pair of nearby T values that map to the same (x,y) value |
| 853 | // if found, see if the pair of other segments share a common point. If |
| 854 | // so, the span from here to there is coincident. |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 855 | for (int index = matchIndex + 1; index < count; ++index) { |
| 856 | Span* test = &fTs[index]; |
| 857 | Segment* tOther = test->fOther; |
| 858 | int toCount = tOther->fTs.count(); |
| 859 | if (toCount < 3) { // require t=0, x, 1 at minimum |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 860 | continue; |
| 861 | } |
| 862 | SkPoint testPt; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 863 | xyAtT(test->fT, &testPt); |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 864 | if (matchPt != testPt) { |
| 865 | matchIndex = index; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 866 | moCount = toCount; |
| 867 | match = test; |
| 868 | mOther = tOther; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 869 | matchPt = testPt; |
| 870 | continue; |
| 871 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 872 | int moStart = -1; // FIXME: initialization is debugging only |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 873 | for (int moIndex = 0; moIndex < moCount; ++moIndex) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 874 | Span& moSpan = mOther->fTs[moIndex]; |
| 875 | if (moSpan.fOther == this) { |
| 876 | if (moSpan.fOtherT == match->fT) { |
| 877 | moStart = moIndex; |
| 878 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 879 | continue; |
| 880 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 881 | if (moSpan.fOther != tOther) { |
| 882 | continue; |
| 883 | } |
| 884 | int toStart = -1; |
| 885 | int toIndex; // FIXME: initialization is debugging only |
| 886 | bool found = false; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 887 | for (toIndex = 0; toIndex < toCount; ++toIndex) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 888 | Span& toSpan = tOther->fTs[toIndex]; |
| 889 | if (toSpan.fOther == this) { |
| 890 | if (toSpan.fOtherT == test->fT) { |
| 891 | toStart = toIndex; |
| 892 | } |
| 893 | continue; |
| 894 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 895 | if (toSpan.fOther == mOther && toSpan.fOtherT == moSpan.fT) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 896 | found = true; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 897 | break; |
| 898 | } |
| 899 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 900 | if (!found) { |
| 901 | continue; |
| 902 | } |
| 903 | SkASSERT(moStart >= 0); |
| 904 | SkASSERT(toStart >= 0); |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 905 | // test to see if the segment between there and here is linear |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 906 | if (!mOther->isLinear(moStart, moIndex) |
| 907 | || !tOther->isLinear(toStart, toIndex)) { |
| 908 | continue; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 909 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 910 | mOther->fTs[moStart].fCoincident = -1; |
| 911 | tOther->fTs[toStart].fCoincident = -1; |
| 912 | mOther->fTs[moIndex].fCoincident = 1; |
| 913 | tOther->fTs[toIndex].fCoincident = 1; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 914 | } |
| 915 | nextStart: |
| 916 | ; |
| 917 | } |
| 918 | } |
| 919 | |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 920 | // find the adjacent T that is leftmost, with a point != base |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 921 | int findLefty(int tIndex, const SkPoint& base) const { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 922 | int bestTIndex = -1; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 923 | SkPoint test; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 924 | SkScalar bestX = FLT_MAX; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 925 | int testTIndex = tIndex; |
| 926 | while (--testTIndex >= 0) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 927 | xyAtT(fTs[testTIndex].fT, &test); |
| 928 | if (test == base) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 929 | continue; |
| 930 | } |
| 931 | bestX = test.fX; |
| 932 | bestTIndex = testTIndex; |
| 933 | break; |
| 934 | } |
| 935 | int count = fTs.count(); |
| 936 | testTIndex = tIndex; |
| 937 | while (++testTIndex < count) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 938 | xyAtT(fTs[testTIndex].fT, &test); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 939 | if (test == base) { |
| 940 | continue; |
| 941 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 942 | if (bestX > test.fX) { |
| 943 | bestTIndex = testTIndex; |
| 944 | } |
| 945 | break; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 946 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 947 | SkASSERT(bestTIndex != -1); |
| 948 | return bestTIndex; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 949 | } |
| 950 | |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 951 | // OPTIMIZATION : for a pair of lines, can we compute points at T (cached) |
| 952 | // and use more concise logic like the old edge walker code? |
| 953 | // FIXME: this needs to deal with coincident edges |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 954 | const Segment* findTop(int& tIndex, int& direction) const { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 955 | // iterate through T intersections and return topmost |
| 956 | // topmost tangent from y-min to first pt is closer to horizontal |
| 957 | int firstT = 0; |
| 958 | int lastT = 0; |
| 959 | SkScalar topY = fPts[0].fY; |
| 960 | int count = fTs.count(); |
| 961 | int index; |
| 962 | for (index = 1; index < count; ++index) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 963 | const Span& span = fTs[index]; |
| 964 | double t = span.fT; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 965 | SkScalar yIntercept = t == 1 ? fPts[fVerb].fY : yAtT(t); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 966 | if (topY > yIntercept) { |
| 967 | topY = yIntercept; |
| 968 | firstT = lastT = index; |
| 969 | } else if (topY == yIntercept) { |
| 970 | lastT = index; |
| 971 | } |
| 972 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 973 | // if there's only a pair of segments, go with the endpoint chosen above |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 974 | if (firstT == lastT) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 975 | tIndex = firstT; |
| 976 | return this; |
| 977 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 978 | // sort the edges to find the leftmost |
| 979 | SkPoint startLoc; // OPTIMIZATION: store this in the t span? |
| 980 | const Span* startSpan = &fTs[firstT]; |
| 981 | xyAtT(startSpan->fT, &startLoc); |
| 982 | SkPoint endLoc; |
| 983 | bool nextCo; |
| 984 | int end = nextSpan(firstT, 1, startLoc, startSpan, &endLoc, nextCo); |
| 985 | if (end == -1) { |
| 986 | end = nextSpan(firstT, -1, startLoc, startSpan, &endLoc, nextCo); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 987 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 988 | // if the topmost T is not on end, or is three-way or more, find left |
| 989 | // look for left-ness from tLeft to firstT (matching y of other) |
| 990 | SkTDArray<Angle> angles; |
| 991 | SkASSERT(firstT - end != 0); |
| 992 | addTwoAngles(end, firstT, endLoc, &fTs[firstT], nextCo, angles); |
| 993 | buildAngles(firstT, lastT, 1, startLoc, angles); |
| 994 | SkTDArray<Angle*> sorted; |
| 995 | sortAngles(angles, sorted); |
| 996 | const Segment* leftSegment = sorted[0]->segment(); |
| 997 | tIndex = sorted[0]->end(); |
| 998 | direction = sorted[0]->start() - tIndex; |
| 999 | SkASSERT(direction); |
| 1000 | direction = direction < 0 ? -1 : 1; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1001 | return leftSegment; |
| 1002 | } |
| 1003 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1004 | // FIXME: not crazy about this |
| 1005 | // when the intersections are performed, the other index is into an |
| 1006 | // incomplete array. as the array grows, the indices become incorrect |
| 1007 | // while the following fixes the indices up again, it isn't smart about |
| 1008 | // skipping segments whose indices are already correct |
| 1009 | // assuming we leave the code that wrote the index in the first place |
| 1010 | void fixOtherTIndex() { |
| 1011 | int iCount = fTs.count(); |
| 1012 | for (int i = 0; i < iCount; ++i) { |
| 1013 | Span& iSpan = fTs[i]; |
| 1014 | double oT = iSpan.fOtherT; |
| 1015 | Segment* other = iSpan.fOther; |
| 1016 | int oCount = other->fTs.count(); |
| 1017 | for (int o = 0; o < oCount; ++o) { |
| 1018 | Span& oSpan = other->fTs[o]; |
| 1019 | if (oT == oSpan.fT && this == oSpan.fOther) { |
| 1020 | iSpan.fOtherIndex = o; |
| 1021 | } |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | void init(const SkPoint pts[], SkPath::Verb verb) { |
| 1027 | fPts = pts; |
| 1028 | fVerb = verb; |
| 1029 | fDone = false; |
| 1030 | fCoincident = 0; |
| 1031 | } |
| 1032 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1033 | bool intersected() const { |
| 1034 | return fTs.count() > 0; |
| 1035 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1036 | |
| 1037 | bool isLinear(int start, int end) const { |
| 1038 | if (fVerb == SkPath::kLine_Verb) { |
| 1039 | return true; |
| 1040 | } |
| 1041 | if (fVerb == SkPath::kQuad_Verb) { |
| 1042 | SkPoint qPart[3]; |
| 1043 | QuadSubDivide(fPts, fTs[start].fT, fTs[end].fT, qPart); |
| 1044 | return QuadIsLinear(qPart); |
| 1045 | } else { |
| 1046 | SkASSERT(fVerb == SkPath::kCubic_Verb); |
| 1047 | SkPoint cPart[4]; |
| 1048 | CubicSubDivide(fPts, fTs[start].fT, fTs[end].fT, cPart); |
| 1049 | return CubicIsLinear(cPart); |
| 1050 | } |
| 1051 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1052 | |
| 1053 | bool isHorizontal() const { |
| 1054 | return fBounds.fTop == fBounds.fBottom; |
| 1055 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1056 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1057 | bool isVertical() const { |
| 1058 | return fBounds.fLeft == fBounds.fRight; |
| 1059 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1060 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1061 | int lastSpan(int end, int step, const SkPoint& startLoc, |
| 1062 | const Span* startSpan, bool& coincident) const { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1063 | int last = end; |
| 1064 | int count = fTs.count(); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1065 | SkPoint lastLoc; |
| 1066 | do { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1067 | end = last; |
| 1068 | if (fTs[end].fCoincident == -step) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1069 | coincident = true; |
| 1070 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1071 | if (step > 0 ? ++last >= count : --last < 0) { |
| 1072 | return end; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1073 | } |
caryclark@google.com | fcd4f3e | 2012-05-07 21:09:32 +0000 | [diff] [blame] | 1074 | const Span& lastSpan = fTs[last]; |
| 1075 | if (lastSpan.fDone == -step) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1076 | return end; |
caryclark@google.com | fcd4f3e | 2012-05-07 21:09:32 +0000 | [diff] [blame] | 1077 | } |
| 1078 | if (lastSpan.fT == startSpan->fT) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1079 | continue; |
| 1080 | } |
caryclark@google.com | fcd4f3e | 2012-05-07 21:09:32 +0000 | [diff] [blame] | 1081 | xyAtT(lastSpan.fT, &lastLoc); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1082 | } while (startLoc == lastLoc); |
| 1083 | return end; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1086 | SkScalar leftMost(int start, int end) const { |
| 1087 | return (*SegmentLeftMost[fVerb])(fPts, fTs[start].fT, fTs[end].fT); |
| 1088 | } |
| 1089 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1090 | int nextSpan(int from, int step, const SkPoint& fromLoc, |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1091 | const Span* fromSpan, SkPoint* toLoc, bool& coincident) const { |
| 1092 | coincident = false; |
| 1093 | if (fDone) { |
| 1094 | return -1; |
| 1095 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1096 | int count = fTs.count(); |
| 1097 | int to = from; |
| 1098 | while (step > 0 ? ++to < count : --to >= 0) { |
| 1099 | Span* span = &fTs[to]; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1100 | if (span->fCoincident == step) { |
| 1101 | coincident = true; |
| 1102 | } |
| 1103 | if (fromSpan->fT == span->fT) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1104 | continue; |
| 1105 | } |
| 1106 | SkPoint loc; |
| 1107 | xyAtT(span->fT, &loc); |
| 1108 | if (fromLoc == loc) { |
| 1109 | continue; |
| 1110 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1111 | if (span->fDone == -step) { |
| 1112 | return -1; |
| 1113 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1114 | if (toLoc) { |
| 1115 | *toLoc = loc; |
| 1116 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1117 | return to; |
| 1118 | } |
| 1119 | return -1; |
| 1120 | } |
| 1121 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1122 | const SkPoint* pts() const { |
| 1123 | return fPts; |
| 1124 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1125 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1126 | void reset() { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1127 | init(NULL, (SkPath::Verb) -1); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1128 | fBounds.set(SK_ScalarMax, SK_ScalarMax, SK_ScalarMax, SK_ScalarMax); |
| 1129 | fTs.reset(); |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1132 | // OPTIMIZATION: remove this function if it's never called |
| 1133 | double t(int tIndex) const { |
| 1134 | return fTs[tIndex].fT; |
| 1135 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1136 | |
| 1137 | void updatePts(const SkPoint pts[]) { |
| 1138 | fPts = pts; |
| 1139 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1140 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1141 | SkPath::Verb verb() const { |
| 1142 | return fVerb; |
| 1143 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1144 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1145 | SkScalar xAtT(double t) const { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1146 | SkASSERT(t >= 0 && t <= 1); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1147 | return (*SegmentXAtT[fVerb])(fPts, t); |
| 1148 | } |
| 1149 | |
| 1150 | void xyAtT(double t, SkPoint* pt) const { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1151 | SkASSERT(t >= 0 && t <= 1); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1152 | (*SegmentXYAtT[fVerb])(fPts, t, pt); |
| 1153 | } |
| 1154 | |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1155 | SkScalar yAtT(double t) const { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1156 | SkASSERT(t >= 0 && t <= 1); |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1157 | return (*SegmentYAtT[fVerb])(fPts, t); |
| 1158 | } |
| 1159 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1160 | #if DEBUG_DUMP |
| 1161 | void dump() const { |
| 1162 | const char className[] = "Segment"; |
| 1163 | const int tab = 4; |
| 1164 | for (int i = 0; i < fTs.count(); ++i) { |
| 1165 | SkPoint out; |
| 1166 | (*SegmentXYAtT[fVerb])(fPts, t(i), &out); |
| 1167 | SkDebugf("%*s [%d] %s.fTs[%d]=%1.9g (%1.9g,%1.9g) other=%d" |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1168 | " otherT=%1.9g winding=%d\n", |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1169 | tab + sizeof(className), className, fID, |
| 1170 | kLVerbStr[fVerb], i, fTs[i].fT, out.fX, out.fY, |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1171 | fTs[i].fOther->fID, fTs[i].fOtherT, fTs[i].fWinding); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1172 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1173 | SkDebugf("%*s [%d] fBounds=(l:%1.9g, t:%1.9g r:%1.9g, b:%1.9g)", |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1174 | tab + sizeof(className), className, fID, |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1175 | fBounds.fLeft, fBounds.fTop, fBounds.fRight, fBounds.fBottom); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1176 | } |
| 1177 | #endif |
| 1178 | |
| 1179 | private: |
| 1180 | const SkPoint* fPts; |
| 1181 | SkPath::Verb fVerb; |
| 1182 | Bounds fBounds; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1183 | SkTDArray<Span> fTs; // two or more (always includes t=0 t=1) |
| 1184 | // FIXME: coincident only needs two bits (-1, 0, 1) |
| 1185 | int fCoincident; // non-zero if some coincident span inside |
| 1186 | bool fDone; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1187 | #if DEBUG_DUMP |
| 1188 | int fID; |
| 1189 | #endif |
| 1190 | }; |
| 1191 | |
| 1192 | class Contour { |
| 1193 | public: |
| 1194 | Contour() { |
| 1195 | reset(); |
| 1196 | #if DEBUG_DUMP |
| 1197 | fID = ++gContourID; |
| 1198 | #endif |
| 1199 | } |
| 1200 | |
| 1201 | bool operator<(const Contour& rh) const { |
| 1202 | return fBounds.fTop == rh.fBounds.fTop |
| 1203 | ? fBounds.fLeft < rh.fBounds.fLeft |
| 1204 | : fBounds.fTop < rh.fBounds.fTop; |
| 1205 | } |
| 1206 | |
| 1207 | void addCubic(const SkPoint pts[4]) { |
| 1208 | fSegments.push_back().addCubic(pts); |
| 1209 | fContainsCurves = true; |
| 1210 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1211 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1212 | int addLine(const SkPoint pts[2]) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1213 | fSegments.push_back().addLine(pts); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1214 | return fSegments.count(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1215 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1216 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1217 | int addQuad(const SkPoint pts[3]) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1218 | fSegments.push_back().addQuad(pts); |
| 1219 | fContainsCurves = true; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1220 | return fSegments.count(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1221 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1222 | |
| 1223 | const Bounds& bounds() const { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1224 | return fBounds; |
| 1225 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1226 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1227 | void complete() { |
| 1228 | setBounds(); |
| 1229 | fContainsIntercepts = false; |
| 1230 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1231 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1232 | void containsIntercepts() { |
| 1233 | fContainsIntercepts = true; |
| 1234 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1235 | |
| 1236 | void findTooCloseToCall(int winding) { |
| 1237 | int segmentCount = fSegments.count(); |
| 1238 | for (int sIndex = 0; sIndex < segmentCount; ++sIndex) { |
| 1239 | fSegments[sIndex].findTooCloseToCall(winding); |
| 1240 | } |
| 1241 | } |
| 1242 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1243 | void fixOtherTIndex() { |
| 1244 | int segmentCount = fSegments.count(); |
| 1245 | for (int sIndex = 0; sIndex < segmentCount; ++sIndex) { |
| 1246 | fSegments[sIndex].fixOtherTIndex(); |
| 1247 | } |
| 1248 | } |
| 1249 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1250 | void reset() { |
| 1251 | fSegments.reset(); |
| 1252 | fBounds.set(SK_ScalarMax, SK_ScalarMax, SK_ScalarMax, SK_ScalarMax); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1253 | fContainsCurves = fContainsIntercepts = false; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1254 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1255 | |
| 1256 | // OPTIMIZATION: feel pretty uneasy about this. It seems like once again |
| 1257 | // we need to sort and walk edges in y, but that on the surface opens the |
| 1258 | // same can of worms as before. But then, this is a rough sort based on |
| 1259 | // segments' top, and not a true sort, so it could be ameniable to regular |
| 1260 | // sorting instead of linear searching. Still feel like I'm missing something |
| 1261 | Segment* topSegment() { |
| 1262 | int segmentCount = fSegments.count(); |
| 1263 | SkASSERT(segmentCount > 0); |
| 1264 | int best = -1; |
| 1265 | Segment* bestSegment = NULL; |
| 1266 | while (++best < segmentCount) { |
| 1267 | Segment* testSegment = &fSegments[best]; |
| 1268 | if (testSegment->done()) { |
| 1269 | continue; |
| 1270 | } |
| 1271 | bestSegment = testSegment; |
| 1272 | break; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1273 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1274 | if (!bestSegment) { |
| 1275 | return NULL; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1276 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1277 | SkScalar bestTop = bestSegment->bounds().fTop; |
| 1278 | for (int test = best + 1; test < segmentCount; ++test) { |
| 1279 | Segment* testSegment = &fSegments[test]; |
| 1280 | if (testSegment->done()) { |
| 1281 | continue; |
| 1282 | } |
| 1283 | SkScalar testTop = testSegment->bounds().fTop; |
| 1284 | if (bestTop > testTop) { |
| 1285 | bestTop = testTop; |
| 1286 | bestSegment = testSegment; |
| 1287 | } |
| 1288 | } |
| 1289 | return bestSegment; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | #if DEBUG_DUMP |
| 1293 | void dump() { |
| 1294 | int i; |
| 1295 | const char className[] = "Contour"; |
| 1296 | const int tab = 4; |
| 1297 | SkDebugf("%s %p (contour=%d)\n", className, this, fID); |
| 1298 | for (i = 0; i < fSegments.count(); ++i) { |
| 1299 | SkDebugf("%*s.fSegments[%d]:\n", tab + sizeof(className), |
| 1300 | className, i); |
| 1301 | fSegments[i].dump(); |
| 1302 | } |
| 1303 | SkDebugf("%*s.fBounds=(l:%1.9g, t:%1.9g r:%1.9g, b:%1.9g)\n", |
| 1304 | tab + sizeof(className), className, |
| 1305 | fBounds.fLeft, fBounds.fTop, |
| 1306 | fBounds.fRight, fBounds.fBottom); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1307 | SkDebugf("%*s.fContainsIntercepts=%d\n", tab + sizeof(className), |
| 1308 | className, fContainsIntercepts); |
| 1309 | SkDebugf("%*s.fContainsCurves=%d\n", tab + sizeof(className), |
| 1310 | className, fContainsCurves); |
| 1311 | } |
| 1312 | #endif |
| 1313 | |
| 1314 | protected: |
| 1315 | void setBounds() { |
| 1316 | int count = fSegments.count(); |
| 1317 | if (count == 0) { |
| 1318 | SkDebugf("%s empty contour\n", __FUNCTION__); |
| 1319 | SkASSERT(0); |
| 1320 | // FIXME: delete empty contour? |
| 1321 | return; |
| 1322 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1323 | fBounds = fSegments.front().bounds(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1324 | for (int index = 1; index < count; ++index) { |
| 1325 | fBounds.growToInclude(fSegments[index].bounds()); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1326 | } |
| 1327 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1328 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1329 | public: |
| 1330 | SkTArray<Segment> fSegments; // not worth accessor functions? |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1331 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1332 | private: |
| 1333 | Bounds fBounds; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1334 | bool fContainsIntercepts; |
| 1335 | bool fContainsCurves; |
| 1336 | #if DEBUG_DUMP |
| 1337 | int fID; |
| 1338 | #endif |
| 1339 | }; |
| 1340 | |
| 1341 | class EdgeBuilder { |
| 1342 | public: |
| 1343 | |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1344 | EdgeBuilder(const SkPath& path, SkTArray<Contour>& contours) |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1345 | : fPath(path) |
| 1346 | , fCurrentContour(NULL) |
| 1347 | , fContours(contours) |
| 1348 | { |
| 1349 | #if DEBUG_DUMP |
| 1350 | gContourID = 0; |
| 1351 | gSegmentID = 0; |
| 1352 | #endif |
| 1353 | walk(); |
| 1354 | } |
| 1355 | |
| 1356 | protected: |
| 1357 | |
| 1358 | void complete() { |
| 1359 | if (fCurrentContour && fCurrentContour->fSegments.count()) { |
| 1360 | fCurrentContour->complete(); |
| 1361 | fCurrentContour = NULL; |
| 1362 | } |
| 1363 | } |
| 1364 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1365 | void walk() { |
| 1366 | // FIXME:remove once we can access path pts directly |
| 1367 | SkPath::RawIter iter(fPath); // FIXME: access path directly when allowed |
| 1368 | SkPoint pts[4]; |
| 1369 | SkPath::Verb verb; |
| 1370 | do { |
| 1371 | verb = iter.next(pts); |
| 1372 | *fPathVerbs.append() = verb; |
| 1373 | if (verb == SkPath::kMove_Verb) { |
| 1374 | *fPathPts.append() = pts[0]; |
| 1375 | } else if (verb >= SkPath::kLine_Verb && verb <= SkPath::kCubic_Verb) { |
| 1376 | fPathPts.append(verb, &pts[1]); |
| 1377 | } |
| 1378 | } while (verb != SkPath::kDone_Verb); |
| 1379 | // FIXME: end of section to remove once path pts are accessed directly |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1380 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1381 | SkPath::Verb reducedVerb; |
| 1382 | uint8_t* verbPtr = fPathVerbs.begin(); |
| 1383 | const SkPoint* pointsPtr = fPathPts.begin(); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1384 | const SkPoint* finalCurveStart = NULL; |
| 1385 | const SkPoint* finalCurveEnd = NULL; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1386 | while ((verb = (SkPath::Verb) *verbPtr++) != SkPath::kDone_Verb) { |
| 1387 | switch (verb) { |
| 1388 | case SkPath::kMove_Verb: |
| 1389 | complete(); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1390 | if (!fCurrentContour) { |
| 1391 | fCurrentContour = fContours.push_back_n(1); |
| 1392 | finalCurveEnd = pointsPtr++; |
| 1393 | *fExtra.append() = -1; // start new contour |
| 1394 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1395 | continue; |
| 1396 | case SkPath::kLine_Verb: |
| 1397 | // skip degenerate points |
| 1398 | if (pointsPtr[-1].fX != pointsPtr[0].fX |
| 1399 | || pointsPtr[-1].fY != pointsPtr[0].fY) { |
| 1400 | fCurrentContour->addLine(&pointsPtr[-1]); |
| 1401 | } |
| 1402 | break; |
| 1403 | case SkPath::kQuad_Verb: |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1404 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1405 | reducedVerb = QuadReduceOrder(&pointsPtr[-1], fReducePts); |
| 1406 | if (reducedVerb == 0) { |
| 1407 | break; // skip degenerate points |
| 1408 | } |
| 1409 | if (reducedVerb == 1) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1410 | *fExtra.append() = |
| 1411 | fCurrentContour->addLine(fReducePts.end() - 2); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1412 | break; |
| 1413 | } |
| 1414 | fCurrentContour->addQuad(&pointsPtr[-1]); |
| 1415 | break; |
| 1416 | case SkPath::kCubic_Verb: |
| 1417 | reducedVerb = CubicReduceOrder(&pointsPtr[-1], fReducePts); |
| 1418 | if (reducedVerb == 0) { |
| 1419 | break; // skip degenerate points |
| 1420 | } |
| 1421 | if (reducedVerb == 1) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1422 | *fExtra.append() = |
| 1423 | fCurrentContour->addLine(fReducePts.end() - 2); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1424 | break; |
| 1425 | } |
| 1426 | if (reducedVerb == 2) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1427 | *fExtra.append() = |
| 1428 | fCurrentContour->addQuad(fReducePts.end() - 3); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1429 | break; |
| 1430 | } |
| 1431 | fCurrentContour->addCubic(&pointsPtr[-1]); |
| 1432 | break; |
| 1433 | case SkPath::kClose_Verb: |
| 1434 | SkASSERT(fCurrentContour); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1435 | if (finalCurveStart && finalCurveEnd |
| 1436 | && *finalCurveStart != *finalCurveEnd) { |
| 1437 | *fReducePts.append() = *finalCurveStart; |
| 1438 | *fReducePts.append() = *finalCurveEnd; |
| 1439 | *fExtra.append() = |
| 1440 | fCurrentContour->addLine(fReducePts.end() - 2); |
| 1441 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1442 | complete(); |
| 1443 | continue; |
| 1444 | default: |
| 1445 | SkDEBUGFAIL("bad verb"); |
| 1446 | return; |
| 1447 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1448 | finalCurveStart = &pointsPtr[verb - 1]; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1449 | pointsPtr += verb; |
| 1450 | SkASSERT(fCurrentContour); |
| 1451 | } |
| 1452 | complete(); |
| 1453 | if (fCurrentContour && !fCurrentContour->fSegments.count()) { |
| 1454 | fContours.pop_back(); |
| 1455 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1456 | // correct pointers in contours since fReducePts may have moved as it grew |
| 1457 | int cIndex = 0; |
| 1458 | fCurrentContour = &fContours[0]; |
| 1459 | int extraCount = fExtra.count(); |
| 1460 | SkASSERT(fExtra[0] == -1); |
| 1461 | int eIndex = 0; |
| 1462 | int rIndex = 0; |
| 1463 | while (++eIndex < extraCount) { |
| 1464 | int offset = fExtra[eIndex]; |
| 1465 | if (offset < 0) { |
| 1466 | fCurrentContour = &fContours[++cIndex]; |
| 1467 | continue; |
| 1468 | } |
| 1469 | Segment& segment = fCurrentContour->fSegments[offset - 1]; |
| 1470 | segment.updatePts(&fReducePts[rIndex]); |
| 1471 | rIndex += segment.verb() + 1; |
| 1472 | } |
| 1473 | fExtra.reset(); // we're done with this |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | private: |
| 1477 | const SkPath& fPath; |
| 1478 | SkTDArray<SkPoint> fPathPts; // FIXME: point directly to path pts instead |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1479 | SkTDArray<uint8_t> fPathVerbs; // FIXME: remove |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1480 | Contour* fCurrentContour; |
| 1481 | SkTArray<Contour>& fContours; |
| 1482 | SkTDArray<SkPoint> fReducePts; // segments created on the fly |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1483 | SkTDArray<int> fExtra; // -1 marks new contour, > 0 offsets into contour |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1484 | }; |
| 1485 | |
| 1486 | class Work { |
| 1487 | public: |
| 1488 | enum SegmentType { |
| 1489 | kHorizontalLine_Segment = -1, |
| 1490 | kVerticalLine_Segment = 0, |
| 1491 | kLine_Segment = SkPath::kLine_Verb, |
| 1492 | kQuad_Segment = SkPath::kQuad_Verb, |
| 1493 | kCubic_Segment = SkPath::kCubic_Verb, |
| 1494 | }; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1495 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1496 | // FIXME: does it make sense to write otherIndex now if we're going to |
| 1497 | // fix it up later? |
| 1498 | void addOtherT(int index, double otherT, int otherIndex) { |
| 1499 | fContour->fSegments[fIndex].addOtherT(index, otherT, otherIndex); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1500 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1501 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1502 | // Avoid collapsing t values that are close to the same since |
| 1503 | // we walk ts to describe consecutive intersections. Since a pair of ts can |
| 1504 | // be nearly equal, any problems caused by this should be taken care |
| 1505 | // of later. |
| 1506 | // On the edge or out of range values are negative; add 2 to get end |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1507 | int addT(double newT, const Work& other, int coincident) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1508 | fContour->containsIntercepts(); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1509 | return fContour->fSegments[fIndex].addT(newT, |
| 1510 | other.fContour->fSegments[other.fIndex], coincident); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1511 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1512 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1513 | bool advance() { |
| 1514 | return ++fIndex < fLast; |
| 1515 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1516 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1517 | SkScalar bottom() const { |
| 1518 | return bounds().fBottom; |
| 1519 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1520 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1521 | const Bounds& bounds() const { |
| 1522 | return fContour->fSegments[fIndex].bounds(); |
| 1523 | } |
| 1524 | |
| 1525 | const SkPoint* cubic() const { |
| 1526 | return fCubic; |
| 1527 | } |
| 1528 | |
| 1529 | void init(Contour* contour) { |
| 1530 | fContour = contour; |
| 1531 | fIndex = 0; |
| 1532 | fLast = contour->fSegments.count(); |
| 1533 | } |
| 1534 | |
| 1535 | SkScalar left() const { |
| 1536 | return bounds().fLeft; |
| 1537 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1538 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1539 | void promoteToCubic() { |
| 1540 | fCubic[0] = pts()[0]; |
| 1541 | fCubic[2] = pts()[1]; |
| 1542 | fCubic[3] = pts()[2]; |
| 1543 | fCubic[1].fX = (fCubic[0].fX + fCubic[2].fX * 2) / 3; |
| 1544 | fCubic[1].fY = (fCubic[0].fY + fCubic[2].fY * 2) / 3; |
| 1545 | fCubic[2].fX = (fCubic[3].fX + fCubic[2].fX * 2) / 3; |
| 1546 | fCubic[2].fY = (fCubic[3].fY + fCubic[2].fY * 2) / 3; |
| 1547 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1548 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1549 | const SkPoint* pts() const { |
| 1550 | return fContour->fSegments[fIndex].pts(); |
| 1551 | } |
| 1552 | |
| 1553 | SkScalar right() const { |
| 1554 | return bounds().fRight; |
| 1555 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1556 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1557 | ptrdiff_t segmentIndex() const { |
| 1558 | return fIndex; |
| 1559 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1560 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1561 | SegmentType segmentType() const { |
| 1562 | const Segment& segment = fContour->fSegments[fIndex]; |
| 1563 | SegmentType type = (SegmentType) segment.verb(); |
| 1564 | if (type != kLine_Segment) { |
| 1565 | return type; |
| 1566 | } |
| 1567 | if (segment.isHorizontal()) { |
| 1568 | return kHorizontalLine_Segment; |
| 1569 | } |
| 1570 | if (segment.isVertical()) { |
| 1571 | return kVerticalLine_Segment; |
| 1572 | } |
| 1573 | return kLine_Segment; |
| 1574 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1575 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1576 | bool startAfter(const Work& after) { |
| 1577 | fIndex = after.fIndex; |
| 1578 | return advance(); |
| 1579 | } |
| 1580 | |
| 1581 | SkScalar top() const { |
| 1582 | return bounds().fTop; |
| 1583 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1584 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1585 | SkPath::Verb verb() const { |
| 1586 | return fContour->fSegments[fIndex].verb(); |
| 1587 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1588 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1589 | SkScalar x() const { |
| 1590 | return bounds().fLeft; |
| 1591 | } |
| 1592 | |
| 1593 | bool xFlipped() const { |
| 1594 | return x() != pts()[0].fX; |
| 1595 | } |
| 1596 | |
| 1597 | SkScalar y() const { |
| 1598 | return bounds().fTop; |
| 1599 | } |
| 1600 | |
| 1601 | bool yFlipped() const { |
| 1602 | return y() != pts()[0].fX; |
| 1603 | } |
| 1604 | |
| 1605 | protected: |
| 1606 | Contour* fContour; |
| 1607 | SkPoint fCubic[4]; |
| 1608 | int fIndex; |
| 1609 | int fLast; |
| 1610 | }; |
| 1611 | |
| 1612 | static void debugShowLineIntersection(int pts, const Work& wt, |
| 1613 | const Work& wn, const double wtTs[2], const double wnTs[2]) { |
| 1614 | #if DEBUG_ADD_INTERSECTING_TS |
| 1615 | if (!pts) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1616 | SkDebugf("%s no intersect (%1.9g,%1.9g %1.9g,%1.9g) (%1.9g,%1.9g %1.9g,%1.9g)\n", |
| 1617 | __FUNCTION__, wt.pts()[0].fX, wt.pts()[0].fY, |
| 1618 | wt.pts()[1].fX, wt.pts()[1].fY, wn.pts()[0].fX, wn.pts()[0].fY, |
| 1619 | wn.pts()[1].fX, wn.pts()[1].fY); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1620 | return; |
| 1621 | } |
| 1622 | SkPoint wtOutPt, wnOutPt; |
| 1623 | LineXYAtT(wt.pts(), wtTs[0], &wtOutPt); |
| 1624 | LineXYAtT(wn.pts(), wnTs[0], &wnOutPt); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1625 | SkDebugf("%s wtTs[0]=%g (%g,%g, %g,%g) (%g,%g)", |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1626 | __FUNCTION__, |
| 1627 | wtTs[0], wt.pts()[0].fX, wt.pts()[0].fY, |
| 1628 | wt.pts()[1].fX, wt.pts()[1].fY, wtOutPt.fX, wtOutPt.fY); |
| 1629 | if (pts == 2) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1630 | SkDebugf(" wtTs[1]=%g", wtTs[1]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1631 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1632 | SkDebugf(" wnTs[0]=%g (%g,%g, %g,%g) (%g,%g)\n", |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1633 | wnTs[0], wn.pts()[0].fX, wn.pts()[0].fY, |
| 1634 | wn.pts()[1].fX, wn.pts()[1].fY, wnOutPt.fX, wnOutPt.fY); |
| 1635 | if (pts == 2) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1636 | SkDebugf(" wnTs[1]=%g", wnTs[1]); |
| 1637 | SkDebugf("\n"); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1638 | } |
| 1639 | #endif |
| 1640 | } |
| 1641 | |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1642 | static bool addIntersectTs(Contour* test, Contour* next, int winding) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1643 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1644 | if (test != next) { |
| 1645 | if (test->bounds().fBottom < next->bounds().fTop) { |
| 1646 | return false; |
| 1647 | } |
| 1648 | if (!Bounds::Intersects(test->bounds(), next->bounds())) { |
| 1649 | return true; |
| 1650 | } |
| 1651 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1652 | Work wt; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1653 | wt.init(test); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1654 | do { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1655 | Work wn; |
| 1656 | wn.init(next); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1657 | if (test == next && !wn.startAfter(wt)) { |
| 1658 | continue; |
| 1659 | } |
| 1660 | do { |
| 1661 | if (!Bounds::Intersects(wt.bounds(), wn.bounds())) { |
| 1662 | continue; |
| 1663 | } |
| 1664 | int pts; |
| 1665 | Intersections ts; |
| 1666 | bool swap = false; |
| 1667 | switch (wt.segmentType()) { |
| 1668 | case Work::kHorizontalLine_Segment: |
| 1669 | swap = true; |
| 1670 | switch (wn.segmentType()) { |
| 1671 | case Work::kHorizontalLine_Segment: |
| 1672 | case Work::kVerticalLine_Segment: |
| 1673 | case Work::kLine_Segment: { |
| 1674 | pts = HLineIntersect(wn.pts(), wt.left(), |
| 1675 | wt.right(), wt.y(), wt.xFlipped(), ts); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1676 | debugShowLineIntersection(pts, wt, wn, |
| 1677 | ts.fT[1], ts.fT[0]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1678 | break; |
| 1679 | } |
| 1680 | case Work::kQuad_Segment: { |
| 1681 | pts = HQuadIntersect(wn.pts(), wt.left(), |
| 1682 | wt.right(), wt.y(), wt.xFlipped(), ts); |
| 1683 | break; |
| 1684 | } |
| 1685 | case Work::kCubic_Segment: { |
| 1686 | pts = HCubicIntersect(wn.pts(), wt.left(), |
| 1687 | wt.right(), wt.y(), wt.xFlipped(), ts); |
| 1688 | break; |
| 1689 | } |
| 1690 | default: |
| 1691 | SkASSERT(0); |
| 1692 | } |
| 1693 | break; |
| 1694 | case Work::kVerticalLine_Segment: |
| 1695 | swap = true; |
| 1696 | switch (wn.segmentType()) { |
| 1697 | case Work::kHorizontalLine_Segment: |
| 1698 | case Work::kVerticalLine_Segment: |
| 1699 | case Work::kLine_Segment: { |
| 1700 | pts = VLineIntersect(wn.pts(), wt.top(), |
| 1701 | wt.bottom(), wt.x(), wt.yFlipped(), ts); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1702 | debugShowLineIntersection(pts, wt, wn, |
| 1703 | ts.fT[1], ts.fT[0]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1704 | break; |
| 1705 | } |
| 1706 | case Work::kQuad_Segment: { |
| 1707 | pts = VQuadIntersect(wn.pts(), wt.top(), |
| 1708 | wt.bottom(), wt.x(), wt.yFlipped(), ts); |
| 1709 | break; |
| 1710 | } |
| 1711 | case Work::kCubic_Segment: { |
| 1712 | pts = VCubicIntersect(wn.pts(), wt.top(), |
| 1713 | wt.bottom(), wt.x(), wt.yFlipped(), ts); |
| 1714 | break; |
| 1715 | } |
| 1716 | default: |
| 1717 | SkASSERT(0); |
| 1718 | } |
| 1719 | break; |
| 1720 | case Work::kLine_Segment: |
| 1721 | switch (wn.segmentType()) { |
| 1722 | case Work::kHorizontalLine_Segment: |
| 1723 | pts = HLineIntersect(wt.pts(), wn.left(), |
| 1724 | wn.right(), wn.y(), wn.xFlipped(), ts); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1725 | debugShowLineIntersection(pts, wt, wn, |
| 1726 | ts.fT[1], ts.fT[0]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1727 | break; |
| 1728 | case Work::kVerticalLine_Segment: |
| 1729 | pts = VLineIntersect(wt.pts(), wn.top(), |
| 1730 | wn.bottom(), wn.x(), wn.yFlipped(), ts); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1731 | debugShowLineIntersection(pts, wt, wn, |
| 1732 | ts.fT[1], ts.fT[0]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1733 | break; |
| 1734 | case Work::kLine_Segment: { |
| 1735 | pts = LineIntersect(wt.pts(), wn.pts(), ts); |
| 1736 | debugShowLineIntersection(pts, wt, wn, |
| 1737 | ts.fT[1], ts.fT[0]); |
| 1738 | break; |
| 1739 | } |
| 1740 | case Work::kQuad_Segment: { |
| 1741 | swap = true; |
| 1742 | pts = QuadLineIntersect(wn.pts(), wt.pts(), ts); |
| 1743 | break; |
| 1744 | } |
| 1745 | case Work::kCubic_Segment: { |
| 1746 | swap = true; |
| 1747 | pts = CubicLineIntersect(wn.pts(), wt.pts(), ts); |
| 1748 | break; |
| 1749 | } |
| 1750 | default: |
| 1751 | SkASSERT(0); |
| 1752 | } |
| 1753 | break; |
| 1754 | case Work::kQuad_Segment: |
| 1755 | switch (wn.segmentType()) { |
| 1756 | case Work::kHorizontalLine_Segment: |
| 1757 | pts = HQuadIntersect(wt.pts(), wn.left(), |
| 1758 | wn.right(), wn.y(), wn.xFlipped(), ts); |
| 1759 | break; |
| 1760 | case Work::kVerticalLine_Segment: |
| 1761 | pts = VQuadIntersect(wt.pts(), wn.top(), |
| 1762 | wn.bottom(), wn.x(), wn.yFlipped(), ts); |
| 1763 | break; |
| 1764 | case Work::kLine_Segment: { |
| 1765 | pts = QuadLineIntersect(wt.pts(), wn.pts(), ts); |
| 1766 | break; |
| 1767 | } |
| 1768 | case Work::kQuad_Segment: { |
| 1769 | pts = QuadIntersect(wt.pts(), wn.pts(), ts); |
| 1770 | break; |
| 1771 | } |
| 1772 | case Work::kCubic_Segment: { |
| 1773 | wt.promoteToCubic(); |
| 1774 | pts = CubicIntersect(wt.cubic(), wn.pts(), ts); |
| 1775 | break; |
| 1776 | } |
| 1777 | default: |
| 1778 | SkASSERT(0); |
| 1779 | } |
| 1780 | break; |
| 1781 | case Work::kCubic_Segment: |
| 1782 | switch (wn.segmentType()) { |
| 1783 | case Work::kHorizontalLine_Segment: |
| 1784 | pts = HCubicIntersect(wt.pts(), wn.left(), |
| 1785 | wn.right(), wn.y(), wn.xFlipped(), ts); |
| 1786 | break; |
| 1787 | case Work::kVerticalLine_Segment: |
| 1788 | pts = VCubicIntersect(wt.pts(), wn.top(), |
| 1789 | wn.bottom(), wn.x(), wn.yFlipped(), ts); |
| 1790 | break; |
| 1791 | case Work::kLine_Segment: { |
| 1792 | pts = CubicLineIntersect(wt.pts(), wn.pts(), ts); |
| 1793 | break; |
| 1794 | } |
| 1795 | case Work::kQuad_Segment: { |
| 1796 | wn.promoteToCubic(); |
| 1797 | pts = CubicIntersect(wt.pts(), wn.cubic(), ts); |
| 1798 | break; |
| 1799 | } |
| 1800 | case Work::kCubic_Segment: { |
| 1801 | pts = CubicIntersect(wt.pts(), wn.pts(), ts); |
| 1802 | break; |
| 1803 | } |
| 1804 | default: |
| 1805 | SkASSERT(0); |
| 1806 | } |
| 1807 | break; |
| 1808 | default: |
| 1809 | SkASSERT(0); |
| 1810 | } |
| 1811 | // in addition to recording T values, record matching segment |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1812 | int coincident = pts == 2 && wn.segmentType() <= Work::kLine_Segment |
| 1813 | && wt.segmentType() <= Work::kLine_Segment ? -1 :0; |
| 1814 | for (int pt = 0; pt < pts; ++pt) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1815 | SkASSERT(ts.fT[0][pt] >= 0 && ts.fT[0][pt] <= 1); |
| 1816 | SkASSERT(ts.fT[1][pt] >= 0 && ts.fT[1][pt] <= 1); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1817 | int testTAt = wt.addT(ts.fT[swap][pt], wn, coincident); |
| 1818 | int nextTAt = wn.addT(ts.fT[!swap][pt], wt, coincident); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1819 | wt.addOtherT(testTAt, ts.fT[!swap][pt], nextTAt); |
| 1820 | wn.addOtherT(nextTAt, ts.fT[swap][pt], testTAt); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1821 | coincident = -coincident; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1822 | } |
| 1823 | } while (wn.advance()); |
| 1824 | } while (wt.advance()); |
| 1825 | return true; |
| 1826 | } |
| 1827 | |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1828 | // see if coincidence is formed by clipping non-concident segments |
| 1829 | static void coincidenceCheck(SkTDArray<Contour*>& contourList, int winding) { |
| 1830 | int contourCount = contourList.count(); |
| 1831 | for (size_t cIndex = 0; cIndex < contourCount; ++cIndex) { |
| 1832 | Contour* contour = contourList[cIndex]; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1833 | contour->findTooCloseToCall(winding); |
| 1834 | } |
| 1835 | } |
| 1836 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1837 | |
| 1838 | // OPTIMIZATION: not crazy about linear search here to find top active y. |
| 1839 | // seems like we should break down and do the sort, or maybe sort each |
| 1840 | // contours' segments? |
| 1841 | // Once the segment array is built, there's no reason I can think of not to |
| 1842 | // sort it in Y. hmmm |
| 1843 | static Segment* findTopContour(SkTDArray<Contour*>& contourList, |
| 1844 | int contourCount) { |
| 1845 | int cIndex = 0; |
| 1846 | Segment* topStart; |
| 1847 | do { |
| 1848 | Contour* topContour = contourList[cIndex]; |
| 1849 | topStart = topContour->topSegment(); |
| 1850 | } while (!topStart && ++cIndex < contourCount); |
| 1851 | if (!topStart) { |
| 1852 | return NULL; |
| 1853 | } |
| 1854 | SkScalar top = topStart->bounds().fTop; |
| 1855 | for (int cTest = cIndex + 1; cTest < contourCount; ++cTest) { |
| 1856 | Contour* contour = contourList[cTest]; |
| 1857 | if (top < contour->bounds().fTop) { |
| 1858 | continue; |
| 1859 | } |
| 1860 | Segment* test = contour->topSegment(); |
| 1861 | if (top > test->bounds().fTop) { |
| 1862 | cIndex = cTest; |
| 1863 | topStart = test; |
| 1864 | top = test->bounds().fTop; |
| 1865 | } |
| 1866 | } |
| 1867 | return topStart; |
| 1868 | } |
| 1869 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1870 | // Each segment may have an inside or an outside. Segments contained within |
| 1871 | // winding may have insides on either side, and form a contour that should be |
| 1872 | // ignored. Segments that are coincident with opposing direction segments may |
| 1873 | // have outsides on either side, and should also disappear. |
| 1874 | // 'Normal' segments will have one inside and one outside. Subsequent connections |
| 1875 | // when winding should follow the intersection direction. If more than one edge |
| 1876 | // is an option, choose first edge that continues the inside. |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1877 | // since we start with leftmost top edge, we'll traverse through a |
| 1878 | // smaller angle counterclockwise to get to the next edge. |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1879 | static void bridge(SkTDArray<Contour*>& contourList) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1880 | int contourCount = contourList.count(); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1881 | int winding = 0; // there are no contours outside this one |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1882 | do { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1883 | Segment* topStart = findTopContour(contourList, contourCount); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1884 | if (!topStart) { |
| 1885 | break; |
| 1886 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1887 | // Start at the top. Above the top is outside, below is inside. |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1888 | // follow edges to intersection by changing the tIndex by direction. |
| 1889 | int tIndex, step; |
| 1890 | const Segment* topSegment = topStart->findTop(tIndex, step); |
| 1891 | const Segment* next = topSegment; |
| 1892 | do { |
| 1893 | int spanIndex; |
| 1894 | next = next->findNext(tIndex, winding, step, spanIndex); |
| 1895 | } while (next != topSegment); |
| 1896 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1897 | // at intersection, stay on outside, but mark remaining edges as inside |
| 1898 | // or, only mark first pair as inside? |
| 1899 | // how is this going to work for contained (but not intersecting) |
| 1900 | // segments? |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1901 | // start here ; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1902 | // find span |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1903 | // mark neighbors winding coverage |
| 1904 | // output span |
| 1905 | // mark span as processed |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1906 | |
| 1907 | } while (true); |
| 1908 | |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1909 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1912 | static void fixOtherTIndex(SkTDArray<Contour*>& contourList) { |
| 1913 | int contourCount = contourList.count(); |
| 1914 | for (int cTest = 0; cTest < contourCount; ++cTest) { |
| 1915 | Contour* contour = contourList[cTest]; |
| 1916 | contour->fixOtherTIndex(); |
| 1917 | } |
| 1918 | } |
| 1919 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1920 | static void makeContourList(SkTArray<Contour>& contours, Contour& sentinel, |
| 1921 | SkTDArray<Contour*>& list) { |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1922 | int count = contours.count(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1923 | if (count == 0) { |
| 1924 | return; |
| 1925 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1926 | for (int index = 0; index < count; ++index) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1927 | *list.append() = &contours[index]; |
| 1928 | } |
| 1929 | *list.append() = &sentinel; |
| 1930 | QSort<Contour>(list.begin(), list.end() - 1); |
| 1931 | } |
| 1932 | |
| 1933 | void simplifyx(const SkPath& path, bool asFill, SkPath& simple) { |
| 1934 | // returns 1 for evenodd, -1 for winding, regardless of inverse-ness |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1935 | int winding = (path.getFillType() & 1) ? 1 : -1; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1936 | simple.reset(); |
| 1937 | simple.setFillType(SkPath::kEvenOdd_FillType); |
| 1938 | |
| 1939 | // turn path into list of segments |
| 1940 | SkTArray<Contour> contours; |
| 1941 | // FIXME: add self-intersecting cubics' T values to segment |
| 1942 | EdgeBuilder builder(path, contours); |
| 1943 | SkTDArray<Contour*> contourList; |
| 1944 | Contour sentinel; |
| 1945 | sentinel.reset(); |
| 1946 | makeContourList(contours, sentinel, contourList); |
| 1947 | Contour** currentPtr = contourList.begin(); |
| 1948 | if (!currentPtr) { |
| 1949 | return; |
| 1950 | } |
| 1951 | // find all intersections between segments |
| 1952 | do { |
| 1953 | Contour** nextPtr = currentPtr; |
| 1954 | Contour* current = *currentPtr++; |
| 1955 | Contour* next; |
| 1956 | do { |
| 1957 | next = *nextPtr++; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1958 | } while (next != &sentinel && addIntersectTs(current, next, winding)); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1959 | } while (*currentPtr != &sentinel); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame^] | 1960 | fixOtherTIndex(contourList); |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1961 | // eat through coincident edges |
| 1962 | coincidenceCheck(contourList, winding); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1963 | // construct closed contours |
| 1964 | bridge(contourList); |
| 1965 | } |
| 1966 | |