From 722b8d308b81cd706aaad263fb23df9646d83c31 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Tue, 10 Mar 2015 16:02:00 -0400
Subject: [PATCH] octa rot working

---
 js/fea/DmaCell.js        | 13 ++++++++++
 js/fea/DmaCellOcta.js    | 55 +++++++++++++---------------------------
 js/models/BasePlane.js   | 19 ++++++++++++++
 js/models/LatticeOcta.js | 41 +++++++++++++++++++++++-------
 4 files changed, 81 insertions(+), 47 deletions(-)

diff --git a/js/fea/DmaCell.js b/js/fea/DmaCell.js
index 2844f1bf..0671a277 100644
--- a/js/fea/DmaCell.js
+++ b/js/fea/DmaCell.js
@@ -95,6 +95,19 @@ DMACell.prototype._setCellMeshVisibility = function(visibility){
     this.cellMesh.visible = visibility;
 };
 
+DMACell.prototype.xScale = function(scale){
+    return dmaGlobals.lattice.xScale(scale);
+};
+
+DMACell.prototype.yScale = function(scale){
+    return dmaGlobals.lattice.yScale(scale);
+};
+
+DMACell.prototype.zScale = function(scale){
+    return dmaGlobals.lattice.zScale(scale);
+};
+
+
 ///////////////////////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////META//////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/js/fea/DmaCellOcta.js b/js/fea/DmaCellOcta.js
index 919c4edc..2cf41250 100644
--- a/js/fea/DmaCellOcta.js
+++ b/js/fea/DmaCellOcta.js
@@ -39,7 +39,7 @@ DMAFaceOctaCell.prototype.calcHighlighterPosition = function(face){
 
     var direction = face.normal;
     var position = this.getPosition();
-    position.z += face.normal.z*dmaGlobals.lattice.zScale()/2;
+    position.z += face.normal.z*this.zScale()/2;
     return {index: _.clone(this.indices), direction:direction, position:position};
 };
 
@@ -119,12 +119,13 @@ DMAEdgeOctaCell.prototype = Object.create(DMAFaceOctaCell.prototype);
 
 DMAEdgeOctaCell.prototype._doMeshTransformations = function(){};
 
