From a03cb89e9a0bfb5ebcd4a1fdd77bd39824c18760 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Fri, 30 Jan 2015 17:56:18 -0500
Subject: [PATCH] highlight parts

---
 js/fea/dmaCell.js          |  2 +-
 js/fea/dmaPart.js          | 62 ++++++++++++++++++++++++++++----------
 js/models/AppState.js      |  2 +-
 js/models/threeModel.js    |  1 +
 js/threeViews/threeView.js | 17 ++++++++---
 5 files changed, 62 insertions(+), 22 deletions(-)

diff --git a/js/fea/dmaCell.js b/js/fea/dmaCell.js
index 9690ce5f..e6a48f3e 100644
--- a/js/fea/dmaCell.js
+++ b/js/fea/dmaCell.js
@@ -98,7 +98,7 @@
             _.each(this.parts, function(part){
                 part.hide();
             });
-        } else if (mode == "parts"){
+        } else if (mode == "part"){
             this._setCellMeshVisibility(false);
             _.each(this.parts, function(part){
                 part.show();
diff --git a/js/fea/dmaPart.js b/js/fea/dmaPart.js
index 833117b3..dae36c09 100644
--- a/js/fea/dmaPart.js
+++ b/js/fea/dmaPart.js
@@ -7,20 +7,35 @@
 
 (function () {
 
-    var unitPartGeo;
+    var unitPartGeo1, unitPartGeo2, unitPartGeo3, unitPartGeo4, unitPartGeo5,unitPartGeo6;
 
     //import part geometry
     var loader = new THREE.STLLoader();
     loader.load("data/trianglePart.stl", function(geometry){
         console.log("part loaded");
-        unitPartGeo = geometry
-        unitPartGeo.dynamic = true;
-        unitPartGeo.computeBoundingBox();
-        var unitScale = 1/unitPartGeo.boundingBox.max.y;
-        unitPartGeo.applyMatrix(new THREE.Matrix4().makeScale(unitScale, unitScale, unitScale));
-        unitPartGeo.applyMatrix(new THREE.Matrix4().makeTranslation(0.2,-0.5, 0));
-        unitPartGeo.applyMatrix(new THREE.Matrix4().makeRotationZ(-Math.PI/6));
-        unitPartGeo.dynamic = true;
+        unitPartGeo1 = geometry
+        unitPartGeo1.dynamic = true;
+        unitPartGeo1.computeBoundingBox();
+        var unitScale = 1/unitPartGeo1.boundingBox.max.y;
+        unitPartGeo1.applyMatrix(new THREE.Matrix4().makeScale(unitScale, unitScale, unitScale));
+        unitPartGeo1.applyMatrix(new THREE.Matrix4().makeTranslation(0.2,-0.5, 0));
+        unitPartGeo1.applyMatrix(new THREE.Matrix4().makeRotationZ(-Math.PI/6));
+        unitPartGeo1.dynamic = true;
+        
+        unitPartGeo2 = unitPartGeo1.clone();
+        unitPartGeo2.applyMatrix(new THREE.Matrix4().makeRotationZ(2*Math.PI/3));
+
+        unitPartGeo3 = unitPartGeo1.clone();
+        unitPartGeo3.applyMatrix(new THREE.Matrix4().makeRotationZ(-2*Math.PI/3));
+        
+        unitPartGeo4 = unitPartGeo1.clone();
+        unitPartGeo4.applyMatrix(new THREE.Matrix4().makeRotationZ(Math.PI));
+
+        unitPartGeo5 = unitPartGeo2.clone();
+        unitPartGeo5.applyMatrix(new THREE.Matrix4().makeRotationZ(Math.PI));
+
+        unitPartGeo6 = unitPartGeo3.clone();
+        unitPartGeo6.applyMatrix(new THREE.Matrix4().makeRotationZ(Math.PI));
     });
 
     var partMaterial = new THREE.MeshLambertMaterial({ color:0xffffff, shading: THREE.FlatShading });
@@ -39,19 +54,24 @@
 
     DMAPart.prototype._makeMeshForType = function(type){
 
-        var mesh = new THREE.Mesh(unitPartGeo, partMaterial);
-        mesh = this._setMeshPosition(mesh);
-        mesh = this._setMeshScale(mesh);
-
+        var mesh;
         switch(type){
+            case 0:
+                if (this.oddZFlag) mesh = new THREE.Mesh(unitPartGeo4, partMaterial.clone());
+                else mesh = new THREE.Mesh(unitPartGeo1, partMaterial.clone());
+                break;
             case 1:
-                mesh.rotateZ(2*Math.PI/3);
+                if (this.oddZFlag) mesh = new THREE.Mesh(unitPartGeo5, partMaterial.clone());
+                else mesh = new THREE.Mesh(unitPartGeo2, partMaterial.clone());
                 break;
             case 2:
-                mesh.rotateZ(-2*Math.PI/3);
+                if (this.oddZFlag) mesh = new THREE.Mesh(unitPartGeo6, partMaterial.clone());
+                else mesh = new THREE.Mesh(unitPartGeo3, partMaterial.clone());
                 break;
         }
-        if (this.oddZFlag) mesh.rotateZ(Math.PI);
+
+        mesh = this._setMeshPosition(mesh);
+        mesh = this._setMeshScale(mesh);
         return mesh;
     };
 
@@ -65,6 +85,7 @@
         if (this.oddZFlag){//adjust some offsets for odd z layers
             mesh.position.y += 7*scale/6;
         }
+        mesh.myPart = this;//need a ref back to this part
         return mesh;
     };
 
@@ -91,9 +112,18 @@
         if (this.mesh) this.mesh.visible = false;
     };
 
+    DMAPart.prototype.highlight = function(){
+        this.mesh.material.color.setRGB(1,0,0);
+    };
+
+    DMAPart.prototype.unhighlight = function(){
+        this.mesh.material.color.setRGB(0.9619657144369509, 0.6625466032079207, 0.20799727886007258);
+    };
+
     DMAPart.prototype.destroy = function(){
         if (this.mesh) {
             window.three.sceneRemove(this.mesh, "part");
+            this.mesh.myPart = null;
 //            this.mesh.dispose();
 //            geometry.dispose();
 //            material.dispose();
diff --git a/js/models/AppState.js b/js/models/AppState.js
index 7f38eb9d..1442e706 100644
--- a/js/models/AppState.js
+++ b/js/models/AppState.js
@@ -51,7 +51,7 @@ AppState = Backbone.Model.extend({
         if (currentTab == "lattice") this.lattice.set("cellMode", "cell");
         else if (currentTab == "import") this.lattice.set("cellMode", "cell");
         else if (currentTab == "sketch") this.lattice.set("cellMode", "cell");
-        else if (currentTab == "part") this.lattice.set("cellMode", "parts");
+        else if (currentTab == "part") this.lattice.set("cellMode", "part");
     },
 
     //update to last tab open in that section
diff --git a/js/models/threeModel.js b/js/models/threeModel.js
index cc38a6c7..bcc51204 100644
--- a/js/models/threeModel.js
+++ b/js/models/threeModel.js
@@ -101,6 +101,7 @@ function ThreeModel(){
         render: render,
         sceneRemove: sceneRemove,
         sceneAdd: sceneAdd,
+//        scene: scene,
         domElement: renderer.domElement,
         camera: camera,
         cells: cells,
diff --git a/js/threeViews/threeView.js b/js/threeViews/threeView.js
index 6e5c4774..23498dfc 100644
--- a/js/threeViews/threeView.js
+++ b/js/threeViews/threeView.js
@@ -104,6 +104,7 @@ ThreeView = Backbone.View.extend({
         var cellIntersections = this.mouseProjection.intersectObjects(this.model.cells.concat(this.model.basePlane), true);
         if (cellIntersections.length == 0) {
             this._setNoCellIntersections();
+            this._setNoPartIntersections();
             return;
         }
         this._handleCellIntersections(cellIntersections);
@@ -120,17 +121,25 @@ ThreeView = Backbone.View.extend({
 
     _setNoCellIntersections: function(){
         this.currentIntersectedCell = null;
-        this.currentIntersectedPart = null;
         this._hideHighlighter();
     },
 
     _setNoPartIntersections: function(){
-        this.currentIntersectedPart = null;
+        if (this.currentIntersectedPart){
+            this.currentIntersectedPart.unhighlight();
+            this.currentIntersectedPart = null;
+            window.three.render();
+        }
     },
 
     _handlePartIntersections: function(intersections){
-
-
+        var part = intersections[0].object.myPart;
+        if (part!= this.currentIntersectedPart){
+            if (this.currentIntersectedPart) this.currentIntersectedPart.unhighlight();
+            part.highlight();
+            this.currentIntersectedPart = part;
+            window.three.render();
+        }
     },
 
     _handleCellIntersections: function(intersections){
-- 
GitLab