From 426a81b7a023573cfc3ee8f2b5135291ba5153ae Mon Sep 17 00:00:00 2001 From: Amanda Ghassaei <amandaghassaei@gmail.com> Date: Mon, 9 Mar 2015 02:48:09 -0400 Subject: [PATCH] eod --- js/fea/DmaBeam.js | 27 ++++++++++++++++++++++----- js/fea/DmaCell.js | 21 ++++++++++++++------- js/fea/DmaNode.js | 4 ++++ js/menus/PartMenuView.js | 12 +++++++++--- js/models/Lattice.js | 7 ++++--- 5 files changed, 53 insertions(+), 18 deletions(-) diff --git a/js/fea/DmaBeam.js b/js/fea/DmaBeam.js index 4eaecfba..ebe433b1 100644 --- a/js/fea/DmaBeam.js +++ b/js/fea/DmaBeam.js @@ -5,11 +5,12 @@ Created by aghassaei on 1/13/15. //a single beam, made from two nodes -var unitGeo = new THREE.CylinderGeometry(0.1, 0.1, 1, 6); +var unitGeo = new THREE.CylinderGeometry(0.05, 0.05, 1, 6); -function DmaBeam(node1, node2) { +function DmaBeam(node1, node2, parent) { this.nodes = [node1, node2]; + this.parentCell = parent; var self = this; _.each(this.nodes, function(node){//give each node a reference to the new beam it is connected to node.addBeam(self); @@ -30,12 +31,28 @@ DmaBeam.prototype._buildBeamMesh = function(){ var position = this.nodes[0].getPosition(); position.sub(this.nodes[1].getPosition()); position.multiplyScalar(0.5); - mesh.position.set(position); + mesh.position.set(position.x, position.y, position.z); + var scale = this.parentCell.getScale(); + mesh.scale.set(scale, scale, scale); + dmaGlobals.three.sceneAdd(mesh, "part"); return mesh; }; -DmaBeam.prototype.render = function(scene){ - if (!this.mesh) this.mesh = this._buildBeamMesh(); +DmaBeam.prototype.setVisibility = function(visible){ + if (visible && !this.mesh) this.mesh = this._buildBeamMesh(); + else if (!this.mesh) return; + this.mesh.visible = visible; +}; + +DmaBeam.prototype.destroy = function(){ + dmaGlobals.three.sceneRemove(this.mesh, "part"); + this.mesh = null; + var self = this; + _.each(this.nodes, function(node){ + node.removeBeam(self); + }); + this.nodes = null; + this.parentCell = null; }; DmaBeam.prototype.calcStiffnessMatrix = function(){ diff --git a/js/fea/DmaCell.js b/js/fea/DmaCell.js index 35d1bd4b..dda72d77 100644 --- a/js/fea/DmaCell.js +++ b/js/fea/DmaCell.js @@ -15,20 +15,24 @@ function DMACell(indices, scale, inverse) { if (!inverse) inverse = false; this.isInverse = inverse; this.cellMesh = this._buildCellMesh(); - this.nodes = this._initNodes(_.clone(this.cellMesh.children[0].geometry.vertices)); + this.nodes = this._initNodes(this.cellMesh.children[0].geometry.vertices); this.beams = this._initBeams(this.nodes, this.cellMesh.children[0].geometry.faces); var cellMode = dmaGlobals.lattice.get("cellMode"); var inverseMode = dmaGlobals.lattice.get("inverseMode"); - this.drawForMode(scale, cellMode, inverseMode); + var beamMode = dmaGlobals.lattice.get("partType") == "beam"; + this.drawForMode(scale, cellMode, inverseMode, beamMode); } -DMACell.prototype.drawForMode = function(scale, cellMode, inverseMode){ +DMACell.prototype.drawForMode = function(scale, cellMode, inverseMode, beamMode){ this.updateForScale(scale, cellMode); this._setCellMeshVisibility(cellMode == "cell" && inverseMode==this.isInverse);//only show if in the correct inverseMode - if (cellMode == "part" && !this.parts) this.parts = this._initParts(); + if (!beamMode && cellMode == "part" && !this.parts) this.parts = this._initParts(); _.each(this.parts, function(part){ - if (part) part.setVisibility(cellMode == "part"); + if (part) part.setVisibility(cellMode == "part" && !beamMode); + }); + _.each(this.beams, function(beam){ + beam.setVisibility(beamMode); }); }; @@ -107,10 +111,12 @@ DMACell.prototype._initNodes = function(vertices){ var position = this.getPosition(); var orientation = this.getOrientation(); var nodes = []; + var scale = this.getScale(); for (var i=0;i<vertices.length;i++){ - var vertex = vertices[i]; + var vertex = vertices[i].clone(); vertex.applyQuaternion(orientation); vertex.add(position); + vertex.multiplyScalar(scale); nodes.push(new DmaNode(vertex, i)); } return nodes; @@ -187,6 +193,7 @@ DMACell.prototype.toJSON = function(){ DMAFaceOctaCell.prototype._initBeams = function(nodes, faces){ var beams = []; + var self = this; var addBeamFunc = function(index1, index2){ var duplicate = false; _.each(beams, function(beam){ @@ -195,7 +202,7 @@ DMACell.prototype.toJSON = function(){ }); if (duplicate) return; if (index2>index1) { - beams.push(new DmaBeam(nodes[index1], nodes[index2])); + beams.push(new DmaBeam(nodes[index1], nodes[index2], self)); } }; for (var i=0;i<nodes.length;i++){ diff --git a/js/fea/DmaNode.js b/js/fea/DmaNode.js index 69f0d3b8..9f52be4b 100644 --- a/js/fea/DmaNode.js +++ b/js/fea/DmaNode.js @@ -18,6 +18,10 @@ DmaNode.prototype.addBeam = function(beam){ this._beams.push(beam); }; +DmaNode.prototype.removeBeam = function(beam){ +// this._beams.push(beam); +}; + DmaNode.prototype.destroy = function(){ this._beams = null;//be sure to remove cyclic reference this.position = null; diff --git a/js/menus/PartMenuView.js b/js/menus/PartMenuView.js index 0f7c1339..18f1b009 100644 --- a/js/menus/PartMenuView.js +++ b/js/menus/PartMenuView.js @@ -7,7 +7,8 @@ PartMenuView = Backbone.View.extend({ el: "#menuContent", events: { - "slide #columnSepSlider": "_changeColSeparation" + "slide #columnSepSlider": "_changeColSeparation", + "click .partType": "_changePartType" }, initialize: function(options){ @@ -15,7 +16,7 @@ PartMenuView = Backbone.View.extend({ this.lattice = options.lattice; _.bindAll(this, "render"); - this.listenTo(this.model, "change:partType", this.render); + this.listenTo(this.lattice, "change:partType", this.render); }, @@ -23,9 +24,14 @@ PartMenuView = Backbone.View.extend({ this.lattice.set("columnSeparation", $(e.target).val()/100); }, + _changePartType: function(e){ + e.preventDefault(); + this.lattice.set("partType", $(e.target).data("type")); + }, + render: function(){ if (this.model.get("currentTab") != "part") return; - this.$el.html(this.template(this.model.attributes)); + this.$el.html(this.template(_.extend(this.model.attributes, this.lattice.attributes))); $('#columnSepSlider').slider({ formatter: function(value) { diff --git a/js/models/Lattice.js b/js/models/Lattice.js index 8de2aa73..c3dd52ed 100644 --- a/js/models/Lattice.js +++ b/js/models/Lattice.js @@ -41,7 +41,7 @@ Lattice = Backbone.Model.extend({ //bind events this.listenTo(this, "change:scale", this._scaleDidChange); - this.listenTo(this, "change:inverseMode change:cellMode", this._updateForMode); + this.listenTo(this, "change:inverseMode change:cellMode change:partType", this._updateForMode); this.listenTo(this, "change:cellType change:connectionType", this._updateLatticeType); }, @@ -334,12 +334,13 @@ Lattice = Backbone.Model.extend({ _updateForMode: function(){ var cellMode = this.get("cellMode"); var inverseMode = this.get("inverseMode"); + var beamMode = this.get("partType") == "beam"; var scale = this.get("scale"); this._iterCells(this.get("cells"), function(cell){ - if (cell) cell.drawForMode(scale, cellMode, inverseMode); + if (cell) cell.drawForMode(scale, cellMode, inverseMode, beamMode); }); this._iterCells(this.get("inverseCells"), function(cell){ - if (cell) cell.drawForMode(scale, cellMode, inverseMode); + if (cell) cell.drawForMode(scale, cellMode, inverseMode, beamMode); }); dmaGlobals.three.render(); }, -- GitLab