From 2a24fbe94df0765985d018eec9fb3fea58da2679 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Tue, 30 Jun 2015 19:03:35 -0700
Subject: [PATCH] first pass calc connected componentes

---
 js/lattice/Lattice.js                  |  2 ++
 js/lattice/LatticeEsim.js              | 46 ++++++++++++++++++++++++++
 js/main.js                             |  6 ++++
 js/menus/ESetupMenuView.js             | 18 ++++++++--
 js/menus/templates/ESetupMenuView.html | 12 ++++++-
 js/simulation/electronics/eCell.js     |  3 --
 js/simulation/electronics/eSim.js      | 15 +++++++++
 js/simulation/electronics/eSimCell.js  | 27 +++++++++++++++
 8 files changed, 123 insertions(+), 6 deletions(-)
 create mode 100644 js/lattice/LatticeEsim.js
 delete mode 100644 js/simulation/electronics/eCell.js
 create mode 100644 js/simulation/electronics/eSimCell.js

diff --git a/js/lattice/Lattice.js b/js/lattice/Lattice.js
index c19814c0..a8daf390 100644
--- a/js/lattice/Lattice.js
+++ b/js/lattice/Lattice.js
@@ -131,6 +131,8 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
 
         _parseSparseCell: function(){
 
+            console.log("parse cells");
+
             if (this.get("numCells") == 0) {
                 console.warn("no cells in assembly");
                 this.cells = [[[null]]];
diff --git a/js/lattice/LatticeEsim.js b/js/lattice/LatticeEsim.js
new file mode 100644
index 00000000..66ed1caa
--- /dev/null
+++ b/js/lattice/LatticeEsim.js
@@ -0,0 +1,46 @@
+/**
+ * Created by aghassaei on 6/30/15.
+ */
+
+define(['lattice', 'eSim', 'eSimCell'], function(lattice, eSim){
+
+    var eSimMethods = {
+
+        calculateConnectivity: function(){
+            var num = 1;
+            this._loopCells(this.cells, function(cell){
+                if (cell) cell.setConnectivityGroupNum(num++, true);
+            });
+            this._loopCells(this.cells, function(cell){
+                if (cell) cell.propagateConnectivityGroupNum();
+            });
+            this._calcNumberConnectedComponents();
+        },
+
+        _calcNumberConnectedComponents: function(){
+            var groups = [];
+            this._loopCells(this.cells, function(cell){
+                if (!cell) return;
+                if (groups.indexOf(cell.getConnectivityGroupNum())<0) groups.push(cell.getConnectivityGroupNum());
+            });
+            eSim.set("numConnectedComponents", groups.length);
+        },
+
+        propagateToNeighbors: function(index, callback){
+            index.sub(this.get("cellsMin"));//todo wrong
+            var xLength = this.cells.length;
+            var yLength = this.cells[0].length;
+            var zLength = this.cells[0][0].length;
+            if (index.x+1 < xLength) callback(this.cells[index.x+1][index.y][index.z]);
+            if (index.x-1 >= 0) callback(this.cells[index.x-1][index.y][index.z]);
+            if (index.y+1 < yLength) callback(this.cells[index.x][index.y+1][index.z]);
+            if (index.y-1 >= 0) callback(this.cells[index.x][index.y-1][index.z]);
+            if (index.z+1 < zLength) callback(this.cells[index.x][index.y][index.z+1]);
+            if (index.z-1 >= 0) callback(this.cells[index.x][index.y][index.z-1]);
+        }
+
+    };
+
+    _.extend(lattice, eSimMethods);
+    return lattice;
+});
diff --git a/js/main.js b/js/main.js
index 7dedc7c1..a88f8bf1 100644
--- a/js/main.js
+++ b/js/main.js
@@ -151,6 +151,12 @@ require.config({
         mStaticMenuTemplate: 'menus/templates/MStaticMenuView.html',
         mDynamicMenuTemplate: 'menus/templates/MDynamicMenuView.html',
 
+        //electronic sim
+        eSim: 'simulation/electronics/eSim',
+        latticeESim: 'lattice/latticeESim',
+        eSimCell: 'simulation/electronics/eSimCell',
+
+
         //cam
         cam: 'cam/cam',
 
diff --git a/js/menus/ESetupMenuView.js b/js/menus/ESetupMenuView.js
index 5967c307..e2a4d086 100644
--- a/js/menus/ESetupMenuView.js
+++ b/js/menus/ESetupMenuView.js
@@ -2,18 +2,32 @@
  * Created by aghassaei on 2/25/15.
  */
 
-define(['jquery', 'underscore', 'menuParent', 'plist', 'text!eSetupMenuTemplate'], function($, _, MenuParentView, plist, template){
+define(['jquery', 'underscore', 'menuParent', 'plist', 'text!eSetupMenuTemplate', 'latticeESim', 'eSim'],
+    function($, _, MenuParentView, plist, template, lattice, eSim){
 
     return MenuParentView.extend({
 
         events: {
+            "click #calcConnectivity":                              "_calcConnectivity"
         },
 
         _initialize: function(){
+            this.listenTo(eSim, "change", this.render);
+            this.listenTo(this.model, "change:materialClass", this._changeSimNav);
+        },
+
+        _changeSimNav: function(){
+            var materialClass = this.model.get("materialClass");
+            this.model.set("currentNav", materialClass + "NavSim");
+        },
+
+        _calcConnectivity: function(e){
+            e.preventDefault();
+            lattice.calculateConnectivity();
         },
 
         _makeTemplateJSON: function(){
-            return null;
+            return _.extend(this.model.toJSON(), eSim.toJSON(), plist);
         },
 
         template: _.template(template)
diff --git a/js/menus/templates/ESetupMenuView.html b/js/menus/templates/ESetupMenuView.html
index c14ccd9c..a6bd5cb1 100644
--- a/js/menus/templates/ESetupMenuView.html
+++ b/js/menus/templates/ESetupMenuView.html
@@ -1 +1,11 @@
-boundaries
\ No newline at end of file
+Simulation Type: &nbsp;&nbsp;
+    <div class="btn-group">
+        <button data-toggle="dropdown" class="btn dropdown-toggle" type="button"><%= allMaterialClasses[materialClass] %><span class="caret"></span></button>
+        <ul role="menu" class="dropdown-menu">
+            <% _.each(_.keys(allMaterialClasses), function(key){ %>
+                <li><a class="appState dropdownSelector" data-property="materialClass" data-value="<%= key %>" href="#"><%= allMaterialClasses[key] %></a></li>
+            <% }); %>
+        </ul>
+    </div><br/><br/>
+<a href="#" id="calcConnectivity" class="btn btn-block btn-lg btn-default">Calculate Connectivity</a><br/>
+<% if(numConnectedComponents){ %>Num Connected Components: &nbsp;&nbsp<%= numConnectedComponents %> <% } %>
\ No newline at end of file
diff --git a/js/simulation/electronics/eCell.js b/js/simulation/electronics/eCell.js
deleted file mode 100644
index 9ae6b0d7..00000000
--- a/js/simulation/electronics/eCell.js
+++ /dev/null
@@ -1,3 +0,0 @@
-/**
- * Created by aghassaei on 6/29/15.
- */
diff --git a/js/simulation/electronics/eSim.js b/js/simulation/electronics/eSim.js
index 9ae6b0d7..3059d4f4 100644
--- a/js/simulation/electronics/eSim.js
+++ b/js/simulation/electronics/eSim.js
@@ -1,3 +1,18 @@
 /**
  * Created by aghassaei on 6/29/15.
  */
+
+
+define(['underscore', 'backbone'], function(_, Backbone){
+
+    var eSim = Backbone.Model.extend({
+
+        defaults:{
+            numConnectedComponents: null
+        }
+
+
+    });
+
+    return new eSim();
+});
\ No newline at end of file
diff --git a/js/simulation/electronics/eSimCell.js b/js/simulation/electronics/eSimCell.js
new file mode 100644
index 00000000..58527050
--- /dev/null
+++ b/js/simulation/electronics/eSimCell.js
@@ -0,0 +1,27 @@
+/**
+ * Created by aghassaei on 6/29/15.
+ */
+
+//assume latticeESim has loaded?
+define(['cell', 'lattice'], function(DMACell, lattice){
+
+    DMACell.prototype.setConnectivityGroupNum = function(num, force){
+        if (force) this._eSimConnectivityGroup = num;
+        else if (this._eSimConnectivityGroup>num){
+            this._eSimConnectivityGroup = num;
+            this.propagateConnectivityGroupNum(num);
+        }
+    };
+
+    DMACell.prototype.getConnectivityGroupNum = function(){
+        return this._eSimConnectivityGroup;
+    };
+
+    DMACell.prototype.propagateConnectivityGroupNum = function(num){
+        if (num===undefined) num = this._eSimConnectivityGroup;
+        lattice.propagateToNeighbors(this.getAbsoluteIndex(), function(neighbor){
+            if (neighbor) neighbor.setConnectivityGroupNum(num);
+        });
+    };
+
+});
\ No newline at end of file
-- 
GitLab