From d9ae92e6bf466159b3eab4c071195d07c1134b43 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Thu, 5 Feb 2015 23:11:23 -0500
Subject: [PATCH] cut with terrain

---
 js/fea/DmaCell.js          |  8 ++---
 js/menus/ImportMenuView.js | 23 +++++++++++--
 js/models/FillGeometry.js  | 14 ++++++--
 js/models/Lattice.js       | 69 +++++++++++++++++++++++++++++++++++---
 4 files changed, 97 insertions(+), 17 deletions(-)

diff --git a/js/fea/DmaCell.js b/js/fea/DmaCell.js
index b1bf66f9..7ffd3853 100644
--- a/js/fea/DmaCell.js
+++ b/js/fea/DmaCell.js
@@ -65,7 +65,7 @@ DMACell.prototype.updateForScale = function(scale){
 };
 
 DMACell.prototype.getScale = function(){//need for part relay
-    return this.lattice.get("scale");
+    return this.lattice.getScale();
 };
 
 DMACell.prototype.getPosition = function(){//need for part relay
@@ -118,7 +118,7 @@ DMACell.prototype.destroy = function(){
     DMASideOctaCell.prototype = Object.create(DMACell.prototype);
 
     DMASideOctaCell.prototype._calcPosition = function(scale, indices){
-        var latticeScale = this.getLatticeScale();
+        var latticeScale = this.lattice.getScale();
         var position = {};
         var octHeight = 2*scale/Math.sqrt(6);
         var triHeight = latticeScale/2*Math.sqrt(3);
@@ -129,10 +129,6 @@ DMACell.prototype.destroy = function(){
         return position;
     };
 
-    DMACell.prototype.getLatticeScale = function(){
-        return this.lattice.get("scale")*(1.0+2*this.lattice.get("columnSeparation"));
-    };
-
     DMASideOctaCell.prototype._initParts = function(zIndex){
         var parts  = [];
         for (var i=0;i<3;i++){
diff --git a/js/menus/ImportMenuView.js b/js/menus/ImportMenuView.js
index b16f4257..e8a6f025 100644
--- a/js/menus/ImportMenuView.js
+++ b/js/menus/ImportMenuView.js
@@ -6,18 +6,21 @@
 ImportMenuView = Backbone.View.extend({
 
     el: "#menuContent",
-    model: new FillGeometry(),
+    model: null,
 
     events: {
         "change #uploadMesh":               "_uploadMesh",
         "click .selectMesh":                "_selectMesh",
         "fileselect .btn-file :file":       "_readDataURL",
         "click #removeFillGeo":             "_removeMesh",
-        "click #selectWall":                "_buildWall"
+        "click #selectWall":                "_buildWall",
+        "click #doSubtractGeo":             "_subtractGeo",
+        "click #doFillGeo":                 "_fillGeo"
     },
 
     initialize: function(options){
 
+        this.model = new FillGeometry({lattice:options.lattice}),
         this.lattice = options.lattice;
         this.appState = options.appState;
 
@@ -34,7 +37,7 @@ ImportMenuView = Backbone.View.extend({
 
     _buildWall: function(e){
         e.preventDefault();
-        this.lattice.addCellsInRange({min:{x:-100,y:-1,z:0}, max:{x:100,y:2,z:18}});
+        this.lattice.addCellsInRange({min:{x:-70,y:-1,z:0}, max:{x:70,y:2,z:18}});
     },
 
     _uploadMesh: function(e){//select a mesh to upload
@@ -67,6 +70,16 @@ ImportMenuView = Backbone.View.extend({
         });
     },
 
+    _subtractGeo: function(e){
+        e.preventDefault();
+        this.model.subtractGeo();
+    },
+
+    _fillGeo: function(e){
+        e.preventDefault();
+
+    },
+
     _removeMesh: function(e){
         e.preventDefault();
         this.model.remove();
@@ -115,7 +128,11 @@ ImportMenuView = Backbone.View.extend({
         <% if (mesh){ %>\
         Rotate:<br/>\
         Scale:<br/><br/>\
+        <% if (isLandscape){ %>\
+        <a href="#" id="doSubtractGeo" class=" btn btn-block btn-lg btn-default">Subtract Mesh</a><br/>\
+        <% } else { %>\
         <a href="#" id="doFllGeo" class=" btn btn-block btn-lg btn-default">Fill Mesh</a><br/>\
+        <% } %>\
         <a href="#" id="removeFillGeo" class=" btn btn-block btn-lg btn-default">Remove Mesh</a><br/>\
         <hr>\
         <% } %>\
diff --git a/js/models/FillGeometry.js b/js/models/FillGeometry.js
index bc7cd763..0aad2f7d 100644
--- a/js/models/FillGeometry.js
+++ b/js/models/FillGeometry.js
@@ -10,15 +10,19 @@ FillGeometry = Backbone.Model.extend({
             {color:0xf25536,
                 shading: THREE.FlatShading,
                 transparent:true,
-                opacity:0.2}),
+                opacity:0.4,
+                side:THREE.DoubleSide}),
         geometry: null,
         filename: "No File Loaded",
         orientation: [0,0,0],
         scale: [1.0,1.0,1.0],
-        mesh: null
+        mesh: null,
+        isLandscape: true
     },
 
-    initialize: function(){
+    initialize: function(options){
+
+        this.lattice = options.lattice;
 
         //bind events
         this.on("change:mesh", this.getBounds);
@@ -64,6 +68,10 @@ FillGeometry = Backbone.Model.extend({
 //        this.trigger("change:boundingBoxHelper");
     },
 
+    subtractGeo: function(){
+        this.lattice.subtractMesh(this.get("mesh"));
+    },
+
     remove: function(){
         if (!this.get("mesh")) return;
         window.three.sceneRemove(this.get("mesh"), null);
diff --git a/js/models/Lattice.js b/js/models/Lattice.js
index 7a0680c0..4f89f69b 100644
--- a/js/models/Lattice.js
+++ b/js/models/Lattice.js
@@ -56,14 +56,15 @@ Lattice = Backbone.Model.extend({
         var cells = this.get("cells");
         this._checkForMatrixExpansion(cells, range.max, range.min);
 
-        var relativeMin = this._subtract(range.min, this.get("cellsMin"));
+        var cellsMin = this.get("cellsMin");
+        var relativeMin = this._subtract(range.min, cellsMin);
         var relativeMax = this._subtract(range.max, this.get("cellsMin"));
 
-        for (var x=relativeMin.x;x<relativeMax.x;x++){
-            for (var y=relativeMin.y;y<relativeMax.y;y++){
-                for (var z=relativeMin.z;z<relativeMax.z;z++){
+        for (var x=relativeMin.x;x<=relativeMax.x;x++){
+            for (var y=relativeMin.y;y<=relativeMax.y;y++){
+                for (var z=relativeMin.z;z<=relativeMax.z;z++){
                     if (!cells[x][y][z]) {
-                        cells[x][y][z] = this._makeCellForLatticeType(this._add({x:x, y:y, z:z}, range.min), scale);
+                        cells[x][y][z] = this._makeCellForLatticeType(this._add({x:x, y:y, z:z}, cellsMin), scale);
                         this.set("numCells", this.get("numCells")+1);
                     } else console.warn("already a cell there");
                 }
@@ -113,6 +114,60 @@ Lattice = Backbone.Model.extend({
         window.three.render();
     },
 
+    ////////////////////////////////////////////////////////////////////////////////////
+    ///////////////////////////////FILL GEOMETRY////////////////////////////////////////
+    ////////////////////////////////////////////////////////////////////////////////////
+
+    subtractMesh: function(mesh){
+
+        var scale = this.getScale();
+        var yscale = scale/2*Math.sqrt(3);
+        var zScale = this.get("scale")*2/Math.sqrt(6);
+
+        var cells = this.get("cells");
+        var cellsMin = this.get("cellsMin");
+
+        var allVertexPos = mesh.geometry.attributes.position.array;
+
+        var zHeight;
+        for (var x=0;x<cells.length;x++){
+            for (var y=0;y<cells[0].length;y++){
+                var firstCell = null;
+                for (var z=0;z<cells[0][0].length;z++){
+                    firstCell = cells[x][y][z];
+                    if (firstCell) break;
+                }
+                if (!firstCell) continue;//nothing in col
+
+                var origin = firstCell._calcPosition(0, this._add({x:x,y:y,z:z}, cellsMin));
+                zHeight = this._findIntersectionsInWindow(scale/2, yscale/2, origin, allVertexPos) || zHeight;
+
+//                if (!zHeight) zHeight = this._findIntersectionsInWindow(scale, yscale, origin, allVertexPos);
+//                if (!zHeight) zHeight = this._findIntersectionsInWindow(scale, 4*yscale, 4*origin, allVertexPos);
+
+                zHeight = Math.floor(zHeight/zScale);
+                for (var z=0;z<zHeight;z++){
+                    cells[x][y][z].destroy();
+                    cells[x][y][z] = null;
+                }
+            }
+
+        }
+        console.log("done");
+        window.three.render();
+    },
+
+    _findIntersectionsInWindow: function(windowX, windowY, origin, allVertexPos){
+        for (var i=0;i<allVertexPos.length;i+=3){
+            if (allVertexPos[i] > origin.x-windowX && allVertexPos[i] < origin.x+windowX
+                && allVertexPos[i+1] > origin.y-windowY && allVertexPos[i+1] < origin.y+windowY){
+                return allVertexPos[i+2];
+            }
+        }
+        return null
+    },
+
+
     ////////////////////////////////////////////////////////////////////////////////////
     ///////////////////////////////CELLS ARRAY//////////////////////////////////////////
     ////////////////////////////////////////////////////////////////////////////////////
@@ -287,6 +342,10 @@ OctaFaceLattice = Lattice.extend({
         this.set("columnSeparation", 0.0);
     },
 
+    getScale: function(){
+        return this.get("scale")*(1.0+2*this.get("columnSeparation"));
+    },
+
     _changeColSeparation: function(){
         var colSep = this.get("columnSeparation");
         var scale = this.get("scale");
-- 
GitLab