Commit 864d6858 authored by amandaghassaei's avatar amandaghassaei

abstracted sim class

parent 56fc3d49
......@@ -93,6 +93,7 @@ require.config({
//simulation
glBoilerplate: "simulation/GLBoilerplate",
GPUMath: "simulation/GPUMath",
Sim: "simulation/Sim",
//emSim
emSimPlist: 'plists/EMSimPlist',
......
......@@ -26,7 +26,7 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'lattice'],
fixedIndices: []
},
initialize: function(){
_initialize: function(){
this.listenTo(appState, "change:currentNav", this._navChanged);
this.listenTo(appState, "change:currentTab", this._tabChanged);
......@@ -74,21 +74,18 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'lattice'],
var self = this;
this.set("isRunning", true);
this.set("needsReset", true);
var dt = this.get("dtSolver")/1000000;//convert to sec
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);
var runConstants = this._setRunConstants();
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.simLattice.iter(self.time, runConstants, false);
}
self.time += dt;
this.simLattice.iter(dt, self.time, gravityVect, groundHeight, friction, true);
self.simLattice.iter(self.time, runConstants, true);
//if (self._getViewMode() == "translation"){
// self.calcTranslation();
//}
......@@ -132,8 +129,9 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'lattice'],
_toggleFixedVisibility: function(){
var state = this.get("showFixed");
var cellsMin = lattice.get("cellsMin");
var self = this;
lattice.loopCells(function(cell, x, y, z){
if (!state || this.simLattice.isFixedAtIndex(new THREE.Vector3(x,y,z))) cell.show();
if (!state || self.simLattice.isFixedAtIndex(new THREE.Vector3(x,y,z))) cell.show();
else cell.hide();
});
three.render();
......
......@@ -3,57 +3,33 @@
*/
define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLattice', 'lattice', 'plist', 'globals'],
function(THREE, _, Backbone, three, appState, EmSimLattice, lattice, plist, globals){
define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'Sim', 'emSimLattice', 'lattice', 'plist', 'globals'],
function(THREE, _, Backbone, three, appState, Sim, EmSimLattice, lattice, plist, globals){
var emSim = Backbone.Model.extend({
defaults:{
var emSim = Sim.extend({
defaults: _.extend({},Sim.prototype.defaults,
{
gravity: 9.8,//m/s^2
gravityVector: new THREE.Vector3(0,0,-1),
dtSolver: 10,//us
dtRender: 100,//frames
isRunning: false,
needsReset: false,
viewMode: 'default',
colorMin: null,
colorMax: null,
autoRangeColor: true,
manualSelectFixed: false,
showFixed: false,
fixedIndices: [],
numSimMaterials: 20,//number of materials used in gradient view
visibleWire: -1,//-2 show all, -1 show conductors, or wire id, -3 show actuators, -4 show problem actuators
visibleActuator: 0,
groundHeight: 0,
friction: true
},
}
),
initialize: function(){
this.simLattice = new EmSimLattice();
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.listenTo(this, "change:visibleWire", function(){this.showConductors();});
this.listenTo(this, "change:visibleActuator", function(){this.showActuator();});
this.listenTo(this, "change:groundHeight", this._changeGroundHeight);
this._navChanged();
this.time = 0;
this.simMaterials = this._buildSimMaterials();
this._initialize();
},
_changeGroundHeight: function(){
......@@ -61,28 +37,8 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti
globals.get("baseplane").set("zIndex", height);
},
_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];
},
//events
_navChanged: function(){
......@@ -152,118 +108,24 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti
three.render();
},
_getViewMode: function(){
var currentTab = appState.get("currentTab");
if (currentTab != "emRun") return "default";
return this.get("viewMode");
},
run: function(){
var self = this;
this.set("isRunning", true);
this.set("needsReset", true);
_setRunConstants: function(){
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 gravity = 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;
self.simLattice.iter(dt, self.time, gravityVect, groundHeight, friction, false);
}
self.time += dt;
self.simLattice.iter(dt, self.time, gravityVect, groundHeight, friction, true);
//if (self._getViewMode() == "translation"){
// self.calcTranslation();
//}
});
this.simLattice.setConstants(dt, gravity, groundHeight, friction);
return {gravity: gravity, groundHeight:groundHeight, friction:friction};//eventually we do not need this, only for types fallback
},
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));
});
},
//save / load
getSaveData: function(){
var json = {
......
......@@ -476,7 +476,7 @@ define(['underscore', 'backbone', 'threeModel', 'lattice', 'plist', 'emWire', 'G
gpuMath.setUniformForProgram("positionCalc", "u_dt", dt, "1f");
},
iter: function(dt, time, gravity, groundHeight, friction, shouldRender){
iter: function(time, runConstants, shouldRender){
gpuMath.step("quaternionCalc", ["u_lastTranslation", "u_lastQuaternion", "u_fixed", "u_neighborsXMapping",
"u_neighborsYMapping", "u_compositeKs"], "u_quaternion", "u_wires", "u_wiresMeta", time);
......@@ -542,6 +542,11 @@ define(['underscore', 'backbone', 'threeModel', 'lattice', 'plist', 'emWire', 'G
gpuMath.swapTextures("u_quaternion", "u_lastQuaternion");
return;
var gravity = runConstants.gravity;
var groundHeight = runConstants.groundHeight;
var friction = runConstants.friction;
var dt = runConstants.dt;
var latticePitch = lattice.getPitch();
latticePitch = [latticePitch.x, latticePitch.y, latticePitch.z];
var multiplier = 1/(plist.allUnitTypes[lattice.getUnits()].multiplier);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment