From e139a6cc41f126a5aad87076f8fe8a3a33379d97 Mon Sep 17 00:00:00 2001 From: Nicola <47360416+unsettledgames@users.noreply.github.com> Date: Wed, 5 Jan 2022 12:02:35 +0100 Subject: [PATCH] Committing right algorithm before optimizing it --- js/tools/LassoSelectionTool.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/js/tools/LassoSelectionTool.js b/js/tools/LassoSelectionTool.js index a59937c..08c72ca 100644 --- a/js/tools/LassoSelectionTool.js +++ b/js/tools/LassoSelectionTool.js @@ -14,6 +14,7 @@ class LassoSelectionTool extends SelectionTool { // Putting the vfx layer on top of everything currFile.VFXLayer.canvas.style.zIndex = MAX_Z_INDEX; // clearSelection(); + this.boundingBox = {minX: 9999999, maxX: -1, minY: 9999999, maxY: -1}; this.currentPixels = []; this.drawSelection(); this.currentPixels.push([mousePos[0] / currFile.zoom, mousePos[1] / currFile.zoom]); @@ -105,15 +106,20 @@ class LassoSelectionTool extends SelectionTool { visit(pixel, visited) { let toVisit = [pixel]; let selected = []; + let currVisited = {}; while (toVisit.length > 0) { pixel = toVisit.pop(); selected.push(pixel); - visited.push(pixel); + + visited[pixel] = true; + currVisited[pixel] = true; let col = currFile.VFXLayer.context.getImageData(pixel[0], pixel[1], 1, 1).data; if (col[3] == 255) continue; + if (this.isBorderOfBox(pixel)) + return []; let top, bottom, left, right; if (pixel[1] > 0) @@ -135,19 +141,15 @@ class LassoSelectionTool extends SelectionTool { right = [pixel[0] + 1, pixel[1]]; else right = undefined; - - if ((right != undefined && this.isBorderOfBox(right)) || (left != undefined && this.isBorderOfBox(left)) - || (top != undefined && this.isBorderOfBox(top)) || (bottom != undefined && this.isBorderOfBox(bottom))) - return []; // The include problem: https://stackoverflow.com/questions/19543514/check-whether-an-array-exists-in-an-array-of-arrays - if (right != undefined && !visited.includes(right)) + if (right != undefined && currVisited[right] == undefined) toVisit.push(right); - if (left != undefined && !visited.includes(left)) + if (left != undefined && currVisited[left] == undefined) toVisit.push(left); - if (top != undefined && !visited.includes(top)) + if (top != undefined && currVisited[top] == undefined) toVisit.push(top); - if (bottom != undefined && !visited.includes(bottom)) + if (bottom != undefined && currVisited[bottom] == undefined) toVisit.push(bottom); } @@ -156,7 +158,7 @@ class LassoSelectionTool extends SelectionTool { getSelection() { let selected = []; - let visited = []; + let visited = {}; if (this.currentPixels.length <= 1){ return; } @@ -170,12 +172,12 @@ class LassoSelectionTool extends SelectionTool { * are inside the selection */ - for (let x=this.boundingBox.minX; x