From 2ceb0dc7230a6d5e156d66c8c8f9e3c05557a7f6 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Fri, 16 Jan 2015 19:29:06 -0500
Subject: [PATCH] working on bbox

---
 js/main.js                |  8 ------
 js/models/fillGeometry.js | 56 +++++++++++++++++++++++++++++----------
 js/views/importView.js    | 12 ++++++---
 3 files changed, 51 insertions(+), 25 deletions(-)

diff --git a/js/main.js b/js/main.js
index f0c1f4df..277bebf3 100644
--- a/js/main.js
+++ b/js/main.js
@@ -6,14 +6,6 @@
 $(function(){
 
     three = Three();
-    _.extend(three, Backbone.Events);
-    three.on("threeRender", three.render);
-    three.on("threeAdd", function(object){
-        three.scene.add(object);
-    });
-    three.on("threeRemove", function(object){
-        three.scene.remove(object);
-    });
 
     //init models and views
     window.fillGeometry = new FillGeometry();//init a singleton, add to global scope
diff --git a/js/models/fillGeometry.js b/js/models/fillGeometry.js
index bf618702..1fd53849 100644
--- a/js/models/fillGeometry.js
+++ b/js/models/fillGeometry.js
@@ -14,36 +14,64 @@ FillGeometry = Backbone.Model.extend({
                 opacity:0.5,
                 side:THREE.DoubleSide}),
         geometry: new THREE.BoxGeometry(100, 100, 100),
-        filename: "no file loaded",
-        dimensions: "",
-        orientation: ""
+        filename: "Cube",
+        orientation: "",
     },
 
     initialize: function(){
 
-        this.buildNewMesh();
-        this.renderMesh();
-
         //bind events
-        this.on("change:mesh change:scale change:orientation", this.renderMesh);
+        this.on("change:mesh", this.getBounds);
+        this.on("change:mesh", this.makeBoundingBoxHelper);
+        this.on("change:scale change:orientation", this.updateBoundingBox);
         this.on("change:geometry", this.buildNewMesh);
+        this.on("change:mesh change:scale change:orientation", this.render);
+
+        this.buildNewMesh();
     },
 
     buildNewMesh:function(){
 
-        three.trigger("threeRemove", this.previous("mesh"));//remove current mesh from scene
+        if (this.previous("mesh")) three.scene.remove(this.previous("mesh"));//remove current mesh from scene
         this.set(_.omit(this.defaults, ["geometry", "material"]), {silent:true});//restore defaults
-        this.set(
-            {mesh: new THREE.Mesh(this.get("geometry"), this.get("material"))});
+        var mesh = new THREE.Mesh(this.get("geometry"), this.get("material"))
+        three.scene.add(mesh);
+        this.set({mesh: mesh});
+
+        //send new geometry out to workers
 //            _.each(workers.allWorkers, function(worker){
-//                worker.postMessage({model: JSON.stringify(e.content)});
+//                worker.postMessage({model: this.toJSON});
 //            });
     },
 
-    renderMesh: function(){
+    getBounds: function(){//bounds is the bounding box of the mesh geometry (before scaling)
+//        this.get("bounds").setFromObject(this.get("mesh"));
+        this.get("mesh").geometry.computeBoundingBox();
+        this.set("bounds", this.get("geometry").boundingBox.clone());
+    },
+
+    makeBoundingBoxHelper: function(){
+        var helper = new THREE.BoundingBoxHelper(this.get("mesh"), 0x000000);
+        this.set("boundingBoxHelper", helper);
+        three.scene.add(helper.object);
+    },
+
+    updateBoundingBox: function(){
+        this.get("boundingBoxHelper").update();
+
+//        var boundingBox = this.get("geometry").boundingBox;
+//        if (!boundingBox){
+//            console.log("no bb");
+//        }
+    },
+
+    updateBoundingBox:function(){
+
+    },
+
+    render: function(){
         console.log("renderfillgeo");
-        three.trigger("threeAdd", this.get("mesh"));
-        three.trigger("threeRender");
+        three.render();
     },
 
     rotate: function(axis){
diff --git a/js/views/importView.js b/js/views/importView.js
index 0f11e67e..940723c3 100644
--- a/js/views/importView.js
+++ b/js/views/importView.js
@@ -65,6 +65,12 @@ ImportView = Backbone.View.extend({
         this.model.set("scale", $(e.target).slider('getValue'));
     },
 
+    makeDimensionString: function(){
+//        var boundingBox = this.model.get("boundingBox");
+//        return ((boundingBox.max.x - boundingBox.min.x)*scale).toFixed(1) + " x " +
+//            ((boundingBox.max.y - boundingBox.min.y)*scale).toFixed(1) + " x " + ((boundingBox.max.z - boundingBox.min.z)*scale).toFixed(1);
+    },
+
     rotate: function(e){
         e.preventDefault();
         var axis = $(e.target).data("axis");
@@ -74,7 +80,7 @@ ImportView = Backbone.View.extend({
     render: function(){
 
         console.log("renderView");
-        this.$el.html(this.template(this.model.attributes));
+        this.$el.html(this.template(_.extend(this.model.attributes, {dimensions:this.makeDimensionString()})));
 
 //        $(".slider").slider({//format slider
 //            formatter: function(value) {
@@ -105,8 +111,8 @@ ImportView = Backbone.View.extend({
             </div>\
             </div> <!-- /.col-xs-3 -->\
             <div class="col-xs-9">\
-                Current File:&nbsp;&nbsp;<%= filename %><br/>\
-                <%= dimensions %>\
+                Geometry:&nbsp;&nbsp;<%= filename %><br/>\
+                Dimensions:&nbsp;&nbsp;<%= dimensions %>\
                 <div>Change model scale:</div>\
                 <div class="col-xs-4">\
                     <a href="#" data-axis="z" class="stlRotate btn btn-block btn-lg btn-default">Rotate X</a>\
-- 
GitLab