From 540b827053e9a82490bc898d2a08e117039e6d0f Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Mon, 10 Aug 2015 17:12:40 -0400
Subject: [PATCH] electric field view close

---
 js/menus/templates/EStaticMenuView.html  |  4 +-
 js/plists/ESimPlist.js                   |  6 +-
 js/simulation/electronics/LatticeEsim.js |  8 +--
 js/simulation/electronics/eSim.js        | 24 +++++++
 js/simulation/electronics/eSimField.js   | 87 ++++++++++++++++++------
 5 files changed, 99 insertions(+), 30 deletions(-)

diff --git a/js/menus/templates/EStaticMenuView.html b/js/menus/templates/EStaticMenuView.html
index 31a5565c..c6da2f99 100644
--- a/js/menus/templates/EStaticMenuView.html
+++ b/js/menus/templates/EStaticMenuView.html
@@ -47,7 +47,9 @@
             <%= simType%>
         </label>
     <% }); %>
-    Simulation Z Height: &nbsp;&nbsp;<input data-property="simZHeight" value="<%= simZHeight %>" placeholder="Height" class="form-control intInput eSim" type="text"><br/><br/>
+    <% if(visibleStaticSim != "none"){ %>
+        Simulation Z Height: &nbsp;&nbsp;<input data-property="simZHeight" value="<%= simZHeight %>" placeholder="Height" class="form-control intInput eSim" type="text"><br/><br/>
+    <% } %>
 <% } else { %>
     <div class="inlineWarning">No conductive groups detected, please navigate to previous tab and calculate.</div>
 <% } %>
\ No newline at end of file
diff --git a/js/plists/ESimPlist.js b/js/plists/ESimPlist.js
index 21909407..11d9af04 100644
--- a/js/plists/ESimPlist.js
+++ b/js/plists/ESimPlist.js
@@ -20,9 +20,9 @@ define([], function(){
 
         visibleStaticSimTypes: {
             none: "None",
-            eField: "Electric Field",
-            charge: "Charge Distribution",
-            capacitance: "Capacitance"
+            electricField: "Electric Field",
+            chargeField: "Charge Distribution",
+            capacitanceField: "Capacitance"
         }
     }
 
diff --git a/js/simulation/electronics/LatticeEsim.js b/js/simulation/electronics/LatticeEsim.js
index c2c58827..3eb10101 100644
--- a/js/simulation/electronics/LatticeEsim.js
+++ b/js/simulation/electronics/LatticeEsim.js
@@ -95,18 +95,16 @@ define(['lattice', 'appState', 'three', 'threeModel', 'eSim', 'eSimCell', 'eSimS
                 for (var i=0;i<resolution;i++){
                     for (var j=0;j<resolution;j++){
                         for (var k=0;k<resolution;k++){
-                            if (cell) eFieldMat[x+i+1][y+j+1][z+k+1] = 1;
+                            if (cell) eFieldMat[resolution*x+i+1][resolution*y+j+1][resolution*z+k+1] = 1;
                         }
                     }
                 }
             });
-            console.log(eFieldMat);
 
-            console.log(this.get("cellsMin"));
             var offset = this.get("cellsMin").clone().sub(new THREE.Vector3(1/(2*resolution)+this.xScale(0)/2, 1/(2*resolution)+this.yScale(0)/2, 1/(2*resolution)+this.zScale(0)/2));
-            console.log(offset);
             require(['eSimField'], function(ESimField){
-                eSim.set("electricField", new ESimField(eFieldMat, offset, resolution));
+                eSim.set("electricField", new ESimField(eFieldMat, offset, resolution, eSim.get("simZHeight")));
+                eSim.set("visibleStaticSim", "electricField");
             });
 
         },
