Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
}
}
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) {
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,
};
}
export function flatten(data) {
return earcut.flatten(data);
}
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,
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), []) };
}
_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 {
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;
});
}
export function fillPolygon(points) {
const flattengeo = earcut.flatten(points);
const triangles = earcut(flattengeo.vertices, flattengeo.holes, flattengeo.dimensions);
return {
positions: flattengeo.vertices,
indexArray: triangles
};
}