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 |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 20 | // An Edge is a Segment generated from a Span |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 21 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 22 | // FIXME: remove once debugging is complete |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 23 | #ifdef SK_DEBUG |
| 24 | int gDebugMaxWindSum = SK_MaxS32; |
| 25 | int gDebugMaxWindValue = SK_MaxS32; |
| 26 | #endif |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 27 | |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 28 | #define DEBUG_UNUSED 0 // set to expose unused functions |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 29 | |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 30 | #if 01 // set to 1 for multiple thread -- no debugging |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 31 | |
| 32 | const bool gRunTestsInOneThread = false; |
| 33 | |
| 34 | #define DEBUG_ACTIVE_SPANS 0 |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 35 | #define DEBUG_ADD_INTERSECTING_TS 0 |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 36 | #define DEBUG_ADD_T_PAIR 0 |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 37 | #define DEBUG_CONCIDENT 0 |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 38 | #define DEBUG_CROSS 0 |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 39 | #define DEBUG_DUMP 0 |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 40 | #define DEBUG_MARK_DONE 0 |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 41 | #define DEBUG_PATH_CONSTRUCTION 0 |
| 42 | #define DEBUG_SORT 0 |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 43 | #define DEBUG_WIND_BUMP 0 |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 44 | #define DEBUG_WINDING 0 |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 45 | |
| 46 | #else |
| 47 | |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 48 | const bool gRunTestsInOneThread = true; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 49 | |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 50 | #define DEBUG_ACTIVE_SPANS 1 |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 51 | #define DEBUG_ADD_INTERSECTING_TS 0 |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 52 | #define DEBUG_ADD_T_PAIR 1 |
| 53 | #define DEBUG_CONCIDENT 1 |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 54 | #define DEBUG_CROSS 0 |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 55 | #define DEBUG_DUMP 1 |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 56 | #define DEBUG_MARK_DONE 1 |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 57 | #define DEBUG_PATH_CONSTRUCTION 1 |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 58 | #define DEBUG_SORT 1 |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 59 | #define DEBUG_WIND_BUMP 0 |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 60 | #define DEBUG_WINDING 1 |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 61 | |
| 62 | #endif |
| 63 | |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 64 | #if (DEBUG_ACTIVE_SPANS || DEBUG_CONCIDENT || DEBUG_SORT) && !DEBUG_DUMP |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 65 | #undef DEBUG_DUMP |
| 66 | #define DEBUG_DUMP 1 |
| 67 | #endif |
| 68 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 69 | #if DEBUG_DUMP |
| 70 | static const char* kLVerbStr[] = {"", "line", "quad", "cubic"}; |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 71 | // static const char* kUVerbStr[] = {"", "Line", "Quad", "Cubic"}; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 72 | static int gContourID; |
| 73 | static int gSegmentID; |
| 74 | #endif |
| 75 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 76 | #ifndef DEBUG_TEST |
| 77 | #define DEBUG_TEST 0 |
| 78 | #endif |
| 79 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 80 | static int LineIntersect(const SkPoint a[2], const SkPoint b[2], |
| 81 | Intersections& intersections) { |
| 82 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
| 83 | const _Line bLine = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}}; |
| 84 | return intersect(aLine, bLine, intersections.fT[0], intersections.fT[1]); |
| 85 | } |
| 86 | |
| 87 | static int QuadLineIntersect(const SkPoint a[3], const SkPoint b[2], |
| 88 | Intersections& intersections) { |
| 89 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}}; |
| 90 | const _Line bLine = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}}; |
| 91 | intersect(aQuad, bLine, intersections); |
| 92 | return intersections.fUsed; |
| 93 | } |
| 94 | |
| 95 | static int CubicLineIntersect(const SkPoint a[2], const SkPoint b[3], |
| 96 | Intersections& intersections) { |
| 97 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}, |
| 98 | {a[3].fX, a[3].fY}}; |
| 99 | const _Line bLine = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}}; |
| 100 | return intersect(aCubic, bLine, intersections.fT[0], intersections.fT[1]); |
| 101 | } |
| 102 | |
| 103 | static int QuadIntersect(const SkPoint a[3], const SkPoint b[3], |
| 104 | Intersections& intersections) { |
| 105 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}}; |
| 106 | const Quadratic bQuad = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}, {b[2].fX, b[2].fY}}; |
| 107 | intersect(aQuad, bQuad, intersections); |
| 108 | return intersections.fUsed; |
| 109 | } |
| 110 | |
| 111 | static int CubicIntersect(const SkPoint a[4], const SkPoint b[4], |
| 112 | Intersections& intersections) { |
| 113 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}, |
| 114 | {a[3].fX, a[3].fY}}; |
| 115 | const Cubic bCubic = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}, {b[2].fX, b[2].fY}, |
| 116 | {b[3].fX, b[3].fY}}; |
| 117 | intersect(aCubic, bCubic, intersections); |
| 118 | return intersections.fUsed; |
| 119 | } |
| 120 | |
| 121 | static int HLineIntersect(const SkPoint a[2], SkScalar left, SkScalar right, |
| 122 | SkScalar y, bool flipped, Intersections& intersections) { |
| 123 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
| 124 | return horizontalIntersect(aLine, left, right, y, flipped, intersections); |
| 125 | } |
| 126 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 127 | static int HQuadIntersect(const SkPoint a[3], SkScalar left, SkScalar right, |
| 128 | SkScalar y, bool flipped, Intersections& intersections) { |
| 129 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}}; |
| 130 | return horizontalIntersect(aQuad, left, right, y, flipped, intersections); |
| 131 | } |
| 132 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 133 | static int HCubicIntersect(const SkPoint a[4], SkScalar left, SkScalar right, |
| 134 | SkScalar y, bool flipped, Intersections& intersections) { |
| 135 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}, |
| 136 | {a[3].fX, a[3].fY}}; |
| 137 | return horizontalIntersect(aCubic, left, right, y, flipped, intersections); |
| 138 | } |
| 139 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 140 | static int VLineIntersect(const SkPoint a[2], SkScalar top, SkScalar bottom, |
| 141 | SkScalar x, bool flipped, Intersections& intersections) { |
| 142 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
| 143 | return verticalIntersect(aLine, top, bottom, x, flipped, intersections); |
| 144 | } |
| 145 | |
| 146 | static int VQuadIntersect(const SkPoint a[3], SkScalar top, SkScalar bottom, |
| 147 | SkScalar x, bool flipped, Intersections& intersections) { |
| 148 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}}; |
| 149 | return verticalIntersect(aQuad, top, bottom, x, flipped, intersections); |
| 150 | } |
| 151 | |
| 152 | static int VCubicIntersect(const SkPoint a[4], SkScalar top, SkScalar bottom, |
| 153 | SkScalar x, bool flipped, Intersections& intersections) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 154 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}, |
| 155 | {a[3].fX, a[3].fY}}; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 156 | return verticalIntersect(aCubic, top, bottom, x, flipped, intersections); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 157 | } |
| 158 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 159 | static int (* const VSegmentIntersect[])(const SkPoint [], SkScalar , |
| 160 | SkScalar , SkScalar , bool , Intersections& ) = { |
| 161 | NULL, |
| 162 | VLineIntersect, |
| 163 | VQuadIntersect, |
| 164 | VCubicIntersect |
| 165 | }; |
| 166 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 167 | static void LineXYAtT(const SkPoint a[2], double t, SkPoint* out) { |
| 168 | const _Line line = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
| 169 | double x, y; |
| 170 | xy_at_t(line, t, x, y); |
| 171 | out->fX = SkDoubleToScalar(x); |
| 172 | out->fY = SkDoubleToScalar(y); |
| 173 | } |
| 174 | |
| 175 | static void QuadXYAtT(const SkPoint a[3], double t, SkPoint* out) { |
| 176 | const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}}; |
| 177 | double x, y; |
| 178 | xy_at_t(quad, t, x, y); |
| 179 | out->fX = SkDoubleToScalar(x); |
| 180 | out->fY = SkDoubleToScalar(y); |
| 181 | } |
| 182 | |
| 183 | static void CubicXYAtT(const SkPoint a[4], double t, SkPoint* out) { |
| 184 | const Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}, |
| 185 | {a[3].fX, a[3].fY}}; |
| 186 | double x, y; |
| 187 | xy_at_t(cubic, t, x, y); |
| 188 | out->fX = SkDoubleToScalar(x); |
| 189 | out->fY = SkDoubleToScalar(y); |
| 190 | } |
| 191 | |
| 192 | static void (* const SegmentXYAtT[])(const SkPoint [], double , SkPoint* ) = { |
| 193 | NULL, |
| 194 | LineXYAtT, |
| 195 | QuadXYAtT, |
| 196 | CubicXYAtT |
| 197 | }; |
| 198 | |
| 199 | static SkScalar LineXAtT(const SkPoint a[2], double t) { |
| 200 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
| 201 | double x; |
| 202 | xy_at_t(aLine, t, x, *(double*) 0); |
| 203 | return SkDoubleToScalar(x); |
| 204 | } |
| 205 | |
| 206 | static SkScalar QuadXAtT(const SkPoint a[3], double t) { |
| 207 | const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}}; |
| 208 | double x; |
| 209 | xy_at_t(quad, t, x, *(double*) 0); |
| 210 | return SkDoubleToScalar(x); |
| 211 | } |
| 212 | |
| 213 | static SkScalar CubicXAtT(const SkPoint a[4], double t) { |
| 214 | const Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}, |
| 215 | {a[3].fX, a[3].fY}}; |
| 216 | double x; |
| 217 | xy_at_t(cubic, t, x, *(double*) 0); |
| 218 | return SkDoubleToScalar(x); |
| 219 | } |
| 220 | |
| 221 | static SkScalar (* const SegmentXAtT[])(const SkPoint [], double ) = { |
| 222 | NULL, |
| 223 | LineXAtT, |
| 224 | QuadXAtT, |
| 225 | CubicXAtT |
| 226 | }; |
| 227 | |
| 228 | static SkScalar LineYAtT(const SkPoint a[2], double t) { |
| 229 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
| 230 | double y; |
| 231 | xy_at_t(aLine, t, *(double*) 0, y); |
| 232 | return SkDoubleToScalar(y); |
| 233 | } |
| 234 | |
| 235 | static SkScalar QuadYAtT(const SkPoint a[3], double t) { |
| 236 | const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}}; |
| 237 | double y; |
| 238 | xy_at_t(quad, t, *(double*) 0, y); |
| 239 | return SkDoubleToScalar(y); |
| 240 | } |
| 241 | |
| 242 | static SkScalar CubicYAtT(const SkPoint a[4], double t) { |
| 243 | const Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}, |
| 244 | {a[3].fX, a[3].fY}}; |
| 245 | double y; |
| 246 | xy_at_t(cubic, t, *(double*) 0, y); |
| 247 | return SkDoubleToScalar(y); |
| 248 | } |
| 249 | |
| 250 | static SkScalar (* const SegmentYAtT[])(const SkPoint [], double ) = { |
| 251 | NULL, |
| 252 | LineYAtT, |
| 253 | QuadYAtT, |
| 254 | CubicYAtT |
| 255 | }; |
| 256 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 257 | static SkScalar LineDXAtT(const SkPoint a[2], double ) { |
| 258 | return a[1].fX - a[0].fX; |
| 259 | } |
| 260 | |
| 261 | static SkScalar QuadDXAtT(const SkPoint a[3], double t) { |
| 262 | const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}}; |
| 263 | double x; |
| 264 | dxdy_at_t(quad, t, x, *(double*) 0); |
| 265 | return SkDoubleToScalar(x); |
| 266 | } |
| 267 | |
| 268 | static SkScalar CubicDXAtT(const SkPoint a[4], double t) { |
| 269 | const Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}, |
| 270 | {a[3].fX, a[3].fY}}; |
| 271 | double x; |
| 272 | dxdy_at_t(cubic, t, x, *(double*) 0); |
| 273 | return SkDoubleToScalar(x); |
| 274 | } |
| 275 | |
| 276 | static SkScalar (* const SegmentDXAtT[])(const SkPoint [], double ) = { |
| 277 | NULL, |
| 278 | LineDXAtT, |
| 279 | QuadDXAtT, |
| 280 | CubicDXAtT |
| 281 | }; |
| 282 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 283 | static void LineSubDivide(const SkPoint a[2], double startT, double endT, |
| 284 | SkPoint sub[2]) { |
| 285 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
| 286 | _Line dst; |
| 287 | sub_divide(aLine, startT, endT, dst); |
| 288 | sub[0].fX = SkDoubleToScalar(dst[0].x); |
| 289 | sub[0].fY = SkDoubleToScalar(dst[0].y); |
| 290 | sub[1].fX = SkDoubleToScalar(dst[1].x); |
| 291 | sub[1].fY = SkDoubleToScalar(dst[1].y); |
| 292 | } |
| 293 | |
| 294 | static void QuadSubDivide(const SkPoint a[3], double startT, double endT, |
| 295 | SkPoint sub[3]) { |
| 296 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 297 | {a[2].fX, a[2].fY}}; |
| 298 | Quadratic dst; |
| 299 | sub_divide(aQuad, startT, endT, dst); |
| 300 | sub[0].fX = SkDoubleToScalar(dst[0].x); |
| 301 | sub[0].fY = SkDoubleToScalar(dst[0].y); |
| 302 | sub[1].fX = SkDoubleToScalar(dst[1].x); |
| 303 | sub[1].fY = SkDoubleToScalar(dst[1].y); |
| 304 | sub[2].fX = SkDoubleToScalar(dst[2].x); |
| 305 | sub[2].fY = SkDoubleToScalar(dst[2].y); |
| 306 | } |
| 307 | |
| 308 | static void CubicSubDivide(const SkPoint a[4], double startT, double endT, |
| 309 | SkPoint sub[4]) { |
| 310 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 311 | {a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}}; |
| 312 | Cubic dst; |
| 313 | sub_divide(aCubic, startT, endT, dst); |
| 314 | sub[0].fX = SkDoubleToScalar(dst[0].x); |
| 315 | sub[0].fY = SkDoubleToScalar(dst[0].y); |
| 316 | sub[1].fX = SkDoubleToScalar(dst[1].x); |
| 317 | sub[1].fY = SkDoubleToScalar(dst[1].y); |
| 318 | sub[2].fX = SkDoubleToScalar(dst[2].x); |
| 319 | sub[2].fY = SkDoubleToScalar(dst[2].y); |
| 320 | sub[3].fX = SkDoubleToScalar(dst[3].x); |
| 321 | sub[3].fY = SkDoubleToScalar(dst[3].y); |
| 322 | } |
| 323 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 324 | static void (* const SegmentSubDivide[])(const SkPoint [], double , double , |
| 325 | SkPoint []) = { |
| 326 | NULL, |
| 327 | LineSubDivide, |
| 328 | QuadSubDivide, |
| 329 | CubicSubDivide |
| 330 | }; |
| 331 | |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 332 | #if DEBUG_UNUSED |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 333 | static void QuadSubBounds(const SkPoint a[3], double startT, double endT, |
| 334 | SkRect& bounds) { |
| 335 | SkPoint dst[3]; |
| 336 | QuadSubDivide(a, startT, endT, dst); |
| 337 | bounds.fLeft = bounds.fRight = dst[0].fX; |
| 338 | bounds.fTop = bounds.fBottom = dst[0].fY; |
| 339 | for (int index = 1; index < 3; ++index) { |
| 340 | bounds.growToInclude(dst[index].fX, dst[index].fY); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | static void CubicSubBounds(const SkPoint a[4], double startT, double endT, |
| 345 | SkRect& bounds) { |
| 346 | SkPoint dst[4]; |
| 347 | CubicSubDivide(a, startT, endT, dst); |
| 348 | bounds.fLeft = bounds.fRight = dst[0].fX; |
| 349 | bounds.fTop = bounds.fBottom = dst[0].fY; |
| 350 | for (int index = 1; index < 4; ++index) { |
| 351 | bounds.growToInclude(dst[index].fX, dst[index].fY); |
| 352 | } |
| 353 | } |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 354 | #endif |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 355 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 356 | static SkPath::Verb QuadReduceOrder(const SkPoint a[3], |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 357 | SkTDArray<SkPoint>& reducePts) { |
| 358 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 359 | {a[2].fX, a[2].fY}}; |
| 360 | Quadratic dst; |
| 361 | int order = reduceOrder(aQuad, dst); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 362 | if (order == 3) { |
| 363 | return SkPath::kQuad_Verb; |
| 364 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 365 | for (int index = 0; index < order; ++index) { |
| 366 | SkPoint* pt = reducePts.append(); |
| 367 | pt->fX = SkDoubleToScalar(dst[index].x); |
| 368 | pt->fY = SkDoubleToScalar(dst[index].y); |
| 369 | } |
| 370 | return (SkPath::Verb) (order - 1); |
| 371 | } |
| 372 | |
| 373 | static SkPath::Verb CubicReduceOrder(const SkPoint a[4], |
| 374 | SkTDArray<SkPoint>& reducePts) { |
| 375 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 376 | {a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}}; |
| 377 | Cubic dst; |
| 378 | int order = reduceOrder(aCubic, dst, kReduceOrder_QuadraticsAllowed); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 379 | if (order == 4) { |
| 380 | return SkPath::kCubic_Verb; |
| 381 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 382 | for (int index = 0; index < order; ++index) { |
| 383 | SkPoint* pt = reducePts.append(); |
| 384 | pt->fX = SkDoubleToScalar(dst[index].x); |
| 385 | pt->fY = SkDoubleToScalar(dst[index].y); |
| 386 | } |
| 387 | return (SkPath::Verb) (order - 1); |
| 388 | } |
| 389 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 390 | static bool QuadIsLinear(const SkPoint a[3]) { |
| 391 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 392 | {a[2].fX, a[2].fY}}; |
| 393 | return isLinear(aQuad, 0, 2); |
| 394 | } |
| 395 | |
| 396 | static bool CubicIsLinear(const SkPoint a[4]) { |
| 397 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 398 | {a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}}; |
| 399 | return isLinear(aCubic, 0, 3); |
| 400 | } |
| 401 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 402 | static SkScalar LineLeftMost(const SkPoint a[2], double startT, double endT) { |
| 403 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
| 404 | double x[2]; |
| 405 | xy_at_t(aLine, startT, x[0], *(double*) 0); |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 406 | xy_at_t(aLine, endT, x[1], *(double*) 0); |
| 407 | return SkMinScalar((float) x[0], (float) x[1]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | static SkScalar QuadLeftMost(const SkPoint a[3], double startT, double endT) { |
| 411 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 412 | {a[2].fX, a[2].fY}}; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 413 | return (float) leftMostT(aQuad, startT, endT); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static SkScalar CubicLeftMost(const SkPoint a[4], double startT, double endT) { |
| 417 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 418 | {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] | 419 | return (float) leftMostT(aCubic, startT, endT); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static SkScalar (* const SegmentLeftMost[])(const SkPoint [], double , double) = { |
| 423 | NULL, |
| 424 | LineLeftMost, |
| 425 | QuadLeftMost, |
| 426 | CubicLeftMost |
| 427 | }; |
| 428 | |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 429 | #if DEBUG_UNUSED |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 430 | static bool IsCoincident(const SkPoint a[2], const SkPoint& above, |
| 431 | const SkPoint& below) { |
| 432 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
| 433 | const _Line bLine = {{above.fX, above.fY}, {below.fX, below.fY}}; |
| 434 | return implicit_matches_ulps(aLine, bLine, 32); |
| 435 | } |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 436 | #endif |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 437 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 438 | class Segment; |
| 439 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 440 | // sorting angles |
| 441 | // given angles of {dx dy ddx ddy dddx dddy} sort them |
| 442 | class Angle { |
| 443 | public: |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 444 | // FIXME: this is bogus for quads and cubics |
| 445 | // if the quads and cubics' line from end pt to ctrl pt are coincident, |
| 446 | // there's no obvious way to determine the curve ordering from the |
| 447 | // derivatives alone. In particular, if one quadratic's coincident tangent |
| 448 | // is longer than the other curve, the final control point can place the |
| 449 | // longer curve on either side of the shorter one. |
| 450 | // Using Bezier curve focus http://cagd.cs.byu.edu/~tom/papers/bezclip.pdf |
| 451 | // may provide some help, but nothing has been figured out yet. |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 452 | bool operator<(const Angle& rh) const { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 453 | if ((fDy < 0) ^ (rh.fDy < 0)) { |
| 454 | return fDy < 0; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 455 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 456 | if (fDy == 0 && rh.fDy == 0 && fDx != rh.fDx) { |
| 457 | return fDx < rh.fDx; |
| 458 | } |
| 459 | SkScalar cmp = fDx * rh.fDy - rh.fDx * fDy; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 460 | if (cmp) { |
| 461 | return cmp < 0; |
| 462 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 463 | if ((fDDy < 0) ^ (rh.fDDy < 0)) { |
| 464 | return fDDy < 0; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 465 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 466 | if (fDDy == 0 && rh.fDDy == 0 && fDDx != rh.fDDx) { |
| 467 | return fDDx < rh.fDDx; |
| 468 | } |
| 469 | cmp = fDDx * rh.fDDy - rh.fDDx * fDDy; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 470 | if (cmp) { |
| 471 | return cmp < 0; |
| 472 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 473 | if ((fDDDy < 0) ^ (rh.fDDDy < 0)) { |
| 474 | return fDDDy < 0; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 475 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 476 | if (fDDDy == 0 && rh.fDDDy == 0) { |
| 477 | return fDDDx < rh.fDDDx; |
| 478 | } |
| 479 | return fDDDx * rh.fDDDy < rh.fDDDx * fDDDy; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 480 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 481 | |
| 482 | double dx() const { |
| 483 | return fDx; |
| 484 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 485 | |
caryclark@google.com | 7db7c6b | 2012-07-27 21:22:25 +0000 | [diff] [blame] | 486 | double dy() const { |
| 487 | return fDy; |
| 488 | } |
| 489 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 490 | int end() const { |
| 491 | return fEnd; |
| 492 | } |
| 493 | |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 494 | bool isHorizontal() const { |
| 495 | return fDy == 0 && fDDy == 0 && fDDDy == 0; |
| 496 | } |
| 497 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 498 | // since all angles share a point, this needs to know which point |
| 499 | // is the common origin, i.e., whether the center is at pts[0] or pts[verb] |
| 500 | // practically, this should only be called by addAngle |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 501 | void set(const SkPoint* pts, SkPath::Verb verb, const Segment* segment, |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 502 | int start, int end) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 503 | SkASSERT(start != end); |
| 504 | fSegment = segment; |
| 505 | fStart = start; |
| 506 | fEnd = end; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 507 | fDx = pts[1].fX - pts[0].fX; // b - a |
| 508 | fDy = pts[1].fY - pts[0].fY; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 509 | if (verb == SkPath::kLine_Verb) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 510 | fDDx = fDDy = fDDDx = fDDDy = 0; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 511 | return; |
| 512 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 513 | fDDx = pts[2].fX - pts[1].fX - fDx; // a - 2b + c |
| 514 | fDDy = pts[2].fY - pts[1].fY - fDy; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 515 | if (verb == SkPath::kQuad_Verb) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 516 | fDDDx = fDDDy = 0; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 517 | return; |
| 518 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 519 | fDDDx = pts[3].fX + 3 * (pts[1].fX - pts[2].fX) - pts[0].fX; |
| 520 | fDDDy = pts[3].fY + 3 * (pts[1].fY - pts[2].fY) - pts[0].fY; |
| 521 | } |
| 522 | |
| 523 | // noncoincident quads/cubics may have the same initial angle |
| 524 | // as lines, so must sort by derivatives as well |
| 525 | // if flatness turns out to be a reasonable way to sort, use the below: |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 526 | void setFlat(const SkPoint* pts, SkPath::Verb verb, Segment* segment, |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 527 | int start, int end) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 528 | fSegment = segment; |
| 529 | fStart = start; |
| 530 | fEnd = end; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 531 | fDx = pts[1].fX - pts[0].fX; // b - a |
| 532 | fDy = pts[1].fY - pts[0].fY; |
| 533 | if (verb == SkPath::kLine_Verb) { |
| 534 | fDDx = fDDy = fDDDx = fDDDy = 0; |
| 535 | return; |
| 536 | } |
| 537 | if (verb == SkPath::kQuad_Verb) { |
| 538 | int uplsX = FloatAsInt(pts[2].fX - pts[1].fY - fDx); |
| 539 | int uplsY = FloatAsInt(pts[2].fY - pts[1].fY - fDy); |
| 540 | int larger = std::max(abs(uplsX), abs(uplsY)); |
| 541 | int shift = 0; |
| 542 | double flatT; |
| 543 | SkPoint ddPt; // FIXME: get rid of copy (change fDD_ to point) |
| 544 | LineParameters implicitLine; |
| 545 | _Line tangent = {{pts[0].fX, pts[0].fY}, {pts[1].fX, pts[1].fY}}; |
| 546 | implicitLine.lineEndPoints(tangent); |
| 547 | implicitLine.normalize(); |
| 548 | while (larger > UlpsEpsilon * 1024) { |
| 549 | larger >>= 2; |
| 550 | ++shift; |
| 551 | flatT = 0.5 / (1 << shift); |
| 552 | QuadXYAtT(pts, flatT, &ddPt); |
| 553 | _Point _pt = {ddPt.fX, ddPt.fY}; |
| 554 | double distance = implicitLine.pointDistance(_pt); |
| 555 | if (approximately_zero(distance)) { |
| 556 | SkDebugf("%s ulps too small %1.9g\n", __FUNCTION__, distance); |
| 557 | break; |
| 558 | } |
| 559 | } |
| 560 | flatT = 0.5 / (1 << shift); |
| 561 | QuadXYAtT(pts, flatT, &ddPt); |
| 562 | fDDx = ddPt.fX - pts[0].fX; |
| 563 | fDDy = ddPt.fY - pts[0].fY; |
| 564 | SkASSERT(fDDx != 0 || fDDy != 0); |
| 565 | fDDDx = fDDDy = 0; |
| 566 | return; |
| 567 | } |
| 568 | SkASSERT(0); // FIXME: add cubic case |
| 569 | } |
| 570 | |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 571 | Segment* segment() const { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 572 | return const_cast<Segment*>(fSegment); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | int sign() const { |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 576 | return SkSign32(fStart - fEnd); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 577 | } |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 578 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 579 | int start() const { |
| 580 | return fStart; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | private: |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 584 | SkScalar fDx; |
| 585 | SkScalar fDy; |
| 586 | SkScalar fDDx; |
| 587 | SkScalar fDDy; |
| 588 | SkScalar fDDDx; |
| 589 | SkScalar fDDDy; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 590 | const Segment* fSegment; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 591 | int fStart; |
| 592 | int fEnd; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 593 | }; |
| 594 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 595 | static void sortAngles(SkTDArray<Angle>& angles, SkTDArray<Angle*>& angleList) { |
| 596 | int angleCount = angles.count(); |
| 597 | int angleIndex; |
| 598 | angleList.setReserve(angleCount); |
| 599 | for (angleIndex = 0; angleIndex < angleCount; ++angleIndex) { |
| 600 | *angleList.append() = &angles[angleIndex]; |
| 601 | } |
| 602 | QSort<Angle>(angleList.begin(), angleList.end() - 1); |
| 603 | } |
| 604 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 605 | // Bounds, unlike Rect, does not consider a line to be empty. |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 606 | struct Bounds : public SkRect { |
| 607 | static bool Intersects(const Bounds& a, const Bounds& b) { |
| 608 | return a.fLeft <= b.fRight && b.fLeft <= a.fRight && |
| 609 | a.fTop <= b.fBottom && b.fTop <= a.fBottom; |
| 610 | } |
| 611 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 612 | void add(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) { |
| 613 | if (left < fLeft) { |
| 614 | fLeft = left; |
| 615 | } |
| 616 | if (top < fTop) { |
| 617 | fTop = top; |
| 618 | } |
| 619 | if (right > fRight) { |
| 620 | fRight = right; |
| 621 | } |
| 622 | if (bottom > fBottom) { |
| 623 | fBottom = bottom; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | void add(const Bounds& toAdd) { |
| 628 | add(toAdd.fLeft, toAdd.fTop, toAdd.fRight, toAdd.fBottom); |
| 629 | } |
| 630 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 631 | bool isEmpty() { |
| 632 | return fLeft > fRight || fTop > fBottom |
| 633 | || fLeft == fRight && fTop == fBottom |
| 634 | || isnan(fLeft) || isnan(fRight) |
| 635 | || isnan(fTop) || isnan(fBottom); |
| 636 | } |
| 637 | |
| 638 | void setCubicBounds(const SkPoint a[4]) { |
| 639 | _Rect dRect; |
| 640 | Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 641 | {a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}}; |
| 642 | dRect.setBounds(cubic); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 643 | set((float) dRect.left, (float) dRect.top, (float) dRect.right, |
| 644 | (float) dRect.bottom); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | void setQuadBounds(const SkPoint a[3]) { |
| 648 | const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 649 | {a[2].fX, a[2].fY}}; |
| 650 | _Rect dRect; |
| 651 | dRect.setBounds(quad); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 652 | set((float) dRect.left, (float) dRect.top, (float) dRect.right, |
| 653 | (float) dRect.bottom); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 654 | } |
| 655 | }; |
| 656 | |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 657 | static bool useInnerWinding(int outerWinding, int innerWinding) { |
| 658 | SkASSERT(outerWinding != innerWinding); |
| 659 | int absOut = abs(outerWinding); |
| 660 | int absIn = abs(innerWinding); |
| 661 | bool result = absOut == absIn ? outerWinding < 0 : absOut < absIn; |
| 662 | if (outerWinding * innerWinding < 0) { |
| 663 | #if DEBUG_WINDING |
| 664 | SkDebugf("%s *** outer=%d inner=%d result=%s\n", __FUNCTION__, |
| 665 | outerWinding, innerWinding, result ? "true" : "false"); |
| 666 | #endif |
| 667 | } |
| 668 | return result; |
| 669 | } |
| 670 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 671 | struct Span { |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 672 | Segment* fOther; |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 673 | mutable SkPoint fPt; // lazily computed as needed |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 674 | double fT; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 675 | double fOtherT; // value at fOther[fOtherIndex].fT |
| 676 | int fOtherIndex; // can't be used during intersection |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 677 | int fWindSum; // accumulated from contours surrounding this one |
| 678 | int fWindValue; // 0 == canceled; 1 == normal; >1 == coincident |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 679 | bool fDone; // if set, this span to next higher T has been processed |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 680 | bool fOutside; // if set, sum is outside, sum + sign * value computes inside |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 681 | }; |
| 682 | |
| 683 | class Segment { |
| 684 | public: |
| 685 | Segment() { |
| 686 | #if DEBUG_DUMP |
| 687 | fID = ++gSegmentID; |
| 688 | #endif |
| 689 | } |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 690 | |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 691 | bool activeAngle(int index, int& done, SkTDArray<Angle>& angles) const { |
| 692 | if (activeAngleInner(index, done, angles)) { |
| 693 | return true; |
| 694 | } |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 695 | double referenceT = fTs[index].fT; |
| 696 | int lesser = index; |
| 697 | while (--lesser >= 0 && referenceT - fTs[lesser].fT < FLT_EPSILON) { |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 698 | if (activeAngleOther(lesser, done, angles)) { |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 699 | return true; |
| 700 | } |
| 701 | } |
| 702 | do { |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 703 | if (activeAngleOther(index, done, angles)) { |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 704 | return true; |
| 705 | } |
| 706 | } while (++index < fTs.count() && fTs[index].fT - referenceT < FLT_EPSILON); |
| 707 | return false; |
| 708 | } |
| 709 | |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 710 | bool activeAngleOther(int index, int& done, SkTDArray<Angle>& angles) const { |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 711 | Span* span = &fTs[index]; |
| 712 | Segment* other = span->fOther; |
| 713 | int oIndex = span->fOtherIndex; |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 714 | return other->activeAngleInner(oIndex, done, angles); |
| 715 | } |
| 716 | |
| 717 | bool activeAngleInner(int index, int& done, SkTDArray<Angle>& angles) const { |
| 718 | int next = nextSpan(index, 1); |
| 719 | if (next > 0) { |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 720 | const Span& upSpan = fTs[index]; |
caryclark@google.com | 210acaf | 2012-07-12 21:05:13 +0000 | [diff] [blame] | 721 | if (upSpan.fWindValue) { |
| 722 | addAngle(angles, index, next); |
| 723 | if (upSpan.fDone) { |
| 724 | done++; |
| 725 | } else if (upSpan.fWindSum != SK_MinS32) { |
| 726 | return true; |
| 727 | } |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 728 | } |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 729 | } |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 730 | int prev = nextSpan(index, -1); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 731 | // edge leading into junction |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 732 | if (prev >= 0) { |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 733 | const Span& downSpan = fTs[prev]; |
caryclark@google.com | 210acaf | 2012-07-12 21:05:13 +0000 | [diff] [blame] | 734 | if (downSpan.fWindValue) { |
| 735 | addAngle(angles, index, prev); |
| 736 | if (downSpan.fDone) { |
| 737 | done++; |
| 738 | } else if (downSpan.fWindSum != SK_MinS32) { |
| 739 | return true; |
| 740 | } |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | return false; |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 744 | } |
| 745 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 746 | SkScalar activeTop() const { |
| 747 | SkASSERT(!done()); |
| 748 | int count = fTs.count(); |
| 749 | SkScalar result = SK_ScalarMax; |
| 750 | bool lastDone = true; |
| 751 | for (int index = 0; index < count; ++index) { |
| 752 | bool done = fTs[index].fDone; |
| 753 | if (!done || !lastDone) { |
| 754 | SkScalar y = yAtT(index); |
| 755 | if (result > y) { |
| 756 | result = y; |
| 757 | } |
| 758 | } |
| 759 | lastDone = done; |
| 760 | } |
| 761 | SkASSERT(result < SK_ScalarMax); |
| 762 | return result; |
| 763 | } |
| 764 | |
| 765 | void addAngle(SkTDArray<Angle>& angles, int start, int end) const { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 766 | SkASSERT(start != end); |
| 767 | SkPoint edge[4]; |
| 768 | (*SegmentSubDivide[fVerb])(fPts, fTs[start].fT, fTs[end].fT, edge); |
| 769 | Angle* angle = angles.append(); |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 770 | angle->set(edge, fVerb, this, start, end); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 771 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 772 | |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 773 | void addCancelOutsides(double tStart, double oStart, Segment& other, |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 774 | double oEnd) { |
| 775 | int tIndex = -1; |
| 776 | int tCount = fTs.count(); |
| 777 | int oIndex = -1; |
| 778 | int oCount = other.fTs.count(); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 779 | do { |
| 780 | ++tIndex; |
| 781 | } while (tStart - fTs[tIndex].fT >= FLT_EPSILON && tIndex < tCount); |
| 782 | int tIndexStart = tIndex; |
| 783 | do { |
| 784 | ++oIndex; |
| 785 | } while (oStart - other.fTs[oIndex].fT >= FLT_EPSILON && oIndex < oCount); |
| 786 | int oIndexStart = oIndex; |
| 787 | double nextT; |
| 788 | do { |
| 789 | nextT = fTs[++tIndex].fT; |
| 790 | } while (nextT < 1 && nextT - tStart < FLT_EPSILON); |
| 791 | double oNextT; |
| 792 | do { |
| 793 | oNextT = other.fTs[++oIndex].fT; |
| 794 | } while (oNextT < 1 && oNextT - oStart < FLT_EPSILON); |
| 795 | // at this point, spans before and after are at: |
| 796 | // fTs[tIndexStart - 1], fTs[tIndexStart], fTs[tIndex] |
| 797 | // if tIndexStart == 0, no prior span |
| 798 | // if nextT == 1, no following span |
| 799 | |
| 800 | // advance the span with zero winding |
| 801 | // if the following span exists (not past the end, non-zero winding) |
| 802 | // connect the two edges |
| 803 | if (!fTs[tIndexStart].fWindValue) { |
| 804 | if (tIndexStart > 0 && fTs[tIndexStart - 1].fWindValue) { |
| 805 | #if DEBUG_CONCIDENT |
| 806 | SkDebugf("%s 1 this=%d other=%d t [%d] %1.9g (%1.9g,%1.9g)\n", |
| 807 | __FUNCTION__, fID, other.fID, tIndexStart - 1, |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 808 | fTs[tIndexStart].fT, xyAtT(tIndexStart).fX, |
| 809 | xyAtT(tIndexStart).fY); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 810 | #endif |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 811 | addTPair(fTs[tIndexStart].fT, other, other.fTs[oIndex].fT, false); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 812 | } |
| 813 | if (nextT < 1 && fTs[tIndex].fWindValue) { |
| 814 | #if DEBUG_CONCIDENT |
| 815 | SkDebugf("%s 2 this=%d other=%d t [%d] %1.9g (%1.9g,%1.9g)\n", |
| 816 | __FUNCTION__, fID, other.fID, tIndex, |
| 817 | fTs[tIndex].fT, xyAtT(tIndex).fX, |
| 818 | xyAtT(tIndex).fY); |
| 819 | #endif |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 820 | addTPair(fTs[tIndex].fT, other, other.fTs[oIndexStart].fT, false); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 821 | } |
| 822 | } else { |
| 823 | SkASSERT(!other.fTs[oIndexStart].fWindValue); |
| 824 | if (oIndexStart > 0 && other.fTs[oIndexStart - 1].fWindValue) { |
| 825 | #if DEBUG_CONCIDENT |
| 826 | SkDebugf("%s 3 this=%d other=%d t [%d] %1.9g (%1.9g,%1.9g)\n", |
| 827 | __FUNCTION__, fID, other.fID, oIndexStart - 1, |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 828 | other.fTs[oIndexStart].fT, other.xyAtT(oIndexStart).fX, |
| 829 | other.xyAtT(oIndexStart).fY); |
| 830 | other.debugAddTPair(other.fTs[oIndexStart].fT, *this, fTs[tIndex].fT); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 831 | #endif |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 832 | } |
| 833 | if (oNextT < 1 && other.fTs[oIndex].fWindValue) { |
| 834 | #if DEBUG_CONCIDENT |
| 835 | SkDebugf("%s 4 this=%d other=%d t [%d] %1.9g (%1.9g,%1.9g)\n", |
| 836 | __FUNCTION__, fID, other.fID, oIndex, |
| 837 | other.fTs[oIndex].fT, other.xyAtT(oIndex).fX, |
| 838 | other.xyAtT(oIndex).fY); |
| 839 | other.debugAddTPair(other.fTs[oIndex].fT, *this, fTs[tIndexStart].fT); |
| 840 | #endif |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | void addCoinOutsides(const SkTDArray<double>& outsideTs, Segment& other, |
| 846 | double oEnd) { |
| 847 | // walk this to outsideTs[0] |
| 848 | // walk other to outsideTs[1] |
| 849 | // if either is > 0, add a pointer to the other, copying adjacent winding |
| 850 | int tIndex = -1; |
| 851 | int oIndex = -1; |
| 852 | double tStart = outsideTs[0]; |
| 853 | double oStart = outsideTs[1]; |
| 854 | do { |
| 855 | ++tIndex; |
| 856 | } while (tStart - fTs[tIndex].fT >= FLT_EPSILON); |
| 857 | do { |
| 858 | ++oIndex; |
| 859 | } while (oStart - other.fTs[oIndex].fT >= FLT_EPSILON); |
| 860 | if (tIndex > 0 || oIndex > 0) { |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 861 | addTPair(tStart, other, oStart, false); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 862 | } |
| 863 | tStart = fTs[tIndex].fT; |
| 864 | oStart = other.fTs[oIndex].fT; |
| 865 | do { |
| 866 | double nextT; |
| 867 | do { |
| 868 | nextT = fTs[++tIndex].fT; |
| 869 | } while (nextT - tStart < FLT_EPSILON); |
| 870 | tStart = nextT; |
| 871 | do { |
| 872 | nextT = other.fTs[++oIndex].fT; |
| 873 | } while (nextT - oStart < FLT_EPSILON); |
| 874 | oStart = nextT; |
| 875 | if (tStart == 1 && oStart == 1) { |
| 876 | break; |
| 877 | } |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 878 | addTPair(tStart, other, oStart, false); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 879 | } while (tStart < 1 && oStart < 1 && oEnd - oStart >= FLT_EPSILON); |
| 880 | } |
| 881 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 882 | void addCubic(const SkPoint pts[4]) { |
| 883 | init(pts, SkPath::kCubic_Verb); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 884 | fBounds.setCubicBounds(pts); |
| 885 | } |
| 886 | |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 887 | // FIXME: this needs to defer add for aligned consecutive line segments |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 888 | SkPoint addCurveTo(int start, int end, SkPath& path, bool active) { |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 889 | SkPoint edge[4]; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 890 | // OPTIMIZE? if not active, skip remainder and return xy_at_t(end) |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 891 | (*SegmentSubDivide[fVerb])(fPts, fTs[start].fT, fTs[end].fT, edge); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 892 | if (active) { |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 893 | #if DEBUG_PATH_CONSTRUCTION |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 894 | SkDebugf("%s %s (%1.9g,%1.9g)", __FUNCTION__, |
| 895 | kLVerbStr[fVerb], edge[1].fX, edge[1].fY); |
| 896 | if (fVerb > 1) { |
| 897 | SkDebugf(" (%1.9g,%1.9g)", edge[2].fX, edge[2].fY); |
| 898 | } |
| 899 | if (fVerb > 2) { |
| 900 | SkDebugf(" (%1.9g,%1.9g)", edge[3].fX, edge[3].fY); |
| 901 | } |
| 902 | SkDebugf("\n"); |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 903 | #endif |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 904 | switch (fVerb) { |
| 905 | case SkPath::kLine_Verb: |
| 906 | path.lineTo(edge[1].fX, edge[1].fY); |
| 907 | break; |
| 908 | case SkPath::kQuad_Verb: |
| 909 | path.quadTo(edge[1].fX, edge[1].fY, edge[2].fX, edge[2].fY); |
| 910 | break; |
| 911 | case SkPath::kCubic_Verb: |
| 912 | path.cubicTo(edge[1].fX, edge[1].fY, edge[2].fX, edge[2].fY, |
| 913 | edge[3].fX, edge[3].fY); |
| 914 | break; |
| 915 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 916 | } |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 917 | return edge[fVerb]; |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 918 | } |
| 919 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 920 | void addLine(const SkPoint pts[2]) { |
| 921 | init(pts, SkPath::kLine_Verb); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 922 | fBounds.set(pts, 2); |
| 923 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 924 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 925 | const SkPoint& addMoveTo(int tIndex, SkPath& path, bool active) { |
| 926 | const SkPoint& pt = xyAtT(tIndex); |
| 927 | if (active) { |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 928 | #if DEBUG_PATH_CONSTRUCTION |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 929 | SkDebugf("%s (%1.9g,%1.9g)\n", __FUNCTION__, pt.fX, pt.fY); |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 930 | #endif |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 931 | path.moveTo(pt.fX, pt.fY); |
| 932 | } |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 933 | return pt; |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 934 | } |
| 935 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 936 | // 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] | 937 | void addOtherT(int index, double otherT, int otherIndex) { |
| 938 | Span& span = fTs[index]; |
| 939 | span.fOtherT = otherT; |
| 940 | span.fOtherIndex = otherIndex; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 941 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 942 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 943 | void addQuad(const SkPoint pts[3]) { |
| 944 | init(pts, SkPath::kQuad_Verb); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 945 | fBounds.setQuadBounds(pts); |
| 946 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 947 | |
| 948 | // Defer all coincident edge processing until |
| 949 | // after normal intersections have been computed |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 950 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 951 | // no need to be tricky; insert in normal T order |
| 952 | // resolve overlapping ts when considering coincidence later |
| 953 | |
| 954 | // add non-coincident intersection. Resulting edges are sorted in T. |
| 955 | int addT(double newT, Segment* other) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 956 | // FIXME: in the pathological case where there is a ton of intercepts, |
| 957 | // binary search? |
| 958 | int insertedAt = -1; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 959 | size_t tCount = fTs.count(); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 960 | for (size_t index = 0; index < tCount; ++index) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 961 | // OPTIMIZATION: if there are three or more identical Ts, then |
| 962 | // the fourth and following could be further insertion-sorted so |
| 963 | // that all the edges are clockwise or counterclockwise. |
| 964 | // This could later limit segment tests to the two adjacent |
| 965 | // neighbors, although it doesn't help with determining which |
| 966 | // circular direction to go in. |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 967 | if (newT < fTs[index].fT) { |
| 968 | insertedAt = index; |
| 969 | break; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 970 | } |
| 971 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 972 | Span* span; |
| 973 | if (insertedAt >= 0) { |
| 974 | span = fTs.insert(insertedAt); |
| 975 | } else { |
| 976 | insertedAt = tCount; |
| 977 | span = fTs.append(); |
| 978 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 979 | span->fT = newT; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 980 | span->fOther = other; |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 981 | span->fPt.fX = SK_ScalarNaN; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 982 | span->fWindSum = SK_MinS32; |
| 983 | span->fWindValue = 1; |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 984 | span->fOutside = false; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 985 | if ((span->fDone = newT == 1)) { |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 986 | ++fDoneSpans; |
| 987 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 988 | return insertedAt; |
| 989 | } |
| 990 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 991 | // set spans from start to end to decrement by one |
| 992 | // note this walks other backwards |
| 993 | // FIMXE: there's probably an edge case that can be constructed where |
| 994 | // two span in one segment are separated by float epsilon on one span but |
| 995 | // not the other, if one segment is very small. For this |
| 996 | // case the counts asserted below may or may not be enough to separate the |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 997 | // spans. Even if the counts work out, what if the spans aren't correctly |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 998 | // sorted? It feels better in such a case to match the span's other span |
| 999 | // pointer since both coincident segments must contain the same spans. |
| 1000 | void addTCancel(double startT, double endT, Segment& other, |
| 1001 | double oStartT, double oEndT) { |
| 1002 | SkASSERT(endT - startT >= FLT_EPSILON); |
| 1003 | SkASSERT(oEndT - oStartT >= FLT_EPSILON); |
| 1004 | int index = 0; |
| 1005 | while (startT - fTs[index].fT >= FLT_EPSILON) { |
| 1006 | ++index; |
| 1007 | } |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 1008 | int oIndex = other.fTs.count(); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1009 | while (other.fTs[--oIndex].fT - oEndT > -FLT_EPSILON) |
| 1010 | ; |
| 1011 | Span* test = &fTs[index]; |
| 1012 | Span* oTest = &other.fTs[oIndex]; |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 1013 | SkTDArray<double> outsideTs; |
| 1014 | SkTDArray<double> oOutsideTs; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1015 | do { |
| 1016 | bool decrement = test->fWindValue && oTest->fWindValue; |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1017 | bool track = test->fWindValue || oTest->fWindValue; |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 1018 | double testT = test->fT; |
| 1019 | double oTestT = oTest->fT; |
| 1020 | Span* span = test; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1021 | do { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1022 | if (decrement) { |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 1023 | decrementSpan(span); |
| 1024 | } else if (track && span->fT < 1 && oTestT < 1) { |
| 1025 | TrackOutside(outsideTs, span->fT, oTestT); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1026 | } |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 1027 | span = &fTs[++index]; |
| 1028 | } while (span->fT - testT < FLT_EPSILON); |
| 1029 | Span* oSpan = oTest; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1030 | do { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1031 | if (decrement) { |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 1032 | other.decrementSpan(oSpan); |
| 1033 | } else if (track && oSpan->fT < 1 && testT < 1) { |
| 1034 | TrackOutside(oOutsideTs, oSpan->fT, testT); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1035 | } |
| 1036 | if (!oIndex) { |
| 1037 | break; |
| 1038 | } |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 1039 | oSpan = &other.fTs[--oIndex]; |
| 1040 | } while (oTestT - oSpan->fT < FLT_EPSILON); |
| 1041 | test = span; |
| 1042 | oTest = oSpan; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1043 | } while (test->fT < endT - FLT_EPSILON); |
| 1044 | SkASSERT(!oIndex || oTest->fT <= oStartT - FLT_EPSILON); |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 1045 | // FIXME: determine if canceled edges need outside ts added |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1046 | if (!done() && outsideTs.count()) { |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1047 | double tStart = outsideTs[0]; |
| 1048 | double oStart = outsideTs[1]; |
| 1049 | addCancelOutsides(tStart, oStart, other, oEndT); |
| 1050 | int count = outsideTs.count(); |
| 1051 | if (count > 2) { |
| 1052 | double tStart = outsideTs[count - 2]; |
| 1053 | double oStart = outsideTs[count - 1]; |
| 1054 | addCancelOutsides(tStart, oStart, other, oEndT); |
| 1055 | } |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 1056 | } |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1057 | if (!other.done() && oOutsideTs.count()) { |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1058 | double tStart = oOutsideTs[0]; |
| 1059 | double oStart = oOutsideTs[1]; |
| 1060 | other.addCancelOutsides(tStart, oStart, *this, endT); |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 1061 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | // set spans from start to end to increment the greater by one and decrement |
| 1065 | // the lesser |
| 1066 | void addTCoincident(double startT, double endT, Segment& other, |
| 1067 | double oStartT, double oEndT) { |
| 1068 | SkASSERT(endT - startT >= FLT_EPSILON); |
| 1069 | SkASSERT(oEndT - oStartT >= FLT_EPSILON); |
| 1070 | int index = 0; |
| 1071 | while (startT - fTs[index].fT >= FLT_EPSILON) { |
| 1072 | ++index; |
| 1073 | } |
| 1074 | int oIndex = 0; |
| 1075 | while (oStartT - other.fTs[oIndex].fT >= FLT_EPSILON) { |
| 1076 | ++oIndex; |
| 1077 | } |
| 1078 | Span* test = &fTs[index]; |
| 1079 | Span* oTest = &other.fTs[oIndex]; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1080 | SkTDArray<double> outsideTs; |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1081 | SkTDArray<double> xOutsideTs; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1082 | SkTDArray<double> oOutsideTs; |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1083 | SkTDArray<double> oxOutsideTs; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1084 | do { |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 1085 | bool transfer = test->fWindValue && oTest->fWindValue; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1086 | bool decrementOther = test->fWindValue >= oTest->fWindValue; |
| 1087 | Span* end = test; |
| 1088 | double startT = end->fT; |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1089 | int startIndex = index; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1090 | double oStartT = oTest->fT; |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1091 | int oStartIndex = oIndex; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1092 | do { |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 1093 | if (transfer) { |
| 1094 | if (decrementOther) { |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 1095 | SkASSERT(abs(end->fWindValue) <= gDebugMaxWindValue); |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 1096 | ++(end->fWindValue); |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 1097 | } else if (decrementSpan(end)) { |
| 1098 | TrackOutside(outsideTs, end->fT, oStartT); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1099 | } |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1100 | } else if (oTest->fWindValue) { |
| 1101 | SkASSERT(!decrementOther); |
| 1102 | if (startIndex > 0 && fTs[startIndex - 1].fWindValue) { |
| 1103 | TrackOutside(xOutsideTs, end->fT, oStartT); |
| 1104 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1105 | } |
| 1106 | end = &fTs[++index]; |
| 1107 | } while (end->fT - test->fT < FLT_EPSILON); |
| 1108 | Span* oEnd = oTest; |
| 1109 | do { |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 1110 | if (transfer) { |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 1111 | if (!decrementOther) { |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 1112 | SkASSERT(abs(oEnd->fWindValue) <= gDebugMaxWindValue); |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 1113 | ++(oEnd->fWindValue); |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 1114 | } else if (other.decrementSpan(oEnd)) { |
| 1115 | TrackOutside(oOutsideTs, oEnd->fT, startT); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1116 | } |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1117 | } else if (test->fWindValue) { |
| 1118 | SkASSERT(!decrementOther); |
| 1119 | if (oStartIndex > 0 && other.fTs[oStartIndex - 1].fWindValue) { |
| 1120 | SkASSERT(0); // track for later? |
| 1121 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1122 | } |
| 1123 | oEnd = &other.fTs[++oIndex]; |
| 1124 | } while (oEnd->fT - oTest->fT < FLT_EPSILON); |
| 1125 | test = end; |
| 1126 | oTest = oEnd; |
| 1127 | } while (test->fT < endT - FLT_EPSILON); |
| 1128 | SkASSERT(oTest->fT < oEndT + FLT_EPSILON); |
| 1129 | SkASSERT(oTest->fT > oEndT - FLT_EPSILON); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1130 | if (!done()) { |
| 1131 | if (outsideTs.count()) { |
| 1132 | addCoinOutsides(outsideTs, other, oEndT); |
| 1133 | } |
| 1134 | if (xOutsideTs.count()) { |
| 1135 | addCoinOutsides(xOutsideTs, other, oEndT); |
| 1136 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1137 | } |
| 1138 | if (!other.done() && oOutsideTs.count()) { |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1139 | other.addCoinOutsides(oOutsideTs, *this, endT); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1140 | } |
| 1141 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1142 | |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1143 | // FIXME: this doesn't prevent the same span from being added twice |
| 1144 | // fix in caller, assert here? |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1145 | void addTPair(double t, Segment& other, double otherT, bool borrowWind) { |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1146 | int tCount = fTs.count(); |
| 1147 | for (int tIndex = 0; tIndex < tCount; ++tIndex) { |
| 1148 | const Span& span = fTs[tIndex]; |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1149 | if (span.fT - t >= FLT_EPSILON) { |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1150 | break; |
| 1151 | } |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1152 | if (span.fT - t < FLT_EPSILON && span.fOther == &other && span.fOtherT == otherT) { |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1153 | #if DEBUG_ADD_T_PAIR |
| 1154 | SkDebugf("%s addTPair duplicate this=%d %1.9g other=%d %1.9g\n", |
| 1155 | __FUNCTION__, fID, t, other.fID, otherT); |
| 1156 | #endif |
| 1157 | return; |
| 1158 | } |
| 1159 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1160 | #if DEBUG_ADD_T_PAIR |
| 1161 | SkDebugf("%s addTPair this=%d %1.9g other=%d %1.9g\n", |
| 1162 | __FUNCTION__, fID, t, other.fID, otherT); |
| 1163 | #endif |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 1164 | int insertedAt = addT(t, &other); |
| 1165 | int otherInsertedAt = other.addT(otherT, this); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1166 | addOtherT(insertedAt, otherT, otherInsertedAt); |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 1167 | other.addOtherT(otherInsertedAt, t, insertedAt); |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1168 | matchWindingValue(insertedAt, t, borrowWind); |
| 1169 | other.matchWindingValue(otherInsertedAt, otherT, borrowWind); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1170 | } |
caryclark@google.com | 0c803d0 | 2012-08-06 11:15:47 +0000 | [diff] [blame] | 1171 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1172 | void addTwoAngles(int start, int end, SkTDArray<Angle>& angles) const { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1173 | // add edge leading into junction |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1174 | if (fTs[SkMin32(end, start)].fWindValue > 0) { |
| 1175 | addAngle(angles, end, start); |
| 1176 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1177 | // add edge leading away from junction |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1178 | int step = SkSign32(end - start); |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1179 | int tIndex = nextSpan(end, step); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1180 | if (tIndex >= 0 && fTs[SkMin32(end, tIndex)].fWindValue > 0) { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1181 | addAngle(angles, end, tIndex); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1182 | } |
| 1183 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1184 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1185 | const Bounds& bounds() const { |
| 1186 | return fBounds; |
| 1187 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1188 | |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1189 | void buildAngles(int index, SkTDArray<Angle>& angles) const { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1190 | double referenceT = fTs[index].fT; |
| 1191 | int lesser = index; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1192 | while (--lesser >= 0 && referenceT - fTs[lesser].fT < FLT_EPSILON) { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1193 | buildAnglesInner(lesser, angles); |
| 1194 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1195 | do { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1196 | buildAnglesInner(index, angles); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1197 | } while (++index < fTs.count() && fTs[index].fT - referenceT < FLT_EPSILON); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1198 | } |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1199 | |
| 1200 | void buildAnglesInner(int index, SkTDArray<Angle>& angles) const { |
| 1201 | Span* span = &fTs[index]; |
| 1202 | Segment* other = span->fOther; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1203 | // if there is only one live crossing, and no coincidence, continue |
| 1204 | // in the same direction |
| 1205 | // if there is coincidence, the only choice may be to reverse direction |
| 1206 | // find edge on either side of intersection |
| 1207 | int oIndex = span->fOtherIndex; |
| 1208 | // if done == -1, prior span has already been processed |
| 1209 | int step = 1; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1210 | int next = other->nextSpan(oIndex, step); |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1211 | if (next < 0) { |
| 1212 | step = -step; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1213 | next = other->nextSpan(oIndex, step); |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1214 | } |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1215 | // add candidate into and away from junction |
| 1216 | other->addTwoAngles(next, oIndex, angles); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1219 | bool cancels(const Segment& other) const { |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 1220 | SkASSERT(fVerb == SkPath::kLine_Verb); |
| 1221 | SkASSERT(other.fVerb == SkPath::kLine_Verb); |
| 1222 | SkPoint dxy = fPts[0] - fPts[1]; |
| 1223 | SkPoint odxy = other.fPts[0] - other.fPts[1]; |
| 1224 | return dxy.fX * odxy.fX < 0 || dxy.fY * odxy.fY < 0; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1225 | } |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1226 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1227 | // figure out if the segment's ascending T goes clockwise or not |
| 1228 | // not enough context to write this as shown |
| 1229 | // instead, add all segments meeting at the top |
| 1230 | // sort them using buildAngleList |
| 1231 | // find the first in the sort |
| 1232 | // see if ascendingT goes to top |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 1233 | bool clockwise(int /* tIndex */) const { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1234 | SkASSERT(0); // incomplete |
| 1235 | return false; |
| 1236 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1237 | |
| 1238 | int computeSum(int startIndex, int endIndex) { |
| 1239 | SkTDArray<Angle> angles; |
| 1240 | addTwoAngles(startIndex, endIndex, angles); |
| 1241 | buildAngles(endIndex, angles); |
| 1242 | SkTDArray<Angle*> sorted; |
| 1243 | sortAngles(angles, sorted); |
| 1244 | int angleCount = angles.count(); |
| 1245 | const Angle* angle; |
| 1246 | const Segment* base; |
| 1247 | int winding; |
| 1248 | int firstIndex = 0; |
| 1249 | do { |
| 1250 | angle = sorted[firstIndex]; |
| 1251 | base = angle->segment(); |
| 1252 | winding = base->windSum(angle); |
| 1253 | if (winding != SK_MinS32) { |
| 1254 | break; |
| 1255 | } |
| 1256 | if (++firstIndex == angleCount) { |
| 1257 | return SK_MinS32; |
| 1258 | } |
| 1259 | } while (true); |
| 1260 | // turn winding into contourWinding |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1261 | int spanWinding = base->spanSign(angle); |
| 1262 | bool inner = useInnerWinding(winding + spanWinding, winding); |
| 1263 | #if DEBUG_WINDING |
| 1264 | SkDebugf("%s --- spanWinding=%d winding=%d sign=%d inner=%d outside=%d result=%d\n", __FUNCTION__, |
| 1265 | spanWinding, winding, angle->sign(), inner, base->spanOutside(angle->start(), angle->end()), |
| 1266 | inner ? winding + spanWinding : winding); |
| 1267 | #endif |
| 1268 | if (inner) { |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1269 | winding += spanWinding; |
| 1270 | } |
| 1271 | #if DEBUG_SORT |
| 1272 | base->debugShowSort(sorted, firstIndex, winding); |
| 1273 | #endif |
| 1274 | int nextIndex = firstIndex + 1; |
| 1275 | int lastIndex = firstIndex != 0 ? firstIndex : angleCount; |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1276 | winding -= base->spanSign(angle); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1277 | do { |
| 1278 | if (nextIndex == angleCount) { |
| 1279 | nextIndex = 0; |
| 1280 | } |
| 1281 | angle = sorted[nextIndex]; |
| 1282 | Segment* segment = angle->segment(); |
| 1283 | int maxWinding = winding; |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1284 | winding -= segment->spanSign(angle); |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 1285 | if (segment->windSum(angle) == SK_MinS32) { |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1286 | bool inside = useInnerWinding(maxWinding, winding); |
| 1287 | if (inside) { |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1288 | maxWinding = winding; |
| 1289 | } |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1290 | segment->markAndChaseWinding(angle, maxWinding, !inside); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1291 | } |
| 1292 | } while (++nextIndex != lastIndex); |
| 1293 | return windSum(SkMin32(startIndex, endIndex)); |
| 1294 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1295 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1296 | int crossedSpan(const SkPoint& basePt, SkScalar& bestY, double& hitT) const { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1297 | int bestT = -1; |
| 1298 | SkScalar top = bounds().fTop; |
| 1299 | SkScalar bottom = bounds().fBottom; |
caryclark@google.com | 210acaf | 2012-07-12 21:05:13 +0000 | [diff] [blame] | 1300 | int end = 0; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1301 | do { |
caryclark@google.com | 210acaf | 2012-07-12 21:05:13 +0000 | [diff] [blame] | 1302 | int start = end; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1303 | end = nextSpan(start, 1); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1304 | if (fTs[start].fWindValue == 0) { |
| 1305 | continue; |
| 1306 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1307 | SkPoint edge[4]; |
| 1308 | // OPTIMIZE: wrap this so that if start==0 end==fTCount-1 we can |
| 1309 | // work with the original data directly |
| 1310 | (*SegmentSubDivide[fVerb])(fPts, fTs[start].fT, fTs[end].fT, edge); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 1311 | // intersect ray starting at basePt with edge |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1312 | Intersections intersections; |
| 1313 | int pts = (*VSegmentIntersect[fVerb])(edge, top, bottom, basePt.fX, |
| 1314 | false, intersections); |
| 1315 | if (pts == 0) { |
| 1316 | continue; |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1317 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1318 | if (pts > 1 && fVerb == SkPath::kLine_Verb) { |
| 1319 | // if the intersection is edge on, wait for another one |
| 1320 | continue; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1321 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1322 | SkASSERT(pts == 1); // FIXME: more code required to disambiguate |
| 1323 | SkPoint pt; |
| 1324 | double foundT = intersections.fT[0][0]; |
| 1325 | (*SegmentXYAtT[fVerb])(fPts, foundT, &pt); |
| 1326 | if (bestY < pt.fY) { |
| 1327 | bestY = pt.fY; |
| 1328 | bestT = foundT < 1 ? start : end; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1329 | hitT = fTs[start].fT + (fTs[end].fT - fTs[start].fT) * foundT; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1330 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1331 | } while (fTs[end].fT != 1); |
| 1332 | return bestT; |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1333 | } |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 1334 | |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1335 | bool crossedSpanHalves(const SkPoint& basePt, bool leftHalf, bool rightHalf) { |
| 1336 | // if a segment is connected to this one, consider it crossing |
| 1337 | int tIndex; |
| 1338 | if (fPts[0].fX == basePt.fX) { |
| 1339 | tIndex = 0; |
| 1340 | do { |
| 1341 | const Span& sSpan = fTs[tIndex]; |
| 1342 | const Segment* sOther = sSpan.fOther; |
| 1343 | if (!sOther->fTs[sSpan.fOtherIndex].fWindValue) { |
| 1344 | continue; |
| 1345 | } |
| 1346 | if (leftHalf ? sOther->fBounds.fLeft < basePt.fX |
| 1347 | : sOther->fBounds.fRight > basePt.fX) { |
| 1348 | return true; |
| 1349 | } |
| 1350 | } while (fTs[++tIndex].fT == 0); |
| 1351 | } |
| 1352 | if (fPts[fVerb].fX == basePt.fX) { |
| 1353 | tIndex = fTs.count() - 1; |
| 1354 | do { |
| 1355 | const Span& eSpan = fTs[tIndex]; |
| 1356 | const Segment* eOther = eSpan.fOther; |
| 1357 | if (!eOther->fTs[eSpan.fOtherIndex].fWindValue) { |
| 1358 | continue; |
| 1359 | } |
| 1360 | if (leftHalf ? eOther->fBounds.fLeft < basePt.fX |
| 1361 | : eOther->fBounds.fRight > basePt.fX) { |
| 1362 | return true; |
| 1363 | } |
| 1364 | } while (fTs[--tIndex].fT == 1); |
| 1365 | } |
| 1366 | return false; |
| 1367 | } |
| 1368 | |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 1369 | bool decrementSpan(Span* span) { |
| 1370 | SkASSERT(span->fWindValue > 0); |
| 1371 | if (--(span->fWindValue) == 0) { |
| 1372 | span->fDone = true; |
| 1373 | ++fDoneSpans; |
| 1374 | return true; |
| 1375 | } |
| 1376 | return false; |
| 1377 | } |
| 1378 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1379 | bool done() const { |
caryclark@google.com | af46cff | 2012-05-22 21:12:00 +0000 | [diff] [blame] | 1380 | SkASSERT(fDoneSpans <= fTs.count()); |
| 1381 | return fDoneSpans == fTs.count(); |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1384 | bool done(const Angle& angle) const { |
| 1385 | int start = angle.start(); |
| 1386 | int end = angle.end(); |
| 1387 | const Span& mSpan = fTs[SkMin32(start, end)]; |
| 1388 | return mSpan.fDone; |
| 1389 | } |
| 1390 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1391 | // so the span needs to contain the pairing info found here |
| 1392 | // this should include the winding computed for the edge, and |
| 1393 | // what edge it connects to, and whether it is discarded |
| 1394 | // (maybe discarded == abs(winding) > 1) ? |
| 1395 | // only need derivatives for duration of sorting, add a new struct |
| 1396 | // for pairings, remove extra spans that have zero length and |
| 1397 | // reference an unused other |
| 1398 | // for coincident, the last span on the other may be marked done |
| 1399 | // (always?) |
| 1400 | |
| 1401 | // if loop is exhausted, contour may be closed. |
| 1402 | // FIXME: pass in close point so we can check for closure |
| 1403 | |
| 1404 | // given a segment, and a sense of where 'inside' is, return the next |
| 1405 | // segment. If this segment has an intersection, or ends in multiple |
| 1406 | // segments, find the mate that continues the outside. |
| 1407 | // note that if there are multiples, but no coincidence, we can limit |
| 1408 | // choices to connections in the correct direction |
| 1409 | |
| 1410 | // mark found segments as done |
| 1411 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1412 | // start is the index of the beginning T of this edge |
| 1413 | // it is guaranteed to have an end which describes a non-zero length (?) |
| 1414 | // winding -1 means ccw, 1 means cw |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1415 | // firstFind allows coincident edges to be treated differently |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 1416 | Segment* findNext(SkTDArray<Span*>& chase, bool firstFind, bool active, |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 1417 | const int startIndex, const int endIndex, int& nextStart, |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 1418 | int& nextEnd, int& winding, int& spanWinding) { |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1419 | int outerWinding = winding; |
| 1420 | int innerWinding = winding + spanWinding; |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 1421 | #if DEBUG_WINDING |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1422 | SkDebugf("%s winding=%d spanWinding=%d outerWinding=%d innerWinding=%d\n", |
| 1423 | __FUNCTION__, winding, spanWinding, outerWinding, innerWinding); |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 1424 | #endif |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1425 | bool inside = useInnerWinding(outerWinding, innerWinding); |
| 1426 | if (inside) { |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1427 | outerWinding = innerWinding; |
| 1428 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1429 | SkASSERT(startIndex != endIndex); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1430 | int count = fTs.count(); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1431 | SkASSERT(startIndex < endIndex ? startIndex < count - 1 |
| 1432 | : startIndex > 0); |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1433 | int step = SkSign32(endIndex - startIndex); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1434 | int end = nextSpan(startIndex, step); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1435 | SkASSERT(end >= 0); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1436 | Span* endSpan = &fTs[end]; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1437 | Segment* other; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1438 | if (isSimple(end)) { |
caryclark@google.com | af46cff | 2012-05-22 21:12:00 +0000 | [diff] [blame] | 1439 | // mark the smaller of startIndex, endIndex done, and all adjacent |
| 1440 | // spans with the same T value (but not 'other' spans) |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1441 | #if DEBUG_WINDING |
| 1442 | SkDebugf("%s simple\n", __FUNCTION__); |
| 1443 | #endif |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1444 | markDone(SkMin32(startIndex, endIndex), outerWinding, !inside); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1445 | other = endSpan->fOther; |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1446 | nextStart = endSpan->fOtherIndex; |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 1447 | double startT = other->fTs[nextStart].fT; |
| 1448 | nextEnd = nextStart; |
| 1449 | do { |
| 1450 | nextEnd += step; |
| 1451 | } while (fabs(startT - other->fTs[nextEnd].fT) < FLT_EPSILON); |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1452 | SkASSERT(step < 0 ? nextEnd >= 0 : nextEnd < other->fTs.count()); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1453 | return other; |
| 1454 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1455 | // more than one viable candidate -- measure angles to find best |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1456 | SkTDArray<Angle> angles; |
caryclark@google.com | af46cff | 2012-05-22 21:12:00 +0000 | [diff] [blame] | 1457 | SkASSERT(startIndex - endIndex != 0); |
| 1458 | SkASSERT((startIndex - endIndex < 0) ^ (step < 0)); |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1459 | addTwoAngles(startIndex, end, angles); |
| 1460 | buildAngles(end, angles); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1461 | SkTDArray<Angle*> sorted; |
| 1462 | sortAngles(angles, sorted); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1463 | int angleCount = angles.count(); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 1464 | int firstIndex = findStartingEdge(sorted, startIndex, end); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1465 | SkASSERT(firstIndex >= 0); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1466 | #if DEBUG_SORT |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1467 | debugShowSort(sorted, firstIndex, winding); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1468 | #endif |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1469 | SkASSERT(sorted[firstIndex]->segment() == this); |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 1470 | #if DEBUG_WINDING |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1471 | SkDebugf("%s sign=%d\n", __FUNCTION__, sorted[firstIndex]->sign()); |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 1472 | #endif |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1473 | int sumWinding = winding - spanSign(sorted[firstIndex]); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1474 | int nextIndex = firstIndex + 1; |
| 1475 | int lastIndex = firstIndex != 0 ? firstIndex : angleCount; |
| 1476 | const Angle* foundAngle = NULL; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1477 | bool foundDone = false; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1478 | // iterate through the angle, and compute everyone's winding |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1479 | int toggleWinding = SK_MinS32; |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 1480 | bool flipFound = false; |
| 1481 | int flipped = 1; |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 1482 | Segment* nextSegment; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1483 | do { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1484 | if (nextIndex == angleCount) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1485 | nextIndex = 0; |
| 1486 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1487 | const Angle* nextAngle = sorted[nextIndex]; |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 1488 | int maxWinding = sumWinding; |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 1489 | nextSegment = nextAngle->segment(); |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1490 | sumWinding -= nextSegment->spanSign(nextAngle); |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 1491 | SkASSERT(abs(sumWinding) <= gDebugMaxWindSum); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 1492 | #if DEBUG_WINDING |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 1493 | SkDebugf("%s maxWinding=%d sumWinding=%d sign=%d\n", __FUNCTION__, |
| 1494 | maxWinding, sumWinding, nextAngle->sign()); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 1495 | #endif |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 1496 | if (maxWinding * sumWinding < 0) { |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 1497 | flipFound ^= true; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1498 | #if DEBUG_WINDING |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1499 | SkDebugf("%s flipFound=%d maxWinding=%d sumWinding=%d\n", |
| 1500 | __FUNCTION__, flipFound, maxWinding, sumWinding); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1501 | #endif |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 1502 | } |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 1503 | if (!sumWinding) { |
caryclark@google.com | 5c286d3 | 2012-07-13 11:57:28 +0000 | [diff] [blame] | 1504 | if (!active) { |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1505 | markDone(SkMin32(startIndex, endIndex), outerWinding, !inside); |
| 1506 | // FIXME: seems like a bug that this isn't calling userInnerWinding |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1507 | nextSegment->markWinding(SkMin32(nextAngle->start(), |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1508 | nextAngle->end()), maxWinding, true); |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 1509 | #if DEBUG_WINDING |
caryclark@google.com | 5c286d3 | 2012-07-13 11:57:28 +0000 | [diff] [blame] | 1510 | SkDebugf("%s inactive\n", __FUNCTION__); |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 1511 | #endif |
caryclark@google.com | 5c286d3 | 2012-07-13 11:57:28 +0000 | [diff] [blame] | 1512 | return NULL; |
| 1513 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1514 | if (!foundAngle || foundDone) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1515 | foundAngle = nextAngle; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1516 | foundDone = nextSegment->done(*nextAngle); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1517 | if (flipFound || (maxWinding * outerWinding < 0)) { |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1518 | flipped = -flipped; |
| 1519 | #if DEBUG_WINDING |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1520 | SkDebugf("%s flipped=%d flipFound=%d maxWinding=%d" |
| 1521 | " outerWinding=%d\n", __FUNCTION__, flipped, |
| 1522 | flipFound, maxWinding, outerWinding); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1523 | #endif |
| 1524 | } |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1525 | } |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 1526 | continue; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1527 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1528 | if (!maxWinding && !foundAngle) { |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 1529 | #if DEBUG_WINDING |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1530 | if (flipped > 0) { |
| 1531 | SkDebugf("%s sumWinding=%d * outerWinding=%d < 0 (%s)\n", |
| 1532 | __FUNCTION__, sumWinding, outerWinding, |
| 1533 | sumWinding * outerWinding < 0 ? "true" : "false"); |
| 1534 | } |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 1535 | #endif |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1536 | if (sumWinding * outerWinding < 0 && flipped > 0) { |
| 1537 | #if DEBUG_WINDING |
| 1538 | SkDebugf("%s toggleWinding=%d\n", __FUNCTION__, sumWinding); |
| 1539 | #endif |
| 1540 | toggleWinding = sumWinding; |
| 1541 | } else if (outerWinding != sumWinding) { |
| 1542 | #if DEBUG_WINDING |
| 1543 | SkDebugf("%s outerWinding=%d != sumWinding=%d winding=%d\n", |
| 1544 | __FUNCTION__, outerWinding, sumWinding, winding); |
| 1545 | #endif |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 1546 | winding = sumWinding; |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1547 | } |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 1548 | foundAngle = nextAngle; |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 1549 | if (flipFound) { |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1550 | flipped = -flipped; |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 1551 | #if DEBUG_WINDING |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1552 | SkDebugf("%s flipped flipFound=%d\n", __FUNCTION__, flipFound); |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 1553 | #endif |
| 1554 | } |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 1555 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1556 | if (nextSegment->done()) { |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 1557 | continue; |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1558 | } |
| 1559 | // if the winding is non-zero, nextAngle does not connect to |
| 1560 | // current chain. If we haven't done so already, mark the angle |
| 1561 | // as done, record the winding value, and mark connected unambiguous |
| 1562 | // segments as well. |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 1563 | if (nextSegment->windSum(nextAngle) == SK_MinS32) { |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1564 | bool inside = useInnerWinding(maxWinding, sumWinding); |
| 1565 | if (inside) { |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 1566 | maxWinding = sumWinding; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1567 | } |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 1568 | Span* last; |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1569 | if (foundAngle) { |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1570 | last = nextSegment->markAndChaseWinding(nextAngle, maxWinding, !inside); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1571 | } else { |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1572 | last = nextSegment->markAndChaseDone(nextAngle, maxWinding, !inside); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 1573 | } |
| 1574 | if (last) { |
| 1575 | *chase.append() = last; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1576 | } |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1577 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1578 | } while (++nextIndex != lastIndex); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1579 | SkASSERT(sorted[firstIndex]->segment() == this); |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1580 | markDone(SkMin32(startIndex, endIndex), outerWinding, !inside); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1581 | if (!foundAngle) { |
| 1582 | return NULL; |
| 1583 | } |
| 1584 | nextStart = foundAngle->start(); |
| 1585 | nextEnd = foundAngle->end(); |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 1586 | nextSegment = foundAngle->segment(); |
| 1587 | spanWinding = SkSign32(spanWinding) * flipped * nextSegment->windValue( |
| 1588 | SkMin32(nextStart, nextEnd)); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1589 | if (toggleWinding != SK_MinS32) { |
| 1590 | winding = toggleWinding; |
| 1591 | spanWinding = -spanWinding; |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 1592 | } |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 1593 | #if DEBUG_WINDING |
| 1594 | SkDebugf("%s spanWinding=%d\n", __FUNCTION__, spanWinding); |
| 1595 | #endif |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 1596 | return nextSegment; |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1597 | } |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 1598 | |
| 1599 | int findStartingEdge(SkTDArray<Angle*>& sorted, int start, int end) { |
| 1600 | int angleCount = sorted.count(); |
| 1601 | int firstIndex = -1; |
| 1602 | for (int angleIndex = 0; angleIndex < angleCount; ++angleIndex) { |
| 1603 | const Angle* angle = sorted[angleIndex]; |
| 1604 | if (angle->segment() == this && angle->start() == end && |
| 1605 | angle->end() == start) { |
| 1606 | firstIndex = angleIndex; |
| 1607 | break; |
| 1608 | } |
| 1609 | } |
| 1610 | return firstIndex; |
| 1611 | } |
| 1612 | |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1613 | // FIXME: this is tricky code; needs its own unit test |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 1614 | void findTooCloseToCall(int /* winding */ ) { // FIXME: winding should be considered |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1615 | int count = fTs.count(); |
| 1616 | if (count < 3) { // require t=0, x, 1 at minimum |
| 1617 | return; |
| 1618 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1619 | int matchIndex = 0; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1620 | int moCount; |
| 1621 | Span* match; |
| 1622 | Segment* mOther; |
| 1623 | do { |
| 1624 | match = &fTs[matchIndex]; |
| 1625 | mOther = match->fOther; |
| 1626 | moCount = mOther->fTs.count(); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1627 | if (moCount >= 3) { |
| 1628 | break; |
| 1629 | } |
| 1630 | if (++matchIndex >= count) { |
| 1631 | return; |
| 1632 | } |
| 1633 | } while (true); // require t=0, x, 1 at minimum |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1634 | // OPTIMIZATION: defer matchPt until qualifying toCount is found? |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1635 | const SkPoint* matchPt = &xyAtT(match); |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1636 | // look for a pair of nearby T values that map to the same (x,y) value |
| 1637 | // if found, see if the pair of other segments share a common point. If |
| 1638 | // so, the span from here to there is coincident. |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1639 | for (int index = matchIndex + 1; index < count; ++index) { |
| 1640 | Span* test = &fTs[index]; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1641 | if (test->fDone) { |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1642 | continue; |
| 1643 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1644 | Segment* tOther = test->fOther; |
| 1645 | int toCount = tOther->fTs.count(); |
| 1646 | if (toCount < 3) { // require t=0, x, 1 at minimum |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1647 | continue; |
| 1648 | } |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1649 | const SkPoint* testPt = &xyAtT(test); |
| 1650 | if (*matchPt != *testPt) { |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1651 | matchIndex = index; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1652 | moCount = toCount; |
| 1653 | match = test; |
| 1654 | mOther = tOther; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1655 | matchPt = testPt; |
| 1656 | continue; |
| 1657 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1658 | int moStart = -1; |
| 1659 | int moEnd = -1; |
| 1660 | double moStartT, moEndT; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1661 | for (int moIndex = 0; moIndex < moCount; ++moIndex) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1662 | Span& moSpan = mOther->fTs[moIndex]; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1663 | if (moSpan.fDone) { |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1664 | continue; |
| 1665 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1666 | if (moSpan.fOther == this) { |
| 1667 | if (moSpan.fOtherT == match->fT) { |
| 1668 | moStart = moIndex; |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1669 | moStartT = moSpan.fT; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1670 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1671 | continue; |
| 1672 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1673 | if (moSpan.fOther == tOther) { |
| 1674 | SkASSERT(moEnd == -1); |
| 1675 | moEnd = moIndex; |
| 1676 | moEndT = moSpan.fT; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1677 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1678 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1679 | if (moStart < 0 || moEnd < 0) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1680 | continue; |
| 1681 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1682 | // FIXME: if moStartT, moEndT are initialized to NaN, can skip this test |
| 1683 | if (moStartT == moEndT) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1684 | continue; |
| 1685 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1686 | int toStart = -1; |
| 1687 | int toEnd = -1; |
| 1688 | double toStartT, toEndT; |
| 1689 | for (int toIndex = 0; toIndex < toCount; ++toIndex) { |
| 1690 | Span& toSpan = tOther->fTs[toIndex]; |
| 1691 | if (toSpan.fOther == this) { |
| 1692 | if (toSpan.fOtherT == test->fT) { |
| 1693 | toStart = toIndex; |
| 1694 | toStartT = toSpan.fT; |
| 1695 | } |
| 1696 | continue; |
| 1697 | } |
| 1698 | if (toSpan.fOther == mOther && toSpan.fOtherT == moEndT) { |
| 1699 | SkASSERT(toEnd == -1); |
| 1700 | toEnd = toIndex; |
| 1701 | toEndT = toSpan.fT; |
| 1702 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1703 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1704 | // FIXME: if toStartT, toEndT are initialized to NaN, can skip this test |
| 1705 | if (toStart <= 0 || toEnd <= 0) { |
| 1706 | continue; |
| 1707 | } |
| 1708 | if (toStartT == toEndT) { |
| 1709 | continue; |
| 1710 | } |
| 1711 | // test to see if the segment between there and here is linear |
| 1712 | if (!mOther->isLinear(moStart, moEnd) |
| 1713 | || !tOther->isLinear(toStart, toEnd)) { |
| 1714 | continue; |
| 1715 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1716 | // FIXME: defer implementation until the rest works |
| 1717 | // this may share code with regular coincident detection |
| 1718 | SkASSERT(0); |
| 1719 | #if 0 |
| 1720 | if (flipped) { |
| 1721 | mOther->addTCancel(moStart, moEnd, tOther, tStart, tEnd); |
| 1722 | } else { |
| 1723 | mOther->addTCoincident(moStart, moEnd, tOther, tStart, tEnd); |
| 1724 | } |
| 1725 | #endif |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1726 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1729 | // OPTIMIZATION : for a pair of lines, can we compute points at T (cached) |
| 1730 | // and use more concise logic like the old edge walker code? |
| 1731 | // FIXME: this needs to deal with coincident edges |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1732 | Segment* findTop(int& tIndex, int& endIndex) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1733 | // iterate through T intersections and return topmost |
| 1734 | // topmost tangent from y-min to first pt is closer to horizontal |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 1735 | SkASSERT(!done()); |
| 1736 | int firstT; |
| 1737 | int lastT; |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 1738 | SkPoint topPt; |
| 1739 | topPt.fY = SK_ScalarMax; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1740 | int count = fTs.count(); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1741 | // see if either end is not done since we want smaller Y of the pair |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 1742 | bool lastDone = true; |
| 1743 | for (int index = 0; index < count; ++index) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1744 | const Span& span = fTs[index]; |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 1745 | if (!span.fDone || !lastDone) { |
| 1746 | const SkPoint& intercept = xyAtT(&span); |
| 1747 | if (topPt.fY > intercept.fY || (topPt.fY == intercept.fY |
| 1748 | && topPt.fX > intercept.fX)) { |
| 1749 | topPt = intercept; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1750 | firstT = lastT = index; |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 1751 | } else if (topPt == intercept) { |
| 1752 | lastT = index; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1753 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1754 | } |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 1755 | lastDone = span.fDone; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1756 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1757 | // sort the edges to find the leftmost |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1758 | int step = 1; |
| 1759 | int end = nextSpan(firstT, step); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1760 | if (end == -1) { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1761 | step = -1; |
| 1762 | end = nextSpan(firstT, step); |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 1763 | SkASSERT(end != -1); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1764 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1765 | // if the topmost T is not on end, or is three-way or more, find left |
| 1766 | // look for left-ness from tLeft to firstT (matching y of other) |
| 1767 | SkTDArray<Angle> angles; |
| 1768 | SkASSERT(firstT - end != 0); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1769 | addTwoAngles(end, firstT, angles); |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1770 | buildAngles(firstT, angles); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1771 | SkTDArray<Angle*> sorted; |
| 1772 | sortAngles(angles, sorted); |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 1773 | // skip edges that have already been processed |
| 1774 | firstT = -1; |
| 1775 | Segment* leftSegment; |
| 1776 | do { |
| 1777 | const Angle* angle = sorted[++firstT]; |
| 1778 | leftSegment = angle->segment(); |
| 1779 | tIndex = angle->end(); |
| 1780 | endIndex = angle->start(); |
| 1781 | } while (leftSegment->fTs[SkMin32(tIndex, endIndex)].fDone); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1782 | return leftSegment; |
| 1783 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1784 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1785 | // FIXME: not crazy about this |
| 1786 | // when the intersections are performed, the other index is into an |
| 1787 | // incomplete array. as the array grows, the indices become incorrect |
| 1788 | // while the following fixes the indices up again, it isn't smart about |
| 1789 | // skipping segments whose indices are already correct |
| 1790 | // assuming we leave the code that wrote the index in the first place |
| 1791 | void fixOtherTIndex() { |
| 1792 | int iCount = fTs.count(); |
| 1793 | for (int i = 0; i < iCount; ++i) { |
| 1794 | Span& iSpan = fTs[i]; |
| 1795 | double oT = iSpan.fOtherT; |
| 1796 | Segment* other = iSpan.fOther; |
| 1797 | int oCount = other->fTs.count(); |
| 1798 | for (int o = 0; o < oCount; ++o) { |
| 1799 | Span& oSpan = other->fTs[o]; |
| 1800 | if (oT == oSpan.fT && this == oSpan.fOther) { |
| 1801 | iSpan.fOtherIndex = o; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1802 | break; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1803 | } |
| 1804 | } |
| 1805 | } |
| 1806 | } |
| 1807 | |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1808 | // OPTIMIZATION: uses tail recursion. Unwise? |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1809 | Span* innerChaseDone(int index, int step, int winding, bool outside) { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1810 | int end = nextSpan(index, step); |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 1811 | SkASSERT(end >= 0); |
| 1812 | if (multipleSpans(end)) { |
| 1813 | return &fTs[end]; |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1814 | } |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 1815 | const Span& endSpan = fTs[end]; |
| 1816 | Segment* other = endSpan.fOther; |
| 1817 | index = endSpan.fOtherIndex; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1818 | int otherEnd = other->nextSpan(index, step); |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1819 | Span* last = other->innerChaseDone(index, step, winding, outside); |
| 1820 | other->markDone(SkMin32(index, otherEnd), winding, outside); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 1821 | return last; |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1824 | Span* innerChaseWinding(int index, int step, int winding, bool outside) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1825 | int end = nextSpan(index, step); |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 1826 | SkASSERT(end >= 0); |
| 1827 | if (multipleSpans(end)) { |
| 1828 | return &fTs[end]; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1829 | } |
| 1830 | const Span& endSpan = fTs[end]; |
| 1831 | Segment* other = endSpan.fOther; |
| 1832 | index = endSpan.fOtherIndex; |
| 1833 | int otherEnd = other->nextSpan(index, step); |
| 1834 | int min = SkMin32(index, otherEnd); |
| 1835 | if (other->fTs[min].fWindSum != SK_MinS32) { |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 1836 | SkASSERT(other->fTs[min].fWindSum == winding); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 1837 | return NULL; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1838 | } |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1839 | Span* last = other->innerChaseWinding(index, step, winding, outside); |
| 1840 | other->markWinding(min, winding, outside); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 1841 | return last; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1844 | void init(const SkPoint pts[], SkPath::Verb verb) { |
| 1845 | fPts = pts; |
| 1846 | fVerb = verb; |
caryclark@google.com | af46cff | 2012-05-22 21:12:00 +0000 | [diff] [blame] | 1847 | fDoneSpans = 0; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1850 | bool intersected() const { |
| 1851 | return fTs.count() > 0; |
| 1852 | } |
caryclark@google.com | 7db7c6b | 2012-07-27 21:22:25 +0000 | [diff] [blame] | 1853 | |
| 1854 | bool isConnected(int startIndex, int endIndex) const { |
| 1855 | return fTs[startIndex].fWindSum != SK_MinS32 |
| 1856 | || fTs[endIndex].fWindSum != SK_MinS32; |
| 1857 | } |
| 1858 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1859 | bool isLinear(int start, int end) const { |
| 1860 | if (fVerb == SkPath::kLine_Verb) { |
| 1861 | return true; |
| 1862 | } |
| 1863 | if (fVerb == SkPath::kQuad_Verb) { |
| 1864 | SkPoint qPart[3]; |
| 1865 | QuadSubDivide(fPts, fTs[start].fT, fTs[end].fT, qPart); |
| 1866 | return QuadIsLinear(qPart); |
| 1867 | } else { |
| 1868 | SkASSERT(fVerb == SkPath::kCubic_Verb); |
| 1869 | SkPoint cPart[4]; |
| 1870 | CubicSubDivide(fPts, fTs[start].fT, fTs[end].fT, cPart); |
| 1871 | return CubicIsLinear(cPart); |
| 1872 | } |
| 1873 | } |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 1874 | |
| 1875 | // OPTIMIZE: successive calls could start were the last leaves off |
| 1876 | // or calls could specialize to walk forwards or backwards |
| 1877 | bool isMissing(double startT) const { |
| 1878 | size_t tCount = fTs.count(); |
| 1879 | for (size_t index = 0; index < tCount; ++index) { |
| 1880 | if (fabs(startT - fTs[index].fT) < FLT_EPSILON) { |
| 1881 | return false; |
| 1882 | } |
| 1883 | } |
| 1884 | return true; |
| 1885 | } |
| 1886 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1887 | bool isSimple(int end) const { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1888 | int count = fTs.count(); |
| 1889 | if (count == 2) { |
| 1890 | return true; |
| 1891 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1892 | double t = fTs[end].fT; |
| 1893 | if (t < FLT_EPSILON) { |
| 1894 | return fTs[1].fT >= FLT_EPSILON; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1895 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1896 | if (t > 1 - FLT_EPSILON) { |
| 1897 | return fTs[count - 2].fT <= 1 - FLT_EPSILON; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1898 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1899 | return false; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1900 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1901 | |
| 1902 | bool isHorizontal() const { |
| 1903 | return fBounds.fTop == fBounds.fBottom; |
| 1904 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1905 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1906 | bool isVertical() const { |
| 1907 | return fBounds.fLeft == fBounds.fRight; |
| 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 | SkScalar leftMost(int start, int end) const { |
| 1911 | return (*SegmentLeftMost[fVerb])(fPts, fTs[start].fT, fTs[end].fT); |
| 1912 | } |
caryclark@google.com | af46cff | 2012-05-22 21:12:00 +0000 | [diff] [blame] | 1913 | |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1914 | // this span is excluded by the winding rule -- chase the ends |
| 1915 | // as long as they are unambiguous to mark connections as done |
| 1916 | // and give them the same winding value |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1917 | Span* markAndChaseDone(const Angle* angle, int winding, bool outside) { |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1918 | int index = angle->start(); |
| 1919 | int endIndex = angle->end(); |
| 1920 | int step = SkSign32(endIndex - index); |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1921 | Span* last = innerChaseDone(index, step, winding, outside); |
| 1922 | markDone(SkMin32(index, endIndex), winding, outside); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 1923 | return last; |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1926 | Span* markAndChaseWinding(const Angle* angle, int winding, bool outside) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1927 | int index = angle->start(); |
| 1928 | int endIndex = angle->end(); |
| 1929 | int min = SkMin32(index, endIndex); |
| 1930 | int step = SkSign32(endIndex - index); |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1931 | Span* last = innerChaseWinding(index, step, winding, outside); |
| 1932 | markWinding(min, winding, outside); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 1933 | return last; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1936 | // FIXME: this should also mark spans with equal (x,y) |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1937 | // This may be called when the segment is already marked done. While this |
| 1938 | // wastes time, it shouldn't do any more than spin through the T spans. |
| 1939 | // OPTIMIZATION: abort on first done found (assuming that this code is |
| 1940 | // always called to mark segments done). |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1941 | void markDone(int index, int winding, bool outside) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1942 | // SkASSERT(!done()); |
caryclark@google.com | af46cff | 2012-05-22 21:12:00 +0000 | [diff] [blame] | 1943 | double referenceT = fTs[index].fT; |
| 1944 | int lesser = index; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1945 | while (--lesser >= 0 && referenceT - fTs[lesser].fT < FLT_EPSILON) { |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1946 | Span& span = fTs[lesser]; |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 1947 | if (span.fDone) { |
| 1948 | continue; |
| 1949 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1950 | #if DEBUG_MARK_DONE |
caryclark@google.com | 0c803d0 | 2012-08-06 11:15:47 +0000 | [diff] [blame] | 1951 | debugShowNewWinding(__FUNCTION__, span, winding); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1952 | #endif |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 1953 | span.fDone = true; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1954 | SkASSERT(span.fWindSum == SK_MinS32 || span.fWindSum == winding); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1955 | SkASSERT(abs(winding) <= gDebugMaxWindSum); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1956 | span.fWindSum = winding; |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1957 | span.fOutside = outside; |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 1958 | fDoneSpans++; |
caryclark@google.com | af46cff | 2012-05-22 21:12:00 +0000 | [diff] [blame] | 1959 | } |
| 1960 | do { |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1961 | Span& span = fTs[index]; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1962 | // SkASSERT(!span.fDone); |
| 1963 | if (span.fDone) { |
| 1964 | continue; |
| 1965 | } |
| 1966 | #if DEBUG_MARK_DONE |
caryclark@google.com | 0c803d0 | 2012-08-06 11:15:47 +0000 | [diff] [blame] | 1967 | debugShowNewWinding(__FUNCTION__, span, winding); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1968 | #endif |
| 1969 | span.fDone = true; |
| 1970 | SkASSERT(span.fWindSum == SK_MinS32 || span.fWindSum == winding); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1971 | SkASSERT(abs(winding) <= gDebugMaxWindSum); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1972 | span.fWindSum = winding; |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1973 | span.fOutside = outside; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1974 | fDoneSpans++; |
| 1975 | } while (++index < fTs.count() && fTs[index].fT - referenceT < FLT_EPSILON); |
| 1976 | } |
| 1977 | |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1978 | void markWinding(int index, int winding, bool outside) { |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 1979 | // SkASSERT(!done()); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1980 | double referenceT = fTs[index].fT; |
| 1981 | int lesser = index; |
| 1982 | while (--lesser >= 0 && referenceT - fTs[lesser].fT < FLT_EPSILON) { |
| 1983 | Span& span = fTs[lesser]; |
| 1984 | if (span.fDone) { |
| 1985 | continue; |
| 1986 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1987 | // SkASSERT(span.fWindValue == 1 || winding == 0); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1988 | #if DEBUG_MARK_DONE |
caryclark@google.com | 0c803d0 | 2012-08-06 11:15:47 +0000 | [diff] [blame] | 1989 | debugShowNewWinding(__FUNCTION__, span, winding); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1990 | #endif |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1991 | SkASSERT(span.fWindSum == SK_MinS32 || span.fWindSum == winding); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1992 | SkASSERT(abs(winding) <= gDebugMaxWindSum); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1993 | span.fWindSum = winding; |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 1994 | span.fOutside = outside; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1995 | } |
| 1996 | do { |
| 1997 | Span& span = fTs[index]; |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 1998 | // SkASSERT(!span.fDone || span.fCoincident); |
| 1999 | if (span.fDone) { |
| 2000 | continue; |
| 2001 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 2002 | // SkASSERT(span.fWindValue == 1 || winding == 0); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2003 | SkASSERT(span.fWindSum == SK_MinS32 || span.fWindSum == winding); |
| 2004 | #if DEBUG_MARK_DONE |
caryclark@google.com | 0c803d0 | 2012-08-06 11:15:47 +0000 | [diff] [blame] | 2005 | debugShowNewWinding(__FUNCTION__, span, winding); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2006 | #endif |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 2007 | SkASSERT(abs(winding) <= gDebugMaxWindSum); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2008 | span.fWindSum = winding; |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 2009 | span.fOutside = outside; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2010 | } while (++index < fTs.count() && fTs[index].fT - referenceT < FLT_EPSILON); |
caryclark@google.com | af46cff | 2012-05-22 21:12:00 +0000 | [diff] [blame] | 2011 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2012 | |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 2013 | void matchWindingValue(int tIndex, double t, bool borrowWind) { |
caryclark@google.com | 0c803d0 | 2012-08-06 11:15:47 +0000 | [diff] [blame] | 2014 | int nextDoorWind = SK_MaxS32; |
| 2015 | if (tIndex > 0) { |
| 2016 | const Span& below = fTs[tIndex - 1]; |
| 2017 | if (t - below.fT < FLT_EPSILON) { |
| 2018 | nextDoorWind = below.fWindValue; |
| 2019 | } |
| 2020 | } |
| 2021 | if (nextDoorWind == SK_MaxS32 && tIndex + 1 < fTs.count()) { |
| 2022 | const Span& above = fTs[tIndex + 1]; |
| 2023 | if (above.fT - t < FLT_EPSILON) { |
| 2024 | nextDoorWind = above.fWindValue; |
| 2025 | } |
| 2026 | } |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 2027 | if (nextDoorWind == SK_MaxS32 && borrowWind && tIndex > 0 && t < 1) { |
| 2028 | const Span& below = fTs[tIndex - 1]; |
| 2029 | nextDoorWind = below.fWindValue; |
| 2030 | } |
caryclark@google.com | 0c803d0 | 2012-08-06 11:15:47 +0000 | [diff] [blame] | 2031 | if (nextDoorWind != SK_MaxS32) { |
| 2032 | Span& newSpan = fTs[tIndex]; |
| 2033 | newSpan.fWindValue = nextDoorWind; |
| 2034 | if (!nextDoorWind) { |
| 2035 | newSpan.fDone = true; |
| 2036 | ++fDoneSpans; |
| 2037 | } |
| 2038 | } |
| 2039 | } |
| 2040 | |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 2041 | // return span if when chasing, two or more radiating spans are not done |
| 2042 | // OPTIMIZATION: ? multiple spans is detected when there is only one valid |
| 2043 | // candidate and the remaining spans have windValue == 0 (canceled by |
| 2044 | // coincidence). The coincident edges could either be removed altogether, |
| 2045 | // or this code could be more complicated in detecting this case. Worth it? |
| 2046 | bool multipleSpans(int end) const { |
| 2047 | return end > 0 && end < fTs.count() - 1; |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 2048 | } |
| 2049 | |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2050 | // This has callers for two different situations: one establishes the end |
| 2051 | // of the current span, and one establishes the beginning of the next span |
| 2052 | // (thus the name). When this is looking for the end of the current span, |
| 2053 | // coincidence is found when the beginning Ts contain -step and the end |
| 2054 | // contains step. When it is looking for the beginning of the next, the |
| 2055 | // first Ts found can be ignored and the last Ts should contain -step. |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2056 | // OPTIMIZATION: probably should split into two functions |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2057 | int nextSpan(int from, int step) const { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2058 | const Span& fromSpan = fTs[from]; |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2059 | int count = fTs.count(); |
| 2060 | int to = from; |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2061 | while (step > 0 ? ++to < count : --to >= 0) { |
| 2062 | const Span& span = fTs[to]; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2063 | if ((step > 0 ? span.fT - fromSpan.fT : fromSpan.fT - span.fT) < FLT_EPSILON) { |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2064 | continue; |
| 2065 | } |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2066 | return to; |
| 2067 | } |
| 2068 | return -1; |
| 2069 | } |
| 2070 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2071 | const SkPoint* pts() const { |
| 2072 | return fPts; |
| 2073 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2074 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2075 | void reset() { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2076 | init(NULL, (SkPath::Verb) -1); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2077 | fBounds.set(SK_ScalarMax, SK_ScalarMax, SK_ScalarMax, SK_ScalarMax); |
| 2078 | fTs.reset(); |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2079 | } |
| 2080 | |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 2081 | // OPTIMIZATION: mark as debugging only if used solely by tests |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2082 | const Span& span(int tIndex) const { |
| 2083 | return fTs[tIndex]; |
| 2084 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2085 | |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 2086 | const bool spanOutside(int startIndex, int endIndex) const { |
| 2087 | return fTs[SkMin32(startIndex, endIndex)].fOutside; |
| 2088 | } |
| 2089 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2090 | int spanSign(int startIndex, int endIndex) const { |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 2091 | int result = startIndex < endIndex ? -fTs[startIndex].fWindValue : |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2092 | fTs[endIndex].fWindValue; |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 2093 | #if DEBUG_WIND_BUMP |
| 2094 | SkDebugf("%s spanSign=%d\n", __FUNCTION__, result); |
| 2095 | #endif |
| 2096 | return result; |
| 2097 | } |
| 2098 | |
| 2099 | int spanSign(const Angle* angle) const { |
| 2100 | SkASSERT(angle->segment() == this); |
| 2101 | return spanSign(angle->start(), angle->end()); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2102 | } |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2103 | |
| 2104 | // OPTIMIZATION: mark as debugging only if used solely by tests |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2105 | double t(int tIndex) const { |
| 2106 | return fTs[tIndex].fT; |
| 2107 | } |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 2108 | |
| 2109 | static void TrackOutside(SkTDArray<double>& outsideTs, double end, |
| 2110 | double start) { |
| 2111 | int outCount = outsideTs.count(); |
| 2112 | if (outCount == 0 || end - outsideTs[outCount - 2] >= FLT_EPSILON) { |
| 2113 | *outsideTs.append() = end; |
| 2114 | *outsideTs.append() = start; |
| 2115 | } |
| 2116 | } |
| 2117 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2118 | void updatePts(const SkPoint pts[]) { |
| 2119 | fPts = pts; |
| 2120 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2121 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2122 | SkPath::Verb verb() const { |
| 2123 | return fVerb; |
| 2124 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2125 | |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 2126 | int windSum(int tIndex) const { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2127 | return fTs[tIndex].fWindSum; |
| 2128 | } |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2129 | |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 2130 | int windSum(const Angle* angle) const { |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2131 | int start = angle->start(); |
| 2132 | int end = angle->end(); |
| 2133 | int index = SkMin32(start, end); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 2134 | return windSum(index); |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2135 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2136 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2137 | int windValue(int tIndex) const { |
| 2138 | return fTs[tIndex].fWindValue; |
| 2139 | } |
| 2140 | |
| 2141 | int windValue(const Angle* angle) const { |
| 2142 | int start = angle->start(); |
| 2143 | int end = angle->end(); |
| 2144 | int index = SkMin32(start, end); |
| 2145 | return windValue(index); |
| 2146 | } |
| 2147 | |
| 2148 | SkScalar xAtT(const Span* span) const { |
| 2149 | return xyAtT(span).fX; |
| 2150 | } |
| 2151 | |
| 2152 | const SkPoint& xyAtT(int index) const { |
| 2153 | return xyAtT(&fTs[index]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2156 | const SkPoint& xyAtT(const Span* span) const { |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 2157 | if (SkScalarIsNaN(span->fPt.fX)) { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2158 | if (span->fT == 0) { |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 2159 | span->fPt = fPts[0]; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2160 | } else if (span->fT == 1) { |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 2161 | span->fPt = fPts[fVerb]; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2162 | } else { |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 2163 | (*SegmentXYAtT[fVerb])(fPts, span->fT, &span->fPt); |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2164 | } |
| 2165 | } |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 2166 | return span->fPt; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2167 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2168 | |
| 2169 | SkScalar yAtT(int index) const { |
| 2170 | return yAtT(&fTs[index]); |
| 2171 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2172 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2173 | SkScalar yAtT(const Span* span) const { |
| 2174 | return xyAtT(span).fY; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2177 | #if DEBUG_DUMP |
| 2178 | void dump() const { |
| 2179 | const char className[] = "Segment"; |
| 2180 | const int tab = 4; |
| 2181 | for (int i = 0; i < fTs.count(); ++i) { |
| 2182 | SkPoint out; |
| 2183 | (*SegmentXYAtT[fVerb])(fPts, t(i), &out); |
| 2184 | SkDebugf("%*s [%d] %s.fTs[%d]=%1.9g (%1.9g,%1.9g) other=%d" |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2185 | " otherT=%1.9g windSum=%d\n", |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2186 | tab + sizeof(className), className, fID, |
| 2187 | kLVerbStr[fVerb], i, fTs[i].fT, out.fX, out.fY, |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2188 | fTs[i].fOther->fID, fTs[i].fOtherT, fTs[i].fWindSum); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2189 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2190 | 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] | 2191 | tab + sizeof(className), className, fID, |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2192 | fBounds.fLeft, fBounds.fTop, fBounds.fRight, fBounds.fBottom); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2193 | } |
| 2194 | #endif |
| 2195 | |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 2196 | #if DEBUG_CONCIDENT |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 2197 | // assert if pair has not already been added |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2198 | void debugAddTPair(double t, const Segment& other, double otherT) const { |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 2199 | for (int i = 0; i < fTs.count(); ++i) { |
| 2200 | if (fTs[i].fT == t && fTs[i].fOther == &other && fTs[i].fOtherT == otherT) { |
| 2201 | return; |
| 2202 | } |
| 2203 | } |
| 2204 | SkASSERT(0); |
| 2205 | } |
| 2206 | #endif |
| 2207 | |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2208 | #if DEBUG_DUMP |
| 2209 | int debugID() const { |
| 2210 | return fID; |
| 2211 | } |
| 2212 | #endif |
| 2213 | |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 2214 | #if DEBUG_CONCIDENT |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2215 | void debugShowTs() const { |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 2216 | SkDebugf("%s %d", __FUNCTION__, fID); |
| 2217 | for (int i = 0; i < fTs.count(); ++i) { |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 2218 | SkDebugf(" [o=%d t=%1.3g %1.9g,%1.9g w=%d]", fTs[i].fOther->fID, |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 2219 | fTs[i].fT, xAtT(&fTs[i]), yAtT(&fTs[i]), fTs[i].fWindValue); |
| 2220 | } |
| 2221 | SkDebugf("\n"); |
| 2222 | } |
| 2223 | #endif |
| 2224 | |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 2225 | #if DEBUG_ACTIVE_SPANS |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2226 | void debugShowActiveSpans() const { |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 2227 | if (done()) { |
| 2228 | return; |
| 2229 | } |
| 2230 | for (int i = 0; i < fTs.count(); ++i) { |
| 2231 | if (fTs[i].fDone) { |
| 2232 | continue; |
| 2233 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2234 | SkDebugf("%s id=%d", __FUNCTION__, fID); |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 2235 | SkDebugf(" (%1.9g,%1.9g", fPts[0].fX, fPts[0].fY); |
| 2236 | for (int vIndex = 1; vIndex <= fVerb; ++vIndex) { |
| 2237 | SkDebugf(" %1.9g,%1.9g", fPts[vIndex].fX, fPts[vIndex].fY); |
| 2238 | } |
| 2239 | const Span* span = &fTs[i]; |
caryclark@google.com | 0c803d0 | 2012-08-06 11:15:47 +0000 | [diff] [blame] | 2240 | SkDebugf(") t=%1.9g (%1.9g,%1.9g)", fTs[i].fT, |
| 2241 | xAtT(span), yAtT(span)); |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 2242 | const Segment* other = fTs[i].fOther; |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2243 | SkDebugf(" other=%d otherT=%1.9g otherIndex=%d windSum=", |
| 2244 | other->fID, fTs[i].fOtherT, fTs[i].fOtherIndex); |
| 2245 | if (fTs[i].fWindSum == SK_MinS32) { |
| 2246 | SkDebugf("?"); |
| 2247 | } else { |
| 2248 | SkDebugf("%d", fTs[i].fWindSum); |
| 2249 | } |
| 2250 | SkDebugf(" windValue=%d\n", fTs[i].fWindValue); |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 2251 | } |
| 2252 | } |
| 2253 | #endif |
| 2254 | |
caryclark@google.com | 0c803d0 | 2012-08-06 11:15:47 +0000 | [diff] [blame] | 2255 | #if DEBUG_MARK_DONE |
| 2256 | void debugShowNewWinding(const char* fun, const Span& span, int winding) { |
| 2257 | const SkPoint& pt = xyAtT(&span); |
| 2258 | SkDebugf("%s id=%d", fun, fID); |
| 2259 | SkDebugf(" (%1.9g,%1.9g", fPts[0].fX, fPts[0].fY); |
| 2260 | for (int vIndex = 1; vIndex <= fVerb; ++vIndex) { |
| 2261 | SkDebugf(" %1.9g,%1.9g", fPts[vIndex].fX, fPts[vIndex].fY); |
| 2262 | } |
| 2263 | SkDebugf(") t=%1.9g (%1.9g,%1.9g) newWindSum=%d windSum=", |
| 2264 | span.fT, pt.fX, pt.fY, winding); |
| 2265 | if (span.fWindSum == SK_MinS32) { |
| 2266 | SkDebugf("?"); |
| 2267 | } else { |
| 2268 | SkDebugf("%d", span.fWindSum); |
| 2269 | } |
| 2270 | SkDebugf(" windValue=%d\n", span.fWindValue); |
| 2271 | } |
| 2272 | #endif |
| 2273 | |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 2274 | #if DEBUG_SORT |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 2275 | void debugShowSort(const SkTDArray<Angle*>& angles, int first, |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2276 | const int contourWinding) const { |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 2277 | SkASSERT(angles[first]->segment() == this); |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 2278 | SkASSERT(angles.count() > 1); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2279 | int lastSum = contourWinding; |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 2280 | int windSum = lastSum - spanSign(angles[first]); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2281 | SkDebugf("%s contourWinding=%d bump=%d\n", __FUNCTION__, |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 2282 | contourWinding, spanSign(angles[first])); |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 2283 | int index = first; |
| 2284 | bool firstTime = true; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 2285 | do { |
| 2286 | const Angle& angle = *angles[index]; |
| 2287 | const Segment& segment = *angle.segment(); |
| 2288 | int start = angle.start(); |
| 2289 | int end = angle.end(); |
| 2290 | const Span& sSpan = segment.fTs[start]; |
| 2291 | const Span& eSpan = segment.fTs[end]; |
| 2292 | const Span& mSpan = segment.fTs[SkMin32(start, end)]; |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2293 | if (!firstTime) { |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 2294 | lastSum = windSum; |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 2295 | windSum -= segment.spanSign(&angle); |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 2296 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 2297 | SkDebugf("%s [%d] id=%d start=%d (%1.9g,%,1.9g) end=%d (%1.9g,%,1.9g)" |
| 2298 | " sign=%d windValue=%d winding: %d->%d (max=%d) done=%d\n", |
| 2299 | __FUNCTION__, index, segment.fID, start, segment.xAtT(&sSpan), |
| 2300 | segment.yAtT(&sSpan), end, segment.xAtT(&eSpan), |
| 2301 | segment.yAtT(&eSpan), angle.sign(), mSpan.fWindValue, |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 2302 | lastSum, windSum, useInnerWinding(lastSum, windSum) |
| 2303 | ? windSum : lastSum, mSpan.fDone); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 2304 | ++index; |
| 2305 | if (index == angles.count()) { |
| 2306 | index = 0; |
| 2307 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2308 | if (firstTime) { |
| 2309 | firstTime = false; |
| 2310 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 2311 | } while (index != first); |
| 2312 | } |
| 2313 | #endif |
| 2314 | |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2315 | #if DEBUG_WINDING |
| 2316 | bool debugVerifyWinding(int start, int end, int winding) const { |
| 2317 | const Span& span = fTs[SkMin32(start, end)]; |
| 2318 | int spanWinding = span.fWindSum; |
| 2319 | if (spanWinding == SK_MinS32) { |
| 2320 | return true; |
| 2321 | } |
| 2322 | int spanSign = SkSign32(start - end); |
| 2323 | int signedVal = spanSign * span.fWindValue; |
| 2324 | if (signedVal < 0) { |
| 2325 | spanWinding -= signedVal; |
| 2326 | } |
| 2327 | return span.fWindSum == winding; |
| 2328 | } |
| 2329 | #endif |
| 2330 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2331 | private: |
| 2332 | const SkPoint* fPts; |
| 2333 | SkPath::Verb fVerb; |
| 2334 | Bounds fBounds; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2335 | SkTDArray<Span> fTs; // two or more (always includes t=0 t=1) |
caryclark@google.com | af46cff | 2012-05-22 21:12:00 +0000 | [diff] [blame] | 2336 | int fDoneSpans; // used for quick check that segment is finished |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2337 | #if DEBUG_DUMP |
| 2338 | int fID; |
| 2339 | #endif |
| 2340 | }; |
| 2341 | |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 2342 | class Contour; |
| 2343 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2344 | struct Coincidence { |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 2345 | Contour* fContours[2]; |
| 2346 | int fSegments[2]; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2347 | double fTs[2][2]; |
| 2348 | }; |
| 2349 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2350 | class Contour { |
| 2351 | public: |
| 2352 | Contour() { |
| 2353 | reset(); |
| 2354 | #if DEBUG_DUMP |
| 2355 | fID = ++gContourID; |
| 2356 | #endif |
| 2357 | } |
| 2358 | |
| 2359 | bool operator<(const Contour& rh) const { |
| 2360 | return fBounds.fTop == rh.fBounds.fTop |
| 2361 | ? fBounds.fLeft < rh.fBounds.fLeft |
| 2362 | : fBounds.fTop < rh.fBounds.fTop; |
| 2363 | } |
| 2364 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2365 | void addCoincident(int index, Contour* other, int otherIndex, |
| 2366 | const Intersections& ts, bool swap) { |
| 2367 | Coincidence& coincidence = *fCoincidences.append(); |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 2368 | coincidence.fContours[0] = this; |
| 2369 | coincidence.fContours[1] = other; |
| 2370 | coincidence.fSegments[0] = index; |
| 2371 | coincidence.fSegments[1] = otherIndex; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2372 | coincidence.fTs[swap][0] = ts.fT[0][0]; |
| 2373 | coincidence.fTs[swap][1] = ts.fT[0][1]; |
| 2374 | coincidence.fTs[!swap][0] = ts.fT[1][0]; |
| 2375 | coincidence.fTs[!swap][1] = ts.fT[1][1]; |
| 2376 | } |
| 2377 | |
| 2378 | void addCross(const Contour* crosser) { |
| 2379 | #ifdef DEBUG_CROSS |
| 2380 | for (int index = 0; index < fCrosses.count(); ++index) { |
| 2381 | SkASSERT(fCrosses[index] != crosser); |
| 2382 | } |
| 2383 | #endif |
| 2384 | *fCrosses.append() = crosser; |
| 2385 | } |
| 2386 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2387 | void addCubic(const SkPoint pts[4]) { |
| 2388 | fSegments.push_back().addCubic(pts); |
| 2389 | fContainsCurves = true; |
| 2390 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2391 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2392 | int addLine(const SkPoint pts[2]) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2393 | fSegments.push_back().addLine(pts); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2394 | return fSegments.count(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2395 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2396 | |
| 2397 | void addOtherT(int segIndex, int tIndex, double otherT, int otherIndex) { |
| 2398 | fSegments[segIndex].addOtherT(tIndex, otherT, otherIndex); |
| 2399 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2400 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2401 | int addQuad(const SkPoint pts[3]) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2402 | fSegments.push_back().addQuad(pts); |
| 2403 | fContainsCurves = true; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2404 | return fSegments.count(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2405 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2406 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2407 | int addT(int segIndex, double newT, Contour* other, int otherIndex) { |
| 2408 | containsIntercepts(); |
| 2409 | return fSegments[segIndex].addT(newT, &other->fSegments[otherIndex]); |
| 2410 | } |
| 2411 | |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2412 | const Bounds& bounds() const { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2413 | return fBounds; |
| 2414 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2415 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2416 | void complete() { |
| 2417 | setBounds(); |
| 2418 | fContainsIntercepts = false; |
| 2419 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2420 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2421 | void containsIntercepts() { |
| 2422 | fContainsIntercepts = true; |
| 2423 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2424 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2425 | const Segment* crossedSegment(const SkPoint& basePt, SkScalar& bestY, |
| 2426 | int &tIndex, double& hitT) { |
| 2427 | int segmentCount = fSegments.count(); |
| 2428 | const Segment* bestSegment = NULL; |
| 2429 | for (int test = 0; test < segmentCount; ++test) { |
| 2430 | Segment* testSegment = &fSegments[test]; |
| 2431 | const SkRect& bounds = testSegment->bounds(); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2432 | if (bounds.fBottom <= bestY) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2433 | continue; |
| 2434 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2435 | if (bounds.fTop >= basePt.fY) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2436 | continue; |
| 2437 | } |
| 2438 | if (bounds.fLeft > basePt.fX) { |
| 2439 | continue; |
| 2440 | } |
| 2441 | if (bounds.fRight < basePt.fX) { |
| 2442 | continue; |
| 2443 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2444 | if (bounds.fLeft == bounds.fRight) { |
| 2445 | continue; |
| 2446 | } |
| 2447 | #if 0 |
| 2448 | bool leftHalf = bounds.fLeft == basePt.fX; |
| 2449 | bool rightHalf = bounds.fRight == basePt.fX; |
| 2450 | if ((leftHalf || rightHalf) && !testSegment->crossedSpanHalves( |
| 2451 | basePt, leftHalf, rightHalf)) { |
| 2452 | continue; |
| 2453 | } |
| 2454 | #endif |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2455 | double testHitT; |
| 2456 | int testT = testSegment->crossedSpan(basePt, bestY, testHitT); |
| 2457 | if (testT >= 0) { |
| 2458 | bestSegment = testSegment; |
| 2459 | tIndex = testT; |
| 2460 | hitT = testHitT; |
| 2461 | } |
| 2462 | } |
| 2463 | return bestSegment; |
| 2464 | } |
| 2465 | |
| 2466 | bool crosses(const Contour* crosser) const { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2467 | for (int index = 0; index < fCrosses.count(); ++index) { |
| 2468 | if (fCrosses[index] == crosser) { |
| 2469 | return true; |
| 2470 | } |
| 2471 | } |
| 2472 | return false; |
| 2473 | } |
| 2474 | |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2475 | void findTooCloseToCall(int winding) { |
| 2476 | int segmentCount = fSegments.count(); |
| 2477 | for (int sIndex = 0; sIndex < segmentCount; ++sIndex) { |
| 2478 | fSegments[sIndex].findTooCloseToCall(winding); |
| 2479 | } |
| 2480 | } |
| 2481 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2482 | void fixOtherTIndex() { |
| 2483 | int segmentCount = fSegments.count(); |
| 2484 | for (int sIndex = 0; sIndex < segmentCount; ++sIndex) { |
| 2485 | fSegments[sIndex].fixOtherTIndex(); |
| 2486 | } |
| 2487 | } |
| 2488 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2489 | void reset() { |
| 2490 | fSegments.reset(); |
| 2491 | fBounds.set(SK_ScalarMax, SK_ScalarMax, SK_ScalarMax, SK_ScalarMax); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2492 | fContainsCurves = fContainsIntercepts = false; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2493 | } |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 2494 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2495 | void resolveCoincidence(int winding) { |
| 2496 | int count = fCoincidences.count(); |
| 2497 | for (int index = 0; index < count; ++index) { |
| 2498 | Coincidence& coincidence = fCoincidences[index]; |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 2499 | Contour* thisContour = coincidence.fContours[0]; |
| 2500 | Contour* otherContour = coincidence.fContours[1]; |
| 2501 | int thisIndex = coincidence.fSegments[0]; |
| 2502 | int otherIndex = coincidence.fSegments[1]; |
| 2503 | Segment& thisOne = thisContour->fSegments[thisIndex]; |
| 2504 | Segment& other = otherContour->fSegments[otherIndex]; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 2505 | #if DEBUG_CONCIDENT |
| 2506 | thisOne.debugShowTs(); |
| 2507 | other.debugShowTs(); |
| 2508 | #endif |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2509 | double startT = coincidence.fTs[0][0]; |
| 2510 | double endT = coincidence.fTs[0][1]; |
| 2511 | if (startT > endT) { |
| 2512 | SkTSwap<double>(startT, endT); |
| 2513 | } |
| 2514 | SkASSERT(endT - startT >= FLT_EPSILON); |
| 2515 | double oStartT = coincidence.fTs[1][0]; |
| 2516 | double oEndT = coincidence.fTs[1][1]; |
| 2517 | if (oStartT > oEndT) { |
| 2518 | SkTSwap<double>(oStartT, oEndT); |
| 2519 | } |
| 2520 | SkASSERT(oEndT - oStartT >= FLT_EPSILON); |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 2521 | if (winding > 0 || thisOne.cancels(other)) { |
| 2522 | // make sure startT and endT have t entries |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 2523 | if (startT > 0 || oEndT < 1 |
| 2524 | || thisOne.isMissing(startT) || other.isMissing(oEndT)) { |
| 2525 | thisOne.addTPair(startT, other, oEndT, true); |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 2526 | } |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 2527 | if (oStartT > 0 || endT < 1 |
| 2528 | || thisOne.isMissing(endT) || other.isMissing(oStartT)) { |
| 2529 | other.addTPair(oStartT, thisOne, endT, true); |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 2530 | } |
| 2531 | thisOne.addTCancel(startT, endT, other, oStartT, oEndT); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2532 | } else { |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 2533 | if (startT > 0 || oStartT > 0 |
| 2534 | || thisOne.isMissing(startT) || other.isMissing(oStartT)) { |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 2535 | thisOne.addTPair(startT, other, oStartT, true); |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 2536 | } |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 2537 | if (endT < 1 || oEndT < 1 |
| 2538 | || thisOne.isMissing(endT) || other.isMissing(oEndT)) { |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 2539 | other.addTPair(oEndT, thisOne, endT, true); |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 2540 | } |
| 2541 | thisOne.addTCoincident(startT, endT, other, oStartT, oEndT); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2542 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 2543 | #if DEBUG_CONCIDENT |
| 2544 | thisOne.debugShowTs(); |
| 2545 | other.debugShowTs(); |
| 2546 | #endif |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2547 | } |
| 2548 | } |
| 2549 | |
| 2550 | const SkTArray<Segment>& segments() { |
| 2551 | return fSegments; |
| 2552 | } |
| 2553 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2554 | // OPTIMIZATION: feel pretty uneasy about this. It seems like once again |
| 2555 | // we need to sort and walk edges in y, but that on the surface opens the |
| 2556 | // same can of worms as before. But then, this is a rough sort based on |
| 2557 | // segments' top, and not a true sort, so it could be ameniable to regular |
| 2558 | // sorting instead of linear searching. Still feel like I'm missing something |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2559 | Segment* topSegment(SkScalar& bestY) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2560 | int segmentCount = fSegments.count(); |
| 2561 | SkASSERT(segmentCount > 0); |
| 2562 | int best = -1; |
| 2563 | Segment* bestSegment = NULL; |
| 2564 | while (++best < segmentCount) { |
| 2565 | Segment* testSegment = &fSegments[best]; |
| 2566 | if (testSegment->done()) { |
| 2567 | continue; |
| 2568 | } |
| 2569 | bestSegment = testSegment; |
| 2570 | break; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2571 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2572 | if (!bestSegment) { |
| 2573 | return NULL; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2574 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2575 | SkScalar bestTop = bestSegment->activeTop(); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2576 | for (int test = best + 1; test < segmentCount; ++test) { |
| 2577 | Segment* testSegment = &fSegments[test]; |
| 2578 | if (testSegment->done()) { |
| 2579 | continue; |
| 2580 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2581 | if (testSegment->bounds().fTop > bestTop) { |
| 2582 | continue; |
| 2583 | } |
| 2584 | SkScalar testTop = testSegment->activeTop(); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2585 | if (bestTop > testTop) { |
| 2586 | bestTop = testTop; |
| 2587 | bestSegment = testSegment; |
| 2588 | } |
| 2589 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2590 | bestY = bestTop; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2591 | return bestSegment; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2592 | } |
| 2593 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2594 | int updateSegment(int index, const SkPoint* pts) { |
| 2595 | Segment& segment = fSegments[index]; |
| 2596 | segment.updatePts(pts); |
| 2597 | return segment.verb() + 1; |
| 2598 | } |
| 2599 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2600 | #if DEBUG_TEST |
| 2601 | SkTArray<Segment>& debugSegments() { |
| 2602 | return fSegments; |
| 2603 | } |
| 2604 | #endif |
| 2605 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2606 | #if DEBUG_DUMP |
| 2607 | void dump() { |
| 2608 | int i; |
| 2609 | const char className[] = "Contour"; |
| 2610 | const int tab = 4; |
| 2611 | SkDebugf("%s %p (contour=%d)\n", className, this, fID); |
| 2612 | for (i = 0; i < fSegments.count(); ++i) { |
| 2613 | SkDebugf("%*s.fSegments[%d]:\n", tab + sizeof(className), |
| 2614 | className, i); |
| 2615 | fSegments[i].dump(); |
| 2616 | } |
| 2617 | SkDebugf("%*s.fBounds=(l:%1.9g, t:%1.9g r:%1.9g, b:%1.9g)\n", |
| 2618 | tab + sizeof(className), className, |
| 2619 | fBounds.fLeft, fBounds.fTop, |
| 2620 | fBounds.fRight, fBounds.fBottom); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2621 | SkDebugf("%*s.fContainsIntercepts=%d\n", tab + sizeof(className), |
| 2622 | className, fContainsIntercepts); |
| 2623 | SkDebugf("%*s.fContainsCurves=%d\n", tab + sizeof(className), |
| 2624 | className, fContainsCurves); |
| 2625 | } |
| 2626 | #endif |
| 2627 | |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 2628 | #if DEBUG_ACTIVE_SPANS |
| 2629 | void debugShowActiveSpans() { |
| 2630 | for (int index = 0; index < fSegments.count(); ++index) { |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2631 | fSegments[index].debugShowActiveSpans(); |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 2632 | } |
| 2633 | } |
| 2634 | #endif |
| 2635 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2636 | protected: |
| 2637 | void setBounds() { |
| 2638 | int count = fSegments.count(); |
| 2639 | if (count == 0) { |
| 2640 | SkDebugf("%s empty contour\n", __FUNCTION__); |
| 2641 | SkASSERT(0); |
| 2642 | // FIXME: delete empty contour? |
| 2643 | return; |
| 2644 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2645 | fBounds = fSegments.front().bounds(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2646 | for (int index = 1; index < count; ++index) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2647 | fBounds.add(fSegments[index].bounds()); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2648 | } |
| 2649 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2650 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2651 | private: |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2652 | SkTArray<Segment> fSegments; |
| 2653 | SkTDArray<Coincidence> fCoincidences; |
| 2654 | SkTDArray<const Contour*> fCrosses; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2655 | Bounds fBounds; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2656 | bool fContainsIntercepts; |
| 2657 | bool fContainsCurves; |
| 2658 | #if DEBUG_DUMP |
| 2659 | int fID; |
| 2660 | #endif |
| 2661 | }; |
| 2662 | |
| 2663 | class EdgeBuilder { |
| 2664 | public: |
| 2665 | |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2666 | EdgeBuilder(const SkPath& path, SkTArray<Contour>& contours) |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2667 | : fPath(path) |
| 2668 | , fCurrentContour(NULL) |
| 2669 | , fContours(contours) |
| 2670 | { |
| 2671 | #if DEBUG_DUMP |
| 2672 | gContourID = 0; |
| 2673 | gSegmentID = 0; |
| 2674 | #endif |
| 2675 | walk(); |
| 2676 | } |
| 2677 | |
| 2678 | protected: |
| 2679 | |
| 2680 | void complete() { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2681 | if (fCurrentContour && fCurrentContour->segments().count()) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2682 | fCurrentContour->complete(); |
| 2683 | fCurrentContour = NULL; |
| 2684 | } |
| 2685 | } |
| 2686 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2687 | void walk() { |
| 2688 | // FIXME:remove once we can access path pts directly |
| 2689 | SkPath::RawIter iter(fPath); // FIXME: access path directly when allowed |
| 2690 | SkPoint pts[4]; |
| 2691 | SkPath::Verb verb; |
| 2692 | do { |
| 2693 | verb = iter.next(pts); |
| 2694 | *fPathVerbs.append() = verb; |
| 2695 | if (verb == SkPath::kMove_Verb) { |
| 2696 | *fPathPts.append() = pts[0]; |
| 2697 | } else if (verb >= SkPath::kLine_Verb && verb <= SkPath::kCubic_Verb) { |
| 2698 | fPathPts.append(verb, &pts[1]); |
| 2699 | } |
| 2700 | } while (verb != SkPath::kDone_Verb); |
| 2701 | // 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] | 2702 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2703 | SkPath::Verb reducedVerb; |
| 2704 | uint8_t* verbPtr = fPathVerbs.begin(); |
| 2705 | const SkPoint* pointsPtr = fPathPts.begin(); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2706 | const SkPoint* finalCurveStart = NULL; |
| 2707 | const SkPoint* finalCurveEnd = NULL; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2708 | while ((verb = (SkPath::Verb) *verbPtr++) != SkPath::kDone_Verb) { |
| 2709 | switch (verb) { |
| 2710 | case SkPath::kMove_Verb: |
| 2711 | complete(); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2712 | if (!fCurrentContour) { |
| 2713 | fCurrentContour = fContours.push_back_n(1); |
| 2714 | finalCurveEnd = pointsPtr++; |
| 2715 | *fExtra.append() = -1; // start new contour |
| 2716 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2717 | continue; |
| 2718 | case SkPath::kLine_Verb: |
| 2719 | // skip degenerate points |
| 2720 | if (pointsPtr[-1].fX != pointsPtr[0].fX |
| 2721 | || pointsPtr[-1].fY != pointsPtr[0].fY) { |
| 2722 | fCurrentContour->addLine(&pointsPtr[-1]); |
| 2723 | } |
| 2724 | break; |
| 2725 | case SkPath::kQuad_Verb: |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2726 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2727 | reducedVerb = QuadReduceOrder(&pointsPtr[-1], fReducePts); |
| 2728 | if (reducedVerb == 0) { |
| 2729 | break; // skip degenerate points |
| 2730 | } |
| 2731 | if (reducedVerb == 1) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2732 | *fExtra.append() = |
| 2733 | fCurrentContour->addLine(fReducePts.end() - 2); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2734 | break; |
| 2735 | } |
| 2736 | fCurrentContour->addQuad(&pointsPtr[-1]); |
| 2737 | break; |
| 2738 | case SkPath::kCubic_Verb: |
| 2739 | reducedVerb = CubicReduceOrder(&pointsPtr[-1], fReducePts); |
| 2740 | if (reducedVerb == 0) { |
| 2741 | break; // skip degenerate points |
| 2742 | } |
| 2743 | if (reducedVerb == 1) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2744 | *fExtra.append() = |
| 2745 | fCurrentContour->addLine(fReducePts.end() - 2); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2746 | break; |
| 2747 | } |
| 2748 | if (reducedVerb == 2) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2749 | *fExtra.append() = |
| 2750 | fCurrentContour->addQuad(fReducePts.end() - 3); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2751 | break; |
| 2752 | } |
| 2753 | fCurrentContour->addCubic(&pointsPtr[-1]); |
| 2754 | break; |
| 2755 | case SkPath::kClose_Verb: |
| 2756 | SkASSERT(fCurrentContour); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2757 | if (finalCurveStart && finalCurveEnd |
| 2758 | && *finalCurveStart != *finalCurveEnd) { |
| 2759 | *fReducePts.append() = *finalCurveStart; |
| 2760 | *fReducePts.append() = *finalCurveEnd; |
| 2761 | *fExtra.append() = |
| 2762 | fCurrentContour->addLine(fReducePts.end() - 2); |
| 2763 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2764 | complete(); |
| 2765 | continue; |
| 2766 | default: |
| 2767 | SkDEBUGFAIL("bad verb"); |
| 2768 | return; |
| 2769 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2770 | finalCurveStart = &pointsPtr[verb - 1]; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2771 | pointsPtr += verb; |
| 2772 | SkASSERT(fCurrentContour); |
| 2773 | } |
| 2774 | complete(); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2775 | if (fCurrentContour && !fCurrentContour->segments().count()) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2776 | fContours.pop_back(); |
| 2777 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2778 | // correct pointers in contours since fReducePts may have moved as it grew |
| 2779 | int cIndex = 0; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2780 | int extraCount = fExtra.count(); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2781 | SkASSERT(extraCount == 0 || fExtra[0] == -1); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2782 | int eIndex = 0; |
| 2783 | int rIndex = 0; |
| 2784 | while (++eIndex < extraCount) { |
| 2785 | int offset = fExtra[eIndex]; |
| 2786 | if (offset < 0) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2787 | ++cIndex; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2788 | continue; |
| 2789 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2790 | fCurrentContour = &fContours[cIndex]; |
| 2791 | rIndex += fCurrentContour->updateSegment(offset - 1, |
| 2792 | &fReducePts[rIndex]); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2793 | } |
| 2794 | fExtra.reset(); // we're done with this |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | private: |
| 2798 | const SkPath& fPath; |
| 2799 | SkTDArray<SkPoint> fPathPts; // FIXME: point directly to path pts instead |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2800 | SkTDArray<uint8_t> fPathVerbs; // FIXME: remove |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2801 | Contour* fCurrentContour; |
| 2802 | SkTArray<Contour>& fContours; |
| 2803 | SkTDArray<SkPoint> fReducePts; // segments created on the fly |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2804 | SkTDArray<int> fExtra; // -1 marks new contour, > 0 offsets into contour |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2805 | }; |
| 2806 | |
| 2807 | class Work { |
| 2808 | public: |
| 2809 | enum SegmentType { |
| 2810 | kHorizontalLine_Segment = -1, |
| 2811 | kVerticalLine_Segment = 0, |
| 2812 | kLine_Segment = SkPath::kLine_Verb, |
| 2813 | kQuad_Segment = SkPath::kQuad_Verb, |
| 2814 | kCubic_Segment = SkPath::kCubic_Verb, |
| 2815 | }; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2816 | |
| 2817 | void addCoincident(Work& other, const Intersections& ts, bool swap) { |
| 2818 | fContour->addCoincident(fIndex, other.fContour, other.fIndex, ts, swap); |
| 2819 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2820 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2821 | // FIXME: does it make sense to write otherIndex now if we're going to |
| 2822 | // fix it up later? |
| 2823 | void addOtherT(int index, double otherT, int otherIndex) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2824 | fContour->addOtherT(fIndex, index, otherT, otherIndex); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2825 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2826 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2827 | // Avoid collapsing t values that are close to the same since |
| 2828 | // we walk ts to describe consecutive intersections. Since a pair of ts can |
| 2829 | // be nearly equal, any problems caused by this should be taken care |
| 2830 | // of later. |
| 2831 | // On the edge or out of range values are negative; add 2 to get end |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2832 | int addT(double newT, const Work& other) { |
| 2833 | return fContour->addT(fIndex, newT, other.fContour, other.fIndex); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2834 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2835 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2836 | bool advance() { |
| 2837 | return ++fIndex < fLast; |
| 2838 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2839 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2840 | SkScalar bottom() const { |
| 2841 | return bounds().fBottom; |
| 2842 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2843 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2844 | const Bounds& bounds() const { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2845 | return fContour->segments()[fIndex].bounds(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | const SkPoint* cubic() const { |
| 2849 | return fCubic; |
| 2850 | } |
| 2851 | |
| 2852 | void init(Contour* contour) { |
| 2853 | fContour = contour; |
| 2854 | fIndex = 0; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2855 | fLast = contour->segments().count(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2856 | } |
caryclark@google.com | 66ca2fb | 2012-07-03 14:30:08 +0000 | [diff] [blame] | 2857 | |
| 2858 | bool isAdjacent(const Work& next) { |
| 2859 | return fContour == next.fContour && fIndex + 1 == next.fIndex; |
| 2860 | } |
| 2861 | |
| 2862 | bool isFirstLast(const Work& next) { |
| 2863 | return fContour == next.fContour && fIndex == 0 |
| 2864 | && next.fIndex == fLast - 1; |
| 2865 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2866 | |
| 2867 | SkScalar left() const { |
| 2868 | return bounds().fLeft; |
| 2869 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2870 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2871 | void promoteToCubic() { |
| 2872 | fCubic[0] = pts()[0]; |
| 2873 | fCubic[2] = pts()[1]; |
| 2874 | fCubic[3] = pts()[2]; |
| 2875 | fCubic[1].fX = (fCubic[0].fX + fCubic[2].fX * 2) / 3; |
| 2876 | fCubic[1].fY = (fCubic[0].fY + fCubic[2].fY * 2) / 3; |
| 2877 | fCubic[2].fX = (fCubic[3].fX + fCubic[2].fX * 2) / 3; |
| 2878 | fCubic[2].fY = (fCubic[3].fY + fCubic[2].fY * 2) / 3; |
| 2879 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2880 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2881 | const SkPoint* pts() const { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2882 | return fContour->segments()[fIndex].pts(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
| 2885 | SkScalar right() const { |
| 2886 | return bounds().fRight; |
| 2887 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2888 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2889 | ptrdiff_t segmentIndex() const { |
| 2890 | return fIndex; |
| 2891 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2892 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2893 | SegmentType segmentType() const { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2894 | const Segment& segment = fContour->segments()[fIndex]; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2895 | SegmentType type = (SegmentType) segment.verb(); |
| 2896 | if (type != kLine_Segment) { |
| 2897 | return type; |
| 2898 | } |
| 2899 | if (segment.isHorizontal()) { |
| 2900 | return kHorizontalLine_Segment; |
| 2901 | } |
| 2902 | if (segment.isVertical()) { |
| 2903 | return kVerticalLine_Segment; |
| 2904 | } |
| 2905 | return kLine_Segment; |
| 2906 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2907 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2908 | bool startAfter(const Work& after) { |
| 2909 | fIndex = after.fIndex; |
| 2910 | return advance(); |
| 2911 | } |
| 2912 | |
| 2913 | SkScalar top() const { |
| 2914 | return bounds().fTop; |
| 2915 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2916 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2917 | SkPath::Verb verb() const { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2918 | return fContour->segments()[fIndex].verb(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2919 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2920 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2921 | SkScalar x() const { |
| 2922 | return bounds().fLeft; |
| 2923 | } |
| 2924 | |
| 2925 | bool xFlipped() const { |
| 2926 | return x() != pts()[0].fX; |
| 2927 | } |
| 2928 | |
| 2929 | SkScalar y() const { |
| 2930 | return bounds().fTop; |
| 2931 | } |
| 2932 | |
| 2933 | bool yFlipped() const { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2934 | return y() != pts()[0].fY; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | protected: |
| 2938 | Contour* fContour; |
| 2939 | SkPoint fCubic[4]; |
| 2940 | int fIndex; |
| 2941 | int fLast; |
| 2942 | }; |
| 2943 | |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 2944 | #if DEBUG_ADD_INTERSECTING_TS |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2945 | static void debugShowLineIntersection(int pts, const Work& wt, |
| 2946 | const Work& wn, const double wtTs[2], const double wnTs[2]) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2947 | if (!pts) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2948 | SkDebugf("%s no intersect (%1.9g,%1.9g %1.9g,%1.9g) (%1.9g,%1.9g %1.9g,%1.9g)\n", |
| 2949 | __FUNCTION__, wt.pts()[0].fX, wt.pts()[0].fY, |
| 2950 | wt.pts()[1].fX, wt.pts()[1].fY, wn.pts()[0].fX, wn.pts()[0].fY, |
| 2951 | wn.pts()[1].fX, wn.pts()[1].fY); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2952 | return; |
| 2953 | } |
| 2954 | SkPoint wtOutPt, wnOutPt; |
| 2955 | LineXYAtT(wt.pts(), wtTs[0], &wtOutPt); |
| 2956 | LineXYAtT(wn.pts(), wnTs[0], &wnOutPt); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2957 | SkDebugf("%s wtTs[0]=%g (%g,%g, %g,%g) (%g,%g)", |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2958 | __FUNCTION__, |
| 2959 | wtTs[0], wt.pts()[0].fX, wt.pts()[0].fY, |
| 2960 | wt.pts()[1].fX, wt.pts()[1].fY, wtOutPt.fX, wtOutPt.fY); |
| 2961 | if (pts == 2) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2962 | SkDebugf(" wtTs[1]=%g", wtTs[1]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2963 | } |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 2964 | SkDebugf(" wnTs[0]=%g (%g,%g, %g,%g) (%g,%g)", |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2965 | wnTs[0], wn.pts()[0].fX, wn.pts()[0].fY, |
| 2966 | wn.pts()[1].fX, wn.pts()[1].fY, wnOutPt.fX, wnOutPt.fY); |
| 2967 | if (pts == 2) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2968 | SkDebugf(" wnTs[1]=%g", wnTs[1]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2969 | } |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 2970 | SkDebugf("\n"); |
| 2971 | } |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 2972 | #else |
| 2973 | static void debugShowLineIntersection(int , const Work& , |
| 2974 | const Work& , const double [2], const double [2]) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2975 | } |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 2976 | #endif |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2977 | |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 2978 | static bool addIntersectTs(Contour* test, Contour* next) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2979 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2980 | if (test != next) { |
| 2981 | if (test->bounds().fBottom < next->bounds().fTop) { |
| 2982 | return false; |
| 2983 | } |
| 2984 | if (!Bounds::Intersects(test->bounds(), next->bounds())) { |
| 2985 | return true; |
| 2986 | } |
| 2987 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2988 | Work wt; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2989 | wt.init(test); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2990 | bool foundCommonContour = test == next; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2991 | do { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2992 | Work wn; |
| 2993 | wn.init(next); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2994 | if (test == next && !wn.startAfter(wt)) { |
| 2995 | continue; |
| 2996 | } |
| 2997 | do { |
| 2998 | if (!Bounds::Intersects(wt.bounds(), wn.bounds())) { |
| 2999 | continue; |
| 3000 | } |
| 3001 | int pts; |
| 3002 | Intersections ts; |
| 3003 | bool swap = false; |
| 3004 | switch (wt.segmentType()) { |
| 3005 | case Work::kHorizontalLine_Segment: |
| 3006 | swap = true; |
| 3007 | switch (wn.segmentType()) { |
| 3008 | case Work::kHorizontalLine_Segment: |
| 3009 | case Work::kVerticalLine_Segment: |
| 3010 | case Work::kLine_Segment: { |
| 3011 | pts = HLineIntersect(wn.pts(), wt.left(), |
| 3012 | wt.right(), wt.y(), wt.xFlipped(), ts); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3013 | debugShowLineIntersection(pts, wt, wn, |
| 3014 | ts.fT[1], ts.fT[0]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3015 | break; |
| 3016 | } |
| 3017 | case Work::kQuad_Segment: { |
| 3018 | pts = HQuadIntersect(wn.pts(), wt.left(), |
| 3019 | wt.right(), wt.y(), wt.xFlipped(), ts); |
| 3020 | break; |
| 3021 | } |
| 3022 | case Work::kCubic_Segment: { |
| 3023 | pts = HCubicIntersect(wn.pts(), wt.left(), |
| 3024 | wt.right(), wt.y(), wt.xFlipped(), ts); |
| 3025 | break; |
| 3026 | } |
| 3027 | default: |
| 3028 | SkASSERT(0); |
| 3029 | } |
| 3030 | break; |
| 3031 | case Work::kVerticalLine_Segment: |
| 3032 | swap = true; |
| 3033 | switch (wn.segmentType()) { |
| 3034 | case Work::kHorizontalLine_Segment: |
| 3035 | case Work::kVerticalLine_Segment: |
| 3036 | case Work::kLine_Segment: { |
| 3037 | pts = VLineIntersect(wn.pts(), wt.top(), |
| 3038 | wt.bottom(), wt.x(), wt.yFlipped(), ts); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3039 | debugShowLineIntersection(pts, wt, wn, |
| 3040 | ts.fT[1], ts.fT[0]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3041 | break; |
| 3042 | } |
| 3043 | case Work::kQuad_Segment: { |
| 3044 | pts = VQuadIntersect(wn.pts(), wt.top(), |
| 3045 | wt.bottom(), wt.x(), wt.yFlipped(), ts); |
| 3046 | break; |
| 3047 | } |
| 3048 | case Work::kCubic_Segment: { |
| 3049 | pts = VCubicIntersect(wn.pts(), wt.top(), |
| 3050 | wt.bottom(), wt.x(), wt.yFlipped(), ts); |
| 3051 | break; |
| 3052 | } |
| 3053 | default: |
| 3054 | SkASSERT(0); |
| 3055 | } |
| 3056 | break; |
| 3057 | case Work::kLine_Segment: |
| 3058 | switch (wn.segmentType()) { |
| 3059 | case Work::kHorizontalLine_Segment: |
| 3060 | pts = HLineIntersect(wt.pts(), wn.left(), |
| 3061 | wn.right(), wn.y(), wn.xFlipped(), ts); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3062 | debugShowLineIntersection(pts, wt, wn, |
| 3063 | ts.fT[1], ts.fT[0]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3064 | break; |
| 3065 | case Work::kVerticalLine_Segment: |
| 3066 | pts = VLineIntersect(wt.pts(), wn.top(), |
| 3067 | wn.bottom(), wn.x(), wn.yFlipped(), ts); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3068 | debugShowLineIntersection(pts, wt, wn, |
| 3069 | ts.fT[1], ts.fT[0]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3070 | break; |
| 3071 | case Work::kLine_Segment: { |
| 3072 | pts = LineIntersect(wt.pts(), wn.pts(), ts); |
| 3073 | debugShowLineIntersection(pts, wt, wn, |
| 3074 | ts.fT[1], ts.fT[0]); |
| 3075 | break; |
| 3076 | } |
| 3077 | case Work::kQuad_Segment: { |
| 3078 | swap = true; |
| 3079 | pts = QuadLineIntersect(wn.pts(), wt.pts(), ts); |
| 3080 | break; |
| 3081 | } |
| 3082 | case Work::kCubic_Segment: { |
| 3083 | swap = true; |
| 3084 | pts = CubicLineIntersect(wn.pts(), wt.pts(), ts); |
| 3085 | break; |
| 3086 | } |
| 3087 | default: |
| 3088 | SkASSERT(0); |
| 3089 | } |
| 3090 | break; |
| 3091 | case Work::kQuad_Segment: |
| 3092 | switch (wn.segmentType()) { |
| 3093 | case Work::kHorizontalLine_Segment: |
| 3094 | pts = HQuadIntersect(wt.pts(), wn.left(), |
| 3095 | wn.right(), wn.y(), wn.xFlipped(), ts); |
| 3096 | break; |
| 3097 | case Work::kVerticalLine_Segment: |
| 3098 | pts = VQuadIntersect(wt.pts(), wn.top(), |
| 3099 | wn.bottom(), wn.x(), wn.yFlipped(), ts); |
| 3100 | break; |
| 3101 | case Work::kLine_Segment: { |
| 3102 | pts = QuadLineIntersect(wt.pts(), wn.pts(), ts); |
| 3103 | break; |
| 3104 | } |
| 3105 | case Work::kQuad_Segment: { |
| 3106 | pts = QuadIntersect(wt.pts(), wn.pts(), ts); |
| 3107 | break; |
| 3108 | } |
| 3109 | case Work::kCubic_Segment: { |
| 3110 | wt.promoteToCubic(); |
| 3111 | pts = CubicIntersect(wt.cubic(), wn.pts(), ts); |
| 3112 | break; |
| 3113 | } |
| 3114 | default: |
| 3115 | SkASSERT(0); |
| 3116 | } |
| 3117 | break; |
| 3118 | case Work::kCubic_Segment: |
| 3119 | switch (wn.segmentType()) { |
| 3120 | case Work::kHorizontalLine_Segment: |
| 3121 | pts = HCubicIntersect(wt.pts(), wn.left(), |
| 3122 | wn.right(), wn.y(), wn.xFlipped(), ts); |
| 3123 | break; |
| 3124 | case Work::kVerticalLine_Segment: |
| 3125 | pts = VCubicIntersect(wt.pts(), wn.top(), |
| 3126 | wn.bottom(), wn.x(), wn.yFlipped(), ts); |
| 3127 | break; |
| 3128 | case Work::kLine_Segment: { |
| 3129 | pts = CubicLineIntersect(wt.pts(), wn.pts(), ts); |
| 3130 | break; |
| 3131 | } |
| 3132 | case Work::kQuad_Segment: { |
| 3133 | wn.promoteToCubic(); |
| 3134 | pts = CubicIntersect(wt.pts(), wn.cubic(), ts); |
| 3135 | break; |
| 3136 | } |
| 3137 | case Work::kCubic_Segment: { |
| 3138 | pts = CubicIntersect(wt.pts(), wn.pts(), ts); |
| 3139 | break; |
| 3140 | } |
| 3141 | default: |
| 3142 | SkASSERT(0); |
| 3143 | } |
| 3144 | break; |
| 3145 | default: |
| 3146 | SkASSERT(0); |
| 3147 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3148 | if (!foundCommonContour && pts > 0) { |
| 3149 | test->addCross(next); |
| 3150 | next->addCross(test); |
| 3151 | foundCommonContour = true; |
| 3152 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3153 | // in addition to recording T values, record matching segment |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 3154 | if (pts == 2 && wn.segmentType() <= Work::kLine_Segment |
| 3155 | && wt.segmentType() <= Work::kLine_Segment) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3156 | wt.addCoincident(wn, ts, swap); |
| 3157 | continue; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 3158 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 3159 | for (int pt = 0; pt < pts; ++pt) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3160 | SkASSERT(ts.fT[0][pt] >= 0 && ts.fT[0][pt] <= 1); |
| 3161 | SkASSERT(ts.fT[1][pt] >= 0 && ts.fT[1][pt] <= 1); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3162 | int testTAt = wt.addT(ts.fT[swap][pt], wn); |
| 3163 | int nextTAt = wn.addT(ts.fT[!swap][pt], wt); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3164 | wt.addOtherT(testTAt, ts.fT[!swap][pt], nextTAt); |
| 3165 | wn.addOtherT(nextTAt, ts.fT[swap][pt], testTAt); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3166 | } |
| 3167 | } while (wn.advance()); |
| 3168 | } while (wt.advance()); |
| 3169 | return true; |
| 3170 | } |
| 3171 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3172 | // resolve any coincident pairs found while intersecting, and |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3173 | // see if coincidence is formed by clipping non-concident segments |
| 3174 | static void coincidenceCheck(SkTDArray<Contour*>& contourList, int winding) { |
| 3175 | int contourCount = contourList.count(); |
caryclark@google.com | f25edfe | 2012-06-01 18:20:10 +0000 | [diff] [blame] | 3176 | for (int cIndex = 0; cIndex < contourCount; ++cIndex) { |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3177 | Contour* contour = contourList[cIndex]; |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 3178 | contour->findTooCloseToCall(winding); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3179 | } |
| 3180 | for (int cIndex = 0; cIndex < contourCount; ++cIndex) { |
| 3181 | Contour* contour = contourList[cIndex]; |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 3182 | contour->resolveCoincidence(winding); |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3183 | } |
| 3184 | } |
| 3185 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3186 | // project a ray from the top of the contour up and see if it hits anything |
| 3187 | // note: when we compute line intersections, we keep track of whether |
| 3188 | // two contours touch, so we need only look at contours not touching this one. |
| 3189 | // OPTIMIZATION: sort contourList vertically to avoid linear walk |
| 3190 | static int innerContourCheck(SkTDArray<Contour*>& contourList, |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3191 | const Segment* current, int index, int endIndex) { |
caryclark@google.com | 7db7c6b | 2012-07-27 21:22:25 +0000 | [diff] [blame] | 3192 | const SkPoint& basePt = current->xyAtT(endIndex); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3193 | int contourCount = contourList.count(); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3194 | SkScalar bestY = SK_ScalarMin; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3195 | const Segment* test = NULL; |
| 3196 | int tIndex; |
| 3197 | double tHit; |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3198 | // bool checkCrosses = true; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3199 | for (int cTest = 0; cTest < contourCount; ++cTest) { |
| 3200 | Contour* contour = contourList[cTest]; |
| 3201 | if (basePt.fY < contour->bounds().fTop) { |
| 3202 | continue; |
| 3203 | } |
| 3204 | if (bestY > contour->bounds().fBottom) { |
| 3205 | continue; |
| 3206 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3207 | #if 0 |
caryclark@google.com | 7db7c6b | 2012-07-27 21:22:25 +0000 | [diff] [blame] | 3208 | // even though the contours crossed, if spans cancel through concidence, |
| 3209 | // the contours may be not have any span links to chase, and the current |
| 3210 | // segment may be isolated. Detect this by seeing if current has |
| 3211 | // uninitialized wind sums. If so, project a ray instead of relying on |
| 3212 | // previously found intersections. |
| 3213 | if (baseContour == contour) { |
| 3214 | continue; |
| 3215 | } |
| 3216 | if (checkCrosses && baseContour->crosses(contour)) { |
| 3217 | if (current->isConnected(index, endIndex)) { |
| 3218 | continue; |
| 3219 | } |
| 3220 | checkCrosses = false; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3221 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3222 | #endif |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3223 | const Segment* next = contour->crossedSegment(basePt, bestY, tIndex, tHit); |
| 3224 | if (next) { |
| 3225 | test = next; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3226 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3227 | } |
| 3228 | if (!test) { |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3229 | return 0; |
| 3230 | } |
| 3231 | int winding, windValue; |
| 3232 | // If the ray hit the end of a span, we need to construct the wheel of |
| 3233 | // angles to find the span closest to the ray -- even if there are just |
| 3234 | // two spokes on the wheel. |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3235 | const Angle* angle = NULL; |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 3236 | if (fabs(tHit - test->t(tIndex)) < FLT_EPSILON) { |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3237 | SkTDArray<Angle> angles; |
| 3238 | int end = test->nextSpan(tIndex, 1); |
| 3239 | if (end < 0) { |
| 3240 | end = test->nextSpan(tIndex, -1); |
| 3241 | } |
| 3242 | test->addTwoAngles(end, tIndex, angles); |
| 3243 | test->buildAngles(tIndex, angles); |
| 3244 | SkTDArray<Angle*> sorted; |
| 3245 | // OPTIMIZATION: call a sort that, if base point is the leftmost, |
| 3246 | // returns the first counterclockwise hour before 6 o'clock, |
| 3247 | // or if the base point is rightmost, returns the first clockwise |
| 3248 | // hour after 6 o'clock |
| 3249 | sortAngles(angles, sorted); |
| 3250 | #if DEBUG_SORT |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3251 | sorted[0]->segment()->debugShowSort(sorted, 0, 0); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3252 | #endif |
| 3253 | // walk the sorted angle fan to find the lowest angle |
| 3254 | // above the base point. Currently, the first angle in the sorted array |
| 3255 | // is 12 noon or an earlier hour (the next counterclockwise) |
| 3256 | int count = sorted.count(); |
| 3257 | int left = -1; |
caryclark@google.com | 7db7c6b | 2012-07-27 21:22:25 +0000 | [diff] [blame] | 3258 | int mid = -1; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3259 | int right = -1; |
caryclark@google.com | 7db7c6b | 2012-07-27 21:22:25 +0000 | [diff] [blame] | 3260 | bool baseMatches = test->yAtT(tIndex) == basePt.fY; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3261 | for (int index = 0; index < count; ++index) { |
caryclark@google.com | 7db7c6b | 2012-07-27 21:22:25 +0000 | [diff] [blame] | 3262 | const Angle* angle = sorted[index]; |
| 3263 | if (baseMatches && angle->isHorizontal()) { |
| 3264 | continue; |
| 3265 | } |
| 3266 | double indexDx = angle->dx(); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3267 | if (indexDx < 0) { |
| 3268 | left = index; |
| 3269 | } else if (indexDx > 0) { |
| 3270 | right = index; |
| 3271 | break; |
caryclark@google.com | 7db7c6b | 2012-07-27 21:22:25 +0000 | [diff] [blame] | 3272 | } else { |
| 3273 | mid = index; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3274 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3275 | } |
caryclark@google.com | 7db7c6b | 2012-07-27 21:22:25 +0000 | [diff] [blame] | 3276 | if (left < 0 && right < 0) { |
| 3277 | left = mid; |
| 3278 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3279 | SkASSERT(left >= 0 || right >= 0); |
| 3280 | if (left < 0) { |
| 3281 | left = right; |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3282 | } else if (left >= 0 && mid >= 0 && right >= 0 |
| 3283 | && sorted[mid]->sign() == sorted[right]->sign()) { |
| 3284 | left = right; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3285 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3286 | angle = sorted[left]; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3287 | test = angle->segment(); |
| 3288 | winding = test->windSum(angle); |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 3289 | SkASSERT(winding != SK_MinS32); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3290 | windValue = test->windValue(angle); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3291 | #if DEBUG_WINDING |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3292 | SkDebugf("%s angle winding=%d windValue=%d sign=%d\n", __FUNCTION__, winding, |
| 3293 | windValue, angle->sign()); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3294 | #endif |
| 3295 | } else { |
| 3296 | winding = test->windSum(tIndex); |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 3297 | SkASSERT(winding != SK_MinS32); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3298 | windValue = test->windValue(tIndex); |
| 3299 | #if DEBUG_WINDING |
| 3300 | SkDebugf("%s single winding=%d windValue=%d\n", __FUNCTION__, winding, |
| 3301 | windValue); |
| 3302 | #endif |
| 3303 | } |
| 3304 | // see if a + change in T results in a +/- change in X (compute x'(T)) |
| 3305 | SkScalar dx = (*SegmentDXAtT[test->verb()])(test->pts(), tHit); |
| 3306 | #if DEBUG_WINDING |
| 3307 | SkDebugf("%s dx=%1.9g\n", __FUNCTION__, dx); |
| 3308 | #endif |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 3309 | SkASSERT(dx != 0); |
| 3310 | if (winding * dx > 0) { // if same signs, result is negative |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3311 | winding += dx > 0 ? -windValue : windValue; |
| 3312 | #if DEBUG_WINDING |
| 3313 | SkDebugf("%s final winding=%d\n", __FUNCTION__, winding); |
| 3314 | #endif |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3315 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3316 | // start here; |
caryclark@google.com | 7db7c6b | 2012-07-27 21:22:25 +0000 | [diff] [blame] | 3317 | // we're broken because we find a vertical span |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3318 | return winding; |
| 3319 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3320 | |
| 3321 | // OPTIMIZATION: not crazy about linear search here to find top active y. |
| 3322 | // seems like we should break down and do the sort, or maybe sort each |
| 3323 | // contours' segments? |
| 3324 | // Once the segment array is built, there's no reason I can think of not to |
| 3325 | // sort it in Y. hmmm |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3326 | // FIXME: return the contour found to pass to inner contour check |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3327 | static Segment* findTopContour(SkTDArray<Contour*>& contourList) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3328 | int contourCount = contourList.count(); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3329 | int cIndex = 0; |
| 3330 | Segment* topStart; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3331 | SkScalar bestY = SK_ScalarMax; |
| 3332 | Contour* contour; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3333 | do { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3334 | contour = contourList[cIndex]; |
| 3335 | topStart = contour->topSegment(bestY); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3336 | } while (!topStart && ++cIndex < contourCount); |
| 3337 | if (!topStart) { |
| 3338 | return NULL; |
| 3339 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3340 | while (++cIndex < contourCount) { |
| 3341 | contour = contourList[cIndex]; |
| 3342 | if (bestY < contour->bounds().fTop) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3343 | continue; |
| 3344 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3345 | SkScalar testY = SK_ScalarMax; |
| 3346 | Segment* test = contour->topSegment(testY); |
| 3347 | if (!test || bestY <= testY) { |
| 3348 | continue; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3349 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3350 | topStart = test; |
| 3351 | bestY = testY; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3352 | } |
| 3353 | return topStart; |
| 3354 | } |
| 3355 | |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 3356 | static Segment* findChase(SkTDArray<Span*>& chase, int& tIndex, int& endIndex, |
| 3357 | int contourWinding) { |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 3358 | while (chase.count()) { |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 3359 | Span* span = chase[chase.count() - 1]; |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 3360 | const Span& backPtr = span->fOther->span(span->fOtherIndex); |
| 3361 | Segment* segment = backPtr.fOther; |
| 3362 | tIndex = backPtr.fOtherIndex; |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 3363 | SkTDArray<Angle> angles; |
| 3364 | int done = 0; |
| 3365 | if (segment->activeAngle(tIndex, done, angles)) { |
| 3366 | Angle* last = angles.end() - 1; |
| 3367 | tIndex = last->start(); |
| 3368 | endIndex = last->end(); |
| 3369 | return last->segment(); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 3370 | } |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 3371 | if (done == angles.count()) { |
| 3372 | chase.pop(&span); |
| 3373 | continue; |
| 3374 | } |
| 3375 | SkTDArray<Angle*> sorted; |
| 3376 | sortAngles(angles, sorted); |
| 3377 | // find first angle, initialize winding to computed fWindSum |
| 3378 | int firstIndex = -1; |
| 3379 | const Angle* angle; |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3380 | int winding; |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 3381 | do { |
| 3382 | angle = sorted[++firstIndex]; |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3383 | segment = angle->segment(); |
| 3384 | winding = segment->windSum(angle); |
| 3385 | } while (winding == SK_MinS32); |
| 3386 | int spanWinding = segment->spanSign(angle->start(), angle->end()); |
| 3387 | #if DEBUG_WINDING |
| 3388 | SkDebugf("%s winding=%d spanWinding=%d contourWinding=%d\n", |
| 3389 | __FUNCTION__, winding, spanWinding, contourWinding); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3390 | #endif |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3391 | // turn swinding into contourWinding |
| 3392 | if (spanWinding * winding < 0) { |
| 3393 | winding += spanWinding; |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 3394 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3395 | #if DEBUG_SORT |
| 3396 | segment->debugShowSort(sorted, firstIndex, winding); |
| 3397 | #endif |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 3398 | // we care about first sign and whether wind sum indicates this |
| 3399 | // edge is inside or outside. Maybe need to pass span winding |
| 3400 | // or first winding or something into this function? |
| 3401 | // advance to first undone angle, then return it and winding |
| 3402 | // (to set whether edges are active or not) |
| 3403 | int nextIndex = firstIndex + 1; |
| 3404 | int angleCount = sorted.count(); |
| 3405 | int lastIndex = firstIndex != 0 ? firstIndex : angleCount; |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3406 | angle = sorted[firstIndex]; |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 3407 | winding -= angle->segment()->spanSign(angle); |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 3408 | do { |
| 3409 | SkASSERT(nextIndex != firstIndex); |
| 3410 | if (nextIndex == angleCount) { |
| 3411 | nextIndex = 0; |
| 3412 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3413 | angle = sorted[nextIndex]; |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 3414 | segment = angle->segment(); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3415 | int maxWinding = winding; |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 3416 | winding -= segment->spanSign(angle); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3417 | #if DEBUG_SORT |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 3418 | SkDebugf("%s id=%d maxWinding=%d winding=%d sign=%d\n", __FUNCTION__, |
| 3419 | segment->debugID(), maxWinding, winding, angle->sign()); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3420 | #endif |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 3421 | tIndex = angle->start(); |
| 3422 | endIndex = angle->end(); |
| 3423 | int lesser = SkMin32(tIndex, endIndex); |
| 3424 | const Span& nextSpan = segment->span(lesser); |
| 3425 | if (!nextSpan.fDone) { |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3426 | #if 1 |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 3427 | // FIXME: this be wrong. assign startWinding if edge is in |
| 3428 | // same direction. If the direction is opposite, winding to |
| 3429 | // assign is flipped sign or +/- 1? |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 3430 | bool inside = useInnerWinding(maxWinding, winding); |
| 3431 | if (inside) { |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3432 | maxWinding = winding; |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 3433 | } |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 3434 | segment->markWinding(lesser, maxWinding, !inside); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3435 | #endif |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 3436 | break; |
| 3437 | } |
| 3438 | } while (++nextIndex != lastIndex); |
| 3439 | return segment; |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 3440 | } |
| 3441 | return NULL; |
| 3442 | } |
| 3443 | |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 3444 | #if DEBUG_ACTIVE_SPANS |
| 3445 | static void debugShowActiveSpans(SkTDArray<Contour*>& contourList) { |
| 3446 | for (int index = 0; index < contourList.count(); ++ index) { |
| 3447 | contourList[index]->debugShowActiveSpans(); |
| 3448 | } |
| 3449 | } |
| 3450 | #endif |
| 3451 | |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 3452 | static bool windingIsActive(int winding, int spanWinding) { |
| 3453 | return winding * spanWinding <= 0 && abs(winding) <= abs(spanWinding) |
| 3454 | && (!winding || !spanWinding || winding == -spanWinding); |
| 3455 | } |
| 3456 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3457 | // Each segment may have an inside or an outside. Segments contained within |
| 3458 | // winding may have insides on either side, and form a contour that should be |
| 3459 | // ignored. Segments that are coincident with opposing direction segments may |
| 3460 | // have outsides on either side, and should also disappear. |
| 3461 | // 'Normal' segments will have one inside and one outside. Subsequent connections |
| 3462 | // when winding should follow the intersection direction. If more than one edge |
| 3463 | // is an option, choose first edge that continues the inside. |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3464 | // since we start with leftmost top edge, we'll traverse through a |
| 3465 | // smaller angle counterclockwise to get to the next edge. |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 3466 | static void bridge(SkTDArray<Contour*>& contourList, SkPath& simple) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3467 | bool firstContour = true; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 3468 | do { |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3469 | Segment* topStart = findTopContour(contourList); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 3470 | if (!topStart) { |
| 3471 | break; |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 3472 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 3473 | // Start at the top. Above the top is outside, below is inside. |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 3474 | // follow edges to intersection by changing the index by direction. |
| 3475 | int index, endIndex; |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 3476 | Segment* current = topStart->findTop(index, endIndex); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 3477 | int contourWinding; |
| 3478 | if (firstContour) { |
| 3479 | contourWinding = 0; |
| 3480 | firstContour = false; |
| 3481 | } else { |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 3482 | int sumWinding = current->windSum(SkMin32(index, endIndex)); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3483 | // FIXME: don't I have to adjust windSum to get contourWinding? |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 3484 | if (sumWinding == SK_MinS32) { |
| 3485 | sumWinding = current->computeSum(index, endIndex); |
| 3486 | } |
| 3487 | if (sumWinding == SK_MinS32) { |
| 3488 | contourWinding = innerContourCheck(contourList, current, |
| 3489 | index, endIndex); |
| 3490 | } else { |
| 3491 | contourWinding = sumWinding; |
| 3492 | int spanWinding = current->spanSign(index, endIndex); |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 3493 | bool inner = useInnerWinding(sumWinding - spanWinding, sumWinding); |
| 3494 | if (inner) { |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 3495 | contourWinding -= spanWinding; |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3496 | } |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 3497 | #if DEBUG_WINDING |
| 3498 | SkDebugf("%s --- sumWinding=%d spanWinding=%d sign=%d inner=%d outside=%d result=%d\n", __FUNCTION__, |
| 3499 | sumWinding, spanWinding, SkSign32(index - endIndex), |
| 3500 | inner, current->spanOutside(index, endIndex), contourWinding); |
| 3501 | #endif |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3502 | } |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 3503 | #if DEBUG_WINDING |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3504 | // SkASSERT(current->debugVerifyWinding(index, endIndex, contourWinding)); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 3505 | SkDebugf("%s contourWinding=%d\n", __FUNCTION__, contourWinding); |
| 3506 | #endif |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3507 | } |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 3508 | SkPoint lastPt; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3509 | bool firstTime = true; |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 3510 | int winding = contourWinding; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3511 | int spanWinding = current->spanSign(index, endIndex); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 3512 | // FIXME: needs work. While it works in limited situations, it does |
| 3513 | // not always compute winding correctly. Active should be removed and instead |
| 3514 | // the initial winding should be correctly passed in so that if the |
| 3515 | // inner contour is wound the same way, it never finds an accumulated |
| 3516 | // winding of zero. Inside 'find next', we need to look for transitions |
| 3517 | // other than zero when resolving sorted angles. |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 3518 | bool active = windingIsActive(winding, spanWinding); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 3519 | SkTDArray<Span*> chaseArray; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3520 | do { |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 3521 | #if DEBUG_WINDING |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 3522 | SkDebugf("%s active=%s winding=%d spanWinding=%d\n", |
| 3523 | __FUNCTION__, active ? "true" : "false", |
| 3524 | winding, spanWinding); |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 3525 | #endif |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 3526 | const SkPoint* firstPt = NULL; |
| 3527 | do { |
| 3528 | SkASSERT(!current->done()); |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 3529 | int nextStart, nextEnd; |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 3530 | Segment* next = current->findNext(chaseArray, |
| 3531 | firstTime, active, index, endIndex, |
| 3532 | nextStart, nextEnd, winding, spanWinding); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 3533 | if (!next) { |
| 3534 | break; |
| 3535 | } |
| 3536 | if (!firstPt) { |
| 3537 | firstPt = ¤t->addMoveTo(index, simple, active); |
| 3538 | } |
| 3539 | lastPt = current->addCurveTo(index, endIndex, simple, active); |
| 3540 | current = next; |
| 3541 | index = nextStart; |
| 3542 | endIndex = nextEnd; |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 3543 | firstTime = false; |
| 3544 | } while (*firstPt != lastPt && (active || !current->done())); |
| 3545 | if (firstPt && active) { |
| 3546 | #if DEBUG_PATH_CONSTRUCTION |
| 3547 | SkDebugf("%s close\n", __FUNCTION__); |
| 3548 | #endif |
| 3549 | simple.close(); |
| 3550 | } |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 3551 | current = findChase(chaseArray, index, endIndex, contourWinding); |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 3552 | #if DEBUG_ACTIVE_SPANS |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 3553 | debugShowActiveSpans(contourList); |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 3554 | #endif |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 3555 | if (!current) { |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 3556 | break; |
| 3557 | } |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 3558 | int lesser = SkMin32(index, endIndex); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3559 | spanWinding = current->spanSign(index, endIndex); |
| 3560 | winding = current->windSum(lesser); |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame^] | 3561 | bool inner = useInnerWinding(winding - spanWinding, winding); |
| 3562 | #if DEBUG_WINDING |
| 3563 | SkDebugf("%s --- id=%d t=%1.9g spanWinding=%d winding=%d sign=%d" |
| 3564 | " inner=%d outside=%d result=%d\n", |
| 3565 | __FUNCTION__, current->debugID(), current->t(lesser), |
| 3566 | spanWinding, winding, SkSign32(index - endIndex), |
| 3567 | useInnerWinding(winding - spanWinding, winding), |
| 3568 | current->spanOutside(index, endIndex), |
| 3569 | inner ? winding - spanWinding : winding); |
| 3570 | #endif |
| 3571 | if (inner) { |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 3572 | winding -= spanWinding; |
| 3573 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3574 | active = windingIsActive(winding, spanWinding); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 3575 | } while (true); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 3576 | } while (true); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3577 | } |
| 3578 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3579 | static void fixOtherTIndex(SkTDArray<Contour*>& contourList) { |
| 3580 | int contourCount = contourList.count(); |
| 3581 | for (int cTest = 0; cTest < contourCount; ++cTest) { |
| 3582 | Contour* contour = contourList[cTest]; |
| 3583 | contour->fixOtherTIndex(); |
| 3584 | } |
| 3585 | } |
| 3586 | |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 3587 | static void makeContourList(SkTArray<Contour>& contours, |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3588 | SkTDArray<Contour*>& list) { |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3589 | int count = contours.count(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3590 | if (count == 0) { |
| 3591 | return; |
| 3592 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3593 | for (int index = 0; index < count; ++index) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3594 | *list.append() = &contours[index]; |
| 3595 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3596 | QSort<Contour>(list.begin(), list.end() - 1); |
| 3597 | } |
| 3598 | |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 3599 | void simplifyx(const SkPath& path, SkPath& simple) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3600 | // returns 1 for evenodd, -1 for winding, regardless of inverse-ness |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3601 | int winding = (path.getFillType() & 1) ? 1 : -1; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3602 | simple.reset(); |
| 3603 | simple.setFillType(SkPath::kEvenOdd_FillType); |
| 3604 | |
| 3605 | // turn path into list of segments |
| 3606 | SkTArray<Contour> contours; |
| 3607 | // FIXME: add self-intersecting cubics' T values to segment |
| 3608 | EdgeBuilder builder(path, contours); |
| 3609 | SkTDArray<Contour*> contourList; |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 3610 | makeContourList(contours, contourList); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3611 | Contour** currentPtr = contourList.begin(); |
| 3612 | if (!currentPtr) { |
| 3613 | return; |
| 3614 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 3615 | Contour** listEnd = contourList.end(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3616 | // find all intersections between segments |
| 3617 | do { |
| 3618 | Contour** nextPtr = currentPtr; |
| 3619 | Contour* current = *currentPtr++; |
| 3620 | Contour* next; |
| 3621 | do { |
| 3622 | next = *nextPtr++; |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 3623 | } while (addIntersectTs(current, next) && nextPtr != listEnd); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 3624 | } while (currentPtr != listEnd); |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3625 | // eat through coincident edges |
| 3626 | coincidenceCheck(contourList, winding); |
caryclark@google.com | 66ca2fb | 2012-07-03 14:30:08 +0000 | [diff] [blame] | 3627 | fixOtherTIndex(contourList); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3628 | // construct closed contours |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 3629 | bridge(contourList, simple); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3630 | } |
| 3631 | |