From e507144f3d8f4deaa6c496ddb9e61f5b69d3db4a Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Fri, 15 May 2015 21:36:41 -0400
Subject: [PATCH] delay init parts

---
 js/fea/DmaCellOther.js    | 24 ++++++++++++---
 js/fea/DmaPart.js         | 62 +++++++++++++++++++++++++++++++++++----
 js/models/LatticeOther.js |  2 +-
 3 files changed, 78 insertions(+), 10 deletions(-)

diff --git a/js/fea/DmaCellOther.js b/js/fea/DmaCellOther.js
index b5a57326..123ceaaa 100644
--- a/js/fea/DmaCellOther.js
+++ b/js/fea/DmaCellOther.js
@@ -83,14 +83,30 @@ var cellMaterial = [new THREE.MeshNormalMaterial()];
     DMAGIKCell.prototype.setSuperCell = function(superCell, index){
         this.superCell = superCell;
         this.superCellIndex = index;
+        if (dmaGlobals.appState.get("cellMode")=="part") {
+            this.parts = this.__initParts();
+            this.draw();
+        }
     };
 
-    DMAGIKCell.prototype._initParts = function(){
+    DMAGIKCell.prototype.__initParts = function(){
         var parts  = [];
-        if (dmaGlobals.lattice.get("partType") == "lego") parts.push(new DMAGIKPart(0, this));
-        else parts.push(new DMAGIKPartLowPoly(0, this));
+        var isEnd = this.superCellIndex == 0 || this.superCellIndex == this.superCell.getLength()-1;
+        if (dmaGlobals.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));
+        }
         return parts;
-};
+    };
+
+    DMAGIKCell.prototype._initParts = function(){
+        if (this.superCell) return this.__initParts();
+        return [];
+    };
 
     self.DMAGIKCell = DMAGIKCell;
 
diff --git a/js/fea/DmaPart.js b/js/fea/DmaPart.js
index 2dc06eb6..ae66d74a 100644
--- a/js/fea/DmaPart.js
+++ b/js/fea/DmaPart.js
@@ -226,12 +226,12 @@ var partMaterial = new THREE.MeshLambertMaterial({ color:0xffffff, shading: THRE
         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/(unitPartGeo.boundingBox.max.x-unitPartGeo.boundingBox.min.x);
+        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, rotated){
+    function DMAGIKPart(type, parent){
         DMAPart.call(this, type, parent);
     }
     DMAGIKPart.prototype = Object.create(DMAPart.prototype);
@@ -258,16 +258,68 @@ var partMaterial = new THREE.MeshLambertMaterial({ color:0xffffff, shading: THRE
         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/(unitPartGeo.boundingBox.max.x-unitPartGeo.boundingBox.min.x);
+        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, rotated){
-        DMAGIKPart.call(this, type, parent, rotated);
+    function DMAGIKPartLowPoly(type, parent){
+        DMAGIKPart.call(this, type, parent);
     }
     DMAGIKPartLowPoly.prototype = Object.create(DMAGIKPart.prototype);
 
     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+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 DMAGIKEndPart(type, parent){
+        DMAGIKPart.call(this, type, parent);
+    }
+    DMAGIKEndPart.prototype = Object.create(DMAGIKPart.prototype);
+
+    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+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 DMAGIKEndPartLowPoly(type, parent){
+        DMAGIKPart.call(this, type, parent);
+    }
+    DMAGIKEndPartLowPoly.prototype = Object.create(DMAGIKPart.prototype);
+
+    self.DMAGIKEndPartLowPoly = DMAGIKEndPartLowPoly;
+
 })();
\ No newline at end of file
diff --git a/js/models/LatticeOther.js b/js/models/LatticeOther.js
index b8197b78..0cbc382d 100644
--- a/js/models/LatticeOther.js
+++ b/js/models/LatticeOther.js
@@ -92,7 +92,7 @@ OtherLatticeSubclasses = {
             var cells = this.addCellsInRange(range);
             var superCell = new DMASuperCell(length, range, cells);
             _.each(cells, function(cell, index){
-                cell.setSuperCell(superCell, index, length);
+                cell.setSuperCell(superCell, index);
             });
         },
 
-- 
GitLab