From fa761c315c0572928385591292839989c8487bb7 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Mon, 8 Jun 2015 21:27:17 -0700
Subject: [PATCH] gik coming back online

---
 js/cells/DMACell.js                 |  12 +--
 js/cells/GIKCell.js                 |  46 ++++-----
 js/cells/supercells/DMASuperCell.js |  13 ++-
 js/cells/supercells/GIKSuperCell.js |   3 +-
 js/main.js                          |   8 +-
 js/parts/GIKPart.js                 | 144 +++++-----------------------
 js/parts/GIKPartLowPoly.js          |  33 +++++++
 7 files changed, 99 insertions(+), 160 deletions(-)
 create mode 100644 js/parts/GIKPartLowPoly.js

diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js
index 5f53be34..a84ae82e 100644
--- a/js/cells/DMACell.js
+++ b/js/cells/DMACell.js
@@ -65,20 +65,16 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals'],
 
         var meshes = [];
         var mesh = new THREE.Mesh(geometry, this.getMaterial());
-        mesh.name = this._getMeshName();
+        mesh.name = "cell";
         meshes.push(mesh);
 
         var wireframe = this._buildWireframe(mesh, geometry);
         if (!wireframe) return meshes;
-        wireframe.name = this._getMeshName();
+        wireframe.name = "cell";
         meshes.push(wireframe);
         return meshes;
     };
 
-    DMACell.prototype._getMeshName = function(){
-        return "cell";
-    };
-
     DMACell.prototype._buildWireframe = function(mesh, geometry){//for "cell" view
         return new THREE.Mesh(geometry, wireframeMaterial);
     };
@@ -238,7 +234,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals'],
                 setVisiblity();
                 break;
             case "part":
