[1580] | 1 | #pragma once |
---|
| 2 | |
---|
| 3 | #include <algorithm> |
---|
| 4 | #include <cassert> |
---|
| 5 | #include <cmath> |
---|
| 6 | #include <memory> |
---|
| 7 | #include <vector> |
---|
[2169] | 8 | #include <limits> |
---|
[1580] | 9 | //#include <tuple> |
---|
[2697] | 10 | #include <cstdint> |
---|
[1580] | 11 | //#include <cstddef> |
---|
| 12 | |
---|
| 13 | namespace mapbox { |
---|
| 14 | |
---|
| 15 | namespace util { |
---|
| 16 | |
---|
| 17 | template <std::size_t I, typename T> struct nth { |
---|
| 18 | inline static typename std::tuple_element<I, T>::type |
---|
| 19 | get(const T& t) { return std::get<I>(t); }; |
---|
| 20 | }; |
---|
| 21 | |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | namespace detail { |
---|
| 25 | |
---|
| 26 | template <typename N = std::uint32_t> |
---|
| 27 | class Earcut { |
---|
| 28 | public: |
---|
| 29 | std::vector<N> indices; |
---|
| 30 | std::size_t vertices = 0; |
---|
| 31 | |
---|
| 32 | template <typename Polygon> |
---|
| 33 | void operator()(const Polygon& points); |
---|
| 34 | |
---|
| 35 | private: |
---|
| 36 | struct Node { |
---|
| 37 | Node(N index, double x_, double y_) : i(index), x(x_), y(y_) {} |
---|
| 38 | Node(const Node&) = delete; |
---|
| 39 | Node& operator=(const Node&) = delete; |
---|
| 40 | Node(Node&&) = delete; |
---|
| 41 | Node& operator=(Node&&) = delete; |
---|
| 42 | |
---|
| 43 | const N i; |
---|
| 44 | const double x; |
---|
| 45 | const double y; |
---|
| 46 | |
---|
| 47 | // previous and next vertice nodes in a polygon ring |
---|
| 48 | Node* prev = nullptr; |
---|
| 49 | Node* next = nullptr; |
---|
| 50 | |
---|
| 51 | // z-order curve value |
---|
| 52 | int32_t z = 0; |
---|
| 53 | |
---|
| 54 | // previous and next nodes in z-order |
---|
| 55 | Node* prevZ = nullptr; |
---|
| 56 | Node* nextZ = nullptr; |
---|
| 57 | |
---|
| 58 | // indicates whether this is a steiner point |
---|
| 59 | bool steiner = false; |
---|
| 60 | }; |
---|
| 61 | |
---|
| 62 | template <typename Ring> Node* linkedList(const Ring& points, const bool clockwise); |
---|
| 63 | Node* filterPoints(Node* start, Node* end = nullptr); |
---|
| 64 | void earcutLinked(Node* ear, int pass = 0); |
---|
| 65 | bool isEar(Node* ear); |
---|
| 66 | bool isEarHashed(Node* ear); |
---|
| 67 | Node* cureLocalIntersections(Node* start); |
---|
| 68 | void splitEarcut(Node* start); |
---|
| 69 | template <typename Polygon> Node* eliminateHoles(const Polygon& points, Node* outerNode); |
---|
| 70 | void eliminateHole(Node* hole, Node* outerNode); |
---|
| 71 | Node* findHoleBridge(Node* hole, Node* outerNode); |
---|
| 72 | void indexCurve(Node* start); |
---|
| 73 | Node* sortLinked(Node* list); |
---|
| 74 | int32_t zOrder(const double x_, const double y_); |
---|
| 75 | Node* getLeftmost(Node* start); |
---|
| 76 | bool pointInTriangle(double ax, double ay, double bx, double by, double cx, double cy, double px, double py) const; |
---|
| 77 | bool isValidDiagonal(Node* a, Node* b); |
---|
| 78 | double area(const Node* p, const Node* q, const Node* r) const; |
---|
| 79 | bool equals(const Node* p1, const Node* p2); |
---|
| 80 | bool intersects(const Node* p1, const Node* q1, const Node* p2, const Node* q2); |
---|
| 81 | bool intersectsPolygon(const Node* a, const Node* b); |
---|
| 82 | bool locallyInside(const Node* a, const Node* b); |
---|
| 83 | bool middleInside(const Node* a, const Node* b); |
---|
| 84 | Node* splitPolygon(Node* a, Node* b); |
---|
| 85 | template <typename Point> Node* insertNode(std::size_t i, const Point& p, Node* last); |
---|
| 86 | void removeNode(Node* p); |
---|
| 87 | |
---|
| 88 | bool hashing; |
---|
| 89 | double minX, maxX; |
---|
| 90 | double minY, maxY; |
---|
| 91 | double inv_size = 0; |
---|
| 92 | |
---|
| 93 | template <typename T, typename Alloc = std::allocator<T>> |
---|
| 94 | class ObjectPool { |
---|
| 95 | public: |
---|
| 96 | ObjectPool() { } |
---|
| 97 | ObjectPool(std::size_t blockSize_) { |
---|
| 98 | reset(blockSize_); |
---|
| 99 | } |
---|
| 100 | ~ObjectPool() { |
---|
| 101 | clear(); |
---|
| 102 | } |
---|
| 103 | template <typename... Args> |
---|
| 104 | T* construct(Args&&... args) { |
---|
| 105 | if (currentIndex >= blockSize) { |
---|
| 106 | currentBlock = alloc.allocate(blockSize); |
---|
| 107 | allocations.emplace_back(currentBlock); |
---|
| 108 | currentIndex = 0; |
---|
| 109 | } |
---|
| 110 | T* object = ¤tBlock[currentIndex++]; |
---|
| 111 | alloc.construct(object, std::forward<Args>(args)...); |
---|
| 112 | return object; |
---|
| 113 | } |
---|
| 114 | void reset(std::size_t newBlockSize) { |
---|
| 115 | for (auto allocation : allocations) alloc.deallocate(allocation, blockSize); |
---|
| 116 | allocations.clear(); |
---|
| 117 | blockSize = std::max<std::size_t>(1, newBlockSize); |
---|
| 118 | currentBlock = nullptr; |
---|
| 119 | currentIndex = blockSize; |
---|
| 120 | } |
---|
| 121 | void clear() { reset(blockSize); } |
---|
| 122 | private: |
---|
| 123 | T* currentBlock = nullptr; |
---|
| 124 | std::size_t currentIndex = 1; |
---|
| 125 | std::size_t blockSize = 1; |
---|
| 126 | std::vector<T*> allocations; |
---|
| 127 | Alloc alloc; |
---|
| 128 | }; |
---|
| 129 | ObjectPool<Node> nodes; |
---|
| 130 | }; |
---|
| 131 | |
---|
| 132 | template <typename N> template <typename Polygon> |
---|
| 133 | void Earcut<N>::operator()(const Polygon& points) { |
---|
| 134 | // reset |
---|
| 135 | indices.clear(); |
---|
| 136 | vertices = 0; |
---|
| 137 | |
---|
| 138 | if (points.empty()) return; |
---|
| 139 | |
---|
| 140 | double x; |
---|
| 141 | double y; |
---|
| 142 | int threshold = 80; |
---|
| 143 | std::size_t len = 0; |
---|
| 144 | |
---|
| 145 | for (size_t i = 0; threshold >= 0 && i < points.size(); i++) { |
---|
| 146 | threshold -= static_cast<int>(points[i].size()); |
---|
| 147 | len += points[i].size(); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | //estimate size of nodes and indices |
---|
| 151 | nodes.reset(len * 3 / 2); |
---|
| 152 | indices.reserve(len + points[0].size()); |
---|
| 153 | |
---|
| 154 | Node* outerNode = linkedList(points[0], true); |
---|
| 155 | if (!outerNode) return; |
---|
| 156 | |
---|
| 157 | if (points.size() > 1) outerNode = eliminateHoles(points, outerNode); |
---|
| 158 | |
---|
| 159 | // if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox |
---|
| 160 | hashing = threshold < 0; |
---|
| 161 | if (hashing) { |
---|
| 162 | Node* p = outerNode->next; |
---|
| 163 | minX = maxX = outerNode->x; |
---|
| 164 | minY = maxY = outerNode->y; |
---|
| 165 | do { |
---|
| 166 | x = p->x; |
---|
| 167 | y = p->y; |
---|
| 168 | minX = std::min<double>(minX, x); |
---|
| 169 | minY = std::min<double>(minY, y); |
---|
| 170 | maxX = std::max<double>(maxX, x); |
---|
| 171 | maxY = std::max<double>(maxY, y); |
---|
| 172 | p = p->next; |
---|
| 173 | } while (p != outerNode); |
---|
| 174 | |
---|
| 175 | // minX, minY and size are later used to transform coords into integers for z-order calculation |
---|
| 176 | inv_size = std::max<double>(maxX - minX, maxY - minY); |
---|
| 177 | inv_size = inv_size != .0 ? (1. / inv_size) : .0; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | earcutLinked(outerNode); |
---|
| 181 | |
---|
| 182 | nodes.clear(); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | // create a circular doubly linked list from polygon points in the specified winding order |
---|
| 186 | template <typename N> template <typename Ring> |
---|
| 187 | typename Earcut<N>::Node* |
---|
| 188 | Earcut<N>::linkedList(const Ring& points, const bool clockwise) { |
---|
| 189 | using Point = typename Ring::value_type; |
---|
| 190 | double sum = 0; |
---|
| 191 | const std::size_t len = points.size(); |
---|
| 192 | std::size_t i, j; |
---|
| 193 | Node* last = nullptr; |
---|
| 194 | |
---|
| 195 | // calculate original winding order of a polygon ring |
---|
| 196 | for (i = 0, j = len > 0 ? len - 1 : 0; i < len; j = i++) { |
---|
| 197 | const auto& p1 = points[i]; |
---|
| 198 | const auto& p2 = points[j]; |
---|
| 199 | const double p20 = util::nth<0, Point>::get(p2); |
---|
| 200 | const double p10 = util::nth<0, Point>::get(p1); |
---|
| 201 | const double p11 = util::nth<1, Point>::get(p1); |
---|
| 202 | const double p21 = util::nth<1, Point>::get(p2); |
---|
| 203 | sum += (p20 - p10) * (p11 + p21); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | // link points into circular doubly-linked list in the specified winding order |
---|
| 207 | if (clockwise == (sum > 0)) { |
---|
| 208 | for (i = 0; i < len; i++) last = insertNode(vertices + i, points[i], last); |
---|
| 209 | } else { |
---|
| 210 | for (i = len; i-- > 0;) last = insertNode(vertices + i, points[i], last); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | if (last && equals(last, last->next)) { |
---|
| 214 | removeNode(last); |
---|
| 215 | last = last->next; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | vertices += len; |
---|
| 219 | |
---|
| 220 | return last; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | // eliminate colinear or duplicate points |
---|
| 224 | template <typename N> |
---|
| 225 | typename Earcut<N>::Node* |
---|
| 226 | Earcut<N>::filterPoints(Node* start, Node* end) { |
---|
| 227 | if (!end) end = start; |
---|
| 228 | |
---|
| 229 | Node* p = start; |
---|
| 230 | bool again; |
---|
| 231 | do { |
---|
| 232 | again = false; |
---|
| 233 | |
---|
| 234 | if (!p->steiner && (equals(p, p->next) || area(p->prev, p, p->next) == 0)) { |
---|
| 235 | removeNode(p); |
---|
| 236 | p = end = p->prev; |
---|
| 237 | |
---|
| 238 | if (p == p->next) break; |
---|
| 239 | again = true; |
---|
| 240 | |
---|
| 241 | } else { |
---|
| 242 | p = p->next; |
---|
| 243 | } |
---|
| 244 | } while (again || p != end); |
---|
| 245 | |
---|
| 246 | return end; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | // main ear slicing loop which triangulates a polygon (given as a linked list) |
---|
| 250 | template <typename N> |
---|
| 251 | void Earcut<N>::earcutLinked(Node* ear, int pass) { |
---|
| 252 | if (!ear) return; |
---|
| 253 | |
---|
| 254 | // interlink polygon nodes in z-order |
---|
| 255 | if (!pass && hashing) indexCurve(ear); |
---|
| 256 | |
---|
| 257 | Node* stop = ear; |
---|
| 258 | Node* prev; |
---|
| 259 | Node* next; |
---|
| 260 | |
---|
| 261 | int iterations = 0; |
---|
| 262 | |
---|
| 263 | // iterate through ears, slicing them one by one |
---|
| 264 | while (ear->prev != ear->next) { |
---|
| 265 | iterations++; |
---|
| 266 | prev = ear->prev; |
---|
| 267 | next = ear->next; |
---|
| 268 | |
---|
| 269 | if (hashing ? isEarHashed(ear) : isEar(ear)) { |
---|
| 270 | // cut off the triangle |
---|
| 271 | indices.emplace_back(prev->i); |
---|
| 272 | indices.emplace_back(ear->i); |
---|
| 273 | indices.emplace_back(next->i); |
---|
| 274 | |
---|
| 275 | removeNode(ear); |
---|
| 276 | |
---|
| 277 | // skipping the next vertice leads to less sliver triangles |
---|
| 278 | ear = next->next; |
---|
| 279 | stop = next->next; |
---|
| 280 | |
---|
| 281 | continue; |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | ear = next; |
---|
| 285 | |
---|
| 286 | // if we looped through the whole remaining polygon and can't find any more ears |
---|
| 287 | if (ear == stop) { |
---|
| 288 | // try filtering points and slicing again |
---|
| 289 | if (!pass) earcutLinked(filterPoints(ear), 1); |
---|
| 290 | |
---|
| 291 | // if this didn't work, try curing all small self-intersections locally |
---|
| 292 | else if (pass == 1) { |
---|
| 293 | ear = cureLocalIntersections(ear); |
---|
| 294 | earcutLinked(ear, 2); |
---|
| 295 | |
---|
| 296 | // as a last resort, try splitting the remaining polygon into two |
---|
| 297 | } else if (pass == 2) splitEarcut(ear); |
---|
| 298 | |
---|
| 299 | break; |
---|
| 300 | } |
---|
| 301 | } |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | // check whether a polygon node forms a valid ear with adjacent nodes |
---|
| 305 | template <typename N> |
---|
| 306 | bool Earcut<N>::isEar(Node* ear) { |
---|
| 307 | const Node* a = ear->prev; |
---|
| 308 | const Node* b = ear; |
---|
| 309 | const Node* c = ear->next; |
---|
| 310 | |
---|
| 311 | if (area(a, b, c) >= 0) return false; // reflex, can't be an ear |
---|
| 312 | |
---|
| 313 | // now make sure we don't have other points inside the potential ear |
---|
| 314 | Node* p = ear->next->next; |
---|
| 315 | |
---|
| 316 | while (p != ear->prev) { |
---|
| 317 | if (pointInTriangle(a->x, a->y, b->x, b->y, c->x, c->y, p->x, p->y) && |
---|
| 318 | area(p->prev, p, p->next) >= 0) return false; |
---|
| 319 | p = p->next; |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | return true; |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | template <typename N> |
---|
| 326 | bool Earcut<N>::isEarHashed(Node* ear) { |
---|
| 327 | const Node* a = ear->prev; |
---|
| 328 | const Node* b = ear; |
---|
| 329 | const Node* c = ear->next; |
---|
| 330 | |
---|
| 331 | if (area(a, b, c) >= 0) return false; // reflex, can't be an ear |
---|
| 332 | |
---|
| 333 | // triangle bbox; min & max are calculated like this for speed |
---|
| 334 | const double minTX = std::min<double>(a->x, std::min<double>(b->x, c->x)); |
---|
| 335 | const double minTY = std::min<double>(a->y, std::min<double>(b->y, c->y)); |
---|
| 336 | const double maxTX = std::max<double>(a->x, std::max<double>(b->x, c->x)); |
---|
| 337 | const double maxTY = std::max<double>(a->y, std::max<double>(b->y, c->y)); |
---|
| 338 | |
---|
| 339 | // z-order range for the current triangle bbox; |
---|
| 340 | const int32_t minZ = zOrder(minTX, minTY); |
---|
| 341 | const int32_t maxZ = zOrder(maxTX, maxTY); |
---|
| 342 | |
---|
| 343 | // first look for points inside the triangle in increasing z-order |
---|
| 344 | Node* p = ear->nextZ; |
---|
| 345 | |
---|
| 346 | while (p && p->z <= maxZ) { |
---|
| 347 | if (p != ear->prev && p != ear->next && |
---|
| 348 | pointInTriangle(a->x, a->y, b->x, b->y, c->x, c->y, p->x, p->y) && |
---|
| 349 | area(p->prev, p, p->next) >= 0) return false; |
---|
| 350 | p = p->nextZ; |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | // then look for points in decreasing z-order |
---|
| 354 | p = ear->prevZ; |
---|
| 355 | |
---|
| 356 | while (p && p->z >= minZ) { |
---|
| 357 | if (p != ear->prev && p != ear->next && |
---|
| 358 | pointInTriangle(a->x, a->y, b->x, b->y, c->x, c->y, p->x, p->y) && |
---|
| 359 | area(p->prev, p, p->next) >= 0) return false; |
---|
| 360 | p = p->prevZ; |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | return true; |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | // go through all polygon nodes and cure small local self-intersections |
---|
| 367 | template <typename N> |
---|
| 368 | typename Earcut<N>::Node* |
---|
| 369 | Earcut<N>::cureLocalIntersections(Node* start) { |
---|
| 370 | Node* p = start; |
---|
| 371 | do { |
---|
| 372 | Node* a = p->prev; |
---|
| 373 | Node* b = p->next->next; |
---|
| 374 | |
---|
| 375 | // a self-intersection where edge (v[i-1],v[i]) intersects (v[i+1],v[i+2]) |
---|
| 376 | if (!equals(a, b) && intersects(a, p, p->next, b) && locallyInside(a, b) && locallyInside(b, a)) { |
---|
| 377 | indices.emplace_back(a->i); |
---|
| 378 | indices.emplace_back(p->i); |
---|
| 379 | indices.emplace_back(b->i); |
---|
| 380 | |
---|
| 381 | // remove two nodes involved |
---|
| 382 | removeNode(p); |
---|
| 383 | removeNode(p->next); |
---|
| 384 | |
---|
| 385 | p = start = b; |
---|
| 386 | } |
---|
| 387 | p = p->next; |
---|
| 388 | } while (p != start); |
---|
| 389 | |
---|
| 390 | return p; |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | // try splitting polygon into two and triangulate them independently |
---|
| 394 | template <typename N> |
---|
| 395 | void Earcut<N>::splitEarcut(Node* start) { |
---|
| 396 | // look for a valid diagonal that divides the polygon into two |
---|
| 397 | Node* a = start; |
---|
| 398 | do { |
---|
| 399 | Node* b = a->next->next; |
---|
| 400 | while (b != a->prev) { |
---|
| 401 | if (a->i != b->i && isValidDiagonal(a, b)) { |
---|
| 402 | // split the polygon in two by the diagonal |
---|
| 403 | Node* c = splitPolygon(a, b); |
---|
| 404 | |
---|
| 405 | // filter colinear points around the cuts |
---|
| 406 | a = filterPoints(a, a->next); |
---|
| 407 | c = filterPoints(c, c->next); |
---|
| 408 | |
---|
| 409 | // run earcut on each half |
---|
| 410 | earcutLinked(a); |
---|
| 411 | earcutLinked(c); |
---|
| 412 | return; |
---|
| 413 | } |
---|
| 414 | b = b->next; |
---|
| 415 | } |
---|
| 416 | a = a->next; |
---|
| 417 | } while (a != start); |
---|
| 418 | } |
---|
| 419 | |
---|
| 420 | // link every hole into the outer loop, producing a single-ring polygon without holes |
---|
| 421 | template <typename N> template <typename Polygon> |
---|
| 422 | typename Earcut<N>::Node* |
---|
| 423 | Earcut<N>::eliminateHoles(const Polygon& points, Node* outerNode) { |
---|
| 424 | const size_t len = points.size(); |
---|
| 425 | |
---|
| 426 | std::vector<Node*> queue; |
---|
| 427 | for (size_t i = 1; i < len; i++) { |
---|
| 428 | Node* list = linkedList(points[i], false); |
---|
| 429 | if (list) { |
---|
| 430 | if (list == list->next) list->steiner = true; |
---|
| 431 | queue.push_back(getLeftmost(list)); |
---|
| 432 | } |
---|
| 433 | } |
---|
| 434 | std::sort(queue.begin(), queue.end(), [](const Node* a, const Node* b) { |
---|
| 435 | return a->x < b->x; |
---|
| 436 | }); |
---|
| 437 | |
---|
| 438 | // process holes from left to right |
---|
| 439 | for (size_t i = 0; i < queue.size(); i++) { |
---|
| 440 | eliminateHole(queue[i], outerNode); |
---|
| 441 | outerNode = filterPoints(outerNode, outerNode->next); |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | return outerNode; |
---|
| 445 | } |
---|
| 446 | |
---|
| 447 | // find a bridge between vertices that connects hole with an outer ring and and link it |
---|
| 448 | template <typename N> |
---|
| 449 | void Earcut<N>::eliminateHole(Node* hole, Node* outerNode) { |
---|
| 450 | outerNode = findHoleBridge(hole, outerNode); |
---|
| 451 | if (outerNode) { |
---|
| 452 | Node* b = splitPolygon(outerNode, hole); |
---|
| 453 | filterPoints(b, b->next); |
---|
| 454 | } |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | // David Eberly's algorithm for finding a bridge between hole and outer polygon |
---|
| 458 | template <typename N> |
---|
| 459 | typename Earcut<N>::Node* |
---|
| 460 | Earcut<N>::findHoleBridge(Node* hole, Node* outerNode) { |
---|
| 461 | Node* p = outerNode; |
---|
| 462 | double hx = hole->x; |
---|
| 463 | double hy = hole->y; |
---|
| 464 | double qx = -std::numeric_limits<double>::infinity(); |
---|
| 465 | Node* m = nullptr; |
---|
| 466 | |
---|
| 467 | // find a segment intersected by a ray from the hole's leftmost Vertex to the left; |
---|
| 468 | // segment's endpoint with lesser x will be potential connection Vertex |
---|
| 469 | do { |
---|
| 470 | if (hy <= p->y && hy >= p->next->y && p->next->y != p->y) { |
---|
| 471 | double x = p->x + (hy - p->y) * (p->next->x - p->x) / (p->next->y - p->y); |
---|
| 472 | if (x <= hx && x > qx) { |
---|
| 473 | qx = x; |
---|
| 474 | if (x == hx) { |
---|
| 475 | if (hy == p->y) return p; |
---|
| 476 | if (hy == p->next->y) return p->next; |
---|
| 477 | } |
---|
| 478 | m = p->x < p->next->x ? p : p->next; |
---|
| 479 | } |
---|
| 480 | } |
---|
| 481 | p = p->next; |
---|
| 482 | } while (p != outerNode); |
---|
| 483 | |
---|
| 484 | if (!m) return 0; |
---|
| 485 | |
---|
| 486 | if (hx == qx) return m->prev; |
---|
| 487 | |
---|
| 488 | // look for points inside the triangle of hole Vertex, segment intersection and endpoint; |
---|
| 489 | // if there are no points found, we have a valid connection; |
---|
| 490 | // otherwise choose the Vertex of the minimum angle with the ray as connection Vertex |
---|
| 491 | |
---|
| 492 | const Node* stop = m; |
---|
| 493 | double tanMin = std::numeric_limits<double>::infinity(); |
---|
| 494 | double tanCur = 0; |
---|
| 495 | |
---|
| 496 | p = m->next; |
---|
| 497 | double mx = m->x; |
---|
| 498 | double my = m->y; |
---|
| 499 | |
---|
| 500 | while (p != stop) { |
---|
| 501 | if (hx >= p->x && p->x >= mx && hx != p->x && |
---|
| 502 | pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p->x, p->y)) { |
---|
| 503 | |
---|
| 504 | tanCur = std::abs(hy - p->y) / (hx - p->x); // tangential |
---|
| 505 | |
---|
| 506 | if ((tanCur < tanMin || (tanCur == tanMin && p->x > m->x)) && locallyInside(p, hole)) { |
---|
| 507 | m = p; |
---|
| 508 | tanMin = tanCur; |
---|
| 509 | } |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | p = p->next; |
---|
| 513 | } |
---|
| 514 | |
---|
| 515 | return m; |
---|
| 516 | } |
---|
| 517 | |
---|
| 518 | // interlink polygon nodes in z-order |
---|
| 519 | template <typename N> |
---|
| 520 | void Earcut<N>::indexCurve(Node* start) { |
---|
| 521 | assert(start); |
---|
| 522 | Node* p = start; |
---|
| 523 | |
---|
| 524 | do { |
---|
| 525 | p->z = p->z ? p->z : zOrder(p->x, p->y); |
---|
| 526 | p->prevZ = p->prev; |
---|
| 527 | p->nextZ = p->next; |
---|
| 528 | p = p->next; |
---|
| 529 | } while (p != start); |
---|
| 530 | |
---|
| 531 | p->prevZ->nextZ = nullptr; |
---|
| 532 | p->prevZ = nullptr; |
---|
| 533 | |
---|
| 534 | sortLinked(p); |
---|
| 535 | } |
---|
| 536 | |
---|
| 537 | // Simon Tatham's linked list merge sort algorithm |
---|
| 538 | // http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html |
---|
| 539 | template <typename N> |
---|
| 540 | typename Earcut<N>::Node* |
---|
| 541 | Earcut<N>::sortLinked(Node* list) { |
---|
| 542 | assert(list); |
---|
| 543 | Node* p; |
---|
| 544 | Node* q; |
---|
| 545 | Node* e; |
---|
| 546 | Node* tail; |
---|
| 547 | int i, numMerges, pSize, qSize; |
---|
| 548 | int inSize = 1; |
---|
| 549 | |
---|
| 550 | for (;;) { |
---|
| 551 | p = list; |
---|
| 552 | list = nullptr; |
---|
| 553 | tail = nullptr; |
---|
| 554 | numMerges = 0; |
---|
| 555 | |
---|
| 556 | while (p) { |
---|
| 557 | numMerges++; |
---|
| 558 | q = p; |
---|
| 559 | pSize = 0; |
---|
| 560 | for (i = 0; i < inSize; i++) { |
---|
| 561 | pSize++; |
---|
| 562 | q = q->nextZ; |
---|
| 563 | if (!q) break; |
---|
| 564 | } |
---|
| 565 | |
---|
| 566 | qSize = inSize; |
---|
| 567 | |
---|
| 568 | while (pSize > 0 || (qSize > 0 && q)) { |
---|
| 569 | |
---|
| 570 | if (pSize == 0) { |
---|
| 571 | e = q; |
---|
| 572 | q = q->nextZ; |
---|
| 573 | qSize--; |
---|
| 574 | } else if (qSize == 0 || !q) { |
---|
| 575 | e = p; |
---|
| 576 | p = p->nextZ; |
---|
| 577 | pSize--; |
---|
| 578 | } else if (p->z <= q->z) { |
---|
| 579 | e = p; |
---|
| 580 | p = p->nextZ; |
---|
| 581 | pSize--; |
---|
| 582 | } else { |
---|
| 583 | e = q; |
---|
| 584 | q = q->nextZ; |
---|
| 585 | qSize--; |
---|
| 586 | } |
---|
| 587 | |
---|
| 588 | if (tail) tail->nextZ = e; |
---|
| 589 | else list = e; |
---|
| 590 | |
---|
| 591 | e->prevZ = tail; |
---|
| 592 | tail = e; |
---|
| 593 | } |
---|
| 594 | |
---|
| 595 | p = q; |
---|
| 596 | } |
---|
| 597 | |
---|
| 598 | tail->nextZ = nullptr; |
---|
| 599 | |
---|
| 600 | if (numMerges <= 1) return list; |
---|
| 601 | |
---|
| 602 | inSize *= 2; |
---|
| 603 | } |
---|
| 604 | } |
---|
| 605 | |
---|
| 606 | // z-order of a Vertex given coords and size of the data bounding box |
---|
| 607 | template <typename N> |
---|
| 608 | int32_t Earcut<N>::zOrder(const double x_, const double y_) { |
---|
| 609 | // coords are transformed into non-negative 15-bit integer range |
---|
| 610 | int32_t x = static_cast<int32_t>(32767.0 * (x_ - minX) * inv_size); |
---|
| 611 | int32_t y = static_cast<int32_t>(32767.0 * (y_ - minY) * inv_size); |
---|
| 612 | |
---|
| 613 | x = (x | (x << 8)) & 0x00FF00FF; |
---|
| 614 | x = (x | (x << 4)) & 0x0F0F0F0F; |
---|
| 615 | x = (x | (x << 2)) & 0x33333333; |
---|
| 616 | x = (x | (x << 1)) & 0x55555555; |
---|
| 617 | |
---|
| 618 | y = (y | (y << 8)) & 0x00FF00FF; |
---|
| 619 | y = (y | (y << 4)) & 0x0F0F0F0F; |
---|
| 620 | y = (y | (y << 2)) & 0x33333333; |
---|
| 621 | y = (y | (y << 1)) & 0x55555555; |
---|
| 622 | |
---|
| 623 | return x | (y << 1); |
---|
| 624 | } |
---|
| 625 | |
---|
| 626 | // find the leftmost node of a polygon ring |
---|
| 627 | template <typename N> |
---|
| 628 | typename Earcut<N>::Node* |
---|
| 629 | Earcut<N>::getLeftmost(Node* start) { |
---|
| 630 | Node* p = start; |
---|
| 631 | Node* leftmost = start; |
---|
| 632 | do { |
---|
| 633 | if (p->x < leftmost->x) leftmost = p; |
---|
| 634 | p = p->next; |
---|
| 635 | } while (p != start); |
---|
| 636 | |
---|
| 637 | return leftmost; |
---|
| 638 | } |
---|
| 639 | |
---|
| 640 | // check if a point lies within a convex triangle |
---|
| 641 | template <typename N> |
---|
| 642 | bool Earcut<N>::pointInTriangle(double ax, double ay, double bx, double by, double cx, double cy, double px, double py) const { |
---|
| 643 | return (cx - px) * (ay - py) - (ax - px) * (cy - py) >= 0 && |
---|
| 644 | (ax - px) * (by - py) - (bx - px) * (ay - py) >= 0 && |
---|
| 645 | (bx - px) * (cy - py) - (cx - px) * (by - py) >= 0; |
---|
| 646 | } |
---|
| 647 | |
---|
| 648 | // check if a diagonal between two polygon nodes is valid (lies in polygon interior) |
---|
| 649 | template <typename N> |
---|
| 650 | bool Earcut<N>::isValidDiagonal(Node* a, Node* b) { |
---|
| 651 | return a->next->i != b->i && a->prev->i != b->i && !intersectsPolygon(a, b) && |
---|
| 652 | locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b); |
---|
| 653 | } |
---|
| 654 | |
---|
| 655 | // signed area of a triangle |
---|
| 656 | template <typename N> |
---|
| 657 | double Earcut<N>::area(const Node* p, const Node* q, const Node* r) const { |
---|
| 658 | return (q->y - p->y) * (r->x - q->x) - (q->x - p->x) * (r->y - q->y); |
---|
| 659 | } |
---|
| 660 | |
---|
| 661 | // check if two points are equal |
---|
| 662 | template <typename N> |
---|
| 663 | bool Earcut<N>::equals(const Node* p1, const Node* p2) { |
---|
| 664 | return p1->x == p2->x && p1->y == p2->y; |
---|
| 665 | } |
---|
| 666 | |
---|
| 667 | // check if two segments intersect |
---|
| 668 | template <typename N> |
---|
| 669 | bool Earcut<N>::intersects(const Node* p1, const Node* q1, const Node* p2, const Node* q2) { |
---|
| 670 | if ((equals(p1, q1) && equals(p2, q2)) || |
---|
| 671 | (equals(p1, q2) && equals(p2, q1))) return true; |
---|
| 672 | return (area(p1, q1, p2) > 0) != (area(p1, q1, q2) > 0) && |
---|
| 673 | (area(p2, q2, p1) > 0) != (area(p2, q2, q1) > 0); |
---|
| 674 | } |
---|
| 675 | |
---|
| 676 | // check if a polygon diagonal intersects any polygon segments |
---|
| 677 | template <typename N> |
---|
| 678 | bool Earcut<N>::intersectsPolygon(const Node* a, const Node* b) { |
---|
| 679 | const Node* p = a; |
---|
| 680 | do { |
---|
| 681 | if (p->i != a->i && p->next->i != a->i && p->i != b->i && p->next->i != b->i && |
---|
| 682 | intersects(p, p->next, a, b)) return true; |
---|
| 683 | p = p->next; |
---|
| 684 | } while (p != a); |
---|
| 685 | |
---|
| 686 | return false; |
---|
| 687 | } |
---|
| 688 | |
---|
| 689 | // check if a polygon diagonal is locally inside the polygon |
---|
| 690 | template <typename N> |
---|
| 691 | bool Earcut<N>::locallyInside(const Node* a, const Node* b) { |
---|
| 692 | return area(a->prev, a, a->next) < 0 ? |
---|
| 693 | area(a, b, a->next) >= 0 && area(a, a->prev, b) >= 0 : |
---|
| 694 | area(a, b, a->prev) < 0 || area(a, a->next, b) < 0; |
---|
| 695 | } |
---|
| 696 | |
---|
| 697 | // check if the middle Vertex of a polygon diagonal is inside the polygon |
---|
| 698 | template <typename N> |
---|
| 699 | bool Earcut<N>::middleInside(const Node* a, const Node* b) { |
---|
| 700 | const Node* p = a; |
---|
| 701 | bool inside = false; |
---|
| 702 | double px = (a->x + b->x) / 2; |
---|
| 703 | double py = (a->y + b->y) / 2; |
---|
| 704 | do { |
---|
| 705 | if (((p->y > py) != (p->next->y > py)) && p->next->y != p->y && |
---|
| 706 | (px < (p->next->x - p->x) * (py - p->y) / (p->next->y - p->y) + p->x)) |
---|
| 707 | inside = !inside; |
---|
| 708 | p = p->next; |
---|
| 709 | } while (p != a); |
---|
| 710 | |
---|
| 711 | return inside; |
---|
| 712 | } |
---|
| 713 | |
---|
| 714 | // link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits |
---|
| 715 | // polygon into two; if one belongs to the outer ring and another to a hole, it merges it into a |
---|
| 716 | // single ring |
---|
| 717 | template <typename N> |
---|
| 718 | typename Earcut<N>::Node* |
---|
| 719 | Earcut<N>::splitPolygon(Node* a, Node* b) { |
---|
| 720 | Node* a2 = nodes.construct(a->i, a->x, a->y); |
---|
| 721 | Node* b2 = nodes.construct(b->i, b->x, b->y); |
---|
| 722 | Node* an = a->next; |
---|
| 723 | Node* bp = b->prev; |
---|
| 724 | |
---|
| 725 | a->next = b; |
---|
| 726 | b->prev = a; |
---|
| 727 | |
---|
| 728 | a2->next = an; |
---|
| 729 | an->prev = a2; |
---|
| 730 | |
---|
| 731 | b2->next = a2; |
---|
| 732 | a2->prev = b2; |
---|
| 733 | |
---|
| 734 | bp->next = b2; |
---|
| 735 | b2->prev = bp; |
---|
| 736 | |
---|
| 737 | return b2; |
---|
| 738 | } |
---|
| 739 | |
---|
| 740 | // create a node and util::optionally link it with previous one (in a circular doubly linked list) |
---|
| 741 | template <typename N> template <typename Point> |
---|
| 742 | typename Earcut<N>::Node* |
---|
| 743 | Earcut<N>::insertNode(std::size_t i, const Point& pt, Node* last) { |
---|
| 744 | Node* p = nodes.construct(static_cast<N>(i), util::nth<0, Point>::get(pt), util::nth<1, Point>::get(pt)); |
---|
| 745 | |
---|
| 746 | if (!last) { |
---|
| 747 | p->prev = p; |
---|
| 748 | p->next = p; |
---|
| 749 | |
---|
| 750 | } else { |
---|
| 751 | assert(last); |
---|
| 752 | p->next = last->next; |
---|
| 753 | p->prev = last; |
---|
| 754 | last->next->prev = p; |
---|
| 755 | last->next = p; |
---|
| 756 | } |
---|
| 757 | return p; |
---|
| 758 | } |
---|
| 759 | |
---|
| 760 | template <typename N> |
---|
| 761 | void Earcut<N>::removeNode(Node* p) { |
---|
| 762 | p->next->prev = p->prev; |
---|
| 763 | p->prev->next = p->next; |
---|
| 764 | |
---|
| 765 | if (p->prevZ) p->prevZ->nextZ = p->nextZ; |
---|
| 766 | if (p->nextZ) p->nextZ->prevZ = p->prevZ; |
---|
| 767 | } |
---|
| 768 | } |
---|
| 769 | |
---|
| 770 | template <typename N = uint32_t, typename Polygon> |
---|
| 771 | std::vector<N> earcut(const Polygon& poly) { |
---|
| 772 | mapbox::detail::Earcut<N> earcut; |
---|
| 773 | earcut(poly); |
---|
| 774 | return std::move(earcut.indices); |
---|
| 775 | } |
---|
| 776 | } |
---|