+//todo fix this
 DMAEdgeOctaCell.prototype.calcHighlighterPosition = function(face){
     var direction = face.normal.clone();
     direction.applyQuaternion(this.cellMesh.quaternion);
 
     var position = this.getPosition();
-    var zScale = dmaGlobals.lattice.zScale();
+    var zScale = this.zScale();
     position.x += direction.x*zScale/2;
     position.y += direction.y*zScale/2;
     position.z += direction.z*zScale/2;
@@ -145,48 +146,26 @@ function DMARotatedEdgeCell(indices, scale){
 }
 DMARotatedEdgeCell.prototype = Object.create(DMACell.prototype);
 
-DMARotatedEdgeCell.prototype._buildCellMesh = function(){//abstract mesh representation of cell
+DMARotatedEdgeCell.prototype._buildCellMesh = function(){
     return DMACell.prototype._buildCellMesh.call(this, unitVertexOcta);
 };
 
+DMARotatedEdgeCell.prototype._doMeshTransformations = function(mesh){
+    mesh.rotation.set(0, 0, Math.PI/4);
+};
+
 DMARotatedEdgeCell.prototype.calcHighlighterPosition = function(face, point){
 
     var position = this.getPosition();
-    var direction = null;
-
-    var xScale = dmaGlobals.lattice.xScale();
-    if (point.x < position.x+xScale/4 && point.x > position.x-xScale/4){
-        if (point.y > position.y-xScale/4 && point.y < position.y+xScale/4){
-            if (face.normal.z > 0) {
-                direction = new THREE.Vector3(0,0,1);
-                position.z += dmaGlobals.lattice.zScale()/2;
-            }
-            else {
-                direction = new THREE.Vector3(0,0,-1);
-                position.z -= dmaGlobals.lattice.zScale()/2;
-            }
-        } else {
-            if (point.y < position.y-xScale/4){
-                direction = new THREE.Vector3(0,-1,0);
-                position.y -= xScale/2;
-            } else {
-                direction = new THREE.Vector3(0,1,0);
-                position.y += xScale/2;
-            }
-        }
-    } else {
-        if (point.x < position.x-xScale/4){
-            direction = new THREE.Vector3(-1,0,0);
-            position.x -= xScale/2;
-        } else {
-            direction = new THREE.Vector3(1,0,0);
-            position.x += xScale/2;
-        }
-    }
-
+    var direction = new THREE.Vector3(0,0,1);
+    position.z += this.zScale()/2;
     return {index: _.clone(this.indices), direction:direction, position:position};
 };
 
+DMARotatedEdgeCell.prototype.zScale = function(scale){
+    return this.xScale(scale)*Math.sqrt(2);
+};
+
 
 ///////////////////////////////////////////////////////////////////////////////////////////////
 ////////////////////////VERTEX CONNECTED///////////////////////////////////////////////////////
@@ -207,16 +186,16 @@ DMAVertexOctaCell.prototype.calcHighlighterPosition = function(face, point){
     var position = this.getPosition();
     var direction = null;
 
-    var xScale = dmaGlobals.lattice.xScale();
+    var xScale = this.xScale();
     if (point.x < position.x+xScale/4 && point.x > position.x-xScale/4){
         if (point.y > position.y-xScale/4 && point.y < position.y+xScale/4){
             if (face.normal.z > 0) {
                 direction = new THREE.Vector3(0,0,1);
-                position.z += dmaGlobals.lattice.zScale()/2;
+                position.z += this.zScale()/2;
             }
             else {
                 direction = new THREE.Vector3(0,0,-1);
-                position.z -= dmaGlobals.lattice.zScale()/2;
+                position.z -= this.zScale()/2;
             }
         } else {
             if (point.y < position.y-xScale/4){
diff --git a/js/models/BasePlane.js b/js/models/BasePlane.js
index 90a3a9fa..e62e4415 100644
--- a/js/models/BasePlane.js
+++ b/js/models/BasePlane.js
@@ -194,6 +194,7 @@ OctaBasePlane = BasePlane.extend({
 
 });
 
+
 ///////////////////////////////////////////////////////////////////////////////////
 /////////////////////////////CUBE GRID/////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////////
@@ -247,4 +248,22 @@ SquareBasePlane = BasePlane.extend({
         return {index: index, direction: new THREE.Vector3(0,0,1), position:latticePosition};
     }
 
+});
+
+
+
+///////////////////////////////////////////////////////////////////////////////////
+//////////////////////////OCTA EDGE ROT////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////////
+
+RotEdgeOctaBasePlane = SquareBasePlane.extend({
+
+    calcHighlighterPosition: function(face, position){
+        var index = dmaGlobals.lattice.getIndexForPosition(position);
+        index.z = this.get("zIndex") - 1;//pretend we're on the top of the cell underneath the baseplane
+        var latticePosition = dmaGlobals.lattice.getPositionForIndex(index);
+        latticePosition.x -= dmaGlobals.lattice.xScale()/2;
+        latticePosition.y -= dmaGlobals.lattice.yScale()/2;
+        return {index: index, direction: new THREE.Vector3(0,0,1), position:latticePosition};
+    }
 });
\ No newline at end of file
diff --git a/js/models/LatticeOcta.js b/js/models/LatticeOcta.js
index 30269f8e..26f7588e 100644
--- a/js/models/LatticeOcta.js
+++ b/js/models/LatticeOcta.js
@@ -184,7 +184,7 @@ OctaLatticeSubclasses = {
             return this.xScale(scale)/2*Math.sqrt(3);
         },
 
-        zScale: function(scale){
+        zScale: function(scale){//base plane calls this, need it
             if (!scale) scale = this.get("scale");
             if (this.get("freeformCellType") == "octa") return 2*scale/Math.sqrt(6);
             return 2*scale/Math.sqrt(24);
@@ -318,22 +318,35 @@ OctaLatticeSubclasses = {
 
             //bind events
 
-            this.set("basePlane", new SquareBasePlane({scale:this.get("scale")}));
+            this.set("basePlane", new RotEdgeOctaBasePlane({scale:this.get("scale")}));
             this.set("highlighter", new OctaVertexHighlighter({scale:this.get("scale")}));
-
         },
 
         getIndexForPosition: function(absPosition){
-            return this._indexForPosition(absPosition);
+            var position = {};
+            var scale = this.get("scale");
+            position.x = Math.floor(absPosition.x/this.xScale(scale)+0.5);
+            position.y = Math.floor(absPosition.y/this.yScale(scale)+0.5);
+            position.z = Math.floor(absPosition.z/this.zScale(scale)+0.5);
+            return position;
         },
 
         getPositionForIndex: function(index){
-            return this._positionForIndex(index);
+            var scale = this.get("scale");
+            var position = _.clone(index);
+            if (index.z %2 != 0){
+                position.x += 0.5;
+                position.y += 0.5;
+            }
+            position.x = (position.x)*this.xScale(scale);
+            position.y = (position.y)*this.yScale(scale);
+            position.z = (position.z+1)*this.zScale(scale);
+            return position;
         },
 
         xScale: function(scale){
             if (!scale) scale = this.get("scale");
-            return scale*Math.sqrt(2);
+            return scale;
         },
 
         yScale: function(scale){
@@ -341,7 +354,7 @@ OctaLatticeSubclasses = {
         },
 
         zScale: function(scale){
-            return this.xScale(scale);
+            return this.xScale(scale)*Math.sqrt(2)/2;
         },
 
         _makeCellForLatticeType: function(indices, scale){
@@ -373,11 +386,21 @@ OctaLatticeSubclasses = {
         },
 
         getIndexForPosition: function(absPosition){
-            return this._indexForPosition(absPosition);
+            var position = {};
+            var scale = this.get("scale");
+            position.x = Math.floor(absPosition.x/this.xScale(scale)+0.5);
+            position.y = Math.floor(absPosition.y/this.yScale(scale)+0.5);
+            position.z = Math.floor(absPosition.z/this.zScale(scale)+0.5);
+            return position;
         },
 
         getPositionForIndex: function(index){
-            return this._positionForIndex(index);
+            var scale = this.get("scale");
+            var position = _.clone(index);
+            position.x = (position.x)*this.xScale(scale);
+            position.y = (position.y)*this.yScale(scale);
+            position.z = (position.z+0.5)*this.zScale(scale);
+            return position;
         },
 
         xScale: function(scale){
-- 
GitLab