From faf1109d92fcf40e826cfe1d074ebef0ab2da17a Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Tue, 27 Jan 2015 19:12:43 -0500
Subject: [PATCH] still working out kinks with exanding cell array

---
 js/fea/dmaCell.js    |  51 ++++++++---------
 js/fea/dmaPart.js    |  57 +++++++------------
 js/models/lattice.js | 129 ++++++++++++++++++++++++++++++++++++++++---
 3 files changed, 166 insertions(+), 71 deletions(-)

diff --git a/js/fea/dmaCell.js b/js/fea/dmaCell.js
index 5143e96a..a19cb7cb 100644
--- a/js/fea/dmaCell.js
+++ b/js/fea/dmaCell.js
@@ -22,24 +22,33 @@
     var cellMaterials = [new THREE.MeshNormalMaterial(),
         new THREE.MeshBasicMaterial({color:0x000000, wireframe:true})];
 
-    function Cell(mode, position) {
+
+    function DMACell(mode, position) {
 
         this.position = position;
-        this.drawForMode(mode, position);
+        this.parts = this._initParts();
+
 
-    //    this.parts = this._createParts(nodes, config);
+        this.drawForMode(mode);
     }
 
-    Cell.prototype._buildPartsMesh = function(){
-//        var parts  = [];
-//        for (var i=0;i<nodes.length;i++){
-//            parts.push(new Part(nodes[i], config[i]));
-//        }
-//        return parts;
-        return null;
+    DMACell.prototype._initParts = function(){
+        var parts  = [];
+        for (var i=0;i<3;i++){
+            parts.push(new DMAPart(i));
+        }
+        return parts;
+    };
+
+    DMACell.prototype._buildPartsMesh = function(position){
+        var parts  = [];
+        for (var i=0;i<nodes.length;i++){
+            parts.push(new Part(nodes[i], config[i]));
+        }
+        return parts;
     };
 
-    Cell.prototype._buildCellMesh = function(position){//abstract mesh representation of cell
+    DMACell.prototype._buildCellMesh = function(position){//abstract mesh representation of cell
 
         var mesh;
 
@@ -56,8 +65,8 @@
         return mesh;
     };
 
-    Cell.prototype.drawForMode = function(mode, position){
-        position = position || this.position;
+    DMACell.prototype.drawForMode = function(mode){
+        position = this.position;
         if (mode == "cell"){
             if (this.cellMesh) this._setCellMeshVisibility(true);
             else {
@@ -75,7 +84,7 @@
         }
     };
 
-    Cell.prototype._setCellMeshVisibility = function(visibility){
+    DMACell.prototype._setCellMeshVisibility = function(visibility){
         if (!this.cellMesh) return;
         this.cellMesh.visible = visibility;
 //        _.each(this.cellMesh.children, function(childMesh){
@@ -83,22 +92,14 @@
 //        });
     };
 
-    Cell.prototype.remove = function(){
+    DMACell.prototype.remove = function(){
         if (this.cellMesh) window.three.sceneRemove(this.cellMesh);
     };
 
-
-    Cell.prototype.translate = function(dx, dy, dz){
-    };
-
-    Cell.prototype.rotate = function(rx, ry, rz){
-    };
-
-    Cell.prototype._destroy = function(){
+    DMACell.prototype._destroy = function(){
         if (this.cellMesh) this.cellMesh.myCell = null;
     };
 
-
-    self.Cell =  Cell;
+    self.DMACell =  DMACell;
 
 })();
diff --git a/js/fea/dmaPart.js b/js/fea/dmaPart.js
index 5dc1fba0..b5b20ca0 100644
--- a/js/fea/dmaPart.js
+++ b/js/fea/dmaPart.js
@@ -5,51 +5,32 @@
 
 //a part, element with a single material, handled by assembler
 
+(function () {
 
 
-function DmaPart(geometry, nodes) {//list of nodes, config tells how nodes are connected
-    this.nodes = nodes;
-//    this.beams = this._createBeams(nodes, config);
-    this.scale = 10;
-    this.geometry = geometry;
-    this.render();
-}
+    var partGeometry1;
 
-DmaPart.prototype._createBeams = function(nodes, config){
-    var beams = [];
-    _.each(config, function(pair){
-        beams.push(new Beam(nodes[pair[0]], nodes[pair[2]]));
-    });
-    return beams;
-};
+    function DMAPart(type) {
+    //    this.nodes = nodes;
+    //    this.beams = this._createBeams(nodes, config);
+        this.scale = 10;
+    //    this.geometry = geometry;
+    }
 
-DmaPart.prototype.render = function(){
-    //translate geo to nodes
-    var midpoint = this.nodes[0].getMidPoint(this.nodes[1]);
-//    this.geometry.applyMatrix(new THREE.Matrix4().makeTranslation());
+    DMAPart.prototype._createBeams = function(nodes, config){
+//        var beams = [];
+//        _.each(config, function(pair){
+//            beams.push(new Beam(nodes[pair[0]], nodes[pair[2]]));
+//        });
+//        return beams;
+    };
 
+    DMAPart.prototype.render = function(){
+    };
 
-    var mesh = new THREE.Mesh(this.geometry);
-    var scale = this.scale*2.5;
-    mesh.scale.set(scale, scale, scale);
-//    mesh.rotateZ(Math.PI);
+    self.DMAPart =  DMAPart;
 
-    mesh.position.x = midpoint[0];
-    mesh.position.y = midpoint[1];
-    mesh.position.z = midpoint[2];
-    new BeamNode(0,0,0);
-
-
-    window.three.sceneAdd(mesh);
-    window.three.render();
-};
-
-
-DmaPart.prototype.translate = function(dx, dy, dz){
-};
-
-DmaPart.prototype.rotate = function(rx, ry, rz){
-};
+})();
 
 //////////////////////////////////////////////////////////////
 /////////////////SUBCLASSES///////////////////////////////////
diff --git a/js/models/lattice.js b/js/models/lattice.js
index 3335c5c7..1fc45d99 100644
--- a/js/models/lattice.js
+++ b/js/models/lattice.js
@@ -10,7 +10,9 @@ Lattice = Backbone.Model.extend({
         cellType: "octa",
         connectionType: "face",
         nodes: [],
-        cells: [],
+        cells: [[[]]],//3D matrix containing all cells and null, dynamic
+        cellsMin: {x:0, y:0, z:0},//min position of cells matrix
+        cellsMax: {x:0, y:0, z:0},//min position of cells matrix
         numCells: 0,
         partType: "triangle",
         cellMode: "cell"
@@ -24,14 +26,125 @@ Lattice = Backbone.Model.extend({
         this.listenTo(this, "change:cellMode", this._cellModeDidChange);
     },
 
-    addCell: function(position){
+    addCell: function(absPosition){
+
         var cells = this.get("cells");
-        if (this.get("cellMode")=="parts") return;//remove this eventually
-        cells.push(new Cell(this.get("cellMode"), position));
-        this.set("numCells", cells.length);
+
+        //calc indices in cell matrix
+        var scale = this.get("scale");
+        var octHeight = 3*scale/8*Math.sqrt(5);//this isn't quite right
+        var triHeight = scale/2*Math.sqrt(3);
+        var position = {};
+        position.x = Math.round(absPosition.x/scale);
+        position.y = Math.round(absPosition.y/triHeight);
+        position.z = Math.round(absPosition.z/octHeight);
+
+        //check for matrix expansion
+        var lastMax = this.get("cellsMax");
+        var lastMin = this.get("cellsMin");
+        var newMax = this._updateCellsMax(position, lastMax);
+        var newMin = this._updateCellsMin(position, lastMin);
+        if (newMax) {
+            this._expandCellsArray(cells, this._subtract(newMax, lastMin), false);
+            this.set("cellsMax", newMax);
+        }
+        if (newMin) {
+            this._expandCellsArray(cells, this._subtract(newMin, lastMin), true);
+            this.set("cellsMin", newMin);
+        }
+
+        console.log(cells);
+
+        cells[position.x][position.y][position.z] = new DMACell(this.get("cellMode"), absPosition);
+//        console.log(cells);
+//        this.set("numCells", cells.length);
         window.three.render();
     },
 
+    _expandCellsArray: function(cells, expansion, fromFront){
+
+        _.each(_.keys(expansion), function(key){
+            if (expansion[key] == 0) return;//no expansion on this axis
+
+            var cellsX = cells.length;
+            var cellsY = cellsX > 0 ? 0 : cells[0].length;
+            var cellsZ = cellsY > 0 ? 0 : cells[0][0].length;
+
+            if (key=="x"){
+                for (var x=0;x<expansion[key];x++){
+                    var newLayer = [];
+                    for (var y=0;y<cellsY;y++){
+                        var newCol = [];
+                        for (var z=0;z<cellsZ;z++){
+                            newCol.push(null);
+                        }
+                        newLayer.push(newCol);
+                    }
+                    if (fromFront) cells.unshift(newLayer);
+                    else cells.push(newLayer);
+                }
+            } else if (key=="y"){
+                for (var x=0;x<cellsX;x++){
+                    for (var y=0;y<expansion[key];y++){
+                        var newCol = [];
+                        for (var z=0;z<cellsZ;z++){
+                            newCol.push(null);
+                        }
+                        if (fromFront) cells.unshift(newCol);
+                        else cells.push(newCol);
+                    }
+                }
+            } else if (key=="z"){
+                for (var x=0;x<cellsX;x++){
+                    for (var y=0;y<cellsY;y++){
+                        for (var z=0;z<expansion[key];z++){
+                            if (fromFront) cells[x][y].unshift(null);
+                            else cells[x][y].push(null);
+                        }
+                    }
+                }
+            }
+        });
+    },
+
+    _updateCellsMin: function(newPosition, currentMin){
+        var newMin = {};
+        var hasChanged = false;
+        _.each(_.keys(newPosition), function(key){
+            if (newPosition[key]<currentMin[key]){
+                hasChanged = true;
+                newMin[key] = newPosition[key];
+            } else {
+                newMin[key] = currentMin[key];
+            }
+        });
+        if (hasChanged) return newMin;
+        return false;
+    },
+
+    _updateCellsMax: function(newPosition, currentMax){
+        var newMax = {};
+        var hasChanged = false;
+        _.each(_.keys(newPosition), function(key){
+            if (newPosition[key]>currentMax[key]){
+                hasChanged = true;
+                newMax[key] = newPosition[key];
+            } else {
+                newMax[key] = currentMax[key];
+            }
+        });
+        if (hasChanged) return newMax;
+        return false;
+    },
+
+    _subtract: function(pos1, pos2){
+        return {x:pos1.x-pos2.x, y:pos1.y-pos2.y, z:pos1.z-pos2.z};
+    },
+
+    _add: function(pos1, pos2){
+        return {x:pos1.x+pos2.x, y:pos1.y+pos2.y, z:pos1.z+pos2.z};
+    },
+
     removeCell: function(object){
         var cells = this.get("cells");
         var cell = object.parent.myCell;
@@ -48,9 +161,9 @@ Lattice = Backbone.Model.extend({
 
     clearCells: function(){
         _.each(this.get("cells"), function(cell){
-            cell.remove();
+            if (cell.remove) cell.remove();
         });
-        this.set("cells", []);
+        this.set("cells", this.defaults.cells);
         this.set("numCells", 0);
         window.three.render();
     },
@@ -58,7 +171,7 @@ Lattice = Backbone.Model.extend({
     _cellModeDidChange: function(){
         var mode = this.get("cellMode");
         _.each(this.get("cells"), function(cell){
-            cell.drawForMode(mode);
+            if (cell.drawForMode) cell.drawForMode(mode);
         });
         window.three.render();
     }
-- 
GitLab