From 438c42b55a34f5f4f244d59078c4b524acc587c6 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Wed, 21 Jan 2015 17:09:20 -0500
Subject: [PATCH] face connected octahedra lattice

---
 js/fea/dmaNode.js          | 26 ++++++++++++++---
 js/main.js                 |  9 +++---
 js/models/lattice.js       | 57 +++++++++++++++++++++++++++++++++++---
 js/threeViews/threeView.js | 46 +++++++++++++++---------------
 main.html                  |  6 ++++
 5 files changed, 109 insertions(+), 35 deletions(-)

diff --git a/js/fea/dmaNode.js b/js/fea/dmaNode.js
index 90823453..a77b2a80 100644
--- a/js/fea/dmaNode.js
+++ b/js/fea/dmaNode.js
@@ -1,14 +1,24 @@
 //a node, two for each dmaBeam, not to be confused with node.js
 
-function BeamNode() {
+function BeamNode(x, y, z) {
     this._beams = [];//store all beams attached to this node, eventually this will be used to calc global stiffness K
+    this.x = x;
+    this.y = y;
+    this.z = z;
+    this.render();
 }
 
 BeamNode.prototype.addBeam = function(beam){
     this._beams.push(beam);
 };
 
-BeamNode.prototype.render = function(scene){
+BeamNode.prototype.render = function(){
+
+    var geometry = new THREE.SphereGeometry(2);
+    geometry.applyMatrix( new THREE.Matrix4().makeTranslation(this.x, this.y, this.z) );
+    var mesh = new THREE.Mesh(geometry);
+
+    window.three.sceneAdd(mesh);
 };
 
 
@@ -16,12 +26,20 @@ BeamNode.prototype.deflect = function(dx, dy, dz){
 };
 
 BeamNode.prototype.translate = function(dx, dy, dz){
+    this.x += dx;
+    this.y += dy;
+    this.z += dz;
+};
+
+BeamNode.prototype.translateAbsolute = function(x, y, z){
+    this.x = x;
+    this.y = y;
+    this.z = z;
 };
 
 BeamNode.prototype.rotate = function(rx, ry, rz){
 };
 
 BeamNode.prototype.destroy = function(){
-    this._beam = null;//be sure to remove cyclic reference
-    this = null;
+    this._beams = null;//be sure to remove cyclic reference
 };
diff --git a/js/main.js b/js/main.js
index b0c8ad76..9b9db456 100644
--- a/js/main.js
+++ b/js/main.js
@@ -10,15 +10,16 @@ $(function(){
 
     //init threeJS
     var threeModel = new ThreeModel();
+    window.three = threeModel;
 
     //backbone models and views
     var fillGeometry = new FillGeometry();//singleton, mesh to fill with lattice
-    new ImportView({model: fillGeometry});
-    var fillGeometryView = new FillGeometryView({model: fillGeometry, three:threeModel});
+//    new ImportView({model: fillGeometry});
+//    var fillGeometryView = new FillGeometryView({model: fillGeometry, three:threeModel});
 
 
-    var lattice = new Lattice();
-    var latticeView = new LatticeView({model:lattice, three:threeModel, fillGeometry:fillGeometry});
+    var lattice = new Lattice({fillGeometry:fillGeometry});
+    var latticeView = new LatticeView({model:lattice, three:threeModel});
 
     var highlightTargets = [latticeView];
 
diff --git a/js/models/lattice.js b/js/models/lattice.js
index 311c65e3..1d87d74b 100644
--- a/js/models/lattice.js
+++ b/js/models/lattice.js
@@ -6,21 +6,70 @@
 Lattice = Backbone.Model.extend({
 
     defaults: {
-        scale: 1.0,
+        scale: 30.0,
         translation: new THREE.Vector3(0,0,0),
         rotation: new THREE.Vector3(0,0,0),
-        type: "cube"
+        type: "octagonFace",
+        nodes: [],
+        min: new THREE.Vector3(0,0,0),
+        max: new THREE.Vector3(0,0,0)
     },
 
+    //pass in fillGeometry
+
     initialize: function(){
 
+        //bind events
 
+        this.layoutNodes();
 
+    },
 
-        //bind events
+    layoutNodes: function(type){
+        type = type || this.get("type");
+        var boundingBox = this.get("fillGeometry").get("boundingBoxHelper").box;
+        var scale = this.get("scale");
 
-    }
+        var nodes;
+
+        if (type == "octagonFace"){
+            nodes = this.faceConnectedOctahedra(boundingBox, scale);
 
 
+        } else {
+            console.log("no type");
+        }
+        this.set("nodes", nodes);
+    },
+
+    faceConnectedOctahedra: function(boundingBox, scale){
+
+        var scaleSqrt3 = scale/2*Math.sqrt(3);
+        var dimX = Math.round((boundingBox.max.x-boundingBox.min.x)/scale);
+        var dimY = Math.round((boundingBox.max.y-boundingBox.min.y)/scaleSqrt3);
+        var dimZ = Math.round((boundingBox.max.z-boundingBox.min.z)/scaleSqrt3);
+
+
+        var nodes = [];
+
+        for (var x=0; x<=dimX; x++){
+            if (nodes.length<=x) nodes[x] = [];
+            for (var y=0; y<=dimY; y++){
+                if (nodes[x].length<=y) nodes[x][y] = [];
+                for (var z=0; z<=dimZ; z++){
+
+                    var xOffset = 0;
+                    if (y%2 == 1) xOffset = scale/2;
+                    var yOffset = 0;
+                    if (z%2 == 1) yOffset  = scaleSqrt3/2;
+
+                    nodes[x][y][z] = new BeamNode(boundingBox.min.x+ xOffset + scale*x,
+                        boundingBox.min.y + yOffset + scaleSqrt3*y,
+                        boundingBox.min.z + z*scaleSqrt3);
+                }
+            }
+        }
+        return nodes;
+    }
 
 });
\ No newline at end of file
diff --git a/js/threeViews/threeView.js b/js/threeViews/threeView.js
index 53ce9bfa..834ed7ed 100644
--- a/js/threeViews/threeView.js
+++ b/js/threeViews/threeView.js
@@ -48,32 +48,32 @@ ThreeView = Backbone.View.extend({
 
     mouseDown: function(e){
         this.mouseIsDown = true;
-
-        var vector = new THREE.Vector2(2*(e.pageX-this.$el.offset().left)/this.$el.width()-1, 1-2*(e.pageY-this.$el.offset().top)/this.$el.height());
-        var camera = this.model.camera;
-        this.mouseProjection.setFromCamera(vector, camera);
-        var intersections = this.mouseProjection.intersectObjects(this.model.objects);
-
-                console.log(intersections);
-
-        if (intersections.length>1){
-            var voxel = new THREE.Mesh(this.cubeGeometry);
-            voxel.position.copy(intersections[1].point);
-            if (intersections[1].face) voxel.position.add(intersections[1].face.normal);
-            voxel.position.divideScalar(5).floor().multiplyScalar(5).addScalar(2.5);
-            this.model.sceneAdd(voxel);
-            this.model.render();
-        }
+//
+//        var vector = new THREE.Vector2(2*(e.pageX-this.$el.offset().left)/this.$el.width()-1, 1-2*(e.pageY-this.$el.offset().top)/this.$el.height());
+//        var camera = this.model.camera;
+//        this.mouseProjection.setFromCamera(vector, camera);
+//        var intersections = this.mouseProjection.intersectObjects(this.model.objects);
+//
+//                console.log(intersections);
+//
+//        if (intersections.length>1){
+//            var voxel = new THREE.Mesh(this.cubeGeometry);
+//            voxel.position.copy(intersections[1].point);
+//            if (intersections[1].face) voxel.position.add(intersections[1].face.normal);
+//            voxel.position.divideScalar(5).floor().multiplyScalar(5).addScalar(2.5);
+//            this.model.sceneAdd(voxel);
+//            this.model.render();
+//        }
     },
 
     mouseMoved: function(e){
-        if (this.mouseIsDown) return;//in the middle of a drag event
-        var vector = new THREE.Vector2(2*(e.pageX-this.$el.offset().left)/this.$el.width()-1, 1-2*(e.pageY-this.$el.offset().top)/this.$el.height());
-        var camera = this.model.camera;
-        this.mouseProjection.setFromCamera(vector, camera);
-        var intersections = this.mouseProjection.intersectObjects(this.model.objects);
-
-
+//        if (this.mouseIsDown) return;//in the middle of a drag event
+//        var vector = new THREE.Vector2(2*(e.pageX-this.$el.offset().left)/this.$el.width()-1, 1-2*(e.pageY-this.$el.offset().top)/this.$el.height());
+//        var camera = this.model.camera;
+//        this.mouseProjection.setFromCamera(vector, camera);
+//        var intersections = this.mouseProjection.intersectObjects(this.model.objects);
+//
+//
 //        _.each(this.highlightTargets, function(target){
 //            target.checkHighlight(intersections);
 //        });
diff --git a/main.html b/main.html
index c4dad7a5..30e987c3 100644
--- a/main.html
+++ b/main.html
@@ -39,6 +39,12 @@
     <script src="js/worker.js"></script>
     <script src="js/persistentWorkers.js"></script><!--global workers-->
 
+    <!--fea stuff-->
+    <script src="js/fea/dmaCell.js"></script>
+    <!--<script src="js/fea/dmaPart.js"></script>-->
+    <!--<script src="js/fea/dmaBeam.js"></script>-->
+    <script src="js/fea/dmaNode.js"></script>
+
     <!--models-->
     <script src="js/models/threeModel.js"></script>
     <script src="js/models/fillGeometry.js"></script>
-- 
GitLab