caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +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 | #ifndef SkIntersections_DEFINE |
| 8 | #define SkIntersections_DEFINE |
| 9 | |
| 10 | #include "SkPathOpsCubic.h" |
| 11 | #include "SkPathOpsLine.h" |
| 12 | #include "SkPathOpsPoint.h" |
| 13 | #include "SkPathOpsQuad.h" |
| 14 | |
| 15 | class SkIntersections { |
| 16 | public: |
| 17 | SkIntersections() |
| 18 | : fSwap(0) |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 19 | , fFlatMeasure(false) |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 20 | #ifdef SK_DEBUG |
| 21 | , fDepth(0) |
| 22 | #endif |
| 23 | { |
| 24 | sk_bzero(fPt, sizeof(fPt)); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 25 | sk_bzero(fPt2, sizeof(fPt2)); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 26 | sk_bzero(fT, sizeof(fT)); |
| 27 | sk_bzero(fIsCoincident, sizeof(fIsCoincident)); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 28 | sk_bzero(fNearlySame, sizeof(fNearlySame)); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 29 | reset(); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 30 | fMax = 0; // require that the caller set the max |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | class TArray { |
| 34 | public: |
| 35 | explicit TArray(const double ts[9]) : fTArray(ts) {} |
| 36 | double operator[](int n) const { |
| 37 | return fTArray[n]; |
| 38 | } |
| 39 | const double* fTArray; |
| 40 | }; |
| 41 | TArray operator[](int n) const { return TArray(fT[n]); } |
| 42 | |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 43 | void allowFlatMeasure(bool flatAllowed) { |
| 44 | fFlatMeasure = flatAllowed; |
| 45 | } |
| 46 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 47 | void allowNear(bool nearAllowed) { |
| 48 | fAllowNear = nearAllowed; |
| 49 | } |
| 50 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 51 | int cubic(const SkPoint a[4]) { |
| 52 | SkDCubic cubic; |
| 53 | cubic.set(a); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 54 | fMax = 1; // self intersect |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 55 | return intersect(cubic); |
| 56 | } |
| 57 | |
| 58 | int cubicCubic(const SkPoint a[4], const SkPoint b[4]) { |
| 59 | SkDCubic aCubic; |
| 60 | aCubic.set(a); |
| 61 | SkDCubic bCubic; |
| 62 | bCubic.set(b); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 63 | fMax = 9; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 64 | return intersect(aCubic, bCubic); |
| 65 | } |
| 66 | |
| 67 | int cubicHorizontal(const SkPoint a[4], SkScalar left, SkScalar right, SkScalar y, |
| 68 | bool flipped) { |
| 69 | SkDCubic cubic; |
| 70 | cubic.set(a); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 71 | fMax = 3; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 72 | return horizontal(cubic, left, right, y, flipped); |
| 73 | } |
| 74 | |
| 75 | int cubicVertical(const SkPoint a[4], SkScalar top, SkScalar bottom, SkScalar x, bool flipped) { |
| 76 | SkDCubic cubic; |
| 77 | cubic.set(a); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 78 | fMax = 3; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 79 | return vertical(cubic, top, bottom, x, flipped); |
| 80 | } |
| 81 | |
| 82 | int cubicLine(const SkPoint a[4], const SkPoint b[2]) { |
| 83 | SkDCubic cubic; |
| 84 | cubic.set(a); |
| 85 | SkDLine line; |
| 86 | line.set(b); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 87 | fMax = 3; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 88 | return intersect(cubic, line); |
| 89 | } |
| 90 | |
| 91 | int cubicQuad(const SkPoint a[4], const SkPoint b[3]) { |
| 92 | SkDCubic cubic; |
| 93 | cubic.set(a); |
| 94 | SkDQuad quad; |
| 95 | quad.set(b); |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 96 | fMax = 7; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 97 | return intersect(cubic, quad); |
| 98 | } |
| 99 | |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 100 | bool flatMeasure() const { |
| 101 | return fFlatMeasure; |
| 102 | } |
| 103 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 104 | bool hasT(double t) const { |
| 105 | SkASSERT(t == 0 || t == 1); |
| 106 | return fUsed > 0 && (t == 0 ? fT[0][0] == 0 : fT[0][fUsed - 1] == 1); |
| 107 | } |
| 108 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 109 | int insertSwap(double one, double two, const SkDPoint& pt) { |
| 110 | if (fSwap) { |
| 111 | return insert(two, one, pt); |
| 112 | } else { |
| 113 | return insert(one, two, pt); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | bool isCoincident(int index) { |
| 118 | return (fIsCoincident[0] & 1 << index) != 0; |
| 119 | } |
| 120 | |
| 121 | int lineHorizontal(const SkPoint a[2], SkScalar left, SkScalar right, SkScalar y, |
| 122 | bool flipped) { |
| 123 | SkDLine line; |
| 124 | line.set(a); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 125 | fMax = 2; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 126 | return horizontal(line, left, right, y, flipped); |
| 127 | } |
| 128 | |
| 129 | int lineVertical(const SkPoint a[2], SkScalar top, SkScalar bottom, SkScalar x, bool flipped) { |
| 130 | SkDLine line; |
| 131 | line.set(a); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 132 | fMax = 2; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 133 | return vertical(line, top, bottom, x, flipped); |
| 134 | } |
| 135 | |
| 136 | int lineLine(const SkPoint a[2], const SkPoint b[2]) { |
| 137 | SkDLine aLine, bLine; |
| 138 | aLine.set(a); |
| 139 | bLine.set(b); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 140 | fMax = 2; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 141 | return intersect(aLine, bLine); |
| 142 | } |
| 143 | |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 144 | bool nearlySame(int index) const { |
| 145 | SkASSERT(index == 0 || index == 1); |
| 146 | return fNearlySame[index]; |
| 147 | } |
| 148 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 149 | const SkDPoint& pt(int index) const { |
| 150 | return fPt[index]; |
| 151 | } |
| 152 | |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 153 | const SkDPoint& pt2(int index) const { |
| 154 | return fPt2[index]; |
| 155 | } |
| 156 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 157 | int quadHorizontal(const SkPoint a[3], SkScalar left, SkScalar right, SkScalar y, |
| 158 | bool flipped) { |
| 159 | SkDQuad quad; |
| 160 | quad.set(a); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 161 | fMax = 2; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 162 | return horizontal(quad, left, right, y, flipped); |
| 163 | } |
| 164 | |
| 165 | int quadVertical(const SkPoint a[3], SkScalar top, SkScalar bottom, SkScalar x, bool flipped) { |
| 166 | SkDQuad quad; |
| 167 | quad.set(a); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 168 | fMax = 2; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 169 | return vertical(quad, top, bottom, x, flipped); |
| 170 | } |
| 171 | |
| 172 | int quadLine(const SkPoint a[3], const SkPoint b[2]) { |
| 173 | SkDQuad quad; |
| 174 | quad.set(a); |
| 175 | SkDLine line; |
| 176 | line.set(b); |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 177 | fMax = 3; // 2; permit small coincident segment + non-coincident intersection |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 178 | return intersect(quad, line); |
| 179 | } |
| 180 | |
| 181 | int quadQuad(const SkPoint a[3], const SkPoint b[3]) { |
| 182 | SkDQuad aQuad; |
| 183 | aQuad.set(a); |
| 184 | SkDQuad bQuad; |
| 185 | bQuad.set(b); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 186 | fMax = 4; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 187 | return intersect(aQuad, bQuad); |
| 188 | } |
| 189 | |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 190 | // leaves swap, max alone |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 191 | void reset() { |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 192 | fAllowNear = true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 193 | fUsed = 0; |
| 194 | } |
| 195 | |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 196 | void set(bool swap, int tIndex, double t) { |
| 197 | fT[(int) swap][tIndex] = t; |
| 198 | } |
| 199 | |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 200 | void setMax(int max) { |
| 201 | fMax = max; |
| 202 | } |
| 203 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 204 | void swap() { |
| 205 | fSwap ^= true; |
| 206 | } |
| 207 | |
| 208 | void swapPts(); |
| 209 | |
| 210 | bool swapped() const { |
| 211 | return fSwap; |
| 212 | } |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 213 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 214 | int used() const { |
| 215 | return fUsed; |
| 216 | } |
| 217 | |
| 218 | void downDepth() { |
| 219 | SkASSERT(--fDepth >= 0); |
| 220 | } |
| 221 | |
| 222 | void upDepth() { |
| 223 | SkASSERT(++fDepth < 16); |
| 224 | } |
| 225 | |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 226 | void alignQuadPts(const SkPoint a[3], const SkPoint b[3]); |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 227 | void append(const SkIntersections& ); |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 228 | int cleanUpCoincidence(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 229 | int coincidentUsed() const; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 230 | void cubicInsert(double one, double two, const SkDPoint& pt, const SkDCubic& c1, |
| 231 | const SkDCubic& c2); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 232 | int cubicRay(const SkPoint pts[4], const SkDLine& line); |
| 233 | void flip(); |
| 234 | int horizontal(const SkDLine&, double y); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 235 | int horizontal(const SkDLine&, double left, double right, double y, bool flipped); |
| 236 | int horizontal(const SkDQuad&, double left, double right, double y, bool flipped); |
| 237 | int horizontal(const SkDQuad&, double left, double right, double y, double tRange[2]); |
| 238 | int horizontal(const SkDCubic&, double y, double tRange[3]); |
| 239 | int horizontal(const SkDCubic&, double left, double right, double y, bool flipped); |
| 240 | int horizontal(const SkDCubic&, double left, double right, double y, double tRange[3]); |
| 241 | // FIXME : does not respect swap |
| 242 | int insert(double one, double two, const SkDPoint& pt); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 243 | void insertNear(double one, double two, const SkDPoint& pt1, const SkDPoint& pt2); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 244 | // start if index == 0 : end if index == 1 |
| 245 | void insertCoincident(double one, double two, const SkDPoint& pt); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 246 | int intersect(const SkDLine&, const SkDLine&); |
| 247 | int intersect(const SkDQuad&, const SkDLine&); |
| 248 | int intersect(const SkDQuad&, const SkDQuad&); |
| 249 | int intersect(const SkDCubic&); // return true if cubic self-intersects |
| 250 | int intersect(const SkDCubic&, const SkDLine&); |
| 251 | int intersect(const SkDCubic&, const SkDQuad&); |
| 252 | int intersect(const SkDCubic&, const SkDCubic&); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 253 | int intersectRay(const SkDLine&, const SkDLine&); |
| 254 | int intersectRay(const SkDQuad&, const SkDLine&); |
| 255 | int intersectRay(const SkDCubic&, const SkDLine&); |
| 256 | static SkDPoint Line(const SkDLine&, const SkDLine&); |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 257 | int lineRay(const SkPoint pts[2], const SkDLine& line); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 258 | void offset(int base, double start, double end); |
| 259 | void quickRemoveOne(int index, int replace); |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 260 | int quadRay(const SkPoint pts[3], const SkDLine& line); |
| 261 | void removeOne(int index); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 262 | static bool Test(const SkDLine& , const SkDLine&); |
| 263 | int vertical(const SkDLine&, double x); |
| 264 | int vertical(const SkDLine&, double top, double bottom, double x, bool flipped); |
| 265 | int vertical(const SkDQuad&, double top, double bottom, double x, bool flipped); |
| 266 | int vertical(const SkDCubic&, double top, double bottom, double x, bool flipped); |
| 267 | int verticalCubic(const SkPoint a[4], SkScalar top, SkScalar bottom, SkScalar x, bool flipped); |
| 268 | int verticalLine(const SkPoint a[2], SkScalar top, SkScalar bottom, SkScalar x, bool flipped); |
| 269 | int verticalQuad(const SkPoint a[3], SkScalar top, SkScalar bottom, SkScalar x, bool flipped); |
| 270 | |
| 271 | int depth() const { |
| 272 | #ifdef SK_DEBUG |
| 273 | return fDepth; |
| 274 | #else |
| 275 | return 0; |
| 276 | #endif |
| 277 | } |
| 278 | |
| 279 | private: |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 280 | bool cubicCheckCoincidence(const SkDCubic& c1, const SkDCubic& c2); |
| 281 | bool cubicExactEnd(const SkDCubic& cubic1, bool start, const SkDCubic& cubic2); |
| 282 | void cubicNearEnd(const SkDCubic& cubic1, bool start, const SkDCubic& cubic2, const SkDRect& ); |
| 283 | void cleanUpParallelLines(bool parallel); |
| 284 | void computePoints(const SkDLine& line, int used); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 285 | |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 286 | SkDPoint fPt[9]; // FIXME: since scans store points as SkPoint, this should also |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 287 | SkDPoint fPt2[9]; // used by nearly same to store alternate intersection point |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 288 | double fT[2][9]; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 289 | uint16_t fIsCoincident[2]; // bit set for each curve's coincident T |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 290 | bool fNearlySame[2]; // true if end points nearly match |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 291 | unsigned char fUsed; |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 292 | unsigned char fMax; |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 293 | bool fAllowNear; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 294 | bool fSwap; |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 295 | bool fFlatMeasure; // backwards-compatibility when cubics uses quad intersection |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 296 | #ifdef SK_DEBUG |
| 297 | int fDepth; |
| 298 | #endif |
| 299 | }; |
| 300 | |
caryclark | 2e40381 | 2014-08-25 06:53:04 -0700 | [diff] [blame] | 301 | extern int (SkIntersections::* const CurveRay[])(const SkPoint[], const SkDLine& ); |
| 302 | extern int (SkIntersections::* const CurveVertical[])(const SkPoint[], SkScalar top, SkScalar bottom, |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 303 | SkScalar x, bool flipped); |
| 304 | |
| 305 | #endif |