earcut-2.2.1.js 20 KB

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