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 | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 28 | #define PRECISE_T_SORT 1 |
| 29 | |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 30 | #define DEBUG_UNUSED 0 // set to expose unused functions |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 31 | |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 32 | #if 1 // set to 1 for multiple thread -- no debugging |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 33 | |
| 34 | const bool gRunTestsInOneThread = false; |
| 35 | |
| 36 | #define DEBUG_ACTIVE_SPANS 0 |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 37 | #define DEBUG_ADD_INTERSECTING_TS 0 |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 38 | #define DEBUG_ADD_T_PAIR 0 |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 39 | #define DEBUG_ANGLE 0 |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 40 | #define DEBUG_CONCIDENT 0 |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 41 | #define DEBUG_CROSS 0 |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 42 | #define DEBUG_MARK_DONE 0 |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 43 | #define DEBUG_PATH_CONSTRUCTION 0 |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 44 | #define DEBUG_SORT 0 |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 45 | #define DEBUG_WIND_BUMP 0 |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 46 | #define DEBUG_WINDING 0 |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 47 | |
| 48 | #else |
| 49 | |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 50 | const bool gRunTestsInOneThread = true; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 51 | |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 52 | #define DEBUG_ACTIVE_SPANS 1 |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 53 | #define DEBUG_ADD_INTERSECTING_TS 1 |
| 54 | #define DEBUG_ADD_T_PAIR 1 |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 55 | #define DEBUG_ANGLE 1 |
| 56 | #define DEBUG_CONCIDENT 1 |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 57 | #define DEBUG_CROSS 0 |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 58 | #define DEBUG_MARK_DONE 1 |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 59 | #define DEBUG_PATH_CONSTRUCTION 1 |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 60 | #define DEBUG_SORT 1 |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 61 | #define DEBUG_WIND_BUMP 0 |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 62 | #define DEBUG_WINDING 1 |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 63 | |
| 64 | #endif |
| 65 | |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 66 | #define DEBUG_DUMP (DEBUG_ACTIVE_SPANS | DEBUG_CONCIDENT | DEBUG_SORT | DEBUG_PATH_CONSTRUCTION) |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 67 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 68 | #if DEBUG_DUMP |
| 69 | static const char* kLVerbStr[] = {"", "line", "quad", "cubic"}; |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 70 | // static const char* kUVerbStr[] = {"", "Line", "Quad", "Cubic"}; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 71 | static int gContourID; |
| 72 | static int gSegmentID; |
| 73 | #endif |
| 74 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 75 | #ifndef DEBUG_TEST |
| 76 | #define DEBUG_TEST 0 |
| 77 | #endif |
| 78 | |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 79 | #define MAKE_CONST_LINE(line, pts) \ |
| 80 | const _Line line = {{pts[0].fX, pts[0].fY}, {pts[1].fX, pts[1].fY}} |
| 81 | #define MAKE_CONST_QUAD(quad, pts) \ |
| 82 | const Quadratic quad = {{pts[0].fX, pts[0].fY}, {pts[1].fX, pts[1].fY}, \ |
| 83 | {pts[2].fX, pts[2].fY}} |
| 84 | #define MAKE_CONST_CUBIC(cubic, pts) \ |
| 85 | const Cubic cubic = {{pts[0].fX, pts[0].fY}, {pts[1].fX, pts[1].fY}, \ |
| 86 | {pts[2].fX, pts[2].fY}, {pts[3].fX, pts[3].fY}} |
| 87 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 88 | static int LineIntersect(const SkPoint a[2], const SkPoint b[2], |
| 89 | Intersections& intersections) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 90 | MAKE_CONST_LINE(aLine, a); |
| 91 | MAKE_CONST_LINE(bLine, b); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 92 | return intersect(aLine, bLine, intersections.fT[0], intersections.fT[1]); |
| 93 | } |
| 94 | |
| 95 | static int QuadLineIntersect(const SkPoint a[3], const SkPoint b[2], |
| 96 | Intersections& intersections) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 97 | MAKE_CONST_QUAD(aQuad, a); |
| 98 | MAKE_CONST_LINE(bLine, b); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 99 | return intersect(aQuad, bLine, intersections); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 100 | } |
| 101 | |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 102 | static int CubicLineIntersect(const SkPoint a[4], const SkPoint b[2], |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 103 | Intersections& intersections) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 104 | MAKE_CONST_CUBIC(aCubic, a); |
| 105 | MAKE_CONST_LINE(bLine, b); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 106 | return intersect(aCubic, bLine, intersections.fT[0], intersections.fT[1]); |
| 107 | } |
| 108 | |
| 109 | static int QuadIntersect(const SkPoint a[3], const SkPoint b[3], |
| 110 | Intersections& intersections) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 111 | MAKE_CONST_QUAD(aQuad, a); |
| 112 | MAKE_CONST_QUAD(bQuad, b); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 113 | #define TRY_QUARTIC_SOLUTION 1 |
| 114 | #if TRY_QUARTIC_SOLUTION |
| 115 | intersect2(aQuad, bQuad, intersections); |
| 116 | #else |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 117 | intersect(aQuad, bQuad, intersections); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 118 | #endif |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 119 | return intersections.fUsed ? intersections.fUsed : intersections.fCoincidentUsed; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static int CubicIntersect(const SkPoint a[4], const SkPoint b[4], |
| 123 | Intersections& intersections) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 124 | MAKE_CONST_CUBIC(aCubic, a); |
| 125 | MAKE_CONST_CUBIC(bCubic, b); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 126 | intersect(aCubic, bCubic, intersections); |
| 127 | return intersections.fUsed; |
| 128 | } |
| 129 | |
| 130 | static int HLineIntersect(const SkPoint a[2], SkScalar left, SkScalar right, |
| 131 | SkScalar y, bool flipped, Intersections& intersections) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 132 | MAKE_CONST_LINE(aLine, a); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 133 | return horizontalIntersect(aLine, left, right, y, flipped, intersections); |
| 134 | } |
| 135 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 136 | static int HQuadIntersect(const SkPoint a[3], SkScalar left, SkScalar right, |
| 137 | SkScalar y, bool flipped, Intersections& intersections) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 138 | MAKE_CONST_QUAD(aQuad, a); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 139 | return horizontalIntersect(aQuad, left, right, y, flipped, intersections); |
| 140 | } |
| 141 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 142 | static int HCubicIntersect(const SkPoint a[4], SkScalar left, SkScalar right, |
| 143 | SkScalar y, bool flipped, Intersections& intersections) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 144 | MAKE_CONST_CUBIC(aCubic, a); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 145 | return horizontalIntersect(aCubic, left, right, y, flipped, intersections); |
| 146 | } |
| 147 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 148 | static int VLineIntersect(const SkPoint a[2], SkScalar top, SkScalar bottom, |
| 149 | SkScalar x, bool flipped, Intersections& intersections) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 150 | MAKE_CONST_LINE(aLine, a); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 151 | return verticalIntersect(aLine, top, bottom, x, flipped, intersections); |
| 152 | } |
| 153 | |
| 154 | static int VQuadIntersect(const SkPoint a[3], SkScalar top, SkScalar bottom, |
| 155 | SkScalar x, bool flipped, Intersections& intersections) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 156 | MAKE_CONST_QUAD(aQuad, a); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 157 | return verticalIntersect(aQuad, top, bottom, x, flipped, intersections); |
| 158 | } |
| 159 | |
| 160 | static int VCubicIntersect(const SkPoint a[4], SkScalar top, SkScalar bottom, |
| 161 | SkScalar x, bool flipped, Intersections& intersections) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 162 | MAKE_CONST_CUBIC(aCubic, a); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 163 | return verticalIntersect(aCubic, top, bottom, x, flipped, intersections); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 164 | } |
| 165 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 166 | static int (* const VSegmentIntersect[])(const SkPoint [], SkScalar , |
| 167 | SkScalar , SkScalar , bool , Intersections& ) = { |
| 168 | NULL, |
| 169 | VLineIntersect, |
| 170 | VQuadIntersect, |
| 171 | VCubicIntersect |
| 172 | }; |
| 173 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 174 | static void LineXYAtT(const SkPoint a[2], double t, SkPoint* out) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 175 | MAKE_CONST_LINE(line, a); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 176 | double x, y; |
| 177 | xy_at_t(line, t, x, y); |
| 178 | out->fX = SkDoubleToScalar(x); |
| 179 | out->fY = SkDoubleToScalar(y); |
| 180 | } |
| 181 | |
| 182 | static void QuadXYAtT(const SkPoint a[3], double t, SkPoint* out) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 183 | MAKE_CONST_QUAD(quad, a); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 184 | double x, y; |
| 185 | xy_at_t(quad, t, x, y); |
| 186 | out->fX = SkDoubleToScalar(x); |
| 187 | out->fY = SkDoubleToScalar(y); |
| 188 | } |
| 189 | |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 190 | static void QuadXYAtT(const SkPoint a[3], double t, _Point* out) { |
| 191 | MAKE_CONST_QUAD(quad, a); |
| 192 | xy_at_t(quad, t, out->x, out->y); |
| 193 | } |
| 194 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 195 | static void CubicXYAtT(const SkPoint a[4], double t, SkPoint* out) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 196 | MAKE_CONST_CUBIC(cubic, a); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 197 | double x, y; |
| 198 | xy_at_t(cubic, t, x, y); |
| 199 | out->fX = SkDoubleToScalar(x); |
| 200 | out->fY = SkDoubleToScalar(y); |
| 201 | } |
| 202 | |
| 203 | static void (* const SegmentXYAtT[])(const SkPoint [], double , SkPoint* ) = { |
| 204 | NULL, |
| 205 | LineXYAtT, |
| 206 | QuadXYAtT, |
| 207 | CubicXYAtT |
| 208 | }; |
| 209 | |
| 210 | static SkScalar LineXAtT(const SkPoint a[2], double t) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 211 | MAKE_CONST_LINE(aLine, a); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 212 | double x; |
| 213 | xy_at_t(aLine, t, x, *(double*) 0); |
| 214 | return SkDoubleToScalar(x); |
| 215 | } |
| 216 | |
| 217 | static SkScalar QuadXAtT(const SkPoint a[3], double t) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 218 | MAKE_CONST_QUAD(quad, a); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 219 | double x; |
| 220 | xy_at_t(quad, t, x, *(double*) 0); |
| 221 | return SkDoubleToScalar(x); |
| 222 | } |
| 223 | |
| 224 | static SkScalar CubicXAtT(const SkPoint a[4], double t) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 225 | MAKE_CONST_CUBIC(cubic, a); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 226 | double x; |
| 227 | xy_at_t(cubic, t, x, *(double*) 0); |
| 228 | return SkDoubleToScalar(x); |
| 229 | } |
| 230 | |
| 231 | static SkScalar (* const SegmentXAtT[])(const SkPoint [], double ) = { |
| 232 | NULL, |
| 233 | LineXAtT, |
| 234 | QuadXAtT, |
| 235 | CubicXAtT |
| 236 | }; |
| 237 | |
| 238 | static SkScalar LineYAtT(const SkPoint a[2], double t) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 239 | MAKE_CONST_LINE(aLine, a); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 240 | double y; |
| 241 | xy_at_t(aLine, t, *(double*) 0, y); |
| 242 | return SkDoubleToScalar(y); |
| 243 | } |
| 244 | |
| 245 | static SkScalar QuadYAtT(const SkPoint a[3], double t) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 246 | MAKE_CONST_QUAD(quad, a); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 247 | double y; |
| 248 | xy_at_t(quad, t, *(double*) 0, y); |
| 249 | return SkDoubleToScalar(y); |
| 250 | } |
| 251 | |
| 252 | static SkScalar CubicYAtT(const SkPoint a[4], double t) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 253 | MAKE_CONST_CUBIC(cubic, a); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 254 | double y; |
| 255 | xy_at_t(cubic, t, *(double*) 0, y); |
| 256 | return SkDoubleToScalar(y); |
| 257 | } |
| 258 | |
| 259 | static SkScalar (* const SegmentYAtT[])(const SkPoint [], double ) = { |
| 260 | NULL, |
| 261 | LineYAtT, |
| 262 | QuadYAtT, |
| 263 | CubicYAtT |
| 264 | }; |
| 265 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 266 | static SkScalar LineDXAtT(const SkPoint a[2], double ) { |
| 267 | return a[1].fX - a[0].fX; |
| 268 | } |
| 269 | |
| 270 | static SkScalar QuadDXAtT(const SkPoint a[3], double t) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 271 | MAKE_CONST_QUAD(quad, a); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 272 | double x; |
| 273 | dxdy_at_t(quad, t, x, *(double*) 0); |
| 274 | return SkDoubleToScalar(x); |
| 275 | } |
| 276 | |
| 277 | static SkScalar CubicDXAtT(const SkPoint a[4], double t) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 278 | MAKE_CONST_CUBIC(cubic, a); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 279 | double x; |
| 280 | dxdy_at_t(cubic, t, x, *(double*) 0); |
| 281 | return SkDoubleToScalar(x); |
| 282 | } |
| 283 | |
| 284 | static SkScalar (* const SegmentDXAtT[])(const SkPoint [], double ) = { |
| 285 | NULL, |
| 286 | LineDXAtT, |
| 287 | QuadDXAtT, |
| 288 | CubicDXAtT |
| 289 | }; |
| 290 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 291 | static void LineSubDivide(const SkPoint a[2], double startT, double endT, |
| 292 | SkPoint sub[2]) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 293 | MAKE_CONST_LINE(aLine, a); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 294 | _Line dst; |
| 295 | sub_divide(aLine, startT, endT, dst); |
| 296 | sub[0].fX = SkDoubleToScalar(dst[0].x); |
| 297 | sub[0].fY = SkDoubleToScalar(dst[0].y); |
| 298 | sub[1].fX = SkDoubleToScalar(dst[1].x); |
| 299 | sub[1].fY = SkDoubleToScalar(dst[1].y); |
| 300 | } |
| 301 | |
| 302 | static void QuadSubDivide(const SkPoint a[3], double startT, double endT, |
| 303 | SkPoint sub[3]) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 304 | MAKE_CONST_QUAD(aQuad, a); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 305 | Quadratic dst; |
| 306 | sub_divide(aQuad, startT, endT, dst); |
| 307 | sub[0].fX = SkDoubleToScalar(dst[0].x); |
| 308 | sub[0].fY = SkDoubleToScalar(dst[0].y); |
| 309 | sub[1].fX = SkDoubleToScalar(dst[1].x); |
| 310 | sub[1].fY = SkDoubleToScalar(dst[1].y); |
| 311 | sub[2].fX = SkDoubleToScalar(dst[2].x); |
| 312 | sub[2].fY = SkDoubleToScalar(dst[2].y); |
| 313 | } |
| 314 | |
| 315 | static void CubicSubDivide(const SkPoint a[4], double startT, double endT, |
| 316 | SkPoint sub[4]) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 317 | MAKE_CONST_CUBIC(aCubic, a); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 318 | Cubic dst; |
| 319 | sub_divide(aCubic, startT, endT, dst); |
| 320 | sub[0].fX = SkDoubleToScalar(dst[0].x); |
| 321 | sub[0].fY = SkDoubleToScalar(dst[0].y); |
| 322 | sub[1].fX = SkDoubleToScalar(dst[1].x); |
| 323 | sub[1].fY = SkDoubleToScalar(dst[1].y); |
| 324 | sub[2].fX = SkDoubleToScalar(dst[2].x); |
| 325 | sub[2].fY = SkDoubleToScalar(dst[2].y); |
| 326 | sub[3].fX = SkDoubleToScalar(dst[3].x); |
| 327 | sub[3].fY = SkDoubleToScalar(dst[3].y); |
| 328 | } |
| 329 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 330 | static void (* const SegmentSubDivide[])(const SkPoint [], double , double , |
| 331 | SkPoint []) = { |
| 332 | NULL, |
| 333 | LineSubDivide, |
| 334 | QuadSubDivide, |
| 335 | CubicSubDivide |
| 336 | }; |
| 337 | |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 338 | static void LineSubDivideHD(const SkPoint a[2], double startT, double endT, |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 339 | _Line sub) { |
| 340 | MAKE_CONST_LINE(aLine, a); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 341 | _Line dst; |
| 342 | sub_divide(aLine, startT, endT, dst); |
| 343 | sub[0] = dst[0]; |
| 344 | sub[1] = dst[1]; |
| 345 | } |
| 346 | |
| 347 | static void QuadSubDivideHD(const SkPoint a[3], double startT, double endT, |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 348 | Quadratic sub) { |
| 349 | MAKE_CONST_QUAD(aQuad, a); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 350 | Quadratic dst; |
| 351 | sub_divide(aQuad, startT, endT, dst); |
| 352 | sub[0] = dst[0]; |
| 353 | sub[1] = dst[1]; |
| 354 | sub[2] = dst[2]; |
| 355 | } |
| 356 | |
| 357 | static void CubicSubDivideHD(const SkPoint a[4], double startT, double endT, |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 358 | Cubic sub) { |
| 359 | MAKE_CONST_CUBIC(aCubic, a); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 360 | Cubic dst; |
| 361 | sub_divide(aCubic, startT, endT, dst); |
| 362 | sub[0] = dst[0]; |
| 363 | sub[1] = dst[1]; |
| 364 | sub[2] = dst[2]; |
| 365 | sub[3] = dst[3]; |
| 366 | } |
| 367 | |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 368 | #if DEBUG_UNUSED |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 369 | static void QuadSubBounds(const SkPoint a[3], double startT, double endT, |
| 370 | SkRect& bounds) { |
| 371 | SkPoint dst[3]; |
| 372 | QuadSubDivide(a, startT, endT, dst); |
| 373 | bounds.fLeft = bounds.fRight = dst[0].fX; |
| 374 | bounds.fTop = bounds.fBottom = dst[0].fY; |
| 375 | for (int index = 1; index < 3; ++index) { |
| 376 | bounds.growToInclude(dst[index].fX, dst[index].fY); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | static void CubicSubBounds(const SkPoint a[4], double startT, double endT, |
| 381 | SkRect& bounds) { |
| 382 | SkPoint dst[4]; |
| 383 | CubicSubDivide(a, startT, endT, dst); |
| 384 | bounds.fLeft = bounds.fRight = dst[0].fX; |
| 385 | bounds.fTop = bounds.fBottom = dst[0].fY; |
| 386 | for (int index = 1; index < 4; ++index) { |
| 387 | bounds.growToInclude(dst[index].fX, dst[index].fY); |
| 388 | } |
| 389 | } |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 390 | #endif |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 391 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 392 | static SkPath::Verb QuadReduceOrder(const SkPoint a[3], |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 393 | SkTDArray<SkPoint>& reducePts) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 394 | MAKE_CONST_QUAD(aQuad, a); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 395 | Quadratic dst; |
| 396 | int order = reduceOrder(aQuad, dst); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 397 | if (order == 2) { // quad became line |
| 398 | for (int index = 0; index < order; ++index) { |
| 399 | SkPoint* pt = reducePts.append(); |
| 400 | pt->fX = SkDoubleToScalar(dst[index].x); |
| 401 | pt->fY = SkDoubleToScalar(dst[index].y); |
| 402 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 403 | } |
| 404 | return (SkPath::Verb) (order - 1); |
| 405 | } |
| 406 | |
| 407 | static SkPath::Verb CubicReduceOrder(const SkPoint a[4], |
| 408 | SkTDArray<SkPoint>& reducePts) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 409 | MAKE_CONST_CUBIC(aCubic, a); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 410 | Cubic dst; |
| 411 | int order = reduceOrder(aCubic, dst, kReduceOrder_QuadraticsAllowed); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 412 | if (order == 2 || order == 3) { // cubic became line or quad |
| 413 | for (int index = 0; index < order; ++index) { |
| 414 | SkPoint* pt = reducePts.append(); |
| 415 | pt->fX = SkDoubleToScalar(dst[index].x); |
| 416 | pt->fY = SkDoubleToScalar(dst[index].y); |
| 417 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 418 | } |
| 419 | return (SkPath::Verb) (order - 1); |
| 420 | } |
| 421 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 422 | static bool QuadIsLinear(const SkPoint a[3]) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 423 | MAKE_CONST_QUAD(aQuad, a); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 424 | return isLinear(aQuad, 0, 2); |
| 425 | } |
| 426 | |
| 427 | static bool CubicIsLinear(const SkPoint a[4]) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 428 | MAKE_CONST_CUBIC(aCubic, a); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 429 | return isLinear(aCubic, 0, 3); |
| 430 | } |
| 431 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 432 | static SkScalar LineLeftMost(const SkPoint a[2], double startT, double endT) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 433 | MAKE_CONST_LINE(aLine, a); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 434 | double x[2]; |
| 435 | xy_at_t(aLine, startT, x[0], *(double*) 0); |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 436 | xy_at_t(aLine, endT, x[1], *(double*) 0); |
| 437 | return SkMinScalar((float) x[0], (float) x[1]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | static SkScalar QuadLeftMost(const SkPoint a[3], double startT, double endT) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 441 | MAKE_CONST_QUAD(aQuad, a); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 442 | return (float) leftMostT(aQuad, startT, endT); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | static SkScalar CubicLeftMost(const SkPoint a[4], double startT, double endT) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 446 | MAKE_CONST_CUBIC(aCubic, a); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 447 | return (float) leftMostT(aCubic, startT, endT); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static SkScalar (* const SegmentLeftMost[])(const SkPoint [], double , double) = { |
| 451 | NULL, |
| 452 | LineLeftMost, |
| 453 | QuadLeftMost, |
| 454 | CubicLeftMost |
| 455 | }; |
| 456 | |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 457 | #if 0 // currently unused |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 458 | static int QuadRayIntersect(const SkPoint a[3], const SkPoint b[2], |
| 459 | Intersections& intersections) { |
| 460 | MAKE_CONST_QUAD(aQuad, a); |
| 461 | MAKE_CONST_LINE(bLine, b); |
| 462 | return intersectRay(aQuad, bLine, intersections); |
| 463 | } |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 464 | #endif |
| 465 | |
| 466 | static int QuadRayIntersect(const SkPoint a[3], const _Line& bLine, |
| 467 | Intersections& intersections) { |
| 468 | MAKE_CONST_QUAD(aQuad, a); |
| 469 | return intersectRay(aQuad, bLine, intersections); |
| 470 | } |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 471 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 472 | class Segment; |
| 473 | |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 474 | struct Span { |
| 475 | Segment* fOther; |
| 476 | mutable SkPoint fPt; // lazily computed as needed |
| 477 | double fT; |
| 478 | double fOtherT; // value at fOther[fOtherIndex].fT |
| 479 | int fOtherIndex; // can't be used during intersection |
| 480 | int fWindSum; // accumulated from contours surrounding this one |
| 481 | int fWindValue; // 0 == canceled; 1 == normal; >1 == coincident |
| 482 | int fWindValueOpp; // opposite value, if any (for binary ops with coincidence) |
| 483 | bool fDone; // if set, this span to next higher T has been processed |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 484 | bool fUnsortableStart; // set when start is part of an unsortable pair |
| 485 | bool fUnsortableEnd; // set when end is part of an unsortable pair |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 486 | }; |
| 487 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 488 | // sorting angles |
| 489 | // given angles of {dx dy ddx ddy dddx dddy} sort them |
| 490 | class Angle { |
| 491 | public: |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 492 | // FIXME: this is bogus for quads and cubics |
| 493 | // if the quads and cubics' line from end pt to ctrl pt are coincident, |
| 494 | // there's no obvious way to determine the curve ordering from the |
| 495 | // derivatives alone. In particular, if one quadratic's coincident tangent |
| 496 | // is longer than the other curve, the final control point can place the |
| 497 | // longer curve on either side of the shorter one. |
| 498 | // Using Bezier curve focus http://cagd.cs.byu.edu/~tom/papers/bezclip.pdf |
| 499 | // may provide some help, but nothing has been figured out yet. |
skia.committer@gmail.com | 4f55d39 | 2012-09-01 02:00:58 +0000 | [diff] [blame] | 500 | |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 501 | // start here |
| 502 | /*( |
| 503 | for quads and cubics, set up a parameterized line (e.g. LineParameters ) |
| 504 | for points [0] to [1]. See if point [2] is on that line, or on one side |
| 505 | or the other. If it both quads' end points are on the same side, choose |
| 506 | the shorter tangent. If the tangents are equal, choose the better second |
| 507 | tangent angle |
skia.committer@gmail.com | 4f55d39 | 2012-09-01 02:00:58 +0000 | [diff] [blame] | 508 | |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 509 | maybe I could set up LineParameters lazily |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 510 | */ |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 511 | bool operator<(const Angle& rh) const { |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 512 | double y = dy(); |
| 513 | double ry = rh.dy(); |
| 514 | if ((y < 0) ^ (ry < 0)) { // OPTIMIZATION: better to use y * ry < 0 ? |
| 515 | return y < 0; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 516 | } |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 517 | double x = dx(); |
| 518 | double rx = rh.dx(); |
| 519 | if (y == 0 && ry == 0 && x * rx < 0) { |
| 520 | return x < rx; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 521 | } |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 522 | double x_ry = x * ry; |
| 523 | double rx_y = rx * y; |
| 524 | double cmp = x_ry - rx_y; |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 525 | if (!approximately_zero(cmp)) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 526 | return cmp < 0; |
| 527 | } |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 528 | if (approximately_zero(x_ry) && approximately_zero(rx_y) |
| 529 | && !approximately_zero_squared(cmp)) { |
| 530 | return cmp < 0; |
| 531 | } |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 532 | // at this point, the initial tangent line is coincident |
| 533 | if (fSide * rh.fSide <= 0 && (!approximately_zero(fSide) || !approximately_zero(rh.fSide))) { |
| 534 | // FIXME: running demo will trigger this assertion |
| 535 | // (don't know if commenting out will trigger further assertion or not) |
| 536 | // commenting it out allows demo to run in release, though |
| 537 | // SkASSERT(fSide != rh.fSide); |
| 538 | return fSide < rh.fSide; |
| 539 | } |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 540 | // see if either curve can be lengthened and try the tangent compare again |
| 541 | if (cmp && (*fSpans)[fEnd].fOther != rh.fSegment // tangents not absolutely identical |
| 542 | && (*rh.fSpans)[rh.fEnd].fOther != fSegment) { // and not intersecting |
| 543 | Angle longer = *this; |
| 544 | Angle rhLonger = rh; |
| 545 | if (longer.lengthen() | rhLonger.lengthen()) { |
| 546 | return longer < rhLonger; |
| 547 | } |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 548 | // what if we extend in the other direction? |
| 549 | longer = *this; |
| 550 | rhLonger = rh; |
| 551 | if (longer.reverseLengthen() | rhLonger.reverseLengthen()) { |
| 552 | return longer < rhLonger; |
| 553 | } |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 554 | } |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 555 | SkASSERT(fVerb == SkPath::kQuad_Verb); // worry about cubics later |
| 556 | SkASSERT(rh.fVerb == SkPath::kQuad_Verb); |
skia.committer@gmail.com | c1ad022 | 2012-09-19 02:01:47 +0000 | [diff] [blame] | 557 | // FIXME: until I can think of something better, project a ray from the |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 558 | // end of the shorter tangent to midway between the end points |
| 559 | // through both curves and use the resulting angle to sort |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 560 | // FIXME: some of this setup can be moved to set() if it works, or cached if it's expensive |
| 561 | double len = fTangent1.normalSquared(); |
| 562 | double rlen = rh.fTangent1.normalSquared(); |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 563 | _Line ray; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 564 | Intersections i, ri; |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 565 | int roots, rroots; |
| 566 | bool flip = false; |
| 567 | do { |
| 568 | const Quadratic& q = (len < rlen) ^ flip ? fQ : rh.fQ; |
| 569 | double midX = (q[0].x + q[2].x) / 2; |
| 570 | double midY = (q[0].y + q[2].y) / 2; |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 571 | ray[0] = q[1]; |
| 572 | ray[1].x = midX; |
| 573 | ray[1].y = midY; |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 574 | SkASSERT(ray[0] != ray[1]); |
| 575 | roots = QuadRayIntersect(fPts, ray, i); |
| 576 | rroots = QuadRayIntersect(rh.fPts, ray, ri); |
| 577 | } while ((roots == 0 || rroots == 0) && (flip ^= true)); |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 578 | if (roots == 0 || rroots == 0) { |
| 579 | // FIXME: we don't have a solution in this case. The interim solution |
| 580 | // is to mark the edges as unsortable, exclude them from this and |
| 581 | // future computations, and allow the returned path to be fragmented |
| 582 | fUnsortable = true; |
| 583 | rh.fUnsortable = true; |
| 584 | return this < &rh; // even with no solution, return a stable sort |
| 585 | } |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 586 | _Point loc; |
| 587 | double best = SK_ScalarInfinity; |
| 588 | double dx, dy, dist; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 589 | int index; |
| 590 | for (index = 0; index < roots; ++index) { |
| 591 | QuadXYAtT(fPts, i.fT[0][index], &loc); |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 592 | dx = loc.x - ray[0].x; |
| 593 | dy = loc.y - ray[0].y; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 594 | dist = dx * dx + dy * dy; |
| 595 | if (best > dist) { |
| 596 | best = dist; |
| 597 | } |
| 598 | } |
| 599 | for (index = 0; index < rroots; ++index) { |
| 600 | QuadXYAtT(rh.fPts, ri.fT[0][index], &loc); |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 601 | dx = loc.x - ray[0].x; |
| 602 | dy = loc.y - ray[0].y; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 603 | dist = dx * dx + dy * dy; |
| 604 | if (best > dist) { |
| 605 | return fSide < 0; |
| 606 | } |
| 607 | } |
| 608 | return fSide > 0; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 609 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 610 | |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 611 | double dx() const { |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 612 | return fTangent1.dx(); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 613 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 614 | |
caryclark@google.com | 7db7c6b | 2012-07-27 21:22:25 +0000 | [diff] [blame] | 615 | double dy() const { |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 616 | return fTangent1.dy(); |
caryclark@google.com | 7db7c6b | 2012-07-27 21:22:25 +0000 | [diff] [blame] | 617 | } |
| 618 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 619 | int end() const { |
| 620 | return fEnd; |
| 621 | } |
| 622 | |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 623 | bool isHorizontal() const { |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 624 | return dy() == 0 && fVerb == SkPath::kLine_Verb; |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 625 | } |
skia.committer@gmail.com | a27096b | 2012-08-30 14:38:00 +0000 | [diff] [blame] | 626 | |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 627 | bool lengthen() { |
| 628 | int newEnd = fEnd; |
| 629 | if (fStart < fEnd ? ++newEnd < fSpans->count() : --newEnd >= 0) { |
| 630 | fEnd = newEnd; |
| 631 | setSpans(); |
| 632 | return true; |
| 633 | } |
| 634 | return false; |
| 635 | } |
| 636 | |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 637 | bool reverseLengthen() { |
| 638 | if (fReversed) { |
| 639 | return false; |
| 640 | } |
| 641 | int newEnd = fStart; |
| 642 | if (fStart > fEnd ? ++newEnd < fSpans->count() : --newEnd >= 0) { |
| 643 | fEnd = newEnd; |
| 644 | fReversed = true; |
| 645 | setSpans(); |
| 646 | return true; |
| 647 | } |
| 648 | return false; |
| 649 | } |
| 650 | |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 651 | void set(const SkPoint* orig, SkPath::Verb verb, const Segment* segment, |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 652 | int start, int end, const SkTDArray<Span>& spans) { |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 653 | fSegment = segment; |
| 654 | fStart = start; |
| 655 | fEnd = end; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 656 | fPts = orig; |
| 657 | fVerb = verb; |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 658 | fSpans = &spans; |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 659 | fReversed = false; |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 660 | fUnsortable = false; |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 661 | setSpans(); |
| 662 | } |
skia.committer@gmail.com | 439cb51 | 2012-10-10 02:01:30 +0000 | [diff] [blame] | 663 | |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 664 | void setSpans() { |
| 665 | double startT = (*fSpans)[fStart].fT; |
| 666 | double endT = (*fSpans)[fEnd].fT; |
| 667 | switch (fVerb) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 668 | case SkPath::kLine_Verb: |
| 669 | _Line l; |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 670 | LineSubDivideHD(fPts, startT, endT, l); |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 671 | // OPTIMIZATION: for pure line compares, we never need fTangent1.c |
| 672 | fTangent1.lineEndPoints(l); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 673 | fSide = 0; |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 674 | break; |
| 675 | case SkPath::kQuad_Verb: |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 676 | QuadSubDivideHD(fPts, startT, endT, fQ); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 677 | fTangent1.quadEndPoints(fQ, 0, 1); |
| 678 | fSide = -fTangent1.pointDistance(fQ[2]); // not normalized -- compare sign only |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 679 | break; |
| 680 | case SkPath::kCubic_Verb: |
| 681 | Cubic c; |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 682 | CubicSubDivideHD(fPts, startT, endT, c); |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 683 | fTangent1.cubicEndPoints(c, 0, 1); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 684 | fSide = -fTangent1.pointDistance(c[2]); // not normalized -- compare sign only |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 685 | break; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 686 | default: |
| 687 | SkASSERT(0); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 688 | } |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 689 | } |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 690 | |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 691 | Segment* segment() const { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 692 | return const_cast<Segment*>(fSegment); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 693 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 694 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 695 | int sign() const { |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 696 | return SkSign32(fStart - fEnd); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 697 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 698 | |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 699 | const SkTDArray<Span>* spans() const { |
| 700 | return fSpans; |
| 701 | } |
| 702 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 703 | int start() const { |
| 704 | return fStart; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 705 | } |
skia.committer@gmail.com | 20c301b | 2012-10-17 02:01:13 +0000 | [diff] [blame^] | 706 | |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 707 | bool unsortable() const { |
| 708 | return fUnsortable; |
| 709 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 710 | |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 711 | #if DEBUG_ANGLE |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 712 | const SkPoint* pts() const { |
| 713 | return fPts; |
| 714 | } |
| 715 | |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 716 | SkPath::Verb verb() const { |
| 717 | return fVerb; |
| 718 | } |
skia.committer@gmail.com | 439cb51 | 2012-10-10 02:01:30 +0000 | [diff] [blame] | 719 | |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 720 | void debugShow(const SkPoint& a) const { |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 721 | SkDebugf(" d=(%1.9g,%1.9g) side=%1.9g\n", dx(), dy(), fSide); |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 722 | } |
| 723 | #endif |
| 724 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 725 | private: |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 726 | const SkPoint* fPts; |
| 727 | Quadratic fQ; |
| 728 | SkPath::Verb fVerb; |
| 729 | double fSide; |
| 730 | LineParameters fTangent1; |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 731 | const SkTDArray<Span>* fSpans; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 732 | const Segment* fSegment; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 733 | int fStart; |
| 734 | int fEnd; |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 735 | bool fReversed; |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 736 | mutable bool fUnsortable; // this alone is editable by the less than operator |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 737 | }; |
| 738 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 739 | // Bounds, unlike Rect, does not consider a line to be empty. |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 740 | struct Bounds : public SkRect { |
| 741 | static bool Intersects(const Bounds& a, const Bounds& b) { |
| 742 | return a.fLeft <= b.fRight && b.fLeft <= a.fRight && |
| 743 | a.fTop <= b.fBottom && b.fTop <= a.fBottom; |
| 744 | } |
| 745 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 746 | void add(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) { |
| 747 | if (left < fLeft) { |
| 748 | fLeft = left; |
| 749 | } |
| 750 | if (top < fTop) { |
| 751 | fTop = top; |
| 752 | } |
| 753 | if (right > fRight) { |
| 754 | fRight = right; |
| 755 | } |
| 756 | if (bottom > fBottom) { |
| 757 | fBottom = bottom; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | void add(const Bounds& toAdd) { |
| 762 | add(toAdd.fLeft, toAdd.fTop, toAdd.fRight, toAdd.fBottom); |
| 763 | } |
| 764 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 765 | bool isEmpty() { |
| 766 | return fLeft > fRight || fTop > fBottom |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 767 | || (fLeft == fRight && fTop == fBottom) |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 768 | || isnan(fLeft) || isnan(fRight) |
| 769 | || isnan(fTop) || isnan(fBottom); |
| 770 | } |
| 771 | |
| 772 | void setCubicBounds(const SkPoint a[4]) { |
| 773 | _Rect dRect; |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 774 | MAKE_CONST_CUBIC(cubic, a); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 775 | dRect.setBounds(cubic); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 776 | set((float) dRect.left, (float) dRect.top, (float) dRect.right, |
| 777 | (float) dRect.bottom); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | void setQuadBounds(const SkPoint a[3]) { |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 781 | MAKE_CONST_QUAD(quad, a); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 782 | _Rect dRect; |
| 783 | dRect.setBounds(quad); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 784 | set((float) dRect.left, (float) dRect.top, (float) dRect.right, |
| 785 | (float) dRect.bottom); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 786 | } |
| 787 | }; |
| 788 | |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 789 | static bool useInnerWinding(int outerWinding, int innerWinding) { |
| 790 | SkASSERT(outerWinding != innerWinding); |
| 791 | int absOut = abs(outerWinding); |
| 792 | int absIn = abs(innerWinding); |
| 793 | bool result = absOut == absIn ? outerWinding < 0 : absOut < absIn; |
| 794 | if (outerWinding * innerWinding < 0) { |
| 795 | #if DEBUG_WINDING |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 796 | SkDebugf("%s outer=%d inner=%d result=%s\n", __FUNCTION__, |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 797 | outerWinding, innerWinding, result ? "true" : "false"); |
| 798 | #endif |
| 799 | } |
| 800 | return result; |
| 801 | } |
| 802 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 803 | static const bool opLookup[][2][2] = { |
| 804 | // ==0 !=0 |
| 805 | // b a b a |
| 806 | {{true , false}, {false, true }}, // a - b |
| 807 | {{false, false}, {true , true }}, // a & b |
| 808 | {{true , true }, {false, false}}, // a | b |
| 809 | {{true , true }, {true , true }}, // a ^ b |
| 810 | }; |
| 811 | |
| 812 | static bool activeOp(bool angleIsOp, int otherNonZero, ShapeOp op) { |
| 813 | return opLookup[op][otherNonZero][angleIsOp]; |
| 814 | } |
| 815 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 816 | class Segment { |
| 817 | public: |
| 818 | Segment() { |
| 819 | #if DEBUG_DUMP |
| 820 | fID = ++gSegmentID; |
| 821 | #endif |
| 822 | } |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 823 | |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 824 | bool activeAngle(int index, int& done, SkTDArray<Angle>& angles) const { |
| 825 | if (activeAngleInner(index, done, angles)) { |
| 826 | return true; |
| 827 | } |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 828 | double referenceT = fTs[index].fT; |
| 829 | int lesser = index; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 830 | while (--lesser >= 0 && approximately_negative(referenceT - fTs[lesser].fT)) { |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 831 | if (activeAngleOther(lesser, done, angles)) { |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 832 | return true; |
| 833 | } |
| 834 | } |
| 835 | do { |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 836 | if (activeAngleOther(index, done, angles)) { |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 837 | return true; |
| 838 | } |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 839 | } while (++index < fTs.count() && approximately_negative(fTs[index].fT - referenceT)); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 840 | return false; |
| 841 | } |
| 842 | |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 843 | bool activeAngleOther(int index, int& done, SkTDArray<Angle>& angles) const { |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 844 | Span* span = &fTs[index]; |
| 845 | Segment* other = span->fOther; |
| 846 | int oIndex = span->fOtherIndex; |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 847 | return other->activeAngleInner(oIndex, done, angles); |
| 848 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 849 | |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 850 | bool activeAngleInner(int index, int& done, SkTDArray<Angle>& angles) const { |
| 851 | int next = nextSpan(index, 1); |
| 852 | if (next > 0) { |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 853 | const Span& upSpan = fTs[index]; |
caryclark@google.com | 210acaf | 2012-07-12 21:05:13 +0000 | [diff] [blame] | 854 | if (upSpan.fWindValue) { |
| 855 | addAngle(angles, index, next); |
| 856 | if (upSpan.fDone) { |
| 857 | done++; |
| 858 | } else if (upSpan.fWindSum != SK_MinS32) { |
| 859 | return true; |
| 860 | } |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 861 | } |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 862 | } |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 863 | int prev = nextSpan(index, -1); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 864 | // edge leading into junction |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 865 | if (prev >= 0) { |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 866 | const Span& downSpan = fTs[prev]; |
caryclark@google.com | 210acaf | 2012-07-12 21:05:13 +0000 | [diff] [blame] | 867 | if (downSpan.fWindValue) { |
| 868 | addAngle(angles, index, prev); |
| 869 | if (downSpan.fDone) { |
| 870 | done++; |
| 871 | } else if (downSpan.fWindSum != SK_MinS32) { |
| 872 | return true; |
| 873 | } |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | return false; |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 877 | } |
| 878 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 879 | SkScalar activeTop() const { |
| 880 | SkASSERT(!done()); |
| 881 | int count = fTs.count(); |
| 882 | SkScalar result = SK_ScalarMax; |
| 883 | bool lastDone = true; |
| 884 | for (int index = 0; index < count; ++index) { |
| 885 | bool done = fTs[index].fDone; |
| 886 | if (!done || !lastDone) { |
| 887 | SkScalar y = yAtT(index); |
| 888 | if (result > y) { |
| 889 | result = y; |
| 890 | } |
| 891 | } |
| 892 | lastDone = done; |
| 893 | } |
| 894 | SkASSERT(result < SK_ScalarMax); |
| 895 | return result; |
| 896 | } |
| 897 | |
| 898 | void addAngle(SkTDArray<Angle>& angles, int start, int end) const { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 899 | SkASSERT(start != end); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 900 | Angle* angle = angles.append(); |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 901 | #if DEBUG_ANGLE |
| 902 | if (angles.count() > 1) { |
| 903 | SkPoint angle0Pt, newPt; |
| 904 | (*SegmentXYAtT[angles[0].verb()])(angles[0].pts(), |
| 905 | (*angles[0].spans())[angles[0].start()].fT, &angle0Pt); |
| 906 | (*SegmentXYAtT[fVerb])(fPts, fTs[start].fT, &newPt); |
| 907 | SkASSERT(approximately_equal(angle0Pt.fX, newPt.fX)); |
| 908 | SkASSERT(approximately_equal(angle0Pt.fY, newPt.fY)); |
| 909 | } |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 910 | #endif |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 911 | angle->set(fPts, fVerb, this, start, end, fTs); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 912 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 913 | |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 914 | void addCancelOutsides(double tStart, double oStart, Segment& other, |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 915 | double oEnd) { |
| 916 | int tIndex = -1; |
| 917 | int tCount = fTs.count(); |
| 918 | int oIndex = -1; |
| 919 | int oCount = other.fTs.count(); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 920 | do { |
| 921 | ++tIndex; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 922 | } while (!approximately_negative(tStart - fTs[tIndex].fT) && tIndex < tCount); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 923 | int tIndexStart = tIndex; |
| 924 | do { |
| 925 | ++oIndex; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 926 | } while (!approximately_negative(oStart - other.fTs[oIndex].fT) && oIndex < oCount); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 927 | int oIndexStart = oIndex; |
| 928 | double nextT; |
| 929 | do { |
| 930 | nextT = fTs[++tIndex].fT; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 931 | } while (nextT < 1 && approximately_negative(nextT - tStart)); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 932 | double oNextT; |
| 933 | do { |
| 934 | oNextT = other.fTs[++oIndex].fT; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 935 | } while (oNextT < 1 && approximately_negative(oNextT - oStart)); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 936 | // at this point, spans before and after are at: |
| 937 | // fTs[tIndexStart - 1], fTs[tIndexStart], fTs[tIndex] |
| 938 | // if tIndexStart == 0, no prior span |
| 939 | // if nextT == 1, no following span |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 940 | |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 941 | // advance the span with zero winding |
| 942 | // if the following span exists (not past the end, non-zero winding) |
| 943 | // connect the two edges |
| 944 | if (!fTs[tIndexStart].fWindValue) { |
| 945 | if (tIndexStart > 0 && fTs[tIndexStart - 1].fWindValue) { |
| 946 | #if DEBUG_CONCIDENT |
| 947 | SkDebugf("%s 1 this=%d other=%d t [%d] %1.9g (%1.9g,%1.9g)\n", |
| 948 | __FUNCTION__, fID, other.fID, tIndexStart - 1, |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 949 | fTs[tIndexStart].fT, xyAtT(tIndexStart).fX, |
| 950 | xyAtT(tIndexStart).fY); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 951 | #endif |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 952 | addTPair(fTs[tIndexStart].fT, other, other.fTs[oIndex].fT, false); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 953 | } |
| 954 | if (nextT < 1 && fTs[tIndex].fWindValue) { |
| 955 | #if DEBUG_CONCIDENT |
| 956 | SkDebugf("%s 2 this=%d other=%d t [%d] %1.9g (%1.9g,%1.9g)\n", |
| 957 | __FUNCTION__, fID, other.fID, tIndex, |
| 958 | fTs[tIndex].fT, xyAtT(tIndex).fX, |
| 959 | xyAtT(tIndex).fY); |
| 960 | #endif |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 961 | addTPair(fTs[tIndex].fT, other, other.fTs[oIndexStart].fT, false); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 962 | } |
| 963 | } else { |
| 964 | SkASSERT(!other.fTs[oIndexStart].fWindValue); |
| 965 | if (oIndexStart > 0 && other.fTs[oIndexStart - 1].fWindValue) { |
| 966 | #if DEBUG_CONCIDENT |
| 967 | SkDebugf("%s 3 this=%d other=%d t [%d] %1.9g (%1.9g,%1.9g)\n", |
| 968 | __FUNCTION__, fID, other.fID, oIndexStart - 1, |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 969 | other.fTs[oIndexStart].fT, other.xyAtT(oIndexStart).fX, |
| 970 | other.xyAtT(oIndexStart).fY); |
| 971 | other.debugAddTPair(other.fTs[oIndexStart].fT, *this, fTs[tIndex].fT); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 972 | #endif |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 973 | } |
| 974 | if (oNextT < 1 && other.fTs[oIndex].fWindValue) { |
| 975 | #if DEBUG_CONCIDENT |
| 976 | SkDebugf("%s 4 this=%d other=%d t [%d] %1.9g (%1.9g,%1.9g)\n", |
| 977 | __FUNCTION__, fID, other.fID, oIndex, |
| 978 | other.fTs[oIndex].fT, other.xyAtT(oIndex).fX, |
| 979 | other.xyAtT(oIndex).fY); |
| 980 | other.debugAddTPair(other.fTs[oIndex].fT, *this, fTs[tIndexStart].fT); |
| 981 | #endif |
| 982 | } |
| 983 | } |
| 984 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 985 | |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 986 | void addCoinOutsides(const SkTDArray<double>& outsideTs, Segment& other, |
| 987 | double oEnd) { |
| 988 | // walk this to outsideTs[0] |
| 989 | // walk other to outsideTs[1] |
| 990 | // if either is > 0, add a pointer to the other, copying adjacent winding |
| 991 | int tIndex = -1; |
| 992 | int oIndex = -1; |
| 993 | double tStart = outsideTs[0]; |
| 994 | double oStart = outsideTs[1]; |
| 995 | do { |
| 996 | ++tIndex; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 997 | } while (!approximately_negative(tStart - fTs[tIndex].fT)); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 998 | do { |
| 999 | ++oIndex; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1000 | } while (!approximately_negative(oStart - other.fTs[oIndex].fT)); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1001 | if (tIndex > 0 || oIndex > 0) { |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 1002 | addTPair(tStart, other, oStart, false); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1003 | } |
| 1004 | tStart = fTs[tIndex].fT; |
| 1005 | oStart = other.fTs[oIndex].fT; |
| 1006 | do { |
| 1007 | double nextT; |
| 1008 | do { |
| 1009 | nextT = fTs[++tIndex].fT; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1010 | } while (approximately_negative(nextT - tStart)); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1011 | tStart = nextT; |
| 1012 | do { |
| 1013 | nextT = other.fTs[++oIndex].fT; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1014 | } while (approximately_negative(nextT - oStart)); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1015 | oStart = nextT; |
| 1016 | if (tStart == 1 && oStart == 1) { |
| 1017 | break; |
| 1018 | } |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 1019 | addTPair(tStart, other, oStart, false); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1020 | } while (tStart < 1 && oStart < 1 && !approximately_negative(oEnd - oStart)); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1021 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1022 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1023 | void addCubic(const SkPoint pts[4], bool operand) { |
| 1024 | init(pts, SkPath::kCubic_Verb, operand); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1025 | fBounds.setCubicBounds(pts); |
| 1026 | } |
| 1027 | |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 1028 | // FIXME: this needs to defer add for aligned consecutive line segments |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1029 | SkPoint addCurveTo(int start, int end, SkPath& path, bool active) { |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1030 | SkPoint edge[4]; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1031 | // 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] | 1032 | (*SegmentSubDivide[fVerb])(fPts, fTs[start].fT, fTs[end].fT, edge); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1033 | if (active) { |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 1034 | #if DEBUG_PATH_CONSTRUCTION |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 1035 | SkDebugf("path.%sTo(%1.9g,%1.9g", |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1036 | kLVerbStr[fVerb], edge[1].fX, edge[1].fY); |
| 1037 | if (fVerb > 1) { |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 1038 | SkDebugf(", %1.9g,%1.9g", edge[2].fX, edge[2].fY); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1039 | } |
| 1040 | if (fVerb > 2) { |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 1041 | SkDebugf(", %1.9g,%1.9g", edge[3].fX, edge[3].fY); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1042 | } |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 1043 | SkDebugf(");\n"); |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 1044 | #endif |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1045 | switch (fVerb) { |
| 1046 | case SkPath::kLine_Verb: |
| 1047 | path.lineTo(edge[1].fX, edge[1].fY); |
| 1048 | break; |
| 1049 | case SkPath::kQuad_Verb: |
| 1050 | path.quadTo(edge[1].fX, edge[1].fY, edge[2].fX, edge[2].fY); |
| 1051 | break; |
| 1052 | case SkPath::kCubic_Verb: |
| 1053 | path.cubicTo(edge[1].fX, edge[1].fY, edge[2].fX, edge[2].fY, |
| 1054 | edge[3].fX, edge[3].fY); |
| 1055 | break; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1056 | default: |
| 1057 | SkASSERT(0); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1058 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1059 | } |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 1060 | return edge[fVerb]; |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1063 | void addLine(const SkPoint pts[2], bool operand) { |
| 1064 | init(pts, SkPath::kLine_Verb, operand); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1065 | fBounds.set(pts, 2); |
| 1066 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1067 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1068 | const SkPoint& addMoveTo(int tIndex, SkPath& path, bool active) { |
| 1069 | const SkPoint& pt = xyAtT(tIndex); |
| 1070 | if (active) { |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 1071 | #if DEBUG_PATH_CONSTRUCTION |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 1072 | SkDebugf("path.moveTo(%1.9g, %1.9g);\n", pt.fX, pt.fY); |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 1073 | #endif |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1074 | path.moveTo(pt.fX, pt.fY); |
| 1075 | } |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 1076 | return pt; |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1079 | // 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] | 1080 | void addOtherT(int index, double otherT, int otherIndex) { |
| 1081 | Span& span = fTs[index]; |
| 1082 | span.fOtherT = otherT; |
| 1083 | span.fOtherIndex = otherIndex; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1084 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1085 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1086 | void addQuad(const SkPoint pts[3], bool operand) { |
| 1087 | init(pts, SkPath::kQuad_Verb, operand); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1088 | fBounds.setQuadBounds(pts); |
| 1089 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1090 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1091 | // Defer all coincident edge processing until |
| 1092 | // after normal intersections have been computed |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1093 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1094 | // no need to be tricky; insert in normal T order |
| 1095 | // resolve overlapping ts when considering coincidence later |
| 1096 | |
| 1097 | // add non-coincident intersection. Resulting edges are sorted in T. |
| 1098 | int addT(double newT, Segment* other) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1099 | // FIXME: in the pathological case where there is a ton of intercepts, |
| 1100 | // binary search? |
| 1101 | int insertedAt = -1; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1102 | size_t tCount = fTs.count(); |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 1103 | // FIXME: only do this pinning here (e.g. this is done also in quad/line intersect) |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1104 | if (approximately_less_than_zero(newT)) { |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 1105 | newT = 0; |
| 1106 | } |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1107 | if (approximately_greater_than_one(newT)) { |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 1108 | newT = 1; |
| 1109 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1110 | for (size_t index = 0; index < tCount; ++index) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1111 | // OPTIMIZATION: if there are three or more identical Ts, then |
| 1112 | // the fourth and following could be further insertion-sorted so |
| 1113 | // that all the edges are clockwise or counterclockwise. |
| 1114 | // This could later limit segment tests to the two adjacent |
| 1115 | // neighbors, although it doesn't help with determining which |
| 1116 | // circular direction to go in. |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1117 | if (newT < fTs[index].fT) { |
| 1118 | insertedAt = index; |
| 1119 | break; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1120 | } |
| 1121 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1122 | Span* span; |
| 1123 | if (insertedAt >= 0) { |
| 1124 | span = fTs.insert(insertedAt); |
| 1125 | } else { |
| 1126 | insertedAt = tCount; |
| 1127 | span = fTs.append(); |
| 1128 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1129 | span->fT = newT; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1130 | span->fOther = other; |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 1131 | span->fPt.fX = SK_ScalarNaN; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1132 | span->fWindSum = SK_MinS32; |
| 1133 | span->fWindValue = 1; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1134 | span->fWindValueOpp = 0; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1135 | if ((span->fDone = newT == 1)) { |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 1136 | ++fDoneSpans; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1137 | } |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 1138 | span->fUnsortableStart = false; |
| 1139 | span->fUnsortableEnd = false; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1140 | return insertedAt; |
| 1141 | } |
| 1142 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1143 | // set spans from start to end to decrement by one |
| 1144 | // note this walks other backwards |
| 1145 | // FIMXE: there's probably an edge case that can be constructed where |
| 1146 | // two span in one segment are separated by float epsilon on one span but |
| 1147 | // not the other, if one segment is very small. For this |
| 1148 | // 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] | 1149 | // 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] | 1150 | // sorted? It feels better in such a case to match the span's other span |
| 1151 | // pointer since both coincident segments must contain the same spans. |
| 1152 | void addTCancel(double startT, double endT, Segment& other, |
| 1153 | double oStartT, double oEndT) { |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1154 | SkASSERT(!approximately_negative(endT - startT)); |
| 1155 | SkASSERT(!approximately_negative(oEndT - oStartT)); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1156 | bool binary = fOperand != other.fOperand; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1157 | int index = 0; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1158 | while (!approximately_negative(startT - fTs[index].fT)) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1159 | ++index; |
| 1160 | } |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 1161 | int oIndex = other.fTs.count(); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1162 | while (approximately_positive(other.fTs[--oIndex].fT - oEndT)) |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1163 | ; |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 1164 | double tRatio = (oEndT - oStartT) / (endT - startT); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1165 | Span* test = &fTs[index]; |
| 1166 | Span* oTest = &other.fTs[oIndex]; |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 1167 | SkTDArray<double> outsideTs; |
| 1168 | SkTDArray<double> oOutsideTs; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1169 | do { |
| 1170 | bool decrement = test->fWindValue && oTest->fWindValue; |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1171 | bool track = test->fWindValue || oTest->fWindValue; |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 1172 | double testT = test->fT; |
| 1173 | double oTestT = oTest->fT; |
| 1174 | Span* span = test; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1175 | do { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1176 | if (decrement) { |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1177 | if (binary) { |
| 1178 | --(span->fWindValueOpp); |
| 1179 | } else { |
| 1180 | decrementSpan(span); |
| 1181 | } |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 1182 | } else if (track && span->fT < 1 && oTestT < 1) { |
| 1183 | TrackOutside(outsideTs, span->fT, oTestT); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1184 | } |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 1185 | span = &fTs[++index]; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1186 | } while (approximately_negative(span->fT - testT)); |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 1187 | Span* oSpan = oTest; |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 1188 | double otherTMatchStart = oEndT - (span->fT - startT) * tRatio; |
| 1189 | double otherTMatchEnd = oEndT - (test->fT - startT) * tRatio; |
| 1190 | SkDEBUGCODE(int originalWindValue = oSpan->fWindValue); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1191 | while (approximately_negative(otherTMatchStart - oSpan->fT) |
| 1192 | && !approximately_negative(otherTMatchEnd - oSpan->fT)) { |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 1193 | #ifdef SK_DEBUG |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 1194 | SkASSERT(originalWindValue == oSpan->fWindValue); |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 1195 | #endif |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1196 | if (decrement) { |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 1197 | other.decrementSpan(oSpan); |
| 1198 | } else if (track && oSpan->fT < 1 && testT < 1) { |
| 1199 | TrackOutside(oOutsideTs, oSpan->fT, testT); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1200 | } |
| 1201 | if (!oIndex) { |
| 1202 | break; |
| 1203 | } |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 1204 | oSpan = &other.fTs[--oIndex]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1205 | } |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 1206 | test = span; |
| 1207 | oTest = oSpan; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1208 | } while (!approximately_negative(endT - test->fT)); |
| 1209 | SkASSERT(!oIndex || approximately_negative(oTest->fT - oStartT)); |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 1210 | // FIXME: determine if canceled edges need outside ts added |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1211 | if (!done() && outsideTs.count()) { |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 1212 | double tStart = outsideTs[0]; |
| 1213 | double oStart = outsideTs[1]; |
| 1214 | addCancelOutsides(tStart, oStart, other, oEndT); |
| 1215 | int count = outsideTs.count(); |
| 1216 | if (count > 2) { |
| 1217 | double tStart = outsideTs[count - 2]; |
| 1218 | double oStart = outsideTs[count - 1]; |
| 1219 | addCancelOutsides(tStart, oStart, other, oEndT); |
| 1220 | } |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 1221 | } |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1222 | if (!other.done() && oOutsideTs.count()) { |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 1223 | double tStart = oOutsideTs[0]; |
| 1224 | double oStart = oOutsideTs[1]; |
| 1225 | other.addCancelOutsides(tStart, oStart, *this, endT); |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 1226 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | // set spans from start to end to increment the greater by one and decrement |
| 1230 | // the lesser |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1231 | void addTCoincident(const bool isXor, double startT, double endT, |
| 1232 | Segment& other, double oStartT, double oEndT) { |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1233 | SkASSERT(!approximately_negative(endT - startT)); |
| 1234 | SkASSERT(!approximately_negative(oEndT - oStartT)); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1235 | bool binary = fOperand != other.fOperand; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1236 | int index = 0; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1237 | while (!approximately_negative(startT - fTs[index].fT)) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1238 | ++index; |
| 1239 | } |
| 1240 | int oIndex = 0; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1241 | while (!approximately_negative(oStartT - other.fTs[oIndex].fT)) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1242 | ++oIndex; |
| 1243 | } |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 1244 | double tRatio = (oEndT - oStartT) / (endT - startT); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1245 | Span* test = &fTs[index]; |
| 1246 | Span* oTest = &other.fTs[oIndex]; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1247 | SkTDArray<double> outsideTs; |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1248 | SkTDArray<double> xOutsideTs; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1249 | SkTDArray<double> oOutsideTs; |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1250 | SkTDArray<double> oxOutsideTs; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1251 | do { |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 1252 | bool transfer = test->fWindValue && oTest->fWindValue; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1253 | bool decrementThis = (test->fWindValue < oTest->fWindValue) & !isXor; |
| 1254 | bool decrementOther = (test->fWindValue >= oTest->fWindValue) & !isXor; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1255 | Span* end = test; |
| 1256 | double startT = end->fT; |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1257 | int startIndex = index; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1258 | double oStartT = oTest->fT; |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1259 | int oStartIndex = oIndex; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1260 | do { |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 1261 | if (transfer) { |
| 1262 | if (decrementOther) { |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 1263 | #ifdef SK_DEBUG |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 1264 | SkASSERT(abs(end->fWindValue) < gDebugMaxWindValue); |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 1265 | #endif |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1266 | if (binary) { |
| 1267 | ++(end->fWindValueOpp); |
| 1268 | } else { |
| 1269 | ++(end->fWindValue); |
| 1270 | } |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 1271 | } else if (decrementSpan(end)) { |
| 1272 | TrackOutside(outsideTs, end->fT, oStartT); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1273 | } |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1274 | } else if (oTest->fWindValue) { |
| 1275 | SkASSERT(!decrementOther); |
| 1276 | if (startIndex > 0 && fTs[startIndex - 1].fWindValue) { |
| 1277 | TrackOutside(xOutsideTs, end->fT, oStartT); |
| 1278 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1279 | } |
| 1280 | end = &fTs[++index]; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1281 | } while (approximately_negative(end->fT - test->fT)); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 1282 | // because of the order in which coincidences are resolved, this and other |
| 1283 | // may not have the same intermediate points. Compute the corresponding |
| 1284 | // intermediate T values (using this as the master, other as the follower) |
| 1285 | // and walk other conditionally -- hoping that it catches up in the end |
| 1286 | double otherTMatch = (test->fT - startT) * tRatio + oStartT; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1287 | Span* oEnd = oTest; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1288 | while (!approximately_negative(oEndT - oEnd->fT) && approximately_negative(oEnd->fT - otherTMatch)) { |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 1289 | if (transfer) { |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 1290 | if (decrementThis) { |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 1291 | #ifdef SK_DEBUG |
| 1292 | SkASSERT(abs(oEnd->fWindValue) < gDebugMaxWindValue); |
| 1293 | #endif |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1294 | if (binary) { |
| 1295 | ++(oEnd->fWindValueOpp); |
| 1296 | } else { |
| 1297 | ++(oEnd->fWindValue); |
| 1298 | } |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 1299 | } else if (other.decrementSpan(oEnd)) { |
| 1300 | TrackOutside(oOutsideTs, oEnd->fT, startT); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1301 | } |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1302 | } else if (test->fWindValue) { |
| 1303 | SkASSERT(!decrementOther); |
| 1304 | if (oStartIndex > 0 && other.fTs[oStartIndex - 1].fWindValue) { |
| 1305 | SkASSERT(0); // track for later? |
| 1306 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1307 | } |
| 1308 | oEnd = &other.fTs[++oIndex]; |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 1309 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1310 | test = end; |
| 1311 | oTest = oEnd; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1312 | } while (!approximately_negative(endT - test->fT)); |
| 1313 | SkASSERT(approximately_negative(oTest->fT - oEndT)); |
| 1314 | SkASSERT(approximately_negative(oEndT - oTest->fT)); |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1315 | if (!done()) { |
| 1316 | if (outsideTs.count()) { |
| 1317 | addCoinOutsides(outsideTs, other, oEndT); |
| 1318 | } |
| 1319 | if (xOutsideTs.count()) { |
| 1320 | addCoinOutsides(xOutsideTs, other, oEndT); |
| 1321 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1322 | } |
| 1323 | if (!other.done() && oOutsideTs.count()) { |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1324 | other.addCoinOutsides(oOutsideTs, *this, endT); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1325 | } |
| 1326 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1327 | |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1328 | // FIXME: this doesn't prevent the same span from being added twice |
| 1329 | // fix in caller, assert here? |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 1330 | void addTPair(double t, Segment& other, double otherT, bool borrowWind) { |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1331 | int tCount = fTs.count(); |
| 1332 | for (int tIndex = 0; tIndex < tCount; ++tIndex) { |
| 1333 | const Span& span = fTs[tIndex]; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1334 | if (!approximately_negative(span.fT - t)) { |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1335 | break; |
| 1336 | } |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 1337 | if (approximately_negative(span.fT - t) && span.fOther == &other |
| 1338 | && approximately_equal(span.fOtherT, otherT)) { |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 1339 | #if DEBUG_ADD_T_PAIR |
| 1340 | SkDebugf("%s addTPair duplicate this=%d %1.9g other=%d %1.9g\n", |
| 1341 | __FUNCTION__, fID, t, other.fID, otherT); |
| 1342 | #endif |
| 1343 | return; |
| 1344 | } |
| 1345 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1346 | #if DEBUG_ADD_T_PAIR |
| 1347 | SkDebugf("%s addTPair this=%d %1.9g other=%d %1.9g\n", |
| 1348 | __FUNCTION__, fID, t, other.fID, otherT); |
| 1349 | #endif |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 1350 | int insertedAt = addT(t, &other); |
| 1351 | int otherInsertedAt = other.addT(otherT, this); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1352 | addOtherT(insertedAt, otherT, otherInsertedAt); |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 1353 | other.addOtherT(otherInsertedAt, t, insertedAt); |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 1354 | matchWindingValue(insertedAt, t, borrowWind); |
| 1355 | other.matchWindingValue(otherInsertedAt, otherT, borrowWind); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1356 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1357 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1358 | void addTwoAngles(int start, int end, SkTDArray<Angle>& angles) const { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1359 | // add edge leading into junction |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1360 | if (fTs[SkMin32(end, start)].fWindValue > 0) { |
| 1361 | addAngle(angles, end, start); |
| 1362 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1363 | // add edge leading away from junction |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1364 | int step = SkSign32(end - start); |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 1365 | int tIndex = nextExactSpan(end, step); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1366 | if (tIndex >= 0 && fTs[SkMin32(end, tIndex)].fWindValue > 0) { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1367 | addAngle(angles, end, tIndex); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1368 | } |
| 1369 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1370 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1371 | const Bounds& bounds() const { |
| 1372 | return fBounds; |
| 1373 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1374 | |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1375 | void buildAngles(int index, SkTDArray<Angle>& angles) const { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1376 | double referenceT = fTs[index].fT; |
| 1377 | int lesser = index; |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 1378 | #if PRECISE_T_SORT |
| 1379 | while (--lesser >= 0 && precisely_negative(referenceT - fTs[lesser].fT)) { |
| 1380 | buildAnglesInner(lesser, angles); |
| 1381 | } |
| 1382 | do { |
| 1383 | buildAnglesInner(index, angles); |
| 1384 | } while (++index < fTs.count() && precisely_negative(fTs[index].fT - referenceT)); |
| 1385 | #else |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1386 | while (--lesser >= 0 && approximately_negative(referenceT - fTs[lesser].fT)) { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1387 | buildAnglesInner(lesser, angles); |
| 1388 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1389 | do { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1390 | buildAnglesInner(index, angles); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 1391 | } while (++index < fTs.count() && approximately_negative(fTs[index].fT - referenceT)); |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 1392 | #endif |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1393 | } |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1394 | |
| 1395 | void buildAnglesInner(int index, SkTDArray<Angle>& angles) const { |
| 1396 | Span* span = &fTs[index]; |
| 1397 | Segment* other = span->fOther; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1398 | // if there is only one live crossing, and no coincidence, continue |
| 1399 | // in the same direction |
| 1400 | // if there is coincidence, the only choice may be to reverse direction |
| 1401 | // find edge on either side of intersection |
| 1402 | int oIndex = span->fOtherIndex; |
| 1403 | // if done == -1, prior span has already been processed |
| 1404 | int step = 1; |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 1405 | #if PRECISE_T_SORT |
| 1406 | int next = other->nextExactSpan(oIndex, step); |
| 1407 | #else |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1408 | int next = other->nextSpan(oIndex, step); |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 1409 | #endif |
| 1410 | if (next < 0) { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1411 | step = -step; |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 1412 | #if PRECISE_T_SORT |
| 1413 | next = other->nextExactSpan(oIndex, step); |
| 1414 | #else |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1415 | next = other->nextSpan(oIndex, step); |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 1416 | #endif |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1417 | } |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1418 | // add candidate into and away from junction |
| 1419 | other->addTwoAngles(next, oIndex, angles); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1422 | // figure out if the segment's ascending T goes clockwise or not |
| 1423 | // not enough context to write this as shown |
| 1424 | // instead, add all segments meeting at the top |
| 1425 | // sort them using buildAngleList |
| 1426 | // find the first in the sort |
| 1427 | // see if ascendingT goes to top |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 1428 | bool clockwise(int /* tIndex */) const { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1429 | SkASSERT(0); // incomplete |
| 1430 | return false; |
| 1431 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1432 | |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 1433 | // FIXME may not need this at all |
| 1434 | // FIXME once I figure out the logic, merge this and too close to call |
| 1435 | // NOTE not sure if tiny triangles can ever form at the edge, so until we |
| 1436 | // see one, only worry about triangles that happen away from 0 and 1 |
| 1437 | void collapseTriangles(bool isXor) { |
| 1438 | if (fTs.count() < 3) { // require t=0, x, 1 at minimum |
| 1439 | return; |
| 1440 | } |
| 1441 | int lastIndex = 1; |
| 1442 | double lastT; |
| 1443 | while (approximately_less_than_zero((lastT = fTs[lastIndex].fT))) { |
| 1444 | ++lastIndex; |
| 1445 | } |
| 1446 | if (approximately_greater_than_one(lastT)) { |
| 1447 | return; |
| 1448 | } |
| 1449 | int matchIndex = lastIndex; |
| 1450 | do { |
| 1451 | Span& match = fTs[++matchIndex]; |
| 1452 | double matchT = match.fT; |
| 1453 | if (approximately_greater_than_one(matchT)) { |
| 1454 | return; |
| 1455 | } |
| 1456 | if (matchT == lastT) { |
| 1457 | goto nextSpan; |
| 1458 | } |
| 1459 | if (approximately_negative(matchT - lastT)) { |
| 1460 | Span& last = fTs[lastIndex]; |
| 1461 | Segment* lOther = last.fOther; |
| 1462 | double lT = last.fOtherT; |
| 1463 | if (approximately_less_than_zero(lT) || approximately_greater_than_one(lT)) { |
| 1464 | goto nextSpan; |
| 1465 | } |
| 1466 | Segment* mOther = match.fOther; |
| 1467 | double mT = match.fOtherT; |
| 1468 | if (approximately_less_than_zero(mT) || approximately_greater_than_one(mT)) { |
| 1469 | goto nextSpan; |
| 1470 | } |
| 1471 | // add new point to connect adjacent spurs |
| 1472 | int count = lOther->fTs.count(); |
| 1473 | for (int index = 0; index < count; ++index) { |
| 1474 | Span& span = lOther->fTs[index]; |
| 1475 | if (span.fOther == mOther && approximately_zero(span.fOtherT - mT)) { |
| 1476 | goto nextSpan; |
| 1477 | } |
| 1478 | } |
| 1479 | mOther->addTPair(mT, *lOther, lT, false); |
| 1480 | // FIXME ? this could go on to detect that spans on mOther, lOther are now coincident |
| 1481 | } |
| 1482 | nextSpan: |
| 1483 | lastIndex = matchIndex; |
| 1484 | lastT = matchT; |
| 1485 | } while (true); |
| 1486 | } |
| 1487 | |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1488 | int computeSum(int startIndex, int endIndex) { |
| 1489 | SkTDArray<Angle> angles; |
| 1490 | addTwoAngles(startIndex, endIndex, angles); |
| 1491 | buildAngles(endIndex, angles); |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 1492 | // OPTIMIZATION: check all angles to see if any have computed wind sum |
| 1493 | // before sorting (early exit if none) |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1494 | SkTDArray<Angle*> sorted; |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 1495 | bool sortable = SortAngles(angles, sorted); |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 1496 | #if DEBUG_SORT |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1497 | sorted[0]->segment()->debugShowSort(__FUNCTION__, sorted, 0, 0); |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 1498 | #endif |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 1499 | if (!sortable) { |
| 1500 | return SK_MinS32; |
| 1501 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1502 | int angleCount = angles.count(); |
| 1503 | const Angle* angle; |
| 1504 | const Segment* base; |
| 1505 | int winding; |
| 1506 | int firstIndex = 0; |
| 1507 | do { |
| 1508 | angle = sorted[firstIndex]; |
| 1509 | base = angle->segment(); |
| 1510 | winding = base->windSum(angle); |
| 1511 | if (winding != SK_MinS32) { |
| 1512 | break; |
| 1513 | } |
| 1514 | if (++firstIndex == angleCount) { |
| 1515 | return SK_MinS32; |
| 1516 | } |
| 1517 | } while (true); |
| 1518 | // turn winding into contourWinding |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 1519 | int spanWinding = base->spanSign(angle); |
| 1520 | bool inner = useInnerWinding(winding + spanWinding, winding); |
| 1521 | #if DEBUG_WINDING |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 1522 | SkDebugf("%s spanWinding=%d winding=%d sign=%d inner=%d result=%d\n", __FUNCTION__, |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 1523 | spanWinding, winding, angle->sign(), inner, |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 1524 | inner ? winding + spanWinding : winding); |
| 1525 | #endif |
| 1526 | if (inner) { |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1527 | winding += spanWinding; |
| 1528 | } |
| 1529 | #if DEBUG_SORT |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 1530 | base->debugShowSort(__FUNCTION__, sorted, firstIndex, winding); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1531 | #endif |
| 1532 | int nextIndex = firstIndex + 1; |
| 1533 | int lastIndex = firstIndex != 0 ? firstIndex : angleCount; |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 1534 | winding -= base->spanSign(angle); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1535 | do { |
| 1536 | if (nextIndex == angleCount) { |
| 1537 | nextIndex = 0; |
| 1538 | } |
| 1539 | angle = sorted[nextIndex]; |
| 1540 | Segment* segment = angle->segment(); |
| 1541 | int maxWinding = winding; |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 1542 | winding -= segment->spanSign(angle); |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 1543 | if (segment->windSum(angle) == SK_MinS32) { |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 1544 | if (useInnerWinding(maxWinding, winding)) { |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1545 | maxWinding = winding; |
| 1546 | } |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 1547 | segment->markAndChaseWinding(angle, maxWinding); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1548 | } |
| 1549 | } while (++nextIndex != lastIndex); |
| 1550 | return windSum(SkMin32(startIndex, endIndex)); |
| 1551 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1552 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1553 | int crossedSpan(const SkPoint& basePt, SkScalar& bestY, double& hitT) const { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1554 | int bestT = -1; |
| 1555 | SkScalar top = bounds().fTop; |
| 1556 | SkScalar bottom = bounds().fBottom; |
caryclark@google.com | 210acaf | 2012-07-12 21:05:13 +0000 | [diff] [blame] | 1557 | int end = 0; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1558 | do { |
caryclark@google.com | 210acaf | 2012-07-12 21:05:13 +0000 | [diff] [blame] | 1559 | int start = end; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1560 | end = nextSpan(start, 1); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1561 | if (fTs[start].fWindValue == 0) { |
| 1562 | continue; |
| 1563 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1564 | SkPoint edge[4]; |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 1565 | double startT = fTs[start].fT; |
| 1566 | double endT = fTs[end].fT; |
| 1567 | (*SegmentSubDivide[fVerb])(fPts, startT, endT, edge); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 1568 | // intersect ray starting at basePt with edge |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1569 | Intersections intersections; |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 1570 | // FIXME: always use original and limit results to T values within |
| 1571 | // start t and end t. |
| 1572 | // OPTIMIZE: use specialty function that intersects ray with curve, |
| 1573 | // returning t values only for curve (we don't care about t on ray) |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1574 | int pts = (*VSegmentIntersect[fVerb])(edge, top, bottom, basePt.fX, |
| 1575 | false, intersections); |
| 1576 | if (pts == 0) { |
| 1577 | continue; |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1578 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1579 | if (pts > 1 && fVerb == SkPath::kLine_Verb) { |
| 1580 | // if the intersection is edge on, wait for another one |
| 1581 | continue; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1582 | } |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1583 | for (int index = 0; index < pts; ++index) { |
| 1584 | SkPoint pt; |
| 1585 | double foundT = intersections.fT[0][index]; |
| 1586 | double testT = startT + (endT - startT) * foundT; |
| 1587 | (*SegmentXYAtT[fVerb])(fPts, testT, &pt); |
| 1588 | if (bestY < pt.fY && pt.fY < basePt.fY) { |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 1589 | if (fVerb > SkPath::kLine_Verb |
| 1590 | && !approximately_less_than_zero(foundT) |
| 1591 | && !approximately_greater_than_one(foundT)) { |
| 1592 | SkScalar dx = (*SegmentDXAtT[fVerb])(fPts, testT); |
| 1593 | if (approximately_zero(dx)) { |
| 1594 | continue; |
| 1595 | } |
| 1596 | } |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1597 | bestY = pt.fY; |
| 1598 | bestT = foundT < 1 ? start : end; |
| 1599 | hitT = testT; |
| 1600 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1601 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1602 | } while (fTs[end].fT != 1); |
| 1603 | return bestT; |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1604 | } |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 1605 | |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1606 | bool crossedSpanHalves(const SkPoint& basePt, bool leftHalf, bool rightHalf) { |
| 1607 | // if a segment is connected to this one, consider it crossing |
| 1608 | int tIndex; |
| 1609 | if (fPts[0].fX == basePt.fX) { |
| 1610 | tIndex = 0; |
| 1611 | do { |
| 1612 | const Span& sSpan = fTs[tIndex]; |
| 1613 | const Segment* sOther = sSpan.fOther; |
| 1614 | if (!sOther->fTs[sSpan.fOtherIndex].fWindValue) { |
| 1615 | continue; |
| 1616 | } |
| 1617 | if (leftHalf ? sOther->fBounds.fLeft < basePt.fX |
| 1618 | : sOther->fBounds.fRight > basePt.fX) { |
| 1619 | return true; |
| 1620 | } |
| 1621 | } while (fTs[++tIndex].fT == 0); |
| 1622 | } |
| 1623 | if (fPts[fVerb].fX == basePt.fX) { |
| 1624 | tIndex = fTs.count() - 1; |
| 1625 | do { |
| 1626 | const Span& eSpan = fTs[tIndex]; |
| 1627 | const Segment* eOther = eSpan.fOther; |
| 1628 | if (!eOther->fTs[eSpan.fOtherIndex].fWindValue) { |
| 1629 | continue; |
| 1630 | } |
| 1631 | if (leftHalf ? eOther->fBounds.fLeft < basePt.fX |
| 1632 | : eOther->fBounds.fRight > basePt.fX) { |
| 1633 | return true; |
| 1634 | } |
| 1635 | } while (fTs[--tIndex].fT == 1); |
| 1636 | } |
| 1637 | return false; |
| 1638 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1639 | |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 1640 | bool decrementSpan(Span* span) { |
| 1641 | SkASSERT(span->fWindValue > 0); |
| 1642 | if (--(span->fWindValue) == 0) { |
| 1643 | span->fDone = true; |
| 1644 | ++fDoneSpans; |
| 1645 | return true; |
| 1646 | } |
| 1647 | return false; |
| 1648 | } |
| 1649 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1650 | bool done() const { |
caryclark@google.com | af46cff | 2012-05-22 21:12:00 +0000 | [diff] [blame] | 1651 | SkASSERT(fDoneSpans <= fTs.count()); |
| 1652 | return fDoneSpans == fTs.count(); |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1655 | bool done(const Angle& angle) const { |
| 1656 | int start = angle.start(); |
| 1657 | int end = angle.end(); |
| 1658 | const Span& mSpan = fTs[SkMin32(start, end)]; |
| 1659 | return mSpan.fDone; |
| 1660 | } |
skia.committer@gmail.com | 055c7c2 | 2012-09-15 02:01:41 +0000 | [diff] [blame] | 1661 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1662 | Segment* findNextOp(SkTDArray<Span*>& chase, bool active, |
skia.committer@gmail.com | 20c301b | 2012-10-17 02:01:13 +0000 | [diff] [blame^] | 1663 | int& nextStart, int& nextEnd, int& winding, int& spanWinding, |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 1664 | bool& unsortable, ShapeOp op, |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1665 | const int aXorMask, const int bXorMask) { |
| 1666 | const int startIndex = nextStart; |
| 1667 | const int endIndex = nextEnd; |
| 1668 | int outerWinding = winding; |
| 1669 | int innerWinding = winding + spanWinding; |
| 1670 | #if DEBUG_WINDING |
| 1671 | SkDebugf("%s winding=%d spanWinding=%d outerWinding=%d innerWinding=%d\n", |
| 1672 | __FUNCTION__, winding, spanWinding, outerWinding, innerWinding); |
| 1673 | #endif |
| 1674 | if (useInnerWinding(outerWinding, innerWinding)) { |
| 1675 | outerWinding = innerWinding; |
| 1676 | } |
| 1677 | SkASSERT(startIndex != endIndex); |
| 1678 | int count = fTs.count(); |
| 1679 | SkASSERT(startIndex < endIndex ? startIndex < count - 1 |
| 1680 | : startIndex > 0); |
| 1681 | int step = SkSign32(endIndex - startIndex); |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 1682 | #if PRECISE_T_SORT |
| 1683 | int end = nextExactSpan(startIndex, step); |
| 1684 | #else |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1685 | int end = nextSpan(startIndex, step); |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 1686 | #endif |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1687 | SkASSERT(end >= 0); |
| 1688 | Span* endSpan = &fTs[end]; |
| 1689 | Segment* other; |
| 1690 | if (isSimple(end)) { |
| 1691 | // mark the smaller of startIndex, endIndex done, and all adjacent |
| 1692 | // spans with the same T value (but not 'other' spans) |
| 1693 | #if DEBUG_WINDING |
| 1694 | SkDebugf("%s simple\n", __FUNCTION__); |
| 1695 | #endif |
| 1696 | markDone(SkMin32(startIndex, endIndex), outerWinding); |
| 1697 | other = endSpan->fOther; |
| 1698 | nextStart = endSpan->fOtherIndex; |
| 1699 | double startT = other->fTs[nextStart].fT; |
| 1700 | nextEnd = nextStart; |
| 1701 | do { |
| 1702 | nextEnd += step; |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 1703 | } |
| 1704 | #if PRECISE_T_SORT |
| 1705 | while (precisely_zero(startT - other->fTs[nextEnd].fT)); |
| 1706 | #else |
| 1707 | while (approximately_zero(startT - other->fTs[nextEnd].fT)); |
| 1708 | #endif |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1709 | SkASSERT(step < 0 ? nextEnd >= 0 : nextEnd < other->fTs.count()); |
| 1710 | return other; |
| 1711 | } |
| 1712 | // more than one viable candidate -- measure angles to find best |
| 1713 | SkTDArray<Angle> angles; |
| 1714 | SkASSERT(startIndex - endIndex != 0); |
| 1715 | SkASSERT((startIndex - endIndex < 0) ^ (step < 0)); |
| 1716 | addTwoAngles(startIndex, end, angles); |
| 1717 | buildAngles(end, angles); |
| 1718 | SkTDArray<Angle*> sorted; |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 1719 | bool sortable = SortAngles(angles, sorted); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1720 | int angleCount = angles.count(); |
| 1721 | int firstIndex = findStartingEdge(sorted, startIndex, end); |
| 1722 | SkASSERT(firstIndex >= 0); |
| 1723 | #if DEBUG_SORT |
| 1724 | debugShowSort(__FUNCTION__, sorted, firstIndex, winding); |
| 1725 | #endif |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 1726 | if (!sortable) { |
| 1727 | unsortable = true; |
| 1728 | return NULL; |
| 1729 | } |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1730 | SkASSERT(sorted[firstIndex]->segment() == this); |
| 1731 | #if DEBUG_WINDING |
| 1732 | SkDebugf("%s [%d] sign=%d\n", __FUNCTION__, firstIndex, sorted[firstIndex]->sign()); |
| 1733 | #endif |
| 1734 | int aSumWinding = winding; |
| 1735 | int bSumWinding = winding; |
| 1736 | bool angleIsOp = sorted[firstIndex]->segment()->operand(); |
| 1737 | int angleSpan = spanSign(sorted[firstIndex]); |
| 1738 | if (angleIsOp) { |
| 1739 | bSumWinding -= angleSpan; |
| 1740 | } else { |
| 1741 | aSumWinding -= angleSpan; |
| 1742 | } |
| 1743 | int nextIndex = firstIndex + 1; |
| 1744 | int lastIndex = firstIndex != 0 ? firstIndex : angleCount; |
| 1745 | const Angle* foundAngle = NULL; |
| 1746 | // FIXME: found done logic probably fails if there are more than 4 |
| 1747 | // sorted angles. It should bias towards the first and last undone |
| 1748 | // edges -- but not sure that it won't choose a middle (incorrect) |
| 1749 | // edge if one is undone |
| 1750 | bool foundDone = false; |
| 1751 | bool foundDone2 = false; |
| 1752 | // iterate through the angle, and compute everyone's winding |
| 1753 | bool altFlipped = false; |
| 1754 | bool foundFlipped = false; |
| 1755 | int foundMax = SK_MinS32; |
| 1756 | int foundSum = SK_MinS32; |
| 1757 | Segment* nextSegment; |
| 1758 | int lastNonZeroSum = winding; |
| 1759 | do { |
| 1760 | if (nextIndex == angleCount) { |
| 1761 | nextIndex = 0; |
| 1762 | } |
| 1763 | const Angle* nextAngle = sorted[nextIndex]; |
| 1764 | nextSegment = nextAngle->segment(); |
| 1765 | angleIsOp = nextSegment->operand(); |
| 1766 | int sumWinding = angleIsOp ? bSumWinding : aSumWinding; |
| 1767 | int maxWinding = sumWinding; |
| 1768 | if (sumWinding) { |
| 1769 | lastNonZeroSum = sumWinding; |
| 1770 | } |
| 1771 | sumWinding -= nextSegment->spanSign(nextAngle); |
| 1772 | int xorMask = nextSegment->operand() ? bXorMask : aXorMask; |
| 1773 | bool otherNonZero; |
| 1774 | if (angleIsOp) { |
| 1775 | bSumWinding = sumWinding; |
| 1776 | otherNonZero = aSumWinding & aXorMask; |
| 1777 | } else { |
| 1778 | aSumWinding = sumWinding; |
| 1779 | otherNonZero = bSumWinding & bXorMask; |
| 1780 | } |
| 1781 | altFlipped ^= lastNonZeroSum * sumWinding < 0; // flip if different signs |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 1782 | #if 0 && DEBUG_WINDING |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1783 | SkASSERT(abs(sumWinding) <= gDebugMaxWindSum); |
| 1784 | SkDebugf("%s [%d] maxWinding=%d sumWinding=%d sign=%d altFlipped=%d\n", __FUNCTION__, |
| 1785 | nextIndex, maxWinding, sumWinding, nextAngle->sign(), altFlipped); |
| 1786 | #endif |
skia.committer@gmail.com | 055c7c2 | 2012-09-15 02:01:41 +0000 | [diff] [blame] | 1787 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1788 | if (!(sumWinding & xorMask) && activeOp(angleIsOp, otherNonZero, op)) { |
| 1789 | if (!active) { |
| 1790 | markDone(SkMin32(startIndex, endIndex), outerWinding); |
| 1791 | // FIXME: seems like a bug that this isn't calling userInnerWinding |
| 1792 | nextSegment->markWinding(SkMin32(nextAngle->start(), |
| 1793 | nextAngle->end()), maxWinding); |
| 1794 | #if DEBUG_WINDING |
| 1795 | SkDebugf("%s [%d] inactive\n", __FUNCTION__, nextIndex); |
| 1796 | #endif |
| 1797 | return NULL; |
| 1798 | } |
| 1799 | if (!foundAngle || foundDone) { |
| 1800 | foundAngle = nextAngle; |
| 1801 | foundDone = nextSegment->done(*nextAngle); |
| 1802 | foundFlipped = altFlipped; |
| 1803 | foundMax = maxWinding; |
| 1804 | } |
| 1805 | continue; |
| 1806 | } |
| 1807 | if (!(maxWinding & xorMask) && (!foundAngle || foundDone2) |
| 1808 | && activeOp(angleIsOp, otherNonZero, op)) { |
| 1809 | #if DEBUG_WINDING |
| 1810 | if (foundAngle && foundDone2) { |
| 1811 | SkDebugf("%s [%d] !foundAngle && foundDone2\n", __FUNCTION__, nextIndex); |
| 1812 | } |
| 1813 | #endif |
| 1814 | foundAngle = nextAngle; |
| 1815 | foundDone2 = nextSegment->done(*nextAngle); |
| 1816 | foundFlipped = altFlipped; |
| 1817 | foundSum = sumWinding; |
| 1818 | } |
| 1819 | if (nextSegment->done()) { |
| 1820 | continue; |
| 1821 | } |
| 1822 | // if the winding is non-zero, nextAngle does not connect to |
| 1823 | // current chain. If we haven't done so already, mark the angle |
| 1824 | // as done, record the winding value, and mark connected unambiguous |
| 1825 | // segments as well. |
| 1826 | if (nextSegment->windSum(nextAngle) == SK_MinS32) { |
| 1827 | if (useInnerWinding(maxWinding, sumWinding)) { |
| 1828 | maxWinding = sumWinding; |
| 1829 | } |
| 1830 | Span* last; |
| 1831 | if (foundAngle) { |
| 1832 | last = nextSegment->markAndChaseWinding(nextAngle, maxWinding); |
| 1833 | } else { |
| 1834 | last = nextSegment->markAndChaseDone(nextAngle, maxWinding); |
| 1835 | } |
| 1836 | if (last) { |
| 1837 | *chase.append() = last; |
| 1838 | } |
| 1839 | } |
| 1840 | } while (++nextIndex != lastIndex); |
| 1841 | markDone(SkMin32(startIndex, endIndex), outerWinding); |
| 1842 | if (!foundAngle) { |
| 1843 | return NULL; |
| 1844 | } |
| 1845 | nextStart = foundAngle->start(); |
| 1846 | nextEnd = foundAngle->end(); |
| 1847 | nextSegment = foundAngle->segment(); |
| 1848 | int flipped = foundFlipped ? -1 : 1; |
| 1849 | spanWinding = SkSign32(spanWinding) * flipped * nextSegment->windValue( |
| 1850 | SkMin32(nextStart, nextEnd)); |
| 1851 | if (winding) { |
| 1852 | #if DEBUG_WINDING |
| 1853 | SkDebugf("%s ---6 winding=%d foundSum=", __FUNCTION__, winding); |
| 1854 | if (foundSum == SK_MinS32) { |
| 1855 | SkDebugf("?"); |
| 1856 | } else { |
| 1857 | SkDebugf("%d", foundSum); |
| 1858 | } |
| 1859 | SkDebugf(" foundMax="); |
| 1860 | if (foundMax == SK_MinS32) { |
| 1861 | SkDebugf("?"); |
| 1862 | } else { |
| 1863 | SkDebugf("%d", foundMax); |
| 1864 | } |
| 1865 | SkDebugf("\n"); |
| 1866 | #endif |
| 1867 | winding = foundSum; |
| 1868 | } |
| 1869 | #if DEBUG_WINDING |
| 1870 | SkDebugf("%s spanWinding=%d flipped=%d\n", __FUNCTION__, spanWinding, flipped); |
| 1871 | #endif |
| 1872 | return nextSegment; |
| 1873 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1874 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1875 | // so the span needs to contain the pairing info found here |
| 1876 | // this should include the winding computed for the edge, and |
| 1877 | // what edge it connects to, and whether it is discarded |
| 1878 | // (maybe discarded == abs(winding) > 1) ? |
| 1879 | // only need derivatives for duration of sorting, add a new struct |
| 1880 | // for pairings, remove extra spans that have zero length and |
| 1881 | // reference an unused other |
| 1882 | // for coincident, the last span on the other may be marked done |
| 1883 | // (always?) |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1884 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1885 | // if loop is exhausted, contour may be closed. |
| 1886 | // FIXME: pass in close point so we can check for closure |
| 1887 | |
| 1888 | // given a segment, and a sense of where 'inside' is, return the next |
| 1889 | // segment. If this segment has an intersection, or ends in multiple |
| 1890 | // segments, find the mate that continues the outside. |
| 1891 | // note that if there are multiples, but no coincidence, we can limit |
| 1892 | // choices to connections in the correct direction |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1893 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1894 | // mark found segments as done |
| 1895 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1896 | // start is the index of the beginning T of this edge |
| 1897 | // it is guaranteed to have an end which describes a non-zero length (?) |
| 1898 | // winding -1 means ccw, 1 means cw |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 1899 | Segment* findNextWinding(SkTDArray<Span*>& chase, bool active, |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 1900 | int& nextStart, int& nextEnd, int& winding, int& spanWinding, |
| 1901 | bool& unsortable) { |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 1902 | const int startIndex = nextStart; |
| 1903 | const int endIndex = nextEnd; |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1904 | int outerWinding = winding; |
| 1905 | int innerWinding = winding + spanWinding; |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 1906 | #if DEBUG_WINDING |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1907 | SkDebugf("%s winding=%d spanWinding=%d outerWinding=%d innerWinding=%d\n", |
| 1908 | __FUNCTION__, winding, spanWinding, outerWinding, innerWinding); |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 1909 | #endif |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 1910 | if (useInnerWinding(outerWinding, innerWinding)) { |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1911 | outerWinding = innerWinding; |
| 1912 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1913 | SkASSERT(startIndex != endIndex); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1914 | int count = fTs.count(); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1915 | SkASSERT(startIndex < endIndex ? startIndex < count - 1 |
| 1916 | : startIndex > 0); |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1917 | int step = SkSign32(endIndex - startIndex); |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 1918 | #if PRECISE_T_SORT |
| 1919 | int end = nextExactSpan(startIndex, step); |
| 1920 | #else |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1921 | int end = nextSpan(startIndex, step); |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 1922 | #endif |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1923 | SkASSERT(end >= 0); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1924 | Span* endSpan = &fTs[end]; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1925 | Segment* other; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1926 | if (isSimple(end)) { |
caryclark@google.com | af46cff | 2012-05-22 21:12:00 +0000 | [diff] [blame] | 1927 | // mark the smaller of startIndex, endIndex done, and all adjacent |
| 1928 | // spans with the same T value (but not 'other' spans) |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1929 | #if DEBUG_WINDING |
| 1930 | SkDebugf("%s simple\n", __FUNCTION__); |
| 1931 | #endif |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 1932 | markDone(SkMin32(startIndex, endIndex), outerWinding); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1933 | other = endSpan->fOther; |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1934 | nextStart = endSpan->fOtherIndex; |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 1935 | double startT = other->fTs[nextStart].fT; |
| 1936 | nextEnd = nextStart; |
| 1937 | do { |
| 1938 | nextEnd += step; |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 1939 | } |
| 1940 | #if PRECISE_T_SORT |
| 1941 | while (precisely_zero(startT - other->fTs[nextEnd].fT)); |
| 1942 | #else |
| 1943 | while (approximately_zero(startT - other->fTs[nextEnd].fT)); |
| 1944 | #endif |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 1945 | SkASSERT(step < 0 ? nextEnd >= 0 : nextEnd < other->fTs.count()); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1946 | return other; |
| 1947 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1948 | // more than one viable candidate -- measure angles to find best |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 1949 | SkTDArray<Angle> angles; |
caryclark@google.com | af46cff | 2012-05-22 21:12:00 +0000 | [diff] [blame] | 1950 | SkASSERT(startIndex - endIndex != 0); |
| 1951 | SkASSERT((startIndex - endIndex < 0) ^ (step < 0)); |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 1952 | addTwoAngles(startIndex, end, angles); |
| 1953 | buildAngles(end, angles); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1954 | SkTDArray<Angle*> sorted; |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 1955 | bool sortable = SortAngles(angles, sorted); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1956 | int angleCount = angles.count(); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 1957 | int firstIndex = findStartingEdge(sorted, startIndex, end); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 1958 | SkASSERT(firstIndex >= 0); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1959 | #if DEBUG_SORT |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 1960 | debugShowSort(__FUNCTION__, sorted, firstIndex, winding); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1961 | #endif |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 1962 | if (!sortable) { |
| 1963 | unsortable = true; |
| 1964 | return NULL; |
| 1965 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 1966 | SkASSERT(sorted[firstIndex]->segment() == this); |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 1967 | #if DEBUG_WINDING |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 1968 | SkDebugf("%s [%d] sign=%d\n", __FUNCTION__, firstIndex, sorted[firstIndex]->sign()); |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 1969 | #endif |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 1970 | int sumWinding = winding - spanSign(sorted[firstIndex]); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1971 | int nextIndex = firstIndex + 1; |
| 1972 | int lastIndex = firstIndex != 0 ? firstIndex : angleCount; |
| 1973 | const Angle* foundAngle = NULL; |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 1974 | // FIXME: found done logic probably fails if there are more than 4 |
| 1975 | // sorted angles. It should bias towards the first and last undone |
| 1976 | // edges -- but not sure that it won't choose a middle (incorrect) |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1977 | // edge if one is undone |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 1978 | bool foundDone = false; |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 1979 | bool foundDone2 = false; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1980 | // iterate through the angle, and compute everyone's winding |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 1981 | bool altFlipped = false; |
| 1982 | bool foundFlipped = false; |
| 1983 | int foundMax = SK_MinS32; |
| 1984 | int foundSum = SK_MinS32; |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 1985 | Segment* nextSegment; |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 1986 | int lastNonZeroSum = winding; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1987 | do { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1988 | if (nextIndex == angleCount) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1989 | nextIndex = 0; |
| 1990 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 1991 | const Angle* nextAngle = sorted[nextIndex]; |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 1992 | int maxWinding = sumWinding; |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 1993 | if (sumWinding) { |
| 1994 | lastNonZeroSum = sumWinding; |
| 1995 | } |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 1996 | nextSegment = nextAngle->segment(); |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 1997 | sumWinding -= nextSegment->spanSign(nextAngle); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 1998 | altFlipped ^= lastNonZeroSum * sumWinding < 0; // flip if different signs |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 1999 | #if 0 && DEBUG_WINDING |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 2000 | SkASSERT(abs(sumWinding) <= gDebugMaxWindSum); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2001 | SkDebugf("%s [%d] maxWinding=%d sumWinding=%d sign=%d altFlipped=%d\n", __FUNCTION__, |
| 2002 | nextIndex, maxWinding, sumWinding, nextAngle->sign(), altFlipped); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 2003 | #endif |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2004 | if (!sumWinding) { |
caryclark@google.com | 5c286d3 | 2012-07-13 11:57:28 +0000 | [diff] [blame] | 2005 | if (!active) { |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 2006 | markDone(SkMin32(startIndex, endIndex), outerWinding); |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 2007 | // FIXME: seems like a bug that this isn't calling userInnerWinding |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 2008 | nextSegment->markWinding(SkMin32(nextAngle->start(), |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 2009 | nextAngle->end()), maxWinding); |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 2010 | #if DEBUG_WINDING |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2011 | SkDebugf("%s [%d] inactive\n", __FUNCTION__, nextIndex); |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 2012 | #endif |
caryclark@google.com | 5c286d3 | 2012-07-13 11:57:28 +0000 | [diff] [blame] | 2013 | return NULL; |
| 2014 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 2015 | if (!foundAngle || foundDone) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2016 | foundAngle = nextAngle; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 2017 | foundDone = nextSegment->done(*nextAngle); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2018 | foundFlipped = altFlipped; |
| 2019 | foundMax = maxWinding; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2020 | } |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 2021 | continue; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2022 | } |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2023 | if (!maxWinding && (!foundAngle || foundDone2)) { |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 2024 | #if DEBUG_WINDING |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2025 | if (foundAngle && foundDone2) { |
| 2026 | SkDebugf("%s [%d] !foundAngle && foundDone2\n", __FUNCTION__, nextIndex); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2027 | } |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 2028 | #endif |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 2029 | foundAngle = nextAngle; |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2030 | foundDone2 = nextSegment->done(*nextAngle); |
| 2031 | foundFlipped = altFlipped; |
| 2032 | foundSum = sumWinding; |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 2033 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2034 | if (nextSegment->done()) { |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 2035 | continue; |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2036 | } |
| 2037 | // if the winding is non-zero, nextAngle does not connect to |
| 2038 | // current chain. If we haven't done so already, mark the angle |
| 2039 | // as done, record the winding value, and mark connected unambiguous |
| 2040 | // segments as well. |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 2041 | if (nextSegment->windSum(nextAngle) == SK_MinS32) { |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 2042 | if (useInnerWinding(maxWinding, sumWinding)) { |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 2043 | maxWinding = sumWinding; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2044 | } |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 2045 | Span* last; |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2046 | if (foundAngle) { |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 2047 | last = nextSegment->markAndChaseWinding(nextAngle, maxWinding); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2048 | } else { |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 2049 | last = nextSegment->markAndChaseDone(nextAngle, maxWinding); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 2050 | } |
| 2051 | if (last) { |
| 2052 | *chase.append() = last; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2053 | } |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2054 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2055 | } while (++nextIndex != lastIndex); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 2056 | markDone(SkMin32(startIndex, endIndex), outerWinding); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2057 | if (!foundAngle) { |
| 2058 | return NULL; |
| 2059 | } |
| 2060 | nextStart = foundAngle->start(); |
| 2061 | nextEnd = foundAngle->end(); |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 2062 | nextSegment = foundAngle->segment(); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2063 | int flipped = foundFlipped ? -1 : 1; |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 2064 | spanWinding = SkSign32(spanWinding) * flipped * nextSegment->windValue( |
| 2065 | SkMin32(nextStart, nextEnd)); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2066 | if (winding) { |
| 2067 | #if DEBUG_WINDING |
| 2068 | SkDebugf("%s ---6 winding=%d foundSum=", __FUNCTION__, winding); |
| 2069 | if (foundSum == SK_MinS32) { |
| 2070 | SkDebugf("?"); |
| 2071 | } else { |
| 2072 | SkDebugf("%d", foundSum); |
| 2073 | } |
| 2074 | SkDebugf(" foundMax="); |
| 2075 | if (foundMax == SK_MinS32) { |
| 2076 | SkDebugf("?"); |
| 2077 | } else { |
| 2078 | SkDebugf("%d", foundMax); |
| 2079 | } |
| 2080 | SkDebugf("\n"); |
| 2081 | #endif |
| 2082 | winding = foundSum; |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 2083 | } |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 2084 | #if DEBUG_WINDING |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2085 | SkDebugf("%s spanWinding=%d flipped=%d\n", __FUNCTION__, spanWinding, flipped); |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 2086 | #endif |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 2087 | return nextSegment; |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 2088 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2089 | |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 2090 | Segment* findNextXor(int& nextStart, int& nextEnd, bool& unsortable) { |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2091 | const int startIndex = nextStart; |
| 2092 | const int endIndex = nextEnd; |
| 2093 | SkASSERT(startIndex != endIndex); |
| 2094 | int count = fTs.count(); |
| 2095 | SkASSERT(startIndex < endIndex ? startIndex < count - 1 |
| 2096 | : startIndex > 0); |
| 2097 | int step = SkSign32(endIndex - startIndex); |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 2098 | #if PRECISE_T_SORT |
| 2099 | int end = nextExactSpan(startIndex, step); |
| 2100 | #else |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2101 | int end = nextSpan(startIndex, step); |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 2102 | #endif |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2103 | SkASSERT(end >= 0); |
| 2104 | Span* endSpan = &fTs[end]; |
| 2105 | Segment* other; |
| 2106 | markDone(SkMin32(startIndex, endIndex), 1); |
| 2107 | if (isSimple(end)) { |
| 2108 | #if DEBUG_WINDING |
| 2109 | SkDebugf("%s simple\n", __FUNCTION__); |
| 2110 | #endif |
| 2111 | other = endSpan->fOther; |
| 2112 | nextStart = endSpan->fOtherIndex; |
| 2113 | double startT = other->fTs[nextStart].fT; |
| 2114 | SkDEBUGCODE(bool firstLoop = true;) |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 2115 | if ((approximately_less_than_zero(startT) && step < 0) |
| 2116 | || (approximately_greater_than_one(startT) && step > 0)) { |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2117 | step = -step; |
| 2118 | SkDEBUGCODE(firstLoop = false;) |
| 2119 | } |
| 2120 | do { |
| 2121 | nextEnd = nextStart; |
| 2122 | do { |
| 2123 | nextEnd += step; |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 2124 | } |
| 2125 | #if PRECISE_T_SORT |
| 2126 | while (precisely_zero(startT - other->fTs[nextEnd].fT)); |
| 2127 | #else |
| 2128 | while (approximately_zero(startT - other->fTs[nextEnd].fT)); |
| 2129 | #endif |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2130 | if (other->fTs[SkMin32(nextStart, nextEnd)].fWindValue) { |
| 2131 | break; |
| 2132 | } |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 2133 | #ifdef SK_DEBUG |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2134 | SkASSERT(firstLoop); |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 2135 | #endif |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2136 | SkDEBUGCODE(firstLoop = false;) |
| 2137 | step = -step; |
| 2138 | } while (true); |
| 2139 | SkASSERT(step < 0 ? nextEnd >= 0 : nextEnd < other->fTs.count()); |
| 2140 | return other; |
| 2141 | } |
| 2142 | SkTDArray<Angle> angles; |
| 2143 | SkASSERT(startIndex - endIndex != 0); |
| 2144 | SkASSERT((startIndex - endIndex < 0) ^ (step < 0)); |
| 2145 | addTwoAngles(startIndex, end, angles); |
| 2146 | buildAngles(end, angles); |
| 2147 | SkTDArray<Angle*> sorted; |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 2148 | bool sortable = SortAngles(angles, sorted); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2149 | int angleCount = angles.count(); |
| 2150 | int firstIndex = findStartingEdge(sorted, startIndex, end); |
| 2151 | SkASSERT(firstIndex >= 0); |
| 2152 | #if DEBUG_SORT |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 2153 | debugShowSort(__FUNCTION__, sorted, firstIndex, 0); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2154 | #endif |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 2155 | if (!sortable) { |
| 2156 | unsortable = true; |
| 2157 | return NULL; |
| 2158 | } |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2159 | SkASSERT(sorted[firstIndex]->segment() == this); |
| 2160 | int nextIndex = firstIndex + 1; |
| 2161 | int lastIndex = firstIndex != 0 ? firstIndex : angleCount; |
| 2162 | const Angle* nextAngle; |
| 2163 | Segment* nextSegment; |
| 2164 | do { |
| 2165 | if (nextIndex == angleCount) { |
| 2166 | nextIndex = 0; |
| 2167 | } |
| 2168 | nextAngle = sorted[nextIndex]; |
| 2169 | nextSegment = nextAngle->segment(); |
| 2170 | if (!nextSegment->done(*nextAngle)) { |
| 2171 | break; |
| 2172 | } |
| 2173 | if (++nextIndex == lastIndex) { |
| 2174 | return NULL; |
| 2175 | } |
| 2176 | } while (true); |
| 2177 | nextStart = nextAngle->start(); |
| 2178 | nextEnd = nextAngle->end(); |
| 2179 | return nextSegment; |
| 2180 | } |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 2181 | |
| 2182 | int findStartingEdge(SkTDArray<Angle*>& sorted, int start, int end) { |
| 2183 | int angleCount = sorted.count(); |
| 2184 | int firstIndex = -1; |
| 2185 | for (int angleIndex = 0; angleIndex < angleCount; ++angleIndex) { |
| 2186 | const Angle* angle = sorted[angleIndex]; |
| 2187 | if (angle->segment() == this && angle->start() == end && |
| 2188 | angle->end() == start) { |
| 2189 | firstIndex = angleIndex; |
| 2190 | break; |
| 2191 | } |
| 2192 | } |
| 2193 | return firstIndex; |
| 2194 | } |
| 2195 | |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 2196 | // FIXME: this is tricky code; needs its own unit test |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 2197 | void findTooCloseToCall(bool isXor) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2198 | int count = fTs.count(); |
| 2199 | if (count < 3) { // require t=0, x, 1 at minimum |
| 2200 | return; |
| 2201 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2202 | int matchIndex = 0; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2203 | int moCount; |
| 2204 | Span* match; |
| 2205 | Segment* mOther; |
| 2206 | do { |
| 2207 | match = &fTs[matchIndex]; |
| 2208 | mOther = match->fOther; |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 2209 | // FIXME: allow quads, cubics to be near coincident? |
| 2210 | if (mOther->fVerb == SkPath::kLine_Verb) { |
| 2211 | moCount = mOther->fTs.count(); |
| 2212 | if (moCount >= 3) { |
| 2213 | break; |
| 2214 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 2215 | } |
| 2216 | if (++matchIndex >= count) { |
| 2217 | return; |
| 2218 | } |
| 2219 | } while (true); // require t=0, x, 1 at minimum |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2220 | // OPTIMIZATION: defer matchPt until qualifying toCount is found? |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2221 | const SkPoint* matchPt = &xyAtT(match); |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2222 | // look for a pair of nearby T values that map to the same (x,y) value |
| 2223 | // if found, see if the pair of other segments share a common point. If |
| 2224 | // so, the span from here to there is coincident. |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2225 | for (int index = matchIndex + 1; index < count; ++index) { |
| 2226 | Span* test = &fTs[index]; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2227 | if (test->fDone) { |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2228 | continue; |
| 2229 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2230 | Segment* tOther = test->fOther; |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 2231 | if (tOther->fVerb != SkPath::kLine_Verb) { |
| 2232 | continue; // FIXME: allow quads, cubics to be near coincident? |
| 2233 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2234 | int toCount = tOther->fTs.count(); |
| 2235 | if (toCount < 3) { // require t=0, x, 1 at minimum |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2236 | continue; |
| 2237 | } |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2238 | const SkPoint* testPt = &xyAtT(test); |
| 2239 | if (*matchPt != *testPt) { |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2240 | matchIndex = index; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2241 | moCount = toCount; |
| 2242 | match = test; |
| 2243 | mOther = tOther; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2244 | matchPt = testPt; |
| 2245 | continue; |
| 2246 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 2247 | int moStart = -1; |
| 2248 | int moEnd = -1; |
| 2249 | double moStartT, moEndT; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2250 | for (int moIndex = 0; moIndex < moCount; ++moIndex) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2251 | Span& moSpan = mOther->fTs[moIndex]; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2252 | if (moSpan.fDone) { |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2253 | continue; |
| 2254 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2255 | if (moSpan.fOther == this) { |
| 2256 | if (moSpan.fOtherT == match->fT) { |
| 2257 | moStart = moIndex; |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 2258 | moStartT = moSpan.fT; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2259 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2260 | continue; |
| 2261 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 2262 | if (moSpan.fOther == tOther) { |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 2263 | if (tOther->fTs[moSpan.fOtherIndex].fWindValue == 0) { |
| 2264 | moStart = -1; |
| 2265 | break; |
| 2266 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 2267 | SkASSERT(moEnd == -1); |
| 2268 | moEnd = moIndex; |
| 2269 | moEndT = moSpan.fT; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2270 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2271 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 2272 | if (moStart < 0 || moEnd < 0) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2273 | continue; |
| 2274 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 2275 | // FIXME: if moStartT, moEndT are initialized to NaN, can skip this test |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 2276 | if (approximately_equal(moStartT, moEndT)) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2277 | continue; |
| 2278 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 2279 | int toStart = -1; |
| 2280 | int toEnd = -1; |
| 2281 | double toStartT, toEndT; |
| 2282 | for (int toIndex = 0; toIndex < toCount; ++toIndex) { |
| 2283 | Span& toSpan = tOther->fTs[toIndex]; |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 2284 | if (toSpan.fDone) { |
| 2285 | continue; |
| 2286 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 2287 | if (toSpan.fOther == this) { |
| 2288 | if (toSpan.fOtherT == test->fT) { |
| 2289 | toStart = toIndex; |
| 2290 | toStartT = toSpan.fT; |
| 2291 | } |
| 2292 | continue; |
| 2293 | } |
| 2294 | if (toSpan.fOther == mOther && toSpan.fOtherT == moEndT) { |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 2295 | if (mOther->fTs[toSpan.fOtherIndex].fWindValue == 0) { |
| 2296 | moStart = -1; |
| 2297 | break; |
| 2298 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 2299 | SkASSERT(toEnd == -1); |
| 2300 | toEnd = toIndex; |
| 2301 | toEndT = toSpan.fT; |
| 2302 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2303 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 2304 | // FIXME: if toStartT, toEndT are initialized to NaN, can skip this test |
| 2305 | if (toStart <= 0 || toEnd <= 0) { |
| 2306 | continue; |
| 2307 | } |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 2308 | if (approximately_equal(toStartT, toEndT)) { |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 2309 | continue; |
| 2310 | } |
| 2311 | // test to see if the segment between there and here is linear |
| 2312 | if (!mOther->isLinear(moStart, moEnd) |
| 2313 | || !tOther->isLinear(toStart, toEnd)) { |
| 2314 | continue; |
| 2315 | } |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 2316 | bool flipped = (moStart - moEnd) * (toStart - toEnd) < 1; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2317 | if (flipped) { |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 2318 | mOther->addTCancel(moStartT, moEndT, *tOther, toEndT, toStartT); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2319 | } else { |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 2320 | // FIXME: this is bogus for multiple ops |
| 2321 | // the xorMask needs to be accumulated from the union of the two |
| 2322 | // edges -- which means that the segment must have its own copy of the mask |
| 2323 | mOther->addTCoincident(isXor, moStartT, moEndT, *tOther, toStartT, toEndT); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2324 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2325 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 2328 | // start here; |
skia.committer@gmail.com | 20c301b | 2012-10-17 02:01:13 +0000 | [diff] [blame^] | 2329 | // either: |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 2330 | // a) mark spans with either end unsortable as done, or |
| 2331 | // b) rewrite findTop / findTopSegment / findTopContour to iterate further |
| 2332 | // when encountering an unsortable span |
| 2333 | |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2334 | // OPTIMIZATION : for a pair of lines, can we compute points at T (cached) |
| 2335 | // and use more concise logic like the old edge walker code? |
| 2336 | // FIXME: this needs to deal with coincident edges |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 2337 | Segment* findTop(int& tIndex, int& endIndex) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2338 | // iterate through T intersections and return topmost |
| 2339 | // 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] | 2340 | SkASSERT(!done()); |
| 2341 | int firstT; |
| 2342 | int lastT; |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 2343 | SkPoint topPt; |
| 2344 | topPt.fY = SK_ScalarMax; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2345 | int count = fTs.count(); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2346 | // 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] | 2347 | bool lastDone = true; |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 2348 | bool lastUnsortableEnd; |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 2349 | for (int index = 0; index < count; ++index) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2350 | const Span& span = fTs[index]; |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 2351 | if ((!span.fDone && !span.fUnsortableStart) || (!lastDone && !lastUnsortableEnd)) { |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 2352 | const SkPoint& intercept = xyAtT(&span); |
| 2353 | if (topPt.fY > intercept.fY || (topPt.fY == intercept.fY |
| 2354 | && topPt.fX > intercept.fX)) { |
| 2355 | topPt = intercept; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2356 | firstT = lastT = index; |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 2357 | } else if (topPt == intercept) { |
| 2358 | lastT = index; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2359 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2360 | } |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 2361 | lastDone = span.fDone; |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 2362 | lastUnsortableEnd = span.fUnsortableEnd; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2363 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2364 | // sort the edges to find the leftmost |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2365 | int step = 1; |
| 2366 | int end = nextSpan(firstT, step); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2367 | if (end == -1) { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2368 | step = -1; |
| 2369 | end = nextSpan(firstT, step); |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 2370 | SkASSERT(end != -1); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2371 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2372 | // if the topmost T is not on end, or is three-way or more, find left |
| 2373 | // look for left-ness from tLeft to firstT (matching y of other) |
| 2374 | SkTDArray<Angle> angles; |
| 2375 | SkASSERT(firstT - end != 0); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2376 | addTwoAngles(end, firstT, angles); |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2377 | buildAngles(firstT, angles); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2378 | SkTDArray<Angle*> sorted; |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 2379 | (void) SortAngles(angles, sorted); |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 2380 | #if DEBUG_SORT |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2381 | sorted[0]->segment()->debugShowSort(__FUNCTION__, sorted, 0, 0); |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 2382 | #endif |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 2383 | // skip edges that have already been processed |
| 2384 | firstT = -1; |
| 2385 | Segment* leftSegment; |
| 2386 | do { |
| 2387 | const Angle* angle = sorted[++firstT]; |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 2388 | if (angle->unsortable()) { |
| 2389 | // FIXME: if all angles are unsortable, find next topmost |
| 2390 | SkASSERT(firstT < angles.count() - 1); |
| 2391 | continue; |
| 2392 | } |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 2393 | leftSegment = angle->segment(); |
| 2394 | tIndex = angle->end(); |
| 2395 | endIndex = angle->start(); |
| 2396 | } while (leftSegment->fTs[SkMin32(tIndex, endIndex)].fDone); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2397 | return leftSegment; |
| 2398 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2399 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2400 | // FIXME: not crazy about this |
| 2401 | // when the intersections are performed, the other index is into an |
| 2402 | // incomplete array. as the array grows, the indices become incorrect |
| 2403 | // while the following fixes the indices up again, it isn't smart about |
| 2404 | // skipping segments whose indices are already correct |
| 2405 | // assuming we leave the code that wrote the index in the first place |
| 2406 | void fixOtherTIndex() { |
| 2407 | int iCount = fTs.count(); |
| 2408 | for (int i = 0; i < iCount; ++i) { |
| 2409 | Span& iSpan = fTs[i]; |
| 2410 | double oT = iSpan.fOtherT; |
| 2411 | Segment* other = iSpan.fOther; |
| 2412 | int oCount = other->fTs.count(); |
| 2413 | for (int o = 0; o < oCount; ++o) { |
| 2414 | Span& oSpan = other->fTs[o]; |
| 2415 | if (oT == oSpan.fT && this == oSpan.fOther) { |
| 2416 | iSpan.fOtherIndex = o; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2417 | break; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2418 | } |
| 2419 | } |
| 2420 | } |
| 2421 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2422 | |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2423 | // OPTIMIZATION: uses tail recursion. Unwise? |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 2424 | Span* innerChaseDone(int index, int step, int winding) { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2425 | int end = nextSpan(index, step); |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 2426 | SkASSERT(end >= 0); |
| 2427 | if (multipleSpans(end)) { |
| 2428 | return &fTs[end]; |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2429 | } |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 2430 | const Span& endSpan = fTs[end]; |
| 2431 | Segment* other = endSpan.fOther; |
| 2432 | index = endSpan.fOtherIndex; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2433 | int otherEnd = other->nextSpan(index, step); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 2434 | Span* last = other->innerChaseDone(index, step, winding); |
| 2435 | other->markDone(SkMin32(index, otherEnd), winding); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 2436 | return last; |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2437 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2438 | |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 2439 | Span* innerChaseWinding(int index, int step, int winding) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2440 | int end = nextSpan(index, step); |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 2441 | SkASSERT(end >= 0); |
| 2442 | if (multipleSpans(end)) { |
| 2443 | return &fTs[end]; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2444 | } |
| 2445 | const Span& endSpan = fTs[end]; |
| 2446 | Segment* other = endSpan.fOther; |
| 2447 | index = endSpan.fOtherIndex; |
| 2448 | int otherEnd = other->nextSpan(index, step); |
| 2449 | int min = SkMin32(index, otherEnd); |
| 2450 | if (other->fTs[min].fWindSum != SK_MinS32) { |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 2451 | SkASSERT(other->fTs[min].fWindSum == winding); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 2452 | return NULL; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2453 | } |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 2454 | Span* last = other->innerChaseWinding(index, step, winding); |
| 2455 | other->markWinding(min, winding); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 2456 | return last; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2457 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2458 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 2459 | void init(const SkPoint pts[], SkPath::Verb verb, bool operand) { |
| 2460 | fDoneSpans = 0; |
| 2461 | fOperand = operand; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2462 | fPts = pts; |
| 2463 | fVerb = verb; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2466 | bool intersected() const { |
| 2467 | return fTs.count() > 0; |
| 2468 | } |
caryclark@google.com | 7db7c6b | 2012-07-27 21:22:25 +0000 | [diff] [blame] | 2469 | |
| 2470 | bool isConnected(int startIndex, int endIndex) const { |
| 2471 | return fTs[startIndex].fWindSum != SK_MinS32 |
| 2472 | || fTs[endIndex].fWindSum != SK_MinS32; |
| 2473 | } |
| 2474 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 2475 | bool isHorizontal() const { |
| 2476 | return fBounds.fTop == fBounds.fBottom; |
| 2477 | } |
| 2478 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2479 | bool isLinear(int start, int end) const { |
| 2480 | if (fVerb == SkPath::kLine_Verb) { |
| 2481 | return true; |
| 2482 | } |
| 2483 | if (fVerb == SkPath::kQuad_Verb) { |
| 2484 | SkPoint qPart[3]; |
| 2485 | QuadSubDivide(fPts, fTs[start].fT, fTs[end].fT, qPart); |
| 2486 | return QuadIsLinear(qPart); |
| 2487 | } else { |
| 2488 | SkASSERT(fVerb == SkPath::kCubic_Verb); |
| 2489 | SkPoint cPart[4]; |
| 2490 | CubicSubDivide(fPts, fTs[start].fT, fTs[end].fT, cPart); |
| 2491 | return CubicIsLinear(cPart); |
| 2492 | } |
| 2493 | } |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 2494 | |
| 2495 | // OPTIMIZE: successive calls could start were the last leaves off |
| 2496 | // or calls could specialize to walk forwards or backwards |
| 2497 | bool isMissing(double startT) const { |
| 2498 | size_t tCount = fTs.count(); |
| 2499 | for (size_t index = 0; index < tCount; ++index) { |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 2500 | if (approximately_zero(startT - fTs[index].fT)) { |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 2501 | return false; |
| 2502 | } |
| 2503 | } |
| 2504 | return true; |
| 2505 | } |
| 2506 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2507 | bool isSimple(int end) const { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2508 | int count = fTs.count(); |
| 2509 | if (count == 2) { |
| 2510 | return true; |
| 2511 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2512 | double t = fTs[end].fT; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 2513 | if (approximately_less_than_zero(t)) { |
| 2514 | return !approximately_less_than_zero(fTs[1].fT); |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2515 | } |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 2516 | if (approximately_greater_than_one(t)) { |
| 2517 | return !approximately_greater_than_one(fTs[count - 2].fT); |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2518 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2519 | return false; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2520 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2521 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2522 | bool isVertical() const { |
| 2523 | return fBounds.fLeft == fBounds.fRight; |
| 2524 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2525 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2526 | SkScalar leftMost(int start, int end) const { |
| 2527 | return (*SegmentLeftMost[fVerb])(fPts, fTs[start].fT, fTs[end].fT); |
| 2528 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2529 | |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2530 | // this span is excluded by the winding rule -- chase the ends |
| 2531 | // as long as they are unambiguous to mark connections as done |
| 2532 | // and give them the same winding value |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 2533 | Span* markAndChaseDone(const Angle* angle, int winding) { |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2534 | int index = angle->start(); |
| 2535 | int endIndex = angle->end(); |
| 2536 | int step = SkSign32(endIndex - index); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 2537 | Span* last = innerChaseDone(index, step, winding); |
| 2538 | markDone(SkMin32(index, endIndex), winding); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 2539 | return last; |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2540 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2541 | |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 2542 | Span* markAndChaseWinding(const Angle* angle, int winding) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2543 | int index = angle->start(); |
| 2544 | int endIndex = angle->end(); |
| 2545 | int min = SkMin32(index, endIndex); |
| 2546 | int step = SkSign32(endIndex - index); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 2547 | Span* last = innerChaseWinding(index, step, winding); |
| 2548 | markWinding(min, winding); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 2549 | return last; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2550 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2551 | |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2552 | // FIXME: this should also mark spans with equal (x,y) |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2553 | // This may be called when the segment is already marked done. While this |
| 2554 | // wastes time, it shouldn't do any more than spin through the T spans. |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2555 | // OPTIMIZATION: abort on first done found (assuming that this code is |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2556 | // always called to mark segments done). |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 2557 | void markDone(int index, int winding) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2558 | // SkASSERT(!done()); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2559 | SkASSERT(winding); |
caryclark@google.com | af46cff | 2012-05-22 21:12:00 +0000 | [diff] [blame] | 2560 | double referenceT = fTs[index].fT; |
| 2561 | int lesser = index; |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 2562 | #if PRECISE_T_SORT |
| 2563 | while (--lesser >= 0 && precisely_negative(referenceT - fTs[lesser].fT)) { |
| 2564 | markOneDone(__FUNCTION__, lesser, winding); |
| 2565 | } |
| 2566 | do { |
| 2567 | markOneDone(__FUNCTION__, index, winding); |
| 2568 | } while (++index < fTs.count() && precisely_negative(fTs[index].fT - referenceT)); |
| 2569 | #else |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 2570 | while (--lesser >= 0 && approximately_negative(referenceT - fTs[lesser].fT)) { |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2571 | markOneDone(__FUNCTION__, lesser, winding); |
caryclark@google.com | af46cff | 2012-05-22 21:12:00 +0000 | [diff] [blame] | 2572 | } |
| 2573 | do { |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2574 | markOneDone(__FUNCTION__, index, winding); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 2575 | } while (++index < fTs.count() && approximately_negative(fTs[index].fT - referenceT)); |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 2576 | #endif |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2577 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2578 | |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2579 | void markOneDone(const char* funName, int tIndex, int winding) { |
| 2580 | Span* span = markOneWinding(funName, tIndex, winding); |
| 2581 | if (!span) { |
| 2582 | return; |
| 2583 | } |
| 2584 | span->fDone = true; |
| 2585 | fDoneSpans++; |
| 2586 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2587 | |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2588 | Span* markOneWinding(const char* funName, int tIndex, int winding) { |
| 2589 | Span& span = fTs[tIndex]; |
| 2590 | if (span.fDone) { |
| 2591 | return NULL; |
| 2592 | } |
| 2593 | #if DEBUG_MARK_DONE |
| 2594 | debugShowNewWinding(funName, span, winding); |
| 2595 | #endif |
| 2596 | SkASSERT(span.fWindSum == SK_MinS32 || span.fWindSum == winding); |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 2597 | #ifdef SK_DEBUG |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2598 | SkASSERT(abs(winding) <= gDebugMaxWindSum); |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 2599 | #endif |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2600 | span.fWindSum = winding; |
| 2601 | return &span; |
| 2602 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2603 | |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 2604 | void markWinding(int index, int winding) { |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 2605 | // SkASSERT(!done()); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2606 | SkASSERT(winding); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2607 | double referenceT = fTs[index].fT; |
| 2608 | int lesser = index; |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 2609 | #if PRECISE_T_SORT |
| 2610 | while (--lesser >= 0 && precisely_negative(referenceT - fTs[lesser].fT)) { |
| 2611 | markOneWinding(__FUNCTION__, lesser, winding); |
| 2612 | } |
| 2613 | do { |
| 2614 | markOneWinding(__FUNCTION__, index, winding); |
| 2615 | } while (++index < fTs.count() && precisely_negative(fTs[index].fT - referenceT)); |
| 2616 | #else |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 2617 | while (--lesser >= 0 && approximately_negative(referenceT - fTs[lesser].fT)) { |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2618 | markOneWinding(__FUNCTION__, lesser, winding); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2619 | } |
| 2620 | do { |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2621 | markOneWinding(__FUNCTION__, index, winding); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 2622 | } while (++index < fTs.count() && approximately_negative(fTs[index].fT - referenceT)); |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 2623 | #endif |
caryclark@google.com | af46cff | 2012-05-22 21:12:00 +0000 | [diff] [blame] | 2624 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2625 | |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 2626 | void matchWindingValue(int tIndex, double t, bool borrowWind) { |
caryclark@google.com | 0c803d0 | 2012-08-06 11:15:47 +0000 | [diff] [blame] | 2627 | int nextDoorWind = SK_MaxS32; |
| 2628 | if (tIndex > 0) { |
| 2629 | const Span& below = fTs[tIndex - 1]; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 2630 | if (approximately_negative(t - below.fT)) { |
caryclark@google.com | 0c803d0 | 2012-08-06 11:15:47 +0000 | [diff] [blame] | 2631 | nextDoorWind = below.fWindValue; |
| 2632 | } |
| 2633 | } |
| 2634 | if (nextDoorWind == SK_MaxS32 && tIndex + 1 < fTs.count()) { |
| 2635 | const Span& above = fTs[tIndex + 1]; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 2636 | if (approximately_negative(above.fT - t)) { |
caryclark@google.com | 0c803d0 | 2012-08-06 11:15:47 +0000 | [diff] [blame] | 2637 | nextDoorWind = above.fWindValue; |
| 2638 | } |
| 2639 | } |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 2640 | if (nextDoorWind == SK_MaxS32 && borrowWind && tIndex > 0 && t < 1) { |
| 2641 | const Span& below = fTs[tIndex - 1]; |
| 2642 | nextDoorWind = below.fWindValue; |
| 2643 | } |
caryclark@google.com | 0c803d0 | 2012-08-06 11:15:47 +0000 | [diff] [blame] | 2644 | if (nextDoorWind != SK_MaxS32) { |
| 2645 | Span& newSpan = fTs[tIndex]; |
| 2646 | newSpan.fWindValue = nextDoorWind; |
| 2647 | if (!nextDoorWind) { |
| 2648 | newSpan.fDone = true; |
| 2649 | ++fDoneSpans; |
| 2650 | } |
| 2651 | } |
| 2652 | } |
| 2653 | |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 2654 | // return span if when chasing, two or more radiating spans are not done |
| 2655 | // OPTIMIZATION: ? multiple spans is detected when there is only one valid |
| 2656 | // candidate and the remaining spans have windValue == 0 (canceled by |
| 2657 | // coincidence). The coincident edges could either be removed altogether, |
| 2658 | // or this code could be more complicated in detecting this case. Worth it? |
| 2659 | bool multipleSpans(int end) const { |
| 2660 | return end > 0 && end < fTs.count() - 1; |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2663 | // This has callers for two different situations: one establishes the end |
| 2664 | // of the current span, and one establishes the beginning of the next span |
| 2665 | // (thus the name). When this is looking for the end of the current span, |
| 2666 | // coincidence is found when the beginning Ts contain -step and the end |
| 2667 | // contains step. When it is looking for the beginning of the next, the |
| 2668 | // 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] | 2669 | // OPTIMIZATION: probably should split into two functions |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2670 | int nextSpan(int from, int step) const { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2671 | const Span& fromSpan = fTs[from]; |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2672 | int count = fTs.count(); |
| 2673 | int to = from; |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2674 | while (step > 0 ? ++to < count : --to >= 0) { |
| 2675 | const Span& span = fTs[to]; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 2676 | if (approximately_zero(span.fT - fromSpan.fT)) { |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2677 | continue; |
| 2678 | } |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2679 | return to; |
| 2680 | } |
| 2681 | return -1; |
| 2682 | } |
skia.committer@gmail.com | 439cb51 | 2012-10-10 02:01:30 +0000 | [diff] [blame] | 2683 | |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 2684 | #if PRECISE_T_SORT |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 2685 | // FIXME |
| 2686 | // this returns at any difference in T, vs. a preset minimum. It may be |
| 2687 | // that all callers to nextSpan should use this instead. |
| 2688 | int nextExactSpan(int from, int step) const { |
| 2689 | const Span& fromSpan = fTs[from]; |
| 2690 | int count = fTs.count(); |
| 2691 | int to = from; |
| 2692 | while (step > 0 ? ++to < count : --to >= 0) { |
| 2693 | const Span& span = fTs[to]; |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 2694 | if (precisely_zero(span.fT - fromSpan.fT)) { |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 2695 | continue; |
| 2696 | } |
| 2697 | return to; |
| 2698 | } |
| 2699 | return -1; |
| 2700 | } |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 2701 | #endif |
skia.committer@gmail.com | 055c7c2 | 2012-09-15 02:01:41 +0000 | [diff] [blame] | 2702 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 2703 | bool operand() const { |
| 2704 | return fOperand; |
| 2705 | } |
| 2706 | |
| 2707 | int oppSign(int startIndex, int endIndex) const { |
| 2708 | int result = startIndex < endIndex ? -fTs[startIndex].fWindValueOpp : |
| 2709 | fTs[endIndex].fWindValueOpp; |
| 2710 | #if DEBUG_WIND_BUMP |
| 2711 | SkDebugf("%s spanSign=%d\n", __FUNCTION__, result); |
| 2712 | #endif |
| 2713 | return result; |
| 2714 | } |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2715 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2716 | const SkPoint* pts() const { |
| 2717 | return fPts; |
| 2718 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2719 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2720 | void reset() { |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 2721 | init(NULL, (SkPath::Verb) -1, false); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2722 | fBounds.set(SK_ScalarMax, SK_ScalarMax, SK_ScalarMax, SK_ScalarMax); |
| 2723 | fTs.reset(); |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 2726 | static bool SortAngles(SkTDArray<Angle>& angles, SkTDArray<Angle*>& angleList) { |
| 2727 | int angleCount = angles.count(); |
| 2728 | int angleIndex; |
| 2729 | angleList.setReserve(angleCount); |
| 2730 | for (angleIndex = 0; angleIndex < angleCount; ++angleIndex) { |
| 2731 | *angleList.append() = &angles[angleIndex]; |
| 2732 | } |
| 2733 | QSort<Angle>(angleList.begin(), angleList.end() - 1); |
| 2734 | bool result = true; |
| 2735 | for (angleIndex = 0; angleIndex < angleCount; ++angleIndex) { |
| 2736 | Angle& angle = angles[angleIndex]; |
| 2737 | if (angle.unsortable()) { |
| 2738 | // so that it is available for early exclusion in findTop and others |
| 2739 | const SkTDArray<Span>* spans = angle.spans(); |
| 2740 | Span* span = const_cast<Span*>(&(*spans)[angle.start()]); |
| 2741 | if (angle.start() < angle.end()) { |
| 2742 | span->fUnsortableStart = true; |
| 2743 | } else { |
| 2744 | --span; |
| 2745 | span->fUnsortableEnd = true; |
| 2746 | } |
| 2747 | result = false; |
| 2748 | } |
| 2749 | } |
| 2750 | return result; |
| 2751 | } |
| 2752 | |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 2753 | // OPTIMIZATION: mark as debugging only if used solely by tests |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2754 | const Span& span(int tIndex) const { |
| 2755 | return fTs[tIndex]; |
| 2756 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2757 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 2758 | int spanSign(const Angle* angle) const { |
| 2759 | SkASSERT(angle->segment() == this); |
| 2760 | return spanSign(angle->start(), angle->end()); |
| 2761 | } |
| 2762 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2763 | int spanSign(int startIndex, int endIndex) const { |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 2764 | int result = startIndex < endIndex ? -fTs[startIndex].fWindValue : |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2765 | fTs[endIndex].fWindValue; |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 2766 | #if DEBUG_WIND_BUMP |
| 2767 | SkDebugf("%s spanSign=%d\n", __FUNCTION__, result); |
| 2768 | #endif |
| 2769 | return result; |
| 2770 | } |
| 2771 | |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2772 | // OPTIMIZATION: mark as debugging only if used solely by tests |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2773 | double t(int tIndex) const { |
| 2774 | return fTs[tIndex].fT; |
| 2775 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2776 | |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 2777 | static void TrackOutside(SkTDArray<double>& outsideTs, double end, |
| 2778 | double start) { |
| 2779 | int outCount = outsideTs.count(); |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 2780 | if (outCount == 0 || !approximately_negative(end - outsideTs[outCount - 2])) { |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 2781 | *outsideTs.append() = end; |
| 2782 | *outsideTs.append() = start; |
| 2783 | } |
| 2784 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2785 | |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2786 | void undoneSpan(int& start, int& end) { |
| 2787 | size_t tCount = fTs.count(); |
| 2788 | size_t index; |
| 2789 | for (index = 0; index < tCount; ++index) { |
| 2790 | if (!fTs[index].fDone) { |
| 2791 | break; |
| 2792 | } |
| 2793 | } |
| 2794 | SkASSERT(index < tCount - 1); |
| 2795 | start = index; |
| 2796 | double startT = fTs[index].fT; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 2797 | while (approximately_negative(fTs[++index].fT - startT)) |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2798 | SkASSERT(index < tCount); |
| 2799 | SkASSERT(index < tCount); |
| 2800 | end = index; |
| 2801 | } |
caryclark@google.com | 1806344 | 2012-07-25 12:05:18 +0000 | [diff] [blame] | 2802 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 2803 | void updatePts(const SkPoint pts[]) { |
| 2804 | fPts = pts; |
| 2805 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2806 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2807 | SkPath::Verb verb() const { |
| 2808 | return fVerb; |
| 2809 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2810 | |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 2811 | int windSum(int tIndex) const { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2812 | return fTs[tIndex].fWindSum; |
| 2813 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2814 | |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 2815 | int windSum(const Angle* angle) const { |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2816 | int start = angle->start(); |
| 2817 | int end = angle->end(); |
| 2818 | int index = SkMin32(start, end); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 2819 | return windSum(index); |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 2820 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2821 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2822 | int windValue(int tIndex) const { |
| 2823 | return fTs[tIndex].fWindValue; |
| 2824 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2825 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2826 | int windValue(const Angle* angle) const { |
| 2827 | int start = angle->start(); |
| 2828 | int end = angle->end(); |
| 2829 | int index = SkMin32(start, end); |
| 2830 | return windValue(index); |
| 2831 | } |
| 2832 | |
| 2833 | SkScalar xAtT(const Span* span) const { |
| 2834 | return xyAtT(span).fX; |
| 2835 | } |
skia.committer@gmail.com | 055c7c2 | 2012-09-15 02:01:41 +0000 | [diff] [blame] | 2836 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2837 | const SkPoint& xyAtT(int index) const { |
| 2838 | return xyAtT(&fTs[index]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2841 | const SkPoint& xyAtT(const Span* span) const { |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 2842 | if (SkScalarIsNaN(span->fPt.fX)) { |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2843 | if (span->fT == 0) { |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 2844 | span->fPt = fPts[0]; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2845 | } else if (span->fT == 1) { |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 2846 | span->fPt = fPts[fVerb]; |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2847 | } else { |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 2848 | (*SegmentXYAtT[fVerb])(fPts, span->fT, &span->fPt); |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 2849 | } |
| 2850 | } |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 2851 | return span->fPt; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2852 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2853 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2854 | SkScalar yAtT(int index) const { |
| 2855 | return yAtT(&fTs[index]); |
| 2856 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2857 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2858 | SkScalar yAtT(const Span* span) const { |
| 2859 | return xyAtT(span).fY; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2862 | #if DEBUG_DUMP |
| 2863 | void dump() const { |
| 2864 | const char className[] = "Segment"; |
| 2865 | const int tab = 4; |
| 2866 | for (int i = 0; i < fTs.count(); ++i) { |
| 2867 | SkPoint out; |
| 2868 | (*SegmentXYAtT[fVerb])(fPts, t(i), &out); |
| 2869 | 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] | 2870 | " otherT=%1.9g windSum=%d\n", |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2871 | tab + sizeof(className), className, fID, |
| 2872 | kLVerbStr[fVerb], i, fTs[i].fT, out.fX, out.fY, |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 2873 | fTs[i].fOther->fID, fTs[i].fOtherT, fTs[i].fWindSum); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2874 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2875 | 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] | 2876 | tab + sizeof(className), className, fID, |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 2877 | fBounds.fLeft, fBounds.fTop, fBounds.fRight, fBounds.fBottom); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2878 | } |
| 2879 | #endif |
| 2880 | |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 2881 | #if DEBUG_CONCIDENT |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 2882 | // assert if pair has not already been added |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2883 | void debugAddTPair(double t, const Segment& other, double otherT) const { |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 2884 | for (int i = 0; i < fTs.count(); ++i) { |
| 2885 | if (fTs[i].fT == t && fTs[i].fOther == &other && fTs[i].fOtherT == otherT) { |
| 2886 | return; |
| 2887 | } |
| 2888 | } |
| 2889 | SkASSERT(0); |
| 2890 | } |
| 2891 | #endif |
| 2892 | |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2893 | #if DEBUG_DUMP |
| 2894 | int debugID() const { |
| 2895 | return fID; |
| 2896 | } |
| 2897 | #endif |
| 2898 | |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2899 | #if DEBUG_WINDING |
| 2900 | void debugShowSums() const { |
| 2901 | SkDebugf("%s id=%d (%1.9g,%1.9g %1.9g,%1.9g)", __FUNCTION__, fID, |
| 2902 | fPts[0].fX, fPts[0].fY, fPts[fVerb].fX, fPts[fVerb].fY); |
| 2903 | for (int i = 0; i < fTs.count(); ++i) { |
| 2904 | const Span& span = fTs[i]; |
| 2905 | SkDebugf(" [t=%1.3g %1.9g,%1.9g w=", span.fT, xAtT(&span), yAtT(&span)); |
| 2906 | if (span.fWindSum == SK_MinS32) { |
| 2907 | SkDebugf("?"); |
| 2908 | } else { |
| 2909 | SkDebugf("%d", span.fWindSum); |
| 2910 | } |
| 2911 | SkDebugf("]"); |
| 2912 | } |
| 2913 | SkDebugf("\n"); |
| 2914 | } |
| 2915 | #endif |
| 2916 | |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 2917 | #if DEBUG_CONCIDENT |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2918 | void debugShowTs() const { |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 2919 | SkDebugf("%s id=%d", __FUNCTION__, fID); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 2920 | for (int i = 0; i < fTs.count(); ++i) { |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 2921 | 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] | 2922 | fTs[i].fT, xAtT(&fTs[i]), yAtT(&fTs[i]), fTs[i].fWindValue); |
| 2923 | } |
| 2924 | SkDebugf("\n"); |
| 2925 | } |
| 2926 | #endif |
| 2927 | |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 2928 | #if DEBUG_ACTIVE_SPANS |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2929 | void debugShowActiveSpans() const { |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 2930 | if (done()) { |
| 2931 | return; |
| 2932 | } |
| 2933 | for (int i = 0; i < fTs.count(); ++i) { |
| 2934 | if (fTs[i].fDone) { |
| 2935 | continue; |
| 2936 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2937 | SkDebugf("%s id=%d", __FUNCTION__, fID); |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 2938 | SkDebugf(" (%1.9g,%1.9g", fPts[0].fX, fPts[0].fY); |
| 2939 | for (int vIndex = 1; vIndex <= fVerb; ++vIndex) { |
| 2940 | SkDebugf(" %1.9g,%1.9g", fPts[vIndex].fX, fPts[vIndex].fY); |
| 2941 | } |
| 2942 | const Span* span = &fTs[i]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2943 | SkDebugf(") t=%1.9g (%1.9g,%1.9g)", fTs[i].fT, |
caryclark@google.com | 0c803d0 | 2012-08-06 11:15:47 +0000 | [diff] [blame] | 2944 | xAtT(span), yAtT(span)); |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 2945 | const Segment* other = fTs[i].fOther; |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 2946 | SkDebugf(" other=%d otherT=%1.9g otherIndex=%d windSum=", |
| 2947 | other->fID, fTs[i].fOtherT, fTs[i].fOtherIndex); |
| 2948 | if (fTs[i].fWindSum == SK_MinS32) { |
| 2949 | SkDebugf("?"); |
| 2950 | } else { |
| 2951 | SkDebugf("%d", fTs[i].fWindSum); |
| 2952 | } |
| 2953 | SkDebugf(" windValue=%d\n", fTs[i].fWindValue); |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 2954 | } |
| 2955 | } |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 2956 | |
| 2957 | // This isn't useful yet -- but leaving it in for now in case i think of something |
| 2958 | // to use it for |
| 2959 | void validateActiveSpans() const { |
| 2960 | if (done()) { |
| 2961 | return; |
| 2962 | } |
| 2963 | int tCount = fTs.count(); |
| 2964 | for (int index = 0; index < tCount; ++index) { |
| 2965 | if (fTs[index].fDone) { |
| 2966 | continue; |
| 2967 | } |
| 2968 | // count number of connections which are not done |
| 2969 | int first = index; |
| 2970 | double baseT = fTs[index].fT; |
| 2971 | while (first > 0 && approximately_equal(fTs[first - 1].fT, baseT)) { |
| 2972 | --first; |
| 2973 | } |
| 2974 | int last = index; |
| 2975 | while (last < tCount - 1 && approximately_equal(fTs[last + 1].fT, baseT)) { |
| 2976 | ++last; |
| 2977 | } |
| 2978 | int connections = 0; |
| 2979 | connections += first > 0 && !fTs[first - 1].fDone; |
| 2980 | for (int test = first; test <= last; ++test) { |
| 2981 | connections += !fTs[test].fDone; |
| 2982 | const Segment* other = fTs[test].fOther; |
| 2983 | int oIndex = fTs[test].fOtherIndex; |
| 2984 | connections += !other->fTs[oIndex].fDone; |
| 2985 | connections += oIndex > 0 && !other->fTs[oIndex - 1].fDone; |
| 2986 | } |
| 2987 | // SkASSERT(!(connections & 1)); |
| 2988 | } |
| 2989 | } |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 2990 | #endif |
| 2991 | |
caryclark@google.com | 0c803d0 | 2012-08-06 11:15:47 +0000 | [diff] [blame] | 2992 | #if DEBUG_MARK_DONE |
| 2993 | void debugShowNewWinding(const char* fun, const Span& span, int winding) { |
| 2994 | const SkPoint& pt = xyAtT(&span); |
| 2995 | SkDebugf("%s id=%d", fun, fID); |
| 2996 | SkDebugf(" (%1.9g,%1.9g", fPts[0].fX, fPts[0].fY); |
| 2997 | for (int vIndex = 1; vIndex <= fVerb; ++vIndex) { |
| 2998 | SkDebugf(" %1.9g,%1.9g", fPts[vIndex].fX, fPts[vIndex].fY); |
| 2999 | } |
| 3000 | SkDebugf(") t=%1.9g (%1.9g,%1.9g) newWindSum=%d windSum=", |
| 3001 | span.fT, pt.fX, pt.fY, winding); |
| 3002 | if (span.fWindSum == SK_MinS32) { |
| 3003 | SkDebugf("?"); |
| 3004 | } else { |
| 3005 | SkDebugf("%d", span.fWindSum); |
| 3006 | } |
| 3007 | SkDebugf(" windValue=%d\n", span.fWindValue); |
| 3008 | } |
| 3009 | #endif |
| 3010 | |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3011 | #if DEBUG_SORT |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 3012 | void debugShowSort(const char* fun, const SkTDArray<Angle*>& angles, int first, |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3013 | const int contourWinding) const { |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 3014 | SkASSERT(angles[first]->segment() == this); |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 3015 | SkASSERT(angles.count() > 1); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3016 | int lastSum = contourWinding; |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 3017 | int windSum = lastSum - spanSign(angles[first]); |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 3018 | SkDebugf("%s %s contourWinding=%d sign=%d\n", fun, __FUNCTION__, |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 3019 | contourWinding, spanSign(angles[first])); |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 3020 | int index = first; |
| 3021 | bool firstTime = true; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3022 | do { |
| 3023 | const Angle& angle = *angles[index]; |
| 3024 | const Segment& segment = *angle.segment(); |
| 3025 | int start = angle.start(); |
| 3026 | int end = angle.end(); |
| 3027 | const Span& sSpan = segment.fTs[start]; |
| 3028 | const Span& eSpan = segment.fTs[end]; |
| 3029 | const Span& mSpan = segment.fTs[SkMin32(start, end)]; |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3030 | if (!firstTime) { |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 3031 | lastSum = windSum; |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 3032 | windSum -= segment.spanSign(&angle); |
caryclark@google.com | afe56de | 2012-07-24 18:11:03 +0000 | [diff] [blame] | 3033 | } |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 3034 | SkDebugf("%s [%d] %s id=%d %s start=%d (%1.9g,%,1.9g) end=%d (%1.9g,%,1.9g)" |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 3035 | " sign=%d windValue=%d winding: %d->%d (max=%d) done=%d\n", |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 3036 | __FUNCTION__, index, angle.unsortable() ? "*** UNSORTABLE ***" : "", |
| 3037 | segment.fID, kLVerbStr[segment.fVerb], |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 3038 | start, segment.xAtT(&sSpan), |
| 3039 | segment.yAtT(&sSpan), end, segment.xAtT(&eSpan), |
| 3040 | segment.yAtT(&eSpan), angle.sign(), mSpan.fWindValue, |
| 3041 | lastSum, windSum, useInnerWinding(lastSum, windSum) |
| 3042 | ? windSum : lastSum, mSpan.fDone); |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 3043 | #if false && DEBUG_ANGLE |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 3044 | angle.debugShow(segment.xyAtT(&sSpan)); |
| 3045 | #endif |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3046 | ++index; |
| 3047 | if (index == angles.count()) { |
| 3048 | index = 0; |
| 3049 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3050 | if (firstTime) { |
| 3051 | firstTime = false; |
| 3052 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3053 | } while (index != first); |
| 3054 | } |
| 3055 | #endif |
| 3056 | |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3057 | #if DEBUG_WINDING |
| 3058 | bool debugVerifyWinding(int start, int end, int winding) const { |
| 3059 | const Span& span = fTs[SkMin32(start, end)]; |
| 3060 | int spanWinding = span.fWindSum; |
| 3061 | if (spanWinding == SK_MinS32) { |
| 3062 | return true; |
| 3063 | } |
| 3064 | int spanSign = SkSign32(start - end); |
| 3065 | int signedVal = spanSign * span.fWindValue; |
| 3066 | if (signedVal < 0) { |
| 3067 | spanWinding -= signedVal; |
| 3068 | } |
| 3069 | return span.fWindSum == winding; |
| 3070 | } |
| 3071 | #endif |
| 3072 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3073 | private: |
| 3074 | const SkPoint* fPts; |
| 3075 | SkPath::Verb fVerb; |
| 3076 | Bounds fBounds; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 3077 | SkTDArray<Span> fTs; // two or more (always includes t=0 t=1) |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 3078 | int fDoneSpans; // quick check that segment is finished |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3079 | bool fOperand; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3080 | #if DEBUG_DUMP |
| 3081 | int fID; |
| 3082 | #endif |
| 3083 | }; |
| 3084 | |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 3085 | class Contour; |
| 3086 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3087 | struct Coincidence { |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 3088 | Contour* fContours[2]; |
| 3089 | int fSegments[2]; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3090 | double fTs[2][2]; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3091 | bool fXor; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3092 | }; |
| 3093 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3094 | class Contour { |
| 3095 | public: |
| 3096 | Contour() { |
| 3097 | reset(); |
| 3098 | #if DEBUG_DUMP |
| 3099 | fID = ++gContourID; |
| 3100 | #endif |
| 3101 | } |
| 3102 | |
| 3103 | bool operator<(const Contour& rh) const { |
| 3104 | return fBounds.fTop == rh.fBounds.fTop |
| 3105 | ? fBounds.fLeft < rh.fBounds.fLeft |
| 3106 | : fBounds.fTop < rh.fBounds.fTop; |
| 3107 | } |
| 3108 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3109 | void addCoincident(int index, Contour* other, int otherIndex, |
| 3110 | const Intersections& ts, bool swap) { |
| 3111 | Coincidence& coincidence = *fCoincidences.append(); |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 3112 | coincidence.fContours[0] = this; |
| 3113 | coincidence.fContours[1] = other; |
| 3114 | coincidence.fSegments[0] = index; |
| 3115 | coincidence.fSegments[1] = otherIndex; |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 3116 | if (fSegments[index].verb() == SkPath::kLine_Verb && |
| 3117 | other->fSegments[otherIndex].verb() == SkPath::kLine_Verb) { |
| 3118 | // FIXME: coincident lines use legacy Ts instead of coincident Ts |
| 3119 | coincidence.fTs[swap][0] = ts.fT[0][0]; |
| 3120 | coincidence.fTs[swap][1] = ts.fT[0][1]; |
| 3121 | coincidence.fTs[!swap][0] = ts.fT[1][0]; |
| 3122 | coincidence.fTs[!swap][1] = ts.fT[1][1]; |
| 3123 | } else if (fSegments[index].verb() == SkPath::kQuad_Verb && |
| 3124 | other->fSegments[otherIndex].verb() == SkPath::kQuad_Verb) { |
| 3125 | coincidence.fTs[swap][0] = ts.fCoincidentT[0][0]; |
| 3126 | coincidence.fTs[swap][1] = ts.fCoincidentT[0][1]; |
| 3127 | coincidence.fTs[!swap][0] = ts.fCoincidentT[1][0]; |
| 3128 | coincidence.fTs[!swap][1] = ts.fCoincidentT[1][1]; |
| 3129 | } |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3130 | coincidence.fXor = fOperand == other->fOperand ? fXor : true; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
| 3133 | void addCross(const Contour* crosser) { |
| 3134 | #ifdef DEBUG_CROSS |
| 3135 | for (int index = 0; index < fCrosses.count(); ++index) { |
| 3136 | SkASSERT(fCrosses[index] != crosser); |
| 3137 | } |
| 3138 | #endif |
| 3139 | *fCrosses.append() = crosser; |
| 3140 | } |
| 3141 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3142 | void addCubic(const SkPoint pts[4]) { |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3143 | fSegments.push_back().addCubic(pts, fOperand); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3144 | fContainsCurves = true; |
| 3145 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3146 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3147 | int addLine(const SkPoint pts[2]) { |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3148 | fSegments.push_back().addLine(pts, fOperand); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3149 | return fSegments.count(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3150 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 3151 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3152 | void addOtherT(int segIndex, int tIndex, double otherT, int otherIndex) { |
| 3153 | fSegments[segIndex].addOtherT(tIndex, otherT, otherIndex); |
| 3154 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3155 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3156 | int addQuad(const SkPoint pts[3]) { |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3157 | fSegments.push_back().addQuad(pts, fOperand); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3158 | fContainsCurves = true; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3159 | return fSegments.count(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3160 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3161 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3162 | int addT(int segIndex, double newT, Contour* other, int otherIndex) { |
| 3163 | containsIntercepts(); |
| 3164 | return fSegments[segIndex].addT(newT, &other->fSegments[otherIndex]); |
| 3165 | } |
| 3166 | |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3167 | const Bounds& bounds() const { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3168 | return fBounds; |
| 3169 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 3170 | |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 3171 | void collapseTriangles() { |
| 3172 | int segmentCount = fSegments.count(); |
| 3173 | for (int sIndex = 0; sIndex < segmentCount; ++sIndex) { |
| 3174 | fSegments[sIndex].collapseTriangles(fXor); |
| 3175 | } |
| 3176 | } |
| 3177 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3178 | void complete() { |
| 3179 | setBounds(); |
| 3180 | fContainsIntercepts = false; |
| 3181 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3182 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3183 | void containsIntercepts() { |
| 3184 | fContainsIntercepts = true; |
| 3185 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3186 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 3187 | const Segment* crossedSegment(const SkPoint& basePt, SkScalar& bestY, |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3188 | int &tIndex, double& hitT) { |
| 3189 | int segmentCount = fSegments.count(); |
| 3190 | const Segment* bestSegment = NULL; |
| 3191 | for (int test = 0; test < segmentCount; ++test) { |
| 3192 | Segment* testSegment = &fSegments[test]; |
| 3193 | const SkRect& bounds = testSegment->bounds(); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3194 | if (bounds.fBottom <= bestY) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3195 | continue; |
| 3196 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3197 | if (bounds.fTop >= basePt.fY) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3198 | continue; |
| 3199 | } |
| 3200 | if (bounds.fLeft > basePt.fX) { |
| 3201 | continue; |
| 3202 | } |
| 3203 | if (bounds.fRight < basePt.fX) { |
| 3204 | continue; |
| 3205 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3206 | if (bounds.fLeft == bounds.fRight) { |
| 3207 | continue; |
| 3208 | } |
| 3209 | #if 0 |
| 3210 | bool leftHalf = bounds.fLeft == basePt.fX; |
| 3211 | bool rightHalf = bounds.fRight == basePt.fX; |
| 3212 | if ((leftHalf || rightHalf) && !testSegment->crossedSpanHalves( |
| 3213 | basePt, leftHalf, rightHalf)) { |
| 3214 | continue; |
| 3215 | } |
| 3216 | #endif |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3217 | double testHitT; |
| 3218 | int testT = testSegment->crossedSpan(basePt, bestY, testHitT); |
| 3219 | if (testT >= 0) { |
| 3220 | bestSegment = testSegment; |
| 3221 | tIndex = testT; |
| 3222 | hitT = testHitT; |
| 3223 | } |
| 3224 | } |
| 3225 | return bestSegment; |
| 3226 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 3227 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3228 | bool crosses(const Contour* crosser) const { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3229 | for (int index = 0; index < fCrosses.count(); ++index) { |
| 3230 | if (fCrosses[index] == crosser) { |
| 3231 | return true; |
| 3232 | } |
| 3233 | } |
| 3234 | return false; |
| 3235 | } |
| 3236 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3237 | void findTooCloseToCall() { |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3238 | int segmentCount = fSegments.count(); |
| 3239 | for (int sIndex = 0; sIndex < segmentCount; ++sIndex) { |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3240 | fSegments[sIndex].findTooCloseToCall(fXor); |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3241 | } |
| 3242 | } |
| 3243 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3244 | void fixOtherTIndex() { |
| 3245 | int segmentCount = fSegments.count(); |
| 3246 | for (int sIndex = 0; sIndex < segmentCount; ++sIndex) { |
| 3247 | fSegments[sIndex].fixOtherTIndex(); |
| 3248 | } |
| 3249 | } |
| 3250 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3251 | void reset() { |
| 3252 | fSegments.reset(); |
| 3253 | fBounds.set(SK_ScalarMax, SK_ScalarMax, SK_ScalarMax, SK_ScalarMax); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 3254 | fContainsCurves = fContainsIntercepts = false; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3255 | } |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 3256 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3257 | // FIXME: for binary ops, need to keep both ops winding contributions separately |
| 3258 | // in edge array |
| 3259 | void resolveCoincidence() { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3260 | int count = fCoincidences.count(); |
| 3261 | for (int index = 0; index < count; ++index) { |
| 3262 | Coincidence& coincidence = fCoincidences[index]; |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 3263 | Contour* thisContour = coincidence.fContours[0]; |
| 3264 | Contour* otherContour = coincidence.fContours[1]; |
| 3265 | int thisIndex = coincidence.fSegments[0]; |
| 3266 | int otherIndex = coincidence.fSegments[1]; |
| 3267 | Segment& thisOne = thisContour->fSegments[thisIndex]; |
| 3268 | Segment& other = otherContour->fSegments[otherIndex]; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3269 | #if DEBUG_CONCIDENT |
| 3270 | thisOne.debugShowTs(); |
| 3271 | other.debugShowTs(); |
| 3272 | #endif |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3273 | double startT = coincidence.fTs[0][0]; |
| 3274 | double endT = coincidence.fTs[0][1]; |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 3275 | bool opposite = false; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3276 | if (startT > endT) { |
| 3277 | SkTSwap<double>(startT, endT); |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 3278 | opposite ^= true; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3279 | } |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 3280 | SkASSERT(!approximately_negative(endT - startT)); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3281 | double oStartT = coincidence.fTs[1][0]; |
| 3282 | double oEndT = coincidence.fTs[1][1]; |
| 3283 | if (oStartT > oEndT) { |
| 3284 | SkTSwap<double>(oStartT, oEndT); |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 3285 | opposite ^= true; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3286 | } |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 3287 | SkASSERT(!approximately_negative(oEndT - oStartT)); |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 3288 | if (opposite) { |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 3289 | // make sure startT and endT have t entries |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 3290 | SkASSERT(opposite); |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 3291 | if (startT > 0 || oEndT < 1 |
| 3292 | || thisOne.isMissing(startT) || other.isMissing(oEndT)) { |
| 3293 | thisOne.addTPair(startT, other, oEndT, true); |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 3294 | } |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 3295 | if (oStartT > 0 || endT < 1 |
| 3296 | || thisOne.isMissing(endT) || other.isMissing(oStartT)) { |
| 3297 | other.addTPair(oStartT, thisOne, endT, true); |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 3298 | } |
| 3299 | thisOne.addTCancel(startT, endT, other, oStartT, oEndT); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3300 | } else { |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 3301 | if (startT > 0 || oStartT > 0 |
| 3302 | || thisOne.isMissing(startT) || other.isMissing(oStartT)) { |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 3303 | thisOne.addTPair(startT, other, oStartT, true); |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 3304 | } |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 3305 | if (endT < 1 || oEndT < 1 |
| 3306 | || thisOne.isMissing(endT) || other.isMissing(oEndT)) { |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 3307 | other.addTPair(oEndT, thisOne, endT, true); |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 3308 | } |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3309 | thisOne.addTCoincident(coincidence.fXor, startT, endT, other, oStartT, oEndT); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3310 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 3311 | #if DEBUG_CONCIDENT |
| 3312 | thisOne.debugShowTs(); |
| 3313 | other.debugShowTs(); |
| 3314 | #endif |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3315 | } |
| 3316 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 3317 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3318 | const SkTArray<Segment>& segments() { |
| 3319 | return fSegments; |
| 3320 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 3321 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3322 | void setOperand(bool isOp) { |
| 3323 | fOperand = isOp; |
| 3324 | } |
skia.committer@gmail.com | 055c7c2 | 2012-09-15 02:01:41 +0000 | [diff] [blame] | 3325 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3326 | void setXor(bool isXor) { |
| 3327 | fXor = isXor; |
| 3328 | } |
| 3329 | |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 3330 | // OPTIMIZATION: feel pretty uneasy about this. It seems like once again |
| 3331 | // we need to sort and walk edges in y, but that on the surface opens the |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 3332 | // same can of worms as before. But then, this is a rough sort based on |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 3333 | // segments' top, and not a true sort, so it could be ameniable to regular |
| 3334 | // 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] | 3335 | Segment* topSegment(SkScalar& bestY) { |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 3336 | int segmentCount = fSegments.count(); |
| 3337 | SkASSERT(segmentCount > 0); |
| 3338 | int best = -1; |
| 3339 | Segment* bestSegment = NULL; |
| 3340 | while (++best < segmentCount) { |
| 3341 | Segment* testSegment = &fSegments[best]; |
| 3342 | if (testSegment->done()) { |
| 3343 | continue; |
| 3344 | } |
| 3345 | bestSegment = testSegment; |
| 3346 | break; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3347 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 3348 | if (!bestSegment) { |
| 3349 | return NULL; |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3350 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3351 | SkScalar bestTop = bestSegment->activeTop(); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 3352 | for (int test = best + 1; test < segmentCount; ++test) { |
| 3353 | Segment* testSegment = &fSegments[test]; |
| 3354 | if (testSegment->done()) { |
| 3355 | continue; |
| 3356 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3357 | if (testSegment->bounds().fTop > bestTop) { |
| 3358 | continue; |
| 3359 | } |
| 3360 | SkScalar testTop = testSegment->activeTop(); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 3361 | if (bestTop > testTop) { |
| 3362 | bestTop = testTop; |
| 3363 | bestSegment = testSegment; |
| 3364 | } |
| 3365 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3366 | bestY = bestTop; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 3367 | return bestSegment; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3368 | } |
| 3369 | |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 3370 | Segment* undoneSegment(int& start, int& end) { |
| 3371 | int segmentCount = fSegments.count(); |
| 3372 | for (int test = 0; test < segmentCount; ++test) { |
| 3373 | Segment* testSegment = &fSegments[test]; |
| 3374 | if (testSegment->done()) { |
| 3375 | continue; |
| 3376 | } |
| 3377 | testSegment->undoneSpan(start, end); |
| 3378 | return testSegment; |
| 3379 | } |
| 3380 | return NULL; |
| 3381 | } |
| 3382 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3383 | int updateSegment(int index, const SkPoint* pts) { |
| 3384 | Segment& segment = fSegments[index]; |
| 3385 | segment.updatePts(pts); |
| 3386 | return segment.verb() + 1; |
| 3387 | } |
| 3388 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3389 | #if DEBUG_TEST |
| 3390 | SkTArray<Segment>& debugSegments() { |
| 3391 | return fSegments; |
| 3392 | } |
| 3393 | #endif |
| 3394 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3395 | #if DEBUG_DUMP |
| 3396 | void dump() { |
| 3397 | int i; |
| 3398 | const char className[] = "Contour"; |
| 3399 | const int tab = 4; |
| 3400 | SkDebugf("%s %p (contour=%d)\n", className, this, fID); |
| 3401 | for (i = 0; i < fSegments.count(); ++i) { |
| 3402 | SkDebugf("%*s.fSegments[%d]:\n", tab + sizeof(className), |
| 3403 | className, i); |
| 3404 | fSegments[i].dump(); |
| 3405 | } |
| 3406 | SkDebugf("%*s.fBounds=(l:%1.9g, t:%1.9g r:%1.9g, b:%1.9g)\n", |
| 3407 | tab + sizeof(className), className, |
| 3408 | fBounds.fLeft, fBounds.fTop, |
| 3409 | fBounds.fRight, fBounds.fBottom); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3410 | SkDebugf("%*s.fContainsIntercepts=%d\n", tab + sizeof(className), |
| 3411 | className, fContainsIntercepts); |
| 3412 | SkDebugf("%*s.fContainsCurves=%d\n", tab + sizeof(className), |
| 3413 | className, fContainsCurves); |
| 3414 | } |
| 3415 | #endif |
| 3416 | |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 3417 | #if DEBUG_ACTIVE_SPANS |
| 3418 | void debugShowActiveSpans() { |
| 3419 | for (int index = 0; index < fSegments.count(); ++index) { |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 3420 | fSegments[index].debugShowActiveSpans(); |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 3421 | } |
| 3422 | } |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 3423 | |
| 3424 | void validateActiveSpans() { |
| 3425 | for (int index = 0; index < fSegments.count(); ++index) { |
| 3426 | fSegments[index].validateActiveSpans(); |
| 3427 | } |
| 3428 | } |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 3429 | #endif |
| 3430 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3431 | protected: |
| 3432 | void setBounds() { |
| 3433 | int count = fSegments.count(); |
| 3434 | if (count == 0) { |
| 3435 | SkDebugf("%s empty contour\n", __FUNCTION__); |
| 3436 | SkASSERT(0); |
| 3437 | // FIXME: delete empty contour? |
| 3438 | return; |
| 3439 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3440 | fBounds = fSegments.front().bounds(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3441 | for (int index = 1; index < count; ++index) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3442 | fBounds.add(fSegments[index].bounds()); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3443 | } |
| 3444 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3445 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3446 | private: |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3447 | SkTArray<Segment> fSegments; |
| 3448 | SkTDArray<Coincidence> fCoincidences; |
| 3449 | SkTDArray<const Contour*> fCrosses; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3450 | Bounds fBounds; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3451 | bool fContainsIntercepts; |
| 3452 | bool fContainsCurves; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3453 | bool fOperand; // true for the second argument to a binary operator |
| 3454 | bool fXor; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3455 | #if DEBUG_DUMP |
| 3456 | int fID; |
| 3457 | #endif |
| 3458 | }; |
| 3459 | |
| 3460 | class EdgeBuilder { |
| 3461 | public: |
| 3462 | |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3463 | EdgeBuilder(const SkPath& path, SkTArray<Contour>& contours) |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3464 | : fPath(&path) |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3465 | , fContours(contours) |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3466 | , fCurrentContour(NULL) |
| 3467 | , fOperand(false) |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3468 | { |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3469 | fXorMask = (path.getFillType() & 1) ? kEvenOdd_Mask : kWinding_Mask; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3470 | #if DEBUG_DUMP |
| 3471 | gContourID = 0; |
| 3472 | gSegmentID = 0; |
| 3473 | #endif |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3474 | fSecondHalf = preFetch(); |
| 3475 | } |
| 3476 | |
| 3477 | void addOperand(const SkPath& path) { |
| 3478 | fPath = &path; |
| 3479 | fXorMask = (path.getFillType() & 1) ? kEvenOdd_Mask : kWinding_Mask; |
| 3480 | preFetch(); |
| 3481 | } |
| 3482 | |
| 3483 | void finish() { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3484 | walk(); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3485 | complete(); |
| 3486 | if (fCurrentContour && !fCurrentContour->segments().count()) { |
| 3487 | fContours.pop_back(); |
| 3488 | } |
| 3489 | // correct pointers in contours since fReducePts may have moved as it grew |
| 3490 | int cIndex = 0; |
| 3491 | int extraCount = fExtra.count(); |
| 3492 | SkASSERT(extraCount == 0 || fExtra[0] == -1); |
| 3493 | int eIndex = 0; |
| 3494 | int rIndex = 0; |
| 3495 | while (++eIndex < extraCount) { |
| 3496 | int offset = fExtra[eIndex]; |
| 3497 | if (offset < 0) { |
| 3498 | ++cIndex; |
| 3499 | continue; |
| 3500 | } |
| 3501 | fCurrentContour = &fContours[cIndex]; |
| 3502 | rIndex += fCurrentContour->updateSegment(offset - 1, |
| 3503 | &fReducePts[rIndex]); |
| 3504 | } |
| 3505 | fExtra.reset(); // we're done with this |
| 3506 | } |
| 3507 | |
| 3508 | ShapeOpMask xorMask() const { |
| 3509 | return fXorMask; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3510 | } |
| 3511 | |
| 3512 | protected: |
| 3513 | |
| 3514 | void complete() { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3515 | if (fCurrentContour && fCurrentContour->segments().count()) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3516 | fCurrentContour->complete(); |
| 3517 | fCurrentContour = NULL; |
| 3518 | } |
| 3519 | } |
| 3520 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3521 | // FIXME:remove once we can access path pts directly |
| 3522 | int preFetch() { |
| 3523 | SkPath::RawIter iter(*fPath); // FIXME: access path directly when allowed |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3524 | SkPoint pts[4]; |
| 3525 | SkPath::Verb verb; |
| 3526 | do { |
| 3527 | verb = iter.next(pts); |
| 3528 | *fPathVerbs.append() = verb; |
| 3529 | if (verb == SkPath::kMove_Verb) { |
| 3530 | *fPathPts.append() = pts[0]; |
| 3531 | } else if (verb >= SkPath::kLine_Verb && verb <= SkPath::kCubic_Verb) { |
| 3532 | fPathPts.append(verb, &pts[1]); |
| 3533 | } |
| 3534 | } while (verb != SkPath::kDone_Verb); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3535 | return fPathVerbs.count(); |
| 3536 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3537 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3538 | void walk() { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3539 | SkPath::Verb reducedVerb; |
| 3540 | uint8_t* verbPtr = fPathVerbs.begin(); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3541 | uint8_t* endOfFirstHalf = &verbPtr[fSecondHalf]; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3542 | const SkPoint* pointsPtr = fPathPts.begin(); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3543 | const SkPoint* finalCurveStart = NULL; |
| 3544 | const SkPoint* finalCurveEnd = NULL; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3545 | SkPath::Verb verb; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3546 | while ((verb = (SkPath::Verb) *verbPtr++) != SkPath::kDone_Verb) { |
| 3547 | switch (verb) { |
| 3548 | case SkPath::kMove_Verb: |
| 3549 | complete(); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3550 | if (!fCurrentContour) { |
| 3551 | fCurrentContour = fContours.push_back_n(1); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3552 | fCurrentContour->setOperand(fOperand); |
| 3553 | fCurrentContour->setXor(fXorMask == kEvenOdd_Mask); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3554 | *fExtra.append() = -1; // start new contour |
| 3555 | } |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 3556 | finalCurveEnd = pointsPtr++; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3557 | continue; |
| 3558 | case SkPath::kLine_Verb: |
| 3559 | // skip degenerate points |
| 3560 | if (pointsPtr[-1].fX != pointsPtr[0].fX |
| 3561 | || pointsPtr[-1].fY != pointsPtr[0].fY) { |
| 3562 | fCurrentContour->addLine(&pointsPtr[-1]); |
| 3563 | } |
| 3564 | break; |
| 3565 | case SkPath::kQuad_Verb: |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 3566 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3567 | reducedVerb = QuadReduceOrder(&pointsPtr[-1], fReducePts); |
| 3568 | if (reducedVerb == 0) { |
| 3569 | break; // skip degenerate points |
| 3570 | } |
| 3571 | if (reducedVerb == 1) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 3572 | *fExtra.append() = |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3573 | fCurrentContour->addLine(fReducePts.end() - 2); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3574 | break; |
| 3575 | } |
| 3576 | fCurrentContour->addQuad(&pointsPtr[-1]); |
| 3577 | break; |
| 3578 | case SkPath::kCubic_Verb: |
| 3579 | reducedVerb = CubicReduceOrder(&pointsPtr[-1], fReducePts); |
| 3580 | if (reducedVerb == 0) { |
| 3581 | break; // skip degenerate points |
| 3582 | } |
| 3583 | if (reducedVerb == 1) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3584 | *fExtra.append() = |
| 3585 | fCurrentContour->addLine(fReducePts.end() - 2); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3586 | break; |
| 3587 | } |
| 3588 | if (reducedVerb == 2) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3589 | *fExtra.append() = |
| 3590 | fCurrentContour->addQuad(fReducePts.end() - 3); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3591 | break; |
| 3592 | } |
| 3593 | fCurrentContour->addCubic(&pointsPtr[-1]); |
| 3594 | break; |
| 3595 | case SkPath::kClose_Verb: |
| 3596 | SkASSERT(fCurrentContour); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3597 | if (finalCurveStart && finalCurveEnd |
| 3598 | && *finalCurveStart != *finalCurveEnd) { |
| 3599 | *fReducePts.append() = *finalCurveStart; |
| 3600 | *fReducePts.append() = *finalCurveEnd; |
| 3601 | *fExtra.append() = |
| 3602 | fCurrentContour->addLine(fReducePts.end() - 2); |
| 3603 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3604 | complete(); |
| 3605 | continue; |
| 3606 | default: |
| 3607 | SkDEBUGFAIL("bad verb"); |
| 3608 | return; |
| 3609 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3610 | finalCurveStart = &pointsPtr[verb - 1]; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3611 | pointsPtr += verb; |
| 3612 | SkASSERT(fCurrentContour); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3613 | if (verbPtr == endOfFirstHalf) { |
| 3614 | fOperand = true; |
skia.committer@gmail.com | 055c7c2 | 2012-09-15 02:01:41 +0000 | [diff] [blame] | 3615 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3616 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3617 | } |
| 3618 | |
| 3619 | private: |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3620 | const SkPath* fPath; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3621 | SkTDArray<SkPoint> fPathPts; // FIXME: point directly to path pts instead |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3622 | SkTDArray<uint8_t> fPathVerbs; // FIXME: remove |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3623 | Contour* fCurrentContour; |
| 3624 | SkTArray<Contour>& fContours; |
| 3625 | SkTDArray<SkPoint> fReducePts; // segments created on the fly |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3626 | SkTDArray<int> fExtra; // -1 marks new contour, > 0 offsets into contour |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 3627 | ShapeOpMask fXorMask; |
| 3628 | int fSecondHalf; |
| 3629 | bool fOperand; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3630 | }; |
| 3631 | |
| 3632 | class Work { |
| 3633 | public: |
| 3634 | enum SegmentType { |
| 3635 | kHorizontalLine_Segment = -1, |
| 3636 | kVerticalLine_Segment = 0, |
| 3637 | kLine_Segment = SkPath::kLine_Verb, |
| 3638 | kQuad_Segment = SkPath::kQuad_Verb, |
| 3639 | kCubic_Segment = SkPath::kCubic_Verb, |
| 3640 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 3641 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3642 | void addCoincident(Work& other, const Intersections& ts, bool swap) { |
| 3643 | fContour->addCoincident(fIndex, other.fContour, other.fIndex, ts, swap); |
| 3644 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3645 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3646 | // FIXME: does it make sense to write otherIndex now if we're going to |
| 3647 | // fix it up later? |
| 3648 | void addOtherT(int index, double otherT, int otherIndex) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3649 | fContour->addOtherT(fIndex, index, otherT, otherIndex); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3650 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3651 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3652 | // Avoid collapsing t values that are close to the same since |
| 3653 | // we walk ts to describe consecutive intersections. Since a pair of ts can |
| 3654 | // be nearly equal, any problems caused by this should be taken care |
| 3655 | // of later. |
| 3656 | // 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] | 3657 | int addT(double newT, const Work& other) { |
| 3658 | return fContour->addT(fIndex, newT, other.fContour, other.fIndex); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3659 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3660 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3661 | bool advance() { |
| 3662 | return ++fIndex < fLast; |
| 3663 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3664 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3665 | SkScalar bottom() const { |
| 3666 | return bounds().fBottom; |
| 3667 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3668 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3669 | const Bounds& bounds() const { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3670 | return fContour->segments()[fIndex].bounds(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3671 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 3672 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3673 | const SkPoint* cubic() const { |
| 3674 | return fCubic; |
| 3675 | } |
| 3676 | |
| 3677 | void init(Contour* contour) { |
| 3678 | fContour = contour; |
| 3679 | fIndex = 0; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3680 | fLast = contour->segments().count(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3681 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 3682 | |
caryclark@google.com | 66ca2fb | 2012-07-03 14:30:08 +0000 | [diff] [blame] | 3683 | bool isAdjacent(const Work& next) { |
| 3684 | return fContour == next.fContour && fIndex + 1 == next.fIndex; |
| 3685 | } |
| 3686 | |
| 3687 | bool isFirstLast(const Work& next) { |
| 3688 | return fContour == next.fContour && fIndex == 0 |
| 3689 | && next.fIndex == fLast - 1; |
| 3690 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3691 | |
| 3692 | SkScalar left() const { |
| 3693 | return bounds().fLeft; |
| 3694 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3695 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3696 | void promoteToCubic() { |
| 3697 | fCubic[0] = pts()[0]; |
| 3698 | fCubic[2] = pts()[1]; |
| 3699 | fCubic[3] = pts()[2]; |
| 3700 | fCubic[1].fX = (fCubic[0].fX + fCubic[2].fX * 2) / 3; |
| 3701 | fCubic[1].fY = (fCubic[0].fY + fCubic[2].fY * 2) / 3; |
| 3702 | fCubic[2].fX = (fCubic[3].fX + fCubic[2].fX * 2) / 3; |
| 3703 | fCubic[2].fY = (fCubic[3].fY + fCubic[2].fY * 2) / 3; |
| 3704 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3705 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3706 | const SkPoint* pts() const { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3707 | return fContour->segments()[fIndex].pts(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
| 3710 | SkScalar right() const { |
| 3711 | return bounds().fRight; |
| 3712 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3713 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3714 | ptrdiff_t segmentIndex() const { |
| 3715 | return fIndex; |
| 3716 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3717 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3718 | SegmentType segmentType() const { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3719 | const Segment& segment = fContour->segments()[fIndex]; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3720 | SegmentType type = (SegmentType) segment.verb(); |
| 3721 | if (type != kLine_Segment) { |
| 3722 | return type; |
| 3723 | } |
| 3724 | if (segment.isHorizontal()) { |
| 3725 | return kHorizontalLine_Segment; |
| 3726 | } |
| 3727 | if (segment.isVertical()) { |
| 3728 | return kVerticalLine_Segment; |
| 3729 | } |
| 3730 | return kLine_Segment; |
| 3731 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3732 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3733 | bool startAfter(const Work& after) { |
| 3734 | fIndex = after.fIndex; |
| 3735 | return advance(); |
| 3736 | } |
| 3737 | |
| 3738 | SkScalar top() const { |
| 3739 | return bounds().fTop; |
| 3740 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3741 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3742 | SkPath::Verb verb() const { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3743 | return fContour->segments()[fIndex].verb(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3744 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 3745 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3746 | SkScalar x() const { |
| 3747 | return bounds().fLeft; |
| 3748 | } |
| 3749 | |
| 3750 | bool xFlipped() const { |
| 3751 | return x() != pts()[0].fX; |
| 3752 | } |
| 3753 | |
| 3754 | SkScalar y() const { |
| 3755 | return bounds().fTop; |
| 3756 | } |
| 3757 | |
| 3758 | bool yFlipped() const { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3759 | return y() != pts()[0].fY; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3760 | } |
| 3761 | |
| 3762 | protected: |
| 3763 | Contour* fContour; |
| 3764 | SkPoint fCubic[4]; |
| 3765 | int fIndex; |
| 3766 | int fLast; |
| 3767 | }; |
| 3768 | |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 3769 | #if DEBUG_ADD_INTERSECTING_TS |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3770 | static void debugShowLineIntersection(int pts, const Work& wt, |
| 3771 | const Work& wn, const double wtTs[2], const double wnTs[2]) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3772 | if (!pts) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3773 | SkDebugf("%s no intersect (%1.9g,%1.9g %1.9g,%1.9g) (%1.9g,%1.9g %1.9g,%1.9g)\n", |
| 3774 | __FUNCTION__, wt.pts()[0].fX, wt.pts()[0].fY, |
| 3775 | wt.pts()[1].fX, wt.pts()[1].fY, wn.pts()[0].fX, wn.pts()[0].fY, |
| 3776 | wn.pts()[1].fX, wn.pts()[1].fY); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3777 | return; |
| 3778 | } |
| 3779 | SkPoint wtOutPt, wnOutPt; |
| 3780 | LineXYAtT(wt.pts(), wtTs[0], &wtOutPt); |
| 3781 | LineXYAtT(wn.pts(), wnTs[0], &wnOutPt); |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 3782 | SkDebugf("%s wtTs[0]=%1.9g (%1.9g,%1.9g %1.9g,%1.9g) (%1.9g,%1.9g)", |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3783 | __FUNCTION__, |
| 3784 | wtTs[0], wt.pts()[0].fX, wt.pts()[0].fY, |
| 3785 | wt.pts()[1].fX, wt.pts()[1].fY, wtOutPt.fX, wtOutPt.fY); |
| 3786 | if (pts == 2) { |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 3787 | SkDebugf(" wtTs[1]=%1.9g", wtTs[1]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3788 | } |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 3789 | SkDebugf(" wnTs[0]=%g (%1.9g,%1.9g %1.9g,%1.9g) (%1.9g,%1.9g)", |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3790 | wnTs[0], wn.pts()[0].fX, wn.pts()[0].fY, |
| 3791 | wn.pts()[1].fX, wn.pts()[1].fY, wnOutPt.fX, wnOutPt.fY); |
| 3792 | if (pts == 2) { |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 3793 | SkDebugf(" wnTs[1]=%1.9g", wnTs[1]); |
| 3794 | } |
| 3795 | SkDebugf("\n"); |
| 3796 | } |
| 3797 | |
| 3798 | static void debugShowQuadIntersection(int pts, const Work& wt, |
| 3799 | const Work& wn, const double wtTs[2], const double wnTs[2]) { |
| 3800 | if (!pts) { |
| 3801 | SkDebugf("%s no intersect (%1.9g,%1.9g %1.9g,%1.9g %1.9g,%1.9g)" |
| 3802 | " (%1.9g,%1.9g %1.9g,%1.9g %1.9g,%1.9g)\n", |
| 3803 | __FUNCTION__, wt.pts()[0].fX, wt.pts()[0].fY, |
skia.committer@gmail.com | 5b6f916 | 2012-10-12 02:01:15 +0000 | [diff] [blame] | 3804 | wt.pts()[1].fX, wt.pts()[1].fY, wt.pts()[2].fX, wt.pts()[2].fY, |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 3805 | wn.pts()[0].fX, wn.pts()[0].fY, wn.pts()[1].fX, wn.pts()[1].fY, |
| 3806 | wt.pts()[2].fX, wt.pts()[2].fY ); |
| 3807 | return; |
| 3808 | } |
| 3809 | SkPoint wtOutPt, wnOutPt; |
| 3810 | QuadXYAtT(wt.pts(), wtTs[0], &wtOutPt); |
| 3811 | QuadXYAtT(wn.pts(), wnTs[0], &wnOutPt); |
| 3812 | SkDebugf("%s wtTs[0]=%1.9g (%1.9g,%1.9g %1.9g,%1.9g %1.9g,%1.9g) (%1.9g,%1.9g)", |
| 3813 | __FUNCTION__, |
| 3814 | wtTs[0], wt.pts()[0].fX, wt.pts()[0].fY, |
| 3815 | wt.pts()[1].fX, wt.pts()[1].fY, wt.pts()[2].fX, wt.pts()[2].fY, |
| 3816 | wtOutPt.fX, wtOutPt.fY); |
| 3817 | if (pts == 2) { |
| 3818 | SkDebugf(" wtTs[1]=%1.9g", wtTs[1]); |
| 3819 | } |
| 3820 | SkDebugf(" wnTs[0]=%g (%1.9g,%1.9g %1.9g,%1.9g %1.9g,%1.9g) (%1.9g,%1.9g)", |
| 3821 | wnTs[0], wn.pts()[0].fX, wn.pts()[0].fY, |
| 3822 | wn.pts()[1].fX, wn.pts()[1].fY, wn.pts()[2].fX, wn.pts()[2].fY, |
| 3823 | wnOutPt.fX, wnOutPt.fY); |
| 3824 | if (pts == 2) { |
| 3825 | SkDebugf(" wnTs[1]=%1.9g", wnTs[1]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3826 | } |
caryclark@google.com | b973801 | 2012-07-03 19:53:30 +0000 | [diff] [blame] | 3827 | SkDebugf("\n"); |
| 3828 | } |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 3829 | #else |
| 3830 | static void debugShowLineIntersection(int , const Work& , |
| 3831 | const Work& , const double [2], const double [2]) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3832 | } |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 3833 | |
| 3834 | static void debugShowQuadIntersection(int , const Work& , |
| 3835 | const Work& , const double [2], const double [2]) { |
| 3836 | } |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 3837 | #endif |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3838 | |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 3839 | static bool addIntersectTs(Contour* test, Contour* next) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3840 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3841 | if (test != next) { |
| 3842 | if (test->bounds().fBottom < next->bounds().fTop) { |
| 3843 | return false; |
| 3844 | } |
| 3845 | if (!Bounds::Intersects(test->bounds(), next->bounds())) { |
| 3846 | return true; |
| 3847 | } |
| 3848 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3849 | Work wt; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3850 | wt.init(test); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 3851 | bool foundCommonContour = test == next; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3852 | do { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3853 | Work wn; |
| 3854 | wn.init(next); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3855 | if (test == next && !wn.startAfter(wt)) { |
| 3856 | continue; |
| 3857 | } |
| 3858 | do { |
| 3859 | if (!Bounds::Intersects(wt.bounds(), wn.bounds())) { |
| 3860 | continue; |
| 3861 | } |
| 3862 | int pts; |
| 3863 | Intersections ts; |
| 3864 | bool swap = false; |
| 3865 | switch (wt.segmentType()) { |
| 3866 | case Work::kHorizontalLine_Segment: |
| 3867 | swap = true; |
| 3868 | switch (wn.segmentType()) { |
| 3869 | case Work::kHorizontalLine_Segment: |
| 3870 | case Work::kVerticalLine_Segment: |
| 3871 | case Work::kLine_Segment: { |
| 3872 | pts = HLineIntersect(wn.pts(), wt.left(), |
| 3873 | wt.right(), wt.y(), wt.xFlipped(), ts); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3874 | debugShowLineIntersection(pts, wt, wn, |
| 3875 | ts.fT[1], ts.fT[0]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3876 | break; |
| 3877 | } |
| 3878 | case Work::kQuad_Segment: { |
| 3879 | pts = HQuadIntersect(wn.pts(), wt.left(), |
| 3880 | wt.right(), wt.y(), wt.xFlipped(), ts); |
| 3881 | break; |
| 3882 | } |
| 3883 | case Work::kCubic_Segment: { |
| 3884 | pts = HCubicIntersect(wn.pts(), wt.left(), |
| 3885 | wt.right(), wt.y(), wt.xFlipped(), ts); |
| 3886 | break; |
| 3887 | } |
| 3888 | default: |
| 3889 | SkASSERT(0); |
| 3890 | } |
| 3891 | break; |
| 3892 | case Work::kVerticalLine_Segment: |
| 3893 | swap = true; |
| 3894 | switch (wn.segmentType()) { |
| 3895 | case Work::kHorizontalLine_Segment: |
| 3896 | case Work::kVerticalLine_Segment: |
| 3897 | case Work::kLine_Segment: { |
| 3898 | pts = VLineIntersect(wn.pts(), wt.top(), |
| 3899 | wt.bottom(), wt.x(), wt.yFlipped(), ts); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3900 | debugShowLineIntersection(pts, wt, wn, |
| 3901 | ts.fT[1], ts.fT[0]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3902 | break; |
| 3903 | } |
| 3904 | case Work::kQuad_Segment: { |
| 3905 | pts = VQuadIntersect(wn.pts(), wt.top(), |
| 3906 | wt.bottom(), wt.x(), wt.yFlipped(), ts); |
| 3907 | break; |
| 3908 | } |
| 3909 | case Work::kCubic_Segment: { |
| 3910 | pts = VCubicIntersect(wn.pts(), wt.top(), |
| 3911 | wt.bottom(), wt.x(), wt.yFlipped(), ts); |
| 3912 | break; |
| 3913 | } |
| 3914 | default: |
| 3915 | SkASSERT(0); |
| 3916 | } |
| 3917 | break; |
| 3918 | case Work::kLine_Segment: |
| 3919 | switch (wn.segmentType()) { |
| 3920 | case Work::kHorizontalLine_Segment: |
| 3921 | pts = HLineIntersect(wt.pts(), wn.left(), |
| 3922 | wn.right(), wn.y(), wn.xFlipped(), ts); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3923 | debugShowLineIntersection(pts, wt, wn, |
| 3924 | ts.fT[1], ts.fT[0]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3925 | break; |
| 3926 | case Work::kVerticalLine_Segment: |
| 3927 | pts = VLineIntersect(wt.pts(), wn.top(), |
| 3928 | wn.bottom(), wn.x(), wn.yFlipped(), ts); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 3929 | debugShowLineIntersection(pts, wt, wn, |
| 3930 | ts.fT[1], ts.fT[0]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3931 | break; |
| 3932 | case Work::kLine_Segment: { |
| 3933 | pts = LineIntersect(wt.pts(), wn.pts(), ts); |
| 3934 | debugShowLineIntersection(pts, wt, wn, |
| 3935 | ts.fT[1], ts.fT[0]); |
| 3936 | break; |
| 3937 | } |
| 3938 | case Work::kQuad_Segment: { |
| 3939 | swap = true; |
| 3940 | pts = QuadLineIntersect(wn.pts(), wt.pts(), ts); |
| 3941 | break; |
| 3942 | } |
| 3943 | case Work::kCubic_Segment: { |
| 3944 | swap = true; |
| 3945 | pts = CubicLineIntersect(wn.pts(), wt.pts(), ts); |
| 3946 | break; |
| 3947 | } |
| 3948 | default: |
| 3949 | SkASSERT(0); |
| 3950 | } |
| 3951 | break; |
| 3952 | case Work::kQuad_Segment: |
| 3953 | switch (wn.segmentType()) { |
| 3954 | case Work::kHorizontalLine_Segment: |
| 3955 | pts = HQuadIntersect(wt.pts(), wn.left(), |
| 3956 | wn.right(), wn.y(), wn.xFlipped(), ts); |
| 3957 | break; |
| 3958 | case Work::kVerticalLine_Segment: |
| 3959 | pts = VQuadIntersect(wt.pts(), wn.top(), |
| 3960 | wn.bottom(), wn.x(), wn.yFlipped(), ts); |
| 3961 | break; |
| 3962 | case Work::kLine_Segment: { |
| 3963 | pts = QuadLineIntersect(wt.pts(), wn.pts(), ts); |
| 3964 | break; |
| 3965 | } |
| 3966 | case Work::kQuad_Segment: { |
| 3967 | pts = QuadIntersect(wt.pts(), wn.pts(), ts); |
caryclark@google.com | a461ff0 | 2012-10-11 12:54:23 +0000 | [diff] [blame] | 3968 | debugShowQuadIntersection(pts, wt, wn, |
| 3969 | ts.fT[1], ts.fT[0]); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 3970 | break; |
| 3971 | } |
| 3972 | case Work::kCubic_Segment: { |
| 3973 | wt.promoteToCubic(); |
| 3974 | pts = CubicIntersect(wt.cubic(), wn.pts(), ts); |
| 3975 | break; |
| 3976 | } |
| 3977 | default: |
| 3978 | SkASSERT(0); |
| 3979 | } |
| 3980 | break; |
| 3981 | case Work::kCubic_Segment: |
| 3982 | switch (wn.segmentType()) { |
| 3983 | case Work::kHorizontalLine_Segment: |
| 3984 | pts = HCubicIntersect(wt.pts(), wn.left(), |
| 3985 | wn.right(), wn.y(), wn.xFlipped(), ts); |
| 3986 | break; |
| 3987 | case Work::kVerticalLine_Segment: |
| 3988 | pts = VCubicIntersect(wt.pts(), wn.top(), |
| 3989 | wn.bottom(), wn.x(), wn.yFlipped(), ts); |
| 3990 | break; |
| 3991 | case Work::kLine_Segment: { |
| 3992 | pts = CubicLineIntersect(wt.pts(), wn.pts(), ts); |
| 3993 | break; |
| 3994 | } |
| 3995 | case Work::kQuad_Segment: { |
| 3996 | wn.promoteToCubic(); |
| 3997 | pts = CubicIntersect(wt.pts(), wn.cubic(), ts); |
| 3998 | break; |
| 3999 | } |
| 4000 | case Work::kCubic_Segment: { |
| 4001 | pts = CubicIntersect(wt.pts(), wn.pts(), ts); |
| 4002 | break; |
| 4003 | } |
| 4004 | default: |
| 4005 | SkASSERT(0); |
| 4006 | } |
| 4007 | break; |
| 4008 | default: |
| 4009 | SkASSERT(0); |
| 4010 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4011 | if (!foundCommonContour && pts > 0) { |
| 4012 | test->addCross(next); |
| 4013 | next->addCross(test); |
| 4014 | foundCommonContour = true; |
| 4015 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 4016 | // in addition to recording T values, record matching segment |
caryclark@google.com | 32546db | 2012-08-31 20:55:07 +0000 | [diff] [blame] | 4017 | if (pts == 2) { |
| 4018 | if (wn.segmentType() <= Work::kLine_Segment |
| 4019 | && wt.segmentType() <= Work::kLine_Segment) { |
| 4020 | wt.addCoincident(wn, ts, swap); |
| 4021 | continue; |
| 4022 | } |
| 4023 | if (wn.segmentType() == Work::kQuad_Segment |
| 4024 | && wt.segmentType() == Work::kQuad_Segment |
| 4025 | && ts.coincidentUsed() == 2) { |
| 4026 | wt.addCoincident(wn, ts, swap); |
| 4027 | continue; |
| 4028 | } |
| 4029 | |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 4030 | } |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 4031 | int pt2 = 0; |
| 4032 | int pt2inc = 1; |
| 4033 | if (ts.fFlip) { |
| 4034 | pt2 = pts - 1; |
| 4035 | pt2inc = -1; |
| 4036 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 4037 | for (int pt = 0; pt < pts; ++pt) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 4038 | SkASSERT(ts.fT[0][pt] >= 0 && ts.fT[0][pt] <= 1); |
| 4039 | SkASSERT(ts.fT[1][pt] >= 0 && ts.fT[1][pt] <= 1); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4040 | int testTAt = wt.addT(ts.fT[swap][pt], wn); |
| 4041 | int nextTAt = wn.addT(ts.fT[!swap][pt], wt); |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 4042 | wt.addOtherT(testTAt, ts.fT[!swap][pt ^ ts.fFlip], nextTAt); |
| 4043 | wn.addOtherT(nextTAt, ts.fT[swap][pt ^ ts.fFlip], testTAt); |
| 4044 | pt2 += pt2inc; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 4045 | } |
| 4046 | } while (wn.advance()); |
| 4047 | } while (wt.advance()); |
| 4048 | return true; |
| 4049 | } |
| 4050 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4051 | // resolve any coincident pairs found while intersecting, and |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 4052 | // see if coincidence is formed by clipping non-concident segments |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 4053 | static void coincidenceCheck(SkTDArray<Contour*>& contourList) { |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 4054 | int contourCount = contourList.count(); |
caryclark@google.com | f25edfe | 2012-06-01 18:20:10 +0000 | [diff] [blame] | 4055 | for (int cIndex = 0; cIndex < contourCount; ++cIndex) { |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 4056 | Contour* contour = contourList[cIndex]; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 4057 | contour->resolveCoincidence(); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4058 | } |
| 4059 | for (int cIndex = 0; cIndex < contourCount; ++cIndex) { |
| 4060 | Contour* contour = contourList[cIndex]; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 4061 | contour->findTooCloseToCall(); |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 4062 | } |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 4063 | #if 0 |
| 4064 | // OPTIMIZATION: this check could be folded in with findTooClose -- maybe |
| 4065 | for (int cIndex = 0; cIndex < contourCount; ++cIndex) { |
| 4066 | Contour* contour = contourList[cIndex]; |
| 4067 | contour->collapseTriangles(); |
| 4068 | } |
| 4069 | #endif |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 4070 | } |
| 4071 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4072 | // project a ray from the top of the contour up and see if it hits anything |
| 4073 | // note: when we compute line intersections, we keep track of whether |
| 4074 | // two contours touch, so we need only look at contours not touching this one. |
| 4075 | // OPTIMIZATION: sort contourList vertically to avoid linear walk |
| 4076 | static int innerContourCheck(SkTDArray<Contour*>& contourList, |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4077 | const Segment* current, int index, int endIndex) { |
caryclark@google.com | 7db7c6b | 2012-07-27 21:22:25 +0000 | [diff] [blame] | 4078 | const SkPoint& basePt = current->xyAtT(endIndex); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4079 | int contourCount = contourList.count(); |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4080 | SkScalar bestY = SK_ScalarMin; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4081 | const Segment* test = NULL; |
| 4082 | int tIndex; |
| 4083 | double tHit; |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4084 | // bool checkCrosses = true; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4085 | for (int cTest = 0; cTest < contourCount; ++cTest) { |
| 4086 | Contour* contour = contourList[cTest]; |
| 4087 | if (basePt.fY < contour->bounds().fTop) { |
| 4088 | continue; |
| 4089 | } |
| 4090 | if (bestY > contour->bounds().fBottom) { |
| 4091 | continue; |
| 4092 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4093 | #if 0 |
caryclark@google.com | 7db7c6b | 2012-07-27 21:22:25 +0000 | [diff] [blame] | 4094 | // even though the contours crossed, if spans cancel through concidence, |
| 4095 | // the contours may be not have any span links to chase, and the current |
| 4096 | // segment may be isolated. Detect this by seeing if current has |
| 4097 | // uninitialized wind sums. If so, project a ray instead of relying on |
| 4098 | // previously found intersections. |
| 4099 | if (baseContour == contour) { |
| 4100 | continue; |
| 4101 | } |
| 4102 | if (checkCrosses && baseContour->crosses(contour)) { |
| 4103 | if (current->isConnected(index, endIndex)) { |
| 4104 | continue; |
| 4105 | } |
| 4106 | checkCrosses = false; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4107 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4108 | #endif |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4109 | const Segment* next = contour->crossedSegment(basePt, bestY, tIndex, tHit); |
| 4110 | if (next) { |
| 4111 | test = next; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4112 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4113 | } |
| 4114 | if (!test) { |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4115 | return 0; |
| 4116 | } |
| 4117 | int winding, windValue; |
| 4118 | // If the ray hit the end of a span, we need to construct the wheel of |
| 4119 | // angles to find the span closest to the ray -- even if there are just |
| 4120 | // two spokes on the wheel. |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4121 | const Angle* angle = NULL; |
caryclark@google.com | 3350c3c | 2012-08-24 15:24:36 +0000 | [diff] [blame] | 4122 | if (approximately_zero(tHit - test->t(tIndex))) { |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4123 | SkTDArray<Angle> angles; |
| 4124 | int end = test->nextSpan(tIndex, 1); |
| 4125 | if (end < 0) { |
| 4126 | end = test->nextSpan(tIndex, -1); |
| 4127 | } |
| 4128 | test->addTwoAngles(end, tIndex, angles); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 4129 | SkASSERT(angles.count() > 0); |
| 4130 | if (angles[0].segment()->yAtT(angles[0].start()) >= basePt.fY) { |
| 4131 | #if DEBUG_SORT |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 4132 | SkDebugf("%s early return\n", __FUNCTION__); |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 4133 | #endif |
| 4134 | return 0; |
| 4135 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4136 | test->buildAngles(tIndex, angles); |
| 4137 | SkTDArray<Angle*> sorted; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 4138 | // OPTIMIZATION: call a sort that, if base point is the leftmost, |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4139 | // returns the first counterclockwise hour before 6 o'clock, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 4140 | // or if the base point is rightmost, returns the first clockwise |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4141 | // hour after 6 o'clock |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 4142 | (void) Segment::SortAngles(angles, sorted); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4143 | #if DEBUG_SORT |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 4144 | sorted[0]->segment()->debugShowSort(__FUNCTION__, sorted, 0, 0); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4145 | #endif |
| 4146 | // walk the sorted angle fan to find the lowest angle |
| 4147 | // above the base point. Currently, the first angle in the sorted array |
| 4148 | // is 12 noon or an earlier hour (the next counterclockwise) |
| 4149 | int count = sorted.count(); |
| 4150 | int left = -1; |
caryclark@google.com | 7db7c6b | 2012-07-27 21:22:25 +0000 | [diff] [blame] | 4151 | int mid = -1; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4152 | int right = -1; |
caryclark@google.com | 7db7c6b | 2012-07-27 21:22:25 +0000 | [diff] [blame] | 4153 | bool baseMatches = test->yAtT(tIndex) == basePt.fY; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4154 | for (int index = 0; index < count; ++index) { |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 4155 | angle = sorted[index]; |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 4156 | if (angle->unsortable()) { |
| 4157 | continue; |
| 4158 | } |
caryclark@google.com | 7db7c6b | 2012-07-27 21:22:25 +0000 | [diff] [blame] | 4159 | if (baseMatches && angle->isHorizontal()) { |
| 4160 | continue; |
| 4161 | } |
| 4162 | double indexDx = angle->dx(); |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 4163 | test = angle->segment(); |
| 4164 | if (test->verb() > SkPath::kLine_Verb && approximately_zero(indexDx)) { |
| 4165 | const SkPoint* pts = test->pts(); |
| 4166 | indexDx = pts[2].fX - pts[1].fX - indexDx; |
| 4167 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4168 | if (indexDx < 0) { |
| 4169 | left = index; |
| 4170 | } else if (indexDx > 0) { |
| 4171 | right = index; |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 4172 | int previous = index - 1; |
| 4173 | if (previous < 0) { |
| 4174 | previous = count - 1; |
| 4175 | } |
| 4176 | const Angle* prev = sorted[previous]; |
| 4177 | if (prev->dy() >= 0 && prev->dx() > 0 && angle->dy() < 0) { |
| 4178 | #if DEBUG_SORT |
| 4179 | SkDebugf("%s use prev\n", __FUNCTION__); |
| 4180 | #endif |
| 4181 | right = previous; |
| 4182 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4183 | break; |
caryclark@google.com | 7db7c6b | 2012-07-27 21:22:25 +0000 | [diff] [blame] | 4184 | } else { |
| 4185 | mid = index; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4186 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4187 | } |
caryclark@google.com | 7db7c6b | 2012-07-27 21:22:25 +0000 | [diff] [blame] | 4188 | if (left < 0 && right < 0) { |
| 4189 | left = mid; |
| 4190 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4191 | SkASSERT(left >= 0 || right >= 0); |
| 4192 | if (left < 0) { |
| 4193 | left = right; |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4194 | } else if (left >= 0 && mid >= 0 && right >= 0 |
| 4195 | && sorted[mid]->sign() == sorted[right]->sign()) { |
| 4196 | left = right; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4197 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4198 | angle = sorted[left]; |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4199 | test = angle->segment(); |
| 4200 | winding = test->windSum(angle); |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 4201 | SkASSERT(winding != SK_MinS32); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4202 | windValue = test->windValue(angle); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4203 | #if DEBUG_WINDING |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4204 | SkDebugf("%s angle winding=%d windValue=%d sign=%d\n", __FUNCTION__, winding, |
| 4205 | windValue, angle->sign()); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4206 | #endif |
| 4207 | } else { |
| 4208 | winding = test->windSum(tIndex); |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 4209 | SkASSERT(winding != SK_MinS32); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4210 | windValue = test->windValue(tIndex); |
| 4211 | #if DEBUG_WINDING |
| 4212 | SkDebugf("%s single winding=%d windValue=%d\n", __FUNCTION__, winding, |
| 4213 | windValue); |
| 4214 | #endif |
| 4215 | } |
| 4216 | // see if a + change in T results in a +/- change in X (compute x'(T)) |
| 4217 | SkScalar dx = (*SegmentDXAtT[test->verb()])(test->pts(), tHit); |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 4218 | if (test->verb() > SkPath::kLine_Verb && approximately_zero(dx)) { |
| 4219 | const SkPoint* pts = test->pts(); |
| 4220 | dx = pts[2].fX - pts[1].fX - dx; |
| 4221 | } |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4222 | #if DEBUG_WINDING |
| 4223 | SkDebugf("%s dx=%1.9g\n", __FUNCTION__, dx); |
| 4224 | #endif |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 4225 | SkASSERT(dx != 0); |
| 4226 | if (winding * dx > 0) { // if same signs, result is negative |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4227 | winding += dx > 0 ? -windValue : windValue; |
| 4228 | #if DEBUG_WINDING |
| 4229 | SkDebugf("%s final winding=%d\n", __FUNCTION__, winding); |
| 4230 | #endif |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4231 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4232 | return winding; |
| 4233 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 4234 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 4235 | // OPTIMIZATION: not crazy about linear search here to find top active y. |
| 4236 | // seems like we should break down and do the sort, or maybe sort each |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 4237 | // contours' segments? |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 4238 | // Once the segment array is built, there's no reason I can think of not to |
| 4239 | // sort it in Y. hmmm |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4240 | // FIXME: return the contour found to pass to inner contour check |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4241 | static Segment* findTopContour(SkTDArray<Contour*>& contourList) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4242 | int contourCount = contourList.count(); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 4243 | int cIndex = 0; |
| 4244 | Segment* topStart; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4245 | SkScalar bestY = SK_ScalarMax; |
| 4246 | Contour* contour; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 4247 | do { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4248 | contour = contourList[cIndex]; |
| 4249 | topStart = contour->topSegment(bestY); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 4250 | } while (!topStart && ++cIndex < contourCount); |
| 4251 | if (!topStart) { |
| 4252 | return NULL; |
| 4253 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4254 | while (++cIndex < contourCount) { |
| 4255 | contour = contourList[cIndex]; |
| 4256 | if (bestY < contour->bounds().fTop) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 4257 | continue; |
| 4258 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4259 | SkScalar testY = SK_ScalarMax; |
| 4260 | Segment* test = contour->topSegment(testY); |
| 4261 | if (!test || bestY <= testY) { |
| 4262 | continue; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 4263 | } |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4264 | topStart = test; |
| 4265 | bestY = testY; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 4266 | } |
| 4267 | return topStart; |
| 4268 | } |
| 4269 | |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 4270 | static Segment* findUndone(SkTDArray<Contour*>& contourList, int& start, int& end) { |
| 4271 | int contourCount = contourList.count(); |
| 4272 | Segment* result; |
| 4273 | for (int cIndex = 0; cIndex < contourCount; ++cIndex) { |
| 4274 | Contour* contour = contourList[cIndex]; |
| 4275 | result = contour->undoneSegment(start, end); |
| 4276 | if (result) { |
| 4277 | return result; |
| 4278 | } |
| 4279 | } |
| 4280 | return NULL; |
| 4281 | } |
| 4282 | |
| 4283 | |
| 4284 | |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 4285 | static Segment* findChase(SkTDArray<Span*>& chase, int& tIndex, int& endIndex, |
| 4286 | int contourWinding) { |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 4287 | while (chase.count()) { |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 4288 | Span* span = chase[chase.count() - 1]; |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 4289 | const Span& backPtr = span->fOther->span(span->fOtherIndex); |
| 4290 | Segment* segment = backPtr.fOther; |
| 4291 | tIndex = backPtr.fOtherIndex; |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 4292 | SkTDArray<Angle> angles; |
| 4293 | int done = 0; |
| 4294 | if (segment->activeAngle(tIndex, done, angles)) { |
| 4295 | Angle* last = angles.end() - 1; |
| 4296 | tIndex = last->start(); |
| 4297 | endIndex = last->end(); |
| 4298 | return last->segment(); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 4299 | } |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 4300 | if (done == angles.count()) { |
| 4301 | chase.pop(&span); |
| 4302 | continue; |
| 4303 | } |
| 4304 | SkTDArray<Angle*> sorted; |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 4305 | bool sortable = Segment::SortAngles(angles, sorted); |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 4306 | #if DEBUG_SORT |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 4307 | sorted[0]->segment()->debugShowSort(__FUNCTION__, sorted, 0, 0); |
caryclark@google.com | 03f9706 | 2012-08-21 13:13:52 +0000 | [diff] [blame] | 4308 | #endif |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 4309 | if (!sortable) { |
| 4310 | chase.pop(&span); |
| 4311 | continue; |
| 4312 | } |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 4313 | // find first angle, initialize winding to computed fWindSum |
| 4314 | int firstIndex = -1; |
| 4315 | const Angle* angle; |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4316 | int winding; |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 4317 | do { |
| 4318 | angle = sorted[++firstIndex]; |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4319 | segment = angle->segment(); |
| 4320 | winding = segment->windSum(angle); |
| 4321 | } while (winding == SK_MinS32); |
| 4322 | int spanWinding = segment->spanSign(angle->start(), angle->end()); |
| 4323 | #if DEBUG_WINDING |
| 4324 | SkDebugf("%s winding=%d spanWinding=%d contourWinding=%d\n", |
| 4325 | __FUNCTION__, winding, spanWinding, contourWinding); |
caryclark@google.com | 4758069 | 2012-07-23 12:14:49 +0000 | [diff] [blame] | 4326 | #endif |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4327 | // turn swinding into contourWinding |
| 4328 | if (spanWinding * winding < 0) { |
| 4329 | winding += spanWinding; |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 4330 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4331 | #if DEBUG_SORT |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 4332 | segment->debugShowSort(__FUNCTION__, sorted, firstIndex, winding); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4333 | #endif |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 4334 | // we care about first sign and whether wind sum indicates this |
| 4335 | // edge is inside or outside. Maybe need to pass span winding |
| 4336 | // or first winding or something into this function? |
| 4337 | // advance to first undone angle, then return it and winding |
| 4338 | // (to set whether edges are active or not) |
| 4339 | int nextIndex = firstIndex + 1; |
| 4340 | int angleCount = sorted.count(); |
| 4341 | int lastIndex = firstIndex != 0 ? firstIndex : angleCount; |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4342 | angle = sorted[firstIndex]; |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 4343 | winding -= angle->segment()->spanSign(angle); |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 4344 | do { |
| 4345 | SkASSERT(nextIndex != firstIndex); |
| 4346 | if (nextIndex == angleCount) { |
| 4347 | nextIndex = 0; |
| 4348 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4349 | angle = sorted[nextIndex]; |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 4350 | segment = angle->segment(); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4351 | int maxWinding = winding; |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 4352 | winding -= segment->spanSign(angle); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4353 | #if DEBUG_SORT |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 4354 | SkDebugf("%s id=%d maxWinding=%d winding=%d sign=%d\n", __FUNCTION__, |
| 4355 | segment->debugID(), maxWinding, winding, angle->sign()); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4356 | #endif |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 4357 | tIndex = angle->start(); |
| 4358 | endIndex = angle->end(); |
| 4359 | int lesser = SkMin32(tIndex, endIndex); |
| 4360 | const Span& nextSpan = segment->span(lesser); |
| 4361 | if (!nextSpan.fDone) { |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4362 | #if 1 |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 4363 | // FIXME: this be wrong. assign startWinding if edge is in |
| 4364 | // same direction. If the direction is opposite, winding to |
| 4365 | // assign is flipped sign or +/- 1? |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 4366 | if (useInnerWinding(maxWinding, winding)) { |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4367 | maxWinding = winding; |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 4368 | } |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 4369 | segment->markWinding(lesser, maxWinding); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4370 | #endif |
caryclark@google.com | 9764cc6 | 2012-07-12 19:29:45 +0000 | [diff] [blame] | 4371 | break; |
| 4372 | } |
| 4373 | } while (++nextIndex != lastIndex); |
| 4374 | return segment; |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 4375 | } |
| 4376 | return NULL; |
| 4377 | } |
| 4378 | |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 4379 | #if DEBUG_ACTIVE_SPANS |
| 4380 | static void debugShowActiveSpans(SkTDArray<Contour*>& contourList) { |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 4381 | int index; |
| 4382 | for (index = 0; index < contourList.count(); ++ index) { |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 4383 | contourList[index]->debugShowActiveSpans(); |
| 4384 | } |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 4385 | for (index = 0; index < contourList.count(); ++ index) { |
| 4386 | contourList[index]->validateActiveSpans(); |
| 4387 | } |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 4388 | } |
| 4389 | #endif |
| 4390 | |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 4391 | static bool windingIsActive(int winding, int spanWinding) { |
| 4392 | return winding * spanWinding <= 0 && abs(winding) <= abs(spanWinding) |
| 4393 | && (!winding || !spanWinding || winding == -spanWinding); |
| 4394 | } |
| 4395 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 4396 | // Each segment may have an inside or an outside. Segments contained within |
| 4397 | // winding may have insides on either side, and form a contour that should be |
| 4398 | // ignored. Segments that are coincident with opposing direction segments may |
| 4399 | // have outsides on either side, and should also disappear. |
| 4400 | // 'Normal' segments will have one inside and one outside. Subsequent connections |
| 4401 | // when winding should follow the intersection direction. If more than one edge |
| 4402 | // is an option, choose first edge that continues the inside. |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 4403 | // since we start with leftmost top edge, we'll traverse through a |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 4404 | // smaller angle counterclockwise to get to the next edge. |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 4405 | // returns true if all edges were processed |
| 4406 | static bool bridgeWinding(SkTDArray<Contour*>& contourList, SkPath& simple) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4407 | bool firstContour = true; |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 4408 | bool unsortable = false; |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 4409 | do { |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4410 | Segment* topStart = findTopContour(contourList); |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 4411 | if (!topStart) { |
| 4412 | break; |
caryclark@google.com | cc90505 | 2012-07-25 20:59:42 +0000 | [diff] [blame] | 4413 | } |
caryclark@google.com | 15fa138 | 2012-05-07 20:49:36 +0000 | [diff] [blame] | 4414 | // 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] | 4415 | // follow edges to intersection by changing the index by direction. |
| 4416 | int index, endIndex; |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 4417 | Segment* current = topStart->findTop(index, endIndex); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 4418 | int contourWinding; |
| 4419 | if (firstContour) { |
| 4420 | contourWinding = 0; |
| 4421 | firstContour = false; |
| 4422 | } else { |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 4423 | int sumWinding = current->windSum(SkMin32(index, endIndex)); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4424 | // FIXME: don't I have to adjust windSum to get contourWinding? |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 4425 | if (sumWinding == SK_MinS32) { |
| 4426 | sumWinding = current->computeSum(index, endIndex); |
| 4427 | } |
| 4428 | if (sumWinding == SK_MinS32) { |
| 4429 | contourWinding = innerContourCheck(contourList, current, |
| 4430 | index, endIndex); |
| 4431 | } else { |
| 4432 | contourWinding = sumWinding; |
| 4433 | int spanWinding = current->spanSign(index, endIndex); |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 4434 | bool inner = useInnerWinding(sumWinding - spanWinding, sumWinding); |
| 4435 | if (inner) { |
caryclark@google.com | 200c211 | 2012-08-03 15:05:04 +0000 | [diff] [blame] | 4436 | contourWinding -= spanWinding; |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4437 | } |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 4438 | #if DEBUG_WINDING |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 4439 | SkDebugf("%s sumWinding=%d spanWinding=%d sign=%d inner=%d result=%d\n", __FUNCTION__, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 4440 | sumWinding, spanWinding, SkSign32(index - endIndex), |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 4441 | inner, contourWinding); |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 4442 | #endif |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4443 | } |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 4444 | #if DEBUG_WINDING |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4445 | // SkASSERT(current->debugVerifyWinding(index, endIndex, contourWinding)); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 4446 | SkDebugf("%s contourWinding=%d\n", __FUNCTION__, contourWinding); |
| 4447 | #endif |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4448 | } |
caryclark@google.com | 88f7d0c | 2012-06-07 21:09:20 +0000 | [diff] [blame] | 4449 | SkPoint lastPt; |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 4450 | int winding = contourWinding; |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 4451 | int spanWinding = current->spanSign(index, endIndex); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 4452 | // FIXME: needs work. While it works in limited situations, it does |
| 4453 | // not always compute winding correctly. Active should be removed and instead |
| 4454 | // the initial winding should be correctly passed in so that if the |
| 4455 | // inner contour is wound the same way, it never finds an accumulated |
| 4456 | // winding of zero. Inside 'find next', we need to look for transitions |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 4457 | // other than zero when resolving sorted angles. |
caryclark@google.com | 27c449a | 2012-07-27 18:26:38 +0000 | [diff] [blame] | 4458 | bool active = windingIsActive(winding, spanWinding); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 4459 | SkTDArray<Span*> chaseArray; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 4460 | do { |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 4461 | #if DEBUG_WINDING |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 4462 | SkDebugf("%s active=%s winding=%d spanWinding=%d\n", |
| 4463 | __FUNCTION__, active ? "true" : "false", |
| 4464 | winding, spanWinding); |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 4465 | #endif |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 4466 | const SkPoint* firstPt = NULL; |
| 4467 | do { |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 4468 | SkASSERT(unsortable || !current->done()); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 4469 | int nextStart = index; |
| 4470 | int nextEnd = endIndex; |
| 4471 | Segment* next = current->findNextWinding(chaseArray, active, |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 4472 | nextStart, nextEnd, winding, spanWinding, unsortable); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 4473 | if (!next) { |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 4474 | if (active && firstPt && current->verb() != SkPath::kLine_Verb && *firstPt != lastPt) { |
| 4475 | lastPt = current->addCurveTo(index, endIndex, simple, true); |
| 4476 | SkASSERT(*firstPt == lastPt); |
| 4477 | } |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 4478 | break; |
| 4479 | } |
| 4480 | if (!firstPt) { |
| 4481 | firstPt = ¤t->addMoveTo(index, simple, active); |
| 4482 | } |
| 4483 | lastPt = current->addCurveTo(index, endIndex, simple, active); |
| 4484 | current = next; |
| 4485 | index = nextStart; |
| 4486 | endIndex = nextEnd; |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 4487 | } while (*firstPt != lastPt && (active || !current->done())); |
| 4488 | if (firstPt && active) { |
| 4489 | #if DEBUG_PATH_CONSTRUCTION |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 4490 | SkDebugf("path.close();\n"); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 4491 | #endif |
| 4492 | simple.close(); |
| 4493 | } |
caryclark@google.com | e21cb18 | 2012-07-23 21:26:31 +0000 | [diff] [blame] | 4494 | current = findChase(chaseArray, index, endIndex, contourWinding); |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 4495 | #if DEBUG_ACTIVE_SPANS |
caryclark@google.com | 027de22 | 2012-07-12 12:52:50 +0000 | [diff] [blame] | 4496 | debugShowActiveSpans(contourList); |
caryclark@google.com | 0e08a19 | 2012-07-13 21:07:52 +0000 | [diff] [blame] | 4497 | #endif |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 4498 | if (!current) { |
caryclark@google.com | 495f8e4 | 2012-05-31 13:13:11 +0000 | [diff] [blame] | 4499 | break; |
| 4500 | } |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 4501 | int lesser = SkMin32(index, endIndex); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4502 | spanWinding = current->spanSign(index, endIndex); |
| 4503 | winding = current->windSum(lesser); |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 4504 | bool inner = useInnerWinding(winding - spanWinding, winding); |
| 4505 | #if DEBUG_WINDING |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 4506 | SkDebugf("%s id=%d t=%1.9g spanWinding=%d winding=%d sign=%d" |
caryclark@google.com | 59823f7 | 2012-08-09 18:17:47 +0000 | [diff] [blame] | 4507 | " inner=%d result=%d\n", |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 4508 | __FUNCTION__, current->debugID(), current->t(lesser), |
| 4509 | spanWinding, winding, SkSign32(index - endIndex), |
| 4510 | useInnerWinding(winding - spanWinding, winding), |
caryclark@google.com | 2ddff93 | 2012-08-07 21:25:27 +0000 | [diff] [blame] | 4511 | inner ? winding - spanWinding : winding); |
| 4512 | #endif |
| 4513 | if (inner) { |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 4514 | winding -= spanWinding; |
| 4515 | } |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 4516 | active = windingIsActive(winding, spanWinding); |
caryclark@google.com | fa4a6e9 | 2012-07-11 17:52:32 +0000 | [diff] [blame] | 4517 | } while (true); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 4518 | } while (true); |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 4519 | return !unsortable; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 4520 | } |
| 4521 | |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 4522 | // returns true if all edges were processed |
| 4523 | static bool bridgeXor(SkTDArray<Contour*>& contourList, SkPath& simple) { |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 4524 | Segment* current; |
| 4525 | int start, end; |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 4526 | bool unsortable = false; |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 4527 | while ((current = findUndone(contourList, start, end))) { |
| 4528 | const SkPoint* firstPt = NULL; |
| 4529 | SkPoint lastPt; |
| 4530 | do { |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 4531 | SkASSERT(unsortable || !current->done()); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 4532 | int nextStart = start; |
| 4533 | int nextEnd = end; |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 4534 | Segment* next = current->findNextXor(nextStart, nextEnd, unsortable); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 4535 | if (!next) { |
caryclark@google.com | c899ad9 | 2012-08-23 15:24:42 +0000 | [diff] [blame] | 4536 | if (firstPt && current->verb() != SkPath::kLine_Verb && *firstPt != lastPt) { |
| 4537 | lastPt = current->addCurveTo(start, end, simple, true); |
| 4538 | SkASSERT(*firstPt == lastPt); |
| 4539 | } |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 4540 | break; |
| 4541 | } |
| 4542 | if (!firstPt) { |
| 4543 | firstPt = ¤t->addMoveTo(start, simple, true); |
| 4544 | } |
| 4545 | lastPt = current->addCurveTo(start, end, simple, true); |
| 4546 | current = next; |
| 4547 | start = nextStart; |
| 4548 | end = nextEnd; |
| 4549 | } while (*firstPt != lastPt); |
| 4550 | if (firstPt) { |
| 4551 | #if DEBUG_PATH_CONSTRUCTION |
| 4552 | SkDebugf("%s close\n", __FUNCTION__); |
| 4553 | #endif |
| 4554 | simple.close(); |
| 4555 | } |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 4556 | #if DEBUG_ACTIVE_SPANS |
| 4557 | debugShowActiveSpans(contourList); |
| 4558 | #endif |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 4559 | } |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 4560 | return !unsortable; |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 4561 | } |
| 4562 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 4563 | static void fixOtherTIndex(SkTDArray<Contour*>& contourList) { |
| 4564 | int contourCount = contourList.count(); |
| 4565 | for (int cTest = 0; cTest < contourCount; ++cTest) { |
| 4566 | Contour* contour = contourList[cTest]; |
| 4567 | contour->fixOtherTIndex(); |
| 4568 | } |
| 4569 | } |
| 4570 | |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 4571 | static void makeContourList(SkTArray<Contour>& contours, |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 4572 | SkTDArray<Contour*>& list) { |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 4573 | int count = contours.count(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 4574 | if (count == 0) { |
| 4575 | return; |
| 4576 | } |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 4577 | for (int index = 0; index < count; ++index) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 4578 | *list.append() = &contours[index]; |
| 4579 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 4580 | QSort<Contour>(list.begin(), list.end() - 1); |
| 4581 | } |
| 4582 | |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 4583 | static void assemble(SkPath& simple) { |
| 4584 | // TODO: find the non-closed paths and connect them together |
| 4585 | SkASSERT(0); |
| 4586 | } |
| 4587 | |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 4588 | void simplifyx(const SkPath& path, SkPath& simple) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 4589 | // returns 1 for evenodd, -1 for winding, regardless of inverse-ness |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 4590 | simple.reset(); |
| 4591 | simple.setFillType(SkPath::kEvenOdd_FillType); |
| 4592 | |
| 4593 | // turn path into list of segments |
| 4594 | SkTArray<Contour> contours; |
| 4595 | // FIXME: add self-intersecting cubics' T values to segment |
| 4596 | EdgeBuilder builder(path, contours); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 4597 | builder.finish(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 4598 | SkTDArray<Contour*> contourList; |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 4599 | makeContourList(contours, contourList); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 4600 | Contour** currentPtr = contourList.begin(); |
| 4601 | if (!currentPtr) { |
| 4602 | return; |
| 4603 | } |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 4604 | Contour** listEnd = contourList.end(); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 4605 | // find all intersections between segments |
| 4606 | do { |
| 4607 | Contour** nextPtr = currentPtr; |
| 4608 | Contour* current = *currentPtr++; |
| 4609 | Contour* next; |
| 4610 | do { |
| 4611 | next = *nextPtr++; |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 4612 | } while (addIntersectTs(current, next) && nextPtr != listEnd); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 4613 | } while (currentPtr != listEnd); |
caryclark@google.com | a833b5c | 2012-04-30 19:38:50 +0000 | [diff] [blame] | 4614 | // eat through coincident edges |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 4615 | coincidenceCheck(contourList); |
caryclark@google.com | 66ca2fb | 2012-07-03 14:30:08 +0000 | [diff] [blame] | 4616 | fixOtherTIndex(contourList); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 4617 | // construct closed contours |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 4618 | if (builder.xorMask() == kWinding_Mask |
| 4619 | ? !bridgeWinding(contourList, simple) |
skia.committer@gmail.com | 20c301b | 2012-10-17 02:01:13 +0000 | [diff] [blame^] | 4620 | : !bridgeXor(contourList, simple)) |
caryclark@google.com | c91dfe4 | 2012-10-16 12:06:27 +0000 | [diff] [blame] | 4621 | { // if some edges could not be resolved, assemble remaining fragments |
| 4622 | assemble(simple); |
caryclark@google.com | 24bec79 | 2012-08-20 12:43:57 +0000 | [diff] [blame] | 4623 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 4624 | } |
| 4625 | |