{"version":3,"sources":["../../index.ts"],"sourcesContent":["import {\n Feature,\n FeatureCollection,\n MultiPolygon,\n Polygon,\n Position,\n} from \"geojson\";\nimport earcut from \"earcut\";\nimport { polygon } from \"@turf/helpers\";\n\n/**\n * Tesselates a polygon or multipolygon into a collection of triangle polygons\n * using [earcut](https://github.com/mapbox/earcut).\n *\n * @function\n * @param {Feature} poly the polygon to tesselate\n * @returns {FeatureCollection} collection of polygon tesselations\n * @example\n * const poly = turf.polygon([[[11, 0], [22, 4], [31, 0], [31, 11], [21, 15], [11, 11], [11, 0]]]);\n * const triangles = turf.tesselate(poly);\n *\n * //addToMap\n * const addToMap = [poly, triangles]\n */\nfunction tesselate(\n poly: Feature\n): FeatureCollection {\n if (\n !poly.geometry ||\n (poly.geometry.type !== \"Polygon\" && poly.geometry.type !== \"MultiPolygon\")\n ) {\n throw new Error(\"input must be a Polygon or MultiPolygon\");\n }\n\n const fc: FeatureCollection = {\n type: \"FeatureCollection\",\n features: [],\n };\n\n if (poly.geometry.type === \"Polygon\") {\n fc.features = processPolygon(poly.geometry.coordinates);\n } else {\n poly.geometry.coordinates.forEach(function (coordinates) {\n fc.features = fc.features.concat(processPolygon(coordinates));\n });\n }\n\n return fc;\n}\n\nfunction processPolygon(coordinates: Position[][]) {\n const data = flattenCoords(coordinates);\n const dim = 2;\n const result = earcut(data.vertices, data.holes, dim);\n\n const features: Feature[] = [];\n const vertices: Position[] = [];\n\n result.forEach(function (vert: any, i: number) {\n const index = result[i];\n vertices.push([data.vertices[index * dim], data.vertices[index * dim + 1]]);\n });\n\n for (var i = 0; i < vertices.length; i += 3) {\n const coords = vertices.slice(i, i + 3);\n coords.push(vertices[i]);\n features.push(polygon([coords]));\n }\n\n return features;\n}\n\nfunction flattenCoords(data: Position[][]) {\n const dim: number = data[0][0].length,\n result: { vertices: number[]; holes: number[]; dimensions: number } = {\n vertices: [],\n holes: [],\n dimensions: dim,\n };\n let holeIndex = 0;\n\n for (let i = 0; i < data.length; i++) {\n for (let j = 0; j < data[i].length; j++) {\n for (let d = 0; d < dim; d++) result.vertices.push(data[i][j][d]);\n }\n if (i > 0) {\n holeIndex += data[i - 1].length;\n result.holes.push(holeIndex);\n }\n }\n\n return result;\n}\n\nexport { tesselate };\nexport default tesselate;\n"],"mappings":";AAOA,OAAO,YAAY;AACnB,SAAS,eAAe;AAgBxB,SAAS,UACP,MAC4B;AAC5B,MACE,CAAC,KAAK,YACL,KAAK,SAAS,SAAS,aAAa,KAAK,SAAS,SAAS,gBAC5D;AACA,UAAM,IAAI,MAAM,yCAAyC;AAAA,EAC3D;AAEA,QAAM,KAAiC;AAAA,IACrC,MAAM;AAAA,IACN,UAAU,CAAC;AAAA,EACb;AAEA,MAAI,KAAK,SAAS,SAAS,WAAW;AACpC,OAAG,WAAW,eAAe,KAAK,SAAS,WAAW;AAAA,EACxD,OAAO;AACL,SAAK,SAAS,YAAY,QAAQ,SAAU,aAAa;AACvD,SAAG,WAAW,GAAG,SAAS,OAAO,eAAe,WAAW,CAAC;AAAA,IAC9D,CAAC;AAAA,EACH;AAEA,SAAO;AACT;AAEA,SAAS,eAAe,aAA2B;AACjD,QAAM,OAAO,cAAc,WAAW;AACtC,QAAM,MAAM;AACZ,QAAM,SAAS,OAAO,KAAK,UAAU,KAAK,OAAO,GAAG;AAEpD,QAAM,WAA+B,CAAC;AACtC,QAAM,WAAuB,CAAC;AAE9B,SAAO,QAAQ,SAAU,MAAWA,IAAW;AAC7C,UAAM,QAAQ,OAAOA,EAAC;AACtB,aAAS,KAAK,CAAC,KAAK,SAAS,QAAQ,GAAG,GAAG,KAAK,SAAS,QAAQ,MAAM,CAAC,CAAC,CAAC;AAAA,EAC5E,CAAC;AAED,WAAS,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK,GAAG;AAC3C,UAAM,SAAS,SAAS,MAAM,GAAG,IAAI,CAAC;AACtC,WAAO,KAAK,SAAS,CAAC,CAAC;AACvB,aAAS,KAAK,QAAQ,CAAC,MAAM,CAAC,CAAC;A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