Commit 55cef654 authored by amandaghassaei's avatar amandaghassaei

refactoring

parent 9bf341e1
......@@ -16,14 +16,14 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
},
initialize: function(options, classProperties, callback){
initialize: function(options){
this.cells = [[[null]]];//3D matrix containing all cells and null, dynamic size
//bind events
this.listenTo(appState, "change:cellMode", this._updateForMode);//show hide cells
if (this.__initialize) this.__initialize(options, callback);
if (this.__initialize) this.__initialize(options);
},
......@@ -454,8 +454,7 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
three.render();
},
loopSketchLayer: function(index, planeType, callback, hideAll){
if (hideAll) this.hideCells(true);
loopSketchLayer: function(index, planeType, callback){
var x = null;
var y = null;
var z = null;
......
......@@ -160,6 +160,7 @@ define(['underscore', 'backbone', 'threeModel', 'three', 'plist', 'globals'],
if (this.get("showOneLayer")){
index = globals.get("baseplane").get("zIndex");
var planeType = globals.get("baseplane").get("planeType");
this.lattice.hideCells(true);
this.lattice.loopSketchLayer(index, planeType, function(cell){
cell.setTransparent(false);
cell.show();
......
//global workers so they do not have to be reinstantiated
function persistentWorkers(numWorkers){
//check compatibility
if (!(typeof window.Worker === "function")){
console.log("workers not supported");
return null;
}
//local variables
var allWorkers = [];
var mapQueue = [];
//create array of workers
var URL = window.URL || window.webkitURL;
var workerURL = makeBlobURL(URL, myWorker);
for (var i=0;i<numWorkers;i++){
var worker = new Worker(workerURL);
worker.onmessage = onMessage;
worker.postMessage({url: document.location.toString()});
allWorkers.push(worker);
}
URL.revokeObjectURL(workerURL);
function map(data, executable, env, incrCallback, finalCallback){
//save new task in map queue
mapQueue.push({data:data,
executable:prepareExeFunc(executable.toString()),
env:env,
index:0,
incrCallback:incrCallback,
finalCallback:finalCallback,
finished:false,
activeThreads:0});
for (var i=0;i<allWorkers.length;i++){
allWorkers[i].postMessage({isWorking:true});//ask workers if they are busy
}
}
function prepareExeFunc(string){
var index = string.indexOf("(");//get index of first ( in function declaration
if (index == -1) {
console.log("exe function not formed properly for web workers " + string);
return null;
}
return "function executable" + string.substring(index);
}
function onMessage(e){
if (e.data.result){//handle result first
//get current work item off queue
if (mapQueue.length == 0) return;
var currentTask = mapQueue[0];
currentTask.activeThreads--;//decrement active threads
if (currentTask.incrCallback) currentTask.incrCallback(e.data.result);//incremental callback
if (currentTask.finished && currentTask.activeThreads == 0){
if (currentTask.finalCallback) currentTask.finalCallback();
mapQueue.shift();//remove first element
}
}
var nextTask = getNextTask(mapQueue[0], 0);
if (!nextTask) return;
var currentIndex = nextTask.index;
nextTask.index++;
//check that the index is not out of bounds
if (nextTask.data.length<=currentIndex){
nextTask.finished = true;
e.data.result = null;//remove result so it doesn't get handled twice
onMessage(e);//try again in case there is another item in the queue to start
return;
}
if (e.data.isWorking === false){
nextTask.activeThreads++;
e.target.postMessage({
arg:nextTask.data[currentIndex],
localEnv:JSON.stringify(nextTask.env),
executable:nextTask.executable});
}
}
function getNextTask(currentTask, index){
if (!currentTask) return null;
if (currentTask.finished){
var nextIndex = index+1;
if (mapQueue.length<=nextIndex) return null;
return getNextTask(mapQueue[nextIndex]);
} else {
return currentTask;
}
}
function makeBlobURL(URL, someFunction) {
var blob = new Blob(["(" + someFunction.toString() + ")()"], { type: "text/javascript" });
return URL.createObjectURL(blob);
}
return {map:map, allWorkers:allWorkers};//return all public methods and vars
}
\ No newline at end of file
/**
* Created by aghassaei on 1/12/15.
*/
function myWorker(){
//local variables
var localEnv = null;//local variables passed in from outside
var working = false;//boolean that says whether I'm busy or not
var arg = null;//main data we are crunching
var modelMesh = null;//hold on to this so we don't have to keep passing it in
self.onmessage = function(e) {
var data = e.data;
if (data.url) {
var url = data.url;
var index = url.indexOf('index.html');//url of landing page
if (index != -1) {
url = url.substring(0, index);
}
//load all scripts
// importScripts(url + 'dependencies/three.js');
// importScripts(url + 'js/models/dmaBeam.js');
}
//
if (data.model){
//var material = new THREE.MeshBasicMaterial({side:THREE.DoubleSide});
//modelMesh = new THREE.Mesh(JSON.parse(data.model), material);
}
if (data.executable){
if (data.localEnv){//be sure to get local environment vars before executable runs
localEnv = JSON.parse(data.localEnv);
}
if (data.arg){//be sure to get arg before executable runs
arg = data.arg;
}
if (working) {
console.log("problem here, already working on something else");
return;
}
working = true;
eval(data.executable);
var result = executable(arg);
working = false;
postMessage({result:result, isWorking:working});
}
if (data.isWorking){
postMessage({isWorking:working});
}
};
}
......@@ -276,7 +276,7 @@ define(['three', 'underscore', 'backbone', 'threeModel', 'appState', 'emSimLatti
var signals = [];
_.each(emSimLattice.get("signals"), function(signal){
if (!signal.index) return;//deleted
signals.push(_.extend(signal.getSignalJSON(), {index:signal.getAbsoluteIndex()}));
signals.push(_.extend(signal.getSignalJSON(), {index:signal.getIndex()}));
});
var latticeJson = {signals:signals};
return _.extend(json, {lattice:latticeJson});
......
......@@ -8,7 +8,7 @@ define(["underscore", "cell", "lattice", "plist", "three"],
function EMSimCell(cell){
this.origPosition = cell.getAbsolutePosition();
this.origPosition = cell.getPosition();
// this.rotation = cell.getAbsoluteOrientation();
this.cell = cell;
......@@ -40,8 +40,8 @@ define(["underscore", "cell", "lattice", "plist", "three"],
}
EMSimCell.prototype.getAbsoluteIndex = function(){
return this.cell.getAbsoluteIndex();
EMSimCell.prototype.getIndex = function(){
return this.cell.getIndex();
};
EMSimCell.prototype.setNeighbors = function(neighbors){//precompute neighbors
......@@ -108,7 +108,7 @@ define(["underscore", "cell", "lattice", "plist", "three"],
this._setPosition(this.origPosition.clone().add(this.translation.clone().multiplyScalar(multiplier)));
};
EMSimCell.prototype.getAbsolutePosition = function(){
EMSimCell.prototype.getPosition = function(){
var multiplier = 1/(plist.allUnitTypes[lattice.getUnits()].multiplier);
return this.origPosition.clone().add(this.translation.clone().multiplyScalar(multiplier))
};
......
......@@ -80,7 +80,7 @@ define(['underscore', 'backbone', 'threeModel', 'lattice', 'plist', 'emWire', 'G
var rgbaIndex = 4*index;
var position = cell.getAbsolutePosition();
var position = cell.getPosition();
self.originalPosition[rgbaIndex] = position.x;
self.originalPosition[rgbaIndex+1] = position.y;
self.originalPosition[rgbaIndex+2] = position.z;
......@@ -124,7 +124,7 @@ define(['underscore', 'backbone', 'threeModel', 'lattice', 'plist', 'emWire', 'G
return;
}
var neighborIndex3D = neighbor.getAbsoluteIndex().sub(cellsMin);
var neighborIndex3D = neighbor.getIndex().sub(cellsMin);
var neighborMappingIndex1D = self.cellsIndexMapping[neighborIndex3D.x][neighborIndex3D.y][neighborIndex3D.z];
self.neighborsXMapping[compositeIndex + neighborIndex%3] = neighborMappingIndex1D%textureDim;
self.neighborsYMapping[compositeIndex + neighborIndex%3] = parseInt(neighborMappingIndex1D/textureDim);
......@@ -900,7 +900,7 @@ define(['underscore', 'backbone', 'threeModel', 'lattice', 'plist', 'emWire', 'G
var self = this;
this._loopCells(lattice.getCells(), function(cell){
var index = cell.getAbsoluteIndex().sub(lattice.get("cellsMin"));
var index = cell.getIndex().sub(lattice.get("cellsMin"));
var rgbaIndex = 4*self.cellsIndexMapping[index.x][index.y][index.z];
var position = [self.originalPosition[rgbaIndex], self.originalPosition[rgbaIndex+1], self.originalPosition[rgbaIndex+2]];
cell.object3D.position.set(position[0], position[1], position[2]);
......
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