{"version":3,"file":"classify_rings.mjs","names":[],"sources":["../../src/util/classify_rings.ts"],"sourcesContent":["import quickselect from 'quickselect';\nimport {Point2D} from '../point2d';\n\nexport type RingWithArea<T extends Point2D> = T[] & {area?: number};\n\n/**\n * Classifies an array of rings into polygons with outer rings and holes\n * @param rings - the rings to classify\n * @param maxRings - the maximum number of rings to include in a polygon, use 0 to include all rings\n * @returns an array of polygons with internal rings as holes\n */\nexport function classifyRings<T extends Point2D>(\n    rings: RingWithArea<T>[],\n    maxRings?: number\n): RingWithArea<T>[][] {\n    const len = rings.length;\n\n    if (len <= 1) return [rings];\n\n    const polygons: RingWithArea<T>[][] = [];\n    let polygon: RingWithArea<T>[];\n    let ccw: boolean | undefined;\n\n    for (const ring of rings) {\n        const area = calculateSignedArea(ring);\n        if (area === 0) continue;\n\n        ring.area = Math.abs(area);\n\n        if (ccw === undefined) ccw = area < 0;\n\n        if (ccw === area < 0) {\n            if (polygon) polygons.push(polygon);\n            polygon = [ring];\n        } else {\n            polygon.push(ring);\n        }\n    }\n    if (polygon) polygons.push(polygon);\n\n    // Earcut performance degrades with the # of rings in a polygon. For this\n    // reason, we limit strip out all but the `maxRings` largest rings.\n    if (maxRings > 1) {\n        for (let j = 0; j < polygons.length; j++) {\n            if (polygons[j].length <= maxRings) continue;\n            quickselect(polygons[j], maxRings, 1, polygons[j].length - 1, compareAreas);\n            polygons[j] = polygons[j].slice(0, maxRings);\n        }\n    }\n\n    return polygons;\n}\n\nfunction compareAreas<T extends Point2D>(a: RingWithArea<T>, b: RingWithArea<T>): number {\n    return b.area - a.area;\n}\n\n/**\n * Returns the signed area for the polygon ring.  Positive areas are exterior rings and\n * have a clockwise winding.  Negative areas are interior rings and have a counter clockwise\n * ordering.\n *\n * @param ring - Exterior or interior ring\n * @returns Signed area\n */\nfunction calculateSignedArea(ring: Point2D[]): number {\n    let sum = 0;\n    for (let i = 0, len = ring.length, j = len - 1, p1, p2; i < len; j = i++) {\n        p1 = ring[i];\n        p2 = ring[j];\n        sum += (p2.x - p1.x) * (p1.y + p2.y);\n    }\n    return sum;\n}\n"],"mappings":";;;;;;;;AAWA,SAAgB,cACZ,OACA,UACmB;CAGnB,IAFY,MAAM,UAEP,GAAG,OAAO,CAAC,KAAK;CAE3B,MAAM,WAAgC,CAAC;CACvC,IAAI;CACJ,IAAI;CAEJ,KAAK,MAAM,QAAQ,OAAO;EACtB,MAAM,OAAO,oBAAoB,IAAI;EACrC,IAAI,SAAS,GAAG;EAEhB,KAAK,OAAO,KAAK,IAAI,IAAI;EAEzB,IAAI,QAAQ,KAAA,GAAW,MAAM,OAAO;EAEpC,IAAI,QAAQ,OAAO,GAAG;GAClB,IAAI,SAAS,SAAS,KAAK,OAAO;GAClC,UAAU,CAAC,IAAI;EACnB,OACI,QAAQ,KAAK,IAAI;CAEzB;CACA,IAAI,SAAS,SAAS,KAAK,OAAO;CAIlC,IAAI,WAAW,GACX,KAAK,IAAI,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK;EACtC,IAAI,SAAS,EAAE,CAAC,UAAU,UAAU;EACpC,YAAY,SAAS,IAAI,UAAU,GAAG,SAAS,EAAE,CAAC,SAAS,GAAG,YAAY;EAC1E,SAAS,KAAK,SAAS,EAAE,CAAC,MAAM,GAAG,QAAQ;CAC/C;CAGJ,OAAO;AACX;AAEA,SAAS,aAAgC,GAAoB,GAA4B;CACrF,OAAO,EAAE,OAAO,EAAE;AACtB;;;;;;;;;AAUA,SAAS,oBAAoB,MAAyB;CAClD,IAAI,MAAM;CACV,KAAK,IAAI,IAAI,GAAG,MAAM,KAAK,QAAQ,IAAI,MAAM,GAAG,IAAI,IAAI,IAAI,KAAK,IAAI,KAAK;EACtE,KAAK,KAAK;EACV,KAAK,KAAK;EACV,QAAQ,GAAG,IAAI,GAAG,MAAM,GAAG,IAAI,GAAG;CACtC;CACA,OAAO;AACX"}