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