From b6dcbe2cf3c86f3be16c686d96ad01dea9e508c3 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Wed, 28 Jan 2015 17:43:55 -0500
Subject: [PATCH] heading home

---
 js/fea/dmaCell.js           | 38 +++++++++++++++++++++++++++++++++----
 js/fea/dmaPart.js           | 27 ++++++++++++++++++--------
 js/menus/LatticeMenuView.js | 13 ++++++++++---
 js/models/lattice.js        | 21 ++++++++++++++++++++
 4 files changed, 84 insertions(+), 15 deletions(-)

diff --git a/js/fea/dmaCell.js b/js/fea/dmaCell.js
index 7e0db245..bc8a5fec 100644
--- a/js/fea/dmaCell.js
+++ b/js/fea/dmaCell.js
@@ -10,6 +10,7 @@
     var unitOctHeight = 2/Math.sqrt(6);
 
     var unitCellGeo1 = new THREE.OctahedronGeometry(1/Math.sqrt(2));
+    unitCellGeo1.dynamic = true;
     unitCellGeo1.applyMatrix(new THREE.Matrix4().makeRotationZ(-3*Math.PI/12));
     unitCellGeo1.applyMatrix(new THREE.Matrix4().makeRotationX(Math.asin(2/Math.sqrt(2)/Math.sqrt(3))));
 
@@ -25,13 +26,28 @@
     var cellGeometry1;
     var cellGeometry2;
 
-    setScale(30);
+    var globalCellScale = 30;
 
-    function setScale(scale){
+    setGlobalCellScale(globalCellScale);
+
+    function setGlobalCellScale(scale){
+        globalCellScale = scale;
         cellGeometry1 = unitCellGeo1.clone();
         cellGeometry1.applyMatrix(new THREE.Matrix4().makeScale(scale, scale, scale));
+        setFlags(cellGeometry1);
         cellGeometry2 = unitCellGeo2.clone();
         cellGeometry2.applyMatrix(new THREE.Matrix4().makeScale(scale, scale, scale));
+        setFlags(cellGeometry2);
+    }
+
+    function setFlags(geometry){
+        geometry.verticesNeedUpdate = true;
+        geometry.elementsNeedUpdate = true;
+        geometry.morphTargetsNeedUpdate = true;
+        geometry.uvsNeedUpdate = true;
+        geometry.normalsNeedUpdate = true;
+        geometry.colorsNeedUpdate = true;
+        geometry.tangentsNeedUpdate = true;
     }
 
     function DMACell(mode, indices, scale) {
@@ -74,11 +90,16 @@
         } else {
             mesh = THREE.SceneUtils.createMultiMaterialObject(cellGeometry2, cellMaterials);
         }
+        mesh = this._setMeshPosition(mesh, position);
+
+        mesh.myCell = this;//we need a reference to this instance from the mesh for intersection selection stuff
+        return mesh;
+    };
+
+    DMACell.prototype._setMeshPosition = function(mesh, position){
         mesh.position.x = position.x;
         mesh.position.y = position.y;
         mesh.position.z = position.z;
-
-        mesh.myCell = this;//we need a reference to this instance from the mesh for intersection selection stuff
         return mesh;
     };
 
@@ -103,6 +124,15 @@
         this.cellMesh.visible = visibility;
     };
 
+    DMACell.prototype.changeScale = function(scale){
+        if (globalCellScale != scale) setGlobalCellScale(scale);
+        var position = this._calcPosition(scale, this.indices);
+        this._setMeshPosition(this.cellMesh, position);
+        _.each(this.parts, function(part){
+                part.changeScale(scale, position);
+         });
+    };
+
     DMACell.prototype.remove = function(){
         if (this.cellMesh) window.three.sceneRemove(this.cellMesh);
     };
