From 2c0adcdcdd75c9889148053aa2219e802f16f1f7 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Wed, 25 Feb 2015 00:39:39 -0500
Subject: [PATCH] moving position calc into lattice

---
 js/fea/DmaCell.js            |  39 +-----------
 js/models/BasePlane.js       |   1 -
 js/models/Lattice.js         | 113 ++++++++++++++++++-----------------
 js/threeViews/Highlighter.js |   1 -
 4 files changed, 61 insertions(+), 93 deletions(-)

diff --git a/js/fea/DmaCell.js b/js/fea/DmaCell.js
index 1e4f81ad..0200257a 100644
--- a/js/fea/DmaCell.js
+++ b/js/fea/DmaCell.js
@@ -57,7 +57,7 @@ DMACell.prototype._setCellMeshVisibility = function(visibility){
 
 DMACell.prototype.updateForScale = function(scale){
     this.cellMesh.scale.set(scale, scale, scale);
-    var position = this._calcPosition(scale, this.indices);
+    var position = this.getPosition();
     this._setMeshPosition(this.cellMesh, position);
     _.each(this.parts, function(part){
         if (part) part.updateForScale(scale, position);
@@ -69,7 +69,7 @@ DMACell.prototype.getScale = function(){//need for part relay
 };
 
 DMACell.prototype.getPosition = function(){//need for part relay
-    return this._calcPosition(this.getScale(), this.indices);
+    return dmaGlobals.lattice.getPositionForIndex(this.indices);
 };
 
 DMACell.prototype.getIndex = function(){
@@ -106,7 +106,7 @@ DMACell.prototype.destroy = function(){
     var unitCellGeo1 = new THREE.OctahedronGeometry(1/Math.sqrt(2));
     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))));
-    unitCellGeo1.applyMatrix(new THREE.Matrix4().makeTranslation(0,0,unitOctHeight/2));
+//    unitCellGeo1.applyMatrix(new THREE.Matrix4().makeTranslation(0,0,unitOctHeight/2));
 
     var unitCellGeo2 = unitCellGeo1.clone();
 
@@ -121,18 +121,6 @@ DMACell.prototype.destroy = function(){
     }
     DMASideOctaCell.prototype = Object.create(DMACell.prototype);
 
-    DMASideOctaCell.prototype._calcPosition = function(scale, indices){
-        var latticeScale = this.lattice.getScale();
-        var position = {};
-        var octHeight = 2*scale/Math.sqrt(6);
-        var triHeight = latticeScale/2*Math.sqrt(3);
-        position.x = indices.x*latticeScale;
-        position.y = indices.y*triHeight-latticeScale/Math.sqrt(3);
-        position.z = indices.z*octHeight;
-        if (Math.abs(indices.y%2) == 1) position.x -= latticeScale/2;
-        return position;
-    };
-
     DMASideOctaCell.prototype._initParts = function(zIndex){
         var parts  = [];
         for (var i=0;i<3;i++){
@@ -190,18 +178,6 @@ DMACell.prototype.destroy = function(){
     }
     DMAVertexOctaCell.prototype = Object.create(DMACell.prototype);
 
-    DMAVertexOctaCell.prototype._calcPosition = function(scale, indices){
-        var position = {};
-//        var octHeight = 2*scale/Math.sqrt(6);
-//        var triHeight = scale/2*Math.sqrt(3);
-//        position.x = indices.x*scale;
-//        position.y = indices.y*triHeight;
-//        position.z = indices.z*octHeight;
-//        if (Math.abs(indices.y%2) == 1) position.x -= scale/2;
-//        if (Math.abs(indices.z%2) == 1) position.y -= triHeight*4/3;
-        return position;
-    };
-
     DMAVertexOctaCell.prototype._initParts = function(zIndex){
         var parts  = [];
         for (var i=0;i<3;i++){
@@ -229,7 +205,6 @@ DMACell.prototype.destroy = function(){
 (function () {
 
     var unitCellGeo = new THREE.BoxGeometry(1,1,1);
-    unitCellGeo.applyMatrix(new THREE.Matrix4().makeTranslation(1/2,1/2,1/2));
 
     var cellMaterials = [new THREE.MeshNormalMaterial(),
         new THREE.MeshBasicMaterial({color:0x000000, wireframe:true})];
@@ -240,14 +215,6 @@ DMACell.prototype.destroy = function(){
     }
     DMACubeCell.prototype = Object.create(DMACell.prototype);
 
-    DMACubeCell.prototype._calcPosition = function(scale, indices){
-        var position = _.clone(indices);
-        _.each(_.keys(position), function(key){
-            position[key] *= scale;
-        });
-        return position;
-    };
-
     DMACubeCell.prototype._initParts = function(zIndex){
         var parts  = [];
         for (var i=0;i<4;i++){
diff --git a/js/models/BasePlane.js b/js/models/BasePlane.js
index 0ad8668d..62ec31d0 100644
--- a/js/models/BasePlane.js
+++ b/js/models/BasePlane.js
@@ -94,7 +94,6 @@ OctaBasePlane = BasePlane.extend({
     _makeBasePlaneMesh: function(){
 
         var geometry = new THREE.Geometry();
-        geometry.dynamic = true;
         geometry.vertices = this._calcOctaFaceVertices(0.0);
         var faces = geometry.faces;
 
diff --git a/js/models/Lattice.js b/js/models/Lattice.js
index ad731317..5b6b523f 100644
--- a/js/models/Lattice.js
+++ b/js/models/Lattice.js
@@ -67,12 +67,22 @@ Lattice = Backbone.Model.extend({
     },
 
     _indexForPosition: function(absPosition){
-            var position = {};
-            position.x = Math.floor(absPosition.x/this.xScale());
-            position.y = Math.floor(absPosition.y/this.yScale());
-            position.z = Math.floor(absPosition.z/this.zScale());
-            return position;
-        },
+        var position = {};
+        var scale = this.get("scale");
+        position.x = Math.floor(absPosition.x/this.xScale(scale));
+        position.y = Math.floor(absPosition.y/this.yScale(scale));
+        position.z = Math.floor(absPosition.z/this.zScale(scale));
+        return position;
+    },
+
+    _positionForIndex: function(index){
+        var scale = this.get("scale");
+        var position = _.clone(index);
+        position.x = (position.x+0.5)*this.xScale(scale);
+        position.y = (position.y+0.5)*this.yScale(scale);
+        position.z = (position.z+0.5)*this.zScale(scale);
+        return position;
+    },
 
     removeCellAtIndex: function(indices){
 
@@ -116,9 +126,10 @@ Lattice = Backbone.Model.extend({
     subtractMesh: function(mesh){
         //todo this is specific to octa face
 
-        var scale = this.getScale();
-        var yscale = scale/2*Math.sqrt(3);
-        var zScale = this.get("scale")*2/Math.sqrt(6);
+        var scale = this.get("scale");
+        var xScale = this.xScale(scale);
+        var yScale = this.yScale(scale);
+        var zScale = this.zScale(scale);
 
         var cells = this.get("cells");
         var cellsMin = this.get("cellsMin");
@@ -136,7 +147,7 @@ Lattice = Backbone.Model.extend({
                 if (!firstCell) continue;//nothing in col
 
                 var origin = firstCell._calcPosition(0, this._add({x:x,y:y,z:z}, cellsMin));
-                zHeight = this._findIntersectionsInWindow(scale/2, yscale/2, origin, allVertexPos) || zHeight;
+                zHeight = this._findIntersectionsInWindow(xScale/2, yScale/2, origin, allVertexPos) || zHeight;
 
                 zHeight = Math.floor(zHeight/zScale);
                 for (var z=0;z<zHeight;z++){
@@ -363,22 +374,30 @@ Lattice = Backbone.Model.extend({
             return position;
         },
 
-        getScale: function(){//todo get rid of this
-            return this.get("scale")*(1.0+2*this.get("columnSeparation"));
+        getPositionForIndex: function(index){
+
+            var position = this._positionForIndex(index);
+//            var octHeight = 2*scale/Math.sqrt(6);
+//            var triHeight = latticeScale/2*Math.sqrt(3);
+//            position.x = indices.x*latticeScale;
+//            position.y = indices.y*triHeight-latticeScale/Math.sqrt(3);
+//            position.z = indices.z*octHeight;
+            if (Math.abs(index.y%2) == 1) position.x -= this.xScale()/2;
+            return position;
         },
 
-        xScale: function(){
-            var scale = this.get("scale");
+        xScale: function(scale){
+            if (!scale) scale = this.get("scale");
             var colSep = this.get("columnSeparation");
             return scale*(1+2*colSep);
         },
 
-        yScale: function(){
-            return this.xScale()/2*Math.sqrt(3);
+        yScale: function(scale){
+            return this.xScale(scale)/2*Math.sqrt(3);
         },
 
-        zScale: function(){
-            var scale = this.get("scale");
+        zScale: function(scale){
+            if (!scale) scale = this.get("scale");
             return 2*scale/Math.sqrt(6);
         },
 
@@ -429,22 +448,18 @@ Lattice = Backbone.Model.extend({
             return position;
         },
 
-        getScale: function(){
-            return this.get("scale")*(1.0+2*this.get("columnSeparation"));
-        },
-
-        xScale: function(){
-            var scale = this.get("scale");
+        xScale: function(scale){
+            if (!scale) scale = this.get("scale");
             var colSep = this.get("columnSeparation");
             return scale*(1+2*colSep);
         },
 
-        yScale: function(){
-            return this.xScale()/2*Math.sqrt(3);
+        yScale: function(scale){
+            return this.xScale(scale)/2*Math.sqrt(3);
         },
 
-        zScale: function(){
-            var scale = this.get("scale");
+        zScale: function(scale){
+            if (!scale) scale = this.get("scale");
             return 2*scale/Math.sqrt(6);
         },
 
@@ -482,21 +497,17 @@ Lattice = Backbone.Model.extend({
             return this._indexForPosition(absPosition);
         },
 
-
-        getScale: function(){
-            return this.get("scale");
+        xScale: function(scale){
+            if (!scale) scale = this.get("scale");
+            return scale;
         },
 
-        xScale: function(){
-            return this.get("scale");
+        yScale: function(scale){
+            return this.xScale(scale);
         },
 
-        yScale: function(){
-            return this.xScale();
-        },
-
-        zScale: function(){
-            return this.xScale();
+        zScale: function(scale){
+            return this.xScale(scale);
         },
 
         _makeCellForLatticeType: function(indices, scale){
@@ -533,28 +544,20 @@ Lattice = Backbone.Model.extend({
         },
 
         getPositionForIndex: function(index){
-            var scale = this.xScale();
-            var position = _.clone(index);
-            _.each(_.keys(position), function(key){
-                position[key] = (position[key]+0.5)*scale;
-            })
-            return position;
-        },
-
-        getScale: function(){
-            return this.get("scale");
+            return this._positionForIndex(index);
         },
 
-        xScale: function(){
-            return this.get("scale");
+        xScale: function(scale){
+            if (!scale) scale = this.get("scale")
+            return scale;
         },
 
-        yScale: function(){
-            return this.xScale();
+        yScale: function(scale){
+            return this.xScale(scale);
         },
 
-        zScale: function(){
-            return this.xScale();
+        zScale: function(scale){
+            return this.xScale(scale);
         },
 
         _makeCellForLatticeType: function(indices, scale){
diff --git a/js/threeViews/Highlighter.js b/js/threeViews/Highlighter.js
index d4d2b14a..7cfde2b6 100644
--- a/js/threeViews/Highlighter.js
+++ b/js/threeViews/Highlighter.js
@@ -12,7 +12,6 @@ Highlighter = Backbone.View.extend({
     initialize: function(options){
 
         var geometry = this._makeGeometry();
-        geometry.dynamic = true;
         this.mesh = new THREE.Mesh(geometry,
             new THREE.MeshBasicMaterial({
                 side:THREE.DoubleSide,
-- 
GitLab