From 913a4aa438508acc9ca7371f48eb092a2188467f Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Sat, 31 Jan 2015 19:38:02 -0500
Subject: [PATCH] cells have lattice ref

---
 js/fea/dmaCell.js    |  6 ++--
 js/models/lattice.js | 66 +++++++++++++++++++++++---------------------
 2 files changed, 39 insertions(+), 33 deletions(-)

diff --git a/js/fea/dmaCell.js b/js/fea/dmaCell.js
index d688cd0f..9e5842bc 100644
--- a/js/fea/dmaCell.js
+++ b/js/fea/dmaCell.js
@@ -38,10 +38,11 @@
         cellGeometry2.applyMatrix(new THREE.Matrix4().makeScale(scale, scale, scale));
     }
 
-    function DMACell(mode, indices, scale) {
+    function DMACell(mode, indices, scale, lattice) {
 
         this.indices = indices;
         this.scale = scale;
+        this.lattice = lattice;//need ref back to lattice
         this.position = this._calcPosition(scale, indices);
         this.cellMesh = this._buildCellMesh(this.position, indices.z);
         window.three.sceneAdd(this.cellMesh, "cell");
@@ -77,7 +78,7 @@
         _.each(this.parts, function(part){
             if (part) hasAnyParts = true;
         });
-        if (!hasAnyParts) window.lattice.removeCell(this);
+        if (!hasAnyParts) this.lattice.removeCell(this);
     };
 
     DMACell.prototype._buildCellMesh = function(position, zIndex){//abstract mesh representation of cell
@@ -170,6 +171,7 @@
         this.indices = null;
         this.scale = null;
         this.position = null;
+        this.lattice = null;
         this.parts = null;
     };
 
diff --git a/js/models/lattice.js b/js/models/lattice.js
index bbb3a233..91ac2250 100644
--- a/js/models/lattice.js
+++ b/js/models/lattice.js
@@ -60,12 +60,46 @@ Lattice = Backbone.Model.extend({
         }
 
         var index = this._subtract(position, this.get("cellsMin"));
-        if (!cells[index.x][index.y][index.z]) cells[index.x][index.y][index.z] = new DMACell(this.get("cellMode"), position, scale);
+        if (!cells[index.x][index.y][index.z]) cells[index.x][index.y][index.z] = new DMACell(this.get("cellMode"), position, scale, this);
         else console.warn("already a cell there");
         this.set("numCells", this.get("numCells")+1);
         window.three.render();
     },
 
+    removeCellFromMesh: function(object){
+
+        if (!object) return;
+        this.removeCell(object.parent.myCell);
+
+    },
+
+    removeCell: function(cell){
+        var index = this._subtract(cell.indices, this.get("cellsMin"));
+        var cells = this.get("cells");
+        cell.destroy();
+        cells[index.x][index.y][index.z] = null;
+
+        //todo shrink cells matrix if needed
+
+        this.set("numCells", this.get("numCells")-1);
+        window.three.render();
+    },
+
+    clearCells: function(){
+        this._iterCells(this.get("cells"), function(cell){
+            if (cell) cell.destroy();
+        });
+        this.set("cells", this.defaults.cells);
+        this.set("cellsMax", this.defaults.cellsMax);
+        this.set("cellsMin", this.defaults.cellsMin);
+        this.set("numCells", 0);
+        window.three.render();
+    },
+
+    ////////////////////////////////////////////////////////////////////////////////////
+    ///////////////////////////////CELLS ARRAY//////////////////////////////////////////
+    ////////////////////////////////////////////////////////////////////////////////////
+
     _expandCellsArray: function(cells, expansion, fromFront){
 
         _.each(_.keys(expansion), function(key){
@@ -150,36 +184,6 @@ Lattice = Backbone.Model.extend({
         return {x:pos1.x+pos2.x, y:pos1.y+pos2.y, z:pos1.z+pos2.z};
     },
 
-    removeCellFromMesh: function(object){
-
-        if (!object) return;
-        this.removeCell(object.parent.myCell);
-
-    },
-
-    removeCell: function(cell){
-        var index = this._subtract(cell.indices, this.get("cellsMin"));
-        var cells = this.get("cells");
-        cell.destroy();
-        cells[index.x][index.y][index.z] = null;
-
-        //todo shrink cells matrix if needed
-
-        this.set("numCells", this.get("numCells")-1);
-        window.three.render();
-    },
-
-    clearCells: function(){
-        this._iterCells(this.get("cells"), function(cell){
-            if (cell) cell.destroy();
-        });
-        this.set("cells", this.defaults.cells);
-        this.set("cellsMax", this.defaults.cellsMax);
-        this.set("cellsMin", this.defaults.cellsMin);
-        this.set("numCells", 0);
-        window.three.render();
-    },
-
     ////////////////////////////////////////////////////////////////////////////////////
     ////////////////////////////////////EVENTS//////////////////////////////////////////
     ////////////////////////////////////////////////////////////////////////////////////
-- 
GitLab