From 74fd207cc3d550699ed5da8da2cb22a99433d71e Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Thu, 26 Feb 2015 00:57:56 -0500
Subject: [PATCH] inverse geometry working

---
 js/fea/DmaCell.js                | 17 +++++--
 js/menus/LatticeMenuView.js      |  9 +++-
 js/menus/OptimizationMenuView.js |  2 +-
 js/models/Lattice.js             | 82 ++++++++++++++++++++++++--------
 4 files changed, 84 insertions(+), 26 deletions(-)

diff --git a/js/fea/DmaCell.js b/js/fea/DmaCell.js
index 265a9923..ada9aa8c 100644
--- a/js/fea/DmaCell.js
+++ b/js/fea/DmaCell.js
@@ -49,6 +49,17 @@ DMACell.prototype.drawForMode = function(mode){
     }
 };
 
+DMACell.prototype.hide = function(){
+    this._setCellMeshVisibility(false);
+    _.each(this.parts, function(part){
+        if (part) part.hide();
+    });
+};
+
+DMACell.prototype.show = function(){
+    this._setCellMeshVisibility(true);
+};
+
 DMACell.prototype._setCellMeshVisibility = function(visibility){
     if (!this.cellMesh) return;
     this.cellMesh.visible = visibility;
@@ -146,7 +157,7 @@ DMACell.prototype.destroy = function(){
     var unitTetraCellGeo = new THREE.TetrahedronGeometry(Math.sqrt(3/8));
     unitTetraCellGeo.applyMatrix(new THREE.Matrix4().makeRotationZ(Math.PI/4));
     unitTetraCellGeo.applyMatrix(new THREE.Matrix4().makeRotationX((Math.PI-Math.atan(2*Math.sqrt(2)))/2));
-    unitTetraCellGeo.applyMatrix(new THREE.Matrix4().makeTranslation(0,0,1/Math.sqrt(8)/2));
+    unitTetraCellGeo.applyMatrix(new THREE.Matrix4().makeTranslation(0,0,Math.sqrt(3/8)-1/Math.sqrt(6)));
 
     function DMATetraCell(indices, scale, lattice){
         DMACell.call(this, indices, scale, lattice);
@@ -160,11 +171,11 @@ DMACell.prototype.destroy = function(){
     DMATetraCell.prototype._buildCellMesh = function(zIndex){//abstract mesh representation of cell
         var mesh;
         mesh = THREE.SceneUtils.createMultiMaterialObject(unitTetraCellGeo, cellMaterials);
-        if (Math.abs(zIndex%4) == 1 || Math.abs(zIndex%4) == 2) mesh.rotation.set(Math.PI,0,0);
+        if (zIndex%2 !=0) mesh.rotateX(Math.PI);
+        if (Math.abs(zIndex%4) == 2 || Math.abs(zIndex%4) == 3) mesh.rotateZ(Math.PI/3);
 
         mesh.myParent = this;//we need a reference to this instance from the mesh for intersection selection stuff
         dmaGlobals.three.sceneAdd(mesh, "inverseCell");
-        mesh.visible = false;
         return mesh;
     };
 
diff --git a/js/menus/LatticeMenuView.js b/js/menus/LatticeMenuView.js
index 8c1dc448..66cf2992 100644
--- a/js/menus/LatticeMenuView.js
+++ b/js/menus/LatticeMenuView.js
@@ -14,7 +14,8 @@ LatticeMenuView = Backbone.View.extend({
         "click .connectionType":                        "_changeConnectionType",
         "slide #scaleSlider":                           "_sliderDidSlide",
         "slideStop #scaleSlider":                       "_changeScaleSlider",
-        "change #preserveCells":                        "_changePreserveCells"
+        "change #preserveCells":                        "_changePreserveCells",
+        "change #showInverse":                          "_showInverseCells"
     },
 
 
@@ -108,6 +109,10 @@ LatticeMenuView = Backbone.View.extend({
         this.lattice.set("shouldPreserveCells", $(e.target).prop("checked"));
     },
 
+    _showInverseCells: function(e){
+        this.lattice.set("shouldShowInverseCells", $(e.target).prop("checked"));
+    },
+
     render: function(){
         if (this.model.get("currentTab") != "lattice") return;
         this.$el.html(this.template(_.extend(this.model.attributes, this.lattice.attributes)));
@@ -143,7 +148,7 @@ LatticeMenuView = Backbone.View.extend({
             Preserve cells on lattice change\
         </label><br/><br/>\
         <label class="checkbox">\
-            <input type="checkbox"  value="" id="showInverse" data-toggle="checkbox" class="custom-checkbox"><span class="icons"><span class="icon-unchecked"></span><span class="icon-checked"></span></span>\
+            <input type="checkbox"  <% if (shouldShowInverseCells) { %> checked="checked" <% } %> value="" id="showInverse" data-toggle="checkbox" class="custom-checkbox"><span class="icons"><span class="icon-unchecked"></span><span class="icon-checked"></span></span>\
             Show Inverse Geometry\
         </label><br/><br/>\
         Scale:&nbsp;&nbsp;<input id="scaleSlider" data-slider-id="ex1Slider" type="text" data-slider-min="1" data-slider-max="100" data-slider-step="0.1" data-slider-value="<%= scale %>"/>\
diff --git a/js/menus/OptimizationMenuView.js b/js/menus/OptimizationMenuView.js
index 7bd368ff..6e566afa 100644
--- a/js/menus/OptimizationMenuView.js
+++ b/js/menus/OptimizationMenuView.js
@@ -24,7 +24,7 @@ OptimizationMenuView = Backbone.View.extend({
         <br/><br/>\
         data representation for structural optimization: <a href="http://www.demo.cs.brandeis.edu/papers/edc98.pdf">lego tree</a>, <a href="http://creativemachines.cornell.edu/sites/default/files/Cheney_MacCurdy_Clune_Lipson--Unshackling_Evolution.pdf">CPPN-NEAT (functional)</a>, octree, some other kind of tree, grammar?\
         <br/><br/>\
-        optimization parameters representation may depend on application: <a href="http://2.bp.blogspot.com/-4o29mAWZjag/UURyXoraMKI/AAAAAAAAHyM/HnevUeJ68Sw/s400/yyy.bmp">nike</a>, airbus, assembler/declarative design\
+        optimization parameters representation may depend on application: <a href="http://en.wikipedia.org/wiki/Evolved_antenna">antenna</a>, <a href="http://2.bp.blogspot.com/-4o29mAWZjag/UURyXoraMKI/AAAAAAAAHyM/HnevUeJ68Sw/s400/yyy.bmp">nike</a>, airbus, assembler/declarative design\
         <br/><br/>\
         active components/structures: <a href="https://www.youtube.com/watch?v=z9ptOeByLA4">voxcad</a>, <a href="http://arxiv.org/pdf/cs/0004003v2.pdf">conway</a>\
         <br/><br/>\
diff --git a/js/models/Lattice.js b/js/models/Lattice.js
index a18e6725..57d67d2b 100644
--- a/js/models/Lattice.js
+++ b/js/models/Lattice.js
@@ -18,7 +18,8 @@ Lattice = Backbone.Model.extend({
         numInvCells: 0,
         basePlane: null,//plane to build from
         highlighter: null,//highlights build-able surfaces
-        shouldPreserveCells: true//preserve cells when changing lattice type
+        shouldPreserveCells: true,//preserve cells when changing lattice type
+        shouldShowInverseCells: false
     },
 
     //pass in fillGeometry
@@ -27,6 +28,7 @@ Lattice = Backbone.Model.extend({
 
         //bind events
         this.listenTo(this, "change:scale", this._scaleDidChange);
+        this.listenTo(this, "change:shouldShowInverseCells", this._showInverseCells);
     },
 
     ////////////////////////////////////////////////////////////////////////////////////
@@ -287,8 +289,32 @@ Lattice = Backbone.Model.extend({
     ////////////////////////////////////////////////////////////////////////////////////
 
     cellModeDidChange: function(mode){
+        if (mode == "part" || !this.get("shouldShowInverseCells")){
+            this._iterCells(this.get("cells"), function(cell){
+                if (cell && cell.drawForMode) cell.drawForMode(mode);
+            });
+            this._iterCells(this.get("inverseCells"), function(cell){
+                if (cell) cell.hide();
+            });
+        } else {
+            this._showInverseCells();
+        }
+        dmaGlobals.three.render();
+    },
+
+    _showInverseCells: function(){
+        var shouldShow = this.get("shouldShowInverseCells");
         this._iterCells(this.get("cells"), function(cell){
-            if (cell && cell.drawForMode) cell.drawForMode(mode);
+            if (cell) {
+                if (shouldShow) cell.hide();
+                else cell.show();
+            }
+        });
+        this._iterCells(this.get("inverseCells"), function(cell){
+            if (cell) {
+                if (shouldShow) cell.show();
+                else cell.hide();
+            }
         });
         dmaGlobals.three.render();
     },
@@ -423,21 +449,28 @@ Lattice = Backbone.Model.extend({
             var scale = this.get("scale");
             var position = _.clone(index);
 
-            var oddZ = (position.z%2 != 0);
+            var oddZ = position.z%2 != 0;
+            var upPoint = (position.z%4 == 0 || Math.abs(position.z%4) == 3);
             position.z = Math.floor(position.z/2);
 
-            if (oddZ){
+            if (!upPoint){
                 position.x = (position.x)*this.xScale(scale);
                 position.y = position.y*this.yScale(scale);
             } else {
                 position.x = (position.x+0.5)*this.xScale(scale);
                 position.y = (position.y)*this.yScale(scale)-scale/Math.sqrt(3)/2;
             }
-            position.z = (position.z)*this.zScale(scale);
-            if (Math.abs(index.z%4) == 1 || Math.abs(index.z%4) == 2) position.z += this.zScale(scale);
+
+            if (oddZ){
+                position.z = (position.z + 1)*this.zScale(scale);
+            } else {
+                position.z = (position.z)*this.zScale(scale);
+            }
+
+//            if (Math.abs(index.z%4) == 1 || Math.abs(index.z%4) == 2) position.z += this.zScale(scale);
 
             if ((index.y%2) != 0) {
-                if (oddZ){
+                if (!upPoint){
                     position.x += this.xScale(scale)/2;
                 } else {
                     position.x -= this.xScale(scale)/2;
@@ -477,27 +510,34 @@ Lattice = Backbone.Model.extend({
         _addInverseCellsForIndex: function(index){
 
             index = _.clone(index);
+
+            var oddZ = index.z%2 != 0;
+            var z0 = 0;
+            if (oddZ) z0 = 1;
+            var z1 = Math.abs(z0-1);
+
             index.z*=2;
             var inverseIndicesToAdd;
             if (index.y%2 == 0){
+
                 inverseIndicesToAdd = [
-                    this._add(index, {x:0,y:0,z:0}),
-                    this._add(index, {x:0,y:1,z:0}),
-                    this._add(index, {x:1,y:1,z:0}),
+                    this._add(index, {x:0,y:0,z:z0}),
+                    this._add(index, {x:0,y:1,z:z0}),
+                    this._add(index, {x:1,y:1,z:z0}),
 
-                    this._add(index, {x:0,y:0,z:1}),
-                    this._add(index, {x:0,y:1,z:1}),
-                    this._add(index, {x:1,y:0,z:1})
+                    this._add(index, {x:0,y:0,z:z1}),
+                    this._add(index, {x:0,y:1,z:z1}),
+                    this._add(index, {x:1,y:0,z:z1})
                 ];
             } else {
                 inverseIndicesToAdd = [
-                    this._add(index, {x:0,y:0,z:0}),
-                    this._add(index, {x:-1,y:1,z:0}),
-                    this._add(index, {x:0,y:1,z:0}),
+                    this._add(index, {x:0,y:0,z:z0}),
+                    this._add(index, {x:-1,y:1,z:z0}),
+                    this._add(index, {x:0,y:1,z:z0}),
 
-                    this._add(index, {x:-1,y:0,z:1}),
-                    this._add(index, {x:0,y:1,z:1}),
-                    this._add(index, {x:0,y:0,z:1})
+                    this._add(index, {x:-1,y:0,z:z1}),
+                    this._add(index, {x:0,y:1,z:z1}),
+                    this._add(index, {x:0,y:0,z:z1})
                 ];
             }
 
@@ -508,7 +548,9 @@ Lattice = Backbone.Model.extend({
                 self._checkForMatrixExpansion(invCells, invIndex, invIndex, "inverseCellsMax", "inverseCellsMin");
                 var indexRel = self._subtract(invIndex, self.get("inverseCellsMin"));
                 if (!invCells[indexRel.x][indexRel.y][indexRel.z]) {
-                    invCells[indexRel.x][indexRel.y][indexRel.z] = self._makeInvCellForLatticeType(invIndex, scale);
+                    var cell = self._makeInvCellForLatticeType(invIndex, scale);
+                    cell.hide();
+                    invCells[indexRel.x][indexRel.y][indexRel.z] = cell;
                     self.set("numInvCells", self.get("numInvCells")+1);
                 }
             });
-- 
GitLab