Commit 56fc3d49 authored by amandaghassaei's avatar amandaghassaei

sim lattice on sim subclasses

parent 01b1db2d
...@@ -96,11 +96,16 @@ require.config({ ...@@ -96,11 +96,16 @@ require.config({
//emSim //emSim
emSimPlist: 'plists/EMSimPlist', emSimPlist: 'plists/EMSimPlist',
emSim: 'simulation/EM/emSim', emSim: 'simulation/function/EM/emSim',
emSimLattice: 'simulation/EM/emSimLattice', emSimLattice: 'simulation/function/EM/emSimLattice',
emSimCell: 'simulation/EM/emSimCell', emSimCell: 'simulation/function/EM/emSimCell',
emSimSuperCell: 'simulation/EM/emSimSuperCell', emWire: 'simulation/function/EM/emWire',
emWire: 'simulation/EM/emWire'
//elementFEASim
elementFEASimPlist: 'plists/EMSimPlist',
elementFEASim: 'simulation/element/FEA/elementFEASim',
elementFEASimLattice: 'simulation/element/FEA/elementFEASimLattice',
elementFEASimCell: 'simulation/element/FEA/elementFEASimCell'
}, },
shim: { shim: {
......
...@@ -30,14 +30,14 @@ define(['jquery', 'underscore', 'menuParent', 'emSimPlist', 'emSimLattice', 'emS ...@@ -30,14 +30,14 @@ define(['jquery', 'underscore', 'menuParent', 'emSimPlist', 'emSimLattice', 'emS
e.stopPropagation(); e.stopPropagation();
e.preventDefault(); e.preventDefault();
var wireId = $(e.target).data("id"); var wireId = $(e.target).data("id");
var signalCell = emSimLattice.get("wires")[wireId].getSignal(); var signalCell = emSim.simLattice.get("wires")[wireId].getSignal();
require(['menuWrapper'], function(menuWrapper){ require(['menuWrapper'], function(menuWrapper){
menuWrapper.initTabWithObject(signalCell, "signal", "emNavSignal") menuWrapper.initTabWithObject(signalCell, "signal", "emNavSignal")
}); });
}, },
_makeTemplateJSON: function(){ _makeTemplateJSON: function(){
return _.extend(emSimLattice.toJSON(), emPlist, emSim.toJSON()); return _.extend(emSim.simLattice.toJSON(), emPlist, emSim.toJSON());
}, },
template: _.template(template) template: _.template(template)
......
...@@ -42,7 +42,7 @@ define(['jquery', 'underscore', 'menuParent', 'emSimPlist', 'emSim', 'emSimLatti ...@@ -42,7 +42,7 @@ define(['jquery', 'underscore', 'menuParent', 'emSimPlist', 'emSim', 'emSimLatti
_makeTemplateJSON: function(){ _makeTemplateJSON: function(){
return _.extend(emSim.toJSON(), emSimLattice.toJSON(), emPlist); return _.extend(emSim.toJSON(), emSim.simLattice.toJSON(), emPlist);
}, },
template: _.template(template) template: _.template(template)
......
...@@ -26,7 +26,7 @@ define(['jquery', 'underscore', 'menuParent', 'emSimPlist', 'emSimLattice', 'emS ...@@ -26,7 +26,7 @@ define(['jquery', 'underscore', 'menuParent', 'emSimPlist', 'emSimLattice', 'emS
saveExitMenu: function(){ saveExitMenu: function(){
this.signal.setAsSignalGenerator(signal); this.signal.setAsSignalGenerator(signal);
emSimLattice.resetWiresMetaTexture(); emSim.simLattice.resetWiresMetaTexture();
return true; return true;
}, },
......
/**
* Created by ghassaei on 5/23/16.
*/
define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'lattice'],
function(THREE, _, Backbone, three, appState, lattice){
var Sim = Backbone.Model.extend({
defaults:{
dtSolver: 10,//us
dtRender: 100,//frames
isRunning: false,
needsReset: false,
viewMode: 'default',
colorMin: null,
colorMax: null,
autoRangeColor: true,
numSimMaterials: 20,//number of materials used in gradient view
manualSelectFixed: false,
showFixed: false,
fixedIndices: []
},
initialize: function(){
this.listenTo(appState, "change:currentNav", this._navChanged);
this.listenTo(appState, "change:currentTab", this._tabChanged);
this.listenTo(this, "change:showFixed", this._toggleFixedVisibility);
this.listenTo(this, "change:viewMode change:colorMax change:colorMin", this._viewModechanged);
this._navChanged();
this.time = 0;
this.simMaterials = this._buildSimMaterials();
},
_buildSimMaterials: function(){
var materials = [];
var numMaterials = this.get("numSimMaterials");
for (var i=0;i<numMaterials;i++){
materials.push(new THREE.MeshLambertMaterial({color: this._colorForVal(i, 0, numMaterials-1)}));
}
return materials;
},
_colorForVal : function(val, min, max){
if (min==max) return new THREE.Color();
var scaledVal = (1-(val - min)/(max - min)) * 0.7;
var color = new THREE.Color();
color.setHSL(scaledVal, 1, 0.5);
return color;
},
_materialForVal: function(val, min, max, numMaterials){
var index = Math.round((numMaterials-1)*(val-min)/(max-min));
if (min == max) index = 0;
return this.simMaterials[index];
},
_getViewMode: function(){
return this.get("viewMode");
},
run: function(){
var self = this;
this.set("isRunning", true);
this.set("needsReset", true);
var dt = this.get("dtSolver")/1000000;//convert to sec
var renderRate = this.get("dtRender");
var gravityVect = this.get("gravityVector").clone().normalize().multiplyScalar(this.get("gravity"));
var groundHeight = this.get("groundHeight");
var friction = this.get("friction");
this.simLattice.setConstants(dt, gravityVect, groundHeight, friction);
three.startAnimationLoop(function(){
for (var i=0;i<renderRate-1;i++){
self.time += dt;
this.simLattice.iter(dt, self.time, gravityVect, groundHeight, friction, false);
}
self.time += dt;
this.simLattice.iter(dt, self.time, gravityVect, groundHeight, friction, true);
//if (self._getViewMode() == "translation"){
// self.calcTranslation();
//}
});
},
pause: function(){
three.stopAnimationLoop();
this.set("isRunning", false);
},
reset: function(){
three.stopAnimationLoop();
this.set("isRunning", false);
this.set("needsReset", false);
this.simLattice.reset();
this.time = 0;
if (this._getViewMode == "translation"){
this.calcTranslation();
}
three.render();
},
fixCellAtIndex: function(index){
var position = index.clone().sub(lattice.get("cellsMin"));
var fixed = this.simLattice.fixCellAtIndex(position);
var fixedIndices = this.get("fixedIndices");
if (fixed) fixedIndices.push(index.clone());
else {
for (var i=0;i<fixedIndices.length;i++){
if (fixedIndices[i].equals(index)) {
fixedIndices.splice(i, 1);
break;
}
}
}
this.trigger("change");
},
_toggleFixedVisibility: function(){
var state = this.get("showFixed");
var cellsMin = lattice.get("cellsMin");
lattice.loopCells(function(cell, x, y, z){
if (!state || this.simLattice.isFixedAtIndex(new THREE.Vector3(x,y,z))) cell.show();
else cell.hide();
});
three.render();
},
_viewModechanged: function(){
//var viewMode = this._getViewMode();
//if (viewMode == "default") {
// this.simLattice.loopCells(function(cell){
// cell.showDefaultColor();
// });
//} else if (viewMode == "translation") {
// this.calcTranslation();
//}
three.render();
},
calcTranslation: function(){
var max = 0;
var min = 0;
var self = this;
var numMaterials = this.get("numSimMaterials");
if (!this.get("autoRangeColor")){
if (this.get("colorMin")) min = this.get("colorMin");
if (this.get("colorMax")) max = this.get("colorMax");
} else {
this.simLattice.loopCells(function(cell){
var translation = cell.getTranslation().length();
if (translation>max) max = translation;
});
this.set("colorMin", min);
this.set("colorMax", max);
}
this.simLattice.loopCells(function(cell){
var val = cell.getTranslation().length();
cell.showTranslation(self._materialForVal(val, min, max, numMaterials));
});
}
});
return Sim;
});
\ No newline at end of file
/**
* Created by ghassaei on 5/23/16.
*/
define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLattice', 'lattice'],
function(THREE, _, Backbone, three, appState, emSimLattice, lattice){
var Sim = Backbone.Model.extend({
defaults:{
dtSolver: 10,//us
dtRender: 100,//frames
isRunning: false,
needsReset: false,
viewMode: 'default',
colorMin: null,
colorMax: null,
autoRangeColor: true,
numSimMaterials: 20,//number of materials used in gradient view
manualSelectFixed: false,
showFixed: false,
fixedIndices: [],
},
initialize: function(){
this.listenTo(appState, "change:currentNav", this._navChanged);
this.listenTo(appState, "change:currentTab", this._tabChanged);
this.listenTo(this, "change:showFixed", this._toggleFixedVisibility);
this.listenTo(this, "change:viewMode change:colorMax change:colorMin", this._viewModechanged);
this._navChanged();
this.time = 0;
this.simMaterials = this._buildSimMaterials();
},
_buildSimMaterials: function(){
var materials = [];
var numMaterials = this.get("numSimMaterials");
for (var i=0;i<numMaterials;i++){
materials.push(new THREE.MeshLambertMaterial({color: this._colorForVal(i, 0, numMaterials-1)}));
}
return materials;
},
_colorForVal : function(val, min, max){
if (min==max) return new THREE.Color();
var scaledVal = (1-(val - min)/(max - min)) * 0.7;
var color = new THREE.Color();
color.setHSL(scaledVal, 1, 0.5);
return color;
},
_materialForVal: function(val, min, max, numMaterials){
var index = Math.round((numMaterials-1)*(val-min)/(max-min));
if (min == max) index = 0;
return this.simMaterials[index];
},
_getViewMode: function(){
return this.get("viewMode");
},
run: function(){
var self = this;
this.set("isRunning", true);
this.set("needsReset", true);
var dt = this.get("dtSolver")/1000000;//convert to sec
var renderRate = this.get("dtRender");
var gravityVect = this.get("gravityVector").clone().normalize().multiplyScalar(this.get("gravity"));
var groundHeight = this.get("groundHeight");
var friction = this.get("friction");
emSimLattice.setConstants(dt, gravityVect, groundHeight, friction);
three.startAnimationLoop(function(){
for (var i=0;i<renderRate-1;i++){
self.time += dt;
emSimLattice.iter(dt, self.time, gravityVect, groundHeight, friction, false);
}
self.time += dt;
emSimLattice.iter(dt, self.time, gravityVect, groundHeight, friction, true);
//if (self._getViewMode() == "translation"){
// self.calcTranslation();
//}
});
},
pause: function(){
three.stopAnimationLoop();
this.set("isRunning", false);
},
reset: function(){
three.stopAnimationLoop();
this.set("isRunning", false);
this.set("needsReset", false);
emSimLattice.reset();
this.time = 0;
if (this._getViewMode == "translation"){
this.calcTranslation();
}
three.render();
},
fixCellAtIndex: function(index){
var position = index.clone().sub(lattice.get("cellsMin"));
var fixed = emSimLattice.fixCellAtIndex(position);
var fixedIndices = this.get("fixedIndices");
if (fixed) fixedIndices.push(index.clone());
else {
for (var i=0;i<fixedIndices.length;i++){
if (fixedIndices[i].equals(index)) {
fixedIndices.splice(i, 1);
break;
}
}
}
this.trigger("change");
},
_toggleFixedVisibility: function(){
var state = this.get("showFixed");
var cellsMin = lattice.get("cellsMin");
lattice.loopCells(function(cell, x, y, z){
if (!state || emSimLattice.isFixedAtIndex(new THREE.Vector3(x,y,z))) cell.show();
else cell.hide();
});
three.render();
},
_viewModechanged: function(){
//var viewMode = this._getViewMode();
//if (viewMode == "default") {
// emSimLattice.loopCells(function(cell){
// cell.showDefaultColor();
// });
//} else if (viewMode == "translation") {
// this.calcTranslation();
//}
three.render();
},
calcTranslation: function(){
var max = 0;
var min = 0;
var self = this;
var numMaterials = this.get("numSimMaterials");
if (!this.get("autoRangeColor")){
if (this.get("colorMin")) min = this.get("colorMin");
if (this.get("colorMax")) max = this.get("colorMax");
} else {
emSimLattice.loopCells(function(cell){
var translation = cell.getTranslation().length();
if (translation>max) max = translation;
});
this.set("colorMin", min);
this.set("colorMax", max);
}
emSimLattice.loopCells(function(cell){
var val = cell.getTranslation().length();
cell.showTranslation(self._materialForVal(val, min, max, numMaterials));
});
}
});
return new Sim();
});
\ No newline at end of file
/**
* Created by ghassaei on 5/23/16.
*/
/**
* Created by ghassaei on 5/23/16.
*/
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLattice', 'lattice', 'plist', 'globals'], define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLattice', 'lattice', 'plist', 'globals'],
function(THREE, _, Backbone, three, appState, emSimLattice, lattice, plist, globals){ function(THREE, _, Backbone, three, appState, EmSimLattice, lattice, plist, globals){
var emSim = Backbone.Model.extend({ var emSim = Backbone.Model.extend({
...@@ -38,6 +38,8 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti ...@@ -38,6 +38,8 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti
}, },
initialize: function(){ initialize: function(){
this.simLattice = new EmSimLattice();
this.listenTo(appState, "change:currentNav", this._navChanged); this.listenTo(appState, "change:currentNav", this._navChanged);
this.listenTo(appState, "change:currentTab", this._tabChanged); this.listenTo(appState, "change:currentTab", this._tabChanged);
...@@ -98,7 +100,7 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti ...@@ -98,7 +100,7 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti
var previous = appState.previous("currentNav"); var previous = appState.previous("currentNav");
if (previous != "emNavSignal" && plist.allMenus[appState.get("currentNav")].parent != "emNavSim"){ if (previous != "emNavSignal" && plist.allMenus[appState.get("currentNav")].parent != "emNavSim"){
emSimLattice.setCells(lattice.getCells(), this.get("fixedIndices")); this.simLattice.setCells(lattice.getCells(), this.get("fixedIndices"));
} }
var currentTab = appState.get("currentTab"); var currentTab = appState.get("currentTab");
...@@ -129,22 +131,22 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti ...@@ -129,22 +131,22 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti
showActuator: function(index){ showActuator: function(index){
if (index === undefined) index = this.get("visibleActuator"); if (index === undefined) index = this.get("visibleActuator");
emSimLattice.loopCells(function(cell){ this.simLattice.loopCells(function(cell){
cell.setTransparent(true); cell.setTransparent(true);
}); });
emSimLattice.get("actuators")[index].cell.setTransparent(false); this.simLattice.get("actuators")[index].cell.setTransparent(false);
three.render(); three.render();
}, },
showConductors: function(groupNum){ showConductors: function(groupNum){
if (groupNum === undefined) groupNum = this.get("visibleWire"); if (groupNum === undefined) groupNum = this.get("visibleWire");
if (_.keys(emSimLattice.get("wires")).length == 0 || groupNum == -2){ if (_.keys(this.simLattice.get("wires")).length == 0 || groupNum == -2){
lattice.setOpaque(); lattice.setOpaque();
three.render(); three.render();
return; return;
} }
var allVisible = groupNum == -1; var allVisible = groupNum == -1;
emSimLattice.loopCells(function(cell){ this.simLattice.loopCells(function(cell){
cell.setTransparent(!cell.conductiveGroupVisible(allVisible, groupNum)); cell.setTransparent(!cell.conductiveGroupVisible(allVisible, groupNum));
}); });
three.render(); three.render();
...@@ -169,15 +171,15 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti ...@@ -169,15 +171,15 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti
var gravityVect = this.get("gravityVector").clone().normalize().multiplyScalar(this.get("gravity")); var gravityVect = this.get("gravityVector").clone().normalize().multiplyScalar(this.get("gravity"));
var groundHeight = this.get("groundHeight"); var groundHeight = this.get("groundHeight");
var friction = this.get("friction"); var friction = this.get("friction");
emSimLattice.setConstants(dt, gravityVect, groundHeight, friction); this.simLattice.setConstants(dt, gravityVect, groundHeight, friction);
three.startAnimationLoop(function(){ three.startAnimationLoop(function(){
for (var i=0;i<renderRate-1;i++){ for (var i=0;i<renderRate-1;i++){
self.time += dt; self.time += dt;
emSimLattice.iter(dt, self.time, gravityVect, groundHeight, friction, false); self.simLattice.iter(dt, self.time, gravityVect, groundHeight, friction, false);
} }
self.time += dt; self.time += dt;
emSimLattice.iter(dt, self.time, gravityVect, groundHeight, friction, true); self.simLattice.iter(dt, self.time, gravityVect, groundHeight, friction, true);
//if (self._getViewMode() == "translation"){ //if (self._getViewMode() == "translation"){
// self.calcTranslation(); // self.calcTranslation();
//} //}
...@@ -193,7 +195,7 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti ...@@ -193,7 +195,7 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti
three.stopAnimationLoop(); three.stopAnimationLoop();
this.set("isRunning", false); this.set("isRunning", false);
this.set("needsReset", false); this.set("needsReset", false);
emSimLattice.reset(); this.simLattice.reset();
this.time = 0; this.time = 0;
if (this._getViewMode == "translation"){ if (this._getViewMode == "translation"){
this.calcTranslation(); this.calcTranslation();
...@@ -204,7 +206,7 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti ...@@ -204,7 +206,7 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti
fixCellAtIndex: function(index){ fixCellAtIndex: function(index){
var position = index.clone().sub(lattice.get("cellsMin")); var position = index.clone().sub(lattice.get("cellsMin"));
var fixed = emSimLattice.fixCellAtIndex(position); var fixed = this.simLattice.fixCellAtIndex(position);
var fixedIndices = this.get("fixedIndices"); var fixedIndices = this.get("fixedIndices");
if (fixed) fixedIndices.push(index.clone()); if (fixed) fixedIndices.push(index.clone());
else { else {
...@@ -222,7 +224,7 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti ...@@ -222,7 +224,7 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti
var state = this.get("showFixed"); var state = this.get("showFixed");
var cellsMin = lattice.get("cellsMin"); var cellsMin = lattice.get("cellsMin");
lattice.loopCells(function(cell, x, y, z){ lattice.loopCells(function(cell, x, y, z){
if (!state || emSimLattice.isFixedAtIndex(new THREE.Vector3(x,y,z))) cell.show(); if (!state || this.simLattice.isFixedAtIndex(new THREE.Vector3(x,y,z))) cell.show();
else cell.hide(); else cell.hide();
}); });
three.render(); three.render();
...@@ -232,7 +234,7 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti ...@@ -232,7 +234,7 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti
_viewModechanged: function(){ _viewModechanged: function(){
//var viewMode = this._getViewMode(); //var viewMode = this._getViewMode();
//if (viewMode == "default") { //if (viewMode == "default") {
// emSimLattice.loopCells(function(cell){ // this.simLattice.loopCells(function(cell){
// cell.showDefaultColor(); // cell.showDefaultColor();
// }); // });
//} else if (viewMode == "translation") { //} else if (viewMode == "translation") {
...@@ -250,14 +252,14 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti ...@@ -250,14 +252,14 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti
if (this.get("colorMin")) min = this.get("colorMin"); if (this.get("colorMin")) min = this.get("colorMin");
if (this.get("colorMax"