From 48f23f1d4c5a9e4279cc613cb62bca1a3bbc4c17 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Wed, 27 May 2015 12:27:23 -0700
Subject: [PATCH] reorganizing

---
 index.html                          |  1 -
 js/cells/DMACell.js                 | 66 ++++++++++++++++++++++++-----
 js/cells/DMAParentCell.js           | 62 ---------------------------
 js/cells/GIKCell.js                 |  1 +
 js/cells/supercells/GIKSuperCell.js | 29 ++++---------
 js/lattice/GIKLattice.js            |  7 +--
 6 files changed, 65 insertions(+), 101 deletions(-)
 delete mode 100644 js/cells/DMAParentCell.js

diff --git a/index.html b/index.html
index d2d11aef..ac6c6f47 100644
--- a/index.html
+++ b/index.html
@@ -44,7 +44,6 @@
     <script src="js/fea/DmaNode.js"></script>
     <script src="js/fea/DmaBeam.js"></script>
 
-    <script src="js/cells/DMAParentCell.js"></script>
     <script src="js/cells/DmaCell.js"></script>
     <script src="js/cells/OctaFaceCell.js"></script>
     <script src="js/cells/OctaEdgeCell.js"></script>
diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js
index 6f1ba1cb..7adc5cba 100644
--- a/js/cells/DMACell.js
+++ b/js/cells/DMACell.js
@@ -13,16 +13,10 @@ function DMACell(indices){
     //object 3d is parent to all 3d elements related to cell, parts, beams, nodes, etc
     this.object3D = this._buildObject3D();
     this._addChildren(this._buildMesh(), this.object3D);//build cell meshes
-    if (!this.superCell) globals.three.sceneAdd(this.object3D, this._getSceneType());
+    if (!this.superCell && this.indices) globals.three.sceneAdd(this.object3D, "cell");
 
     this.setMode();
 }
-DMACell.prototype = Object.create(DMAParentCell.prototype);
-
-DMACell.prototype._getSceneType = function(){//todo need this?
-    if (this.indices) return "cell";
-    return null;
-};
 
 DMACell.prototype._buildObject3D = function(){
     var object3D = this._translateCell(this._rotateCell(new THREE.Object3D()));
@@ -31,9 +25,15 @@ DMACell.prototype._buildObject3D = function(){
     return object3D;
 };
 
-DMACell.prototype.getObject3D = function(){//only called by supercells
-    return this.object3D;
-}
+DMACell.prototype._rotateCell = function(object3D){
+    return object3D;//by default, no mesh transformations
+};
+
+DMACell.prototype._translateCell = function(object3D){
+    var position = globals.lattice.getPositionForIndex(this.indices);
+    object3D.position.set(position.x, position.y, position.z);
+    return object3D;
+};
 
 DMACell.prototype._buildMesh = function(){
     var geometry = this._getGeometry();
@@ -52,6 +52,37 @@ DMACell.prototype._buildWireframe = function(mesh, geometry){//abstract mesh rep
     return new THREE.Mesh(geometry, wireframeMaterial);
 };
 
+DMACell.prototype._addChildren = function(children, object3D){//accepts an array or a single mesh
+    this._addRemoveChildren(true, children, object3D);
+};
+
+DMACell.prototype._removeChildren = function(children, object3D){//accepts an array or a single mesh
+    this._addRemoveMeshes(false, children, object3D);
+};
+
+DMACell.prototype._addRemoveChildren = function(shouldAdd, children, object3D){//accepts an array or a single mesh
+    if (object3D === undefined) object3D = this.object3D;
+    if (children.constructor === Array){
+        _.each(children, function(child){
+            if (shouldAdd) object3D.add(child);
+            else object3D.remove(child);
+        });
+    } else if (shouldAdd) object3D.add(children);
+    else object3D.remove(children);
+};
+
+DMACell.prototype.hide = function(){
+    this.object3D.visible = false;
+};
+
+DMACell.prototype.show = function(mode){
+    this.object3D.visible = true;
+    this.setMode(mode);
+};
+
+DMACell.prototype.setOpacity = function(opacity){
+};
+
 DMACell.prototype._initParts = function(){
     return [];//override in subclasses
 };
@@ -96,6 +127,18 @@ DMACell.prototype.setMode = function(mode){
     });
 };
 
+DMACell.prototype.getPosition = function(){
+    return this.object3D.position.clone();
+};
+
+DMACell.prototype.getQuaternion = function(){
+    return this.object3D.quaternion.clone();
+};
+
+DMACell.prototype.getEuler = function(){
+    return this.object3D.rotation.clone();
+};
+
 DMACell.prototype.xScale = function(){
     return globals.lattice.xScale(0);
 };
