From 81274c7a3bcf0bca40b6c60db1554b5f7c464df3 Mon Sep 17 00:00:00 2001 From: Amanda Ghassaei <amandaghassaei@gmail.com> Date: Mon, 23 Feb 2015 17:52:40 -0500 Subject: [PATCH] square faces --- js/models/BasePlane.js | 76 +++++++++++++++++++++++++----------- js/models/Lattice.js | 69 ++++++++++++++++++++++++-------- js/threeViews/Highlighter.js | 29 ++++++++++---- 3 files changed, 128 insertions(+), 46 deletions(-) diff --git a/js/models/BasePlane.js b/js/models/BasePlane.js index 99cfa238..c49bafe4 100644 --- a/js/models/BasePlane.js +++ b/js/models/BasePlane.js @@ -11,35 +11,23 @@ BasePlane = Backbone.Model.extend({ dimX: 100, dimY: 100, material: new THREE.MeshBasicMaterial({color:0x000000, transparent:true, opacity:0.2, wireframe:true, side:THREE.DoubleSide}), - currentScene: "default", - allScenes: {default:"Default", "mars":"Mars"} +// currentScene: "default", +// allScenes: {default:"Default", "mars":"Mars"} }, initialize: function(options){ //bind events - this.listenTo(this, "change:currentScene", this._renderForCurrentScene); +// this.listenTo(this, "change:currentScene", this._renderForCurrentScene); this.listenTo(this, "change:zIndex", this._renderZIndexChange); //draw mesh this.set("mesh", this._makeBasePlaneMesh()); this.updateScale(options.scale); - this._showMesh(); + this._showMesh();//do this once }, - _renderZIndexChange: function(){ - var zIndex = this.get("zIndex"); - var scale = this.get("mesh").scale.z; - _.each(this.get("mesh"), function(mesh){ - mesh.position.set(0, 0, zIndex*scale*2/Math.sqrt(6)); - }); - window.three.render(); - }, - - _renderForCurrentScene: function(){ - }, - updateScale: function(scale){ _.each(this.get("mesh"), function(mesh){ mesh.scale.set(scale, scale, scale); @@ -54,24 +42,50 @@ BasePlane = Backbone.Model.extend({ window.three.render(); }, - _removeMesh: function(){ - var self = this; + _renderZIndexChange: function(){ + var zIndex = this.get("zIndex"); + var scale = this.get("mesh").scale.z; _.each(this.get("mesh"), function(mesh){ - if (mesh.myParent) mesh.myParent = null; - window.three.sceneRemove(mesh, self._checkIsHighlightable(mesh)); + mesh.position.set(0, 0, zIndex*scale*2/Math.sqrt(6)); }); window.three.render(); }, +// _renderForCurrentScene: function(){ +// }, + + /////////////////////////////////////////////////////////////////////////////////// + //////////////////////HIGHLIGHTER FUNCTIONALITY//////////////////////////////////// + /////////////////////////////////////////////////////////////////////////////////// + _checkIsHighlightable: function(mesh){ if (mesh.type == "Mesh") return "basePlane"; return null; }, + getIndex: function(face){ + return window.lattice.getIndexForPosition(face.geometry.vertices[0]); + }, + canRemove: function(){ return false;//tells highlighter that the baseplane is not something to be deleted }, + //subclasses handle getHighlighterVertices + + /////////////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////DEALLOC//////////////////////////////////// + /////////////////////////////////////////////////////////////////////////////////// + + _removeMesh: function(){ + var self = this; + _.each(this.get("mesh"), function(mesh){ + if (mesh.myParent) mesh.myParent = null; + window.three.sceneRemove(mesh, self._checkIsHighlightable(mesh)); + }); + window.three.render(); + }, + destroy: function(){ this.stopListening(); this.set("zIndex", null, {silent:true}); @@ -179,8 +193,8 @@ OctaBasePlane = BasePlane.extend({ return newVertices; }, - getIndex: function(face){ - return window.lattice.getIndexForPosition(face.geometry.vertices[0]); + highlighterFaces: function(){ + return [new THREE.Face3(0,1,2), new THREE.Face3(0,0,0)]; } }); @@ -218,6 +232,24 @@ SquareBasePlane = BasePlane.extend({ var mesh = new THREE.Mesh(planeGeometry, new THREE.MeshBasicMaterial({color:0x000000, transparent:true, opacity:0.0, side:THREE.DoubleSide})); mesh.myParent = this;//reference used for intersection highlighting return [mesh, new THREE.Line(geometry, new THREE.LineBasicMaterial({color:0x000000, transparent:true, linewidth:1, opacity:this.get("material").opacity}), THREE.LinePieces)]; + }, + + getHighlighterVertices: function(face, position){ + //the vertices don't include the position transformation applied to cell. Add these to create highlighter vertices + var index = window.lattice.getIndexForPosition(position); + var scale = this.get("mesh")[0].scale.x; + var vertices = []; + vertices.push(new THREE.Vector3(index.x*scale, index.y*scale, index.z*scale)); + vertices.push(new THREE.Vector3((index.x+1)*scale, index.y*scale, index.z*scale)); + vertices.push(new THREE.Vector3((index.x+1)*scale, (index.y+1)*scale, index.z*scale)); + vertices.push(new THREE.Vector3(index.x*scale, (index.y+1)*scale, index.z*scale)); + + return vertices; + }, + + highlighterFaces: function(){ + return [new THREE.Face3(0,1,2), new THREE.Face3(0,2,3)]; } + }); \ No newline at end of file diff --git a/js/models/Lattice.js b/js/models/Lattice.js index 87b1fd53..d687ab92 100644 --- a/js/models/Lattice.js +++ b/js/models/Lattice.js @@ -286,6 +286,7 @@ Lattice = Backbone.Model.extend({ updateLatticeType: function(cellType, connectionType){ this.clearCells(); if (this._undo) this._undo(); + if (this.get("basePlane")) this.get("basePlane").destroy(); if (cellType == "octa"){ if (connectionType == "face"){ _.extend(this, this.OctaFaceLattice); @@ -297,7 +298,7 @@ Lattice = Backbone.Model.extend({ _.extend(this, this.OctaVertexLattice); } } else if (cellType == "cube"){ - + _.extend(this, this.CubeLattice); } this._initLatticeType(); }, @@ -333,7 +334,6 @@ Lattice = Backbone.Model.extend({ //bind events this.listenTo(this, "change:columnSeparation", this._changeColSeparation); - if (this.get("basePlane")) this.get("basePlane").destroy(); this.set("basePlane", new OctaBasePlane({scale:this.get("scale")})); this.set("columnSeparation", 0.0); }, @@ -376,7 +376,7 @@ Lattice = Backbone.Model.extend({ _undo: function(){//remove all the mixins, this will help with debugging later this._initLatticeType = null; this._changeColSeparation = null; - this.addCellAtPosition = null; + this.getIndexForPosition = null; this.getScale = null; this._makeCellForLatticeType = null; this.stopListening(this, "columnSeparation"); @@ -397,7 +397,6 @@ Lattice = Backbone.Model.extend({ //bind events this.listenTo(this, "change:columnSeparation", this._changeColSeparation); - if (this.get("basePlane")) this.get("basePlane").destroy(); this.set("basePlane", new OctaBasePlane({scale:this.get("scale")})); this.set("columnSeparation", 0.0); }, @@ -412,7 +411,7 @@ Lattice = Backbone.Model.extend({ window.three.render(); }, - addCellAtPosition: function(absPosition){ + getIndexForPosition: function(absPosition){ //calc indices in cell matrix var scale = this.get("scale"); @@ -426,7 +425,7 @@ Lattice = Backbone.Model.extend({ position.z = Math.round(absPosition.z/octHeight); if (position.z%2 == 1) position.y += 1; - this.addCellAtIndex(position); + return position; }, getScale: function(){ @@ -440,7 +439,7 @@ Lattice = Backbone.Model.extend({ _undo: function(){//remove all the mixins, this will help with debugging later this._initLatticeType = null; this._changeColSeparation = null; - this.addCellAtPosition = null; + this.getIndexForPosition = null; this.getScale = null; this._makeCellForLatticeType = null; this.stopListening(this, "columnSeparation"); @@ -461,27 +460,25 @@ Lattice = Backbone.Model.extend({ //bind events - if (this.get("basePlane")) this.get("basePlane").destroy(); this.set("basePlane", new SquareBasePlane({scale:this.get("scale")})); }, - addCellAtPosition: function(absPosition){ + getIndexForPosition: function(absPosition){ //calc indices in cell matrix var scale = this.get("scale"); - var colSep = this.get("columnSeparation"); - var latticeScale = scale*(1+2*colSep); var octHeight = 2*scale/Math.sqrt(6); - var triHeight = latticeScale/2*Math.sqrt(3); + var triHeight = scale/2*Math.sqrt(3); var position = {}; position.x = Math.round(absPosition.x/latticeScale); position.y = Math.round(absPosition.y/triHeight); - position.z = Math.round(absPosition.z/octHeight); + position.z = Math.round(absPosition.z/octHeight)-1; if (position.z%2 == 1) position.y += 1; - this.addCellAtIndex(position); + return position; }, + getScale: function(){ return this.get("scale"); }, @@ -492,7 +489,7 @@ Lattice = Backbone.Model.extend({ _undo: function(){//remove all the mixins, this will help with debugging later this._initLatticeType = null; - this.addCellAtPosition = null; + this.getIndexForPosition = null; this.getScale = null; this._makeCellForLatticeType = null; this.stopListening(this, "columnSeparation"); @@ -500,7 +497,7 @@ Lattice = Backbone.Model.extend({ this._undo = null; } - } + }, @@ -508,6 +505,46 @@ Lattice = Backbone.Model.extend({ //////////////////////////////CUBE LATTICE////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////// + CubeLattice: { + + _initLatticeType: function(){ + + //bind events + this.set("basePlane", new SquareBasePlane({scale:this.get("scale")})); + }, + + getIndexForPosition: function(absPosition){ + + //calc indices in cell matrix + var scale = this.get("scale"); + var index = {}; + index.x = Math.round((absPosition.x-scale/2)/scale); + index.y = Math.round((absPosition.y-scale/2)/scale); + index.z = Math.round(absPosition.z/scale); + + return index; + }, + + + getScale: function(){ + return this.get("scale"); + }, + + _makeCellForLatticeType: function(indices, scale){ + return new DMACubeCell(indices, scale, this); + }, + + _undo: function(){//remove all the mixins, this will help with debugging later + this._initLatticeType = null; + this.getIndexForPosition = null; + this.getScale = null; + this._makeCellForLatticeType = null; + this.stopListening(this, "columnSeparation"); + this.set("columnSeparation", null); + this._undo = null; + } + + } }); \ No newline at end of file diff --git a/js/threeViews/Highlighter.js b/js/threeViews/Highlighter.js index 34e68cb4..ac62a221 100644 --- a/js/threeViews/Highlighter.js +++ b/js/threeViews/Highlighter.js @@ -11,8 +11,8 @@ Highlighter = Backbone.View.extend({ var geometry = new THREE.Geometry(); //can't change size of faces or vertices buffers dynamically - geometry.vertices = [new THREE.Vector3(0,0,0), new THREE.Vector3(0,0,0), new THREE.Vector3(0,0,0)]; - geometry.faces = [new THREE.Face3(0,1,2)]; + geometry.vertices = [new THREE.Vector3(0,0,0), new THREE.Vector3(0,0,0), new THREE.Vector3(0,0,0), new THREE.Vector3(0,0,0)]; + geometry.faces = this._getFacesArray(); geometry.dynamic = true; this.mesh = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ @@ -24,6 +24,17 @@ Highlighter = Backbone.View.extend({ })); window.three.sceneAdd(this.mesh, null); this.hide(); + + //bind events + this.listenTo(window.appState, "change:cellType change:connectionType", this.updateHighlighterFaces); + }, + + _getFacesArray: function(){ + return window.lattice.get("basePlane").highlighterFaces(); + }, + + updateHighlighterFaces: function(){ + this.mesh.geometry.faces = this._getFacesArray(); }, hide: function(){ @@ -49,13 +60,15 @@ Highlighter = Backbone.View.extend({ if (!highlightable.myParent) console.warn("no parent for highlightable object"); this.highlightedObject = highlightable.myParent; - this.highlightedFace = intersection.face; - var newVertices = highlightable.myParent.getHighlighterVertices(intersection.face); + var newVertices = highlightable.myParent.getHighlighterVertices(intersection.face, intersection.point); if (!newVertices) { this.hide(); return; } - this.mesh.geometry.vertices = newVertices; + + for (var i=0;i<newVertices.length;i++){ + this.mesh.geometry.vertices[i] = newVertices[i]; + } this.mesh.geometry.verticesNeedUpdate = true; // this.mesh.geometry.normalsNeedUpdate = true; // this.mesh.geometry.computeFaceNormals(); @@ -68,9 +81,9 @@ Highlighter = Backbone.View.extend({ return this.mesh.visible; }, - _getNextCellPosition: function(indices){//add one to z index - indices.z += 1; - return indices; + _getNextCellPosition: function(index){//add one to z index + index.z += 1; + return index; }, addRemoveVoxel: function(shouldAdd){ -- GitLab