From 131abb5e12728792c66d00661ece16695772d470 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Mon, 10 Aug 2015 16:06:50 -0400
Subject: [PATCH] e field matrix drawing

---
 js/main.js                               |  1 +
 js/simulation/electronics/LatticeEsim.js | 17 ++++++-
 js/simulation/electronics/eSimField.js   | 65 ++++++++++++++++++++++++
 3 files changed, 82 insertions(+), 1 deletion(-)
 create mode 100644 js/simulation/electronics/eSimField.js

diff --git a/js/main.js b/js/main.js
index 4b15527a..52f5492e 100644
--- a/js/main.js
+++ b/js/main.js
@@ -160,6 +160,7 @@ require.config({
         latticeESim: 'simulation/electronics/LatticeEsim',
         eSimCell: 'simulation/electronics/cells/eSimCell',
         eSimSuperCell: 'simulation/electronics/cells/eSimSuperCell',
+        eSimField: 'simulation/electronics/eSimField',
 
 
         //cam
diff --git a/js/simulation/electronics/LatticeEsim.js b/js/simulation/electronics/LatticeEsim.js
index d8f099b7..c2c58827 100644
--- a/js/simulation/electronics/LatticeEsim.js
+++ b/js/simulation/electronics/LatticeEsim.js
@@ -2,7 +2,8 @@
  * Created by aghassaei on 6/30/15.
  */
 
-define(['lattice', 'appState', 'threeModel', 'eSim', 'eSimCell', 'eSimSuperCell'], function(lattice, appState, three, eSim){
+define(['lattice', 'appState', 'three', 'threeModel', 'eSim', 'eSimCell', 'eSimSuperCell'],
+    function(lattice, appState, THREE, three, eSim){
 
 
 
@@ -70,6 +71,12 @@ define(['lattice', 'appState', 'threeModel', 'eSim', 'eSimCell', 'eSimSuperCell'
         },
 
         calcEField: function(conductorGroups, resolution){
+
+            if (this.numCells == 0){
+                console.warn("no cells!");
+                return;
+            }
+
             var eFieldMat = [];
             //init size of field mat and fill with zeros, +2 puts a shell of zeros at boundary (infinity)
             for (var x=0;x<resolution*this.cells.length+2;x++){
@@ -82,6 +89,7 @@ define(['lattice', 'appState', 'threeModel', 'eSim', 'eSimCell', 'eSimSuperCell'
                 }
             }
 
+            //input conductor potentials
             this._loopCells(this.cells, function(cell, x, y, z){
                 if (!cell) return;
                 for (var i=0;i<resolution;i++){
@@ -94,6 +102,13 @@ define(['lattice', 'appState', 'threeModel', 'eSim', 'eSimCell', 'eSimSuperCell'
             });
             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));
+            });
+
         },
 
         calcCapacitance: function(){
diff --git a/js/simulation/electronics/eSimField.js b/js/simulation/electronics/eSimField.js
new file mode 100644
index 00000000..7f70d5da
--- /dev/null
+++ b/js/simulation/electronics/eSimField.js
@@ -0,0 +1,65 @@
+/**
+ * Created by aghassaei on 8/10/15.
+ */
+
+
+//hold and display data for various fields
+
+define(['underscore', 'threeModel'], function(_, threeModel){
+
+    function ESimField(data, offset, resolution){
+
+        this._data = data;
+        this._offset = offset;
+
+        this._createThreeObjects(data, offset, 1/resolution);
+
+    }
+
+    ESimField.prototype._createThreeObjects = function(data, offset, size){
+        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);
+                }
+            }
+        }
+        threeModel.render();
+    };
+
+    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]);
+            }
+        }
+
+    };
+
+    ESimField.prototype.hide = function(){
+
+
+    };
+
+    ESimField.prototype._loopCells = 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++){
+                    callback(data[x][y][z], x, y, z, this);
+                }
+            }
+        }
+    };
+
+
+    return ESimField;
+
+});
\ No newline at end of file
-- 
GitLab