Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 8 | #include "SkPolyUtils.h" |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 9 | |
Jim Van Verth | b7c9551 | 2018-09-11 12:57:42 -0400 | [diff] [blame] | 10 | #include <limits> |
| 11 | |
Jim Van Verth | 9b21825 | 2018-09-21 14:27:35 -0400 | [diff] [blame] | 12 | #include "SkNx.h" |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 13 | #include "SkPointPriv.h" |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 14 | #include "SkTArray.h" |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 15 | #include "SkTemplates.h" |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 16 | #include "SkTDPQueue.h" |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 17 | #include "SkTInternalLList.h" |
| 18 | |
| 19 | ////////////////////////////////////////////////////////////////////////////////// |
| 20 | // Helper data structures and functions |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 21 | |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 22 | struct OffsetSegment { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 23 | SkPoint fP0; |
Jim Van Verth | a631683 | 2018-07-24 09:30:37 -0400 | [diff] [blame] | 24 | SkVector fV; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 27 | constexpr SkScalar kCrossTolerance = SK_ScalarNearlyZero * SK_ScalarNearlyZero; |
| 28 | |
| 29 | // Computes perpDot for point p compared to segment defined by origin p0 and vector v. |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 30 | // A positive value means the point is to the left of the segment, |
| 31 | // negative is to the right, 0 is collinear. |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 32 | static int compute_side(const SkPoint& p0, const SkVector& v, const SkPoint& p) { |
| 33 | SkVector w = p - p0; |
| 34 | SkScalar perpDot = v.cross(w); |
| 35 | if (!SkScalarNearlyZero(perpDot, kCrossTolerance)) { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 36 | return ((perpDot > 0) ? 1 : -1); |
| 37 | } |
| 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 42 | // Returns 1 for cw, -1 for ccw and 0 if zero signed area (either degenerate or self-intersecting) |
| 43 | int SkGetPolygonWinding(const SkPoint* polygonVerts, int polygonSize) { |
| 44 | if (polygonSize < 3) { |
| 45 | return 0; |
| 46 | } |
| 47 | |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 48 | // compute area and use sign to determine winding |
| 49 | SkScalar quadArea = 0; |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 50 | SkVector v0 = polygonVerts[1] - polygonVerts[0]; |
Jim Van Verth | 9b21825 | 2018-09-21 14:27:35 -0400 | [diff] [blame] | 51 | for (int curr = 2; curr < polygonSize; ++curr) { |
| 52 | SkVector v1 = polygonVerts[curr] - polygonVerts[0]; |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 53 | quadArea += v0.cross(v1); |
| 54 | v0 = v1; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 55 | } |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 56 | if (SkScalarNearlyZero(quadArea, kCrossTolerance)) { |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | // 1 == ccw, -1 == cw |
| 60 | return (quadArea > 0) ? 1 : -1; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 63 | // Compute difference vector to offset p0-p1 'offset' units in direction specified by 'side' |
Jim Van Verth | 8222f3b | 2018-11-29 13:14:17 -0500 | [diff] [blame] | 64 | bool compute_offset_vector(const SkPoint& p0, const SkPoint& p1, SkScalar offset, int side, |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 65 | SkPoint* vector) { |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 66 | SkASSERT(side == -1 || side == 1); |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 67 | // if distances are equal, can just outset by the perpendicular |
| 68 | SkVector perp = SkVector::Make(p0.fY - p1.fY, p1.fX - p0.fX); |
Jim Van Verth | 8222f3b | 2018-11-29 13:14:17 -0500 | [diff] [blame] | 69 | if (!perp.setLength(offset*side)) { |
| 70 | return false; |
| 71 | } |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 72 | *vector = perp; |
Jim Van Verth | 8222f3b | 2018-11-29 13:14:17 -0500 | [diff] [blame] | 73 | return true; |
Jim Van Verth | bdde428 | 2018-06-14 09:09:18 -0400 | [diff] [blame] | 74 | } |
| 75 | |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 76 | // check interval to see if intersection is in segment |
| 77 | static inline bool outside_interval(SkScalar numer, SkScalar denom, bool denomPositive) { |
| 78 | return (denomPositive && (numer < 0 || numer > denom)) || |
| 79 | (!denomPositive && (numer > 0 || numer < denom)); |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 80 | } |
| 81 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 82 | // Compute the intersection 'p' between segments s0 and s1, if any. |
| 83 | // 's' is the parametric value for the intersection along 's0' & 't' is the same for 's1'. |
| 84 | // Returns false if there is no intersection. |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 85 | static bool compute_intersection(const OffsetSegment& s0, const OffsetSegment& s1, |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 86 | SkPoint* p, SkScalar* s, SkScalar* t) { |
Jim Van Verth | a631683 | 2018-07-24 09:30:37 -0400 | [diff] [blame] | 87 | const SkVector& v0 = s0.fV; |
| 88 | const SkVector& v1 = s1.fV; |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 89 | SkVector w = s1.fP0 - s0.fP0; |
| 90 | SkScalar denom = v0.cross(v1); |
| 91 | bool denomPositive = (denom > 0); |
| 92 | SkScalar sNumer, tNumer; |
| 93 | if (SkScalarNearlyZero(denom, kCrossTolerance)) { |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 94 | // segments are parallel, but not collinear |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 95 | if (!SkScalarNearlyZero(w.cross(v0), kCrossTolerance) || |
| 96 | !SkScalarNearlyZero(w.cross(v1), kCrossTolerance)) { |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 97 | return false; |
| 98 | } |
| 99 | |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 100 | // Check for zero-length segments |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 101 | if (!SkPointPriv::CanNormalize(v0.fX, v0.fY)) { |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 102 | // Both are zero-length |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 103 | if (!SkPointPriv::CanNormalize(v1.fX, v1.fY)) { |
| 104 | // Check if they're the same point |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 105 | if (!SkPointPriv::CanNormalize(w.fX, w.fY)) { |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 106 | *p = s0.fP0; |
| 107 | *s = 0; |
| 108 | *t = 0; |
| 109 | return true; |
| 110 | } else { |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 111 | return false; |
| 112 | } |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 113 | } |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 114 | // Otherwise project segment0's origin onto segment1 |
| 115 | tNumer = v1.dot(-w); |
| 116 | denom = v1.dot(v1); |
| 117 | if (outside_interval(tNumer, denom, true)) { |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 118 | return false; |
| 119 | } |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 120 | sNumer = 0; |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 121 | } else { |
| 122 | // Project segment1's endpoints onto segment0 |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 123 | sNumer = v0.dot(w); |
| 124 | denom = v0.dot(v0); |
| 125 | tNumer = 0; |
| 126 | if (outside_interval(sNumer, denom, true)) { |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 127 | // The first endpoint doesn't lie on segment0 |
| 128 | // If segment1 is degenerate, then there's no collision |
| 129 | if (!SkPointPriv::CanNormalize(v1.fX, v1.fY)) { |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | // Otherwise try the other one |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 134 | SkScalar oldSNumer = sNumer; |
| 135 | sNumer = v0.dot(w + v1); |
| 136 | tNumer = denom; |
| 137 | if (outside_interval(sNumer, denom, true)) { |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 138 | // it's possible that segment1's interval surrounds segment0 |
| 139 | // this is false if params have the same signs, and in that case no collision |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 140 | if (sNumer*oldSNumer > 0) { |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 141 | return false; |
| 142 | } |
| 143 | // otherwise project segment0's endpoint onto segment1 instead |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 144 | sNumer = 0; |
| 145 | tNumer = v1.dot(-w); |
| 146 | denom = v1.dot(v1); |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 147 | } |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | } else { |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 151 | sNumer = w.cross(v1); |
| 152 | if (outside_interval(sNumer, denom, denomPositive)) { |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 153 | return false; |
| 154 | } |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 155 | tNumer = w.cross(v0); |
| 156 | if (outside_interval(tNumer, denom, denomPositive)) { |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 157 | return false; |
| 158 | } |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 161 | SkScalar localS = sNumer/denom; |
| 162 | SkScalar localT = tNumer/denom; |
| 163 | |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 164 | *p = s0.fP0 + v0*localS; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 165 | *s = localS; |
| 166 | *t = localT; |
| 167 | |
| 168 | return true; |
| 169 | } |
| 170 | |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 171 | bool SkIsConvexPolygon(const SkPoint* polygonVerts, int polygonSize) { |
| 172 | if (polygonSize < 3) { |
| 173 | return false; |
Jim Van Verth | 0513f14 | 2017-03-24 14:28:57 -0400 | [diff] [blame] | 174 | } |
| 175 | |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 176 | SkScalar lastArea = 0; |
| 177 | SkScalar lastPerpDot = 0; |
Jim Van Verth | 0513f14 | 2017-03-24 14:28:57 -0400 | [diff] [blame] | 178 | |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 179 | int prevIndex = polygonSize - 1; |
| 180 | int currIndex = 0; |
| 181 | int nextIndex = 1; |
| 182 | SkPoint origin = polygonVerts[0]; |
| 183 | SkVector v0 = polygonVerts[currIndex] - polygonVerts[prevIndex]; |
| 184 | SkVector v1 = polygonVerts[nextIndex] - polygonVerts[currIndex]; |
| 185 | SkVector w0 = polygonVerts[currIndex] - origin; |
| 186 | SkVector w1 = polygonVerts[nextIndex] - origin; |
| 187 | for (int i = 0; i < polygonSize; ++i) { |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 188 | if (!polygonVerts[i].isFinite()) { |
| 189 | return false; |
| 190 | } |
| 191 | |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 192 | // Check that winding direction is always the same (otherwise we have a reflex vertex) |
Jim Van Verth | 0513f14 | 2017-03-24 14:28:57 -0400 | [diff] [blame] | 193 | SkScalar perpDot = v0.cross(v1); |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 194 | if (lastPerpDot*perpDot < 0) { |
Jim Van Verth | 0513f14 | 2017-03-24 14:28:57 -0400 | [diff] [blame] | 195 | return false; |
| 196 | } |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 197 | if (0 != perpDot) { |
| 198 | lastPerpDot = perpDot; |
| 199 | } |
| 200 | |
| 201 | // If the signed area ever flips it's concave |
| 202 | // TODO: see if we can verify convexity only with signed area |
| 203 | SkScalar quadArea = w0.cross(w1); |
| 204 | if (quadArea*lastArea < 0) { |
| 205 | return false; |
| 206 | } |
| 207 | if (0 != quadArea) { |
| 208 | lastArea = quadArea; |
| 209 | } |
| 210 | |
| 211 | prevIndex = currIndex; |
| 212 | currIndex = nextIndex; |
| 213 | nextIndex = (currIndex + 1) % polygonSize; |
| 214 | v0 = v1; |
| 215 | v1 = polygonVerts[nextIndex] - polygonVerts[currIndex]; |
| 216 | w0 = w1; |
| 217 | w1 = polygonVerts[nextIndex] - origin; |
Jim Van Verth | 0513f14 | 2017-03-24 14:28:57 -0400 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | return true; |
| 221 | } |
Jim Van Verth | 0513f14 | 2017-03-24 14:28:57 -0400 | [diff] [blame] | 222 | |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 223 | struct OffsetEdge { |
| 224 | OffsetEdge* fPrev; |
| 225 | OffsetEdge* fNext; |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 226 | OffsetSegment fOffset; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 227 | SkPoint fIntersection; |
| 228 | SkScalar fTValue; |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 229 | uint16_t fIndex; |
Jim Van Verth | a631683 | 2018-07-24 09:30:37 -0400 | [diff] [blame] | 230 | uint16_t fEnd; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 231 | |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 232 | void init(uint16_t start = 0, uint16_t end = 0) { |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 233 | fIntersection = fOffset.fP0; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 234 | fTValue = SK_ScalarMin; |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 235 | fIndex = start; |
Jim Van Verth | a631683 | 2018-07-24 09:30:37 -0400 | [diff] [blame] | 236 | fEnd = end; |
| 237 | } |
| 238 | |
| 239 | // special intersection check that looks for endpoint intersection |
| 240 | bool checkIntersection(const OffsetEdge* that, |
| 241 | SkPoint* p, SkScalar* s, SkScalar* t) { |
| 242 | if (this->fEnd == that->fIndex) { |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 243 | SkPoint p1 = this->fOffset.fP0 + this->fOffset.fV; |
| 244 | if (SkPointPriv::EqualsWithinTolerance(p1, that->fOffset.fP0)) { |
Jim Van Verth | a631683 | 2018-07-24 09:30:37 -0400 | [diff] [blame] | 245 | *p = p1; |
| 246 | *s = SK_Scalar1; |
| 247 | *t = 0; |
| 248 | return true; |
| 249 | } |
| 250 | } |
| 251 | |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 252 | return compute_intersection(this->fOffset, that->fOffset, p, s, t); |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 253 | } |
Jim Van Verth | eddb3d9 | 2018-08-02 10:56:26 -0400 | [diff] [blame] | 254 | |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 255 | // computes the line intersection and then the "distance" from that to this |
| 256 | // this is really a signed squared distance, where negative means that |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 257 | // the intersection lies inside this->fOffset |
Jim Van Verth | eddb3d9 | 2018-08-02 10:56:26 -0400 | [diff] [blame] | 258 | SkScalar computeCrossingDistance(const OffsetEdge* that) { |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 259 | const OffsetSegment& s0 = this->fOffset; |
| 260 | const OffsetSegment& s1 = that->fOffset; |
Jim Van Verth | eddb3d9 | 2018-08-02 10:56:26 -0400 | [diff] [blame] | 261 | const SkVector& v0 = s0.fV; |
| 262 | const SkVector& v1 = s1.fV; |
| 263 | |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 264 | SkScalar denom = v0.cross(v1); |
| 265 | if (SkScalarNearlyZero(denom, kCrossTolerance)) { |
Jim Van Verth | eddb3d9 | 2018-08-02 10:56:26 -0400 | [diff] [blame] | 266 | // segments are parallel |
| 267 | return SK_ScalarMax; |
| 268 | } |
| 269 | |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 270 | SkVector w = s1.fP0 - s0.fP0; |
| 271 | SkScalar localS = w.cross(v1) / denom; |
Jim Van Verth | eddb3d9 | 2018-08-02 10:56:26 -0400 | [diff] [blame] | 272 | if (localS < 0) { |
| 273 | localS = -localS; |
| 274 | } else { |
| 275 | localS -= SK_Scalar1; |
| 276 | } |
| 277 | |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 278 | localS *= SkScalarAbs(localS); |
| 279 | localS *= v0.dot(v0); |
Jim Van Verth | eddb3d9 | 2018-08-02 10:56:26 -0400 | [diff] [blame] | 280 | |
| 281 | return localS; |
| 282 | } |
| 283 | |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 284 | }; |
| 285 | |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 286 | static void remove_node(const OffsetEdge* node, OffsetEdge** head) { |
| 287 | // remove from linked list |
| 288 | node->fPrev->fNext = node->fNext; |
| 289 | node->fNext->fPrev = node->fPrev; |
| 290 | if (node == *head) { |
| 291 | *head = (node->fNext == node) ? nullptr : node->fNext; |
| 292 | } |
| 293 | } |
| 294 | |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 295 | ////////////////////////////////////////////////////////////////////////////////// |
| 296 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 297 | // The objective here is to inset all of the edges by the given distance, and then |
| 298 | // remove any invalid inset edges by detecting right-hand turns. In a ccw polygon, |
| 299 | // we should only be making left-hand turns (for cw polygons, we use the winding |
| 300 | // parameter to reverse this). We detect this by checking whether the second intersection |
| 301 | // on an edge is closer to its tail than the first one. |
| 302 | // |
| 303 | // We might also have the case that there is no intersection between two neighboring inset edges. |
| 304 | // In this case, one edge will lie to the right of the other and should be discarded along with |
| 305 | // its previous intersection (if any). |
| 306 | // |
| 307 | // Note: the assumption is that inputPolygon is convex and has no coincident points. |
| 308 | // |
| 309 | bool SkInsetConvexPolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize, |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 310 | SkScalar inset, SkTDArray<SkPoint>* insetPolygon) { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 311 | if (inputPolygonSize < 3) { |
| 312 | return false; |
| 313 | } |
| 314 | |
Jim Van Verth | a631683 | 2018-07-24 09:30:37 -0400 | [diff] [blame] | 315 | // restrict this to match other routines |
| 316 | // practically we don't want anything bigger than this anyway |
Jim Van Verth | b7c9551 | 2018-09-11 12:57:42 -0400 | [diff] [blame] | 317 | if (inputPolygonSize > std::numeric_limits<uint16_t>::max()) { |
Jim Van Verth | a631683 | 2018-07-24 09:30:37 -0400 | [diff] [blame] | 318 | return false; |
| 319 | } |
| 320 | |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 321 | // get winding direction |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 322 | int winding = SkGetPolygonWinding(inputPolygonVerts, inputPolygonSize); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 323 | if (0 == winding) { |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | // set up |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 328 | SkAutoSTMalloc<64, OffsetEdge> edgeData(inputPolygonSize); |
| 329 | int prev = inputPolygonSize - 1; |
| 330 | for (int curr = 0; curr < inputPolygonSize; prev = curr, ++curr) { |
| 331 | int next = (curr + 1) % inputPolygonSize; |
| 332 | if (!inputPolygonVerts[curr].isFinite()) { |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 333 | return false; |
| 334 | } |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 335 | // check for convexity just to be sure |
Jim Van Verth | a631683 | 2018-07-24 09:30:37 -0400 | [diff] [blame] | 336 | if (compute_side(inputPolygonVerts[prev], inputPolygonVerts[curr] - inputPolygonVerts[prev], |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 337 | inputPolygonVerts[next])*winding < 0) { |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 338 | return false; |
| 339 | } |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 340 | SkVector v = inputPolygonVerts[next] - inputPolygonVerts[curr]; |
| 341 | SkVector perp = SkVector::Make(-v.fY, v.fX); |
| 342 | perp.setLength(inset*winding); |
Jim Van Verth | a631683 | 2018-07-24 09:30:37 -0400 | [diff] [blame] | 343 | edgeData[curr].fPrev = &edgeData[prev]; |
| 344 | edgeData[curr].fNext = &edgeData[next]; |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 345 | edgeData[curr].fOffset.fP0 = inputPolygonVerts[curr] + perp; |
| 346 | edgeData[curr].fOffset.fV = v; |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 347 | edgeData[curr].init(); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 350 | OffsetEdge* head = &edgeData[0]; |
| 351 | OffsetEdge* currEdge = head; |
| 352 | OffsetEdge* prevEdge = currEdge->fPrev; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 353 | int insetVertexCount = inputPolygonSize; |
Jim Van Verth | b7c9551 | 2018-09-11 12:57:42 -0400 | [diff] [blame] | 354 | unsigned int iterations = 0; |
| 355 | unsigned int maxIterations = inputPolygonSize * inputPolygonSize; |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 356 | while (head && prevEdge != currEdge) { |
Jim Van Verth | 796bc1d | 2018-06-19 15:00:28 -0400 | [diff] [blame] | 357 | ++iterations; |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 358 | // we should check each edge against each other edge at most once |
Jim Van Verth | b7c9551 | 2018-09-11 12:57:42 -0400 | [diff] [blame] | 359 | if (iterations > maxIterations) { |
Jim Van Verth | 796bc1d | 2018-06-19 15:00:28 -0400 | [diff] [blame] | 360 | return false; |
| 361 | } |
| 362 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 363 | SkScalar s, t; |
| 364 | SkPoint intersection; |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 365 | if (compute_intersection(prevEdge->fOffset, currEdge->fOffset, |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 366 | &intersection, &s, &t)) { |
| 367 | // if new intersection is further back on previous inset from the prior intersection |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 368 | if (s < prevEdge->fTValue) { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 369 | // no point in considering this one again |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 370 | remove_node(prevEdge, &head); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 371 | --insetVertexCount; |
| 372 | // go back one segment |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 373 | prevEdge = prevEdge->fPrev; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 374 | // we've already considered this intersection, we're done |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 375 | } else if (currEdge->fTValue > SK_ScalarMin && |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 376 | SkPointPriv::EqualsWithinTolerance(intersection, |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 377 | currEdge->fIntersection, |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 378 | 1.0e-6f)) { |
| 379 | break; |
| 380 | } else { |
| 381 | // add intersection |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 382 | currEdge->fIntersection = intersection; |
| 383 | currEdge->fTValue = t; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 384 | |
| 385 | // go to next segment |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 386 | prevEdge = currEdge; |
| 387 | currEdge = currEdge->fNext; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 388 | } |
| 389 | } else { |
| 390 | // if prev to right side of curr |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 391 | int side = winding*compute_side(currEdge->fOffset.fP0, |
| 392 | currEdge->fOffset.fV, |
| 393 | prevEdge->fOffset.fP0); |
Jim Van Verth | a631683 | 2018-07-24 09:30:37 -0400 | [diff] [blame] | 394 | if (side < 0 && |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 395 | side == winding*compute_side(currEdge->fOffset.fP0, |
| 396 | currEdge->fOffset.fV, |
| 397 | prevEdge->fOffset.fP0 + prevEdge->fOffset.fV)) { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 398 | // no point in considering this one again |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 399 | remove_node(prevEdge, &head); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 400 | --insetVertexCount; |
| 401 | // go back one segment |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 402 | prevEdge = prevEdge->fPrev; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 403 | } else { |
| 404 | // move to next segment |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 405 | remove_node(currEdge, &head); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 406 | --insetVertexCount; |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 407 | currEdge = currEdge->fNext; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 412 | // store all the valid intersections that aren't nearly coincident |
| 413 | // TODO: look at the main algorithm and see if we can detect these better |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 414 | insetPolygon->reset(); |
Jim Van Verth | b7c9551 | 2018-09-11 12:57:42 -0400 | [diff] [blame] | 415 | if (!head) { |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | static constexpr SkScalar kCleanupTolerance = 0.01f; |
| 420 | if (insetVertexCount >= 0) { |
| 421 | insetPolygon->setReserve(insetVertexCount); |
| 422 | } |
| 423 | int currIndex = 0; |
| 424 | *insetPolygon->push() = head->fIntersection; |
| 425 | currEdge = head->fNext; |
| 426 | while (currEdge != head) { |
| 427 | if (!SkPointPriv::EqualsWithinTolerance(currEdge->fIntersection, |
| 428 | (*insetPolygon)[currIndex], |
| 429 | kCleanupTolerance)) { |
| 430 | *insetPolygon->push() = currEdge->fIntersection; |
| 431 | currIndex++; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 432 | } |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 433 | currEdge = currEdge->fNext; |
Jim Van Verth | b7c9551 | 2018-09-11 12:57:42 -0400 | [diff] [blame] | 434 | } |
| 435 | // make sure the first and last points aren't coincident |
| 436 | if (currIndex >= 1 && |
| 437 | SkPointPriv::EqualsWithinTolerance((*insetPolygon)[0], (*insetPolygon)[currIndex], |
| 438 | kCleanupTolerance)) { |
| 439 | insetPolygon->pop(); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 440 | } |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 441 | |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 442 | return SkIsConvexPolygon(insetPolygon->begin(), insetPolygon->count()); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 443 | } |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 444 | |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 445 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 446 | |
| 447 | // compute the number of points needed for a circular join when offsetting a reflex vertex |
Jim Van Verth | 66c5dc5 | 2018-08-06 14:38:31 -0400 | [diff] [blame] | 448 | bool SkComputeRadialSteps(const SkVector& v1, const SkVector& v2, SkScalar offset, |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 449 | SkScalar* rotSin, SkScalar* rotCos, int* n) { |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 450 | const SkScalar kRecipPixelsPerArcSegment = 0.25f; |
| 451 | |
| 452 | SkScalar rCos = v1.dot(v2); |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 453 | if (!SkScalarIsFinite(rCos)) { |
| 454 | return false; |
| 455 | } |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 456 | SkScalar rSin = v1.cross(v2); |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 457 | if (!SkScalarIsFinite(rSin)) { |
| 458 | return false; |
| 459 | } |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 460 | SkScalar theta = SkScalarATan2(rSin, rCos); |
| 461 | |
Jim Van Verth | 66c5dc5 | 2018-08-06 14:38:31 -0400 | [diff] [blame] | 462 | SkScalar floatSteps = SkScalarAbs(offset*theta*kRecipPixelsPerArcSegment); |
Jim Van Verth | 206dbe8 | 2018-07-23 11:48:31 -0400 | [diff] [blame] | 463 | // limit the number of steps to at most max uint16_t (that's all we can index) |
| 464 | // knock one value off the top to account for rounding |
Jim Van Verth | b7c9551 | 2018-09-11 12:57:42 -0400 | [diff] [blame] | 465 | if (floatSteps >= std::numeric_limits<uint16_t>::max()) { |
Jim Van Verth | 206dbe8 | 2018-07-23 11:48:31 -0400 | [diff] [blame] | 466 | return false; |
| 467 | } |
| 468 | int steps = SkScalarRoundToInt(floatSteps); |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 469 | |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 470 | SkScalar dTheta = steps > 0 ? theta / steps : 0; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 471 | *rotSin = SkScalarSinCos(dTheta, rotCos); |
| 472 | *n = steps; |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 473 | return true; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 474 | } |
| 475 | |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 476 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 477 | |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 478 | // a point is "left" to another if its x-coord is less, or if equal, its y-coord is greater |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 479 | static bool left(const SkPoint& p0, const SkPoint& p1) { |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 480 | return p0.fX < p1.fX || (!(p0.fX > p1.fX) && p0.fY > p1.fY); |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 481 | } |
| 482 | |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 483 | // a point is "right" to another if its x-coord is greater, or if equal, its y-coord is less |
| 484 | static bool right(const SkPoint& p0, const SkPoint& p1) { |
| 485 | return p0.fX > p1.fX || (!(p0.fX < p1.fX) && p0.fY < p1.fY); |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 486 | } |
| 487 | |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 488 | struct Vertex { |
| 489 | static bool Left(const Vertex& qv0, const Vertex& qv1) { |
| 490 | return left(qv0.fPosition, qv1.fPosition); |
| 491 | } |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 492 | |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 493 | // packed to fit into 16 bytes (one cache line) |
| 494 | SkPoint fPosition; |
| 495 | uint16_t fIndex; // index in unsorted polygon |
| 496 | uint16_t fPrevIndex; // indices for previous and next vertex in unsorted polygon |
| 497 | uint16_t fNextIndex; |
| 498 | uint16_t fFlags; |
| 499 | }; |
| 500 | |
| 501 | enum VertexFlags { |
| 502 | kPrevLeft_VertexFlag = 0x1, |
| 503 | kNextLeft_VertexFlag = 0x2, |
| 504 | }; |
| 505 | |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 506 | struct ActiveEdge { |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 507 | ActiveEdge() : fChild{ nullptr, nullptr }, fAbove(nullptr), fBelow(nullptr), fRed(false) {} |
| 508 | ActiveEdge(const SkPoint& p0, const SkVector& v, uint16_t index0, uint16_t index1) |
| 509 | : fSegment({ p0, v }) |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 510 | , fIndex0(index0) |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 511 | , fIndex1(index1) |
| 512 | , fAbove(nullptr) |
| 513 | , fBelow(nullptr) |
| 514 | , fRed(true) { |
| 515 | fChild[0] = nullptr; |
| 516 | fChild[1] = nullptr; |
| 517 | } |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 518 | |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 519 | // Returns true if "this" is above "that", assuming this->p0 is to the left of that->p0 |
| 520 | // This is only used to verify the edgelist -- the actual test for insertion/deletion is much |
| 521 | // simpler because we can make certain assumptions then. |
| 522 | bool aboveIfLeft(const ActiveEdge* that) const { |
| 523 | const SkPoint& p0 = this->fSegment.fP0; |
| 524 | const SkPoint& q0 = that->fSegment.fP0; |
| 525 | SkASSERT(p0.fX <= q0.fX); |
| 526 | SkVector d = q0 - p0; |
| 527 | const SkVector& v = this->fSegment.fV; |
| 528 | const SkVector& w = that->fSegment.fV; |
| 529 | // The idea here is that if the vector between the origins of the two segments (d) |
| 530 | // rotates counterclockwise up to the vector representing the "this" segment (v), |
| 531 | // then we know that "this" is above "that". If the result is clockwise we say it's below. |
| 532 | if (this->fIndex0 != that->fIndex0) { |
| 533 | SkScalar cross = d.cross(v); |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 534 | if (cross > kCrossTolerance) { |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 535 | return true; |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 536 | } else if (cross < -kCrossTolerance) { |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 537 | return false; |
| 538 | } |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 539 | } else if (this->fIndex1 == that->fIndex1) { |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 540 | return false; |
| 541 | } |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 542 | // At this point either the two origins are nearly equal or the origin of "that" |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 543 | // lies on dv. So then we try the same for the vector from the tail of "this" |
| 544 | // to the head of "that". Again, ccw means "this" is above "that". |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 545 | // d = that.P1 - this.P0 |
| 546 | // = that.fP0 + that.fV - this.fP0 |
| 547 | // = that.fP0 - this.fP0 + that.fV |
| 548 | // = old_d + that.fV |
| 549 | d += w; |
| 550 | SkScalar cross = d.cross(v); |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 551 | if (cross > kCrossTolerance) { |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 552 | return true; |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 553 | } else if (cross < -kCrossTolerance) { |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 554 | return false; |
| 555 | } |
| 556 | // If the previous check fails, the two segments are nearly collinear |
| 557 | // First check y-coord of first endpoints |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 558 | if (p0.fX < q0.fX) { |
| 559 | return (p0.fY >= q0.fY); |
| 560 | } else if (p0.fY > q0.fY) { |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 561 | return true; |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 562 | } else if (p0.fY < q0.fY) { |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 563 | return false; |
| 564 | } |
| 565 | // The first endpoints are the same, so check the other endpoint |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 566 | SkPoint p1 = p0 + v; |
| 567 | SkPoint q1 = q0 + w; |
| 568 | if (p1.fX < q1.fX) { |
| 569 | return (p1.fY >= q1.fY); |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 570 | } else { |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 571 | return (p1.fY > q1.fY); |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 572 | } |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 573 | } |
| 574 | |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 575 | // same as leftAndAbove(), but generalized |
| 576 | bool above(const ActiveEdge* that) const { |
| 577 | const SkPoint& p0 = this->fSegment.fP0; |
| 578 | const SkPoint& q0 = that->fSegment.fP0; |
| 579 | if (right(p0, q0)) { |
| 580 | return !that->aboveIfLeft(this); |
| 581 | } else { |
| 582 | return this->aboveIfLeft(that); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | bool intersect(const SkPoint& q0, const SkVector& w, uint16_t index0, uint16_t index1) const { |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 587 | // check first to see if these edges are neighbors in the polygon |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 588 | if (this->fIndex0 == index0 || this->fIndex1 == index0 || |
| 589 | this->fIndex0 == index1 || this->fIndex1 == index1) { |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 590 | return false; |
| 591 | } |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 592 | |
| 593 | // We don't need the exact intersection point so we can do a simpler test here. |
| 594 | const SkPoint& p0 = this->fSegment.fP0; |
| 595 | const SkVector& v = this->fSegment.fV; |
| 596 | SkPoint p1 = p0 + v; |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 597 | SkPoint q1 = q0 + w; |
| 598 | |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 599 | // We assume some x-overlap due to how the edgelist works |
| 600 | // This allows us to simplify our test |
| 601 | // We need some slop here because storing the vector and recomputing the second endpoint |
| 602 | // doesn't necessary give us the original result in floating point. |
| 603 | // TODO: Store vector as double? Store endpoint as well? |
| 604 | SkASSERT(q0.fX <= p1.fX + SK_ScalarNearlyZero); |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 605 | |
| 606 | // if each segment straddles the other (i.e., the endpoints have different sides) |
| 607 | // then they intersect |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 608 | bool result; |
| 609 | if (p0.fX < q0.fX) { |
| 610 | if (q1.fX < p1.fX) { |
| 611 | result = (compute_side(p0, v, q0)*compute_side(p0, v, q1) < 0); |
| 612 | } else { |
| 613 | result = (compute_side(p0, v, q0)*compute_side(q0, w, p1) > 0); |
| 614 | } |
| 615 | } else { |
| 616 | if (p1.fX < q1.fX) { |
| 617 | result = (compute_side(q0, w, p0)*compute_side(q0, w, p1) < 0); |
| 618 | } else { |
| 619 | result = (compute_side(q0, w, p0)*compute_side(p0, v, q1) > 0); |
| 620 | } |
Jim Van Verth | ba4847c | 2018-08-07 16:02:33 -0400 | [diff] [blame] | 621 | } |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 622 | return result; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 623 | } |
| 624 | |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 625 | bool intersect(const ActiveEdge* edge) { |
| 626 | return this->intersect(edge->fSegment.fP0, edge->fSegment.fV, edge->fIndex0, edge->fIndex1); |
| 627 | } |
| 628 | |
| 629 | bool lessThan(const ActiveEdge* that) const { |
| 630 | SkASSERT(!this->above(this)); |
| 631 | SkASSERT(!that->above(that)); |
| 632 | SkASSERT(!(this->above(that) && that->above(this))); |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 633 | return this->above(that); |
| 634 | } |
| 635 | |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 636 | bool equals(uint16_t index0, uint16_t index1) const { |
| 637 | return (this->fIndex0 == index0 && this->fIndex1 == index1); |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 638 | } |
| 639 | |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 640 | OffsetSegment fSegment; |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 641 | uint16_t fIndex0; // indices for previous and next vertex in polygon |
Jim Van Verth | a631683 | 2018-07-24 09:30:37 -0400 | [diff] [blame] | 642 | uint16_t fIndex1; |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 643 | ActiveEdge* fChild[2]; |
| 644 | ActiveEdge* fAbove; |
| 645 | ActiveEdge* fBelow; |
| 646 | int32_t fRed; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 647 | }; |
| 648 | |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 649 | class ActiveEdgeList { |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 650 | public: |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 651 | ActiveEdgeList(int maxEdges) { |
| 652 | fAllocation = (char*) sk_malloc_throw(sizeof(ActiveEdge)*maxEdges); |
| 653 | fCurrFree = 0; |
| 654 | fMaxFree = maxEdges; |
| 655 | } |
| 656 | ~ActiveEdgeList() { |
| 657 | fTreeHead.fChild[1] = nullptr; |
| 658 | sk_free(fAllocation); |
| 659 | } |
| 660 | |
Jim Van Verth | a631683 | 2018-07-24 09:30:37 -0400 | [diff] [blame] | 661 | bool insert(const SkPoint& p0, const SkPoint& p1, uint16_t index0, uint16_t index1) { |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 662 | SkVector v = p1 - p0; |
Jim Van Verth | c77cd1a | 2018-10-23 11:54:26 -0400 | [diff] [blame] | 663 | if (!v.isFinite()) { |
| 664 | return false; |
| 665 | } |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 666 | // empty tree case -- easy |
| 667 | if (!fTreeHead.fChild[1]) { |
| 668 | ActiveEdge* root = fTreeHead.fChild[1] = this->allocate(p0, v, index0, index1); |
| 669 | SkASSERT(root); |
| 670 | if (!root) { |
| 671 | return false; |
| 672 | } |
| 673 | root->fRed = false; |
| 674 | return true; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 675 | } |
| 676 | |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 677 | // set up helpers |
| 678 | ActiveEdge* top = &fTreeHead; |
| 679 | ActiveEdge *grandparent = nullptr; |
| 680 | ActiveEdge *parent = nullptr; |
| 681 | ActiveEdge *curr = top->fChild[1]; |
| 682 | int dir = 0; |
| 683 | int last = 0; // ? |
| 684 | // predecessor and successor, for intersection check |
| 685 | ActiveEdge* pred = nullptr; |
| 686 | ActiveEdge* succ = nullptr; |
| 687 | |
| 688 | // search down the tree |
| 689 | while (true) { |
| 690 | if (!curr) { |
| 691 | // check for intersection with predecessor and successor |
| 692 | if ((pred && pred->intersect(p0, v, index0, index1)) || |
| 693 | (succ && succ->intersect(p0, v, index0, index1))) { |
| 694 | return false; |
| 695 | } |
| 696 | // insert new node at bottom |
| 697 | parent->fChild[dir] = curr = this->allocate(p0, v, index0, index1); |
| 698 | SkASSERT(curr); |
| 699 | if (!curr) { |
| 700 | return false; |
| 701 | } |
| 702 | curr->fAbove = pred; |
| 703 | curr->fBelow = succ; |
| 704 | if (pred) { |
| 705 | pred->fBelow = curr; |
| 706 | } |
| 707 | if (succ) { |
| 708 | succ->fAbove = curr; |
| 709 | } |
| 710 | if (IsRed(parent)) { |
| 711 | int dir2 = (top->fChild[1] == grandparent); |
| 712 | if (curr == parent->fChild[last]) { |
| 713 | top->fChild[dir2] = SingleRotation(grandparent, !last); |
| 714 | } else { |
| 715 | top->fChild[dir2] = DoubleRotation(grandparent, !last); |
| 716 | } |
| 717 | } |
| 718 | break; |
| 719 | } else if (IsRed(curr->fChild[0]) && IsRed(curr->fChild[1])) { |
| 720 | // color flip |
| 721 | curr->fRed = true; |
| 722 | curr->fChild[0]->fRed = false; |
| 723 | curr->fChild[1]->fRed = false; |
| 724 | if (IsRed(parent)) { |
| 725 | int dir2 = (top->fChild[1] == grandparent); |
| 726 | if (curr == parent->fChild[last]) { |
| 727 | top->fChild[dir2] = SingleRotation(grandparent, !last); |
| 728 | } else { |
| 729 | top->fChild[dir2] = DoubleRotation(grandparent, !last); |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | last = dir; |
| 735 | int side; |
| 736 | // check to see if segment is above or below |
| 737 | if (curr->fIndex0 == index0) { |
| 738 | side = compute_side(curr->fSegment.fP0, curr->fSegment.fV, p1); |
| 739 | } else { |
| 740 | side = compute_side(curr->fSegment.fP0, curr->fSegment.fV, p0); |
| 741 | } |
| 742 | if (0 == side) { |
| 743 | return false; |
| 744 | } |
| 745 | dir = (side < 0); |
| 746 | |
| 747 | if (0 == dir) { |
| 748 | succ = curr; |
| 749 | } else { |
| 750 | pred = curr; |
| 751 | } |
| 752 | |
| 753 | // update helpers |
| 754 | if (grandparent) { |
| 755 | top = grandparent; |
| 756 | } |
| 757 | grandparent = parent; |
| 758 | parent = curr; |
| 759 | curr = curr->fChild[dir]; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 760 | } |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 761 | |
| 762 | // update root and make it black |
| 763 | fTreeHead.fChild[1]->fRed = false; |
| 764 | |
| 765 | SkDEBUGCODE(VerifyTree(fTreeHead.fChild[1])); |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 766 | |
| 767 | return true; |
| 768 | } |
| 769 | |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 770 | // replaces edge p0p1 with p1p2 |
| 771 | bool replace(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2, |
| 772 | uint16_t index0, uint16_t index1, uint16_t index2) { |
| 773 | if (!fTreeHead.fChild[1]) { |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 774 | return false; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 775 | } |
| 776 | |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 777 | SkVector v = p2 - p1; |
| 778 | ActiveEdge* curr = &fTreeHead; |
| 779 | ActiveEdge* found = nullptr; |
| 780 | int dir = 1; |
| 781 | |
| 782 | // search |
| 783 | while (curr->fChild[dir] != nullptr) { |
| 784 | // update helpers |
| 785 | curr = curr->fChild[dir]; |
| 786 | // save found node |
| 787 | if (curr->equals(index0, index1)) { |
| 788 | found = curr; |
| 789 | break; |
| 790 | } else { |
| 791 | // check to see if segment is above or below |
| 792 | int side; |
| 793 | if (curr->fIndex1 == index1) { |
| 794 | side = compute_side(curr->fSegment.fP0, curr->fSegment.fV, p0); |
| 795 | } else { |
| 796 | side = compute_side(curr->fSegment.fP0, curr->fSegment.fV, p1); |
| 797 | } |
| 798 | if (0 == side) { |
| 799 | return false; |
| 800 | } |
| 801 | dir = (side < 0); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | if (!found) { |
| 806 | return false; |
| 807 | } |
| 808 | |
| 809 | // replace if found |
| 810 | ActiveEdge* pred = found->fAbove; |
| 811 | ActiveEdge* succ = found->fBelow; |
| 812 | // check deletion and insert intersection cases |
| 813 | if (pred && (pred->intersect(found) || pred->intersect(p1, v, index1, index2))) { |
| 814 | return false; |
| 815 | } |
| 816 | if (succ && (succ->intersect(found) || succ->intersect(p1, v, index1, index2))) { |
| 817 | return false; |
| 818 | } |
| 819 | found->fSegment.fP0 = p1; |
| 820 | found->fSegment.fV = v; |
| 821 | found->fIndex0 = index1; |
| 822 | found->fIndex1 = index2; |
| 823 | // above and below should stay the same |
| 824 | |
| 825 | SkDEBUGCODE(VerifyTree(fTreeHead.fChild[1])); |
| 826 | |
| 827 | return true; |
| 828 | } |
| 829 | |
| 830 | bool remove(const SkPoint& p0, const SkPoint& p1, uint16_t index0, uint16_t index1) { |
| 831 | if (!fTreeHead.fChild[1]) { |
| 832 | return false; |
| 833 | } |
| 834 | |
| 835 | ActiveEdge* curr = &fTreeHead; |
| 836 | ActiveEdge* parent = nullptr; |
| 837 | ActiveEdge* grandparent = nullptr; |
| 838 | ActiveEdge* found = nullptr; |
| 839 | int dir = 1; |
| 840 | |
| 841 | // search and push a red node down |
| 842 | while (curr->fChild[dir] != nullptr) { |
| 843 | int last = dir; |
| 844 | |
| 845 | // update helpers |
| 846 | grandparent = parent; |
| 847 | parent = curr; |
| 848 | curr = curr->fChild[dir]; |
| 849 | // save found node |
| 850 | if (curr->equals(index0, index1)) { |
| 851 | found = curr; |
| 852 | dir = 0; |
| 853 | } else { |
| 854 | // check to see if segment is above or below |
| 855 | int side; |
| 856 | if (curr->fIndex1 == index1) { |
| 857 | side = compute_side(curr->fSegment.fP0, curr->fSegment.fV, p0); |
| 858 | } else { |
| 859 | side = compute_side(curr->fSegment.fP0, curr->fSegment.fV, p1); |
| 860 | } |
| 861 | if (0 == side) { |
| 862 | return false; |
| 863 | } |
| 864 | dir = (side < 0); |
| 865 | } |
| 866 | |
| 867 | // push the red node down |
| 868 | if (!IsRed(curr) && !IsRed(curr->fChild[dir])) { |
| 869 | if (IsRed(curr->fChild[!dir])) { |
| 870 | parent = parent->fChild[last] = SingleRotation(curr, dir); |
| 871 | } else { |
| 872 | ActiveEdge *s = parent->fChild[!last]; |
| 873 | |
| 874 | if (s != NULL) { |
| 875 | if (!IsRed(s->fChild[!last]) && !IsRed(s->fChild[last])) { |
| 876 | // color flip |
| 877 | parent->fRed = false; |
| 878 | s->fRed = true; |
| 879 | curr->fRed = true; |
| 880 | } else { |
| 881 | int dir2 = (grandparent->fChild[1] == parent); |
| 882 | |
| 883 | if (IsRed(s->fChild[last])) { |
| 884 | grandparent->fChild[dir2] = DoubleRotation(parent, last); |
| 885 | } else if (IsRed(s->fChild[!last])) { |
| 886 | grandparent->fChild[dir2] = SingleRotation(parent, last); |
| 887 | } |
| 888 | |
| 889 | // ensure correct coloring |
| 890 | curr->fRed = grandparent->fChild[dir2]->fRed = true; |
| 891 | grandparent->fChild[dir2]->fChild[0]->fRed = false; |
| 892 | grandparent->fChild[dir2]->fChild[1]->fRed = false; |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | // replace and remove if found |
| 900 | if (found) { |
| 901 | ActiveEdge* pred = found->fAbove; |
| 902 | ActiveEdge* succ = found->fBelow; |
| 903 | if ((pred && pred->intersect(found)) || (succ && succ->intersect(found))) { |
| 904 | return false; |
| 905 | } |
| 906 | if (found != curr) { |
| 907 | found->fSegment = curr->fSegment; |
| 908 | found->fIndex0 = curr->fIndex0; |
| 909 | found->fIndex1 = curr->fIndex1; |
| 910 | found->fAbove = curr->fAbove; |
| 911 | pred = found->fAbove; |
| 912 | // we don't need to set found->fBelow here |
| 913 | } else { |
| 914 | if (succ) { |
| 915 | succ->fAbove = pred; |
| 916 | } |
| 917 | } |
| 918 | if (pred) { |
| 919 | pred->fBelow = curr->fBelow; |
| 920 | } |
| 921 | parent->fChild[parent->fChild[1] == curr] = curr->fChild[!curr->fChild[0]]; |
| 922 | |
| 923 | // no need to delete |
| 924 | curr->fAbove = reinterpret_cast<ActiveEdge*>(0xdeadbeefll); |
| 925 | curr->fBelow = reinterpret_cast<ActiveEdge*>(0xdeadbeefll); |
| 926 | if (fTreeHead.fChild[1]) { |
| 927 | fTreeHead.fChild[1]->fRed = false; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | // update root and make it black |
| 932 | if (fTreeHead.fChild[1]) { |
| 933 | fTreeHead.fChild[1]->fRed = false; |
| 934 | } |
| 935 | |
| 936 | SkDEBUGCODE(VerifyTree(fTreeHead.fChild[1])); |
| 937 | |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 938 | return true; |
| 939 | } |
| 940 | |
| 941 | private: |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 942 | // allocator |
| 943 | ActiveEdge * allocate(const SkPoint& p0, const SkPoint& p1, uint16_t index0, uint16_t index1) { |
| 944 | if (fCurrFree >= fMaxFree) { |
| 945 | return nullptr; |
| 946 | } |
| 947 | char* bytes = fAllocation + sizeof(ActiveEdge)*fCurrFree; |
| 948 | ++fCurrFree; |
| 949 | return new(bytes) ActiveEdge(p0, p1, index0, index1); |
| 950 | } |
| 951 | |
| 952 | /////////////////////////////////////////////////////////////////////////////////// |
| 953 | // Red-black tree methods |
| 954 | /////////////////////////////////////////////////////////////////////////////////// |
| 955 | static bool IsRed(const ActiveEdge* node) { |
| 956 | return node && node->fRed; |
| 957 | } |
| 958 | |
| 959 | static ActiveEdge* SingleRotation(ActiveEdge* node, int dir) { |
| 960 | ActiveEdge* tmp = node->fChild[!dir]; |
| 961 | |
| 962 | node->fChild[!dir] = tmp->fChild[dir]; |
| 963 | tmp->fChild[dir] = node; |
| 964 | |
| 965 | node->fRed = true; |
| 966 | tmp->fRed = false; |
| 967 | |
| 968 | return tmp; |
| 969 | } |
| 970 | |
| 971 | static ActiveEdge* DoubleRotation(ActiveEdge* node, int dir) { |
| 972 | node->fChild[!dir] = SingleRotation(node->fChild[!dir], !dir); |
| 973 | |
| 974 | return SingleRotation(node, dir); |
| 975 | } |
| 976 | |
| 977 | // returns black link count |
| 978 | static int VerifyTree(const ActiveEdge* tree) { |
| 979 | if (!tree) { |
| 980 | return 1; |
| 981 | } |
| 982 | |
| 983 | const ActiveEdge* left = tree->fChild[0]; |
| 984 | const ActiveEdge* right = tree->fChild[1]; |
| 985 | |
| 986 | // no consecutive red links |
| 987 | if (IsRed(tree) && (IsRed(left) || IsRed(right))) { |
| 988 | SkASSERT(false); |
| 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | // check secondary links |
| 993 | if (tree->fAbove) { |
| 994 | SkASSERT(tree->fAbove->fBelow == tree); |
| 995 | SkASSERT(tree->fAbove->lessThan(tree)); |
| 996 | } |
| 997 | if (tree->fBelow) { |
| 998 | SkASSERT(tree->fBelow->fAbove == tree); |
| 999 | SkASSERT(tree->lessThan(tree->fBelow)); |
| 1000 | } |
| 1001 | |
| 1002 | // violates binary tree order |
| 1003 | if ((left && tree->lessThan(left)) || (right && right->lessThan(tree))) { |
| 1004 | SkASSERT(false); |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
| 1008 | int leftCount = VerifyTree(left); |
| 1009 | int rightCount = VerifyTree(right); |
| 1010 | |
| 1011 | // return black link count |
| 1012 | if (leftCount != 0 && rightCount != 0) { |
| 1013 | // black height mismatch |
| 1014 | if (leftCount != rightCount) { |
| 1015 | SkASSERT(false); |
| 1016 | return 0; |
| 1017 | } |
| 1018 | return IsRed(tree) ? leftCount : leftCount + 1; |
| 1019 | } else { |
| 1020 | return 0; |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | ActiveEdge fTreeHead; |
| 1025 | char* fAllocation; |
| 1026 | int fCurrFree; |
| 1027 | int fMaxFree; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1028 | }; |
| 1029 | |
| 1030 | // Here we implement a sweep line algorithm to determine whether the provided points |
| 1031 | // represent a simple polygon, i.e., the polygon is non-self-intersecting. |
| 1032 | // We first insert the vertices into a priority queue sorting horizontally from left to right. |
| 1033 | // Then as we pop the vertices from the queue we generate events which indicate that an edge |
| 1034 | // should be added or removed from an edge list. If any intersections are detected in the edge |
| 1035 | // list, then we know the polygon is self-intersecting and hence not simple. |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1036 | bool SkIsSimplePolygon(const SkPoint* polygon, int polygonSize) { |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 1037 | if (polygonSize < 3) { |
| 1038 | return false; |
| 1039 | } |
| 1040 | |
Jim Van Verth | a631683 | 2018-07-24 09:30:37 -0400 | [diff] [blame] | 1041 | // need to be able to represent all the vertices in the 16-bit indices |
Jim Van Verth | b7c9551 | 2018-09-11 12:57:42 -0400 | [diff] [blame] | 1042 | if (polygonSize > std::numeric_limits<uint16_t>::max()) { |
Jim Van Verth | a631683 | 2018-07-24 09:30:37 -0400 | [diff] [blame] | 1043 | return false; |
| 1044 | } |
| 1045 | |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 1046 | // If it's convex, it's simple |
| 1047 | if (SkIsConvexPolygon(polygon, polygonSize)) { |
| 1048 | return true; |
| 1049 | } |
| 1050 | |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1051 | SkTDPQueue <Vertex, Vertex::Left> vertexQueue(polygonSize); |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1052 | for (int i = 0; i < polygonSize; ++i) { |
| 1053 | Vertex newVertex; |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 1054 | if (!polygon[i].isFinite()) { |
| 1055 | return false; |
| 1056 | } |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1057 | newVertex.fPosition = polygon[i]; |
| 1058 | newVertex.fIndex = i; |
| 1059 | newVertex.fPrevIndex = (i - 1 + polygonSize) % polygonSize; |
| 1060 | newVertex.fNextIndex = (i + 1) % polygonSize; |
| 1061 | newVertex.fFlags = 0; |
| 1062 | if (left(polygon[newVertex.fPrevIndex], polygon[i])) { |
| 1063 | newVertex.fFlags |= kPrevLeft_VertexFlag; |
| 1064 | } |
| 1065 | if (left(polygon[newVertex.fNextIndex], polygon[i])) { |
| 1066 | newVertex.fFlags |= kNextLeft_VertexFlag; |
| 1067 | } |
| 1068 | vertexQueue.insert(newVertex); |
| 1069 | } |
| 1070 | |
| 1071 | // pop each vertex from the queue and generate events depending on |
| 1072 | // where it lies relative to its neighboring edges |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 1073 | ActiveEdgeList sweepLine(polygonSize); |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1074 | while (vertexQueue.count() > 0) { |
| 1075 | const Vertex& v = vertexQueue.peek(); |
| 1076 | |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 1077 | // both to the right -- insert both |
| 1078 | if (v.fFlags == 0) { |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1079 | if (!sweepLine.insert(v.fPosition, polygon[v.fPrevIndex], v.fIndex, v.fPrevIndex)) { |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1080 | break; |
| 1081 | } |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1082 | if (!sweepLine.insert(v.fPosition, polygon[v.fNextIndex], v.fIndex, v.fNextIndex)) { |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1083 | break; |
| 1084 | } |
Jim Van Verth | 04d1632 | 2018-08-15 15:01:35 -0400 | [diff] [blame] | 1085 | // both to the left -- remove both |
| 1086 | } else if (v.fFlags == (kPrevLeft_VertexFlag | kNextLeft_VertexFlag)) { |
| 1087 | if (!sweepLine.remove(polygon[v.fPrevIndex], v.fPosition, v.fPrevIndex, v.fIndex)) { |
| 1088 | break; |
| 1089 | } |
| 1090 | if (!sweepLine.remove(polygon[v.fNextIndex], v.fPosition, v.fNextIndex, v.fIndex)) { |
| 1091 | break; |
| 1092 | } |
| 1093 | // one to left and right -- replace one with another |
| 1094 | } else { |
| 1095 | if (v.fFlags & kPrevLeft_VertexFlag) { |
| 1096 | if (!sweepLine.replace(polygon[v.fPrevIndex], v.fPosition, polygon[v.fNextIndex], |
| 1097 | v.fPrevIndex, v.fIndex, v.fNextIndex)) { |
| 1098 | break; |
| 1099 | } |
| 1100 | } else { |
| 1101 | SkASSERT(v.fFlags & kNextLeft_VertexFlag); |
| 1102 | if (!sweepLine.replace(polygon[v.fNextIndex], v.fPosition, polygon[v.fPrevIndex], |
| 1103 | v.fNextIndex, v.fIndex, v.fPrevIndex)) { |
| 1104 | break; |
| 1105 | } |
| 1106 | } |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | vertexQueue.pop(); |
| 1110 | } |
| 1111 | |
| 1112 | return (vertexQueue.count() == 0); |
| 1113 | } |
| 1114 | |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1115 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 1116 | |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1117 | // helper function for SkOffsetSimplePolygon |
| 1118 | static void setup_offset_edge(OffsetEdge* currEdge, |
| 1119 | const SkPoint& endpoint0, const SkPoint& endpoint1, |
Jim Van Verth | eddb3d9 | 2018-08-02 10:56:26 -0400 | [diff] [blame] | 1120 | uint16_t startIndex, uint16_t endIndex) { |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 1121 | currEdge->fOffset.fP0 = endpoint0; |
| 1122 | currEdge->fOffset.fV = endpoint1 - endpoint0; |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1123 | currEdge->init(startIndex, endIndex); |
| 1124 | } |
| 1125 | |
Jim Van Verth | 98d3375 | 2018-08-03 15:59:46 -0400 | [diff] [blame] | 1126 | static bool is_reflex_vertex(const SkPoint* inputPolygonVerts, int winding, SkScalar offset, |
| 1127 | uint16_t prevIndex, uint16_t currIndex, uint16_t nextIndex) { |
| 1128 | int side = compute_side(inputPolygonVerts[prevIndex], |
| 1129 | inputPolygonVerts[currIndex] - inputPolygonVerts[prevIndex], |
| 1130 | inputPolygonVerts[nextIndex]); |
| 1131 | // if reflex point, we need to add extra edges |
| 1132 | return (side*winding*offset < 0); |
| 1133 | } |
| 1134 | |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 1135 | bool SkOffsetSimplePolygon(const SkPoint* inputPolygonVerts, int inputPolygonSize, SkScalar offset, |
Jim Van Verth | bdde428 | 2018-06-14 09:09:18 -0400 | [diff] [blame] | 1136 | SkTDArray<SkPoint>* offsetPolygon, SkTDArray<int>* polygonIndices) { |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1137 | if (inputPolygonSize < 3) { |
| 1138 | return false; |
| 1139 | } |
| 1140 | |
Jim Van Verth | a631683 | 2018-07-24 09:30:37 -0400 | [diff] [blame] | 1141 | // need to be able to represent all the vertices in the 16-bit indices |
Jim Van Verth | b7c9551 | 2018-09-11 12:57:42 -0400 | [diff] [blame] | 1142 | if (inputPolygonSize >= std::numeric_limits<uint16_t>::max()) { |
Jim Van Verth | a631683 | 2018-07-24 09:30:37 -0400 | [diff] [blame] | 1143 | return false; |
| 1144 | } |
| 1145 | |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 1146 | if (!SkScalarIsFinite(offset)) { |
| 1147 | return false; |
| 1148 | } |
| 1149 | |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1150 | // get winding direction |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 1151 | int winding = SkGetPolygonWinding(inputPolygonVerts, inputPolygonSize); |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1152 | if (0 == winding) { |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1153 | return false; |
| 1154 | } |
| 1155 | |
Jim Van Verth | bdde428 | 2018-06-14 09:09:18 -0400 | [diff] [blame] | 1156 | // build normals |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 1157 | SkAutoSTMalloc<64, SkVector> normals(inputPolygonSize); |
Jim Van Verth | b7c9551 | 2018-09-11 12:57:42 -0400 | [diff] [blame] | 1158 | unsigned int numEdges = 0; |
Jim Van Verth | 98d3375 | 2018-08-03 15:59:46 -0400 | [diff] [blame] | 1159 | for (int currIndex = 0, prevIndex = inputPolygonSize - 1; |
| 1160 | currIndex < inputPolygonSize; |
| 1161 | prevIndex = currIndex, ++currIndex) { |
| 1162 | if (!inputPolygonVerts[currIndex].isFinite()) { |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 1163 | return false; |
| 1164 | } |
Jim Van Verth | 98d3375 | 2018-08-03 15:59:46 -0400 | [diff] [blame] | 1165 | int nextIndex = (currIndex + 1) % inputPolygonSize; |
Jim Van Verth | 8222f3b | 2018-11-29 13:14:17 -0500 | [diff] [blame] | 1166 | if (!compute_offset_vector(inputPolygonVerts[currIndex], inputPolygonVerts[nextIndex], |
| 1167 | offset, winding, &normals[currIndex])) { |
| 1168 | return false; |
| 1169 | } |
Jim Van Verth | 98d3375 | 2018-08-03 15:59:46 -0400 | [diff] [blame] | 1170 | if (currIndex > 0) { |
| 1171 | // if reflex point, we need to add extra edges |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 1172 | if (is_reflex_vertex(inputPolygonVerts, winding, offset, |
Jim Van Verth | 98d3375 | 2018-08-03 15:59:46 -0400 | [diff] [blame] | 1173 | prevIndex, currIndex, nextIndex)) { |
| 1174 | SkScalar rotSin, rotCos; |
| 1175 | int numSteps; |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 1176 | if (!SkComputeRadialSteps(normals[prevIndex], normals[currIndex], offset, |
Jim Van Verth | 98d3375 | 2018-08-03 15:59:46 -0400 | [diff] [blame] | 1177 | &rotSin, &rotCos, &numSteps)) { |
| 1178 | return false; |
| 1179 | } |
| 1180 | numEdges += SkTMax(numSteps, 1); |
| 1181 | } |
| 1182 | } |
| 1183 | numEdges++; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1184 | } |
Jim Van Verth | 98d3375 | 2018-08-03 15:59:46 -0400 | [diff] [blame] | 1185 | // finish up the edge counting |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 1186 | if (is_reflex_vertex(inputPolygonVerts, winding, offset, inputPolygonSize-1, 0, 1)) { |
Jim Van Verth | 98d3375 | 2018-08-03 15:59:46 -0400 | [diff] [blame] | 1187 | SkScalar rotSin, rotCos; |
| 1188 | int numSteps; |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 1189 | if (!SkComputeRadialSteps(normals[inputPolygonSize-1], normals[0], offset, |
Jim Van Verth | 98d3375 | 2018-08-03 15:59:46 -0400 | [diff] [blame] | 1190 | &rotSin, &rotCos, &numSteps)) { |
| 1191 | return false; |
| 1192 | } |
| 1193 | numEdges += SkTMax(numSteps, 1); |
| 1194 | } |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1195 | |
Jim Van Verth | b7c9551 | 2018-09-11 12:57:42 -0400 | [diff] [blame] | 1196 | // Make sure we don't overflow the max array count. |
| 1197 | // We shouldn't overflow numEdges, as SkComputeRadialSteps returns a max of 2^16-1, |
| 1198 | // and we have a max of 2^16-1 original vertices. |
| 1199 | if (numEdges > (unsigned int)std::numeric_limits<int32_t>::max()) { |
| 1200 | return false; |
| 1201 | } |
| 1202 | |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1203 | // build initial offset edge list |
Jim Van Verth | 98d3375 | 2018-08-03 15:59:46 -0400 | [diff] [blame] | 1204 | SkSTArray<64, OffsetEdge> edgeData(numEdges); |
| 1205 | OffsetEdge* prevEdge = nullptr; |
| 1206 | for (int currIndex = 0, prevIndex = inputPolygonSize - 1; |
| 1207 | currIndex < inputPolygonSize; |
| 1208 | prevIndex = currIndex, ++currIndex) { |
| 1209 | int nextIndex = (currIndex + 1) % inputPolygonSize; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1210 | // if reflex point, fill in curve |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 1211 | if (is_reflex_vertex(inputPolygonVerts, winding, offset, |
Jim Van Verth | 98d3375 | 2018-08-03 15:59:46 -0400 | [diff] [blame] | 1212 | prevIndex, currIndex, nextIndex)) { |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1213 | SkScalar rotSin, rotCos; |
| 1214 | int numSteps; |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 1215 | SkVector prevNormal = normals[prevIndex]; |
| 1216 | if (!SkComputeRadialSteps(prevNormal, normals[currIndex], offset, |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 1217 | &rotSin, &rotCos, &numSteps)) { |
| 1218 | return false; |
| 1219 | } |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1220 | auto currEdge = edgeData.push_back_n(SkTMax(numSteps, 1)); |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1221 | for (int i = 0; i < numSteps - 1; ++i) { |
| 1222 | SkVector currNormal = SkVector::Make(prevNormal.fX*rotCos - prevNormal.fY*rotSin, |
| 1223 | prevNormal.fY*rotCos + prevNormal.fX*rotSin); |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1224 | setup_offset_edge(currEdge, |
| 1225 | inputPolygonVerts[currIndex] + prevNormal, |
| 1226 | inputPolygonVerts[currIndex] + currNormal, |
| 1227 | currIndex, currIndex); |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1228 | prevNormal = currNormal; |
Jim Van Verth | 98d3375 | 2018-08-03 15:59:46 -0400 | [diff] [blame] | 1229 | currEdge->fPrev = prevEdge; |
| 1230 | if (prevEdge) { |
| 1231 | prevEdge->fNext = currEdge; |
| 1232 | } |
| 1233 | prevEdge = currEdge; |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1234 | ++currEdge; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1235 | } |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1236 | setup_offset_edge(currEdge, |
| 1237 | inputPolygonVerts[currIndex] + prevNormal, |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 1238 | inputPolygonVerts[currIndex] + normals[currIndex], |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1239 | currIndex, currIndex); |
Jim Van Verth | 98d3375 | 2018-08-03 15:59:46 -0400 | [diff] [blame] | 1240 | currEdge->fPrev = prevEdge; |
| 1241 | if (prevEdge) { |
| 1242 | prevEdge->fNext = currEdge; |
| 1243 | } |
| 1244 | prevEdge = currEdge; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | // Add the edge |
Jim Van Verth | 98d3375 | 2018-08-03 15:59:46 -0400 | [diff] [blame] | 1248 | auto currEdge = edgeData.push_back_n(1); |
| 1249 | setup_offset_edge(currEdge, |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 1250 | inputPolygonVerts[currIndex] + normals[currIndex], |
| 1251 | inputPolygonVerts[nextIndex] + normals[currIndex], |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1252 | currIndex, nextIndex); |
Jim Van Verth | 98d3375 | 2018-08-03 15:59:46 -0400 | [diff] [blame] | 1253 | currEdge->fPrev = prevEdge; |
| 1254 | if (prevEdge) { |
| 1255 | prevEdge->fNext = currEdge; |
| 1256 | } |
| 1257 | prevEdge = currEdge; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1258 | } |
Jim Van Verth | 98d3375 | 2018-08-03 15:59:46 -0400 | [diff] [blame] | 1259 | // close up the linked list |
| 1260 | SkASSERT(prevEdge); |
| 1261 | prevEdge->fNext = &edgeData[0]; |
| 1262 | edgeData[0].fPrev = prevEdge; |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1263 | |
| 1264 | // now clip edges |
Jim Van Verth | b7c9551 | 2018-09-11 12:57:42 -0400 | [diff] [blame] | 1265 | SkASSERT(edgeData.count() == (int)numEdges); |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1266 | auto head = &edgeData[0]; |
| 1267 | auto currEdge = head; |
Jim Van Verth | b7c9551 | 2018-09-11 12:57:42 -0400 | [diff] [blame] | 1268 | unsigned int offsetVertexCount = numEdges; |
| 1269 | unsigned long long iterations = 0; |
Kevin Lubick | bfd90e3 | 2018-11-26 13:01:10 -0500 | [diff] [blame] | 1270 | unsigned long long maxIterations = (unsigned long long)(numEdges) * numEdges; |
Jim Van Verth | b7c9551 | 2018-09-11 12:57:42 -0400 | [diff] [blame] | 1271 | while (head && prevEdge != currEdge && offsetVertexCount > 0) { |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 1272 | ++iterations; |
| 1273 | // we should check each edge against each other edge at most once |
Jim Van Verth | b7c9551 | 2018-09-11 12:57:42 -0400 | [diff] [blame] | 1274 | if (iterations > maxIterations) { |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 1275 | return false; |
| 1276 | } |
| 1277 | |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1278 | SkScalar s, t; |
| 1279 | SkPoint intersection; |
Jim Van Verth | a631683 | 2018-07-24 09:30:37 -0400 | [diff] [blame] | 1280 | if (prevEdge->checkIntersection(currEdge, &intersection, &s, &t)) { |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1281 | // if new intersection is further back on previous inset from the prior intersection |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1282 | if (s < prevEdge->fTValue) { |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1283 | // no point in considering this one again |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1284 | remove_node(prevEdge, &head); |
| 1285 | --offsetVertexCount; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1286 | // go back one segment |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1287 | prevEdge = prevEdge->fPrev; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1288 | // we've already considered this intersection, we're done |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1289 | } else if (currEdge->fTValue > SK_ScalarMin && |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1290 | SkPointPriv::EqualsWithinTolerance(intersection, |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1291 | currEdge->fIntersection, |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1292 | 1.0e-6f)) { |
| 1293 | break; |
| 1294 | } else { |
| 1295 | // add intersection |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1296 | currEdge->fIntersection = intersection; |
| 1297 | currEdge->fTValue = t; |
| 1298 | currEdge->fIndex = prevEdge->fEnd; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1299 | |
| 1300 | // go to next segment |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1301 | prevEdge = currEdge; |
| 1302 | currEdge = currEdge->fNext; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1303 | } |
| 1304 | } else { |
| 1305 | // If there is no intersection, we want to minimize the distance between |
| 1306 | // the point where the segment lines cross and the segments themselves. |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1307 | OffsetEdge* prevPrevEdge = prevEdge->fPrev; |
| 1308 | OffsetEdge* currNextEdge = currEdge->fNext; |
Jim Van Verth | eddb3d9 | 2018-08-02 10:56:26 -0400 | [diff] [blame] | 1309 | SkScalar dist0 = currEdge->computeCrossingDistance(prevPrevEdge); |
| 1310 | SkScalar dist1 = prevEdge->computeCrossingDistance(currNextEdge); |
| 1311 | // if both lead to direct collision |
| 1312 | if (dist0 < 0 && dist1 < 0) { |
| 1313 | // check first to see if either represent parts of one contour |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 1314 | SkPoint p1 = prevPrevEdge->fOffset.fP0 + prevPrevEdge->fOffset.fV; |
Jim Van Verth | eddb3d9 | 2018-08-02 10:56:26 -0400 | [diff] [blame] | 1315 | bool prevSameContour = SkPointPriv::EqualsWithinTolerance(p1, |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 1316 | prevEdge->fOffset.fP0); |
| 1317 | p1 = currEdge->fOffset.fP0 + currEdge->fOffset.fV; |
Jim Van Verth | eddb3d9 | 2018-08-02 10:56:26 -0400 | [diff] [blame] | 1318 | bool currSameContour = SkPointPriv::EqualsWithinTolerance(p1, |
Jim Van Verth | da58cac | 2018-09-05 12:41:56 -0400 | [diff] [blame] | 1319 | currNextEdge->fOffset.fP0); |
Jim Van Verth | eddb3d9 | 2018-08-02 10:56:26 -0400 | [diff] [blame] | 1320 | |
| 1321 | // want to step along contour to find intersections rather than jump to new one |
| 1322 | if (currSameContour && !prevSameContour) { |
| 1323 | remove_node(currEdge, &head); |
| 1324 | currEdge = currNextEdge; |
| 1325 | --offsetVertexCount; |
| 1326 | continue; |
| 1327 | } else if (prevSameContour && !currSameContour) { |
| 1328 | remove_node(prevEdge, &head); |
| 1329 | prevEdge = prevPrevEdge; |
| 1330 | --offsetVertexCount; |
| 1331 | continue; |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | // otherwise minimize collision distance along segment |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1336 | if (dist0 < dist1) { |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1337 | remove_node(prevEdge, &head); |
| 1338 | prevEdge = prevPrevEdge; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1339 | } else { |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1340 | remove_node(currEdge, &head); |
| 1341 | currEdge = currNextEdge; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1342 | } |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1343 | --offsetVertexCount; |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | // store all the valid intersections that aren't nearly coincident |
| 1348 | // TODO: look at the main algorithm and see if we can detect these better |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1349 | offsetPolygon->reset(); |
Jim Van Verth | b7c9551 | 2018-09-11 12:57:42 -0400 | [diff] [blame] | 1350 | if (!head || offsetVertexCount == 0 || |
| 1351 | offsetVertexCount >= std::numeric_limits<uint16_t>::max()) { |
| 1352 | return false; |
| 1353 | } |
| 1354 | |
| 1355 | static constexpr SkScalar kCleanupTolerance = 0.01f; |
| 1356 | offsetPolygon->setReserve(offsetVertexCount); |
| 1357 | int currIndex = 0; |
| 1358 | *offsetPolygon->push() = head->fIntersection; |
| 1359 | if (polygonIndices) { |
| 1360 | *polygonIndices->push() = head->fIndex; |
| 1361 | } |
| 1362 | currEdge = head->fNext; |
| 1363 | while (currEdge != head) { |
| 1364 | if (!SkPointPriv::EqualsWithinTolerance(currEdge->fIntersection, |
| 1365 | (*offsetPolygon)[currIndex], |
| 1366 | kCleanupTolerance)) { |
| 1367 | *offsetPolygon->push() = currEdge->fIntersection; |
| 1368 | if (polygonIndices) { |
| 1369 | *polygonIndices->push() = currEdge->fIndex; |
| 1370 | } |
| 1371 | currIndex++; |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 1372 | } |
| 1373 | currEdge = currEdge->fNext; |
Jim Van Verth | b7c9551 | 2018-09-11 12:57:42 -0400 | [diff] [blame] | 1374 | } |
| 1375 | // make sure the first and last points aren't coincident |
| 1376 | if (currIndex >= 1 && |
| 1377 | SkPointPriv::EqualsWithinTolerance((*offsetPolygon)[0], (*offsetPolygon)[currIndex], |
| 1378 | kCleanupTolerance)) { |
| 1379 | offsetPolygon->pop(); |
| 1380 | if (polygonIndices) { |
| 1381 | polygonIndices->pop(); |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 1382 | } |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1383 | } |
| 1384 | |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1385 | // check winding of offset polygon (it should be same as the original polygon) |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 1386 | SkScalar offsetWinding = SkGetPolygonWinding(offsetPolygon->begin(), offsetPolygon->count()); |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1387 | |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1388 | return (winding*offsetWinding > 0 && |
| 1389 | SkIsSimplePolygon(offsetPolygon->begin(), offsetPolygon->count())); |
Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame] | 1390 | } |
| 1391 | |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1392 | ////////////////////////////////////////////////////////////////////////////////////////// |
| 1393 | |
| 1394 | struct TriangulationVertex { |
| 1395 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(TriangulationVertex); |
| 1396 | |
| 1397 | enum class VertexType { kConvex, kReflex }; |
| 1398 | |
| 1399 | SkPoint fPosition; |
| 1400 | VertexType fVertexType; |
| 1401 | uint16_t fIndex; |
| 1402 | uint16_t fPrevIndex; |
| 1403 | uint16_t fNextIndex; |
| 1404 | }; |
| 1405 | |
Jim Van Verth | 9b21825 | 2018-09-21 14:27:35 -0400 | [diff] [blame] | 1406 | static void compute_triangle_bounds(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2, |
| 1407 | SkRect* bounds) { |
| 1408 | Sk4s min, max; |
| 1409 | min = max = Sk4s(p0.fX, p0.fY, p0.fX, p0.fY); |
| 1410 | Sk4s xy(p1.fX, p1.fY, p2.fX, p2.fY); |
| 1411 | min = Sk4s::Min(min, xy); |
| 1412 | max = Sk4s::Max(max, xy); |
| 1413 | bounds->set(SkTMin(min[0], min[2]), SkTMin(min[1], min[3]), |
| 1414 | SkTMax(max[0], max[2]), SkTMax(max[1], max[3])); |
| 1415 | } |
| 1416 | |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1417 | // test to see if point p is in triangle p0p1p2. |
| 1418 | // for now assuming strictly inside -- if on the edge it's outside |
| 1419 | static bool point_in_triangle(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2, |
| 1420 | const SkPoint& p) { |
| 1421 | SkVector v0 = p1 - p0; |
| 1422 | SkVector v1 = p2 - p1; |
| 1423 | SkScalar n = v0.cross(v1); |
| 1424 | |
| 1425 | SkVector w0 = p - p0; |
| 1426 | if (n*v0.cross(w0) < SK_ScalarNearlyZero) { |
| 1427 | return false; |
| 1428 | } |
| 1429 | |
| 1430 | SkVector w1 = p - p1; |
| 1431 | if (n*v1.cross(w1) < SK_ScalarNearlyZero) { |
| 1432 | return false; |
| 1433 | } |
| 1434 | |
| 1435 | SkVector v2 = p0 - p2; |
| 1436 | SkVector w2 = p - p2; |
| 1437 | if (n*v2.cross(w2) < SK_ScalarNearlyZero) { |
| 1438 | return false; |
| 1439 | } |
| 1440 | |
| 1441 | return true; |
| 1442 | } |
| 1443 | |
| 1444 | // Data structure to track reflex vertices and check whether any are inside a given triangle |
| 1445 | class ReflexHash { |
| 1446 | public: |
Jim Van Verth | 6a300ac | 2018-10-23 13:19:21 -0400 | [diff] [blame] | 1447 | bool init(const SkRect& bounds, int vertexCount) { |
| 1448 | fBounds = bounds; |
| 1449 | fNumVerts = 0; |
Jim Van Verth | 5b5ab12 | 2018-10-31 13:26:18 -0400 | [diff] [blame] | 1450 | SkScalar width = bounds.width(); |
| 1451 | SkScalar height = bounds.height(); |
| 1452 | if (!SkScalarIsFinite(width) || !SkScalarIsFinite(height)) { |
| 1453 | return false; |
| 1454 | } |
Jim Van Verth | 6a300ac | 2018-10-23 13:19:21 -0400 | [diff] [blame] | 1455 | |
Jim Van Verth | 9b21825 | 2018-09-21 14:27:35 -0400 | [diff] [blame] | 1456 | // We want vertexCount grid cells, roughly distributed to match the bounds ratio |
Jim Van Verth | 5b5ab12 | 2018-10-31 13:26:18 -0400 | [diff] [blame] | 1457 | SkScalar hCount = SkScalarSqrt(sk_ieee_float_divide(vertexCount*width, height)); |
Jim Van Verth | 6a300ac | 2018-10-23 13:19:21 -0400 | [diff] [blame] | 1458 | if (!SkScalarIsFinite(hCount)) { |
| 1459 | return false; |
| 1460 | } |
Jim Van Verth | ac9f090 | 2018-09-24 12:24:15 -0400 | [diff] [blame] | 1461 | fHCount = SkTMax(SkTMin(SkScalarRoundToInt(hCount), vertexCount), 1); |
Jim Van Verth | 9b21825 | 2018-09-21 14:27:35 -0400 | [diff] [blame] | 1462 | fVCount = vertexCount/fHCount; |
Jim Van Verth | 5b5ab12 | 2018-10-31 13:26:18 -0400 | [diff] [blame] | 1463 | fGridConversion.set(sk_ieee_float_divide(fHCount - 0.001f, width), |
| 1464 | sk_ieee_float_divide(fVCount - 0.001f, height)); |
Jim Van Verth | 6a300ac | 2018-10-23 13:19:21 -0400 | [diff] [blame] | 1465 | if (!fGridConversion.isFinite()) { |
| 1466 | return false; |
| 1467 | } |
| 1468 | |
Jim Van Verth | 9b21825 | 2018-09-21 14:27:35 -0400 | [diff] [blame] | 1469 | fGrid.setCount(fHCount*fVCount); |
| 1470 | for (int i = 0; i < fGrid.count(); ++i) { |
| 1471 | fGrid[i].reset(); |
| 1472 | } |
Jim Van Verth | 6a300ac | 2018-10-23 13:19:21 -0400 | [diff] [blame] | 1473 | |
| 1474 | return true; |
Jim Van Verth | 9b21825 | 2018-09-21 14:27:35 -0400 | [diff] [blame] | 1475 | } |
| 1476 | |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1477 | void add(TriangulationVertex* v) { |
Jim Van Verth | 9b21825 | 2018-09-21 14:27:35 -0400 | [diff] [blame] | 1478 | int index = hash(v); |
| 1479 | fGrid[index].addToTail(v); |
| 1480 | ++fNumVerts; |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | void remove(TriangulationVertex* v) { |
Jim Van Verth | 9b21825 | 2018-09-21 14:27:35 -0400 | [diff] [blame] | 1484 | int index = hash(v); |
| 1485 | fGrid[index].remove(v); |
| 1486 | --fNumVerts; |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1487 | } |
| 1488 | |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 1489 | bool checkTriangle(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2, |
Jim Van Verth | 9b21825 | 2018-09-21 14:27:35 -0400 | [diff] [blame] | 1490 | uint16_t ignoreIndex0, uint16_t ignoreIndex1) const { |
| 1491 | if (!fNumVerts) { |
| 1492 | return false; |
| 1493 | } |
| 1494 | |
| 1495 | SkRect triBounds; |
| 1496 | compute_triangle_bounds(p0, p1, p2, &triBounds); |
| 1497 | int h0 = (triBounds.fLeft - fBounds.fLeft)*fGridConversion.fX; |
| 1498 | int h1 = (triBounds.fRight - fBounds.fLeft)*fGridConversion.fX; |
| 1499 | int v0 = (triBounds.fTop - fBounds.fTop)*fGridConversion.fY; |
| 1500 | int v1 = (triBounds.fBottom - fBounds.fTop)*fGridConversion.fY; |
| 1501 | |
| 1502 | for (int v = v0; v <= v1; ++v) { |
| 1503 | for (int h = h0; h <= h1; ++h) { |
| 1504 | int i = v * fHCount + h; |
| 1505 | for (SkTInternalLList<TriangulationVertex>::Iter reflexIter = fGrid[i].begin(); |
| 1506 | reflexIter != fGrid[i].end(); ++reflexIter) { |
| 1507 | TriangulationVertex* reflexVertex = *reflexIter; |
| 1508 | if (reflexVertex->fIndex != ignoreIndex0 && |
| 1509 | reflexVertex->fIndex != ignoreIndex1 && |
| 1510 | point_in_triangle(p0, p1, p2, reflexVertex->fPosition)) { |
| 1511 | return true; |
| 1512 | } |
| 1513 | } |
| 1514 | |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | return false; |
| 1519 | } |
| 1520 | |
| 1521 | private: |
Jim Van Verth | 9b21825 | 2018-09-21 14:27:35 -0400 | [diff] [blame] | 1522 | int hash(TriangulationVertex* vert) const { |
| 1523 | int h = (vert->fPosition.fX - fBounds.fLeft)*fGridConversion.fX; |
| 1524 | int v = (vert->fPosition.fY - fBounds.fTop)*fGridConversion.fY; |
Jim Van Verth | 6a300ac | 2018-10-23 13:19:21 -0400 | [diff] [blame] | 1525 | SkASSERT(v*fHCount + h >= 0); |
Jim Van Verth | 9b21825 | 2018-09-21 14:27:35 -0400 | [diff] [blame] | 1526 | return v*fHCount + h; |
| 1527 | } |
| 1528 | |
| 1529 | SkRect fBounds; |
| 1530 | int fHCount; |
| 1531 | int fVCount; |
| 1532 | int fNumVerts; |
| 1533 | // converts distance from the origin to a grid location (when cast to int) |
| 1534 | SkVector fGridConversion; |
| 1535 | SkTDArray<SkTInternalLList<TriangulationVertex>> fGrid; |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1536 | }; |
| 1537 | |
| 1538 | // Check to see if a reflex vertex has become a convex vertex after clipping an ear |
| 1539 | static void reclassify_vertex(TriangulationVertex* p, const SkPoint* polygonVerts, |
| 1540 | int winding, ReflexHash* reflexHash, |
| 1541 | SkTInternalLList<TriangulationVertex>* convexList) { |
| 1542 | if (TriangulationVertex::VertexType::kReflex == p->fVertexType) { |
| 1543 | SkVector v0 = p->fPosition - polygonVerts[p->fPrevIndex]; |
| 1544 | SkVector v1 = polygonVerts[p->fNextIndex] - p->fPosition; |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 1545 | if (winding*v0.cross(v1) > SK_ScalarNearlyZero*SK_ScalarNearlyZero) { |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1546 | p->fVertexType = TriangulationVertex::VertexType::kConvex; |
| 1547 | reflexHash->remove(p); |
| 1548 | p->fPrev = p->fNext = nullptr; |
| 1549 | convexList->addToTail(p); |
| 1550 | } |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | bool SkTriangulateSimplePolygon(const SkPoint* polygonVerts, uint16_t* indexMap, int polygonSize, |
| 1555 | SkTDArray<uint16_t>* triangleIndices) { |
| 1556 | if (polygonSize < 3) { |
| 1557 | return false; |
| 1558 | } |
| 1559 | // need to be able to represent all the vertices in the 16-bit indices |
Jim Van Verth | b7c9551 | 2018-09-11 12:57:42 -0400 | [diff] [blame] | 1560 | if (polygonSize >= std::numeric_limits<uint16_t>::max()) { |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1561 | return false; |
| 1562 | } |
| 1563 | |
Jim Van Verth | 9b21825 | 2018-09-21 14:27:35 -0400 | [diff] [blame] | 1564 | // get bounds |
| 1565 | SkRect bounds; |
Jim Van Verth | 11dd1ab | 2018-10-15 10:16:42 -0400 | [diff] [blame] | 1566 | if (!bounds.setBoundsCheck(polygonVerts, polygonSize)) { |
| 1567 | return false; |
| 1568 | } |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1569 | // get winding direction |
| 1570 | // TODO: we do this for all the polygon routines -- might be better to have the client |
| 1571 | // compute it and pass it in |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 1572 | int winding = SkGetPolygonWinding(polygonVerts, polygonSize); |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1573 | if (0 == winding) { |
| 1574 | return false; |
| 1575 | } |
| 1576 | |
Jim Van Verth | 9b21825 | 2018-09-21 14:27:35 -0400 | [diff] [blame] | 1577 | // Set up vertices |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1578 | SkAutoSTMalloc<64, TriangulationVertex> triangulationVertices(polygonSize); |
| 1579 | int prevIndex = polygonSize - 1; |
Jim Van Verth | 9b21825 | 2018-09-21 14:27:35 -0400 | [diff] [blame] | 1580 | SkVector v0 = polygonVerts[0] - polygonVerts[prevIndex]; |
| 1581 | for (int currIndex = 0; currIndex < polygonSize; ++currIndex) { |
| 1582 | int nextIndex = (currIndex + 1) % polygonSize; |
| 1583 | |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1584 | SkDEBUGCODE(memset(&triangulationVertices[currIndex], 0, sizeof(TriangulationVertex))); |
| 1585 | triangulationVertices[currIndex].fPosition = polygonVerts[currIndex]; |
| 1586 | triangulationVertices[currIndex].fIndex = currIndex; |
| 1587 | triangulationVertices[currIndex].fPrevIndex = prevIndex; |
| 1588 | triangulationVertices[currIndex].fNextIndex = nextIndex; |
Jim Van Verth | 9b21825 | 2018-09-21 14:27:35 -0400 | [diff] [blame] | 1589 | SkVector v1 = polygonVerts[nextIndex] - polygonVerts[currIndex]; |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 1590 | if (winding*v0.cross(v1) > SK_ScalarNearlyZero*SK_ScalarNearlyZero) { |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1591 | triangulationVertices[currIndex].fVertexType = TriangulationVertex::VertexType::kConvex; |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1592 | } else { |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1593 | triangulationVertices[currIndex].fVertexType = TriangulationVertex::VertexType::kReflex; |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | prevIndex = currIndex; |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1597 | v0 = v1; |
Jim Van Verth | 9b21825 | 2018-09-21 14:27:35 -0400 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | // Classify initial vertices into a list of convex vertices and a hash of reflex vertices |
| 1601 | // TODO: possibly sort the convexList in some way to get better triangles |
| 1602 | SkTInternalLList<TriangulationVertex> convexList; |
Jim Van Verth | 6a300ac | 2018-10-23 13:19:21 -0400 | [diff] [blame] | 1603 | ReflexHash reflexHash; |
| 1604 | if (!reflexHash.init(bounds, polygonSize)) { |
| 1605 | return false; |
| 1606 | } |
Jim Van Verth | 9b21825 | 2018-09-21 14:27:35 -0400 | [diff] [blame] | 1607 | prevIndex = polygonSize - 1; |
| 1608 | for (int currIndex = 0; currIndex < polygonSize; prevIndex = currIndex, ++currIndex) { |
| 1609 | TriangulationVertex::VertexType currType = triangulationVertices[currIndex].fVertexType; |
| 1610 | if (TriangulationVertex::VertexType::kConvex == currType) { |
| 1611 | int nextIndex = (currIndex + 1) % polygonSize; |
| 1612 | TriangulationVertex::VertexType prevType = triangulationVertices[prevIndex].fVertexType; |
| 1613 | TriangulationVertex::VertexType nextType = triangulationVertices[nextIndex].fVertexType; |
| 1614 | // We prioritize clipping vertices with neighboring reflex vertices. |
| 1615 | // The intent here is that it will cull reflex vertices more quickly. |
| 1616 | if (TriangulationVertex::VertexType::kReflex == prevType || |
| 1617 | TriangulationVertex::VertexType::kReflex == nextType) { |
| 1618 | convexList.addToHead(&triangulationVertices[currIndex]); |
| 1619 | } else { |
| 1620 | convexList.addToTail(&triangulationVertices[currIndex]); |
| 1621 | } |
| 1622 | } else { |
| 1623 | // We treat near collinear vertices as reflex |
| 1624 | reflexHash.add(&triangulationVertices[currIndex]); |
| 1625 | } |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | // The general concept: We are trying to find three neighboring vertices where |
| 1629 | // no other vertex lies inside the triangle (an "ear"). If we find one, we clip |
| 1630 | // that ear off, and then repeat on the new polygon. Once we get down to three vertices |
| 1631 | // we have triangulated the entire polygon. |
| 1632 | // In the worst case this is an n^2 algorithm. We can cut down the search space somewhat by |
| 1633 | // noting that only convex vertices can be potential ears, and we only need to check whether |
| 1634 | // any reflex vertices lie inside the ear. |
| 1635 | triangleIndices->setReserve(triangleIndices->count() + 3 * (polygonSize - 2)); |
| 1636 | int vertexCount = polygonSize; |
| 1637 | while (vertexCount > 3) { |
| 1638 | bool success = false; |
| 1639 | TriangulationVertex* earVertex = nullptr; |
| 1640 | TriangulationVertex* p0 = nullptr; |
| 1641 | TriangulationVertex* p2 = nullptr; |
| 1642 | // find a convex vertex to clip |
| 1643 | for (SkTInternalLList<TriangulationVertex>::Iter convexIter = convexList.begin(); |
| 1644 | convexIter != convexList.end(); ++convexIter) { |
| 1645 | earVertex = *convexIter; |
| 1646 | SkASSERT(TriangulationVertex::VertexType::kReflex != earVertex->fVertexType); |
| 1647 | |
| 1648 | p0 = &triangulationVertices[earVertex->fPrevIndex]; |
| 1649 | p2 = &triangulationVertices[earVertex->fNextIndex]; |
| 1650 | |
| 1651 | // see if any reflex vertices are inside the ear |
| 1652 | bool failed = reflexHash.checkTriangle(p0->fPosition, earVertex->fPosition, |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 1653 | p2->fPosition, p0->fIndex, p2->fIndex); |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 1654 | if (failed) { |
| 1655 | continue; |
| 1656 | } |
| 1657 | |
| 1658 | // found one we can clip |
| 1659 | success = true; |
| 1660 | break; |
| 1661 | } |
| 1662 | // If we can't find any ears to clip, this probably isn't a simple polygon |
| 1663 | if (!success) { |
| 1664 | return false; |
| 1665 | } |
| 1666 | |
| 1667 | // add indices |
| 1668 | auto indices = triangleIndices->append(3); |
| 1669 | indices[0] = indexMap[p0->fIndex]; |
| 1670 | indices[1] = indexMap[earVertex->fIndex]; |
| 1671 | indices[2] = indexMap[p2->fIndex]; |
| 1672 | |
| 1673 | // clip the ear |
| 1674 | convexList.remove(earVertex); |
| 1675 | --vertexCount; |
| 1676 | |
| 1677 | // reclassify reflex verts |
| 1678 | p0->fNextIndex = earVertex->fNextIndex; |
| 1679 | reclassify_vertex(p0, polygonVerts, winding, &reflexHash, &convexList); |
| 1680 | |
| 1681 | p2->fPrevIndex = earVertex->fPrevIndex; |
| 1682 | reclassify_vertex(p2, polygonVerts, winding, &reflexHash, &convexList); |
| 1683 | } |
| 1684 | |
| 1685 | // output indices |
| 1686 | for (SkTInternalLList<TriangulationVertex>::Iter vertexIter = convexList.begin(); |
| 1687 | vertexIter != convexList.end(); ++vertexIter) { |
| 1688 | TriangulationVertex* vertex = *vertexIter; |
| 1689 | *triangleIndices->push() = indexMap[vertex->fIndex]; |
| 1690 | } |
| 1691 | |
| 1692 | return true; |
| 1693 | } |
Mike Reed | 64284e1 | 2018-11-30 15:55:15 -0500 | [diff] [blame] | 1694 | |
| 1695 | /////////// |
| 1696 | |
| 1697 | static double crs(SkVector a, SkVector b) { |
| 1698 | return a.fX * b.fY - a.fY * b.fX; |
| 1699 | } |
| 1700 | |
| 1701 | static int sign(SkScalar v) { |
| 1702 | return v < 0 ? -1 : (v > 0); |
| 1703 | } |
| 1704 | |
| 1705 | struct SignTracker { |
| 1706 | int fSign; |
| 1707 | int fSignChanges; |
| 1708 | |
| 1709 | void reset() { |
| 1710 | fSign = 0; |
| 1711 | fSignChanges = 0; |
| 1712 | } |
| 1713 | |
| 1714 | void init(int s) { |
| 1715 | SkASSERT(fSignChanges == 0); |
| 1716 | SkASSERT(s == 1 || s == -1 || s == 0); |
| 1717 | fSign = s; |
| 1718 | fSignChanges = 1; |
| 1719 | } |
| 1720 | |
| 1721 | void update(int s) { |
| 1722 | if (s) { |
| 1723 | if (fSign != s) { |
| 1724 | fSignChanges += 1; |
| 1725 | fSign = s; |
| 1726 | } |
| 1727 | } |
| 1728 | } |
| 1729 | }; |
| 1730 | |
| 1731 | struct ConvexTracker { |
| 1732 | SkVector fFirst, fPrev; |
| 1733 | SignTracker fDSign, fCSign; |
| 1734 | int fVecCounter; |
| 1735 | bool fIsConcave; |
| 1736 | |
| 1737 | ConvexTracker() { this->reset(); } |
| 1738 | |
| 1739 | void reset() { |
| 1740 | fPrev = {0, 0}; |
| 1741 | fDSign.reset(); |
| 1742 | fCSign.reset(); |
| 1743 | fVecCounter = 0; |
| 1744 | fIsConcave = false; |
| 1745 | } |
| 1746 | |
| 1747 | void addVec(SkPoint p1, SkPoint p0) { |
| 1748 | this->addVec(p1 - p0); |
| 1749 | } |
| 1750 | void addVec(SkVector v) { |
| 1751 | if (v.fX == 0 && v.fY == 0) { |
| 1752 | return; |
| 1753 | } |
| 1754 | |
| 1755 | fVecCounter += 1; |
| 1756 | if (fVecCounter == 1) { |
| 1757 | fFirst = fPrev = v; |
| 1758 | fDSign.update(sign(v.fX)); |
| 1759 | return; |
| 1760 | } |
| 1761 | |
| 1762 | SkScalar d = v.fX; |
| 1763 | SkScalar c = crs(fPrev, v); |
| 1764 | int sign_c; |
| 1765 | if (c) { |
| 1766 | sign_c = sign(c); |
| 1767 | } else { |
| 1768 | if (d >= 0) { |
| 1769 | sign_c = fCSign.fSign; |
| 1770 | } else { |
| 1771 | sign_c = -fCSign.fSign; |
| 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | fDSign.update(sign(d)); |
| 1776 | fCSign.update(sign_c); |
| 1777 | fPrev = v; |
| 1778 | |
| 1779 | if (fDSign.fSignChanges > 3 || fCSign.fSignChanges > 1) { |
| 1780 | fIsConcave = true; |
| 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | void finalCross() { |
| 1785 | this->addVec(fFirst); |
| 1786 | } |
| 1787 | }; |
| 1788 | |
| 1789 | bool SkIsPolyConvex_experimental(const SkPoint pts[], int count) { |
| 1790 | if (count <= 3) { |
| 1791 | return true; |
| 1792 | } |
| 1793 | |
| 1794 | ConvexTracker tracker; |
| 1795 | |
| 1796 | for (int i = 0; i < count - 1; ++i) { |
| 1797 | tracker.addVec(pts[i + 1], pts[i]); |
| 1798 | if (tracker.fIsConcave) { |
| 1799 | return false; |
| 1800 | } |
| 1801 | } |
| 1802 | tracker.addVec(pts[0], pts[count - 1]); |
| 1803 | tracker.finalCross(); |
| 1804 | return !tracker.fIsConcave; |
| 1805 | } |
| 1806 | |