diff --git a/js/fea/DmaCell.js b/js/fea/DmaCell.js index f1747f4f69390359d4a6b51c774927d924cd283b..d9e3259a4adbcbcfa9c3b03f914d74a620de57ba 100644 --- a/js/fea/DmaCell.js +++ b/js/fea/DmaCell.js @@ -5,14 +5,16 @@ //a Cell, a unit piece of the lattice -function DMACell(indices, scale, lattice, inverse) { +var cellMaterials = [new THREE.MeshNormalMaterial(), + new THREE.MeshBasicMaterial({color:0x000000, wireframe:true})]; +var cellMaterial = [new THREE.MeshNormalMaterial()]; + +function DMACell(indices, scale, inverse) { this.indices = indices; if (!inverse) inverse = false; this.isInverse = inverse; - this.lattice = lattice;//need ref back to lattice this.cellMesh = this._buildCellMesh(indices.z); - this.parts = this._initParts(indices.z); var cellMode = dmaGlobals.lattice.get("cellMode"); var inverseMode = dmaGlobals.lattice.get("inverseMode"); @@ -20,35 +22,31 @@ function DMACell(indices, scale, lattice, inverse) { this.drawForMode(scale, cellMode, inverseMode); } -DMACell.prototype.removePart = function(index){ - this.parts[index].destroy(); - this.parts[index] = null; - var hasAnyParts = false;//check if all parts have been deleted - _.each(this.parts, function(part){ - if (part) hasAnyParts = true; - }); - if (!hasAnyParts) this.lattice.removeCell(this);//if all parts are gone, remove cell -}; - -DMACell.prototype._setMeshPosition = function(mesh, position){ - mesh.position.x = position.x; - mesh.position.y = position.y; - mesh.position.z = position.z; -}; - DMACell.prototype.drawForMode = function(scale, cellMode, inverseMode){ 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(this.indices.z); _.each(this.parts, function(part){ if (part) part.setVisibility(cellMode == "part"); }); }; -DMACell.prototype._setCellMeshVisibility = function(visibility){ - if (!this.cellMesh) return; - this.cellMesh.visible = visibility; +DMACell.prototype._superBuildCellMesh = function(zIndex, unitCellGeo, material){//abstract mesh representation of cell + if (!material) material = cellMaterials; + var mesh = THREE.SceneUtils.createMultiMaterialObject(unitCellGeo, material); + this._doMeshTransformations(zIndex, mesh); + mesh.myParent = this;//we need a reference to this instance from the mesh for intersection selection stuff + if (this.isInverse) dmaGlobals.three.sceneAdd(mesh, "inverseCell"); + else dmaGlobals.three.sceneAdd(mesh, "cell"); + return mesh; }; +DMACell.prototype._doMeshTransformations = function(zIndex, mesh){}; + +/////////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////SCALE/POSITION//////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////////// + DMACell.prototype.updateForScale = function(scale, cellMode){ //only update visible object to scale var position = this.getPosition(); @@ -61,8 +59,14 @@ DMACell.prototype.updateForScale = function(scale, cellMode){ } }; +DMACell.prototype._setMeshPosition = function(mesh, position){ + mesh.position.x = position.x; + mesh.position.y = position.y; + mesh.position.z = position.z; +}; + DMACell.prototype.getScale = function(){//need for part relay - return this.lattice.get("scale"); + return dmaGlobals.lattice.get("scale"); }; DMACell.prototype.getPosition = function(){//need for part relay @@ -70,8 +74,23 @@ DMACell.prototype.getPosition = function(){//need for part relay return dmaGlobals.lattice.getPositionForIndex(this.indices); }; -DMACell.prototype.getIndex = function(){ - return _.clone(this.indices); +DMACell.prototype._setCellMeshVisibility = function(visibility){ + if (!this.cellMesh) return; + this.cellMesh.visible = visibility; +}; + +/////////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////DELETE//////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////////// + +DMACell.prototype.removePart = function(index){ + this.parts[index].destroy(); + this.parts[index] = null; + var hasAnyParts = false;//check if all parts have been deleted + _.each(this.parts, function(part){ + if (part) hasAnyParts = true; + }); + if (!hasAnyParts) dmaGlobals.lattice.removeCell(this);//if all parts are gone, remove cell }; DMACell.prototype.destroy = function(){ @@ -89,27 +108,25 @@ DMACell.prototype.destroy = function(){ if (part) part.destroy(); }); this.indices = null; - this.lattice = null; this.parts = null; }; + + + /////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////OCTA FACE AND EDGE CLASS/////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////// - (function () { var unitCellGeo = new THREE.OctahedronGeometry(1/Math.sqrt(2)); unitCellGeo.applyMatrix(new THREE.Matrix4().makeRotationZ(-3*Math.PI/12)); unitCellGeo.applyMatrix(new THREE.Matrix4().makeRotationX(Math.asin(2/Math.sqrt(2)/Math.sqrt(3)))); - var cellMaterials = [new THREE.MeshNormalMaterial(), - new THREE.MeshBasicMaterial({color:0x000000, wireframe:true})]; - - function DMAFaceOctaCell(indices, scale, lattice){ - DMACell.call(this, indices, scale, lattice); + function DMAFaceOctaCell(indices, scale){ + DMACell.call(this, indices, scale); } DMAFaceOctaCell.prototype = Object.create(DMACell.prototype); @@ -121,13 +138,12 @@ DMACell.prototype.destroy = function(){ return parts; }; - DMAFaceOctaCell.prototype._buildCellMesh = function(zIndex){//abstract mesh representation of cell - var mesh; - mesh = THREE.SceneUtils.createMultiMaterialObject(unitCellGeo, cellMaterials); + DMAFaceOctaCell.prototype._buildCellMesh = function(zIndex){ + return this._superBuildCellMesh(zIndex, unitCellGeo); + }; + + DMAFaceOctaCell.prototype._doMeshTransformations = function(zIndex, mesh){ if (zIndex%2!=0) mesh.rotation.set(0, 0, Math.PI); - mesh.myParent = this;//we need a reference to this instance from the mesh for intersection selection stuff - dmaGlobals.three.sceneAdd(mesh, "cell"); - return mesh; }; DMAFaceOctaCell.prototype.calcHighlighterPosition = function(face){ @@ -143,33 +159,31 @@ DMACell.prototype.destroy = function(){ self.DMAFaceOctaCell = DMAFaceOctaCell; - function DMAEdgeOctaCell(indices, scale, lattice){ - DMAFaceOctaCell.call(this, indices, scale, lattice); + function DMAEdgeOctaCell(indices, scale){ + DMAFaceOctaCell.call(this, indices, scale); } DMAEdgeOctaCell.prototype = Object.create(DMAFaceOctaCell.prototype); - DMAEdgeOctaCell.prototype._buildCellMesh = function(zIndex){//abstract mesh representation of cell - var mesh; - mesh = THREE.SceneUtils.createMultiMaterialObject(unitCellGeo, cellMaterials); - mesh.myParent = this;//we need a reference to this instance from the mesh for intersection selection stuff - dmaGlobals.three.sceneAdd(mesh, "cell"); - return mesh; - }; + DMAEdgeOctaCell.prototype._doMeshTransformations = function(){}; self.DMAEdgeOctaCell = DMAEdgeOctaCell; +})(); + /////////////////////////////////////////////////TETRA CELL//////////////////////////////////// - var unitTetraCellGeo = new THREE.TetrahedronGeometry(Math.sqrt(3/8)); - unitTetraCellGeo.applyMatrix(new THREE.Matrix4().makeRotationZ(Math.PI/4)); - unitTetraCellGeo.applyMatrix(new THREE.Matrix4().makeRotationX((Math.PI-Math.atan(2*Math.sqrt(2)))/2)); - unitTetraCellGeo.applyMatrix(new THREE.Matrix4().makeTranslation(0,0,Math.sqrt(3/8)-1/Math.sqrt(6))); +(function () { - var unitTetraCellGeoUpsideDown = unitTetraCellGeo.clone(); - unitTetraCellGeoUpsideDown.applyMatrix(new THREE.Matrix4().makeRotationX(Math.PI)); + var unitCellGeo = new THREE.TetrahedronGeometry(Math.sqrt(3/8)); + unitCellGeo.applyMatrix(new THREE.Matrix4().makeRotationZ(Math.PI/4)); + unitCellGeo.applyMatrix(new THREE.Matrix4().makeRotationX((Math.PI-Math.atan(2*Math.sqrt(2)))/2)); + unitCellGeo.applyMatrix(new THREE.Matrix4().makeTranslation(0,0,Math.sqrt(3/8)-1/Math.sqrt(6))); - function DMATetraFaceCell(indices, scale, lattice){ - DMACell.call(this, indices, scale, lattice, true); + var unitCellGeoUpsideDown = unitCellGeo.clone(); + unitCellGeoUpsideDown.applyMatrix(new THREE.Matrix4().makeRotationX(Math.PI)); + + function DMATetraFaceCell(indices, scale){ + DMACell.call(this, indices, scale, true); } DMATetraFaceCell.prototype = Object.create(DMACell.prototype); @@ -178,14 +192,12 @@ DMACell.prototype.destroy = function(){ }; DMATetraFaceCell.prototype._buildCellMesh = function(zIndex){//abstract mesh representation of cell - var mesh; - if (zIndex%2 ==0) mesh = THREE.SceneUtils.createMultiMaterialObject(unitTetraCellGeo, cellMaterials); - else mesh = THREE.SceneUtils.createMultiMaterialObject(unitTetraCellGeoUpsideDown, cellMaterials); - if (Math.abs(zIndex%4) == 2 || Math.abs(zIndex%4) == 3) mesh.rotateZ(Math.PI/3); + if (zIndex%2 ==0) return this._superBuildCellMesh(zIndex, unitCellGeo); + return this._superBuildCellMesh(zIndex, unitCellGeoUpsideDown); + }; - mesh.myParent = this;//we need a reference to this instance from the mesh for intersection selection stuff - dmaGlobals.three.sceneAdd(mesh, "inverseCell"); - return mesh; + DMATetraFaceCell.prototype._doMeshTransformations = function(zIndex, mesh){ + if (Math.abs(zIndex%4) == 2 || Math.abs(zIndex%4) == 3) mesh.rotateZ(Math.PI/3); }; DMATetraFaceCell.prototype.calcHighlighterPosition = function(face){ @@ -201,25 +213,14 @@ DMACell.prototype.destroy = function(){ return {index: _.clone(this.indices), direction:direction, position:position}; }; - DMATetraFaceCell.prototype.getPosition = function(){//need for part relay - return dmaGlobals.lattice.getInvCellPositionForIndex(this.indices); - }; - self.DMATetraFaceCell = DMATetraFaceCell; - function DMATetraEdgeCell(indices, scale, lattice){ - DMATetraFaceCell.call(this, indices, scale, lattice, true); + function DMATetraEdgeCell(indices, scale){ + DMATetraFaceCell.call(this, indices, scale, true); } DMATetraEdgeCell.prototype = Object.create(DMATetraFaceCell.prototype); - DMATetraEdgeCell.prototype._buildCellMesh = function(zIndex){//abstract mesh representation of cell - var mesh; - if (zIndex%2 ==0) mesh = THREE.SceneUtils.createMultiMaterialObject(unitTetraCellGeo, cellMaterials); - else mesh = THREE.SceneUtils.createMultiMaterialObject(unitTetraCellGeoUpsideDown, cellMaterials); - mesh.myParent = this;//we need a reference to this instance from the mesh for intersection selection stuff - dmaGlobals.three.sceneAdd(mesh, "inverseCell"); - return mesh; - }; + DMATetraEdgeCell.prototype._doMeshTransformations = function(){}; self.DMATetraEdgeCell = DMATetraEdgeCell; @@ -232,13 +233,10 @@ DMACell.prototype.destroy = function(){ (function () { - var vertexOctaGeo = new THREE.OctahedronGeometry(1/Math.sqrt(2)); - - var cellMaterials = [new THREE.MeshNormalMaterial(), - new THREE.MeshBasicMaterial({color:0x000000, wireframe:true})]; + var unitCellGeo = new THREE.OctahedronGeometry(1/Math.sqrt(2)); - function DMAVertexOctaCell(indices, scale, lattice){ - DMACell.call(this, indices, scale, lattice); + function DMAVertexOctaCell(indices, scale){ + DMACell.call(this, indices, scale); } DMAVertexOctaCell.prototype = Object.create(DMACell.prototype); @@ -246,11 +244,8 @@ DMACell.prototype.destroy = function(){ return []; }; - DMAVertexOctaCell.prototype._buildCellMesh = function(){//abstract mesh representation of cell - var mesh = THREE.SceneUtils.createMultiMaterialObject(vertexOctaGeo, cellMaterials); - mesh.myParent = this;//we need a reference to this instance from the mesh for intersection selection stuff - dmaGlobals.three.sceneAdd(mesh, "cell"); - return mesh; + DMAVertexOctaCell.prototype._buildCellMesh = function(zIndex){//abstract mesh representation of cell + return this._superBuildCellMesh(zIndex, unitCellGeo); }; DMAVertexOctaCell.prototype.calcHighlighterPosition = function(face, point){ @@ -293,11 +288,15 @@ DMACell.prototype.destroy = function(){ self.DMAVertexOctaCell = DMAVertexOctaCell; +})(); + /////////////////////////////////////////TRUNCATED CUBE//////////////////////////////////// +(function(){ + var truncCubeRad = Math.sqrt(2)/2; - var truncCubeGeo = new THREE.Geometry(); - truncCubeGeo.vertices = [ + var unitCellGeo = new THREE.Geometry(); + unitCellGeo.vertices = [ new THREE.Vector3(truncCubeRad, 0, truncCubeRad), new THREE.Vector3(0, truncCubeRad, truncCubeRad), new THREE.Vector3(-truncCubeRad, 0, truncCubeRad), @@ -313,7 +312,7 @@ DMACell.prototype.destroy = function(){ new THREE.Vector3(-truncCubeRad, 0, -truncCubeRad), new THREE.Vector3(0, -truncCubeRad, -truncCubeRad) ]; - truncCubeGeo.faces = [ + unitCellGeo.faces = [ new THREE.Face3(1,0,4), new THREE.Face3(2,1,5), new THREE.Face3(3,2,6), @@ -337,10 +336,10 @@ DMACell.prototype.destroy = function(){ new THREE.Face3(3,11,7), new THREE.Face3(3,6,11) ]; - truncCubeGeo.computeFaceNormals(); + unitCellGeo.computeFaceNormals(); - function DMATruncCubeCell(indices, scale, lattice){ - DMACell.call(this, indices, scale, lattice, true); + function DMATruncCubeCell(indices, scale){ + DMACell.call(this, indices, scale, true); } DMATruncCubeCell.prototype = Object.create(DMACell.prototype); @@ -349,11 +348,8 @@ DMACell.prototype.destroy = function(){ }; DMATruncCubeCell.prototype._buildCellMesh = function(){//abstract mesh representation of cell - var mesh = THREE.SceneUtils.createMultiMaterialObject(truncCubeGeo, [new THREE.MeshNormalMaterial()]); - mesh.myParent = this;//we need a reference to this instance from the mesh for intersection selection stuff - var wireframe = new THREE.EdgesHelper(mesh.children[0], 0x000000); - mesh.children.push(wireframe); - dmaGlobals.three.sceneAdd(mesh, "inverseCell"); + var mesh = this._superBuildCellMesh(null, unitCellGeo, cellMaterial); + mesh.children.push(new THREE.EdgesHelper(mesh.children[0], 0x000000)); return mesh; }; @@ -371,11 +367,8 @@ DMACell.prototype.destroy = function(){ var unitCellGeo = new THREE.BoxGeometry(1,1,1); - var cellMaterials = [new THREE.MeshNormalMaterial()]; - - - function DMACubeCell(indices, scale, lattice){ - DMACell.call(this, indices, scale, lattice); + function DMACubeCell(indices, scale){ + DMACell.call(this, indices, scale); } DMACubeCell.prototype = Object.create(DMACell.prototype); @@ -388,12 +381,10 @@ DMACell.prototype.destroy = function(){ }; DMACubeCell.prototype._buildCellMesh = function(){//abstract mesh representation of cell - var mesh = new THREE.SceneUtils.createMultiMaterialObject(unitCellGeo, cellMaterials); - mesh.myParent = this;//we need a reference to this instance from the mesh for intersection selection stuff + var mesh = this._superBuildCellMesh(null, unitCellGeo, cellMaterial); var wireframe = new THREE.BoxHelper(mesh.children[0]); wireframe.material.color.set(0x000000); mesh.children.push(wireframe); - dmaGlobals.three.sceneAdd(mesh, "cell"); return mesh; }; diff --git a/js/models/BasePlane.js b/js/models/BasePlane.js index d570835b7963f057fa55108ccfed95c28b3c90ac..7f551e5efc9c55ee2a96fd1b1cfa1be88df515fd 100644 --- a/js/models/BasePlane.js +++ b/js/models/BasePlane.js @@ -51,10 +51,6 @@ BasePlane = Backbone.Model.extend({ return null; }, - getIndex: function(face){ - return dmaGlobals.lattice.getIndexForPosition(face.geometry.vertices[0]); - }, - //subclasses handle getHighlighterVertices /////////////////////////////////////////////////////////////////////////////////// diff --git a/js/models/Lattice.js b/js/models/Lattice.js index 956dae6578932e3dfb17dc65a85b1d3f42d679df..8ab6dacea4f4bba2d6c9ea43131966b22f54720f 100644 --- a/js/models/Lattice.js +++ b/js/models/Lattice.js @@ -530,11 +530,11 @@ Lattice = Backbone.Model.extend({ _makeCellForLatticeType: function(indices, scale){ this._addInverseCellsForIndex(indices); - return new DMAFaceOctaCell(indices, scale, this); + return new DMAFaceOctaCell(indices, scale); }, _makeInvCellForLatticeType: function(indices, scale){ - return new DMATetraFaceCell(indices, scale, this); + return new DMATetraFaceCell(indices, scale); }, _inverseIndicesToAdd: function(index){ @@ -641,11 +641,11 @@ Lattice = Backbone.Model.extend({ _makeCellForLatticeType: function(indices, scale){ this._addInverseCellsForIndex(indices); - return new DMAEdgeOctaCell(indices, scale, this); + return new DMAEdgeOctaCell(indices, scale); }, _makeInvCellForLatticeType: function(indices, scale){ - return new DMATetraEdgeCell(indices, scale, this); + return new DMATetraEdgeCell(indices, scale); }, getInvCellPositionForIndex: function(index){ @@ -766,11 +766,11 @@ Lattice = Backbone.Model.extend({ _makeCellForLatticeType: function(indices, scale){ this._addInverseCellsForIndex(indices); - return new DMAVertexOctaCell(indices, scale, this); + return new DMAVertexOctaCell(indices, scale); }, _makeInvCellForLatticeType: function(indices, scale){ - return new DMATruncCubeCell(indices, scale, this); + return new DMATruncCubeCell(indices, scale); }, _undo: function(){//remove all the mixins, this will help with debugging later @@ -820,7 +820,7 @@ Lattice = Backbone.Model.extend({ }, _makeCellForLatticeType: function(indices, scale){ - return new DMACubeCell(indices, scale, this); + return new DMACubeCell(indices, scale); }, _undo: function(){//remove all the mixins, this will help with debugging later