From c8d0664903f21ca9b26165617d62d835b980033b Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Fri, 6 Mar 2015 13:29:39 -0500
Subject: [PATCH] position calculations for freeform truss

---
 js/fea/DmaCell.js            | 44 ++++++++++++++++++++-
 js/models/AppState.js        |  2 +-
 js/models/BasePlane.js       |  4 ++
 js/models/Lattice.js         | 77 +++++++++++++++++++++++++++++++++++-
 js/threeViews/Highlighter.js | 16 ++++++++
 5 files changed, 140 insertions(+), 3 deletions(-)

diff --git a/js/fea/DmaCell.js b/js/fea/DmaCell.js
index 23c9530d..1675974c 100644
--- a/js/fea/DmaCell.js
+++ b/js/fea/DmaCell.js
@@ -72,6 +72,10 @@ DMACell.prototype.getPosition = function(){//need for part relay
     return this.cellMesh.position.clone();
 };
 
+DMACell.prototype.getOrientation = function(){//need for part relay
+    return this.cellMesh.rotation.clone();
+};
+
 DMACell.prototype._calcPosition = function(){//need for part relay
     if (this.isInverse) return dmaGlobals.lattice.getInvCellPositionForIndex(this.indices);
     return dmaGlobals.lattice.getPositionForIndex(this.indices);
@@ -154,7 +158,7 @@ DMACell.prototype.destroy = function(){
         if (face.normal.z<0.99) direction = null;//only highlight horizontal faces
 
         var position = this.getPosition();
-        position.z += dmaGlobals.lattice.zScale()/2;
+        position.z += face.normal.z*dmaGlobals.lattice.zScale()/2;
         return {index: _.clone(this.indices), direction:direction, position:position};
     }
 
@@ -170,6 +174,44 @@ DMACell.prototype.destroy = function(){
 
     self.DMAEdgeOctaCell = DMAEdgeOctaCell;
 
+
+    function DMAFreeFormOctaCell(indices, scale, parentCellPos, parentCellOrient, direction){
+        this.parentPos = parentCellPos;
+        this.parentOrient = parentCellOrient;
+        this.parentDirection = direction;
+        DMAFaceOctaCell.call(this, indices, scale);
+    }
+    DMAFreeFormOctaCell.prototype = Object.create(DMAFaceOctaCell.prototype);
+
+    DMAFreeFormOctaCell.prototype._doMeshTransformations = function(mesh){
+
+//        console.log(this.parentPos);
+//        console.log(this.parentDirection);
+//        console.log(position);
+//        mesh.position.set(position);
+
+    };
+
+    DMAFreeFormOctaCell.prototype.calcHighlighterPosition = function(face){
+        var direction = face.normal;
+        var position = this.getPosition();
+        position.x += direction.x*dmaGlobals.lattice.xScale()/2;
+        position.y += direction.y*dmaGlobals.lattice.yScale()/2;
+        position.z += direction.z*dmaGlobals.lattice.zScale()/2;
+        return {index: _.clone(this.indices), direction:direction, position:position};
+    }
+
+    DMAFreeFormOctaCell.prototype._calcPosition = function(){
+        var position = {};
+        var zScale = dmaGlobals.lattice.zScale();
+        position.x = this.parentPos.x+this.parentDirection.x*zScale/2;
+        position.y = this.parentPos.y+this.parentDirection.y*zScale/2;
+        position.z = this.parentPos.z+this.parentDirection.z*zScale/2;
+        return position;
+    };
+
+    self.DMAFreeFormOctaCell = DMAFreeFormOctaCell;
+
 })();
 
     /////////////////////////////////////////////////TETRA CELL////////////////////////////////////
