babylon.earcut.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. // All the credit goes to this project and the guy who's behind it https://github.com/mapbox/earcut
  2. // Huge respect for a such great lib.
  3. // Earcut license:
  4. // Copyright (c) 2016, Mapbox
  5. //
  6. // Permission to use, copy, modify, and/or distribute this software for any purpose
  7. // with or without fee is hereby granted, provided that the above copyright notice
  8. // and this permission notice appear in all copies.
  9. //
  10. // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  11. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. // FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  13. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  14. // OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  16. // THIS SOFTWARE.
  17. module Earcut {
  18. /**
  19. * The fastest and smallest JavaScript polygon triangulation library for your WebGL apps
  20. * @param data is a flat array of vertice coordinates like [x0, y0, x1, y1, x2, y2, ...].
  21. * @param holeIndices is an array of hole indices if any (e.g. [5, 8] for a 12- vertice input would mean one hole with vertices 5–7 and another with 8–11).
  22. * @param dim is the number of coordinates per vertice in the input array (2 by default).
  23. */
  24. export function earcut(data: number[], holeIndices: number[], dim: number): Array<number> {
  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 = new Array<number>();
  30. if (!outerNode) return triangles;
  31. var minX = 0, minY = 0, maxX, maxY, x, y, size = 0;
  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 size are later used to transform coords into integers for z-order calculation
  46. size = Math.max(maxX - minX, maxY - minY);
  47. }
  48. earcutLinked(outerNode, triangles, dim, minX, minY, size, 0);
  49. return triangles;
  50. }
  51. class Node {
  52. public prev: any = null;
  53. public next: any = null;
  54. public z: any = null;
  55. public prevZ: any = null;
  56. public nextZ: any = null;
  57. public steiner: boolean = false;
  58. public constructor(public i: number, public x: number, public y: number) {
  59. }
  60. }
  61. // create a circular doubly linked list from polygon points in the specified winding order
  62. function linkedList(data: number[], start: number, end: number, dim: number, clockwise: boolean): Node {
  63. var i, last: Node | null = null;
  64. if (clockwise === (signedArea(data, start, end, dim) > 0)) {
  65. for (i = start; i < end; i += dim) last = insertNode(i, data[i], data[i + 1], (<Node>last));
  66. } else {
  67. for (i = end - dim; i >= start; i -= dim) last = insertNode(i, data[i], data[i + 1], (<Node>last));
  68. }
  69. if (last && equals(last, last.next)) {
  70. removeNode(last);
  71. last = last.next;
  72. }
  73. return (<Node>last);
  74. }
  75. // eliminate colinear or duplicate points
  76. function filterPoints(start: Node, end?: Node) {
  77. if (!start) return start;
  78. if (!end) end = start;
  79. var p = start,
  80. again;
  81. do {
  82. again = false;
  83. if (!p.steiner && (equals(p, p.next) || area(p.prev, p, p.next) === 0)) {
  84. removeNode(p);
  85. p = end = p.prev;
  86. if (p === p.next) return undefined;
  87. again = true;
  88. } else {
  89. p = p.next;
  90. }
  91. } while (again || p !== end);
  92. return end;
  93. }
  94. // main ear slicing loop which triangulates a polygon (given as a linked list)
  95. function earcutLinked(ear: any, triangles: number[], dim: number, minX: number, minY: number, size: number, pass?: number) {
  96. if (!ear) return;
  97. // interlink polygon nodes in z-order
  98. if (!pass && size) indexCurve(ear, minX, minY, size);
  99. var stop = ear,
  100. prev,
  101. next;
  102. // iterate through ears, slicing them one by one
  103. while (ear.prev !== ear.next) {
  104. prev = ear.prev;
  105. next = ear.next;
  106. if (size ? isEarHashed(ear, minX, minY, size) : isEar(ear)) {
  107. // cut off the triangle
  108. triangles.push(prev.i / dim);
  109. triangles.push(ear.i / dim);
  110. triangles.push(next.i / dim);
  111. removeNode(ear);
  112. // skipping the next vertice leads to less sliver triangles
  113. ear = next.next;
  114. stop = next.next;
  115. continue;
  116. }
  117. ear = next;
  118. // if we looped through the whole remaining polygon and can't find any more ears
  119. if (ear === stop) {
  120. // try filtering points and slicing again
  121. if (!pass) {
  122. earcutLinked(filterPoints(ear, undefined), triangles, dim, minX, minY, size, 1);
  123. // if this didn't work, try curing all small self-intersections locally
  124. } else if (pass === 1) {
  125. ear = cureLocalIntersections(ear, triangles, dim);
  126. earcutLinked(ear, triangles, dim, minX, minY, size, 2);
  127. // as a last resort, try splitting the remaining polygon into two
  128. } else if (pass === 2) {
  129. splitEarcut(ear, triangles, dim, minX, minY, size);
  130. }
  131. break;
  132. }
  133. }
  134. }
  135. // check whether a polygon node forms a valid ear with adjacent nodes
  136. function isEar(ear: Node) {
  137. var a = ear.prev,
  138. b = ear,
  139. c = ear.next;
  140. if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
  141. // now make sure we don't have other points inside the potential ear
  142. var p = ear.next.next;
  143. while (p !== ear.prev) {
  144. if (pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  145. area(p.prev, p, p.next) >= 0) return false;
  146. p = p.next;
  147. }
  148. return true;
  149. }
  150. function isEarHashed(ear: Node, minX: number, minY: number, size: number) {
  151. var a = ear.prev,
  152. b = ear,
  153. c = ear.next;
  154. if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
  155. // triangle bbox; min & max are calculated like this for speed
  156. var minTX = a.x < b.x ? (a.x < c.x ? a.x : c.x) : (b.x < c.x ? b.x : c.x),
  157. minTY = a.y < b.y ? (a.y < c.y ? a.y : c.y) : (b.y < c.y ? b.y : c.y),
  158. maxTX = a.x > b.x ? (a.x > c.x ? a.x : c.x) : (b.x > c.x ? b.x : c.x),
  159. maxTY = a.y > b.y ? (a.y > c.y ? a.y : c.y) : (b.y > c.y ? b.y : c.y);
  160. // z-order range for the current triangle bbox;
  161. var minZ = zOrder(minTX, minTY, minX, minY, size),
  162. maxZ = zOrder(maxTX, maxTY, minX, minY, size);
  163. // first look for points inside the triangle in increasing z-order
  164. var p = ear.nextZ;
  165. while (p && p.z <= maxZ) {
  166. if (p !== ear.prev &&
  167. p !== ear.next &&
  168. pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  169. area(p.prev, p, p.next) >= 0) return false;
  170. p = p.nextZ;
  171. }
  172. // then look for points in decreasing z-order
  173. p = ear.prevZ;
  174. while (p && p.z >= minZ) {
  175. if (p !== ear.prev &&
  176. p !== ear.next &&
  177. pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  178. area(p.prev, p, p.next) >= 0) return false;
  179. p = p.prevZ;
  180. }
  181. return true;
  182. }
  183. // go through all polygon nodes and cure small local self-intersections
  184. function cureLocalIntersections(start: Node, triangles: number[], dim: number) {
  185. var p = start;
  186. do {
  187. var a = p.prev,
  188. b = p.next.next;
  189. if (!equals(a, b) && intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a)) {
  190. triangles.push(a.i / dim);
  191. triangles.push(p.i / dim);
  192. triangles.push(b.i / dim);
  193. // remove two nodes involved
  194. removeNode(p);
  195. removeNode(p.next);
  196. p = start = b;
  197. }
  198. p = p.next;
  199. } while (p !== start);
  200. return p;
  201. }
  202. // try splitting polygon into two and triangulate them independently
  203. function splitEarcut(start: Node, triangles: number[], dim: number, minX: number, minY: number, size: number) {
  204. // look for a valid diagonal that divides the polygon into two
  205. var a = start;
  206. do {
  207. var b = a.next.next;
  208. while (b !== a.prev) {
  209. if (a.i !== b.i && isValidDiagonal(a, b)) {
  210. // split the polygon in two by the diagonal
  211. var c = splitPolygon(a, b);
  212. // filter colinear points around the cuts
  213. a = (<Node>filterPoints(a, a.next));
  214. c = (<Node>filterPoints(c, c.next));
  215. // run earcut on each half
  216. earcutLinked(a, triangles, dim, minX, minY, size, undefined);
  217. earcutLinked(c, triangles, dim, minX, minY, size, undefined);
  218. return;
  219. }
  220. b = b.next;
  221. }
  222. a = a.next;
  223. } while (a !== start);
  224. }
  225. // link every hole into the outer loop, producing a single-ring polygon without holes
  226. function eliminateHoles(data: number[], holeIndices: number[], outerNode: Node, dim: number) {
  227. var queue = [],
  228. i,
  229. len,
  230. start,
  231. end,
  232. list;
  233. for (i = 0, len = holeIndices.length; i < len; i++) {
  234. start = holeIndices[i] * dim;
  235. end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
  236. list = linkedList(data, start, end, dim, false);
  237. if (list === list.next) list.steiner = true;
  238. queue.push(getLeftmost(list));
  239. }
  240. queue.sort(compareX);
  241. // process holes from left to right
  242. for (i = 0; i < queue.length; i++) {
  243. eliminateHole(queue[i], outerNode);
  244. outerNode = (<Node>filterPoints(outerNode, outerNode.next));
  245. }
  246. return outerNode;
  247. }
  248. function compareX(a: Node, b: Node) {
  249. return a.x - b.x;
  250. }
  251. // find a bridge between vertices that connects hole with an outer ring and and link it
  252. function eliminateHole(hole: Node, outerNode: Node) {
  253. outerNode = (<Node>findHoleBridge(hole, outerNode));
  254. if (outerNode) {
  255. var b = splitPolygon(outerNode, hole);
  256. filterPoints(b, b.next);
  257. }
  258. }
  259. // David Eberly's algorithm for finding a bridge between hole and outer polygon
  260. function findHoleBridge(hole: Node, outerNode: Node) {
  261. var p = outerNode,
  262. hx = hole.x,
  263. hy = hole.y,
  264. qx = -Infinity,
  265. m;
  266. // find a segment intersected by a ray from the hole's leftmost point to the left;
  267. // segment's endpoint with lesser x will be potential connection point
  268. do {
  269. if (hy <= p.y && hy >= p.next.y) {
  270. var x = p.x + (hy - p.y) * (p.next.x - p.x) / (p.next.y - p.y);
  271. if (x <= hx && x > qx) {
  272. qx = x;
  273. if (x === hx) {
  274. if (hy === p.y) return p;
  275. if (hy === p.next.y) return p.next;
  276. }
  277. m = p.x < p.next.x ? p : p.next;
  278. }
  279. }
  280. p = p.next;
  281. } while (p !== outerNode);
  282. if (!m) return null;
  283. if (hx === qx) return m.prev; // hole touches outer segment; pick lower endpoint
  284. // look for points inside the triangle of hole point, segment intersection and endpoint;
  285. // if there are no points found, we have a valid connection;
  286. // otherwise choose the point of the minimum angle with the ray as connection point
  287. var stop = m,
  288. mx = m.x,
  289. my = m.y,
  290. tanMin = Infinity,
  291. tan;
  292. p = m.next;
  293. while (p !== stop) {
  294. if (hx >= p.x &&
  295. p.x >= mx &&
  296. pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) {
  297. tan = Math.abs(hy - p.y) / (hx - p.x); // tangential
  298. if ((tan < tanMin || (tan === tanMin && p.x > m.x)) && locallyInside(p, hole)) {
  299. m = p;
  300. tanMin = tan;
  301. }
  302. }
  303. p = p.next;
  304. }
  305. return m;
  306. }
  307. // interlink polygon nodes in z-order
  308. function indexCurve(start: Node, minX: number, minY: number, size: number) {
  309. var p = start;
  310. do {
  311. if (p.z === null) p.z = zOrder(p.x, p.y, minX, minY, size);
  312. p.prevZ = p.prev;
  313. p.nextZ = p.next;
  314. p = p.next;
  315. } while (p !== start);
  316. (<any>p.prevZ.nextZ) = null;
  317. (<any>p.prevZ) = null;
  318. sortLinked(p);
  319. }
  320. // Simon Tatham's linked list merge sort algorithm
  321. // http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
  322. function sortLinked(list: Node) {
  323. var i, p, q, e, tail, numMerges, pSize, qSize, inSize = 1;
  324. do {
  325. p = list;
  326. (<any>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) {
  341. e = q;
  342. q = q.nextZ;
  343. qSize--;
  344. } else if (qSize === 0 || !q) {
  345. e = p;
  346. p = p.nextZ;
  347. pSize--;
  348. } else if (p.z <= q.z) {
  349. e = p;
  350. p = p.nextZ;
  351. pSize--;
  352. } else {
  353. e = q;
  354. q = q.nextZ;
  355. qSize--;
  356. }
  357. if (tail) tail.nextZ = e;
  358. else list = e;
  359. (<any>e.prevZ) = tail;
  360. tail = e;
  361. }
  362. p = q;
  363. }
  364. (<any>tail).nextZ = null;
  365. inSize *= 2;
  366. } while (numMerges > 1);
  367. return list;
  368. }
  369. // z-order of a point given coords and size of the data bounding box
  370. function zOrder(x: number, y: number, minX: number, minY: number, size: number) {
  371. // coords are transformed into non-negative 15-bit integer range
  372. x = 32767 * (x - minX) / size;
  373. y = 32767 * (y - minY) / size;
  374. x = (x | (x << 8)) & 0x00FF00FF;
  375. x = (x | (x << 4)) & 0x0F0F0F0F;
  376. x = (x | (x << 2)) & 0x33333333;
  377. x = (x | (x << 1)) & 0x55555555;
  378. y = (y | (y << 8)) & 0x00FF00FF;
  379. y = (y | (y << 4)) & 0x0F0F0F0F;
  380. y = (y | (y << 2)) & 0x33333333;
  381. y = (y | (y << 1)) & 0x55555555;
  382. return x | (y << 1);
  383. }
  384. // find the leftmost node of a polygon ring
  385. function getLeftmost(start: Node) {
  386. var p = start,
  387. leftmost = start;
  388. do {
  389. if (p.x < leftmost.x) leftmost = p;
  390. p = p.next;
  391. } while (p !== start);
  392. return leftmost;
  393. }
  394. // check if a point lies within a convex triangle
  395. function pointInTriangle(ax: number, ay: number, bx: number, by: number, cx: number, cy: number, px: number, py: number) {
  396. return (cx - px) * (ay - py) - (ax - px) * (cy - py) >= 0 &&
  397. (ax - px) * (by - py) - (bx - px) * (ay - py) >= 0 &&
  398. (bx - px) * (cy - py) - (cx - px) * (by - py) >= 0;
  399. }
  400. // check if a diagonal between two polygon nodes is valid (lies in polygon interior)
  401. function isValidDiagonal(a: Node, b: Node) {
  402. return a.next.i !== b.i &&
  403. a.prev.i !== b.i &&
  404. !intersectsPolygon(a, b) &&
  405. locallyInside(a, b) &&
  406. locallyInside(b, a) &&
  407. middleInside(a, b);
  408. }
  409. // signed area of a triangle
  410. function area(p: Node, q: Node, r: Node) {
  411. return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
  412. }
  413. // check if two points are equal
  414. function equals(p1: Node, p2: Node) {
  415. return p1.x === p2.x && p1.y === p2.y;
  416. }
  417. // check if two segments intersect
  418. function intersects(p1: Node, q1: Node, p2: Node, q2: Node) {
  419. if ((equals(p1, q1) && equals(p2, q2)) ||
  420. (equals(p1, q2) && equals(p2, q1))) return true;
  421. return area(p1, q1, p2) > 0 !== area(p1, q1, q2) > 0 &&
  422. area(p2, q2, p1) > 0 !== area(p2, q2, q1) > 0;
  423. }
  424. // check if a polygon diagonal intersects any polygon segments
  425. function intersectsPolygon(a: Node, b: Node) {
  426. var p = a;
  427. do {
  428. if (p.i !== a.i &&
  429. p.next.i !== a.i &&
  430. p.i !== b.i &&
  431. 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: Node, b: Node) {
  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: Node, b: Node) {
  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)) && (px < (p.next.x - p.x) * (py - p.y) / (p.next.y - p.y) + p.x))
  451. inside = !inside;
  452. p = p.next;
  453. } while (p !== a);
  454. return inside;
  455. }
  456. // link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two;
  457. // if one belongs to the outer ring and another to a hole, it merges it into a single ring
  458. function splitPolygon(a: Node, b: Node) {
  459. var a2 = new Node(a.i, a.x, a.y),
  460. b2 = new Node(b.i, b.x, b.y),
  461. an = a.next,
  462. bp = b.prev;
  463. a.next = b;
  464. b.prev = a;
  465. a2.next = an;
  466. an.prev = a2;
  467. b2.next = a2;
  468. a2.prev = b2;
  469. bp.next = b2;
  470. b2.prev = bp;
  471. return b2;
  472. }
  473. // create a node and optionally link it with previous one (in a circular doubly linked list)
  474. function insertNode(i: number, x: number, y: number, last?: Node) {
  475. var p = new Node(i, x, y);
  476. if (!last) {
  477. p.prev = p;
  478. p.next = p;
  479. } else {
  480. p.next = last.next;
  481. p.prev = last;
  482. last.next.prev = p;
  483. last.next = p;
  484. }
  485. return p;
  486. }
  487. function removeNode(p: Node) {
  488. p.next.prev = p.prev;
  489. p.prev.next = p.next;
  490. if (p.prevZ) p.prevZ.nextZ = p.nextZ;
  491. if (p.nextZ) p.nextZ.prevZ = p.prevZ;
  492. }
  493. /**
  494. * return a percentage difference between the polygon area and its triangulation area;
  495. * used to verify correctness of triangulation
  496. */
  497. export function deviation(data: number[], holeIndices: number[], dim: number, triangles: number[]) {
  498. var hasHoles = holeIndices && holeIndices.length;
  499. var outerLen = hasHoles ? holeIndices[0] * dim : data.length;
  500. var polygonArea = Math.abs(signedArea(data, 0, outerLen, dim));
  501. if (hasHoles) {
  502. for (var i = 0, len = holeIndices.length; i < len; i++) {
  503. var start = holeIndices[i] * dim;
  504. var end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
  505. polygonArea -= Math.abs(signedArea(data, start, end, dim));
  506. }
  507. }
  508. var trianglesArea = 0;
  509. for (i = 0; i < triangles.length; i += 3) {
  510. var a = triangles[i] * dim;
  511. var b = triangles[i + 1] * dim;
  512. var c = triangles[i + 2] * dim;
  513. trianglesArea += Math.abs(
  514. (data[a] - data[c]) * (data[b + 1] - data[a + 1]) -
  515. (data[a] - data[b]) * (data[c + 1] - data[a + 1]));
  516. }
  517. return polygonArea === 0 && trianglesArea === 0 ? 0 : Math.abs((trianglesArea - polygonArea) / polygonArea);
  518. };
  519. function signedArea(data: number[], start: number, end: number, dim: number) {
  520. var sum = 0;
  521. for (var i = start, j = end - dim; i < end; i += dim) {
  522. sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1]);
  523. j = i;
  524. }
  525. return sum;
  526. }
  527. /**
  528. * turn a polygon in a multi-dimensional array form (e.g. as in GeoJSON) into a form Earcut accepts
  529. */
  530. export function flatten(data: number[][][]) {
  531. var dim = data[0][0].length,
  532. result = { vertices: new Array<number>(), holes: new Array<number>(), 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. }