caryclark@google.com | c682590 | 2012-02-03 22:07:47 +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 | */ |
| 7 | |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 8 | #include "CurveIntersection.h" |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 9 | #include "Intersections.h" |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 10 | #include "LineIntersection.h" |
| 11 | #include "SkPath.h" |
| 12 | #include "SkRect.h" |
| 13 | #include "SkTArray.h" |
| 14 | #include "SkTDArray.h" |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 15 | #include "ShapeOps.h" |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 16 | #include "TSearch.h" |
| 17 | |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 18 | #undef SkASSERT |
| 19 | #define SkASSERT(cond) while (!(cond)) { sk_throw(); } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 20 | |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 21 | // FIXME: remove once debugging is complete |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 22 | #if 01 // set to 1 for no debugging whatsoever |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 23 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 24 | const bool gRunTestsInOneThread = false; |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 25 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 26 | #define DEBUG_ACTIVE_LESS_THAN 0 |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 27 | #define DEBUG_ADD 0 |
| 28 | #define DEBUG_ADD_BOTTOM_TS 0 |
| 29 | #define DEBUG_ADD_INTERSECTING_TS 0 |
| 30 | #define DEBUG_ADJUST_COINCIDENT 0 |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 31 | #define DEBUG_ASSEMBLE 0 |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 32 | #define DEBUG_BOTTOM 0 |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 33 | #define DEBUG_BRIDGE 0 |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 34 | #define DEBUG_DUMP 0 |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 35 | #define DEBUG_SORT_HORIZONTAL 0 |
| 36 | #define DEBUG_OUT 0 |
| 37 | #define DEBUG_OUT_LESS_THAN 0 |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 38 | #define DEBUG_SPLIT 0 |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 39 | #define DEBUG_STITCH_EDGE 0 |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 40 | #define DEBUG_TRIM_LINE 0 |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 41 | |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 42 | #else |
| 43 | |
| 44 | const bool gRunTestsInOneThread = true; |
| 45 | |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 46 | #define DEBUG_ACTIVE_LESS_THAN 0 |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 47 | #define DEBUG_ADD 01 |
| 48 | #define DEBUG_ADD_BOTTOM_TS 0 |
| 49 | #define DEBUG_ADD_INTERSECTING_TS 0 |
| 50 | #define DEBUG_ADJUST_COINCIDENT 1 |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 51 | #define DEBUG_ASSEMBLE 1 |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 52 | #define DEBUG_BOTTOM 0 |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 53 | #define DEBUG_BRIDGE 1 |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 54 | #define DEBUG_DUMP 1 |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 55 | #define DEBUG_SORT_HORIZONTAL 01 |
| 56 | #define DEBUG_OUT 01 |
| 57 | #define DEBUG_OUT_LESS_THAN 0 |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 58 | #define DEBUG_SPLIT 1 |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 59 | #define DEBUG_STITCH_EDGE 1 |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 60 | #define DEBUG_TRIM_LINE 1 |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 61 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 62 | #endif |
| 63 | |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 64 | #if DEBUG_ASSEMBLE || DEBUG_BRIDGE |
| 65 | static const char* kLVerbStr[] = {"", "line", "quad", "cubic"}; |
| 66 | #endif |
| 67 | #if DEBUG_STITCH_EDGE |
| 68 | static const char* kUVerbStr[] = {"", "Line", "Quad", "Cubic"}; |
| 69 | #endif |
| 70 | |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 71 | static int LineIntersect(const SkPoint a[2], const SkPoint b[2], |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 72 | Intersections& intersections) { |
| 73 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
| 74 | const _Line bLine = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}}; |
| 75 | return intersect(aLine, bLine, intersections.fT[0], intersections.fT[1]); |
| 76 | } |
| 77 | |
| 78 | static int QuadLineIntersect(const SkPoint a[3], const SkPoint b[2], |
| 79 | Intersections& intersections) { |
| 80 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}}; |
| 81 | const _Line bLine = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}}; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 82 | intersect(aQuad, bLine, intersections); |
| 83 | return intersections.fUsed; |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static int CubicLineIntersect(const SkPoint a[2], const SkPoint b[3], |
| 87 | Intersections& intersections) { |
| 88 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}, |
| 89 | {a[3].fX, a[3].fY}}; |
| 90 | const _Line bLine = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}}; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 91 | return intersect(aCubic, bLine, intersections.fT[0], intersections.fT[1]); |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static int QuadIntersect(const SkPoint a[3], const SkPoint b[3], |
| 95 | Intersections& intersections) { |
| 96 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}}; |
| 97 | const Quadratic bQuad = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}, {b[2].fX, b[2].fY}}; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 98 | intersect(aQuad, bQuad, intersections); |
| 99 | return intersections.fUsed; |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static int CubicIntersect(const SkPoint a[4], const SkPoint b[4], |
| 103 | Intersections& intersections) { |
| 104 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}, |
| 105 | {a[3].fX, a[3].fY}}; |
| 106 | const Cubic bCubic = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}, {b[2].fX, b[2].fY}, |
| 107 | {b[3].fX, b[3].fY}}; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 108 | intersect(aCubic, bCubic, intersections); |
| 109 | return intersections.fUsed; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 110 | } |
| 111 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 112 | static int LineIntersect(const SkPoint a[2], SkScalar left, SkScalar right, |
| 113 | SkScalar y, double aRange[2]) { |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 114 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 115 | return horizontalLineIntersect(aLine, left, right, y, aRange); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 116 | } |
| 117 | |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 118 | static int QuadIntersect(const SkPoint a[3], SkScalar left, SkScalar right, |
| 119 | SkScalar y, double aRange[3]) { |
| 120 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}}; |
| 121 | return horizontalIntersect(aQuad, left, right, y, aRange); |
| 122 | } |
| 123 | |
| 124 | static int CubicIntersect(const SkPoint a[4], SkScalar left, SkScalar right, |
| 125 | SkScalar y, double aRange[4]) { |
| 126 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}, |
| 127 | {a[3].fX, a[3].fY}}; |
| 128 | return horizontalIntersect(aCubic, left, right, y, aRange); |
| 129 | } |
| 130 | |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 131 | static void LineXYAtT(const SkPoint a[2], double t, SkPoint* out) { |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 132 | const _Line line = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 133 | double x, y; |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 134 | xy_at_t(line, t, x, y); |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 135 | out->fX = SkDoubleToScalar(x); |
| 136 | out->fY = SkDoubleToScalar(y); |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 137 | } |
| 138 | |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 139 | static void QuadXYAtT(const SkPoint a[3], double t, SkPoint* out) { |
| 140 | const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}}; |
| 141 | double x, y; |
| 142 | xy_at_t(quad, t, x, y); |
| 143 | out->fX = SkDoubleToScalar(x); |
| 144 | out->fY = SkDoubleToScalar(y); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 145 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 146 | |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 147 | static void CubicXYAtT(const SkPoint a[4], double t, SkPoint* out) { |
| 148 | const Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}, |
| 149 | {a[3].fX, a[3].fY}}; |
| 150 | double x, y; |
| 151 | xy_at_t(cubic, t, x, y); |
| 152 | out->fX = SkDoubleToScalar(x); |
| 153 | out->fY = SkDoubleToScalar(y); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 154 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 155 | |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 156 | static SkScalar LineYAtT(const SkPoint a[2], double t) { |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 157 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 158 | double y; |
| 159 | xy_at_t(aLine, t, *(double*) 0, y); |
| 160 | return SkDoubleToScalar(y); |
| 161 | } |
| 162 | |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 163 | static SkScalar QuadYAtT(const SkPoint a[3], double t) { |
| 164 | const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}}; |
| 165 | double y; |
| 166 | xy_at_t(quad, t, *(double*) 0, y); |
| 167 | return SkDoubleToScalar(y); |
| 168 | } |
| 169 | |
| 170 | static SkScalar CubicYAtT(const SkPoint a[4], double t) { |
| 171 | const Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}, |
| 172 | {a[3].fX, a[3].fY}}; |
| 173 | double y; |
| 174 | xy_at_t(cubic, t, *(double*) 0, y); |
| 175 | return SkDoubleToScalar(y); |
| 176 | } |
| 177 | |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 178 | static void LineSubDivide(const SkPoint a[2], double startT, double endT, |
| 179 | SkPoint sub[2]) { |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 180 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 181 | _Line dst; |
| 182 | sub_divide(aLine, startT, endT, dst); |
| 183 | sub[0].fX = SkDoubleToScalar(dst[0].x); |
| 184 | sub[0].fY = SkDoubleToScalar(dst[0].y); |
| 185 | sub[1].fX = SkDoubleToScalar(dst[1].x); |
| 186 | sub[1].fY = SkDoubleToScalar(dst[1].y); |
| 187 | } |
| 188 | |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 189 | static void QuadSubDivide(const SkPoint a[3], double startT, double endT, |
| 190 | SkPoint sub[3]) { |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 191 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 192 | {a[2].fX, a[2].fY}}; |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 193 | Quadratic dst; |
| 194 | sub_divide(aQuad, startT, endT, dst); |
| 195 | sub[0].fX = SkDoubleToScalar(dst[0].x); |
| 196 | sub[0].fY = SkDoubleToScalar(dst[0].y); |
| 197 | sub[1].fX = SkDoubleToScalar(dst[1].x); |
| 198 | sub[1].fY = SkDoubleToScalar(dst[1].y); |
| 199 | sub[2].fX = SkDoubleToScalar(dst[2].x); |
| 200 | sub[2].fY = SkDoubleToScalar(dst[2].y); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 201 | } |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 202 | |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 203 | static void CubicSubDivide(const SkPoint a[4], double startT, double endT, |
| 204 | SkPoint sub[4]) { |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 205 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 206 | {a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}}; |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 207 | Cubic dst; |
| 208 | sub_divide(aCubic, startT, endT, dst); |
| 209 | sub[0].fX = SkDoubleToScalar(dst[0].x); |
| 210 | sub[0].fY = SkDoubleToScalar(dst[0].y); |
| 211 | sub[1].fX = SkDoubleToScalar(dst[1].x); |
| 212 | sub[1].fY = SkDoubleToScalar(dst[1].y); |
| 213 | sub[2].fX = SkDoubleToScalar(dst[2].x); |
| 214 | sub[2].fY = SkDoubleToScalar(dst[2].y); |
| 215 | sub[3].fX = SkDoubleToScalar(dst[3].x); |
| 216 | sub[3].fY = SkDoubleToScalar(dst[3].y); |
| 217 | } |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 218 | |
| 219 | static void QuadSubBounds(const SkPoint a[3], double startT, double endT, |
| 220 | SkRect& bounds) { |
| 221 | SkPoint dst[3]; |
| 222 | QuadSubDivide(a, startT, endT, dst); |
| 223 | bounds.fLeft = bounds.fRight = dst[0].fX; |
| 224 | bounds.fTop = bounds.fBottom = dst[0].fY; |
| 225 | for (int index = 1; index < 3; ++index) { |
| 226 | bounds.growToInclude(dst[index].fX, dst[index].fY); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | static void CubicSubBounds(const SkPoint a[4], double startT, double endT, |
| 231 | SkRect& bounds) { |
| 232 | SkPoint dst[4]; |
| 233 | CubicSubDivide(a, startT, endT, dst); |
| 234 | bounds.fLeft = bounds.fRight = dst[0].fX; |
| 235 | bounds.fTop = bounds.fBottom = dst[0].fY; |
| 236 | for (int index = 1; index < 4; ++index) { |
| 237 | bounds.growToInclude(dst[index].fX, dst[index].fY); |
| 238 | } |
| 239 | } |
| 240 | |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 241 | static SkPath::Verb QuadReduceOrder(SkPoint a[4]) { |
| 242 | const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 243 | {a[2].fX, a[2].fY}}; |
| 244 | Quadratic dst; |
| 245 | int order = reduceOrder(aQuad, dst); |
| 246 | for (int index = 0; index < order; ++index) { |
| 247 | a[index].fX = SkDoubleToScalar(dst[index].x); |
| 248 | a[index].fY = SkDoubleToScalar(dst[index].y); |
| 249 | } |
| 250 | if (order == 1) { // FIXME: allow returning points, caller should discard |
| 251 | a[1] = a[0]; |
| 252 | return (SkPath::Verb) order; |
| 253 | } |
| 254 | return (SkPath::Verb) (order - 1); |
| 255 | } |
| 256 | |
| 257 | static SkPath::Verb CubicReduceOrder(SkPoint a[4]) { |
| 258 | const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, |
| 259 | {a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}}; |
| 260 | Cubic dst; |
| 261 | int order = reduceOrder(aCubic, dst, kReduceOrder_QuadraticsAllowed); |
| 262 | for (int index = 0; index < order; ++index) { |
| 263 | a[index].fX = SkDoubleToScalar(dst[index].x); |
| 264 | a[index].fY = SkDoubleToScalar(dst[index].y); |
| 265 | } |
| 266 | if (order == 1) { // FIXME: allow returning points, caller should discard |
| 267 | a[1] = a[0]; |
| 268 | return (SkPath::Verb) order; |
| 269 | } |
| 270 | return (SkPath::Verb) (order - 1); |
| 271 | } |
| 272 | |
| 273 | static bool IsCoincident(const SkPoint a[2], const SkPoint& above, |
| 274 | const SkPoint& below) { |
| 275 | const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}}; |
| 276 | const _Line bLine = {{above.fX, above.fY}, {below.fX, below.fY}}; |
| 277 | return implicit_matches_ulps(aLine, bLine, 32); |
| 278 | } |
| 279 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 280 | /* |
| 281 | list of edges |
| 282 | bounds for edge |
| 283 | sort |
| 284 | active T |
| 285 | |
| 286 | if a contour's bounds is outside of the active area, no need to create edges |
| 287 | */ |
| 288 | |
| 289 | /* given one or more paths, |
| 290 | find the bounds of each contour, select the active contours |
| 291 | for each active contour, compute a set of edges |
| 292 | each edge corresponds to one or more lines and curves |
| 293 | leave edges unbroken as long as possible |
| 294 | when breaking edges, compute the t at the break but leave the control points alone |
| 295 | |
| 296 | */ |
| 297 | |
| 298 | void contourBounds(const SkPath& path, SkTDArray<SkRect>& boundsArray) { |
| 299 | SkPath::Iter iter(path, false); |
| 300 | SkPoint pts[4]; |
| 301 | SkPath::Verb verb; |
| 302 | SkRect bounds; |
| 303 | bounds.setEmpty(); |
| 304 | int count = 0; |
| 305 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 306 | switch (verb) { |
| 307 | case SkPath::kMove_Verb: |
| 308 | if (!bounds.isEmpty()) { |
| 309 | *boundsArray.append() = bounds; |
| 310 | } |
| 311 | bounds.set(pts[0].fX, pts[0].fY, pts[0].fX, pts[0].fY); |
| 312 | count = 0; |
| 313 | break; |
| 314 | case SkPath::kLine_Verb: |
| 315 | count = 1; |
| 316 | break; |
| 317 | case SkPath::kQuad_Verb: |
| 318 | count = 2; |
| 319 | break; |
| 320 | case SkPath::kCubic_Verb: |
| 321 | count = 3; |
| 322 | break; |
| 323 | case SkPath::kClose_Verb: |
| 324 | count = 0; |
| 325 | break; |
| 326 | default: |
| 327 | SkDEBUGFAIL("bad verb"); |
| 328 | return; |
| 329 | } |
| 330 | for (int i = 1; i <= count; ++i) { |
| 331 | bounds.growToInclude(pts[i].fX, pts[i].fY); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 336 | static bool extendLine(const SkPoint line[2], const SkPoint& add) { |
| 337 | // FIXME: allow this to extend lines that have slopes that are nearly equal |
| 338 | SkScalar dx1 = line[1].fX - line[0].fX; |
| 339 | SkScalar dy1 = line[1].fY - line[0].fY; |
| 340 | SkScalar dx2 = add.fX - line[0].fX; |
| 341 | SkScalar dy2 = add.fY - line[0].fY; |
| 342 | return dx1 * dy2 == dx2 * dy1; |
| 343 | } |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 344 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 345 | // OPTIMIZATION: this should point to a list of input data rather than duplicating |
| 346 | // the line data here. This would reduce the need to assemble the results. |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 347 | struct OutEdge { |
| 348 | bool operator<(const OutEdge& rh) const { |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 349 | const SkPoint& first = fPts[0]; |
| 350 | const SkPoint& rhFirst = rh.fPts[0]; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 351 | return first.fY == rhFirst.fY |
| 352 | ? first.fX < rhFirst.fX |
| 353 | : first.fY < rhFirst.fY; |
| 354 | } |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 355 | |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 356 | SkPoint fPts[4]; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 357 | int fID; // id of edge generating data |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 358 | uint8_t fVerb; // FIXME: not read from everywhere |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 359 | bool fCloseCall; // edge is trimmable if not originally coincident |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 360 | }; |
| 361 | |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 362 | class OutEdgeBuilder { |
| 363 | public: |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 364 | OutEdgeBuilder(bool fill) |
| 365 | : fFill(fill) { |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 366 | } |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 367 | |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 368 | void addCurve(const SkPoint line[4], SkPath::Verb verb, int id, |
| 369 | bool closeCall) { |
caryclark@google.com | c17972e | 2012-02-20 21:33:22 +0000 | [diff] [blame] | 370 | OutEdge& newEdge = fEdges.push_back(); |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 371 | memcpy(newEdge.fPts, line, (verb + 1) * sizeof(SkPoint)); |
| 372 | newEdge.fVerb = verb; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 373 | newEdge.fID = id; |
| 374 | newEdge.fCloseCall = closeCall; |
| 375 | } |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 376 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 377 | bool trimLine(SkScalar y, int id) { |
| 378 | size_t count = fEdges.count(); |
| 379 | while (count-- != 0) { |
| 380 | OutEdge& edge = fEdges[count]; |
| 381 | if (edge.fID != id) { |
| 382 | continue; |
| 383 | } |
| 384 | if (edge.fCloseCall) { |
| 385 | return false; |
| 386 | } |
| 387 | SkASSERT(edge.fPts[0].fY <= y); |
| 388 | if (edge.fPts[1].fY <= y) { |
| 389 | continue; |
| 390 | } |
| 391 | edge.fPts[1].fX = edge.fPts[0].fX + (y - edge.fPts[0].fY) |
| 392 | * (edge.fPts[1].fX - edge.fPts[0].fX) |
| 393 | / (edge.fPts[1].fY - edge.fPts[0].fY); |
| 394 | edge.fPts[1].fY = y; |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 395 | #if DEBUG_TRIM_LINE |
| 396 | SkDebugf("%s edge=%d %1.9g,%1.9g\n", __FUNCTION__, id, |
| 397 | edge.fPts[1].fX, y); |
| 398 | #endif |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 399 | return true; |
| 400 | } |
| 401 | return false; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | void assemble(SkPath& simple) { |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 405 | size_t listCount = fEdges.count(); |
| 406 | if (listCount == 0) { |
| 407 | return; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 408 | } |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 409 | do { |
| 410 | size_t listIndex = 0; |
| 411 | int advance = 1; |
| 412 | while (listIndex < listCount && fTops[listIndex] == 0) { |
| 413 | ++listIndex; |
| 414 | } |
| 415 | if (listIndex >= listCount) { |
| 416 | break; |
| 417 | } |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 418 | int closeEdgeIndex = -listIndex - 1; |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 419 | // the curve is deferred and not added right away because the |
| 420 | // following edge may extend the first curve. |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 421 | SkPoint firstPt, lastCurve[4]; |
| 422 | uint8_t lastVerb; |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 423 | #if DEBUG_ASSEMBLE |
| 424 | int firstIndex, lastIndex; |
| 425 | const int tab = 8; |
| 426 | #endif |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 427 | bool doMove = true; |
| 428 | int edgeIndex; |
| 429 | do { |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 430 | SkPoint* ptArray = fEdges[listIndex].fPts; |
| 431 | uint8_t verb = fEdges[listIndex].fVerb; |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 432 | SkPoint* curve[4]; |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 433 | if (advance < 0) { |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 434 | curve[0] = &ptArray[verb]; |
| 435 | if (verb == SkPath::kCubic_Verb) { |
| 436 | curve[1] = &ptArray[2]; |
| 437 | curve[2] = &ptArray[1]; |
| 438 | } |
| 439 | curve[verb] = &ptArray[0]; |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 440 | } else { |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 441 | curve[0] = &ptArray[0]; |
| 442 | if (verb == SkPath::kCubic_Verb) { |
| 443 | curve[1] = &ptArray[1]; |
| 444 | curve[2] = &ptArray[2]; |
| 445 | } |
| 446 | curve[verb] = &ptArray[verb]; |
| 447 | } |
| 448 | if (verb == SkPath::kQuad_Verb) { |
| 449 | curve[1] = &ptArray[1]; |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 450 | } |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 451 | if (doMove) { |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 452 | firstPt = *curve[0]; |
| 453 | simple.moveTo(curve[0]->fX, curve[0]->fY); |
| 454 | #if DEBUG_ASSEMBLE |
| 455 | SkDebugf("%s %d moveTo (%g,%g)\n", __FUNCTION__, |
| 456 | listIndex + 1, curve[0]->fX, curve[0]->fY); |
| 457 | firstIndex = listIndex; |
| 458 | #endif |
| 459 | for (int index = 0; index <= verb; ++index) { |
| 460 | lastCurve[index] = *curve[index]; |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 461 | } |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 462 | doMove = false; |
| 463 | } else { |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 464 | bool gap = lastCurve[lastVerb] != *curve[0]; |
| 465 | if (gap || lastVerb != SkPath::kLine_Verb) { // output the accumulated curve before the gap |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 466 | // FIXME: see comment in bridge -- this probably |
| 467 | // conceals errors |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 468 | SkASSERT(fFill && UlpsDiff(lastCurve[lastVerb].fY, |
| 469 | curve[0]->fY) <= 10); |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 470 | switch (lastVerb) { |
| 471 | case SkPath::kLine_Verb: |
| 472 | simple.lineTo(lastCurve[1].fX, lastCurve[1].fY); |
| 473 | break; |
| 474 | case SkPath::kQuad_Verb: |
| 475 | simple.quadTo(lastCurve[1].fX, lastCurve[1].fY, |
| 476 | lastCurve[2].fX, lastCurve[2].fY); |
| 477 | break; |
| 478 | case SkPath::kCubic_Verb: |
| 479 | simple.cubicTo(lastCurve[1].fX, lastCurve[1].fY, |
| 480 | lastCurve[2].fX, lastCurve[2].fY, |
| 481 | lastCurve[3].fX, lastCurve[3].fY); |
| 482 | break; |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 483 | } |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 484 | #if DEBUG_ASSEMBLE |
| 485 | SkDebugf("%*s %d %sTo (%g,%g)\n", tab, "", lastIndex + 1, |
| 486 | kLVerbStr[lastVerb], lastCurve[lastVerb].fX, |
| 487 | lastCurve[lastVerb].fY); |
| 488 | #endif |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 489 | } |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 490 | int firstCopy = 1; |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 491 | if (gap || (lastVerb == SkPath::kLine_Verb |
| 492 | && (verb != SkPath::kLine_Verb |
| 493 | || !extendLine(lastCurve, *curve[verb])))) { |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 494 | // FIXME: see comment in bridge -- this probably |
| 495 | // conceals errors |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 496 | SkASSERT(lastCurve[lastVerb] == *curve[0] || |
| 497 | (fFill && UlpsDiff(lastCurve[lastVerb].fY, |
| 498 | curve[0]->fY) <= 10)); |
| 499 | simple.lineTo(curve[0]->fX, curve[0]->fY); |
| 500 | #if DEBUG_ASSEMBLE |
| 501 | SkDebugf("%*s %d gap lineTo (%g,%g)\n", tab, "", |
| 502 | lastIndex + 1, curve[0]->fX, curve[0]->fY); |
| 503 | #endif |
| 504 | firstCopy = 0; |
| 505 | } else if (lastVerb != SkPath::kLine_Verb) { |
| 506 | firstCopy = 0; |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 507 | } |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 508 | for (int index = firstCopy; index <= verb; ++index) { |
| 509 | lastCurve[index] = *curve[index]; |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 510 | } |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 511 | } |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 512 | lastVerb = verb; |
| 513 | #if DEBUG_ASSEMBLE |
| 514 | lastIndex = listIndex; |
| 515 | #endif |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 516 | if (advance < 0) { |
| 517 | edgeIndex = fTops[listIndex]; |
| 518 | fTops[listIndex] = 0; |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 519 | } else { |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 520 | edgeIndex = fBottoms[listIndex]; |
| 521 | fBottoms[listIndex] = 0; |
| 522 | } |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 523 | if (edgeIndex) { |
| 524 | listIndex = abs(edgeIndex) - 1; |
| 525 | if (edgeIndex < 0) { |
| 526 | fTops[listIndex] = 0; |
| 527 | } else { |
| 528 | fBottoms[listIndex] = 0; |
| 529 | } |
| 530 | } |
| 531 | if (edgeIndex == closeEdgeIndex || edgeIndex == 0) { |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 532 | switch (lastVerb) { |
| 533 | case SkPath::kLine_Verb: |
| 534 | simple.lineTo(lastCurve[1].fX, lastCurve[1].fY); |
| 535 | break; |
| 536 | case SkPath::kQuad_Verb: |
| 537 | simple.quadTo(lastCurve[1].fX, lastCurve[1].fY, |
| 538 | lastCurve[2].fX, lastCurve[2].fY); |
| 539 | break; |
| 540 | case SkPath::kCubic_Verb: |
| 541 | simple.cubicTo(lastCurve[1].fX, lastCurve[1].fY, |
| 542 | lastCurve[2].fX, lastCurve[2].fY, |
| 543 | lastCurve[3].fX, lastCurve[3].fY); |
| 544 | break; |
| 545 | } |
| 546 | #if DEBUG_ASSEMBLE |
| 547 | SkDebugf("%*s %d %sTo last (%g, %g)\n", tab, "", |
| 548 | lastIndex + 1, kLVerbStr[lastVerb], |
| 549 | lastCurve[lastVerb].fX, lastCurve[lastVerb].fY); |
| 550 | #endif |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 551 | if (lastCurve[lastVerb] != firstPt) { |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 552 | simple.lineTo(firstPt.fX, firstPt.fY); |
| 553 | #if DEBUG_ASSEMBLE |
| 554 | SkDebugf("%*s %d final line (%g, %g)\n", tab, "", |
| 555 | firstIndex + 1, firstPt.fX, firstPt.fY); |
| 556 | #endif |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 557 | } |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 558 | simple.close(); |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 559 | #if DEBUG_ASSEMBLE |
| 560 | SkDebugf("%*s close\n", tab, ""); |
| 561 | #endif |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 562 | break; |
| 563 | } |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 564 | // if this and next edge go different directions |
| 565 | #if DEBUG_ASSEMBLE |
| 566 | SkDebugf("%*s advance=%d edgeIndex=%d flip=%s\n", tab, "", |
| 567 | advance, edgeIndex, advance > 0 ^ edgeIndex < 0 ? |
| 568 | "true" : "false"); |
| 569 | #endif |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 570 | if (advance > 0 ^ edgeIndex < 0) { |
| 571 | advance = -advance; |
| 572 | } |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 573 | } while (edgeIndex); |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 574 | } while (true); |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 575 | } |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 576 | |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 577 | // sort points by y, then x |
| 578 | // if x/y is identical, sort bottoms before tops |
| 579 | // if identical and both tops/bottoms, sort by angle |
| 580 | static bool lessThan(SkTArray<OutEdge>& edges, const int one, |
| 581 | const int two) { |
| 582 | const OutEdge& oneEdge = edges[abs(one) - 1]; |
| 583 | int oneIndex = one < 0 ? 0 : oneEdge.fVerb; |
| 584 | const SkPoint& startPt1 = oneEdge.fPts[oneIndex]; |
| 585 | const OutEdge& twoEdge = edges[abs(two) - 1]; |
| 586 | int twoIndex = two < 0 ? 0 : twoEdge.fVerb; |
| 587 | const SkPoint& startPt2 = twoEdge.fPts[twoIndex]; |
| 588 | if (startPt1.fY != startPt2.fY) { |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 589 | #if DEBUG_OUT_LESS_THAN |
| 590 | SkDebugf("%s %d<%d (%g,%g) %s startPt1.fY < startPt2.fY\n", __FUNCTION__, |
| 591 | one, two, startPt1.fY, startPt2.fY, |
| 592 | startPt1.fY < startPt2.fY ? "true" : "false"); |
| 593 | #endif |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 594 | return startPt1.fY < startPt2.fY; |
| 595 | } |
| 596 | if (startPt1.fX != startPt2.fX) { |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 597 | #if DEBUG_OUT_LESS_THAN |
| 598 | SkDebugf("%s %d<%d (%g,%g) %s startPt1.fX < startPt2.fX\n", __FUNCTION__, |
| 599 | one, two, startPt1.fX, startPt2.fX, |
| 600 | startPt1.fX < startPt2.fX ? "true" : "false"); |
| 601 | #endif |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 602 | return startPt1.fX < startPt2.fX; |
| 603 | } |
| 604 | const SkPoint& endPt1 = oneEdge.fPts[oneIndex ^ oneEdge.fVerb]; |
| 605 | const SkPoint& endPt2 = twoEdge.fPts[twoIndex ^ twoEdge.fVerb]; |
| 606 | SkScalar dy1 = startPt1.fY - endPt1.fY; |
| 607 | SkScalar dy2 = startPt2.fY - endPt2.fY; |
| 608 | SkScalar dy1y2 = dy1 * dy2; |
| 609 | if (dy1y2 < 0) { // different signs |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 610 | #if DEBUG_OUT_LESS_THAN |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 611 | SkDebugf("%s %d<%d %s dy1 > 0\n", __FUNCTION__, one, two, |
| 612 | dy1 > 0 ? "true" : "false"); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 613 | #endif |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 614 | return dy1 > 0; // one < two if one goes up and two goes down |
| 615 | } |
| 616 | if (dy1y2 == 0) { |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 617 | #if DEBUG_OUT_LESS_THAN |
| 618 | SkDebugf("%s %d<%d %s endPt1.fX < endPt2.fX\n", __FUNCTION__, |
| 619 | one, two, endPt1.fX < endPt2.fX ? "true" : "false"); |
| 620 | #endif |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 621 | return endPt1.fX < endPt2.fX; |
| 622 | } |
| 623 | SkScalar dx1y2 = (startPt1.fX - endPt1.fX) * dy2; |
| 624 | SkScalar dx2y1 = (startPt2.fX - endPt2.fX) * dy1; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 625 | #if DEBUG_OUT_LESS_THAN |
| 626 | SkDebugf("%s %d<%d %s dy2 < 0 ^ dx1y2 < dx2y1\n", __FUNCTION__, |
| 627 | one, two, dy2 < 0 ^ dx1y2 < dx2y1 ? "true" : "false"); |
| 628 | #endif |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 629 | return dy2 > 0 ^ dx1y2 < dx2y1; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 630 | } |
| 631 | |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 632 | // Sort the indices of paired points and then create more indices so |
| 633 | // assemble() can find the next edge and connect the top or bottom |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 634 | void bridge() { |
| 635 | size_t index; |
| 636 | size_t count = fEdges.count(); |
| 637 | if (!count) { |
| 638 | return; |
| 639 | } |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 640 | SkASSERT(!fFill || count > 1); |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 641 | fTops.setCount(count); |
| 642 | sk_bzero(fTops.begin(), sizeof(fTops[0]) * count); |
| 643 | fBottoms.setCount(count); |
| 644 | sk_bzero(fBottoms.begin(), sizeof(fBottoms[0]) * count); |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 645 | SkTDArray<int> order; |
| 646 | for (index = 1; index <= count; ++index) { |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 647 | *order.append() = -index; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 648 | } |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 649 | for (index = 1; index <= count; ++index) { |
| 650 | *order.append() = index; |
| 651 | } |
| 652 | QSort<SkTArray<OutEdge>, int>(fEdges, order.begin(), order.end() - 1, lessThan); |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 653 | int* lastPtr = order.end() - 1; |
| 654 | int* leftPtr = order.begin(); |
| 655 | while (leftPtr < lastPtr) { |
| 656 | int leftIndex = *leftPtr; |
| 657 | int leftOutIndex = abs(leftIndex) - 1; |
| 658 | const OutEdge& left = fEdges[leftOutIndex]; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 659 | int* rightPtr = leftPtr + 1; |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 660 | int rightIndex = *rightPtr; |
| 661 | int rightOutIndex = abs(rightIndex) - 1; |
| 662 | const OutEdge& right = fEdges[rightOutIndex]; |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 663 | bool pairUp = fFill; |
| 664 | if (!pairUp) { |
| 665 | const SkPoint& leftMatch = |
| 666 | left.fPts[leftIndex < 0 ? 0 : left.fVerb]; |
| 667 | const SkPoint& rightMatch = |
| 668 | right.fPts[rightIndex < 0 ? 0 : right.fVerb]; |
| 669 | pairUp = leftMatch == rightMatch; |
| 670 | } else { |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 671 | #if DEBUG_OUT |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 672 | // FIXME : not happy that error in low bit is allowed |
| 673 | // this probably conceals error elsewhere |
| 674 | if (UlpsDiff(left.fPts[leftIndex < 0 ? 0 : left.fVerb].fY, |
| 675 | right.fPts[rightIndex < 0 ? 0 : right.fVerb].fY) > 1) { |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 676 | *fMismatches.append() = leftIndex; |
| 677 | if (rightPtr == lastPtr) { |
| 678 | *fMismatches.append() = rightIndex; |
| 679 | } |
| 680 | pairUp = false; |
| 681 | } |
| 682 | #else |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 683 | SkASSERT(UlpsDiff(left.fPts[leftIndex < 0 ? 0 : left.fVerb].fY, |
| 684 | right.fPts[rightIndex < 0 ? 0 : right.fVerb].fY) <= 10); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 685 | #endif |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 686 | } |
| 687 | if (pairUp) { |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 688 | if (leftIndex < 0) { |
| 689 | fTops[leftOutIndex] = rightIndex; |
| 690 | } else { |
| 691 | fBottoms[leftOutIndex] = rightIndex; |
| 692 | } |
| 693 | if (rightIndex < 0) { |
| 694 | fTops[rightOutIndex] = leftIndex; |
| 695 | } else { |
| 696 | fBottoms[rightOutIndex] = leftIndex; |
| 697 | } |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 698 | ++rightPtr; |
| 699 | } |
| 700 | leftPtr = rightPtr; |
| 701 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 702 | #if DEBUG_OUT |
| 703 | int* mismatch = fMismatches.begin(); |
| 704 | while (mismatch != fMismatches.end()) { |
| 705 | int leftIndex = *mismatch++; |
| 706 | int leftOutIndex = abs(leftIndex) - 1; |
| 707 | const OutEdge& left = fEdges[leftOutIndex]; |
| 708 | const SkPoint& leftPt = left.fPts[leftIndex < 0 ? 0 : left.fVerb]; |
| 709 | SkDebugf("%s left=%d %s (%1.9g,%1.9g)\n", |
| 710 | __FUNCTION__, left.fID, leftIndex < 0 ? "top" : "bot", |
| 711 | leftPt.fX, leftPt.fY); |
| 712 | } |
| 713 | SkASSERT(fMismatches.count() == 0); |
| 714 | #endif |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 715 | #if DEBUG_BRIDGE |
| 716 | for (index = 0; index < count; ++index) { |
| 717 | const OutEdge& edge = fEdges[index]; |
| 718 | uint8_t verb = edge.fVerb; |
| 719 | SkDebugf("%s %d edge=%d %s (%1.9g,%1.9g) (%1.9g,%1.9g)\n", |
| 720 | index == 0 ? __FUNCTION__ : " ", |
| 721 | index + 1, edge.fID, kLVerbStr[verb], edge.fPts[0].fX, |
| 722 | edge.fPts[0].fY, edge.fPts[verb].fX, edge.fPts[verb].fY); |
| 723 | } |
| 724 | for (index = 0; index < count; ++index) { |
| 725 | SkDebugf(" top of % 2d connects to %s of % 2d\n", index + 1, |
| 726 | fTops[index] < 0 ? "top " : "bottom", abs(fTops[index])); |
| 727 | SkDebugf(" bottom of % 2d connects to %s of % 2d\n", index + 1, |
| 728 | fBottoms[index] < 0 ? "top " : "bottom", abs(fBottoms[index])); |
| 729 | } |
| 730 | #endif |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 731 | } |
| 732 | |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 733 | protected: |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 734 | SkTArray<OutEdge> fEdges; |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 735 | SkTDArray<int> fTops; |
| 736 | SkTDArray<int> fBottoms; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 737 | bool fFill; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 738 | #if DEBUG_OUT |
| 739 | SkTDArray<int> fMismatches; |
| 740 | #endif |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 741 | }; |
| 742 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 743 | // Bounds, unlike Rect, does not consider a vertical line to be empty. |
| 744 | struct Bounds : public SkRect { |
| 745 | static bool Intersects(const Bounds& a, const Bounds& b) { |
| 746 | return a.fLeft <= b.fRight && b.fLeft <= a.fRight && |
| 747 | a.fTop <= b.fBottom && b.fTop <= a.fBottom; |
| 748 | } |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 749 | |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 750 | bool isEmpty() { |
| 751 | return fLeft > fRight || fTop > fBottom |
| 752 | || fLeft == fRight && fTop == fBottom |
| 753 | || isnan(fLeft) || isnan(fRight) |
| 754 | || isnan(fTop) || isnan(fBottom); |
| 755 | } |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 756 | }; |
| 757 | |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 758 | class Intercepts { |
| 759 | public: |
| 760 | Intercepts() |
| 761 | : fTopIntercepts(0) |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 762 | , fBottomIntercepts(0) |
| 763 | , fExplicit(false) { |
| 764 | } |
| 765 | |
| 766 | Intercepts& operator=(const Intercepts& src) { |
| 767 | fTs = src.fTs; |
| 768 | fTopIntercepts = src.fTopIntercepts; |
| 769 | fBottomIntercepts = src.fBottomIntercepts; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 770 | return *this; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | // OPTIMIZATION: remove this function if it's never called |
| 774 | double t(int tIndex) const { |
| 775 | if (tIndex == 0) { |
| 776 | return 0; |
| 777 | } |
| 778 | if (tIndex > fTs.count()) { |
| 779 | return 1; |
| 780 | } |
| 781 | return fTs[tIndex - 1]; |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 782 | } |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 783 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 784 | #if DEBUG_DUMP |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 785 | void dump(const SkPoint* pts, SkPath::Verb verb) { |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 786 | const char className[] = "Intercepts"; |
| 787 | const int tab = 8; |
| 788 | for (int i = 0; i < fTs.count(); ++i) { |
| 789 | SkPoint out; |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 790 | switch (verb) { |
| 791 | case SkPath::kLine_Verb: |
| 792 | LineXYAtT(pts, fTs[i], &out); |
| 793 | break; |
| 794 | case SkPath::kQuad_Verb: |
| 795 | QuadXYAtT(pts, fTs[i], &out); |
| 796 | break; |
| 797 | case SkPath::kCubic_Verb: |
| 798 | CubicXYAtT(pts, fTs[i], &out); |
| 799 | break; |
| 800 | default: |
| 801 | SkASSERT(0); |
| 802 | } |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 803 | SkDebugf("%*s.fTs[%d]=%1.9g (%1.9g,%1.9g)\n", tab + sizeof(className), |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 804 | className, i, fTs[i], out.fX, out.fY); |
| 805 | } |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 806 | SkDebugf("%*s.fTopIntercepts=%u\n", tab + sizeof(className), |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 807 | className, fTopIntercepts); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 808 | SkDebugf("%*s.fBottomIntercepts=%u\n", tab + sizeof(className), |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 809 | className, fBottomIntercepts); |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 810 | SkDebugf("%*s.fExplicit=%d\n", tab + sizeof(className), |
| 811 | className, fExplicit); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 812 | } |
| 813 | #endif |
| 814 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 815 | SkTDArray<double> fTs; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 816 | unsigned char fTopIntercepts; // 0=init state 1=1 edge >1=multiple edges |
| 817 | unsigned char fBottomIntercepts; |
| 818 | bool fExplicit; // if set, suppress 0 and 1 |
| 819 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 820 | }; |
| 821 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 822 | struct HorizontalEdge { |
| 823 | bool operator<(const HorizontalEdge& rh) const { |
| 824 | return fY == rh.fY ? fLeft == rh.fLeft ? fRight < rh.fRight |
| 825 | : fLeft < rh.fLeft : fY < rh.fY; |
| 826 | } |
| 827 | |
| 828 | #if DEBUG_DUMP |
| 829 | void dump() { |
| 830 | const char className[] = "HorizontalEdge"; |
| 831 | const int tab = 4; |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 832 | SkDebugf("%*s.fLeft=%1.9g\n", tab + sizeof(className), className, fLeft); |
| 833 | SkDebugf("%*s.fRight=%1.9g\n", tab + sizeof(className), className, fRight); |
| 834 | SkDebugf("%*s.fY=%1.9g\n", tab + sizeof(className), className, fY); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 835 | } |
| 836 | #endif |
| 837 | |
| 838 | SkScalar fLeft; |
| 839 | SkScalar fRight; |
| 840 | SkScalar fY; |
| 841 | }; |
| 842 | |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 843 | struct InEdge { |
| 844 | bool operator<(const InEdge& rh) const { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 845 | return fBounds.fTop == rh.fBounds.fTop |
| 846 | ? fBounds.fLeft < rh.fBounds.fLeft |
| 847 | : fBounds.fTop < rh.fBounds.fTop; |
| 848 | } |
| 849 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 850 | // Avoid collapsing t values that are close to the same since |
| 851 | // we walk ts to describe consecutive intersections. Since a pair of ts can |
| 852 | // be nearly equal, any problems caused by this should be taken care |
| 853 | // of later. |
| 854 | int add(double* ts, size_t count, ptrdiff_t verbIndex) { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 855 | // FIXME: in the pathological case where there is a ton of intercepts, binary search? |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 856 | bool foundIntercept = false; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 857 | int insertedAt = -1; |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 858 | Intercepts& intercepts = fIntercepts[verbIndex]; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 859 | for (size_t index = 0; index < count; ++index) { |
| 860 | double t = ts[index]; |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 861 | if (t <= 0) { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 862 | intercepts.fTopIntercepts <<= 1; |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 863 | fContainsIntercepts |= ++intercepts.fTopIntercepts > 1; |
| 864 | continue; |
| 865 | } |
| 866 | if (t >= 1) { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 867 | intercepts.fBottomIntercepts <<= 1; |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 868 | fContainsIntercepts |= ++intercepts.fBottomIntercepts > 1; |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 869 | continue; |
| 870 | } |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 871 | fIntersected = true; |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 872 | foundIntercept = true; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 873 | size_t tCount = intercepts.fTs.count(); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 874 | double delta; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 875 | for (size_t idx2 = 0; idx2 < tCount; ++idx2) { |
| 876 | if (t <= intercepts.fTs[idx2]) { |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 877 | // FIXME: ? if (t < intercepts.fTs[idx2]) // failed |
| 878 | delta = intercepts.fTs[idx2] - t; |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 879 | if (delta > 0) { |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 880 | insertedAt = idx2; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 881 | *intercepts.fTs.insert(idx2) = t; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 882 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 883 | goto nextPt; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 884 | } |
| 885 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 886 | if (tCount == 0 || (delta = t - intercepts.fTs[tCount - 1]) > 0) { |
| 887 | insertedAt = tCount; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 888 | *intercepts.fTs.append() = t; |
| 889 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 890 | nextPt: |
| 891 | ; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 892 | } |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 893 | fContainsIntercepts |= foundIntercept; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 894 | return insertedAt; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 895 | } |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 896 | |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 897 | void addPartial(SkTArray<InEdge>& edges, int ptStart, int ptEnd, |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 898 | int verbStart, int verbEnd) { |
| 899 | InEdge* edge = edges.push_back_n(1); |
| 900 | int verbCount = verbEnd - verbStart; |
| 901 | edge->fIntercepts.push_back_n(verbCount); |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame^] | 902 | // uint8_t* verbs = &fVerbs[verbStart]; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 903 | for (int ceptIdx = 0; ceptIdx < verbCount; ++ceptIdx) { |
| 904 | edge->fIntercepts[ceptIdx] = fIntercepts[verbStart + ceptIdx]; |
| 905 | } |
| 906 | edge->fPts.append(ptEnd - ptStart, &fPts[ptStart]); |
| 907 | edge->fVerbs.append(verbCount, &fVerbs[verbStart]); |
| 908 | edge->setBounds(); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 909 | edge->fWinding = fWinding; |
| 910 | edge->fContainsIntercepts = fContainsIntercepts; // FIXME: may not be correct -- but do we need to know? |
| 911 | } |
| 912 | |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 913 | void addSplit(SkTArray<InEdge>& edges, SkPoint* pts, uint8_t verb, |
| 914 | Intercepts& intercepts, int firstT, int lastT, bool flipped) { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 915 | InEdge* edge = edges.push_back_n(1); |
| 916 | edge->fIntercepts.push_back_n(1); |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 917 | if (firstT == 0) { |
| 918 | *edge->fIntercepts[0].fTs.append() = 0; |
| 919 | } else { |
| 920 | *edge->fIntercepts[0].fTs.append() = intercepts.fTs[firstT - 1]; |
| 921 | } |
| 922 | bool add1 = lastT == intercepts.fTs.count(); |
| 923 | edge->fIntercepts[0].fTs.append(lastT - firstT, &intercepts.fTs[firstT]); |
| 924 | if (add1) { |
| 925 | *edge->fIntercepts[0].fTs.append() = 1; |
| 926 | } |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 927 | edge->fIntercepts[0].fExplicit = true; |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 928 | edge->fPts.append(verb + 1, pts); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 929 | edge->fVerbs.append(1, &verb); |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 930 | // FIXME: bounds could be better for partial Ts |
| 931 | edge->setSubBounds(); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 932 | edge->fContainsIntercepts = fContainsIntercepts; // FIXME: may not be correct -- but do we need to know? |
| 933 | if (flipped) { |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 934 | edge->flipTs(); |
| 935 | edge->fWinding = -fWinding; |
| 936 | } else { |
| 937 | edge->fWinding = fWinding; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 938 | } |
| 939 | } |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 940 | |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 941 | bool cached(const InEdge* edge) { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 942 | // FIXME: in the pathological case where there is a ton of edges, binary search? |
| 943 | size_t count = fCached.count(); |
| 944 | for (size_t index = 0; index < count; ++index) { |
| 945 | if (edge == fCached[index]) { |
| 946 | return true; |
| 947 | } |
| 948 | if (edge < fCached[index]) { |
| 949 | *fCached.insert(index) = edge; |
| 950 | return false; |
| 951 | } |
| 952 | } |
| 953 | *fCached.append() = edge; |
| 954 | return false; |
| 955 | } |
| 956 | |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 957 | void complete(signed char winding) { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 958 | setBounds(); |
| 959 | fIntercepts.push_back_n(fVerbs.count()); |
| 960 | if ((fWinding = winding) < 0) { // reverse verbs, pts, if bottom to top |
| 961 | flip(); |
| 962 | } |
| 963 | fContainsIntercepts = fIntersected = false; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 964 | } |
| 965 | |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 966 | void flip() { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 967 | size_t index; |
| 968 | size_t last = fPts.count() - 1; |
| 969 | for (index = 0; index < last; ++index, --last) { |
| 970 | SkTSwap<SkPoint>(fPts[index], fPts[last]); |
| 971 | } |
| 972 | last = fVerbs.count() - 1; |
| 973 | for (index = 0; index < last; ++index, --last) { |
| 974 | SkTSwap<uint8_t>(fVerbs[index], fVerbs[last]); |
| 975 | } |
| 976 | } |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 977 | |
| 978 | void flipTs() { |
| 979 | SkASSERT(fIntercepts.count() == 1); |
| 980 | Intercepts& intercepts = fIntercepts[0]; |
| 981 | SkASSERT(intercepts.fExplicit); |
| 982 | SkTDArray<double>& ts = intercepts.fTs; |
| 983 | size_t index; |
| 984 | size_t last = ts.count() - 1; |
| 985 | for (index = 0; index < last; ++index, --last) { |
| 986 | SkTSwap<double>(ts[index], ts[last]); |
| 987 | } |
| 988 | } |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 989 | |
| 990 | void reset() { |
| 991 | fCached.reset(); |
| 992 | fIntercepts.reset(); |
| 993 | fPts.reset(); |
| 994 | fVerbs.reset(); |
| 995 | fBounds.set(SK_ScalarMax, SK_ScalarMax, SK_ScalarMax, SK_ScalarMax); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 996 | fWinding = 0; |
| 997 | fContainsIntercepts = false; |
| 998 | fIntersected = false; |
| 999 | } |
| 1000 | |
| 1001 | void setBounds() { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1002 | SkPoint* ptPtr = fPts.begin(); |
| 1003 | SkPoint* ptLast = fPts.end(); |
| 1004 | if (ptPtr == ptLast) { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1005 | SkDebugf("%s empty edge\n", __FUNCTION__); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1006 | SkASSERT(0); |
| 1007 | // FIXME: delete empty edge? |
| 1008 | return; |
| 1009 | } |
| 1010 | fBounds.set(ptPtr->fX, ptPtr->fY, ptPtr->fX, ptPtr->fY); |
| 1011 | ++ptPtr; |
| 1012 | while (ptPtr != ptLast) { |
| 1013 | fBounds.growToInclude(ptPtr->fX, ptPtr->fY); |
| 1014 | ++ptPtr; |
| 1015 | } |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1016 | } |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 1017 | |
| 1018 | // recompute bounds based on subrange of T values |
| 1019 | void setSubBounds() { |
| 1020 | SkASSERT(fIntercepts.count() == 1); |
| 1021 | Intercepts& intercepts = fIntercepts[0]; |
| 1022 | SkASSERT(intercepts.fExplicit); |
| 1023 | SkASSERT(fVerbs.count() == 1); |
| 1024 | SkTDArray<double>& ts = intercepts.fTs; |
| 1025 | if (fVerbs[0] == SkPath::kQuad_Verb) { |
| 1026 | SkASSERT(fPts.count() == 3); |
| 1027 | QuadSubBounds(fPts.begin(), ts[0], ts[ts.count() - 1], fBounds); |
| 1028 | } else { |
| 1029 | SkASSERT(fVerbs[0] == SkPath::kCubic_Verb); |
| 1030 | SkASSERT(fPts.count() == 4); |
| 1031 | CubicSubBounds(fPts.begin(), ts[0], ts[ts.count() - 1], fBounds); |
| 1032 | } |
| 1033 | } |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1034 | |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 1035 | void splitInflectionPts(SkTArray<InEdge>& edges) { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1036 | if (!fIntersected) { |
| 1037 | return; |
| 1038 | } |
| 1039 | uint8_t* verbs = fVerbs.begin(); |
| 1040 | SkPoint* pts = fPts.begin(); |
| 1041 | int lastVerb = 0; |
| 1042 | int lastPt = 0; |
| 1043 | uint8_t verb; |
| 1044 | bool edgeSplit = false; |
| 1045 | for (int ceptIdx = 0; ceptIdx < fIntercepts.count(); ++ceptIdx, pts += verb) { |
| 1046 | Intercepts& intercepts = fIntercepts[ceptIdx]; |
| 1047 | verb = *verbs++; |
| 1048 | if (verb <= SkPath::kLine_Verb) { |
| 1049 | continue; |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1050 | } |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1051 | size_t tCount = intercepts.fTs.count(); |
| 1052 | if (!tCount) { |
| 1053 | continue; |
| 1054 | } |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame^] | 1055 | size_t tIndex = (size_t) -1; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1056 | SkScalar y = pts[0].fY; |
| 1057 | int lastSplit = 0; |
| 1058 | int firstSplit = -1; |
| 1059 | bool curveSplit = false; |
| 1060 | while (++tIndex < tCount) { |
| 1061 | double nextT = intercepts.fTs[tIndex]; |
| 1062 | SkScalar nextY = verb == SkPath::kQuad_Verb |
| 1063 | ? QuadYAtT(pts, nextT) : CubicYAtT(pts, nextT); |
| 1064 | if (nextY < y) { |
| 1065 | edgeSplit = curveSplit = true; |
| 1066 | if (firstSplit < 0) { |
| 1067 | firstSplit = tIndex; |
| 1068 | int nextPt = pts - fPts.begin(); |
| 1069 | int nextVerb = verbs - 1 - fVerbs.begin(); |
| 1070 | if (lastVerb < nextVerb) { |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 1071 | addPartial(edges, lastPt, nextPt, lastVerb, nextVerb); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1072 | #if DEBUG_SPLIT |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 1073 | SkDebugf("%s addPartial 1\n", __FUNCTION__); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1074 | #endif |
| 1075 | } |
| 1076 | lastPt = nextPt; |
| 1077 | lastVerb = nextVerb; |
| 1078 | } |
| 1079 | } else { |
| 1080 | if (firstSplit >= 0) { |
| 1081 | if (lastSplit < firstSplit) { |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 1082 | addSplit(edges, pts, verb, intercepts, |
| 1083 | lastSplit, firstSplit, false); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1084 | #if DEBUG_SPLIT |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 1085 | SkDebugf("%s addSplit 1 tIndex=%d,%d\n", |
| 1086 | __FUNCTION__, lastSplit, firstSplit); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1087 | #endif |
| 1088 | } |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 1089 | addSplit(edges, pts, verb, intercepts, |
| 1090 | firstSplit, tIndex, true); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1091 | #if DEBUG_SPLIT |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 1092 | SkDebugf("%s addSplit 2 tIndex=%d,%d flip\n", |
| 1093 | __FUNCTION__, firstSplit, tIndex); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1094 | #endif |
| 1095 | lastSplit = tIndex; |
| 1096 | firstSplit = -1; |
| 1097 | } |
| 1098 | } |
| 1099 | y = nextY; |
| 1100 | } |
| 1101 | if (curveSplit) { |
| 1102 | if (firstSplit < 0) { |
| 1103 | firstSplit = lastSplit; |
| 1104 | } else { |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 1105 | addSplit(edges, pts, verb, intercepts, lastSplit, |
| 1106 | firstSplit, false); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1107 | #if DEBUG_SPLIT |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 1108 | SkDebugf("%s addSplit 3 tIndex=%d,%d\n", __FUNCTION__, |
| 1109 | lastSplit, firstSplit); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1110 | #endif |
| 1111 | } |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 1112 | addSplit(edges, pts, verb, intercepts, firstSplit, |
| 1113 | tIndex, pts[verb].fY < y); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1114 | #if DEBUG_SPLIT |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 1115 | SkDebugf("%s addSplit 4 tIndex=%d,%d %s\n", __FUNCTION__, |
| 1116 | firstSplit, tIndex, pts[verb].fY < y ? "flip" : ""); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1117 | #endif |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1118 | } |
| 1119 | } |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1120 | // collapse remainder -- if there's nothing left, clear it somehow? |
| 1121 | if (edgeSplit) { |
| 1122 | int nextVerb = verbs - 1 - fVerbs.begin(); |
| 1123 | if (lastVerb < nextVerb) { |
| 1124 | int nextPt = pts - fPts.begin(); |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 1125 | addPartial(edges, lastPt, nextPt, lastVerb, nextVerb); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1126 | #if DEBUG_SPLIT |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 1127 | SkDebugf("%s addPartial 2\n", __FUNCTION__); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1128 | #endif |
| 1129 | } |
| 1130 | // OPTIMIZATION: reuse the edge instead of marking it empty |
| 1131 | reset(); |
| 1132 | } |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1135 | #if DEBUG_DUMP |
| 1136 | void dump() { |
| 1137 | int i; |
| 1138 | const char className[] = "InEdge"; |
| 1139 | const int tab = 4; |
| 1140 | SkDebugf("InEdge %p (edge=%d)\n", this, fID); |
| 1141 | for (i = 0; i < fCached.count(); ++i) { |
| 1142 | SkDebugf("%*s.fCached[%d]=0x%08x\n", tab + sizeof(className), |
| 1143 | className, i, fCached[i]); |
| 1144 | } |
| 1145 | uint8_t* verbs = fVerbs.begin(); |
| 1146 | SkPoint* pts = fPts.begin(); |
| 1147 | for (i = 0; i < fIntercepts.count(); ++i) { |
| 1148 | SkDebugf("%*s.fIntercepts[%d]:\n", tab + sizeof(className), |
| 1149 | className, i); |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 1150 | fIntercepts[i].dump(pts, (SkPath::Verb) *verbs); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1151 | pts += *verbs++; |
| 1152 | } |
| 1153 | for (i = 0; i < fPts.count(); ++i) { |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1154 | SkDebugf("%*s.fPts[%d]=(%1.9g,%1.9g)\n", tab + sizeof(className), |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1155 | className, i, fPts[i].fX, fPts[i].fY); |
| 1156 | } |
| 1157 | for (i = 0; i < fVerbs.count(); ++i) { |
| 1158 | SkDebugf("%*s.fVerbs[%d]=%d\n", tab + sizeof(className), |
| 1159 | className, i, fVerbs[i]); |
| 1160 | } |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 1161 | SkDebugf("%*s.fBounds=(%1.9g, %1.9g, %1.9g, %1.9g)\n", tab + sizeof(className), |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1162 | className, fBounds.fLeft, fBounds.fTop, |
| 1163 | fBounds.fRight, fBounds.fBottom); |
| 1164 | SkDebugf("%*s.fWinding=%d\n", tab + sizeof(className), className, |
| 1165 | fWinding); |
| 1166 | SkDebugf("%*s.fContainsIntercepts=%d\n", tab + sizeof(className), |
| 1167 | className, fContainsIntercepts); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1168 | SkDebugf("%*s.fIntersected=%d\n", tab + sizeof(className), |
| 1169 | className, fIntersected); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1170 | } |
| 1171 | #endif |
| 1172 | |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1173 | // FIXME: temporary data : move this to a separate struct? |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1174 | SkTDArray<const InEdge*> fCached; // list of edges already intercepted |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1175 | SkTArray<Intercepts> fIntercepts; // one per verb |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 1176 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1177 | // persistent data |
| 1178 | SkTDArray<SkPoint> fPts; |
| 1179 | SkTDArray<uint8_t> fVerbs; |
| 1180 | Bounds fBounds; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1181 | int fID; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1182 | signed char fWinding; |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1183 | bool fContainsIntercepts; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1184 | bool fIntersected; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1185 | }; |
| 1186 | |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1187 | class InEdgeBuilder { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1188 | public: |
| 1189 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1190 | InEdgeBuilder(const SkPath& path, bool ignoreHorizontal, SkTArray<InEdge>& edges, |
| 1191 | SkTDArray<HorizontalEdge>& horizontalEdges) |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1192 | : fPath(path) |
| 1193 | , fCurrentEdge(NULL) |
| 1194 | , fEdges(edges) |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1195 | , fHorizontalEdges(horizontalEdges) |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1196 | , fIgnoreHorizontal(ignoreHorizontal) |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1197 | , fContainsCurves(false) |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1198 | { |
| 1199 | walk(); |
| 1200 | } |
| 1201 | |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1202 | bool containsCurves() const { |
| 1203 | return fContainsCurves; |
| 1204 | } |
| 1205 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1206 | protected: |
| 1207 | |
| 1208 | void addEdge() { |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 1209 | SkASSERT(fCurrentEdge); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1210 | fCurrentEdge->fPts.append(fPtCount - fPtOffset, &fPts[fPtOffset]); |
| 1211 | fPtOffset = 1; |
| 1212 | *fCurrentEdge->fVerbs.append() = fVerb; |
| 1213 | } |
| 1214 | |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 1215 | bool complete() { |
| 1216 | if (fCurrentEdge && fCurrentEdge->fVerbs.count()) { |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 1217 | fCurrentEdge->complete(fWinding); |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 1218 | fCurrentEdge = NULL; |
| 1219 | return true; |
| 1220 | } |
| 1221 | return false; |
| 1222 | } |
| 1223 | |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1224 | int direction(SkPath::Verb verb) { |
| 1225 | fPtCount = verb + 1; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1226 | if (fIgnoreHorizontal && isHorizontal()) { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1227 | return 0; |
| 1228 | } |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1229 | return fPts[0].fY == fPts[verb].fY |
| 1230 | ? fPts[0].fX == fPts[verb].fX ? 0 : fPts[0].fX < fPts[verb].fX |
| 1231 | ? 1 : -1 : fPts[0].fY < fPts[verb].fY ? 1 : -1; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | bool isHorizontal() { |
| 1235 | SkScalar y = fPts[0].fY; |
| 1236 | for (int i = 1; i < fPtCount; ++i) { |
| 1237 | if (fPts[i].fY != y) { |
| 1238 | return false; |
| 1239 | } |
| 1240 | } |
| 1241 | return true; |
| 1242 | } |
| 1243 | |
| 1244 | void startEdge() { |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 1245 | if (!fCurrentEdge) { |
| 1246 | fCurrentEdge = fEdges.push_back_n(1); |
| 1247 | } |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1248 | fWinding = 0; |
| 1249 | fPtOffset = 0; |
| 1250 | } |
| 1251 | |
| 1252 | void walk() { |
| 1253 | SkPath::Iter iter(fPath, true); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1254 | int winding = 0; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1255 | while ((fVerb = iter.next(fPts)) != SkPath::kDone_Verb) { |
| 1256 | switch (fVerb) { |
| 1257 | case SkPath::kMove_Verb: |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1258 | startEdge(); |
| 1259 | continue; |
| 1260 | case SkPath::kLine_Verb: |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1261 | winding = direction(SkPath::kLine_Verb); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1262 | break; |
| 1263 | case SkPath::kQuad_Verb: |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1264 | fVerb = QuadReduceOrder(fPts); |
| 1265 | winding = direction(fVerb); |
| 1266 | fContainsCurves |= fVerb == SkPath::kQuad_Verb; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1267 | break; |
| 1268 | case SkPath::kCubic_Verb: |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1269 | fVerb = CubicReduceOrder(fPts); |
| 1270 | winding = direction(fVerb); |
| 1271 | fContainsCurves |= fVerb >= SkPath::kQuad_Verb; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1272 | break; |
| 1273 | case SkPath::kClose_Verb: |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 1274 | SkASSERT(fCurrentEdge); |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 1275 | complete(); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1276 | continue; |
| 1277 | default: |
| 1278 | SkDEBUGFAIL("bad verb"); |
| 1279 | return; |
| 1280 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1281 | if (winding == 0) { |
| 1282 | HorizontalEdge* horizontalEdge = fHorizontalEdges.append(); |
| 1283 | // FIXME: for degenerate quads and cubics, compute x extremes |
| 1284 | horizontalEdge->fLeft = fPts[0].fX; |
| 1285 | horizontalEdge->fRight = fPts[fVerb].fX; |
| 1286 | horizontalEdge->fY = fPts[0].fY; |
| 1287 | if (horizontalEdge->fLeft > horizontalEdge->fRight) { |
| 1288 | SkTSwap<SkScalar>(horizontalEdge->fLeft, horizontalEdge->fRight); |
| 1289 | } |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 1290 | if (complete()) { |
| 1291 | startEdge(); |
| 1292 | } |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1293 | continue; |
| 1294 | } |
| 1295 | if (fWinding + winding == 0) { |
| 1296 | // FIXME: if prior verb or this verb is a horizontal line, reverse |
| 1297 | // it instead of starting a new edge |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 1298 | SkASSERT(fCurrentEdge); |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 1299 | if (complete()) { |
| 1300 | startEdge(); |
| 1301 | } |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1302 | } |
| 1303 | fWinding = winding; |
| 1304 | addEdge(); |
| 1305 | } |
caryclark@google.com | 6b9cfb3 | 2012-02-16 21:24:41 +0000 | [diff] [blame] | 1306 | if (!complete()) { |
| 1307 | if (fCurrentEdge) { |
| 1308 | fEdges.pop_back(); |
| 1309 | } |
| 1310 | } |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | private: |
| 1314 | const SkPath& fPath; |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1315 | InEdge* fCurrentEdge; |
| 1316 | SkTArray<InEdge>& fEdges; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1317 | SkTDArray<HorizontalEdge>& fHorizontalEdges; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1318 | SkPoint fPts[4]; |
| 1319 | SkPath::Verb fVerb; |
| 1320 | int fPtCount; |
| 1321 | int fPtOffset; |
| 1322 | int8_t fWinding; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1323 | bool fIgnoreHorizontal; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1324 | bool fContainsCurves; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1325 | }; |
| 1326 | |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1327 | struct WorkEdge { |
| 1328 | SkScalar bottom() const { |
| 1329 | return fPts[verb()].fY; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1332 | void init(const InEdge* edge) { |
| 1333 | fEdge = edge; |
| 1334 | fPts = edge->fPts.begin(); |
| 1335 | fVerb = edge->fVerbs.begin(); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 1338 | bool advance() { |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1339 | SkASSERT(fVerb < fEdge->fVerbs.end()); |
| 1340 | fPts += *fVerb++; |
| 1341 | return fVerb != fEdge->fVerbs.end(); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1342 | } |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1343 | |
| 1344 | const SkPoint* lastPoints() const { |
| 1345 | SkASSERT(fPts >= fEdge->fPts.begin() + lastVerb()); |
| 1346 | return &fPts[-lastVerb()]; |
| 1347 | } |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1348 | |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 1349 | SkPath::Verb lastVerb() const { |
| 1350 | SkASSERT(fVerb > fEdge->fVerbs.begin()); |
| 1351 | return (SkPath::Verb) fVerb[-1]; |
| 1352 | } |
| 1353 | |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1354 | const SkPoint* points() const { |
| 1355 | return fPts; |
| 1356 | } |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1357 | |
| 1358 | SkPath::Verb verb() const { |
| 1359 | return (SkPath::Verb) *fVerb; |
| 1360 | } |
| 1361 | |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 1362 | ptrdiff_t verbIndex() const { |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1363 | return fVerb - fEdge->fVerbs.begin(); |
| 1364 | } |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1365 | |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1366 | int winding() const { |
| 1367 | return fEdge->fWinding; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1370 | const InEdge* fEdge; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1371 | const SkPoint* fPts; |
| 1372 | const uint8_t* fVerb; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1373 | }; |
| 1374 | |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1375 | // always constructed with SkTDArray because new edges are inserted |
| 1376 | // this may be a inappropriate optimization, suggesting that a separate array of |
| 1377 | // ActiveEdge* may be faster to insert and search |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 1378 | |
| 1379 | // OPTIMIZATION: Brian suggests that global sorting should be unnecessary, since |
| 1380 | // as active edges are introduced, only local sorting should be required |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1381 | class ActiveEdge { |
| 1382 | public: |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1383 | // this logic must be kept in sync with tooCloseToCall |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1384 | // callers expect this to only read fAbove, fTangent |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 1385 | bool operator<(const ActiveEdge& rh) const { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1386 | if (fVerb == rh.fVerb) { |
| 1387 | // FIXME: don't know what to do if verb is quad, cubic |
| 1388 | return abCompare(fAbove, fBelow, rh.fAbove, rh.fBelow); |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 1389 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1390 | // figure out which is quad, line |
| 1391 | // if cached data says line did not intersect quad, use top/bottom |
| 1392 | if (fVerb != SkPath::kLine_Verb ? noIntersect(rh) : rh.noIntersect(*this)) { |
| 1393 | return abCompare(fAbove, fBelow, rh.fAbove, rh.fBelow); |
| 1394 | } |
| 1395 | // use whichever of top/tangent tangent/bottom overlaps more |
| 1396 | // with line top/bot |
| 1397 | // assumes quad/cubic can already be upconverted to cubic/cubic |
| 1398 | const SkPoint* line[2]; |
| 1399 | const SkPoint* curve[4]; |
| 1400 | if (fVerb != SkPath::kLine_Verb) { |
| 1401 | line[0] = &rh.fAbove; |
| 1402 | line[1] = &rh.fBelow; |
| 1403 | curve[0] = &fAbove; |
| 1404 | curve[1] = &fTangent; |
| 1405 | curve[2] = &fBelow; |
| 1406 | } else { |
| 1407 | line[0] = &fAbove; |
| 1408 | line[1] = &fBelow; |
| 1409 | curve[0] = &rh.fAbove; |
| 1410 | curve[1] = &rh.fTangent; |
| 1411 | curve[2] = &rh.fBelow; |
| 1412 | } |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1413 | // FIXME: code has been abandoned, incomplete.... |
| 1414 | return false; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | bool abCompare(const SkPoint& a1, const SkPoint& a2, const SkPoint& b1, |
| 1418 | const SkPoint& b2) const { |
| 1419 | double topD = a1.fX - b1.fX; |
| 1420 | if (b1.fY < a1.fY) { |
| 1421 | topD = (b2.fY - b1.fY) * topD - (a1.fY - b1.fY) * (b2.fX - b1.fX); |
| 1422 | } else if (b1.fY > a1.fY) { |
| 1423 | topD = (a2.fY - a1.fY) * topD + (b1.fY - a1.fY) * (a2.fX - a1.fX); |
| 1424 | } |
| 1425 | double botD = a2.fX - b2.fX; |
| 1426 | if (b2.fY > a2.fY) { |
| 1427 | botD = (b2.fY - b1.fY) * botD - (a2.fY - b2.fY) * (b2.fX - b1.fX); |
| 1428 | } else if (b2.fY < a2.fY) { |
| 1429 | botD = (a2.fY - a1.fY) * botD + (b2.fY - a2.fY) * (a2.fX - a1.fX); |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 1430 | } |
| 1431 | // return sign of greater absolute value |
| 1432 | return (fabs(topD) > fabs(botD) ? topD : botD) < 0; |
| 1433 | } |
| 1434 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1435 | // If a pair of edges are nearly coincident for some span, add a T in the |
| 1436 | // edge so it can be shortened to match the other edge. Note that another |
| 1437 | // approach is to trim the edge after it is added to the OutBuilder list -- |
| 1438 | // FIXME: since this has no effect if the edge is already done (i.e., |
| 1439 | // fYBottom >= y) maybe this can only be done by calling trimLine later. |
| 1440 | void addTatYBelow(SkScalar y) { |
| 1441 | if (fBelow.fY <= y || fYBottom >= y) { |
| 1442 | return; |
| 1443 | } |
| 1444 | addTatYInner(y); |
| 1445 | fFixBelow = true; |
| 1446 | } |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1447 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1448 | void addTatYAbove(SkScalar y) { |
| 1449 | if (fBelow.fY <= y) { |
| 1450 | return; |
| 1451 | } |
| 1452 | addTatYInner(y); |
| 1453 | } |
| 1454 | |
| 1455 | void addTatYInner(SkScalar y) { |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1456 | if (fWorkEdge.fPts[0].fY > y) { |
| 1457 | backup(y); |
| 1458 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1459 | SkScalar left = fWorkEdge.fPts[0].fX; |
| 1460 | SkScalar right = fWorkEdge.fPts[1].fX; |
| 1461 | if (left > right) { |
| 1462 | SkTSwap(left, right); |
| 1463 | } |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1464 | double ts[2]; |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1465 | SkASSERT(fWorkEdge.fVerb[0] == SkPath::kLine_Verb); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1466 | int pts = LineIntersect(fWorkEdge.fPts, left, right, y, ts); |
| 1467 | SkASSERT(pts == 1); |
| 1468 | // An ActiveEdge or WorkEdge has no need to modify the T values computed |
| 1469 | // in the InEdge, except in the following case. If a pair of edges are |
| 1470 | // nearly coincident, this may not be detected when the edges are |
| 1471 | // intersected. Later, when sorted, and this near-coincidence is found, |
| 1472 | // an additional t value must be added, requiring the cast below. |
| 1473 | InEdge* writable = const_cast<InEdge*>(fWorkEdge.fEdge); |
| 1474 | int insertedAt = writable->add(ts, pts, fWorkEdge.verbIndex()); |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1475 | #if DEBUG_ADJUST_COINCIDENT |
| 1476 | SkDebugf("%s edge=%d y=%1.9g t=%1.9g\n", __FUNCTION__, ID(), y, ts[0]); |
| 1477 | #endif |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1478 | if (insertedAt >= 0) { |
| 1479 | if (insertedAt + 1 < fTIndex) { |
| 1480 | SkASSERT(insertedAt + 2 == fTIndex); |
| 1481 | --fTIndex; |
| 1482 | } |
| 1483 | } |
| 1484 | } |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 1485 | |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 1486 | bool advanceT() { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1487 | SkASSERT(fTIndex <= fTs->count() - fExplicitTs); |
| 1488 | return ++fTIndex <= fTs->count() - fExplicitTs; |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 1489 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1490 | |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 1491 | bool advance() { |
| 1492 | // FIXME: flip sense of next |
| 1493 | bool result = fWorkEdge.advance(); |
| 1494 | fDone = !result; |
| 1495 | initT(); |
| 1496 | return result; |
| 1497 | } |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1498 | |
| 1499 | void backup(SkScalar y) { |
| 1500 | do { |
| 1501 | SkASSERT(fWorkEdge.fEdge->fVerbs.begin() < fWorkEdge.fVerb); |
| 1502 | fWorkEdge.fPts -= *--fWorkEdge.fVerb; |
| 1503 | SkASSERT(fWorkEdge.fEdge->fPts.begin() <= fWorkEdge.fPts); |
| 1504 | } while (fWorkEdge.fPts[0].fY >= y); |
| 1505 | initT(); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1506 | SkASSERT(!fExplicitTs); |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1507 | fTIndex = fTs->count() + 1; |
| 1508 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1509 | |
| 1510 | void calcAboveBelow(double tAbove, double tBelow) { |
| 1511 | fVerb = fWorkEdge.verb(); |
| 1512 | switch (fVerb) { |
| 1513 | case SkPath::kLine_Verb: |
| 1514 | LineXYAtT(fWorkEdge.fPts, tAbove, &fAbove); |
| 1515 | LineXYAtT(fWorkEdge.fPts, tBelow, &fTangent); |
| 1516 | fBelow = fTangent; |
| 1517 | break; |
| 1518 | case SkPath::kQuad_Verb: |
| 1519 | // FIXME: put array in struct to avoid copy? |
| 1520 | SkPoint quad[3]; |
| 1521 | QuadSubDivide(fWorkEdge.fPts, tAbove, tBelow, quad); |
| 1522 | fAbove = quad[0]; |
| 1523 | fTangent = quad[0] != quad[1] ? quad[1] : quad[2]; |
| 1524 | fBelow = quad[2]; |
| 1525 | break; |
| 1526 | case SkPath::kCubic_Verb: |
| 1527 | SkPoint cubic[3]; |
| 1528 | CubicSubDivide(fWorkEdge.fPts, tAbove, tBelow, cubic); |
| 1529 | fAbove = cubic[0]; |
| 1530 | // FIXME: can't see how quad logic for how tangent is used |
| 1531 | // extends to cubic |
| 1532 | fTangent = cubic[0] != cubic[1] ? cubic[1] |
| 1533 | : cubic[0] != cubic[2] ? cubic[2] : cubic[3]; |
| 1534 | fBelow = cubic[3]; |
| 1535 | break; |
| 1536 | default: |
| 1537 | SkASSERT(0); |
| 1538 | } |
| 1539 | } |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1540 | |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 1541 | void calcLeft(SkScalar y) { |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 1542 | // OPTIMIZE: put a kDone_Verb at the end of the verb list? |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 1543 | if (fDone || fBelow.fY > y) { |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 1544 | return; // nothing to do; use last |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 1545 | } |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 1546 | calcLeft(); |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1547 | if (fAbove.fY == fBelow.fY) { |
| 1548 | SkDebugf("%s edge=%d fAbove.fY != fBelow.fY %1.9g\n", __FUNCTION__, |
| 1549 | ID(), fAbove.fY); |
| 1550 | } |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 1551 | } |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1552 | |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 1553 | void calcLeft() { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1554 | int add = (fTIndex <= fTs->count() - fExplicitTs) - 1; |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 1555 | double tAbove = t(fTIndex + add); |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 1556 | double tBelow = t(fTIndex - ~add); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1557 | // OPTIMIZATION: if fAbove, fBelow have already been computed |
| 1558 | // for the fTIndex, don't do it again |
| 1559 | calcAboveBelow(tAbove, tBelow); |
| 1560 | // For identical x, this lets us know which edge is first. |
| 1561 | // If both edges have T values < 1, check x at next T (fBelow). |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 1562 | SkASSERT(tAbove != tBelow); |
| 1563 | // FIXME: this can loop forever |
| 1564 | // need a break if we hit the end |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1565 | // FIXME: in unit test, figure out how explicit Ts work as well |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 1566 | while (fAbove.fY == fBelow.fY) { |
| 1567 | if (add < 0 || fTIndex == fTs->count()) { |
| 1568 | add -= 1; |
| 1569 | SkASSERT(fTIndex + add >= 0); |
| 1570 | tAbove = t(fTIndex + add); |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 1571 | } else { |
| 1572 | add += 1; |
| 1573 | SkASSERT(fTIndex - ~add <= fTs->count() + 1); |
| 1574 | tBelow = t(fTIndex - ~add); |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 1575 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1576 | calcAboveBelow(tAbove, tBelow); |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 1577 | } |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 1578 | fTAbove = tAbove; |
| 1579 | fTBelow = tBelow; |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1582 | bool done(SkScalar bottom) const { |
| 1583 | return fDone || fYBottom >= bottom; |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 1584 | } |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1585 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1586 | void fixBelow() { |
| 1587 | if (fFixBelow) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1588 | fTBelow = nextT(); |
| 1589 | calcAboveBelow(fTAbove, fTBelow); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1590 | fFixBelow = false; |
| 1591 | } |
| 1592 | } |
| 1593 | |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1594 | void init(const InEdge* edge) { |
| 1595 | fWorkEdge.init(edge); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1596 | fDone = false; |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1597 | initT(); |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 1598 | fBelow.fY = SK_ScalarMin; |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 1599 | fYBottom = SK_ScalarMin; |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1600 | } |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1601 | |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1602 | void initT() { |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 1603 | const Intercepts& intercepts = fWorkEdge.fEdge->fIntercepts.front(); |
| 1604 | SkASSERT(fWorkEdge.verbIndex() <= fWorkEdge.fEdge->fIntercepts.count()); |
| 1605 | const Intercepts* interceptPtr = &intercepts + fWorkEdge.verbIndex(); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1606 | fTs = &interceptPtr->fTs; |
| 1607 | fExplicitTs = interceptPtr->fExplicit; |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 1608 | // the above is conceptually the same as |
| 1609 | // fTs = &fWorkEdge.fEdge->fIntercepts[fWorkEdge.verbIndex()].fTs; |
| 1610 | // but templated arrays don't allow returning a pointer to the end() element |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1611 | fTIndex = 0; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1612 | if (!fDone) { |
| 1613 | fVerb = fWorkEdge.verb(); |
| 1614 | } |
| 1615 | SkASSERT(fVerb > SkPath::kMove_Verb); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1616 | } |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1617 | |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 1618 | // OPTIMIZATION: record if two edges are coincident when the are intersected |
| 1619 | // It's unclear how to do this -- seems more complicated than recording the |
| 1620 | // t values, since the same t values could exist intersecting non-coincident |
| 1621 | // edges. |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1622 | bool isCoincidentWith(const ActiveEdge* edge) const { |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 1623 | if (fAbove != edge->fAbove || fBelow != edge->fBelow) { |
| 1624 | return false; |
| 1625 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1626 | if (fVerb != edge->fVerb) { |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 1627 | return false; |
| 1628 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1629 | switch (fVerb) { |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1630 | case SkPath::kLine_Verb: |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 1631 | return true; |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 1632 | default: |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1633 | // FIXME: add support for quads, cubics |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 1634 | SkASSERT(0); |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1635 | return false; |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 1636 | } |
| 1637 | return false; |
| 1638 | } |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1639 | |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1640 | bool isUnordered(const ActiveEdge* edge) const { |
| 1641 | return fAbove == edge->fAbove && fBelow == edge->fBelow; |
| 1642 | } |
| 1643 | |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1644 | // SkPath::Verb lastVerb() const { |
| 1645 | // return fDone ? fWorkEdge.lastVerb() : fWorkEdge.verb(); |
| 1646 | // } |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1647 | |
| 1648 | const SkPoint* lastPoints() const { |
| 1649 | return fDone ? fWorkEdge.lastPoints() : fWorkEdge.points(); |
| 1650 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1651 | |
| 1652 | bool noIntersect(const ActiveEdge& ) const { |
| 1653 | // incomplete |
| 1654 | return false; |
| 1655 | } |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1656 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1657 | // The shortest close call edge should be moved into a position where |
| 1658 | // it contributes if the winding is transitioning to or from zero. |
| 1659 | bool swapClose(const ActiveEdge* next, int prev, int wind, int mask) const { |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 1660 | #if DEBUG_ADJUST_COINCIDENT |
| 1661 | SkDebugf("%s edge=%d (%g) next=%d (%g) prev=%d wind=%d nextWind=%d\n", |
| 1662 | __FUNCTION__, ID(), fBelow.fY, next->ID(), next->fBelow.fY, |
| 1663 | prev, wind, wind + next->fWorkEdge.winding()); |
| 1664 | #endif |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1665 | if ((prev & mask) == 0 || (wind & mask) == 0) { |
| 1666 | return next->fBelow.fY < fBelow.fY; |
| 1667 | } |
| 1668 | int nextWinding = wind + next->fWorkEdge.winding(); |
| 1669 | if ((nextWinding & mask) == 0) { |
| 1670 | return fBelow.fY < next->fBelow.fY; |
| 1671 | } |
| 1672 | return false; |
| 1673 | } |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1674 | |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 1675 | bool swapCoincident(const ActiveEdge* edge, SkScalar bottom) const { |
| 1676 | if (fBelow.fY >= bottom || fDone || edge->fDone) { |
| 1677 | return false; |
| 1678 | } |
| 1679 | ActiveEdge thisWork = *this; |
| 1680 | ActiveEdge edgeWork = *edge; |
| 1681 | while ((thisWork.advanceT() || thisWork.advance()) |
| 1682 | && (edgeWork.advanceT() || edgeWork.advance())) { |
| 1683 | thisWork.calcLeft(); |
| 1684 | edgeWork.calcLeft(); |
| 1685 | if (thisWork < edgeWork) { |
| 1686 | return false; |
| 1687 | } |
| 1688 | if (edgeWork < thisWork) { |
| 1689 | return true; |
| 1690 | } |
| 1691 | } |
| 1692 | return false; |
| 1693 | } |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1694 | |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame^] | 1695 | bool swapUnordered(const ActiveEdge* edge, SkScalar /* bottom */) const { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1696 | SkASSERT(fVerb != SkPath::kLine_Verb |
| 1697 | || edge->fVerb != SkPath::kLine_Verb); |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1698 | if (fDone || edge->fDone) { |
| 1699 | return false; |
| 1700 | } |
| 1701 | ActiveEdge thisWork, edgeWork; |
| 1702 | extractAboveBelow(thisWork); |
| 1703 | edge->extractAboveBelow(edgeWork); |
| 1704 | return edgeWork < thisWork; |
| 1705 | } |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1706 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1707 | bool tooCloseToCall(const ActiveEdge* edge) const { |
| 1708 | int ulps; |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 1709 | double t1, t2, b1, b2; |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1710 | // This logic must be kept in sync with operator < |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 1711 | if (edge->fAbove.fY < fAbove.fY) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1712 | t1 = (edge->fTangent.fY - edge->fAbove.fY) * (fAbove.fX - edge->fAbove.fX); |
| 1713 | t2 = (fAbove.fY - edge->fAbove.fY) * (edge->fTangent.fX - edge->fAbove.fX); |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 1714 | } else if (edge->fAbove.fY > fAbove.fY) { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1715 | t1 = (fTangent.fY - fAbove.fY) * (fAbove.fX - edge->fAbove.fX); |
| 1716 | t2 = (fAbove.fY - edge->fAbove.fY) * (fTangent.fX - fAbove.fX); |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 1717 | } else { |
| 1718 | t1 = fAbove.fX; |
| 1719 | t2 = edge->fAbove.fX; |
| 1720 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1721 | if (edge->fTangent.fY > fTangent.fY) { |
| 1722 | b1 = (edge->fTangent.fY - edge->fAbove.fY) * (fTangent.fX - edge->fTangent.fX); |
| 1723 | b2 = (fTangent.fY - edge->fTangent.fY) * (edge->fTangent.fX - edge->fAbove.fX); |
| 1724 | } else if (edge->fTangent.fY < fTangent.fY) { |
| 1725 | b1 = (fTangent.fY - fAbove.fY) * (fTangent.fX - edge->fTangent.fX); |
| 1726 | b2 = (fTangent.fY - edge->fTangent.fY) * (fTangent.fX - fAbove.fX); |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 1727 | } else { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1728 | b1 = fTangent.fX; |
| 1729 | b2 = edge->fTangent.fX; |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 1730 | } |
| 1731 | if (fabs(t1 - t2) > fabs(b1 - b2)) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1732 | ulps = UlpsDiff((float) t1, (float) t2); |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 1733 | } else { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 1734 | ulps = UlpsDiff((float) b1, (float) b2); |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 1735 | } |
| 1736 | #if DEBUG_ADJUST_COINCIDENT |
| 1737 | SkDebugf("%s this=%d edge=%d ulps=%d\n", __FUNCTION__, ID(), edge->ID(), |
| 1738 | ulps); |
| 1739 | #endif |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1740 | if (ulps < 0 || ulps > 32) { |
| 1741 | return false; |
| 1742 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1743 | if (fVerb == SkPath::kLine_Verb && edge->fVerb == SkPath::kLine_Verb) { |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1744 | return true; |
| 1745 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1746 | if (fVerb != SkPath::kLine_Verb && edge->fVerb != SkPath::kLine_Verb) { |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1747 | return false; |
| 1748 | } |
| 1749 | |
| 1750 | double ts[2]; |
| 1751 | bool isLine = true; |
| 1752 | bool curveQuad = true; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1753 | if (fVerb == SkPath::kCubic_Verb) { |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1754 | ts[0] = (fTAbove * 2 + fTBelow) / 3; |
| 1755 | ts[1] = (fTAbove + fTBelow * 2) / 3; |
| 1756 | curveQuad = isLine = false; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1757 | } else if (edge->fVerb == SkPath::kCubic_Verb) { |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1758 | ts[0] = (edge->fTAbove * 2 + edge->fTBelow) / 3; |
| 1759 | ts[1] = (edge->fTAbove + edge->fTBelow * 2) / 3; |
| 1760 | curveQuad = false; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1761 | } else if (fVerb == SkPath::kQuad_Verb) { |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1762 | ts[0] = fTAbove; |
| 1763 | ts[1] = (fTAbove + fTBelow) / 2; |
| 1764 | isLine = false; |
| 1765 | } else { |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1766 | SkASSERT(edge->fVerb == SkPath::kQuad_Verb); |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1767 | ts[0] = edge->fTAbove; |
| 1768 | ts[1] = (edge->fTAbove + edge->fTBelow) / 2; |
| 1769 | } |
| 1770 | const SkPoint* curvePts = isLine ? edge->lastPoints() : lastPoints(); |
| 1771 | const ActiveEdge* lineEdge = isLine ? this : edge; |
| 1772 | SkPoint curveSample[2]; |
| 1773 | for (int index = 0; index < 2; ++index) { |
| 1774 | if (curveQuad) { |
| 1775 | QuadXYAtT(curvePts, ts[index], &curveSample[index]); |
| 1776 | } else { |
| 1777 | CubicXYAtT(curvePts, ts[index], &curveSample[index]); |
| 1778 | } |
| 1779 | } |
| 1780 | return IsCoincident(curveSample, lineEdge->fAbove, lineEdge->fBelow); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1781 | } |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1782 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1783 | double nextT() const { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1784 | SkASSERT(fTIndex <= fTs->count() - fExplicitTs); |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 1785 | return t(fTIndex + 1); |
| 1786 | } |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1787 | |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 1788 | double t() const { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1789 | return t(fTIndex); |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 1792 | double t(int tIndex) const { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1793 | if (fExplicitTs) { |
| 1794 | SkASSERT(tIndex < fTs->count()); |
| 1795 | return (*fTs)[tIndex]; |
| 1796 | } |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 1797 | if (tIndex == 0) { |
| 1798 | return 0; |
| 1799 | } |
| 1800 | if (tIndex > fTs->count()) { |
| 1801 | return 1; |
| 1802 | } |
| 1803 | return (*fTs)[tIndex - 1]; |
| 1804 | } |
| 1805 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1806 | // FIXME: debugging only |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1807 | int ID() const { |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1808 | return fWorkEdge.fEdge->fID; |
| 1809 | } |
| 1810 | |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1811 | private: |
| 1812 | // utility used only by swapUnordered |
| 1813 | void extractAboveBelow(ActiveEdge& extracted) const { |
| 1814 | SkPoint curve[4]; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1815 | switch (fVerb) { |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1816 | case SkPath::kLine_Verb: |
| 1817 | extracted.fAbove = fAbove; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1818 | extracted.fTangent = fTangent; |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1819 | return; |
| 1820 | case SkPath::kQuad_Verb: |
| 1821 | QuadSubDivide(lastPoints(), fTAbove, fTBelow, curve); |
| 1822 | break; |
| 1823 | case SkPath::kCubic_Verb: |
| 1824 | CubicSubDivide(lastPoints(), fTAbove, fTBelow, curve); |
| 1825 | break; |
| 1826 | default: |
| 1827 | SkASSERT(0); |
| 1828 | } |
| 1829 | extracted.fAbove = curve[0]; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1830 | extracted.fTangent = curve[1]; |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1833 | public: |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1834 | WorkEdge fWorkEdge; |
| 1835 | const SkTDArray<double>* fTs; |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 1836 | SkPoint fAbove; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1837 | SkPoint fTangent; |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 1838 | SkPoint fBelow; |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 1839 | double fTAbove; // OPTIMIZATION: only required if edge has quads or cubics |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1840 | double fTBelow; |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 1841 | SkScalar fYBottom; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1842 | int fCoincident; |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1843 | int fTIndex; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 1844 | SkPath::Verb fVerb; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1845 | bool fSkip; // OPTIMIZATION: use bitfields? |
| 1846 | bool fCloseCall; |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 1847 | bool fDone; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1848 | bool fFixBelow; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1849 | bool fExplicitTs; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 1850 | }; |
| 1851 | |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1852 | static void addToActive(SkTDArray<ActiveEdge>& activeEdges, const InEdge* edge) { |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1853 | size_t count = activeEdges.count(); |
| 1854 | for (size_t index = 0; index < count; ++index) { |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 1855 | if (edge == activeEdges[index].fWorkEdge.fEdge) { |
| 1856 | return; |
| 1857 | } |
| 1858 | } |
| 1859 | ActiveEdge* active = activeEdges.append(); |
| 1860 | active->init(edge); |
| 1861 | } |
| 1862 | |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 1863 | // Find any intersections in the range of active edges. A pair of edges, on |
| 1864 | // either side of another edge, may change the winding contribution for part of |
| 1865 | // the edge. |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1866 | // Keep horizontal edges just for |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 1867 | // the purpose of computing when edges change their winding contribution, since |
| 1868 | // this is essentially computing the horizontal intersection. |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1869 | static void addBottomT(InEdge** currentPtr, InEdge** lastPtr, |
| 1870 | HorizontalEdge** horizontal) { |
| 1871 | InEdge** testPtr = currentPtr - 1; |
| 1872 | HorizontalEdge* horzEdge = *horizontal; |
| 1873 | SkScalar left = horzEdge->fLeft; |
| 1874 | SkScalar bottom = horzEdge->fY; |
| 1875 | while (++testPtr != lastPtr) { |
| 1876 | InEdge* test = *testPtr; |
| 1877 | if (test->fBounds.fBottom <= bottom || test->fBounds.fRight <= left) { |
| 1878 | continue; |
| 1879 | } |
| 1880 | WorkEdge wt; |
| 1881 | wt.init(test); |
| 1882 | do { |
| 1883 | HorizontalEdge** sorted = horizontal; |
| 1884 | horzEdge = *sorted; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 1885 | do { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1886 | double wtTs[4]; |
| 1887 | int pts; |
| 1888 | uint8_t verb = wt.verb(); |
| 1889 | switch (verb) { |
| 1890 | case SkPath::kLine_Verb: |
| 1891 | pts = LineIntersect(wt.fPts, horzEdge->fLeft, |
| 1892 | horzEdge->fRight, horzEdge->fY, wtTs); |
| 1893 | break; |
| 1894 | case SkPath::kQuad_Verb: |
| 1895 | pts = QuadIntersect(wt.fPts, horzEdge->fLeft, |
| 1896 | horzEdge->fRight, horzEdge->fY, wtTs); |
| 1897 | break; |
| 1898 | case SkPath::kCubic_Verb: |
| 1899 | pts = CubicIntersect(wt.fPts, horzEdge->fLeft, |
| 1900 | horzEdge->fRight, horzEdge->fY, wtTs); |
| 1901 | break; |
| 1902 | } |
| 1903 | if (pts) { |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1904 | #if DEBUG_ADD_BOTTOM_TS |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1905 | for (int x = 0; x < pts; ++x) { |
| 1906 | SkDebugf("%s y=%g wtTs[0]=%g (%g,%g", __FUNCTION__, |
| 1907 | horzEdge->fY, wtTs[x], wt.fPts[0].fX, wt.fPts[0].fY); |
| 1908 | for (int y = 0; y < verb; ++y) { |
| 1909 | SkDebugf(" %g,%g", wt.fPts[y + 1].fX, wt.fPts[y + 1].fY)); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1910 | } |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1911 | SkDebugf(")\n"); |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 1912 | } |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1913 | if (pts > verb) { |
| 1914 | SkASSERT(0); // FIXME ? should this work? |
| 1915 | SkDebugf("%s wtTs[1]=%g\n", __FUNCTION__, wtTs[1]); |
| 1916 | } |
| 1917 | #endif |
| 1918 | test->add(wtTs, pts, wt.verbIndex()); |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 1919 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 1920 | horzEdge = *++sorted; |
| 1921 | } while (horzEdge->fY == bottom |
| 1922 | && horzEdge->fLeft <= test->fBounds.fRight); |
| 1923 | } while (wt.advance()); |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 1924 | } |
| 1925 | } |
| 1926 | |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame^] | 1927 | #if DEBUG_ADD_INTERSECTING_TS |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 1928 | static void debugShowLineIntersection(int pts, const WorkEdge& wt, |
| 1929 | const WorkEdge& wn, const double wtTs[2], const double wnTs[2]) { |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 1930 | if (!pts) { |
| 1931 | return; |
| 1932 | } |
| 1933 | SkPoint wtOutPt, wnOutPt; |
| 1934 | LineXYAtT(wt.fPts, wtTs[0], &wtOutPt); |
| 1935 | LineXYAtT(wn.fPts, wnTs[0], &wnOutPt); |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 1936 | SkDebugf("%s wtTs[0]=%g (%g,%g, %g,%g) (%g,%g)\n", |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 1937 | __FUNCTION__, |
| 1938 | wtTs[0], wt.fPts[0].fX, wt.fPts[0].fY, |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 1939 | wt.fPts[1].fX, wt.fPts[1].fY, wtOutPt.fX, wtOutPt.fY); |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 1940 | if (pts == 2) { |
| 1941 | SkDebugf("%s wtTs[1]=%g\n", __FUNCTION__, wtTs[1]); |
| 1942 | } |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 1943 | SkDebugf("%s wnTs[0]=%g (%g,%g, %g,%g) (%g,%g)\n", |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 1944 | __FUNCTION__, |
| 1945 | wnTs[0], wn.fPts[0].fX, wn.fPts[0].fY, |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 1946 | wn.fPts[1].fX, wn.fPts[1].fY, wnOutPt.fX, wnOutPt.fY); |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 1947 | if (pts == 2) { |
| 1948 | SkDebugf("%s wnTs[1]=%g\n", __FUNCTION__, wnTs[1]); |
| 1949 | } |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 1950 | } |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame^] | 1951 | #else |
| 1952 | static void debugShowLineIntersection(int , const WorkEdge& , |
| 1953 | const WorkEdge& , const double [2], const double [2]) { |
| 1954 | } |
| 1955 | #endif |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 1956 | |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 1957 | static void addIntersectingTs(InEdge** currentPtr, InEdge** lastPtr) { |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 1958 | InEdge** testPtr = currentPtr - 1; |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1959 | // FIXME: lastPtr should be past the point of interest, so |
| 1960 | // test below should be lastPtr - 2 |
| 1961 | // that breaks testSimplifyTriangle22, so further investigation is needed |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 1962 | while (++testPtr != lastPtr - 1) { |
| 1963 | InEdge* test = *testPtr; |
| 1964 | InEdge** nextPtr = testPtr; |
| 1965 | do { |
| 1966 | InEdge* next = *++nextPtr; |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 1967 | // FIXME: this compares against the sentinel sometimes |
| 1968 | // OPTIMIZATION: this may never be needed since this gets called |
| 1969 | // in two passes now. Verify that double hits are appropriate. |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 1970 | if (test->cached(next)) { |
| 1971 | continue; |
| 1972 | } |
| 1973 | if (!Bounds::Intersects(test->fBounds, next->fBounds)) { |
| 1974 | continue; |
| 1975 | } |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 1976 | WorkEdge wt, wn; |
| 1977 | wt.init(test); |
| 1978 | wn.init(next); |
| 1979 | do { |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 1980 | int pts; |
| 1981 | Intersections ts; |
| 1982 | bool swap = false; |
| 1983 | switch (wt.verb()) { |
| 1984 | case SkPath::kLine_Verb: |
| 1985 | switch (wn.verb()) { |
| 1986 | case SkPath::kLine_Verb: { |
| 1987 | pts = LineIntersect(wt.fPts, wn.fPts, ts); |
| 1988 | debugShowLineIntersection(pts, wt, wn, |
| 1989 | ts.fT[0], ts.fT[1]); |
| 1990 | break; |
| 1991 | } |
| 1992 | case SkPath::kQuad_Verb: { |
| 1993 | swap = true; |
| 1994 | pts = QuadLineIntersect(wn.fPts, wt.fPts, ts); |
| 1995 | break; |
| 1996 | } |
| 1997 | case SkPath::kCubic_Verb: { |
| 1998 | swap = true; |
| 1999 | pts = CubicLineIntersect(wn.fPts, wt.fPts, ts); |
| 2000 | break; |
| 2001 | } |
| 2002 | default: |
| 2003 | SkASSERT(0); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2004 | } |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 2005 | break; |
| 2006 | case SkPath::kQuad_Verb: |
| 2007 | switch (wn.verb()) { |
| 2008 | case SkPath::kLine_Verb: { |
| 2009 | pts = QuadLineIntersect(wt.fPts, wn.fPts, ts); |
| 2010 | break; |
| 2011 | } |
| 2012 | case SkPath::kQuad_Verb: { |
| 2013 | pts = QuadIntersect(wt.fPts, wn.fPts, ts); |
| 2014 | break; |
| 2015 | } |
| 2016 | case SkPath::kCubic_Verb: { |
| 2017 | // FIXME: promote quad to cubic |
| 2018 | pts = CubicIntersect(wt.fPts, wn.fPts, ts); |
| 2019 | break; |
| 2020 | } |
| 2021 | default: |
| 2022 | SkASSERT(0); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2023 | } |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 2024 | break; |
| 2025 | case SkPath::kCubic_Verb: |
| 2026 | switch (wn.verb()) { |
| 2027 | case SkPath::kLine_Verb: { |
| 2028 | pts = CubicLineIntersect(wt.fPts, wn.fPts, ts); |
| 2029 | break; |
| 2030 | } |
| 2031 | case SkPath::kQuad_Verb: { |
| 2032 | // FIXME: promote quad to cubic |
| 2033 | pts = CubicIntersect(wt.fPts, wn.fPts, ts); |
| 2034 | break; |
| 2035 | } |
| 2036 | case SkPath::kCubic_Verb: { |
| 2037 | pts = CubicIntersect(wt.fPts, wn.fPts, ts); |
| 2038 | break; |
| 2039 | } |
| 2040 | default: |
| 2041 | SkASSERT(0); |
| 2042 | } |
| 2043 | break; |
| 2044 | default: |
| 2045 | SkASSERT(0); |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2046 | } |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 2047 | test->add(ts.fT[swap], pts, wt.verbIndex()); |
| 2048 | next->add(ts.fT[!swap], pts, wn.verbIndex()); |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 2049 | } while (wt.bottom() <= wn.bottom() ? wt.advance() : wn.advance()); |
| 2050 | } while (nextPtr != lastPtr); |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2051 | } |
| 2052 | } |
| 2053 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2054 | static InEdge** advanceEdges(SkTDArray<ActiveEdge>* activeEdges, |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 2055 | InEdge** currentPtr, InEdge** lastPtr, SkScalar y) { |
| 2056 | InEdge** testPtr = currentPtr - 1; |
| 2057 | while (++testPtr != lastPtr) { |
| 2058 | if ((*testPtr)->fBounds.fBottom > y) { |
| 2059 | continue; |
| 2060 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2061 | if (activeEdges) { |
| 2062 | InEdge* test = *testPtr; |
| 2063 | ActiveEdge* activePtr = activeEdges->begin() - 1; |
| 2064 | ActiveEdge* lastActive = activeEdges->end(); |
| 2065 | while (++activePtr != lastActive) { |
| 2066 | if (activePtr->fWorkEdge.fEdge == test) { |
| 2067 | activeEdges->remove(activePtr - activeEdges->begin()); |
| 2068 | break; |
| 2069 | } |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 2070 | } |
| 2071 | } |
| 2072 | if (testPtr == currentPtr) { |
| 2073 | ++currentPtr; |
| 2074 | } |
| 2075 | } |
| 2076 | return currentPtr; |
| 2077 | } |
| 2078 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2079 | // OPTIMIZE: inline? |
| 2080 | static HorizontalEdge** advanceHorizontal(HorizontalEdge** edge, SkScalar y) { |
| 2081 | while ((*edge)->fY < y) { |
| 2082 | ++edge; |
| 2083 | } |
| 2084 | return edge; |
| 2085 | } |
| 2086 | |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2087 | // compute bottom taking into account any intersected edges |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2088 | static SkScalar computeInterceptBottom(SkTDArray<ActiveEdge>& activeEdges, |
| 2089 | SkScalar y, SkScalar bottom) { |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2090 | ActiveEdge* activePtr = activeEdges.begin() - 1; |
| 2091 | ActiveEdge* lastActive = activeEdges.end(); |
| 2092 | while (++activePtr != lastActive) { |
| 2093 | const InEdge* test = activePtr->fWorkEdge.fEdge; |
| 2094 | if (!test->fContainsIntercepts) { |
| 2095 | continue; |
| 2096 | } |
| 2097 | WorkEdge wt; |
| 2098 | wt.init(test); |
| 2099 | do { |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2100 | const Intercepts& intercepts = test->fIntercepts[wt.verbIndex()]; |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 2101 | if (intercepts.fTopIntercepts > 1) { |
| 2102 | SkScalar yTop = wt.fPts[0].fY; |
| 2103 | if (yTop > y && bottom > yTop) { |
| 2104 | bottom = yTop; |
| 2105 | } |
| 2106 | } |
| 2107 | if (intercepts.fBottomIntercepts > 1) { |
| 2108 | SkScalar yBottom = wt.fPts[wt.verb()].fY; |
| 2109 | if (yBottom > y && bottom > yBottom) { |
| 2110 | bottom = yBottom; |
| 2111 | } |
| 2112 | } |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2113 | const SkTDArray<double>& fTs = intercepts.fTs; |
| 2114 | size_t count = fTs.count(); |
| 2115 | for (size_t index = 0; index < count; ++index) { |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 2116 | SkScalar yIntercept; |
| 2117 | switch (wt.verb()) { |
| 2118 | case SkPath::kLine_Verb: { |
| 2119 | yIntercept = LineYAtT(wt.fPts, fTs[index]); |
| 2120 | break; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2121 | } |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 2122 | case SkPath::kQuad_Verb: { |
| 2123 | yIntercept = QuadYAtT(wt.fPts, fTs[index]); |
| 2124 | break; |
| 2125 | } |
| 2126 | case SkPath::kCubic_Verb: { |
| 2127 | yIntercept = CubicYAtT(wt.fPts, fTs[index]); |
| 2128 | break; |
| 2129 | } |
| 2130 | default: |
| 2131 | SkASSERT(0); // should never get here |
| 2132 | } |
| 2133 | if (yIntercept > y && bottom > yIntercept) { |
| 2134 | bottom = yIntercept; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2135 | } |
| 2136 | } |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 2137 | } while (wt.advance()); |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2138 | } |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 2139 | #if DEBUG_BOTTOM |
| 2140 | SkDebugf("%s bottom=%1.9g\n", __FUNCTION__, bottom); |
| 2141 | #endif |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2142 | return bottom; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | static SkScalar findBottom(InEdge** currentPtr, |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2146 | InEdge** edgeListEnd, SkTDArray<ActiveEdge>* activeEdges, SkScalar y, |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame^] | 2147 | bool /*asFill*/, InEdge**& testPtr) { |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2148 | InEdge* current = *currentPtr; |
| 2149 | SkScalar bottom = current->fBounds.fBottom; |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 2150 | |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2151 | // find the list of edges that cross y |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 2152 | InEdge* test = *testPtr; |
| 2153 | while (testPtr != edgeListEnd) { |
| 2154 | SkScalar testTop = test->fBounds.fTop; |
| 2155 | if (bottom <= testTop) { |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2156 | break; |
| 2157 | } |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 2158 | SkScalar testBottom = test->fBounds.fBottom; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2159 | // OPTIMIZATION: Shortening the bottom is only interesting when filling |
| 2160 | // and when the edge is to the left of a longer edge. If it's a framing |
| 2161 | // edge, or part of the right, it won't effect the longer edges. |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 2162 | if (testTop > y) { |
| 2163 | bottom = testTop; |
| 2164 | break; |
| 2165 | } |
| 2166 | if (y < testBottom) { |
| 2167 | if (bottom > testBottom) { |
| 2168 | bottom = testBottom; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2169 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2170 | if (activeEdges) { |
| 2171 | addToActive(*activeEdges, test); |
| 2172 | } |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2173 | } |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 2174 | test = *++testPtr; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2175 | } |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 2176 | #if DEBUG_BOTTOM |
| 2177 | SkDebugf("%s %d bottom=%1.9g\n", __FUNCTION__, activeEdges ? 2 : 1, bottom); |
| 2178 | #endif |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2179 | return bottom; |
| 2180 | } |
| 2181 | |
| 2182 | static void makeEdgeList(SkTArray<InEdge>& edges, InEdge& edgeSentinel, |
| 2183 | SkTDArray<InEdge*>& edgeList) { |
| 2184 | size_t edgeCount = edges.count(); |
| 2185 | if (edgeCount == 0) { |
| 2186 | return; |
| 2187 | } |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 2188 | int id = 0; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2189 | for (size_t index = 0; index < edgeCount; ++index) { |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 2190 | InEdge& edge = edges[index]; |
| 2191 | if (!edge.fWinding) { |
| 2192 | continue; |
| 2193 | } |
| 2194 | edge.fID = ++id; |
| 2195 | *edgeList.append() = &edge; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2196 | } |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2197 | *edgeList.append() = &edgeSentinel; |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 2198 | QSort<InEdge>(edgeList.begin(), edgeList.end() - 1); |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2201 | static void makeHorizontalList(SkTDArray<HorizontalEdge>& edges, |
| 2202 | HorizontalEdge& edgeSentinel, SkTDArray<HorizontalEdge*>& edgeList) { |
| 2203 | size_t edgeCount = edges.count(); |
| 2204 | if (edgeCount == 0) { |
| 2205 | return; |
| 2206 | } |
| 2207 | for (size_t index = 0; index < edgeCount; ++index) { |
| 2208 | *edgeList.append() = &edges[index]; |
| 2209 | } |
| 2210 | edgeSentinel.fLeft = edgeSentinel.fRight = edgeSentinel.fY = SK_ScalarMax; |
| 2211 | *edgeList.append() = &edgeSentinel; |
| 2212 | QSort<HorizontalEdge>(edgeList.begin(), edgeList.end() - 1); |
| 2213 | } |
caryclark@google.com | 6b9cfb3 | 2012-02-16 21:24:41 +0000 | [diff] [blame] | 2214 | |
| 2215 | static void skipCoincidence(int lastWinding, int winding, int windingMask, |
| 2216 | ActiveEdge* activePtr, ActiveEdge* firstCoincident) { |
| 2217 | if (((lastWinding & windingMask) == 0) ^ (winding & windingMask) != 0) { |
| 2218 | return; |
| 2219 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2220 | // FIXME: ? shouldn't this be if (lastWinding & windingMask) ? |
caryclark@google.com | 6b9cfb3 | 2012-02-16 21:24:41 +0000 | [diff] [blame] | 2221 | if (lastWinding) { |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 2222 | #if DEBUG_ADJUST_COINCIDENT |
| 2223 | SkDebugf("%s edge=%d 1 set skip=false\n", __FUNCTION__, activePtr->ID()); |
| 2224 | #endif |
caryclark@google.com | 6b9cfb3 | 2012-02-16 21:24:41 +0000 | [diff] [blame] | 2225 | activePtr->fSkip = false; |
| 2226 | } else { |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 2227 | #if DEBUG_ADJUST_COINCIDENT |
| 2228 | SkDebugf("%s edge=%d 2 set skip=false\n", __FUNCTION__, firstCoincident->ID()); |
| 2229 | #endif |
caryclark@google.com | 6b9cfb3 | 2012-02-16 21:24:41 +0000 | [diff] [blame] | 2230 | firstCoincident->fSkip = false; |
| 2231 | } |
| 2232 | } |
| 2233 | |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 2234 | static void sortHorizontal(SkTDArray<ActiveEdge>& activeEdges, |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2235 | SkTDArray<ActiveEdge*>& edgeList, SkScalar y) { |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 2236 | size_t edgeCount = activeEdges.count(); |
| 2237 | if (edgeCount == 0) { |
| 2238 | return; |
| 2239 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2240 | #if DEBUG_SORT_HORIZONTAL |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 2241 | const int tab = 3; // FIXME: debugging only |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2242 | SkDebugf("%s y=%1.9g\n", __FUNCTION__, y); |
| 2243 | #endif |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 2244 | size_t index; |
| 2245 | for (index = 0; index < edgeCount; ++index) { |
| 2246 | ActiveEdge& activeEdge = activeEdges[index]; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2247 | do { |
| 2248 | activeEdge.calcLeft(y); |
| 2249 | // skip segments that don't span y |
| 2250 | if (activeEdge.fAbove != activeEdge.fBelow) { |
| 2251 | break; |
| 2252 | } |
| 2253 | if (activeEdge.fDone) { |
| 2254 | #if DEBUG_SORT_HORIZONTAL |
| 2255 | SkDebugf("%*s edge=%d done\n", tab, "", activeEdge.ID()); |
| 2256 | #endif |
| 2257 | goto nextEdge; |
| 2258 | } |
| 2259 | #if DEBUG_SORT_HORIZONTAL |
| 2260 | SkDebugf("%*s edge=%d above==below\n", tab, "", activeEdge.ID()); |
| 2261 | #endif |
| 2262 | } while (activeEdge.advanceT() || activeEdge.advance()); |
| 2263 | #if DEBUG_SORT_HORIZONTAL |
| 2264 | SkDebugf("%*s edge=%d above=(%1.9g,%1.9g) (%1.9g) below=(%1.9g,%1.9g)" |
| 2265 | " (%1.9g)\n", tab, "", activeEdge.ID(), |
| 2266 | activeEdge.fAbove.fX, activeEdge.fAbove.fY, activeEdge.fTAbove, |
| 2267 | activeEdge.fBelow.fX, activeEdge.fBelow.fY, activeEdge.fTBelow); |
| 2268 | #endif |
| 2269 | activeEdge.fSkip = activeEdge.fCloseCall = activeEdge.fFixBelow = false; |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 2270 | *edgeList.append() = &activeEdge; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2271 | nextEdge: |
| 2272 | ; |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 2273 | } |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 2274 | QSort<ActiveEdge>(edgeList.begin(), edgeList.end() - 1); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | // remove coincident edges |
| 2278 | // OPTIMIZE: remove edges? This is tricky because the current logic expects |
| 2279 | // the winding count to be maintained while skipping coincident edges. In |
| 2280 | // addition to removing the coincident edges, the remaining edges would need |
| 2281 | // to have a different winding value, possibly different per intercept span. |
| 2282 | static SkScalar adjustCoincident(SkTDArray<ActiveEdge*>& edgeList, |
| 2283 | int windingMask, SkScalar y, SkScalar bottom, OutEdgeBuilder& outBuilder) |
| 2284 | { |
| 2285 | #if DEBUG_ADJUST_COINCIDENT |
| 2286 | SkDebugf("%s y=%1.9g bottom=%1.9g\n", __FUNCTION__, y, bottom); |
| 2287 | #endif |
| 2288 | size_t edgeCount = edgeList.count(); |
| 2289 | if (edgeCount == 0) { |
| 2290 | return bottom; |
| 2291 | } |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 2292 | ActiveEdge* activePtr, * nextPtr = edgeList[0]; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2293 | size_t index; |
| 2294 | bool foundCoincident = false; |
| 2295 | int firstIndex = 0; |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 2296 | for (index = 1; index < edgeCount; ++index) { |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 2297 | activePtr = nextPtr; |
| 2298 | nextPtr = edgeList[index]; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2299 | if (firstIndex != index - 1 && activePtr->fVerb > SkPath::kLine_Verb |
| 2300 | && nextPtr->fVerb == SkPath::kLine_Verb |
| 2301 | && activePtr->isUnordered(nextPtr)) { |
| 2302 | // swap the line with the curve |
| 2303 | // back up to the previous edge and retest |
| 2304 | SkTSwap<ActiveEdge*>(edgeList[index - 1], edgeList[index]); |
| 2305 | SkASSERT(index > 1); |
| 2306 | index -= 2; |
| 2307 | nextPtr = edgeList[index]; |
| 2308 | continue; |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 2309 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2310 | bool closeCall = false; |
| 2311 | activePtr->fCoincident = firstIndex; |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 2312 | if (activePtr->isCoincidentWith(nextPtr) |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2313 | || (closeCall = activePtr->tooCloseToCall(nextPtr))) { |
| 2314 | activePtr->fSkip = nextPtr->fSkip = foundCoincident = true; |
| 2315 | activePtr->fCloseCall = nextPtr->fCloseCall = closeCall; |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 2316 | } else if (activePtr->isUnordered(nextPtr)) { |
| 2317 | foundCoincident = true; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2318 | } else { |
| 2319 | firstIndex = index; |
| 2320 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2321 | } |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 2322 | nextPtr->fCoincident = firstIndex; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2323 | if (!foundCoincident) { |
| 2324 | return bottom; |
| 2325 | } |
| 2326 | int winding = 0; |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 2327 | nextPtr = edgeList[0]; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2328 | for (index = 1; index < edgeCount; ++index) { |
| 2329 | int priorWinding = winding; |
caryclark@google.com | 6b9cfb3 | 2012-02-16 21:24:41 +0000 | [diff] [blame] | 2330 | winding += activePtr->fWorkEdge.winding(); |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 2331 | activePtr = nextPtr; |
| 2332 | nextPtr = edgeList[index]; |
| 2333 | SkASSERT(activePtr == edgeList[index - 1]); |
| 2334 | SkASSERT(nextPtr == edgeList[index]); |
| 2335 | if (activePtr->fCoincident != nextPtr->fCoincident) { |
| 2336 | continue; |
| 2337 | } |
| 2338 | // the coincident edges may not have been sorted above -- advance |
| 2339 | // the edges and resort if needed |
| 2340 | // OPTIMIZE: if sorting is done incrementally as new edges are added |
| 2341 | // and not all at once as is done here, fold this test into the |
| 2342 | // current less than test. |
| 2343 | while ((!activePtr->fSkip || !nextPtr->fSkip) |
| 2344 | && activePtr->fCoincident == nextPtr->fCoincident) { |
| 2345 | if (activePtr->swapUnordered(nextPtr, bottom)) { |
| 2346 | winding -= activePtr->fWorkEdge.winding(); |
| 2347 | SkASSERT(activePtr == edgeList[index - 1]); |
| 2348 | SkASSERT(nextPtr == edgeList[index]); |
| 2349 | SkTSwap<ActiveEdge*>(edgeList[index - 1], edgeList[index]); |
| 2350 | if (--index == 0) { |
| 2351 | winding += activePtr->fWorkEdge.winding(); |
| 2352 | break; |
| 2353 | } |
| 2354 | // back up one |
| 2355 | activePtr = edgeList[index - 1]; |
| 2356 | continue; |
| 2357 | } |
| 2358 | SkASSERT(activePtr == edgeList[index - 1]); |
| 2359 | SkASSERT(nextPtr == edgeList[index]); |
| 2360 | break; |
| 2361 | } |
| 2362 | if (activePtr->fSkip && nextPtr->fSkip) { |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2363 | if (activePtr->fCloseCall ? activePtr->swapClose(nextPtr, |
| 2364 | priorWinding, winding, windingMask) |
| 2365 | : activePtr->swapCoincident(nextPtr, bottom)) { |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 2366 | winding -= activePtr->fWorkEdge.winding(); |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 2367 | SkASSERT(activePtr == edgeList[index - 1]); |
| 2368 | SkASSERT(nextPtr == edgeList[index]); |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 2369 | SkTSwap<ActiveEdge*>(edgeList[index - 1], edgeList[index]); |
| 2370 | SkTSwap<ActiveEdge*>(activePtr, nextPtr); |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 2371 | winding += activePtr->fWorkEdge.winding(); |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 2372 | SkASSERT(activePtr == edgeList[index - 1]); |
| 2373 | SkASSERT(nextPtr == edgeList[index]); |
caryclark@google.com | cd4421d | 2012-03-01 19:16:31 +0000 | [diff] [blame] | 2374 | } |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2375 | } |
| 2376 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2377 | int firstCoincidentWinding = 0; |
| 2378 | ActiveEdge* firstCoincident = NULL; |
| 2379 | winding = 0; |
| 2380 | activePtr = edgeList[0]; |
| 2381 | for (index = 1; index < edgeCount; ++index) { |
| 2382 | int priorWinding = winding; |
caryclark@google.com | 6b9cfb3 | 2012-02-16 21:24:41 +0000 | [diff] [blame] | 2383 | winding += activePtr->fWorkEdge.winding(); |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 2384 | nextPtr = edgeList[index]; |
| 2385 | if (activePtr->fSkip && nextPtr->fSkip |
| 2386 | && activePtr->fCoincident == nextPtr->fCoincident) { |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2387 | if (!firstCoincident) { |
| 2388 | firstCoincident = activePtr; |
| 2389 | firstCoincidentWinding = priorWinding; |
| 2390 | } |
| 2391 | if (activePtr->fCloseCall) { |
| 2392 | // If one of the edges has already been added to out as a non |
| 2393 | // coincident edge, trim it back to the top of this span |
| 2394 | if (outBuilder.trimLine(y, activePtr->ID())) { |
| 2395 | activePtr->addTatYAbove(y); |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 2396 | #if DEBUG_ADJUST_COINCIDENT |
| 2397 | SkDebugf("%s 1 edge=%d y=%1.9g (was fYBottom=%1.9g)\n", |
| 2398 | __FUNCTION__, activePtr->ID(), y, activePtr->fYBottom); |
| 2399 | #endif |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2400 | activePtr->fYBottom = y; |
| 2401 | } |
| 2402 | if (outBuilder.trimLine(y, nextPtr->ID())) { |
| 2403 | nextPtr->addTatYAbove(y); |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 2404 | #if DEBUG_ADJUST_COINCIDENT |
| 2405 | SkDebugf("%s 2 edge=%d y=%1.9g (was fYBottom=%1.9g)\n", |
| 2406 | __FUNCTION__, nextPtr->ID(), y, nextPtr->fYBottom); |
| 2407 | #endif |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2408 | nextPtr->fYBottom = y; |
| 2409 | } |
| 2410 | // add missing t values so edges can be the same length |
| 2411 | SkScalar testY = activePtr->fBelow.fY; |
| 2412 | nextPtr->addTatYBelow(testY); |
| 2413 | if (bottom > testY && testY > y) { |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 2414 | #if DEBUG_ADJUST_COINCIDENT |
| 2415 | SkDebugf("%s 3 edge=%d bottom=%1.9g (was bottom=%1.9g)\n", |
| 2416 | __FUNCTION__, activePtr->ID(), testY, bottom); |
| 2417 | #endif |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2418 | bottom = testY; |
| 2419 | } |
| 2420 | testY = nextPtr->fBelow.fY; |
| 2421 | activePtr->addTatYBelow(testY); |
| 2422 | if (bottom > testY && testY > y) { |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 2423 | #if DEBUG_ADJUST_COINCIDENT |
| 2424 | SkDebugf("%s 4 edge=%d bottom=%1.9g (was bottom=%1.9g)\n", |
| 2425 | __FUNCTION__, nextPtr->ID(), testY, bottom); |
| 2426 | #endif |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2427 | bottom = testY; |
| 2428 | } |
| 2429 | } |
| 2430 | } else if (firstCoincident) { |
| 2431 | skipCoincidence(firstCoincidentWinding, winding, windingMask, |
| 2432 | activePtr, firstCoincident); |
| 2433 | firstCoincident = NULL; |
| 2434 | } |
| 2435 | activePtr = nextPtr; |
| 2436 | } |
| 2437 | if (firstCoincident) { |
| 2438 | winding += activePtr->fWorkEdge.winding(); |
| 2439 | skipCoincidence(firstCoincidentWinding, winding, windingMask, activePtr, |
caryclark@google.com | 6b9cfb3 | 2012-02-16 21:24:41 +0000 | [diff] [blame] | 2440 | firstCoincident); |
| 2441 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2442 | // fix up the bottom for close call edges. OPTIMIZATION: maybe this could |
| 2443 | // be in the loop above, but moved here since loop above reads fBelow and |
| 2444 | // it felt unsafe to write it in that loop |
| 2445 | for (index = 0; index < edgeCount; ++index) { |
| 2446 | (edgeList[index])->fixBelow(); |
| 2447 | } |
| 2448 | return bottom; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
| 2451 | // stitch edge and t range that satisfies operation |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame^] | 2452 | static void stitchEdge(SkTDArray<ActiveEdge*>& edgeList, SkScalar |
| 2453 | #if DEBUG_STITCH_EDGE |
| 2454 | y |
| 2455 | #endif |
| 2456 | , |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 2457 | SkScalar bottom, int windingMask, bool fill, OutEdgeBuilder& outBuilder) { |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2458 | int winding = 0; |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 2459 | ActiveEdge** activeHandle = edgeList.begin() - 1; |
| 2460 | ActiveEdge** lastActive = edgeList.end(); |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 2461 | #if DEBUG_STITCH_EDGE |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame^] | 2462 | const int tab = 7; // FIXME: debugging only |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 2463 | SkDebugf("%s y=%1.9g bottom=%1.9g\n", __FUNCTION__, y, bottom); |
| 2464 | #endif |
caryclark@google.com | 6008c656 | 2012-02-15 22:01:16 +0000 | [diff] [blame] | 2465 | while (++activeHandle != lastActive) { |
| 2466 | ActiveEdge* activePtr = *activeHandle; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2467 | const WorkEdge& wt = activePtr->fWorkEdge; |
| 2468 | int lastWinding = winding; |
| 2469 | winding += wt.winding(); |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 2470 | #if DEBUG_STITCH_EDGE |
| 2471 | SkDebugf("%*s edge=%d lastWinding=%d winding=%d skip=%d close=%d" |
| 2472 | " above=%1.9g below=%1.9g\n", |
| 2473 | tab-4, "", activePtr->ID(), lastWinding, |
| 2474 | winding, activePtr->fSkip, activePtr->fCloseCall, |
| 2475 | activePtr->fTAbove, activePtr->fTBelow); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2476 | #endif |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 2477 | if (activePtr->done(bottom)) { |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 2478 | #if DEBUG_STITCH_EDGE |
| 2479 | SkDebugf("%*s fDone=%d || fYBottom=%1.9g >= bottom\n", tab, "", |
| 2480 | activePtr->fDone, activePtr->fYBottom); |
| 2481 | #endif |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 2482 | continue; |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 2483 | } |
caryclark@google.com | c17972e | 2012-02-20 21:33:22 +0000 | [diff] [blame] | 2484 | int opener = (lastWinding & windingMask) == 0; |
| 2485 | bool closer = (winding & windingMask) == 0; |
| 2486 | SkASSERT(!opener | !closer); |
| 2487 | bool inWinding = opener | closer; |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 2488 | SkPoint clippedPts[4]; |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 2489 | const SkPoint* clipped = NULL; |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 2490 | bool moreToDo, aboveBottom; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2491 | do { |
| 2492 | double currentT = activePtr->t(); |
| 2493 | const SkPoint* points = wt.fPts; |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 2494 | double nextT; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2495 | SkPath::Verb verb = activePtr->fVerb; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2496 | do { |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 2497 | nextT = activePtr->nextT(); |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 2498 | // FIXME: obtuse: want efficient way to say |
| 2499 | // !currentT && currentT != 1 || !nextT && nextT != 1 |
| 2500 | if (currentT * nextT != 0 || currentT + nextT != 1) { |
| 2501 | // OPTIMIZATION: if !inWinding, we only need |
| 2502 | // clipped[1].fY |
| 2503 | switch (verb) { |
| 2504 | case SkPath::kLine_Verb: |
| 2505 | LineSubDivide(points, currentT, nextT, clippedPts); |
| 2506 | break; |
| 2507 | case SkPath::kQuad_Verb: |
| 2508 | QuadSubDivide(points, currentT, nextT, clippedPts); |
| 2509 | break; |
| 2510 | case SkPath::kCubic_Verb: |
| 2511 | CubicSubDivide(points, currentT, nextT, clippedPts); |
| 2512 | break; |
| 2513 | default: |
| 2514 | SkASSERT(0); |
| 2515 | break; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2516 | } |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 2517 | clipped = clippedPts; |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 2518 | } else { |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 2519 | clipped = points; |
| 2520 | } |
| 2521 | if (inWinding && !activePtr->fSkip && (fill ? clipped[0].fY |
| 2522 | != clipped[verb].fY : clipped[0] != clipped[verb])) { |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 2523 | #if DEBUG_STITCH_EDGE |
| 2524 | SkDebugf("%*s add%s %1.9g,%1.9g %1.9g,%1.9g edge=%d" |
| 2525 | " v=%d t=(%1.9g,%1.9g)\n", tab, "", |
| 2526 | kUVerbStr[verb], clipped[0].fX, clipped[0].fY, |
| 2527 | clipped[verb].fX, clipped[verb].fY, |
| 2528 | activePtr->ID(), |
| 2529 | activePtr->fWorkEdge.fVerb |
| 2530 | - activePtr->fWorkEdge.fEdge->fVerbs.begin(), |
| 2531 | currentT, nextT); |
| 2532 | #endif |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 2533 | outBuilder.addCurve(clipped, (SkPath::Verb) verb, |
| 2534 | activePtr->fWorkEdge.fEdge->fID, |
| 2535 | activePtr->fCloseCall); |
| 2536 | } else { |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 2537 | #if DEBUG_STITCH_EDGE |
| 2538 | SkDebugf("%*s skip%s %1.9g,%1.9g %1.9g,%1.9g" |
| 2539 | " edge=%d v=%d t=(%1.9g,%1.9g)\n", tab, "", |
| 2540 | kUVerbStr[verb], clipped[0].fX, clipped[0].fY, |
| 2541 | clipped[verb].fX, clipped[verb].fY, |
| 2542 | activePtr->ID(), |
| 2543 | activePtr->fWorkEdge.fVerb |
| 2544 | - activePtr->fWorkEdge.fEdge->fVerbs.begin(), |
| 2545 | currentT, nextT); |
| 2546 | #endif |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 2547 | } |
| 2548 | // by advancing fAbove/fBelow, the next call to sortHorizontal |
| 2549 | // will use these values if they're still valid instead of |
| 2550 | // recomputing |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 2551 | if (clipped[verb].fY > activePtr->fBelow.fY |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2552 | && bottom >= activePtr->fBelow.fY |
| 2553 | && verb == SkPath::kLine_Verb) { |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 2554 | activePtr->fAbove = activePtr->fBelow; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2555 | activePtr->fBelow = activePtr->fTangent = clipped[verb]; |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 2556 | activePtr->fTAbove = activePtr->fTBelow < 1 |
| 2557 | ? activePtr->fTBelow : 0; |
caryclark@google.com | a576423 | 2012-03-28 16:20:21 +0000 | [diff] [blame] | 2558 | activePtr->fTBelow = nextT; |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2559 | } |
| 2560 | currentT = nextT; |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 2561 | moreToDo = activePtr->advanceT(); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2562 | activePtr->fYBottom = clipped[verb].fY; // was activePtr->fCloseCall ? bottom : |
| 2563 | |
| 2564 | // clearing the fSkip/fCloseCall bit here means that trailing edges |
| 2565 | // fall out of sync, if one edge is long and another is a series of short pieces |
| 2566 | // if fSkip/fCloseCall is set, need to recompute coincidence/too-close-to-call |
| 2567 | // after advancing |
| 2568 | // another approach would be to restrict bottom to smaller part of close call |
| 2569 | // maybe this is already happening with coincidence when intersection is computed, |
| 2570 | // and needs to be added to the close call computation as well |
| 2571 | // this is hard to do because that the bottom is important is not known when |
| 2572 | // the lines are intersected; only when the computation for edge sorting is done |
| 2573 | // does the need for new bottoms become apparent. |
| 2574 | // maybe this is good incentive to scrap the current sort and do an insertion |
| 2575 | // sort that can take this into consideration when the x value is computed |
| 2576 | |
| 2577 | // FIXME: initialized in sortHorizontal, cleared here as well so |
| 2578 | // that next edge is not skipped -- but should skipped edges ever |
| 2579 | // continue? (probably not) |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 2580 | aboveBottom = clipped[verb].fY < bottom; |
| 2581 | if (clipped[0].fY != clipped[verb].fY) { |
| 2582 | activePtr->fSkip = false; |
| 2583 | activePtr->fCloseCall = false; |
| 2584 | aboveBottom &= !activePtr->fCloseCall; |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 2585 | } |
| 2586 | #if DEBUG_STITCH_EDGE |
| 2587 | else { |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 2588 | if (activePtr->fSkip || activePtr->fCloseCall) { |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 2589 | SkDebugf("%s skip or close == %1.9g\n", __FUNCTION__, |
| 2590 | clippedPts[0].fY); |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 2591 | } |
| 2592 | } |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 2593 | #endif |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 2594 | } while (moreToDo & aboveBottom); |
| 2595 | } while ((moreToDo || activePtr->advance()) & aboveBottom); |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2596 | } |
| 2597 | } |
| 2598 | |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame^] | 2599 | #if DEBUG_DUMP |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 2600 | static void dumpEdgeList(const SkTDArray<InEdge*>& edgeList, |
| 2601 | const InEdge& edgeSentinel) { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 2602 | InEdge** debugPtr = edgeList.begin(); |
| 2603 | do { |
| 2604 | (*debugPtr++)->dump(); |
| 2605 | } while (*debugPtr != &edgeSentinel); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 2606 | } |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame^] | 2607 | #else |
| 2608 | static void dumpEdgeList(const SkTDArray<InEdge*>& , |
| 2609 | const InEdge& ) { |
| 2610 | } |
| 2611 | #endif |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 2612 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 2613 | void simplify(const SkPath& path, bool asFill, SkPath& simple) { |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2614 | // returns 1 for evenodd, -1 for winding, regardless of inverse-ness |
| 2615 | int windingMask = (path.getFillType() & 1) ? 1 : -1; |
| 2616 | simple.reset(); |
| 2617 | simple.setFillType(SkPath::kEvenOdd_FillType); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 2618 | // turn path into list of edges increasing in y |
caryclark@google.com | cef7e3f | 2012-02-28 16:57:05 +0000 | [diff] [blame] | 2619 | // if an edge is a quad or a cubic with a y extrema, note it, but leave it |
| 2620 | // unbroken. Once we have a list, sort it, then walk the list (walk edges |
| 2621 | // twice that have y extrema's on top) and detect crossings -- look for raw |
| 2622 | // bounds that cross over, then tight bounds that cross |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 2623 | SkTArray<InEdge> edges; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2624 | SkTDArray<HorizontalEdge> horizontalEdges; |
| 2625 | InEdgeBuilder builder(path, asFill, edges, horizontalEdges); |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 2626 | SkTDArray<InEdge*> edgeList; |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 2627 | InEdge edgeSentinel; |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 2628 | edgeSentinel.reset(); |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2629 | makeEdgeList(edges, edgeSentinel, edgeList); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2630 | SkTDArray<HorizontalEdge*> horizontalList; |
| 2631 | HorizontalEdge horizontalSentinel; |
| 2632 | makeHorizontalList(horizontalEdges, horizontalSentinel, horizontalList); |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 2633 | InEdge** currentPtr = edgeList.begin(); |
caryclark@google.com | 4917f17 | 2012-03-05 22:01:21 +0000 | [diff] [blame] | 2634 | if (!currentPtr) { |
| 2635 | return; |
| 2636 | } |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2637 | // find all intersections between edges |
| 2638 | // beyond looking for horizontal intercepts, we need to know if any active edges |
| 2639 | // intersect edges below 'bottom', but above the active edge segment. |
| 2640 | // maybe it makes more sense to compute all intercepts before doing anything |
| 2641 | // else, since the intercept list is long-lived, at least in the current design. |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2642 | SkScalar y = (*currentPtr)->fBounds.fTop; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2643 | HorizontalEdge** currentHorizontal = horizontalList.begin(); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 2644 | do { |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 2645 | InEdge** lastPtr = currentPtr; // find the edge below the bottom of the first set |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2646 | SkScalar bottom = findBottom(currentPtr, edgeList.end(), |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2647 | NULL, y, asFill, lastPtr); |
caryclark@google.com | c17972e | 2012-02-20 21:33:22 +0000 | [diff] [blame] | 2648 | if (lastPtr > currentPtr) { |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2649 | if (currentHorizontal) { |
| 2650 | if ((*currentHorizontal)->fY < SK_ScalarMax) { |
| 2651 | addBottomT(currentPtr, lastPtr, currentHorizontal); |
| 2652 | } |
| 2653 | currentHorizontal = advanceHorizontal(currentHorizontal, bottom); |
| 2654 | } |
caryclark@google.com | c17972e | 2012-02-20 21:33:22 +0000 | [diff] [blame] | 2655 | addIntersectingTs(currentPtr, lastPtr); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2656 | } |
| 2657 | y = bottom; |
| 2658 | currentPtr = advanceEdges(NULL, currentPtr, lastPtr, y); |
| 2659 | } while (*currentPtr != &edgeSentinel); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 2660 | // if a quadratic or cubic now has an intermediate T value, see if the Ts |
| 2661 | // on either side cause the Y values to monotonically increase. If not, split |
| 2662 | // the curve at the new T. |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 2663 | |
| 2664 | // try an alternate approach which does not split curves or stitch edges |
| 2665 | // (may still need adjustCoincident, though) |
| 2666 | // the idea is to output non-intersecting contours, then figure out their |
| 2667 | // respective winding contribution |
| 2668 | // each contour will need to know whether it is CW or CCW, and then whether |
| 2669 | // a ray from that contour hits any a contour that contains it. The ray can |
| 2670 | // move to the left and then arbitrarily move up or down (as long as it never |
| 2671 | // moves to the right) to find a reference sibling contour or containing |
| 2672 | // contour. If the contour is part of an intersection, the companion contour |
| 2673 | // that is part of the intersection can determine the containership. |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 2674 | if (builder.containsCurves()) { |
| 2675 | currentPtr = edgeList.begin(); |
| 2676 | SkTArray<InEdge> splits; |
| 2677 | do { |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 2678 | (*currentPtr)->splitInflectionPts(splits); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 2679 | } while (*++currentPtr != &edgeSentinel); |
| 2680 | if (splits.count()) { |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 2681 | for (int index = 0; index < splits.count(); ++index) { |
| 2682 | edges.push_back(splits[index]); |
| 2683 | } |
caryclark@google.com | fb17342 | 2012-04-10 18:28:55 +0000 | [diff] [blame] | 2684 | edgeList.reset(); |
caryclark@google.com | 198e054 | 2012-03-30 18:47:02 +0000 | [diff] [blame] | 2685 | makeEdgeList(edges, edgeSentinel, edgeList); |
| 2686 | } |
| 2687 | } |
| 2688 | dumpEdgeList(edgeList, edgeSentinel); |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2689 | // walk the sorted edges from top to bottom, computing accumulated winding |
| 2690 | SkTDArray<ActiveEdge> activeEdges; |
| 2691 | OutEdgeBuilder outBuilder(asFill); |
| 2692 | currentPtr = edgeList.begin(); |
| 2693 | y = (*currentPtr)->fBounds.fTop; |
| 2694 | do { |
| 2695 | InEdge** lastPtr = currentPtr; // find the edge below the bottom of the first set |
| 2696 | SkScalar bottom = findBottom(currentPtr, edgeList.end(), |
| 2697 | &activeEdges, y, asFill, lastPtr); |
| 2698 | if (lastPtr > currentPtr) { |
| 2699 | bottom = computeInterceptBottom(activeEdges, y, bottom); |
caryclark@google.com | c17972e | 2012-02-20 21:33:22 +0000 | [diff] [blame] | 2700 | SkTDArray<ActiveEdge*> activeEdgeList; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2701 | sortHorizontal(activeEdges, activeEdgeList, y); |
| 2702 | bottom = adjustCoincident(activeEdgeList, windingMask, y, bottom, |
| 2703 | outBuilder); |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 2704 | stitchEdge(activeEdgeList, y, bottom, windingMask, asFill, outBuilder); |
caryclark@google.com | c17972e | 2012-02-20 21:33:22 +0000 | [diff] [blame] | 2705 | } |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 2706 | y = bottom; |
caryclark@google.com | 2e7f4c8 | 2012-03-20 21:11:59 +0000 | [diff] [blame] | 2707 | // OPTIMIZATION: as edges expire, InEdge allocations could be released |
| 2708 | currentPtr = advanceEdges(&activeEdges, currentPtr, lastPtr, y); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 2709 | } while (*currentPtr != &edgeSentinel); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 2710 | // assemble output path from string of pts, verbs |
caryclark@google.com | f8b000d | 2012-02-09 22:04:27 +0000 | [diff] [blame] | 2711 | outBuilder.bridge(); |
| 2712 | outBuilder.assemble(simple); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 2713 | } |