From 968b5ca036eb85c5c4f5cc02c7c6beb2cebff1ad Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Wed, 27 May 2015 11:09:39 -0700
Subject: [PATCH] gik parts working again

---
 index.html                                   |  4 +-
 js/cells/DMACell.js                          |  9 +++-
 js/cells/DMAParentCell.js                    | 22 +++++-----
 js/cells/GIKCell.js                          |  2 +-
 js/{ => cells}/supercells/GIKSuperCell.js    | 21 +++++++--
 js/lattice/Lattice.js                        |  6 +--
 js/models/ThreeModel.js                      |  7 ++-
 js/parts/{GIKMicroLegoPart.js => GIKPart.js} | 45 +++++++-------------
 js/threeViews/Highlighter.js                 |  2 +-
 9 files changed, 65 insertions(+), 53 deletions(-)
 rename js/{ => cells}/supercells/GIKSuperCell.js (84%)
 rename js/parts/{GIKMicroLegoPart.js => GIKPart.js} (74%)

diff --git a/index.html b/index.html
index f8653968..d2d11aef 100644
--- a/index.html
+++ b/index.html
@@ -51,7 +51,7 @@
     <script src="js/cells/OctaRotEdgeCell.js"></script>
     <script src="js/cells/OctaVertexCell.js"></script>
 
-    <script src="js/supercells/GIKSuperCell.js"></script>
+    <script src="js/cells/supercells/GIKSuperCell.js"></script>
     <script src="js/cells/DMACellFreeform.js"></script>
 
     <script src="js/cells/CubeCell.js"></script>
@@ -62,7 +62,7 @@
     <!--<script src="js/cells/TetraFaceCell.js"></script>-->
 
     <script src="js/parts/DmaPart.js"></script>
-    <script src="js/parts/GIKMicroLegoPart.js"></script>
+    <script src="js/parts/GIKPart.js"></script>
     <script src="js/parts/OctaEdgeVoxPart.js"></script>
     <script src="js/parts/OctaFaceTriPart.js"></script>
 
diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js
index e27013a4..08e91992 100644
--- a/js/cells/DMACell.js
+++ b/js/cells/DMACell.js
@@ -12,7 +12,7 @@ function DMACell(indices){
 
     //object 3d is parent to all 3d elements related to cell, parts, beams, nodes, etc
     this.object3D = this._buildObject3D();
-    this._addMeshes(this._buildMesh(), this.object3D);//build cell meshes
+    this._addChildren(this._buildMesh(), this.object3D);//build cell meshes
     if (!this.superCell) globals.three.sceneAdd(this.object3D, this._getSceneType());
 
     this.setMode();
@@ -27,9 +27,14 @@ DMACell.prototype._getSceneType = function(){//todo need this?
 DMACell.prototype._buildObject3D = function(){
     var object3D = this._translateCell(this._rotateCell(new THREE.Object3D()));
     object3D.myParent = this;//reference to get mouse raycasting back
+    object3D.name = "object3d";
     return object3D;
 };
 
+DMACell.prototype.getObject3D = function(){//only called by supercells
+    return this.object3D;
+}
+
 DMACell.prototype._buildMesh = function(){
     var geometry = this._getGeometry();
     var meshes = [];
@@ -74,7 +79,7 @@ DMACell.prototype.setMode = function(mode){
                 this.parts = this._initParts();
                 var self = this;
                 _.each(this.parts, function(part){
-                    self._addMeshes(part.getMesh());
+                    self._addChildren(part.getMesh());
                 });
             }
             break;
diff --git a/js/cells/DMAParentCell.js b/js/cells/DMAParentCell.js
index 20f9cbf9..21ac7195 100644
--- a/js/cells/DMAParentCell.js
+++ b/js/cells/DMAParentCell.js
@@ -18,23 +18,23 @@ DMAParentCell.prototype._translateCell = function(object3D){
     return object3D;
 };
 
-DMAParentCell.prototype._addMeshes = function(meshes, object3D){//accepts an array or a single mesh
-    this._addRemoveMeshes(true, meshes, object3D);
+DMAParentCell.prototype._addChildren = function(children, object3D){//accepts an array or a single mesh
+    this._addRemoveChildren(true, children, object3D);
 };
 
-DMAParentCell.prototype._removeMeshes = function(meshes, object3D){//accepts an array or a single mesh
-    this._addRemoveMeshes(false, meshes, object3D);
+DMAParentCell.prototype._removeChildren = function(children, object3D){//accepts an array or a single mesh
+    this._addRemoveMeshes(false, children, object3D);
 };
 
-DMAParentCell.prototype._addRemoveMeshes = function(shouldAdd, meshes, object3D){//accepts an array or a single mesh
+DMAParentCell.prototype._addRemoveChildren = function(shouldAdd, children, object3D){//accepts an array or a single mesh
     if (object3D === undefined) object3D = this.object3D;
-    if (meshes.constructor === Array){
-        _.each(meshes, function(mesh){
-            if (shouldAdd) object3D.add(mesh);
-            else object3D.remove(mesh);
+    if (children.constructor === Array){
+        _.each(children, function(child){
+            if (shouldAdd) object3D.add(child);
+            else object3D.remove(child);
         });
-    } else if (shouldAdd) object3D.add(meshes);
-    else object3D.remove(meshes);
+    } else if (shouldAdd) object3D.add(children);
+    else object3D.remove(children);
 };
 
 DMAParentCell.prototype.hide = function(){
diff --git a/js/cells/GIKCell.js b/js/cells/GIKCell.js
index 5093ccae..f156f061 100644
--- a/js/cells/GIKCell.js
+++ b/js/cells/GIKCell.js
@@ -22,7 +22,7 @@
     };
 
     GIKCell.prototype._initParts = function(){
-        if (!this.superCell) return [];
+        if (!this.superCell || this.superCell === true) return null;
         var parts  = [];
         var isEnd = this.superCellIndex == 0 || this.superCellIndex == this.superCell.getLength();
         if (globals.lattice.get("partType") == "lego") {
diff --git a/js/supercells/GIKSuperCell.js b/js/cells/supercells/GIKSuperCell.js
similarity index 84%
rename from js/supercells/GIKSuperCell.js
rename to js/cells/supercells/GIKSuperCell.js
index 244fad6c..aeb4cce8 100644
--- a/js/supercells/GIKSuperCell.js
+++ b/js/cells/supercells/GIKSuperCell.js
@@ -12,17 +12,32 @@ GIKSuperCell = function(length, range, cells){
     this.cells = cells;
 
     this.object3D = this._buildObject3D();
-    this._addMeshes(this._buildMesh(length), this.object3D);
-    globals.three.sceneAdd(this.object3D);
+    this._addChildren(this._buildMesh(length), this.object3D);
+    this._addChildren(this._getCellObject3Ds(cells));
+    globals.three.sceneAdd(this.object3D, this._getSceneType());
 
+//    this.setMode();
 
 };
 GIKSuperCell.prototype = Object.create(DMAParentCell.prototype);
 
+GIKSuperCell.prototype._getSceneType = function(){//todo need this?
+    if (this.indices) return "supercell";
+    return null;
+};
+
 GIKSuperCell.prototype._buildObject3D = function(){
     return this._translateCell(this._rotateCell(new THREE.Object3D()));
 };
 
+GIKSuperCell.prototype._getCellObject3Ds = function(cells){
+    var object3Ds = [];
+    _.each(cells, function(cell){
+        object3Ds.push(cell.getObject3D());
+    });
+    return object3Ds;
+};
+
 GIKSuperCell.prototype._rotateCell = function(object3D){
     if (this.indices && this.indices.z%2 != 0) object3D.rotateZ(Math.PI/2);
     return object3D;
@@ -72,7 +87,7 @@ GIKSuperCell.prototype.setMode = function(mode){
                 this.parts = this._initParts();
                 var self = this;
                 _.each(this.parts, function(part){
-                    self._addMeshes(part.getMesh());
+                    self._addChildren(part.getMesh());
                 });
             }
             break;
diff --git a/js/lattice/Lattice.js b/js/lattice/Lattice.js
index da455ee5..79fa84da 100644
--- a/js/lattice/Lattice.js
+++ b/js/lattice/Lattice.js
@@ -21,9 +21,9 @@ Lattice = Backbone.Model.extend({
         //spacing for connectors/joints
         cellSeparation: {xy:0, z:0},
 
-        cellType: "octa",
-        connectionType: "edgeRot",
-        partType: "vox",
+        cellType: "cube",
+        connectionType: "gik",
+        partType: "lego",
         materialType: "fiberGlass",
         gikLength: 4
     },
diff --git a/js/models/ThreeModel.js b/js/models/ThreeModel.js
index f416c2ee..11870b1d 100644
--- a/js/models/ThreeModel.js
+++ b/js/models/ThreeModel.js
@@ -57,7 +57,12 @@ function ThreeModel(){
 
         if (type == "cell"){
             cells.push(object.children[0]);
-        } else if (type == "part"){
+        } else if (type == "supercell"){
+            _.each(object.children, function(child){//add cells as hover
+                if (child.name != "object3d") return;
+                cells.push(child.children[0]);
+            });
+        } else if (type == "part"){//todo change this
             parts.push(object);
         } else if (type == "basePlane"){
             basePlane.push(object);
diff --git a/js/parts/GIKMicroLegoPart.js b/js/parts/GIKPart.js
similarity index 74%
rename from js/parts/GIKMicroLegoPart.js
rename to js/parts/GIKPart.js
index 0098a4c6..6fb931e4 100644
--- a/js/parts/GIKMicroLegoPart.js
+++ b/js/parts/GIKPart.js
@@ -26,25 +26,18 @@
     DMAGIKPart.prototype = Object.create(DMAPart.prototype);
 
     DMAGIKPart.prototype._makeGikWireframe = function(positions, yPosition){
-        var geometry = new THREE.Geometry();
-        _.each(positions, function(position, index){
-            if (position == yPosition){
-                geometry.vertices.push(new THREE.Vector3(positions[index-1], yPosition, positions[index+1]));
-            }
-        });
-        console.log(geometry.vertices);
-        return new THREE.Line(geometry);
+//        var geometry = new THREE.Geometry();
+//        _.each(positions, function(position, index){
+//            if (position == yPosition){
+//                geometry.vertices.push(new THREE.Vector3(positions[index-1], yPosition, positions[index+1]));
+//            }
+//        });
+//        console.log(geometry.vertices);
+//        return new THREE.Line(geometry);
     };
 
-    DMAGIKPart.prototype._makeMeshForType = function(){
-        var mesh = new THREE.Mesh(unitPartGeo, this.parentCell.getMaterialType());
-
-
-
-        mesh.myPart = this;//need a ref back to this part
-        var wireframe = new THREE.EdgesHelper(mesh, 0x000000);
-        mesh.children.push(wireframe);
-        return mesh;
+    DMAGIKPart.prototype._getGeometry = function(){
+        return unitPartGeo;//this.parentCell.getMaterialType()
     };
 
     self.DMAGIKPart = DMAGIKPart;
@@ -73,10 +66,8 @@
     }
     DMAGIKPartLowPoly.prototype = Object.create(DMAGIKPart.prototype);
 
-    DMAGIKPartLowPoly.prototype._makeMeshForType = function(){
-        var mesh = new THREE.Mesh(unitPartGeo, this.parentCell.getMaterialType());
-        mesh.myPart = this;//need a ref back to this part
-        return mesh;
+    DMAGIKPartLowPoly.prototype._getGeometry = function(){
+        return unitPartGeo;
     };
 
     self.DMAGIKPartLowPoly = DMAGIKPartLowPoly;
@@ -105,10 +96,8 @@
     }
     DMAGIKEndPart.prototype = Object.create(DMAGIKPart.prototype);
 
-    DMAGIKEndPart.prototype._makeMeshForType = function(){
-        var mesh = new THREE.Mesh(unitPartGeo, this.parentCell.getMaterialType());
-        mesh.myPart = this;//need a ref back to this part
-        return mesh;
+    DMAGIKEndPart.prototype._getGeometry = function(){
+        return unitPartGeo;
     };
 
     self.DMAGIKEndPart = DMAGIKEndPart;
@@ -137,10 +126,8 @@
     }
     DMAGIKEndPartLowPoly.prototype = Object.create(DMAGIKPart.prototype);
 
-    DMAGIKEndPartLowPoly.prototype._makeMeshForType = function(){
-        var mesh = new THREE.Mesh(unitPartGeo, this.parentCell.getMaterialType());
-        mesh.myPart = this;//need a ref back to this part
-        return mesh;
+    DMAGIKEndPartLowPoly.prototype._getGeometry = function(){
+        return unitPartGeo;
     };
 
     self.DMAGIKEndPartLowPoly = DMAGIKEndPartLowPoly;
diff --git a/js/threeViews/Highlighter.js b/js/threeViews/Highlighter.js
index 6900c66f..76cba575 100644
--- a/js/threeViews/Highlighter.js
+++ b/js/threeViews/Highlighter.js
@@ -210,7 +210,7 @@ GIKHighlighter = Highlighter.extend({
     },
 
     _setPosition: function(position, direction){
-        this.mesh.position.set(position.x+direction.x, position.y+direction.y, position.z+globals.lattice.zScale(1)/2*direction.z);
+        this.mesh.position.set(position.x+direction.x, position.y+direction.y, position.z+globals.lattice.zScale()/2*direction.z);
     },
 
     _setRotation: function(direction, index){
-- 
GitLab