@@ -112,7 +155,8 @@ DMACell.prototype.destroy = function(){
     if (this.destroyStarted) return;
     this.destroyStarted = true;
     if (this.object3D) {
-        globals.three.sceneRemove(this.object3D, this._getSceneType());
+        if (this.superCell) this.object3D.parent.remove(this.object3D);
+        else if (this.indices) globals.three.sceneRemove(this.object3D, "cell");
         this.object3D.myParent = null;
 //            this.object3D.dispose();
 //            geometry.dispose();
diff --git a/js/cells/DMAParentCell.js b/js/cells/DMAParentCell.js
deleted file mode 100644
index 21ac7195..00000000
--- a/js/cells/DMAParentCell.js
+++ /dev/null
@@ -1,62 +0,0 @@
-/**
- * Created by aghassaei on 5/27/15.
- */
-
-
-//common methods between cell and supercell classes
-
-function DMAParentCell(){
-}
-
-DMAParentCell.prototype._rotateCell = function(object3D){
-    return object3D;//by default, no mesh transformations
-};
-
-DMAParentCell.prototype._translateCell = function(object3D){
-    var position = globals.lattice.getPositionForIndex(this.indices);
-    object3D.position.set(position.x, position.y, position.z);
-    return object3D;
-};
-
-DMAParentCell.prototype._addChildren = function(children, object3D){//accepts an array or a single mesh
-    this._addRemoveChildren(true, children, object3D);
-};
-
-DMAParentCell.prototype._removeChildren = function(children, object3D){//accepts an array or a single mesh
-    this._addRemoveMeshes(false, children, object3D);
-};
-
-DMAParentCell.prototype._addRemoveChildren = function(shouldAdd, children, object3D){//accepts an array or a single mesh
-    if (object3D === undefined) object3D = this.object3D;
-    if (children.constructor === Array){
-        _.each(children, function(child){
-            if (shouldAdd) object3D.add(child);
-            else object3D.remove(child);
-        });
-    } else if (shouldAdd) object3D.add(children);
-    else object3D.remove(children);
-};
-
-DMAParentCell.prototype.hide = function(){
-    this.object3D.visible = false;
-};
-
-DMAParentCell.prototype.show = function(mode){
-    this.object3D.visible = true;
-    this.setMode(mode);
-};
-
-DMAParentCell.prototype.setOpacity = function(opacity){
-};
-
-DMAParentCell.prototype.getPosition = function(){
-    return this.object3D.position.clone();
-};
-
-DMAParentCell.prototype.getQuaternion = function(){
-    return this.object3D.quaternion.clone();
-};
-
-DMAParentCell.prototype.getEuler = function(){
-    return this.object3D.rotation.clone();
-};
\ No newline at end of file
diff --git a/js/cells/GIKCell.js b/js/cells/GIKCell.js
index 1e7ea8c0..7aacaa64 100644
--- a/js/cells/GIKCell.js
+++ b/js/cells/GIKCell.js
@@ -15,6 +15,7 @@
         this.superCellIndex = index;
         CubeCell.call(this, this.indices);
         if (this.superCellIndex == this.superCell.getLength()) this.object3D.rotateZ(Math.PI);
+        return this.object3D;
     };
 
     GIKCell.prototype._translateCell = function(object3D){
diff --git a/js/cells/supercells/GIKSuperCell.js b/js/cells/supercells/GIKSuperCell.js
index 59d8e090..dda8c5f2 100644
--- a/js/cells/supercells/GIKSuperCell.js
+++ b/js/cells/supercells/GIKSuperCell.js
@@ -13,35 +13,22 @@ GIKSuperCell = function(length, range, cells){
 
     this.object3D = this._buildObject3D();
     this._addChildren(this._buildMesh(length), this.object3D);
-
-    globals.three.sceneAdd(this.object3D, this._getSceneType());
+    var self = this;
+    _.each(cells, function(cell, index){
+        self.addObject3D(cell.setSuperCell(self, index));
+    });
+    if (this.indices) globals.three.sceneAdd(this.object3D, "supercell");
 
 //    this.setMode();
-
-};
-GIKSuperCell.prototype = Object.create(DMAParentCell.prototype);
-
-GIKSuperCell.prototype._getSceneType = function(){//todo need this?
-    if (this.indices) return "supercell";
-    return null;
 };
+GIKSuperCell.prototype = Object.create(DMACell.prototype);
 
 GIKSuperCell.prototype._buildObject3D = function(){
     return this._translateCell(this._rotateCell(new THREE.Object3D()));
 };
 
-GIKSuperCell.prototype.addCellsToScene = function(){
-    this._addChildren(this._getCellObject3Ds(this.cells));
-};
-
-GIKSuperCell.prototype._getCellObject3Ds = function(cells){
-    var object3Ds = [];
-    var self = this;
-    _.each(cells, function(cell){
-        var object3D = cell.getObject3D();
-        object3Ds.push(object3D);
-    });
-    return object3Ds;
+GIKSuperCell.prototype.addObject3D = function(object3D){
+    this._addChildren(object3D);
 };
 
 GIKSuperCell.prototype._rotateCell = function(object3D){
diff --git a/js/lattice/GIKLattice.js b/js/lattice/GIKLattice.js
index d211fc4a..1cc3e3d8 100644
--- a/js/lattice/GIKLattice.js
+++ b/js/lattice/GIKLattice.js
@@ -48,12 +48,7 @@ latticeSubclasses["GIKLattice"] = {
                 }
             }
             if (cells.length < 1) return null;
-            var superCell = new GIKSuperCell(length, range, cells);
-            _.each(cells, function(cell, index){
-                cell.setSuperCell(superCell, index);
-            });
-            superCell.addCellsToScene();
-            return superCell;
+            return new GIKSuperCell(length, range, cells);
         },
 
         _rasterGikCells: function(order, callback, var1, var2, var3, cells){
-- 
GitLab