earcut-2.2.1-b404d9e6.js 23 KB

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