From db0808f32d0868d05f987d499d7d68694ee55fb5 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Thu, 28 May 2015 16:23:23 -0700
Subject: [PATCH] adding move commands

---
 js/cam/assemblers/Assembler.js        | 108 ++++++++++++++++++++++++--
 js/cam/assemblers/StaplerAssembler.js |  22 ++++++
 js/cam/cam.js                         |   8 --
 3 files changed, 125 insertions(+), 13 deletions(-)

diff --git a/js/cam/assemblers/Assembler.js b/js/cam/assemblers/Assembler.js
index 318ce58a..bc88c628 100644
--- a/js/cam/assemblers/Assembler.js
+++ b/js/cam/assemblers/Assembler.js
@@ -133,23 +133,121 @@ Assembler.prototype._postReleaseStock = function(cellPosition, cell, exporter, r
 
 
 Assembler.prototype.updateCellMode = function(){
-    this.stock.setMode();
+//    this.stock.setMode();//todo fix this
+    _.each(this.stock.cells, function(cell){
+        cell.setMode();
+    });
 };
 
 Assembler.prototype.pickUpStock = function(){
-    this.hasStock = true;
-    this.cell.draw();
+    this.stock.show();
 };
 
 Assembler.prototype.releaseStock = function(index){
-    this.hasStock = false;
     globals.lattice.showCellAtIndex(JSON.parse(index));
-    this.cell.hide();
+    this.stock.hide();
 };
 
 Assembler.prototype.pause = function(){
 };
 
+Assembler.prototype.moveTo = function(x, y, z, speed, wcs, callback){
+    x = this._makeAbsPosition(x, wcs.x);
+    y = this._makeAbsPosition(y, wcs.y);
+    z = this._makeAbsPosition(z, wcs.z);
+    this._moveTo(x, y, z, speed, wcs, callback);
+};
+
+Assembler.prototype._moveTo = function(x, y, z, speed, wcs, callback){
+    var totalThreads = 3;
+    function sketchyCallback(){
+        totalThreads -= 1;
+        if (totalThreads > 0) return;
+        callback();
+    }
+    var startingPos = this.stock.getPosition();
+    speed = this._normalizeSpeed(startingPos, x, y, this._reorganizeSpeed(speed));
+    this._moveXAxis(startingPos.x, x, speed.x, sketchyCallback);
+    this._moveYAxis(startingPos.y, y, speed.y, sketchyCallback);
+    this._moveZAxis(startingPos.z, z, speed.z, sketchyCallback);
+};
+
+Assembler.prototype._moveXAxis = function(startingPos, target, speed, callback){
+    this._moveAxis(startingPos, target, "x", speed, callback);
+};
+Assembler.prototype._moveYAxis = function(startingPos, target, speed, callback){
+    this._moveAxis(startingPos, target, "y", speed, callback);
+};
+Assembler.prototype._moveZAxis = function(startingPos, target, speed, callback){
+    this._moveAxis(startingPos, target, "z", speed, callback);
+};
+
+Assembler.prototype._moveAxis = function(startingPos, target, axis, speed, callback){
+    if (target == null || target === undefined) {
+        callback();
+        return;
+    }
+    this._animateObjects([this.stock], axis, speed, startingPos, target, callback);
+};
+
+Assembler.prototype._makeAbsPosition = function(target, wcs){
+    if (target == "" || target == null || target === undefined) return null;
+    return parseFloat(target)+wcs;
+};
+
+Assembler.prototype._reorganizeSpeed = function(speed){
+    var newSpeed = {};
+    newSpeed.x = speed.xy;
+    newSpeed.y = speed.xy;
+    newSpeed.z = speed.z;
+    return newSpeed;
+};
+
+Assembler.prototype._normalizeSpeed = function(startingPos, x, y, speed){//xy moves need speed normalization
+    var normSpeed = {};
+    if (x == "" || y == "") return speed;
+    var deltaX = x-startingPos.x;
+    var deltaY = y-startingPos.y;
+    var totalDistance = Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));
+    if (totalDistance == 0) return speed;
+    normSpeed.x = Math.abs(deltaX/totalDistance*speed.x);
+    normSpeed.y = Math.abs(deltaY/totalDistance*speed.y);
+    normSpeed.z = speed.z;
+    return normSpeed;
+};
+
+Assembler.prototype._animateObjects = function(objects, axis, speed, startingPos, target, callback){
+    var increment = speed/25*globals.cam.get("simSpeed");
+    if (increment == 0) {
+        if (callback) callback();
+        return;
+    }
+    var direction = 1;
+    if (target-startingPos < 0) direction = -1;
+    increment = Math.max(increment, 0.00001)*direction;//need to put a min on the increment - other wise this stall out with floating pt tol
+    this._incrementalMove(objects, axis, increment, startingPos, target, direction, callback);
+};
+
+Assembler.prototype._incrementalMove = function(objects, axis, increment, currentPos, target, direction, callback){
+    var self = this;
+    setTimeout(function(){
+        if ((target-currentPos)*direction <= 0) {
+            if (callback) callback();
+            return;
+        }
+        var nextPos = currentPos + increment;
+        if (Math.abs(target-currentPos) < Math.abs(increment)) nextPos = target;//don't overshoot
+        self._setPosition(objects, nextPos, axis);
+        self._incrementalMove(objects, axis, increment, nextPos, target, direction, callback)
+    }, 10);
+};
+
+Assembler.prototype._setPosition = function(objects, nextPos, axis){
+    _.each(objects, function(object){
+        object.position[axis] = nextPos;
+    });
+};
+
 
 
 