diff --git a/js/fea/dmaPart.js b/js/fea/dmaPart.js
index 7f25da7c..28d0ef61 100644
--- a/js/fea/dmaPart.js
+++ b/js/fea/dmaPart.js
@@ -38,14 +38,7 @@
     DMAPart.prototype._makeMeshForType = function(type){
 
         var mesh = new THREE.Mesh(partGeometry);
-        mesh.position.x = this.position.x;
-        mesh.position.y = -30/3*Math.sqrt(3)+this.position.y;
-        mesh.position.z = this.position.z;
-
-        if (this.oddZFlag){//adjust some offsets for odd z layers
-            mesh.position.y += 30 + 30/6;
-            mesh.rotateZ(Math.PI);
-        }
+        mesh = this._setMeshPosition(mesh, 30);
 
         switch(type){
             case 1:
@@ -58,6 +51,24 @@
         return mesh;
     };
 
+    DMAPart.prototype._setMeshPosition = function(mesh, scale, position){
+        position = position || this.position;
+        mesh.position.x = position.x;
+        mesh.position.y = -scale/3*Math.sqrt(3)+position.y;
+        mesh.position.z = position.z;
+
+        if (this.oddZFlag){//adjust some offsets for odd z layers
+            mesh.position.y += 7*scale/6;
+            mesh.rotateZ(Math.PI);
+        }
+        return mesh;
+    };
+
+    DMAPart.prototype.changeScale = function(scale, position){
+        this.position = position;
+        if (this.mesh) this._setMeshPosition(this.mesh, scale, position);
+    };
+
     DMAPart.prototype.show = function(){
         if (!this.mesh) this._draw();
         this.mesh.visible = true;
diff --git a/js/menus/LatticeMenuView.js b/js/menus/LatticeMenuView.js
index 446ced46..428c865d 100644
--- a/js/menus/LatticeMenuView.js
+++ b/js/menus/LatticeMenuView.js
@@ -8,8 +8,8 @@ LatticeMenuView = Backbone.View.extend({
     el: "#menuContent",
 
     events: {
-        "click #latticeMenuClearCells":                "_clearCells"
-
+        "click #latticeMenuClearCells":                 "_clearCells",
+        "change #latticeScale":                         "_changeScale"
     },
 
     currentlySelected: false,
@@ -30,6 +30,13 @@ LatticeMenuView = Backbone.View.extend({
         this.model.clearCells();
     },
 
+    _changeScale: function(e){
+        e.preventDefault();
+        var val = parseFloat($(e.target).val());
+        if (isNaN(val)) return;
+        this.model.set("scale", val);
+    },
+
     _formatData: function(){
         var formattedCellType = "Octagon";
         var formattedConnectionType = "Face-Connected";
@@ -45,7 +52,7 @@ LatticeMenuView = Backbone.View.extend({
     template: _.template('\
         Cell Type: &nbsp;&nbsp;<%= formattedCellType %><br/>\
         Cell Connection:&nbsp;&nbsp;<%= formattedConnectionType %><br/>\
-        Scale:&nbsp;&nbsp;<%= scale %><br/>\
+        Scale:&nbsp;&nbsp;<input id="latticeScale" value="<%= scale %>" placeholder="enter scale" class="form-control" type="text"><br/>\
         NumCells:&nbsp;&nbsp;<%= numCells %><br/>\
         Show Bounding Box:<br/><br/>\
         <a href="#" id="latticeMenuClearCells" class=" btn btn-block btn-lg btn-default">Clear All Cells</a><br/>\
diff --git a/js/models/lattice.js b/js/models/lattice.js
index 6a8afa8b..7cd78258 100644
--- a/js/models/lattice.js
+++ b/js/models/lattice.js
@@ -24,8 +24,13 @@ Lattice = Backbone.Model.extend({
 
         //bind events
         this.listenTo(this, "change:cellMode", this._cellModeDidChange);
+        this.listenTo(this, "change:scale", this._scaleDidChange);
     },
 
+    ////////////////////////////////////////////////////////////////////////////////////
+    ///////////////////////////////ADD/REMOVE CELLS/////////////////////////////////////
+    ////////////////////////////////////////////////////////////////////////////////////
+
     addCell: function(absPosition){
 
         var cells = this.get("cells");
@@ -167,6 +172,10 @@ Lattice = Backbone.Model.extend({
         window.three.render();
     },
 
+    ////////////////////////////////////////////////////////////////////////////////////
+    ////////////////////////////////////EVENTS//////////////////////////////////////////
+    ////////////////////////////////////////////////////////////////////////////////////
+
     _cellModeDidChange: function(){
         var mode = this.get("cellMode");
         this._iterCells(this.get("cells"), function(cell){
@@ -175,6 +184,18 @@ Lattice = Backbone.Model.extend({
         window.three.render();
     },
 
+    _scaleDidChange: function(){
+        var scale = this.get("scale");
+        this._iterCells(this.get("cells"), function(cell){
+            if (cell) cell.changeScale(scale);
+        });
+        window.three.render();
+    },
+
+    ////////////////////////////////////////////////////////////////////////////////////
+    ////////////////////////////////////UTILS///////////////////////////////////////////
+    ////////////////////////////////////////////////////////////////////////////////////
+
     _iterCells: function(cells, callback){
         _.each(cells, function(cellLayer){
             _.each(cellLayer, function(cellColumn){
-- 
GitLab