diff --git a/js/models/AppState.js b/js/models/AppState.js
index f3e50eee..da1790be 100644
--- a/js/models/AppState.js
+++ b/js/models/AppState.js
@@ -20,7 +20,7 @@ AppState = Backbone.Model.extend({
 
         allCellTypes: {octa:"Octahedron", cube:"Cube"},
         allConnectionTypes: {
-            octa: {face:"Face", edge:"Edge", edgeRot:"Rotated Edge", vertex:"Vertex"},
+            octa: {face:"Face", freeformFace:"Freeform Face", edge:"Edge", edgeRot:"Rotated Edge", vertex:"Vertex"},
             cube: {face:"Face"}
         },
         allPartTypes:{
diff --git a/js/models/BasePlane.js b/js/models/BasePlane.js
index 7f551e5e..f21e2db1 100644
--- a/js/models/BasePlane.js
+++ b/js/models/BasePlane.js
@@ -39,6 +39,10 @@ BasePlane = Backbone.Model.extend({
         });
     },
 
+    getOrientation: function(){
+        return {x:0, y:0, z:0};
+    },
+
 //    _renderForCurrentScene: function(){
 //    },
 
diff --git a/js/models/Lattice.js b/js/models/Lattice.js
index 2ac1c6fc..222f47e9 100644
--- a/js/models/Lattice.js
+++ b/js/models/Lattice.js
@@ -371,13 +371,16 @@ Lattice = Backbone.Model.extend({
     _updateLatticeType: function(){
         var cellType = this.get("cellType");
         var connectionType = this.get("connectionType");
-        if (!this.get("shouldPreserveCells")) this.clearCells();
+        if (!this.get("shouldPreserveCells") || this.previous("connectionType") == "freeformFace") this.clearCells();
         if (this._undo) this._undo();
         if (this.get("basePlane")) this.get("basePlane").destroy();
         if (this.get("highlighter")) this.get("highlighter").destroy();
         if (cellType == "octa"){
             if (connectionType == "face"){
                 _.extend(this, this.OctaFaceLattice);
+            } else if (connectionType == "freeformFace"){
+                this.clearCells();
+                _.extend(this, this.OctaFreeFormFaceLattice);
             } else if (connectionType == "edge"){
                 _.extend(this, this.OctaFaceLattice);
                 _.extend(this, this.OctaEdgeLattice);
@@ -590,6 +593,78 @@ Lattice = Backbone.Model.extend({
 
     },
 
+
+////////////////////////////////////////////////////////////////////////////////////////
+//////////////////////////////FACE CONN OCTA FREEFORM////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////
+
+
+    OctaFreeFormFaceLattice: {
+
+        _initLatticeType: function(){
+
+            //bind events
+
+            this.set("basePlane", new OctaBasePlane({scale:this.get("scale")}));
+            this.set("highlighter", new OctaFreeFormHighlighter({scale:this.get("scale")}));
+
+        },
+
+        addFreeFormCell: function(parentCellPos, parentCellOrient, direction){
+            var scale = this.get("scale");
+            var cells = this.get("cells");
+//            console.log(parentCellPos);
+//            console.log(parentCellOrient);
+//            console.log(direction);
+            cells[0][0].push(new DMAFreeFormOctaCell({x:0,y:0,z:cells[0][0].length}, scale, parentCellPos, parentCellOrient, direction));
+            this.set("numCells", this.get("numCells")+1);
+            dmaGlobals.three.render();
+        },
+
+        getIndexForPosition: function(absPosition){//only used by baseplane
+            var scale = this.get("scale");
+            var yIndex = Math.floor(absPosition.y/this.yScale(scale));
+            if (yIndex%2 != 0) absPosition.x += this.xScale(scale)/2;
+            var index = this._indexForPosition(absPosition);
+            if (index.z%2 == 1) index.y += 1;
+            return index;
+        },
+
+        getPositionForIndex: function(index){//only used by baseplane
+            var scale = this.get("scale");
+            var position = _.clone(index);
+            var xScale = this.xScale(scale);
+            position.x = (position.x+1/2)*xScale;
+            position.y = position.y*this.yScale(scale)+scale/Math.sqrt(3)/2;
+            position.z = (position.z+0.5)*this.zScale(scale);
+            if ((index.y%2) != 0) position.x -= this.xScale(scale)/2;
+            return position;
+        },
+
+        xScale: function(scale){
+            if (!scale) scale = this.get("scale");
+            var colSep = this.get("columnSeparation");
+            return scale*(1+2*colSep);
+        },
+
+        yScale: function(scale){
+            return this.xScale(scale)/2*Math.sqrt(3);
+        },
+
+        zScale: function(scale){
+            if (!scale) scale = this.get("scale");
+            return 2*scale/Math.sqrt(6);
+        },
+
+        _undo: function(){//remove all the mixins, this will help with debugging later
+            var self = this;
+            _.each(_.keys(this.OctaFreeFormFaceLattice), function(key){
+                self[key] = null;
+            });
+        }
+
+    },
+
 ////////////////////////////////////////////////////////////////////////////////////////
 //////////////////////////////EDGE CONN OCTA LATTICE////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////////////
diff --git a/js/threeViews/Highlighter.js b/js/threeViews/Highlighter.js
index 03f4c4cc..264fc158 100644
--- a/js/threeViews/Highlighter.js
+++ b/js/threeViews/Highlighter.js
@@ -109,6 +109,11 @@ Highlighter = Backbone.View.extend({
 
         if (shouldAdd){
             if (!this.isVisible() || !this.highlightedObject) return;
+            if (dmaGlobals.lattice.get("connectionType") == "freeformFace"){
+                //todo make this work for baseplane
+                dmaGlobals.lattice.addFreeFormCell(this.mesh.position.clone(), this.highlightedObject.getOrientation(), this.direction);
+                return;
+            }
             dmaGlobals.lattice.addCellAtIndex(this._getNextCellPosition());
         } else {
             if (!this.highlightedObject) return;
@@ -163,6 +168,17 @@ OctaVertexHighlighter = Highlighter.extend({
 
 });
 
+OctaFreeFormHighlighter = Highlighter.extend({
+
+    _makeGeometry: function(){
+        return new THREE.SphereGeometry(0.2);
+    },
+
+    _setPosition: function(position, direction){
+        this.mesh.position.set(position.x, position.y, position.z);
+    }
+});
+
 ///////////////////////////////////////////////////////////////////////////////////
 /////////////////////////////CUBE /////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////////
-- 
GitLab