From 30fef845e76e0c45264a51362158545522e08f08 Mon Sep 17 00:00:00 2001 From: Amanda Ghassaei <amandaghassaei@gmail.com> Date: Fri, 12 Jun 2015 14:30:40 -0700 Subject: [PATCH] contract cells array --- js/lattice/LatticeBase.js | 85 ++++++++++++++++++++++++++++++++++++++- js/three/ThreeModel.js | 2 +- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/js/lattice/LatticeBase.js b/js/lattice/LatticeBase.js index c7d38bb4..cbeb311b 100644 --- a/js/lattice/LatticeBase.js +++ b/js/lattice/LatticeBase.js @@ -162,7 +162,7 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre cell.destroy(); this.sparseCells[index.x][index.y][index.z] = null; - //todo shrink cells matrix if needed + this._checkForMatrixContraction(this.sparseCells); this.set("numCells", this.get("numCells")-1); three.render(); @@ -300,6 +300,89 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre return false; }, + _checkForMatrixContraction: function(cells, deletedIndex){//this could be more efficient + + var cellsMax = this.get("cellsMax"); + var cellsMin = this.get("cellsMin"); + if (!cells || !cellsMin || !cellsMax) { + console.warn("missing param for cells contraction"); + return; + } + + var newMin = this._contractCellsArray(cells, true, cellsMin.clone(), cellsMax.clone()); + var newMax = null; + if (newMin) newMax = this._contractCellsArray(cells, false, newMin.clone(), cellsMax.clone()); + + this.set("cellsMax", newMax, {silent:true}); + this.set("cellsMin", newMin); + + if (!newMin || !newMax){ + cells = [[[null]]]; + } + + }, + + _contractCellsArray: function(cells, fromFront, cellsMin, cellsMax){ + + if (cellsMax.x < cellsMin.x || cellsMax.y < cellsMin.y || cellsMax.z < cellsMin.z) return null; + + var xTrim = true; + var yTrim = true; + var zTrim = true; + this._loopCells(cells, function(cell, x, y, z){ + if (cell){ + if (fromFront){ + if (x == 0) xTrim = false; + if (y == 0) yTrim = false; + if (z == 0) zTrim = false; + } else { + if (x == cellsMax.x-cellsMin.x) xTrim = false; + if (y == cellsMax.y-cellsMin.y) yTrim = false; + if (z == cellsMax.z-cellsMin.z) zTrim = false; + } + } + }); + + if (!xTrim && !yTrim && !zTrim) { + if (fromFront) return cellsMin; + return cellsMax; + } + if (xTrim) { + if (fromFront) { + cellsMin.x += 1; + cells.shift(); + } + else { + cellsMax.x -= 1; + cells.pop(); + } + } + if (yTrim || zTrim) { + if (yTrim){ + if (fromFront) cellsMin.y += 1; + else cellsMax.y -= 1; + } + if (zTrim){ + if (fromFront) cellsMin.z += 1; + else cellsMax.z -= 1; + } + _.each(cells, function(cellLayer){ + if (yTrim) { + if (fromFront) cellLayer.shift(); + else cellLayer.pop(); + } + if (!zTrim) return; + _.each(cellLayer, function(cellColumn){ + if (zTrim) { + if (fromFront) cellColumn.shift(); + else cellColumn.pop(); + } + }); + }); + } + return this._contractCellsArray(cells, fromFront, cellsMin, cellsMax); + }, + //events diff --git a/js/three/ThreeModel.js b/js/three/ThreeModel.js index 35a1d72f..29997090 100644 --- a/js/three/ThreeModel.js +++ b/js/three/ThreeModel.js @@ -81,7 +81,7 @@ define(['underscore', 'three'], function(_, THREE){ } function removeCompositeCell(cell){ - compositeCells.splice(compositeCells.indexOf(cell)); + compositeCells.splice(compositeCells.indexOf(cell), 1); } function getCells(){ -- GitLab