From 8d588df10191607b1454772bd5c8a24436f93494 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Tue, 3 Mar 2015 17:19:57 -0500
Subject: [PATCH] part vs inverse modes

---
 js/fea/DmaCell.js    | 28 ++++------------------------
 js/fea/DmaPart.js    |  9 +++++++--
 js/models/Lattice.js | 35 +++++++----------------------------
 3 files changed, 18 insertions(+), 54 deletions(-)

diff --git a/js/fea/DmaCell.js b/js/fea/DmaCell.js
index 16dfd171..fee67837 100644
--- a/js/fea/DmaCell.js
+++ b/js/fea/DmaCell.js
@@ -15,7 +15,7 @@ function DMACell(indices, scale, lattice, inverse) {
     this.parts = this._initParts(indices.z);
     this.updateForScale(scale);
 
-    this.drawForMode(dmaGlobals.appState.get("cellMode"));
+    this.drawForMode(dmaGlobals.appState.get("cellMode"), dmaGlobals.appState.get("inverseMode"));
 }
 
 DMACell.prototype.removePart = function(index){
@@ -35,33 +35,13 @@ DMACell.prototype._setMeshPosition = function(mesh, position){
     return mesh;
 };
 
-DMACell.prototype.drawForMode = function(mode){
-    if (mode == "cell"){
-        this._setCellMeshVisibility(true);
-        _.each(this.parts, function(part){
-            if (part) part.hide();
-        });
-    } else if (mode == "part"){
-        this._setCellMeshVisibility(false);
-        _.each(this.parts, function(part){
-            if (part) part.show();
-        });
-    } else {
-        console.warn("unrecognized draw mode for cell");
-    }
-};
-
-DMACell.prototype.hide = function(){
-    this._setCellMeshVisibility(false);
+DMACell.prototype.drawForMode = function(cellMode, inverseMode){
+    this._setCellMeshVisibility(cellMode == "cell" && inverseMode==this.isInverse);//only show if in the correct inverseMode
     _.each(this.parts, function(part){
-        if (part) part.hide();
+        if (part) part.setVisibility(cellMode == "part");
     });
 };
 
-DMACell.prototype.show = function(){
-    this._setCellMeshVisibility(true);
-};
-
 DMACell.prototype._setCellMeshVisibility = function(visibility){
     if (!this.cellMesh) return;
     this.cellMesh.visible = visibility;
diff --git a/js/fea/DmaPart.js b/js/fea/DmaPart.js
index aa09216c..ec4396fd 100644
--- a/js/fea/DmaPart.js
+++ b/js/fea/DmaPart.js
@@ -32,12 +32,17 @@
         mesh.position.z = position.z;
     };
 
-    DMAPart.prototype.show = function(){
+    DMAPart.prototype.setVisibility = function(visibility){
+        if (visibility) this._show();
+        else this._hide();
+    };
+
+    DMAPart.prototype._show = function(){
         if (!this.mesh) this._draw();
         this.mesh.visible = true;
     };
 
-    DMAPart.prototype.hide = function(){
+    DMAPart.prototype._hide = function(){
         if (this.mesh) this.mesh.visible = false;
     };
 
diff --git a/js/models/Lattice.js b/js/models/Lattice.js
index 983e8813..ba855ae1 100644
--- a/js/models/Lattice.js
+++ b/js/models/Lattice.js
@@ -36,8 +36,7 @@ Lattice = Backbone.Model.extend({
 
         //bind events
         this.listenTo(this, "change:scale", this._scaleDidChange);
-        this.listenTo(this, "change:inverseMode", this._showInverseCells);
-        this.listenTo(this, "change:cellMode", this._cellModeDidChange);
+        this.listenTo(this, "change:inverseMode change:cellMode", this._updateForMode);
         this.listenTo(this, "change:cellType change:connectionType", this._updateLatticeType);
 
     },
@@ -299,35 +298,14 @@ Lattice = Backbone.Model.extend({
     ////////////////////////////////////EVENTS//////////////////////////////////////////
     ////////////////////////////////////////////////////////////////////////////////////
 
-    _cellModeDidChange: function(){
-        var mode = this.get("cellMode");
-        var inverseMode = this.get("inverseMode");
-        if (mode == "part" || !inverseMode){
-            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(){
+    _updateForMode: function(){
+        var cellMode = this.get("cellMode");
         var inverseMode = this.get("inverseMode");
         this._iterCells(this.get("cells"), function(cell){
-            if (cell) {
-                if (inverseMode) cell.hide();
-                else cell.show();
-            }
+            if (cell) cell.drawForMode(cellMode, inverseMode);
         });
         this._iterCells(this.get("inverseCells"), function(cell){
-            if (cell) {
-                if (inverseMode) cell.show();
-                else cell.hide();
-            }
+            if (cell) cell.drawForMode(cellMode, inverseMode);
         });
         dmaGlobals.three.render();
     },
@@ -336,6 +314,8 @@ Lattice = Backbone.Model.extend({
         var scale = this.get("scale");
         this.get("basePlane").updateScale(scale);
         this.get("highlighter").updateScale(scale);
+
+        //only update visible cells
         this._iterCells(this.get("cells"), function(cell){
             if (cell) cell.updateForScale(scale);
         });
@@ -570,7 +550,6 @@ Lattice = Backbone.Model.extend({
                 var indexRel = self._subtract(invIndex, self.get("inverseCellsMin"));
                 if (!invCells[indexRel.x][indexRel.y][indexRel.z]) {
                     var cell = self._makeInvCellForLatticeType(invIndex, scale);
-                    cell.hide();
                     invCells[indexRel.x][indexRel.y][indexRel.z] = cell;
                     self.set("numInvCells", self.get("numInvCells")+1);
                 }
-- 
GitLab