diff --git a/js/cam/assemblers/StaplerAssembler.js b/js/cam/assemblers/StaplerAssembler.js
index c638f5a9..b815ec6e 100644
--- a/js/cam/assemblers/StaplerAssembler.js
+++ b/js/cam/assemblers/StaplerAssembler.js
@@ -57,4 +57,26 @@ StaplerAssembler.prototype._loadSTls = function(doAdd){
         geometry.applyMatrix(new THREE.Matrix4().makeTranslation(0, 1.8545, -1.2598));
         doAdd(geometryScale(geometry), "substrate");
     });
+};
+
+StaplerAssembler.prototype._moveXAxis = function(startingPos, target, axis, speed, callback){
+    if (target == null || target === undefined) {
+        callback();
+        return;
+    }
+    this._animateObjects([this.xAxis], axis, speed, startingPos, target, callback);
+};
+StaplerAssembler.prototype._moveYAxis = function(startingPos, target, axis, speed, callback){
+    if (target == null || target === undefined) {
+        callback();
+        return;
+    }
+    this._animateObjects([this.yAxis], axis, speed, startingPos, target, callback);
+};
+StaplerAssembler.prototype._moveZAxis = function(startingPos, target, axis, speed, callback){
+    if (target == null || target === undefined) {
+        callback();
+        return;
+    }
+    this._animateObjects([this.zAxis], axis, speed, startingPos, target, callback);
 };
\ No newline at end of file
diff --git a/js/cam/cam.js b/js/cam/cam.js
index c6c25b4b..3eaa46c0 100644
--- a/js/cam/cam.js
+++ b/js/cam/cam.js
@@ -67,7 +67,6 @@ Cam = Backbone.Model.extend({
                 "change:cellType " +
                 "change:connectionType",
             this._setNeedsPostProcessing);
-        this.listenTo(globals.lattice, "change:scale", this._setCAMScale);
         this.listenTo(globals.appState, "change:stockSimulationPlaying", this._stockSimulation);
 
         this.listenTo(globals.lattice, "change:partType", this._updatePartType);
@@ -126,13 +125,6 @@ Cam = Backbone.Model.extend({
         globals.three.render();
     },
 
-    _setCAMScale: function(){
-        var scale = globals.lattice.get("scale");
-        this.get("origin").scale.set(scale/8, scale/8, scale/8);
-        this.get("stock").scale.set(scale/8, scale/8, scale/8);
-        if (this.get("assembler")) this.get("assembler").setScale(scale);
-    },
-
     _tabChanged: function(){
         this._setCAMVisibility();
         if (globals.appState.get("currentTab") != "animate") this.resetSimulation();
-- 
GitLab