From e6861849190ebc520f8198c2a344fd0d9dcabd9e Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Thu, 25 Jun 2015 22:07:30 -0700
Subject: [PATCH] eod

---
 js/cells/supercells/DMASuperCell.js       |  4 +++-
 js/lattice/CompositeEditorLattice.js      | 16 +++++++++-------
 js/lattice/LatticeBase.js                 |  4 ++--
 js/menus/CompositeMenuView.js             | 12 ++++++------
 js/menus/templates/CompositeMenuView.html |  3 +++
 5 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/js/cells/supercells/DMASuperCell.js b/js/cells/supercells/DMASuperCell.js
index 44bf5cee..045e736e 100644
--- a/js/cells/supercells/DMASuperCell.js
+++ b/js/cells/supercells/DMASuperCell.js
@@ -42,12 +42,14 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'],
 
                     var json = {index: new THREE.Vector3(x, y, z)};
                     var callback = function(cell){
-                        var cellIndex = cell.getIndex();//x, y, z have changed by now
+                        var cellIndex = cell.getIndex();
+                        if (material.cellsMin) cellIndex.sub(material.cellsMin);
                         cells[cellIndex.x][cellIndex.y][cellIndex.z] = cell;
                     };
 
                     if (material.sparseCells){
                         if (material.sparseCells[x][y][z]){
+                            json.index.add(material.cellsMin);
                             json = _.extend(json, material.sparseCells[x][y][z]);
                             this._makeSubCellForIndex(json, callback);
                         }//else no cell in this spot
diff --git a/js/lattice/CompositeEditorLattice.js b/js/lattice/CompositeEditorLattice.js
index a8b40430..ecf86628 100644
--- a/js/lattice/CompositeEditorLattice.js
+++ b/js/lattice/CompositeEditorLattice.js
@@ -16,7 +16,8 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
 
         defaults: _.extend(LatticeBase.prototype.defaults, {
             name: "",
-            color: null
+            color: null,
+            origin: new THREE.Vector3(0,0,0)
         }),
 
         __initialize: function(options, callback){
@@ -69,26 +70,27 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
         },
 
 
-        makeNewCompositeMaterial: function(dimensions){
+        makeNewCompositeMaterial: function(bounds){
             if (this.get("numCells") == 0) {
                 console.warn("no cells in this composite");
                 return;
             }
             var id = this.get("id");
-            var data = this.toJSONForSave(dimensions);
+            var data = this.toJSONForSave(bounds);
             materials.setMaterial(id, data);
         },
 
-        toJSONForSave: function(dimensions){
+        toJSONForSave: function(bounds){
             var name = this.get("name");
             if (name == "") name = "Composite Material " + compositeNum++;
-            if (dimensions) var _dimensions = dimensions.clone();
+            if (bounds) var _dimensions = bounds.max.clone().sub(bounds.min);
             var cellsMin = this.get("cellsMin");
             var cellsMax = this.get("cellsMax");
             if (cellsMax) {
+                console.log(bounds.min);
                 cellsMax = cellsMax.clone();
-                cellsMax.sub(cellsMin);
-                cellsMin = new THREE.Vector3(0,0,0);
+                cellsMax.sub(cellsMin).sub(bounds.min);
+                cellsMin = new THREE.Vector3(0,0,0).sub(bounds.min);
             }
             var data = {
                 name: name,
diff --git a/js/lattice/LatticeBase.js b/js/lattice/LatticeBase.js
index 540b1b3e..6cdbccc7 100644
--- a/js/lattice/LatticeBase.js
+++ b/js/lattice/LatticeBase.js
@@ -198,7 +198,7 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
         },
 
         calculateBoundingBox: function(){
-            if (!this.get("cellsMax") || !this.get("cellsMin")) return new THREE.Vector3(0,0,0);
+            if (!this.get("cellsMax") || !this.get("cellsMin")) return {max:new THREE.Vector3(0,0,0), min:new THREE.Vector3(0,0,0)};
             var dimMax = this.get("cellsMax").clone().sub(this.get("cellsMin")).add(new THREE.Vector3(1,1,1));
             var dimMin = new THREE.Vector3(0,0,0);
             this._loopCells(this.sparseCells, function(cell, x, y, z){
@@ -215,7 +215,7 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre
                     }
                 }
             });
-            return dimMax.sub(dimMin);
+            return {max:dimMax, min:dimMin};
         },
 
 
diff --git a/js/menus/CompositeMenuView.js b/js/menus/CompositeMenuView.js
index 32276511..66eceac2 100644
--- a/js/menus/CompositeMenuView.js
+++ b/js/menus/CompositeMenuView.js
@@ -5,7 +5,7 @@
 define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice', 'globals', 'materials', 'text!compositeMenuTemplate', 'fileSaver'],
     function($, _, MenuParentView, plist, lattice, globals, materials, template, fileSaver){
 
-    var dimensions;
+    var bounds;
 
     return MenuParentView.extend({
 
@@ -24,12 +24,12 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice', 'globals', 'ma
                 return;
             }
             this.listenTo(lattice.compositeEditor, "change", function(){
-                if (lattice.compositeEditor.changedAttributes().numCells !== undefined) dimensions = lattice.compositeEditor.calculateBoundingBox();
+                if (lattice.compositeEditor.changedAttributes().numCells !== undefined) bounds = lattice.compositeEditor.calculateBoundingBox();
                 this.render();
             });
             this.listenTo(this.model, "change", this.render);
 
-            dimensions = lattice.compositeEditor.calculateBoundingBox();
+            bounds = lattice.compositeEditor.calculateBoundingBox();
         },
 
 
@@ -57,13 +57,13 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice', 'globals', 'ma
                 this._exit();
                 return;
             }
-            lattice.compositeEditor.makeNewCompositeMaterial(dimensions.clone());
+            lattice.compositeEditor.makeNewCompositeMaterial(bounds);
             this._exit();
         },
 
         _saveComposite: function(e){
             e.preventDefault();
-            fileSaver.saveMaterial(lattice.compositeEditor.get("id"), lattice.compositeEditor.toJSONForSave(dimensions));
+            fileSaver.saveMaterial(lattice.compositeEditor.get("id"), lattice.compositeEditor.toJSONForSave(bounds));
         },
 
         _cancelComposite: function(e){
@@ -89,7 +89,7 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice', 'globals', 'ma
         _makeTemplateJSON: function(){
             return _.extend(this.model.toJSON(), plist, globals, lattice.compositeEditor.toJSON(),
                 {
-                    dimensions: dimensions,
+                    dimensions: bounds.max.clone().sub(bounds.min),
                     materials: materials.list,
                     validCompositeMaterials: materials.getVaildAvailableCompositeKeys(lattice.compositeEditor.get("id"))
                 });
diff --git a/js/menus/templates/CompositeMenuView.html b/js/menus/templates/CompositeMenuView.html
index 90e019eb..47c20a23 100644
--- a/js/menus/templates/CompositeMenuView.html
+++ b/js/menus/templates/CompositeMenuView.html
@@ -1,6 +1,9 @@
 Name: &nbsp;&nbsp;<input id="compositeName" data-property="name" value="<%= name %>" placeholder="Enter Name" class="seventyFiveWidth form-control textInput compositeEditor" type="text"><br/><br/>
 Num Cells: &nbsp;&nbsp;<%= numCells %><br/><br/>
 Bounding Box: &nbsp;&nbsp;<%= dimensions.x %> x <%= dimensions.y %> x <%= dimensions.z %><br/><br/>
+Origin (xyz): &nbsp;&nbsp;<input data-property="origin" data-key="x" value="<%= origin.x %>" placeholder="X" class="form-control intInput compositeEditor" type="text">&nbsp;
+            <input data-property="origin" data-key="y" value="<%= origin.y %>" placeholder="Y" class="form-control intInput compositeEditor" type="text">&nbsp;
+            <input data-property="origin" data-key="z" value="<%= origin.z %>" placeholder="Z" class="form-control intInput compositeEditor" type="text"><br/><br/>
 Display Color: &nbsp;&nbsp;
     <input id="compositeColor" style="border-color: <%= color %> ;" data-property="color" value="<%= color %>" placeholder="Enter HEX" class="halfWidth compositeEditor form-control hexInput" type="text"><br/><br/>
 <a id="newRandomColor" href="#" class="btn btn-block btn-lg btn-default">New Random Color</a><br/>
-- 
GitLab