-                if (!this.parts) {
+                if (!this.cells && !this.parts) {
                     this._initParts(function(parts){
                         self.parts = parts;
                         setVisiblity();
@@ -253,6 +249,8 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals'],
 //                if (!this.nodes) this.nodes = this._initNodes();
                 setVisiblity();
                 break;
+            default:
+                break;
         }
 
         function setVisiblity(){
diff --git a/js/cells/GIKCell.js b/js/cells/GIKCell.js
index 3966517d..8ce29634 100644
--- a/js/cells/GIKCell.js
+++ b/js/cells/GIKCell.js
@@ -6,18 +6,11 @@
 define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cubeCell'],
     function(_, THREE, three, lattice, appState, CubeCell){
 
-    var unitGeo = new THREE.BoxGeometry(1,1,1.28);
-
     function GIKCell(index, superCell){
         CubeCell.call(this, index, superCell);
     }
     GIKCell.prototype = Object.create(CubeCell.prototype);
 
-
-    GIKCell.prototype._getGeometry = function(){
-        return unitGeo;
-    };
-
     GIKCell.prototype._translateCell = function(object3D){
         if (this.index) {
             var offset = this.index.x-this.superCell.getLength();
@@ -26,30 +19,27 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cubeCell'],
         return object3D;
     };
 
-    GIKCell.prototype._rotateCell = function(object3D){
-        var length = this.superCell.getLength();
-        if (this.index.x == length) object3D.rotateZ(Math.PI);
-        return object3D;
-    };
-
-    GIKCell.prototype.getMaterial = function(){
-        return this.superCell.getMaterial();
-    };
-
     GIKCell.prototype._initParts = function(){
-        if (!this.superCell) return null;
+        if (!this.superCell) return;
+        var self = this;
         var parts  = [];
-        var isEnd = false;
-//        var isEnd = this.superCellIndex == 0 || this.superCellIndex == this.superCell.getLength();
-        if (globals.lattice.get("partType") == "lego") {
-            if (isEnd) parts.push(new DMAGIKEndPart(0, this));
-            else parts.push(new DMAGIKPart(0, this));
-        }
-        else {
-            if (isEnd) parts.push(new DMAGIKEndPartLowPoly(0, this));
-            else parts.push(new DMAGIKPartLowPoly(0, this));
+        var length = this.superCell.getLength()+1;
+
+        if (lattice.get("partType") == "lego") {
+            require(['gikPart'], function(GIKPart){
+                for (var i=0;i<length;i++){
+                    parts.push(new GIKPart(i, self));
+                }
+                self.parts = parts;
+            });
+        } else {
+            require(['gikPartLowPoly'], function(GIKPartLowPoly){
+                for (var i=0;i<length;i++){
+                    parts.push(new GIKPartLowPoly(i, self));
+                }
+                self.parts = parts;
+            });
         }
-        return parts;
     };
 
     GIKCell.prototype.calcHighlighterPosition = function(face){
diff --git a/js/cells/supercells/DMASuperCell.js b/js/cells/supercells/DMASuperCell.js
index 42fdec52..b245ce3e 100644
--- a/js/cells/supercells/DMASuperCell.js
+++ b/js/cells/supercells/DMASuperCell.js
@@ -21,11 +21,13 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'],
     DMASuperCell.prototype._makeChildCells = function(range, material){
         var cells = [];
         for (var x=0;x<range.x;x++){
+            cells.push([]);
             for (var y=0;y<range.y;y++){
+                cells[x].push([]);
                 for (var z=0;z<range.z;z++){
                     //child cells add themselves to object3D
-                    if (material.cells) var cellMaterial = material.cells[x][y][z].material;
-                    if (cells[x][y][z]) cells.push(this._makeSubCellForIndex(new THREE.Vector3(x, y, z), cellMaterial || material));
+                    if (material.cells && material.cells[x][y][z]) cells[x][y].push(this._makeSubCellForIndex(new THREE.Vector3(x, y, z), this, material.cells[x][y][z].material));
+                    else cells[x][y].push(this._makeSubCellForIndex(new THREE.Vector3(x, y, z), this, material));
                 }
             }
         }
@@ -37,9 +39,9 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'],
     };
 
     DMASuperCell.prototype.setMode = function(mode){
-        DMACell.prototype.setMode.call(mode);
-        _.each(this.cells, function(cell){
-            cell.setMode(mode);
+        DMACell.prototype.setMode.call(this, mode);
+        this._loopCells(function(cell){
+            if (cell) cell.setMode(mode);
         });
     };
 
@@ -54,6 +56,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'],
 
     DMASuperCell.prototype._loopCells = function(callback){
         var cells = this.cells;
+        if (!cells || cells === undefined) return;
         for (var x=0;x<cells.length;x++){
             for (var y=0;y<cells[0].length;y++){
                 for (var z=0;z<cells[0][0].length;z++){
diff --git a/js/cells/supercells/GIKSuperCell.js b/js/cells/supercells/GIKSuperCell.js
index 4a25a5a5..818a6aa1 100644
--- a/js/cells/supercells/GIKSuperCell.js
+++ b/js/cells/supercells/GIKSuperCell.js
@@ -23,11 +23,12 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'superCell',
         return object3D;
     };
 
-    GIKSuperCell.prototype._getGeometry = function(){
+    GIKSuperCell.prototype._getGeometry = function(){//todo , do this to mesh?
         var geo = unitGeo.clone();
         var length = this.getLength() + 1;
         geo.applyMatrix(new THREE.Matrix4().makeScale(length, 1, 1));
         geo.applyMatrix(new THREE.Matrix4().makeTranslation(-length/2+0.5, 0, 0));
+        return geo;
     };
 
     GIKSuperCell.prototype._buildWireframe = function(mesh){
diff --git a/js/main.js b/js/main.js
index 05d40afc..dd462c28 100644
--- a/js/main.js
+++ b/js/main.js
@@ -73,6 +73,8 @@ require.config({
         octaFaceTriPart: 'parts/OctaFaceTriPart',
         octaEdgeVoxPart: 'parts/OctaEdgeVoxPart',
         octaEdgeVoxPartLowPoly: 'parts/OctaEdgeVoxPartLowPoly',
+        gikPart: 'parts/GIKPart',
+        gikPartLowPoly: 'parts/GIKPartLowPoly',
 
         //materials
         materials: 'materials/DMAMaterials',
@@ -100,7 +102,11 @@ require.config({
         //stls
         octaFaceTrianglePartSTL: '../assets/stls/parts/OctaFaceTrianglePart.stl',
         octaEdgeVoxPartSTL: '../assets/stls/parts/OctaEdgeVoxPart.stl',
-        octaEdgeVoxPartLowPolySTL: '../assets/stls/parts/OctaEdgeVoxPartLowPoly.stl'
+        octaEdgeVoxPartLowPolySTL: '../assets/stls/parts/OctaEdgeVoxPartLowPoly.stl',
+        gikPartSTL: '../assets/stls/parts/GIKPart.stl',
+        gikEndPartSTL: '../assets/stls/parts/GIKEndPart.stl',
+        gikPartLowPolySTL: '../assets/stls/parts/GIKPartLowPoly.stl',
+        gikEndPartLowPolySTL: '../assets/stls/parts/GIKEndPartLowPoly.stl'
 
     },
 
diff --git a/js/parts/GIKPart.js b/js/parts/GIKPart.js
index 53a56741..a1291d00 100644
--- a/js/parts/GIKPart.js
+++ b/js/parts/GIKPart.js
@@ -3,137 +3,45 @@
  */
 
 
-(function () {
+define(['underscore', 'three', 'part', 'bin!gikPartSTL', 'bin!gikEndPartSTL', 'stlLoader'], function(_, THREE, DMAPart, gikPart, gikEndPart){
 
-    var unitPartGeo;
-
-    //import part geometry
     var loader = new THREE.STLLoader();
-    loader.load("assets/stls/parts/GIKPart.stl", function(geometry){
+    var unitGeo = preProcessGeo(loader.parse(gikPart));
+    var unitGeoEnd = preProcessGeo(loader.parse(gikEndPart));
 
-        unitPartGeo = geometry;
-        unitPartGeo.computeBoundingBox();
-        unitPartGeo.applyMatrix(new THREE.Matrix4().makeTranslation(-(unitPartGeo.boundingBox.min.x+unitPartGeo.boundingBox.max.x)/2,
-            -(unitPartGeo.boundingBox.min.y+unitPartGeo.boundingBox.max.y)/2, -(unitPartGeo.boundingBox.min.z+unitPartGeo.boundingBox.max.z)/2));
+    function preProcessGeo(geo){
+        geo.computeBoundingBox();
+        geo.applyMatrix(new THREE.Matrix4().makeTranslation(-(geo.boundingBox.min.x+0.5),
+            -(geo.boundingBox.min.y+geo.boundingBox.max.y)/2, -(geo.boundingBox.min.z+geo.boundingBox.max.z)/2));
         var unitScale = 1/(1.2699999809265137);
-        unitPartGeo.applyMatrix(new THREE.Matrix4().makeRotationX(Math.PI/2));
-        unitPartGeo.applyMatrix(new THREE.Matrix4().makeScale(unitScale, unitScale, unitScale));
-    });
-
-    function DMAGIKPart(type, parent){
-        DMAPart.call(this, type, parent);
+        geo.applyMatrix(new THREE.Matrix4().makeRotationX(Math.PI/2));
+        geo.applyMatrix(new THREE.Matrix4().makeScale(unitScale, unitScale, unitScale));
+        return geo;
     }
-    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);
-    };
-
-    DMAGIKPart.prototype._getGeometry = function(){
-        return unitPartGeo;//this.parentCell.getMaterialType()
-    };
-
-    DMAGIKPart.prototype.getMaterial = function(){
-        return this.parentCell.getMaterial();
-    };
-
-    self.DMAGIKPart = DMAGIKPart;
 
-})();
-
-(function () {
-
-    var unitPartGeo;
-
-    //import part geometry
-    var loader = new THREE.STLLoader();
-    loader.load("assets/stls/parts/GIKPartLowPoly.stl", function(geometry){
-
-        unitPartGeo = geometry;
-        unitPartGeo.computeBoundingBox();
-        unitPartGeo.applyMatrix(new THREE.Matrix4().makeTranslation(-(unitPartGeo.boundingBox.min.x+unitPartGeo.boundingBox.max.x)/2,
-            -(unitPartGeo.boundingBox.min.y+unitPartGeo.boundingBox.max.y)/2, -(unitPartGeo.boundingBox.min.z+unitPartGeo.boundingBox.max.z)/2));
-        var unitScale = 1/(1.2699999809265137);
-        unitPartGeo.applyMatrix(new THREE.Matrix4().makeRotationX(Math.PI/2));
-        unitPartGeo.applyMatrix(new THREE.Matrix4().makeScale(unitScale, unitScale, unitScale));
-    });
-
-    function DMAGIKPartLowPoly(type, parent){
-        DMAGIKPart.call(this, type, parent);
+    function GIKPart(index, parent){
+        DMAPart.call(this, index, parent);
     }
-    DMAGIKPartLowPoly.prototype = Object.create(DMAGIKPart.prototype);
+    GIKPart.prototype = Object.create(DMAPart.prototype);
 
-    DMAGIKPartLowPoly.prototype._getGeometry = function(){
-        return unitPartGeo;
+    GIKPart.prototype._getGeometry = function(){
+        if (this._isEnd()) return unitGeoEnd;
+        return unitGeo;
     };
 
-    self.DMAGIKPartLowPoly = DMAGIKPartLowPoly;
-
-})();
-
-(function () {
-
-    var unitPartGeo;
-
-    //import part geometry
-    var loader = new THREE.STLLoader();
-    loader.load("assets/stls/parts/GIKEndPart.stl", function(geometry){
-
-        unitPartGeo = geometry;
-        unitPartGeo.computeBoundingBox();
-        unitPartGeo.applyMatrix(new THREE.Matrix4().makeTranslation(-(unitPartGeo.boundingBox.min.x+0.5),
-            -(unitPartGeo.boundingBox.min.y+unitPartGeo.boundingBox.max.y)/2, -(unitPartGeo.boundingBox.min.z+unitPartGeo.boundingBox.max.z)/2));
-        var unitScale = 1/(1.2699999809265137);
-        unitPartGeo.applyMatrix(new THREE.Matrix4().makeRotationX(Math.PI/2));
-        unitPartGeo.applyMatrix(new THREE.Matrix4().makeScale(unitScale, unitScale, unitScale));
-    });
-
-    function DMAGIKEndPart(type, parent){
-        DMAGIKPart.call(this, type, parent);
-    }
-    DMAGIKEndPart.prototype = Object.create(DMAGIKPart.prototype);
-
-    DMAGIKEndPart.prototype._getGeometry = function(){
-        return unitPartGeo;
+    GIKPart.prototype._rotatePart = function(mesh){
+        if (this._getGIKLength() == this.index) mesh.rotateZ(Math.PI);
+        return mesh;
     };
 
-    self.DMAGIKEndPart = DMAGIKEndPart;
-
-})();
-
-(function () {
-
-    var unitPartGeo;
-
-    //import part geometry
-    var loader = new THREE.STLLoader();
-    loader.load("assets/stls/parts/GIKEndPartLowPoly.stl", function(geometry){
-
-        unitPartGeo = geometry;
-        unitPartGeo.computeBoundingBox();
-        unitPartGeo.applyMatrix(new THREE.Matrix4().makeTranslation(-(unitPartGeo.boundingBox.min.x+0.5),
-            -(unitPartGeo.boundingBox.min.y+unitPartGeo.boundingBox.max.y)/2, -(unitPartGeo.boundingBox.min.z+unitPartGeo.boundingBox.max.z)/2));
-        var unitScale = 1/(1.2699999809265137);
-        unitPartGeo.applyMatrix(new THREE.Matrix4().makeRotationX(Math.PI/2));
-        unitPartGeo.applyMatrix(new THREE.Matrix4().makeScale(unitScale, unitScale, unitScale));
-    });
-
-    function DMAGIKEndPartLowPoly(type, parent){
-        DMAGIKPart.call(this, type, parent);
-    }
-    DMAGIKEndPartLowPoly.prototype = Object.create(DMAGIKPart.prototype);
+    GIKPart.prototype._isEnd = function(){
+        return this.index == 0 || this.index == this._getGIKLength();
+    };
 
-    DMAGIKEndPartLowPoly.prototype._getGeometry = function(){
-        return unitPartGeo;
+    GIKPart.prototype._getGIKLength = function(){
+        return this.parentCell.superCell.getLength();
     };
 
-    self.DMAGIKEndPartLowPoly = DMAGIKEndPartLowPoly;
+    return GIKPart;
 
-})();
\ No newline at end of file
+});
\ No newline at end of file
diff --git a/js/parts/GIKPartLowPoly.js b/js/parts/GIKPartLowPoly.js
new file mode 100644
index 00000000..e071eb12
--- /dev/null
+++ b/js/parts/GIKPartLowPoly.js
@@ -0,0 +1,33 @@
+/**
+ * Created by aghassaei on 6/8/15.
+ */
+
+
+
+define(['underscore', 'three', 'gikPart', 'bin!gikPartLowPolySTL', 'bin!gikEndPartLowPolySTL', 'stlLoader'], function(_, THREE, GIKPart, gikPartLowPoly, gikEndPartLowPoly){
+
+    var loader = new THREE.STLLoader();
+    var unitGeo = preProcessGeo(loader.parse(gikPartLowPoly));
+    var unitGeoEnd = preProcessGeo(loader.parse(gikEndPartLowPoly));
+
+    function preProcessGeo(geo){
+        geo.applyMatrix(new THREE.Matrix4().makeTranslation(-(geo.boundingBox.min.x+0.5),
+            -(geo.boundingBox.min.y+geo.boundingBox.max.y)/2, -(geo.boundingBox.min.z+geo.boundingBox.max.z)/2));
+        var unitScale = 1/(1.2699999809265137);
+        geo.applyMatrix(new THREE.Matrix4().makeRotationX(Math.PI/2));
+        geo.applyMatrix(new THREE.Matrix4().makeScale(unitScale, unitScale, unitScale));
+        return geo;
+    }
+
+    function GIKPartLowPoly(index, parent){
+        GIKPart.call(this, index, parent);
+    }
+    GIKPartLowPoly.prototype = Object.create(GIKPart.prototype);
+
+    GIKPartLowPoly.prototype._getGeometry = function(){
+        if (this.index == 0 || this.index == this.superCell.getLength()) return unitGeoEnd;
+        return unitGeo;
+    };
+
+    return GIKPartLowPoly;
+});
\ No newline at end of file
-- 
GitLab