How to use the earcut.flatten function in earcut

To help you get started, we’ve selected a few earcut examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github vasturiano / three-globe / src / layers / polygons / ConicPolygonGeometry.js View on Github external
function generateCap(top = true) {
    // !! using the 3d coords generates shapes with the wrong winding, connecting the outsides of the contour
    // so we derive the indexes from the lat,lng coordinates directly
    // let capIndices = earcut(top ? topVerts : bottomVerts, holes, 3);
    let capIndices = earcut(earcut.flatten(polygonGeoJson).vertices, holes, 2);

    !top && (capIndices = capIndices.map(v => v + numPoints)); // translate bottom indices

    return capIndices;
  }
}
github maptalks / maptalks.biglayer / src / painter / PolygonPainter.js View on Github external
if (vertice[0] && Array.isArray(vertice[0][0]) && Array.isArray(vertice[0][0][0])) {
            for (let i = 0, l = vertice.length; i < l; i++) {
                this.addPolygon(vertice[i], style);
            }
            return this;
        }
        vertice.forEach(ring => {
            if (!ring.length) {
                return;
            }
            if (!this._equalCoord(ring[0], ring[ring.length - 1])) {
                ring.push(ring[0], ring[1]);
            }
        });
        const targetZ = getTargetZoom(this.map);
        const data = earcut.flatten(vertice);

        if (this.options['project']) {
            const v = [];
            let c;
            for (let i = 0, l = data.vertices.length; i < l; i += 2) {
                c = this.map.coordinateToPoint(new maptalks.Coordinate(data.vertices[i], data.vertices[i + 1]), targetZ);
                v.push(c.x, c.y);
            }
            data.vertices = v;
        }
        let triangles = earcut(data.vertices, data.holes, 2);
        if (triangles.length <= 2) {
            return this;
        }
        const deviation = earcut.deviation(data.vertices, data.holes, 2, triangles);
        if (Math.round(deviation * 1E3) / 1E3 !== 0) {
github antvis / L7 / packages / layers / src / core / triangulation.ts View on Github external
export function polygonTriangulation(feature: IEncodeFeature) {
  const { coordinates } = feature;
  const flattengeo = earcut.flatten(coordinates as number[][][]);
  const { vertices, dimensions, holes } = flattengeo;

  return {
    indices: earcut(vertices, holes, dimensions),
    vertices,
    size: dimensions,
  };
}
github pissang / geometry-extrude / src / main.js View on Github external
export function flatten(data) {
    return earcut.flatten(data);
}
github pissang / geometry-extrude / src / main.js View on Github external
boundingRect.width, boundingRect.height
    ) / 1e5;
    for (let i = 0; i < polygons.length; i++) {
        let newPolygon = removeClosePointsOfPolygon(polygons[i], epsilon);
        if (!newPolygon) {
            continue;
        }
        const simplifyTolerance = opts.simplify / Math.max(scale[0], scale[1]);
        if (simplifyTolerance > 0) {
            newPolygon = simplifyPolygon(newPolygon, simplifyTolerance);
        }
        if (!newPolygon) {
            continue;
        }

        const {vertices, holes, dimensions} = earcut.flatten(newPolygon);

        for (let k = 0; k < vertices.length;) {
            vertices[k] = vertices[k++] * scale[0] + translate[0];
            vertices[k] = vertices[k++] * scale[1] + translate[1];
        }

        convertToClockwise(vertices, holes);

        if (dimensions !== 2) {
            throw new Error('Only 2D polygon points are supported');
        }
        const topVertices = opts.bevelSize > 0
            ? offsetPolygon(vertices, holes, opts.bevelSize, null, true) : vertices;
        const indices = triangulate(topVertices, holes, dimensions);
        preparedData.push({
            indices,
github mattdesl / canvas-sketch / examples / experimental / primitive-polyline.js View on Github external
function triangulateEarcut (contours) {
  const flat = earcut.flatten(contours);
  const indices = earcut(flat.vertices, flat.holes, flat.dimensions);
  const cells = [];
  for (let i = 0; i < indices.length / 3; i++) {
    const cell = [];
    for (let d = 0; d < 3; d++) {
      cell.push(indices[i * 3 + d]);
    }
    cells.push(cell);
  }
  return { cells, positions: contours.reduce((a, b) => a.concat(b), []) };
}
github maptalks / maptalks.biglayer / src / painter / ExtrudePainter.js View on Github external
_fillArrays(vertice, height, style) {
        const dimension = 3;

        const targetZ = getTargetZoom(this.map);
        const data = earcut.flatten(vertice);

        const bottom = [];
        const top = [];
        let c;
        //push 3d points
        for (let i = 0, l = data.vertices.length; i < l; i += 2) {
            if (i === l - 1) {
                if (this._equalCoord(data.vertices[i], data.vertices[0])) {
                    continue;
                }
            }
            if (this.options['project']) {
                c = this.map.coordinateToPoint(new maptalks.Coordinate(data.vertices[i], data.vertices[i + 1]), targetZ);
                bottom.push(c.x, c.y, 0);
                top.push(c.x, c.y, height);
            } else {
github antvis / L7 / packages / layers / src / polygon / buffers / FillBuffer.ts View on Github external
layerData.forEach((feature: IEncodeFeature) => {
      const { coordinates } = feature;
      const flattengeo = earcut.flatten(coordinates as Position[][]);
      const { vertices, dimensions, holes } = flattengeo;
      const indexArray = earcut(vertices, holes, dimensions).map(
        (v) => this.verticesCount + v,
      );
      const bufferInfo: IBufferInfo = {
        vertices,
        indexArray,
        verticesOffset: this.verticesCount + 0,
        indexOffset: this.indexCount + 0,
        dimensions,
      };
      this.indexCount += indexArray.length;
      this.verticesCount += vertices.length / dimensions;
      feature.bufferInfo = bufferInfo;
    });
  }
github antvis / L7 / src / geom / extrude.js View on Github external
export function fillPolygon(points) {
  const flattengeo = earcut.flatten(points);
  const triangles = earcut(flattengeo.vertices, flattengeo.holes, flattengeo.dimensions);
  return {
    positions: flattengeo.vertices,
    indexArray: triangles
  };
}

earcut

The fastest and smallest JavaScript polygon triangulation library for your WebGL apps

ISC
Latest version published 5 months ago

Package Health Score

86 / 100
Full package analysis

Popular earcut functions

Similar packages