diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js index bc8c1be1c77426964d552b74c42ea1677020faf7..dce6d981ad72915d712edc73ab9cf687bc76b1bd 100644 --- a/js/cells/DMACell.js +++ b/js/cells/DMACell.js @@ -400,7 +400,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', ' }; DMACell.prototype.getCells = function(){ - return null; + return [[[this]]]; }; DMACell.prototype.getSparseCells = function(){ @@ -496,7 +496,6 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', ' //destroy DMACell.prototype.destroy = function(){//todo remove reference from lattice.cells - console.log("destroy"); this.destroyParts(); if (this.object3D) { if (this.superCell) this.superCell.removeChildren(this.object3D); diff --git a/js/cells/supercells/DMASuperCell.js b/js/cells/supercells/DMASuperCell.js index 607cfa63ad384e9e8b6897e08d6cffc097eb8e3a..5cd169325067b18f5599bfb2cc46b517f242e374 100644 --- a/js/cells/supercells/DMASuperCell.js +++ b/js/cells/supercells/DMASuperCell.js @@ -158,8 +158,6 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell', 'mat var size = this.getAbsoluteDimensions(); var bounds = this.getAbsoluteBounds(); - console.log(size); - //create array of nulls var cells = []; for (var x=0;x<size.x;x++){ diff --git a/js/lattice/LatticeBase.js b/js/lattice/LatticeBase.js index b1a2b02653e3a2016c7b917fc972322f3183aaae..895445c97917e8074674995574959446af90bdb7 100644 --- a/js/lattice/LatticeBase.js +++ b/js/lattice/LatticeBase.js @@ -139,18 +139,20 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre addCellAtIndex: function(index){ var self = this; this.makeCellWithJSON({index: index, materialID:appState.get("materialType")}, function(cell){ - var bounds = cell.getAbsoluteBounds(); - self._checkForMatrixExpansion(bounds.max, bounds.min); - var relIndex = new THREE.Vector3().subVectors(index, self.get("cellsMin")); var flattenedCells = cell.getCells(); - if (self.cells[relIndex.x][relIndex.y][relIndex.z] !== null || - (flattenedCells !== null && self._checkForCellOverlap(flattenedCells, relIndex))){ + var bounds = cell.getAbsoluteBounds(); + + if (self._checkForCellOverlap(flattenedCells, bounds.min)){ console.warn("overlap detected, addCellAtIndex operation cancelled"); cell.destroy(); return; } + var cellOutsideCurrentBounds = self._checkForIndexOutsideBounds(bounds.min) || self._checkForIndexOutsideBounds(bounds.max); + if (cellOutsideCurrentBounds) self._expandCellsMatrix(bounds.max, bounds.min); + var relIndex = self._getCellsIndexForLatticeIndex(index); + if (flattenedCells === null) flattenedCells = [[[cell]]]; self.sparseCells[relIndex.x][relIndex.y][relIndex.z] = cell; self._loopCells(flattenedCells, function(flatCell, x, y, z){ @@ -167,6 +169,12 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre // this._addCellWithJSON({index: index, materialID:appState.get("materialType")}); }, + _getCellsIndexForLatticeIndex: function(index){ + var cellsMin = this.get("cellsMin"); + if (cellsMin === null) return new THREE.Vector3(0,0,0); + return index.clone().sub(cellsMin); + }, + // _booleanAddCells: function(cellsJSON, offset){ // if (offset === undefined) offset = new THREE.Vector3(0,0,0); @@ -183,12 +191,23 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre this._loopCells(cells, function(cell, x, y, z){ if (overlapDetected) return; var index = new THREE.Vector3(x, y, z).add(offset); - if (index > self.get("cellsMax")) return; + if (self._checkForIndexOutsideBounds(index)) return; + index = self._getCellsIndexForLatticeIndex(index); if (existingCells[index.x][index.y][index.z]) overlapDetected = true; }); return overlapDetected; }, + _checkForIndexOutsideBounds: function(index){ + var cellsMin = this.get("cellsMin"); + var cellsMax = this.get("cellsMax"); + if (cellsMax === null || cellsMin === null) return true; + if (index.x < cellsMin.x || index.x > cellsMax.x) return true; + if (index.y < cellsMin.y || index.y > cellsMax.y) return true; + if (index.z < cellsMin.z || index.z > cellsMax.z) return true; + return false; + }, + _addCellWithJSON: function(json, index){ var self = this; if (!this.sparseCells[index.x][index.y][index.z]) { @@ -277,7 +296,7 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre //cells array - _checkForMatrixExpansion: function(indicesMax, indicesMin){ + _expandCellsMatrix: function(indicesMax, indicesMin){ if (!this.get("cellsMax") || !this.get("cellsMin")){ this.set("cellsMax", indicesMax);