earcut.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. var earcut_1 = earcut;
  3. var _default = earcut;
  4. function earcut(data, holeIndices, dim) {
  5. dim = dim || 2;
  6. var hasHoles = holeIndices && holeIndices.length,
  7. outerLen = hasHoles ? holeIndices[0] * dim : data.length,
  8. outerNode = linkedList(data, 0, outerLen, dim, true),
  9. triangles = [];
  10. if (!outerNode || outerNode.next === outerNode.prev) return triangles;
  11. var minX, minY, maxX, maxY, x, y, invSize;
  12. if (hasHoles) outerNode = eliminateHoles(data, holeIndices, outerNode, dim);
  13. // if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox
  14. if (data.length > 80 * dim) {
  15. minX = maxX = data[0];
  16. minY = maxY = data[1];
  17. for (var i = dim; i < outerLen; i += dim) {
  18. x = data[i];
  19. y = data[i + 1];
  20. if (x < minX) minX = x;
  21. if (y < minY) minY = y;
  22. if (x > maxX) maxX = x;
  23. if (y > maxY) maxY = y;
  24. }
  25. // minX, minY and invSize are later used to transform coords into integers for z-order calculation
  26. invSize = Math.max(maxX - minX, maxY - minY);
  27. invSize = invSize !== 0 ? 1 / invSize : 0;
  28. }
  29. earcutLinked(outerNode, triangles, dim, minX, minY, invSize);
  30. return triangles;
  31. }
  32. // create a circular doubly linked list from polygon points in the specified winding order
  33. function linkedList(data, start, end, dim, clockwise) {
  34. var i, last;
  35. if (clockwise === (signedArea(data, start, end, dim) > 0)) {
  36. for (i = start; i < end; i += dim) last = insertNode(i, data[i], data[i + 1], last);
  37. } else {
  38. for (i = end - dim; i >= start; i -= dim) last = insertNode(i, data[i], data[i + 1], last);
  39. }
  40. if (last && equals(last, last.next)) {
  41. removeNode(last);
  42. last = last.next;
  43. }
  44. return last;
  45. }
  46. // eliminate colinear or duplicate points
  47. function filterPoints(start, end) {
  48. if (!start) return start;
  49. if (!end) end = start;
  50. var p = start,
  51. again;
  52. do {
  53. again = false;
  54. if (!p.steiner && (equals(p, p.next) || area(p.prev, p, p.next) === 0)) {
  55. removeNode(p);
  56. p = end = p.prev;
  57. if (p === p.next) break;
  58. again = true;
  59. } else {
  60. p = p.next;
  61. }
  62. } while (again || p !== end);
  63. return end;
  64. }
  65. // main ear slicing loop which triangulates a polygon (given as a linked list)
  66. function earcutLinked(ear, triangles, dim, minX, minY, invSize, pass) {
  67. if (!ear) return;
  68. // interlink polygon nodes in z-order
  69. if (!pass && invSize) indexCurve(ear, minX, minY, invSize);
  70. var stop = ear,
  71. prev, next;
  72. // iterate through ears, slicing them one by one
  73. while (ear.prev !== ear.next) {
  74. prev = ear.prev;
  75. next = ear.next;
  76. if (invSize ? isEarHashed(ear, minX, minY, invSize) : isEar(ear)) {
  77. // cut off the triangle
  78. triangles.push(prev.i / dim);
  79. triangles.push(ear.i / dim);
  80. triangles.push(next.i / dim);
  81. removeNode(ear);
  82. // skipping the next vertex leads to less sliver triangles
  83. ear = next.next;
  84. stop = next.next;
  85. continue;
  86. }
  87. ear = next;
  88. // if we looped through the whole remaining polygon and can't find any more ears
  89. if (ear === stop) {
  90. // try filtering points and slicing again
  91. if (!pass) {
  92. earcutLinked(filterPoints(ear), triangles, dim, minX, minY, invSize, 1);
  93. // if this didn't work, try curing all small self-intersections locally
  94. } else if (pass === 1) {
  95. ear = cureLocalIntersections(filterPoints(ear), triangles, dim);
  96. earcutLinked(ear, triangles, dim, minX, minY, invSize, 2);
  97. // as a last resort, try splitting the remaining polygon into two
  98. } else if (pass === 2) {
  99. splitEarcut(ear, triangles, dim, minX, minY, invSize);
  100. }
  101. break;
  102. }
  103. }
  104. }
  105. // check whether a polygon node forms a valid ear with adjacent nodes
  106. function isEar(ear) {
  107. var a = ear.prev,
  108. b = ear,
  109. c = ear.next;
  110. if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
  111. // now make sure we don't have other points inside the potential ear
  112. var p = ear.next.next;
  113. while (p !== ear.prev) {
  114. if (pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  115. area(p.prev, p, p.next) >= 0) return false;
  116. p = p.next;
  117. }
  118. return true;
  119. }
  120. function isEarHashed(ear, minX, minY, invSize) {
  121. var a = ear.prev,
  122. b = ear,
  123. c = ear.next;
  124. if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
  125. // triangle bbox; min & max are calculated like this for speed
  126. var minTX = a.x < b.x ? (a.x < c.x ? a.x : c.x) : (b.x < c.x ? b.x : c.x),
  127. minTY = a.y < b.y ? (a.y < c.y ? a.y : c.y) : (b.y < c.y ? b.y : c.y),
  128. maxTX = a.x > b.x ? (a.x > c.x ? a.x : c.x) : (b.x > c.x ? b.x : c.x),
  129. maxTY = a.y > b.y ? (a.y > c.y ? a.y : c.y) : (b.y > c.y ? b.y : c.y);
  130. // z-order range for the current triangle bbox;
  131. var minZ = zOrder(minTX, minTY, minX, minY, invSize),
  132. maxZ = zOrder(maxTX, maxTY, minX, minY, invSize);
  133. var p = ear.prevZ,
  134. n = ear.nextZ;
  135. // look for points inside the triangle in both directions
  136. while (p && p.z >= minZ && n && n.z <= maxZ) {
  137. if (p !== ear.prev && p !== ear.next &&
  138. pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  139. area(p.prev, p, p.next) >= 0) return false;
  140. p = p.prevZ;
  141. if (n !== ear.prev && n !== ear.next &&
  142. pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, n.x, n.y) &&
  143. area(n.prev, n, n.next) >= 0) return false;
  144. n = n.nextZ;
  145. }
  146. // look for remaining points in decreasing z-order
  147. while (p && p.z >= minZ) {
  148. if (p !== ear.prev && p !== ear.next &&
  149. pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  150. area(p.prev, p, p.next) >= 0) return false;
  151. p = p.prevZ;
  152. }
  153. // look for remaining points in increasing z-order
  154. while (n && n.z <= maxZ) {
  155. if (n !== ear.prev && n !== ear.next &&
  156. pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, n.x, n.y) &&
  157. area(n.prev, n, n.next) >= 0) return false;
  158. n = n.nextZ;
  159. }
  160. return true;
  161. }
  162. // go through all polygon nodes and cure small local self-intersections
  163. function cureLocalIntersections(start, triangles, dim) {
  164. var p = start;
  165. do {
  166. var a = p.prev,
  167. b = p.next.next;
  168. if (!equals(a, b) && intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a)) {
  169. triangles.push(a.i / dim);
  170. triangles.push(p.i / dim);
  171. triangles.push(b.i / dim);
  172. // remove two nodes involved
  173. removeNode(p);
  174. removeNode(p.next);
  175. p = start = b;
  176. }
  177. p = p.next;
  178. } while (p !== start);
  179. return filterPoints(p);
  180. }
  181. // try splitting polygon into two and triangulate them independently
  182. function splitEarcut(start, triangles, dim, minX, minY, invSize) {
  183. // look for a valid diagonal that divides the polygon into two
  184. var a = start;
  185. do {
  186. var b = a.next.next;
  187. while (b !== a.prev) {
  188. if (a.i !== b.i && isValidDiagonal(a, b)) {
  189. // split the polygon in two by the diagonal
  190. var c = splitPolygon(a, b);
  191. // filter colinear points around the cuts
  192. a = filterPoints(a, a.next);
  193. c = filterPoints(c, c.next);
  194. // run earcut on each half
  195. earcutLinked(a, triangles, dim, minX, minY, invSize);
  196. earcutLinked(c, triangles, dim, minX, minY, invSize);
  197. return;
  198. }
  199. b = b.next;
  200. }
  201. a = a.next;
  202. } while (a !== start);
  203. }
  204. // link every hole into the outer loop, producing a single-ring polygon without holes
  205. function eliminateHoles(data, holeIndices, outerNode, dim) {
  206. var queue = [],
  207. i, len, start, end, list;
  208. for (i = 0, len = holeIndices.length; i < len; i++) {
  209. start = holeIndices[i] * dim;
  210. end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
  211. list = linkedList(data, start, end, dim, false);
  212. if (list === list.next) list.steiner = true;
  213. queue.push(getLeftmost(list));
  214. }
  215. queue.sort(compareX);
  216. // process holes from left to right
  217. for (i = 0; i < queue.length; i++) {
  218. outerNode = eliminateHole(queue[i], outerNode);
  219. outerNode = filterPoints(outerNode, outerNode.next);
  220. }
  221. return outerNode;
  222. }
  223. function compareX(a, b) {
  224. return a.x - b.x;
  225. }
  226. // find a bridge between vertices that connects hole with an outer ring and and link it
  227. function eliminateHole(hole, outerNode) {
  228. var bridge = findHoleBridge(hole, outerNode);
  229. if (!bridge) {
  230. return outerNode;
  231. }
  232. var bridgeReverse = splitPolygon(bridge, hole);
  233. // filter collinear points around the cuts
  234. var filteredBridge = filterPoints(bridge, bridge.next);
  235. filterPoints(bridgeReverse, bridgeReverse.next);
  236. // Check if input node was removed by the filtering
  237. return outerNode === bridge ? filteredBridge : outerNode;
  238. }
  239. // David Eberly's algorithm for finding a bridge between hole and outer polygon
  240. function findHoleBridge(hole, outerNode) {
  241. var p = outerNode,
  242. hx = hole.x,
  243. hy = hole.y,
  244. qx = -Infinity,
  245. m;
  246. // find a segment intersected by a ray from the hole's leftmost point to the left;
  247. // segment's endpoint with lesser x will be potential connection point
  248. do {
  249. if (hy <= p.y && hy >= p.next.y && p.next.y !== p.y) {
  250. var x = p.x + (hy - p.y) * (p.next.x - p.x) / (p.next.y - p.y);
  251. if (x <= hx && x > qx) {
  252. qx = x;
  253. if (x === hx) {
  254. if (hy === p.y) return p;
  255. if (hy === p.next.y) return p.next;
  256. }
  257. m = p.x < p.next.x ? p : p.next;
  258. }
  259. }
  260. p = p.next;
  261. } while (p !== outerNode);
  262. if (!m) return null;
  263. if (hx === qx) return m; // hole touches outer segment; pick leftmost endpoint
  264. // look for points inside the triangle of hole point, segment intersection and endpoint;
  265. // if there are no points found, we have a valid connection;
  266. // otherwise choose the point of the minimum angle with the ray as connection point
  267. var stop = m,
  268. mx = m.x,
  269. my = m.y,
  270. tanMin = Infinity,
  271. tan;
  272. p = m;
  273. do {
  274. if (hx >= p.x && p.x >= mx && hx !== p.x &&
  275. pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) {
  276. tan = Math.abs(hy - p.y) / (hx - p.x); // tangential
  277. if (locallyInside(p, hole) &&
  278. (tan < tanMin || (tan === tanMin && (p.x > m.x || (p.x === m.x && sectorContainsSector(m, p)))))) {
  279. m = p;
  280. tanMin = tan;
  281. }
  282. }
  283. p = p.next;
  284. } while (p !== stop);
  285. return m;
  286. }
  287. // whether sector in vertex m contains sector in vertex p in the same coordinates
  288. function sectorContainsSector(m, p) {
  289. return area(m.prev, m, p.prev) < 0 && area(p.next, m, m.next) < 0;
  290. }
  291. // interlink polygon nodes in z-order
  292. function indexCurve(start, minX, minY, invSize) {
  293. var p = start;
  294. do {
  295. if (p.z === null) p.z = zOrder(p.x, p.y, minX, minY, invSize);
  296. p.prevZ = p.prev;
  297. p.nextZ = p.next;
  298. p = p.next;
  299. } while (p !== start);
  300. p.prevZ.nextZ = null;
  301. p.prevZ = null;
  302. sortLinked(p);
  303. }
  304. // Simon Tatham's linked list merge sort algorithm
  305. // http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
  306. function sortLinked(list) {
  307. var i, p, q, e, tail, numMerges, pSize, qSize,
  308. inSize = 1;
  309. do {
  310. p = list;
  311. list = null;
  312. tail = null;
  313. numMerges = 0;
  314. while (p) {
  315. numMerges++;
  316. q = p;
  317. pSize = 0;
  318. for (i = 0; i < inSize; i++) {
  319. pSize++;
  320. q = q.nextZ;
  321. if (!q) break;
  322. }
  323. qSize = inSize;
  324. while (pSize > 0 || (qSize > 0 && q)) {
  325. if (pSize !== 0 && (qSize === 0 || !q || p.z <= q.z)) {
  326. e = p;
  327. p = p.nextZ;
  328. pSize--;
  329. } else {
  330. e = q;
  331. q = q.nextZ;
  332. qSize--;
  333. }
  334. if (tail) tail.nextZ = e;
  335. else list = e;
  336. e.prevZ = tail;
  337. tail = e;
  338. }
  339. p = q;
  340. }
  341. tail.nextZ = null;
  342. inSize *= 2;
  343. } while (numMerges > 1);
  344. return list;
  345. }
  346. // z-order of a point given coords and inverse of the longer side of data bbox
  347. function zOrder(x, y, minX, minY, invSize) {
  348. // coords are transformed into non-negative 15-bit integer range
  349. x = 32767 * (x - minX) * invSize;
  350. y = 32767 * (y - minY) * invSize;
  351. x = (x | (x << 8)) & 0x00FF00FF;
  352. x = (x | (x << 4)) & 0x0F0F0F0F;
  353. x = (x | (x << 2)) & 0x33333333;
  354. x = (x | (x << 1)) & 0x55555555;
  355. y = (y | (y << 8)) & 0x00FF00FF;
  356. y = (y | (y << 4)) & 0x0F0F0F0F;
  357. y = (y | (y << 2)) & 0x33333333;
  358. y = (y | (y << 1)) & 0x55555555;
  359. return x | (y << 1);
  360. }
  361. // find the leftmost node of a polygon ring
  362. function getLeftmost(start) {
  363. var p = start,
  364. leftmost = start;
  365. do {
  366. if (p.x < leftmost.x || (p.x === leftmost.x && p.y < leftmost.y)) leftmost = p;
  367. p = p.next;
  368. } while (p !== start);
  369. return leftmost;
  370. }
  371. // check if a point lies within a convex triangle
  372. function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
  373. return (cx - px) * (ay - py) - (ax - px) * (cy - py) >= 0 &&
  374. (ax - px) * (by - py) - (bx - px) * (ay - py) >= 0 &&
  375. (bx - px) * (cy - py) - (cx - px) * (by - py) >= 0;
  376. }
  377. // check if a diagonal between two polygon nodes is valid (lies in polygon interior)
  378. function isValidDiagonal(a, b) {
  379. return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) && // dones't intersect other edges
  380. (locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b) && // locally visible
  381. (area(a.prev, a, b.prev) || area(a, b.prev, b)) || // does not create opposite-facing sectors
  382. equals(a, b) && area(a.prev, a, a.next) > 0 && area(b.prev, b, b.next) > 0); // special zero-length case
  383. }
  384. // signed area of a triangle
  385. function area(p, q, r) {
  386. return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
  387. }
  388. // check if two points are equal
  389. function equals(p1, p2) {
  390. return p1.x === p2.x && p1.y === p2.y;
  391. }
  392. // check if two segments intersect
  393. function intersects(p1, q1, p2, q2) {
  394. var o1 = sign(area(p1, q1, p2));
  395. var o2 = sign(area(p1, q1, q2));
  396. var o3 = sign(area(p2, q2, p1));
  397. var o4 = sign(area(p2, q2, q1));
  398. if (o1 !== o2 && o3 !== o4) return true; // general case
  399. if (o1 === 0 && onSegment(p1, p2, q1)) return true; // p1, q1 and p2 are collinear and p2 lies on p1q1
  400. if (o2 === 0 && onSegment(p1, q2, q1)) return true; // p1, q1 and q2 are collinear and q2 lies on p1q1
  401. if (o3 === 0 && onSegment(p2, p1, q2)) return true; // p2, q2 and p1 are collinear and p1 lies on p2q2
  402. if (o4 === 0 && onSegment(p2, q1, q2)) return true; // p2, q2 and q1 are collinear and q1 lies on p2q2
  403. return false;
  404. }
  405. // for collinear points p, q, r, check if point q lies on segment pr
  406. function onSegment(p, q, r) {
  407. return q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) && q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y);
  408. }
  409. function sign(num) {
  410. return num > 0 ? 1 : num < 0 ? -1 : 0;
  411. }
  412. // check if a polygon diagonal intersects any polygon segments
  413. function intersectsPolygon(a, b) {
  414. var p = a;
  415. do {
  416. if (p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i &&
  417. intersects(p, p.next, a, b)) return true;
  418. p = p.next;
  419. } while (p !== a);
  420. return false;
  421. }
  422. // check if a polygon diagonal is locally inside the polygon
  423. function locallyInside(a, b) {
  424. return area(a.prev, a, a.next) < 0 ?
  425. area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0 :
  426. area(a, b, a.prev) < 0 || area(a, a.next, b) < 0;
  427. }
  428. // check if the middle point of a polygon diagonal is inside the polygon
  429. function middleInside(a, b) {
  430. var p = a,
  431. inside = false,
  432. px = (a.x + b.x) / 2,
  433. py = (a.y + b.y) / 2;
  434. do {
  435. if (((p.y > py) !== (p.next.y > py)) && p.next.y !== p.y &&
  436. (px < (p.next.x - p.x) * (py - p.y) / (p.next.y - p.y) + p.x))
  437. inside = !inside;
  438. p = p.next;
  439. } while (p !== a);
  440. return inside;
  441. }
  442. // link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two;
  443. // if one belongs to the outer ring and another to a hole, it merges it into a single ring
  444. function splitPolygon(a, b) {
  445. var a2 = new Node(a.i, a.x, a.y),
  446. b2 = new Node(b.i, b.x, b.y),
  447. an = a.next,
  448. bp = b.prev;
  449. a.next = b;
  450. b.prev = a;
  451. a2.next = an;
  452. an.prev = a2;
  453. b2.next = a2;
  454. a2.prev = b2;
  455. bp.next = b2;
  456. b2.prev = bp;
  457. return b2;
  458. }
  459. // create a node and optionally link it with previous one (in a circular doubly linked list)
  460. function insertNode(i, x, y, last) {
  461. var p = new Node(i, x, y);
  462. if (!last) {
  463. p.prev = p;
  464. p.next = p;
  465. } else {
  466. p.next = last.next;
  467. p.prev = last;
  468. last.next.prev = p;
  469. last.next = p;
  470. }
  471. return p;
  472. }
  473. function removeNode(p) {
  474. p.next.prev = p.prev;
  475. p.prev.next = p.next;
  476. if (p.prevZ) p.prevZ.nextZ = p.nextZ;
  477. if (p.nextZ) p.nextZ.prevZ = p.prevZ;
  478. }
  479. function Node(i, x, y) {
  480. // vertex index in coordinates array
  481. this.i = i;
  482. // vertex coordinates
  483. this.x = x;
  484. this.y = y;
  485. // previous and next vertex nodes in a polygon ring
  486. this.prev = null;
  487. this.next = null;
  488. // z-order curve value
  489. this.z = null;
  490. // previous and next nodes in z-order
  491. this.prevZ = null;
  492. this.nextZ = null;
  493. // indicates whether this is a steiner point
  494. this.steiner = false;
  495. }
  496. // return a percentage difference between the polygon area and its triangulation area;
  497. // used to verify correctness of triangulation
  498. earcut.deviation = function (data, holeIndices, dim, triangles) {
  499. var hasHoles = holeIndices && holeIndices.length;
  500. var outerLen = hasHoles ? holeIndices[0] * dim : data.length;
  501. var polygonArea = Math.abs(signedArea(data, 0, outerLen, dim));
  502. if (hasHoles) {
  503. for (var i = 0, len = holeIndices.length; i < len; i++) {
  504. var start = holeIndices[i] * dim;
  505. var end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
  506. polygonArea -= Math.abs(signedArea(data, start, end, dim));
  507. }
  508. }
  509. var trianglesArea = 0;
  510. for (i = 0; i < triangles.length; i += 3) {
  511. var a = triangles[i] * dim;
  512. var b = triangles[i + 1] * dim;
  513. var c = triangles[i + 2] * dim;
  514. trianglesArea += Math.abs(
  515. (data[a] - data[c]) * (data[b + 1] - data[a + 1]) -
  516. (data[a] - data[b]) * (data[c + 1] - data[a + 1]));
  517. }
  518. return polygonArea === 0 && trianglesArea === 0 ? 0 :
  519. Math.abs((trianglesArea - polygonArea) / polygonArea);
  520. };
  521. function signedArea(data, start, end, dim) {
  522. var sum = 0;
  523. for (var i = start, j = end - dim; i < end; i += dim) {
  524. sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1]);
  525. j = i;
  526. }
  527. return sum;
  528. }
  529. // turn a polygon in a multi-dimensional array form (e.g. as in GeoJSON) into a form Earcut accepts
  530. earcut.flatten = function (data) {
  531. var dim = data[0][0].length,
  532. result = {vertices: [], holes: [], dimensions: dim},
  533. holeIndex = 0;
  534. for (var i = 0; i < data.length; i++) {
  535. for (var j = 0; j < data[i].length; j++) {
  536. for (var d = 0; d < dim; d++) result.vertices.push(data[i][j][d]);
  537. }
  538. if (i > 0) {
  539. holeIndex += data[i - 1].length;
  540. result.holes.push(holeIndex);
  541. }
  542. }
  543. return result;
  544. };
  545. earcut_1.default = _default;
  546. export { earcut_1 as default };