diff --git a/js/simulation/electronics/eSim.js b/js/simulation/electronics/eSim.js
index e8ec9270..f6d086e7 100644
--- a/js/simulation/electronics/eSim.js
+++ b/js/simulation/electronics/eSim.js
@@ -25,6 +25,30 @@ define(['underscore', 'backbone'], function(_, Backbone){
             globalCapacitance: null,
             numRelaxationSteps: 5,
             visibleStaticSim: "none"//eField, charge, capacitance
+        },
+
+        initialize: function(){
+
+            this.listenTo(this, "change:simZHeight", this._refreshVisibleField);
+            this.listenTo(this, "change:visibleStaticSim", this._refreshVisibleField);
+
+        },
+
+        _hideAllFields: function(){
+            if (this.get("electricField")) this.get("electricField").hide();
+            if (this.get("chargeField")) this.get("chargeField").hide();
+            if (this.get("capacitanceField")) this.get("capacitanceField").hide();
+        },
+
+        _refreshVisibleField: function(){
+            this._hideAllFields();
+            var height = this.get("simZHeight");
+            var visibleSim = this.get("visibleStaticSim");
+            if (visibleSim == "none") {
+                console.warn("no visible simulation selected");
+                return;
+            }
+            this.get(visibleSim).show(height);
         }
 
 
diff --git a/js/simulation/electronics/eSimField.js b/js/simulation/electronics/eSimField.js
index 7f70d5da..80f7c760 100644
--- a/js/simulation/electronics/eSimField.js
+++ b/js/simulation/electronics/eSimField.js
@@ -7,49 +7,76 @@
 
 define(['underscore', 'threeModel'], function(_, threeModel){
 
-    function ESimField(data, offset, resolution){
+    function ESimField(data, offset, resolution, height){
 
+        this._object3D = new THREE.Object3D();
+        this._setData(data, offset, resolution, height);
+
+        threeModel.sceneAdd(this._object3D);
+        this.hide();
+    }
+
+    ESimField.prototype._setData = function(data, offset, resolution, height){
+        this._destroyData();
         this._data = data;
+        this._max = _.max(data);
+        this._min = _.min(data);
+        this._resolution = resolution;
+
         this._offset = offset;
+        this._setObject3DPosition(offset, resolution, height);
 
-        this._createThreeObjects(data, offset, 1/resolution);
+        this._threeObjects = this._createThreeObjects(data, offset, 1/resolution, height, this._object3D);
+    };
 
-    }
+    ESimField.prototype._setObject3DPosition = function(offset, resolution, height){
+        this._object3D.position.set(offset.x, offset.y, offset.z+height/resolution);
+    };
 
-    ESimField.prototype._createThreeObjects = function(data, offset, size){
+    ESimField.prototype._createThreeObjects = function(data, offset, size, height, object3D){
         var threeObjects = [];
         for (var x=0;x<data.length;x++){
             threeObjects.push([]);
             for (var y=0;y<data[0].length;y++){
-                threeObjects[x].push([]);
-                for (var z=0;z<data[0][0].length;z++){
-                    var box = new THREE.Mesh(new THREE.BoxGeometry(size, size, size), new THREE.MeshLambertMaterial({color:"#ff0000"}));
-                    box.position.set(x*size+offset.x, y*size+offset.y, z*size+offset.z);
-                    threeModel.sceneAdd(box);
-                    threeObjects[x][y].push(box);
-                }
+                var box = new THREE.Mesh(new THREE.BoxGeometry(size, size, size), this._materialForVal(data[x][y][height]));
+                box.position.set(x*size, y*size, 0);
+                object3D.add(box);
+                threeObjects[x].push(box);
             }
         }
-        threeModel.render();
+        return threeObjects;
+    };
+
+    ESimField.prototype._materialForVal = function(val){
+        return new THREE.MeshLambertMaterial({color:this._colorForVal(val)});
+    };
+
+    ESimField.prototype._colorForVal = function(val){
+        return new THREE.Color(val, 0, 0);
+        var scaledVal = (val - this._min)/(this._max - this._min) * (1-0)+ 0;
+        console.log(scaledVal);
+        console.log(this._min);
+        console.log(this._max);
     };
 
     ESimField.prototype.show = function(height){
-        var data = [];
-        for (var x=0;x<this._data.length;x++){
-            data.push([]);
-            for (var y=0;y<this._data[x].length;y++){
-                data[x].push(this._data[x][y][height]);
+        if (height){
+            for (var x=0;x<this._threeObjects.length;x++){
+                for (var y=0;y<this._threeObjects[x].length;y++){
+                    this._threeObjects[x][y].material.color = this._colorForVal(this._data[x][y][height]);
+                }
             }
         }
-
+        this._setObject3DPosition(this._offset, this._resolution, height);
+        this._object3D.visible = true;
+        threeModel.render();
     };
 
     ESimField.prototype.hide = function(){
-
-
+        this._object3D.visible = false;
     };
 
-    ESimField.prototype._loopCells = function(data, callback){
+    ESimField.prototype._loop = function(data, callback){
         for (var x=0;x<data.length;x++){
             for (var y=0;y<data[0].length;y++){
                 for (var z=0;z<data[0][0].length;z++){
@@ -59,6 +86,24 @@ define(['underscore', 'threeModel'], function(_, threeModel){
         }
     };
 
+    ESimField.prototype.destroy = function(){
+        this._destroyData();
+        threeModel.sceneRemove(this._object3D);
+        this._object3D = null;
+    };
+
+    ESimField.prototype._destroyData = function(){
+        if (this._data) this._loop(this._data, function(data){
+            data = null;
+        });
+        this._data = null;
+        this._min = null;
+        this._max = null;
+        this._offset = null;
+        this._resolution = null;
+
+    };
+
 
     return ESimField;
 
-- 
GitLab