MCPcopy Create free account
hub / github.com/microsoft/SandDance / cutPolygonByGrid

Function cutPolygonByGrid

docs/app/js/sanddance-app.js:102311–102371  ·  view source on GitHub ↗
(positions, holeIndices = null, options)

Source from the content-addressed store, hash-verified

102309 return arr1;
102310}
102311function cutPolygonByGrid(positions, holeIndices = null, options) {
102312 if (!positions.length) return [];
102313 const { size =2 , gridResolution =10 , gridOffset =[
102314 0,
102315 0
102316 ] , edgeTypes =false } = options || {};
102317 const result = [];
102318 const queue = [
102319 {
102320 pos: positions,
102321 types: edgeTypes ? new Array(positions.length / size).fill(TYPE_BORDER) : null,
102322 holes: holeIndices || []
102323 }
102324 ];
102325 const bbox = [
102326 [],
102327 []
102328 ];
102329 let cell = [];
102330 while(queue.length){
102331 const { pos , types , holes } = queue.shift();
102332 getBoundingBox(pos, size, holes[0] || pos.length, bbox);
102333 cell = getGridCell(bbox[0], gridResolution, gridOffset, cell);
102334 const code = (0, _lineclip.bitCode)(bbox[1], cell);
102335 if (code) {
102336 let parts = bisectPolygon(pos, types, size, 0, holes[0] || pos.length, cell, code);
102337 const polygonLow = {
102338 pos: parts[0].pos,
102339 types: parts[0].types,
102340 holes: []
102341 };
102342 const polygonHigh = {
102343 pos: parts[1].pos,
102344 types: parts[1].types,
102345 holes: []
102346 };
102347 queue.push(polygonLow, polygonHigh);
102348 for(let i = 0; i < holes.length; i++){
102349 parts = bisectPolygon(pos, types, size, holes[i], holes[i + 1] || pos.length, cell, code);
102350 if (parts[0]) {
102351 polygonLow.holes.push(polygonLow.pos.length);
102352 polygonLow.pos = concatInPlace(polygonLow.pos, parts[0].pos);
102353 if (edgeTypes) polygonLow.types = concatInPlace(polygonLow.types, parts[0].types);
102354 }
102355 if (parts[1]) {
102356 polygonHigh.holes.push(polygonHigh.pos.length);
102357 polygonHigh.pos = concatInPlace(polygonHigh.pos, parts[1].pos);
102358 if (edgeTypes) polygonHigh.types = concatInPlace(polygonHigh.types, parts[1].types);
102359 }
102360 }
102361 } else {
102362 const polygon = {
102363 positions: pos
102364 };
102365 if (edgeTypes) polygon.edgeTypes = types;
102366 if (holes.length) polygon.holeIndices = holes;
102367 result.push(polygon);
102368 }

Callers

nothing calls this directly

Calls 4

getBoundingBoxFunction · 0.70
getGridCellFunction · 0.70
bisectPolygonFunction · 0.70
concatInPlaceFunction · 0.70

Tested by

no test coverage detected