diff --git a/index.html b/index.html index 454c423f9447f9d51eeb6b7429a642d00aca095c..f865396852e75e68cc32c302aea83b417f0c7081 100644 --- a/index.html +++ b/index.html @@ -44,13 +44,14 @@ <script src="js/fea/DmaNode.js"></script> <script src="js/fea/DmaBeam.js"></script> + <script src="js/cells/DMAParentCell.js"></script> <script src="js/cells/DmaCell.js"></script> <script src="js/cells/OctaFaceCell.js"></script> <script src="js/cells/OctaEdgeCell.js"></script> <script src="js/cells/OctaRotEdgeCell.js"></script> <script src="js/cells/OctaVertexCell.js"></script> - <script src="js/cells/DMASuperCell.js"></script> + <script src="js/supercells/GIKSuperCell.js"></script> <script src="js/cells/DMACellFreeform.js"></script> <script src="js/cells/CubeCell.js"></script> diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js index 688a09d8bc350e41d08c87a26ad7f26f066a8980..e27013a494c13be7c251d153dfae29875548ff9b 100644 --- a/js/cells/DMACell.js +++ b/js/cells/DMACell.js @@ -13,10 +13,11 @@ function DMACell(indices){ //object 3d is parent to all 3d elements related to cell, parts, beams, nodes, etc this.object3D = this._buildObject3D(); this._addMeshes(this._buildMesh(), this.object3D);//build cell meshes - globals.three.sceneAdd(this.object3D, this._getSceneType()); + if (!this.superCell) globals.three.sceneAdd(this.object3D, this._getSceneType()); this.setMode(); } +DMACell.prototype = Object.create(DMAParentCell.prototype); DMACell.prototype._getSceneType = function(){//todo need this? if (this.indices) return "cell"; @@ -29,43 +30,15 @@ DMACell.prototype._buildObject3D = function(){ return object3D; }; -DMACell.prototype._rotateCell = function(object3D){ - return object3D;//by default, no mesh transformations -}; - -DMACell.prototype._translateCell = function(object3D){ - var position = globals.lattice.getPositionForIndex(this.indices); - object3D.position.set(position.x, position.y, position.z); - return object3D; -}; - -DMACell.prototype._addMeshes = function(meshes, object3D){//accepts an array or a single mesh - this._addRemoveMeshes(true, meshes, object3D); -}; - -DMACell.prototype._removeMeshes = function(meshes, object3D){//accepts an array or a single mesh - this._addRemoveMeshes(false, meshes, object3D); -}; - -DMACell.prototype._addRemoveMeshes = function(shouldAdd, meshes, object3D){//accepts an array or a single mesh - if (object3D === undefined) object3D = this.object3D; - if (meshes.constructor === Array){ - _.each(meshes, function(mesh){ - if (shouldAdd) object3D.add(mesh); - else object3D.remove(mesh); - }); - } else if (shouldAdd) object3D.add(meshes); - else object3D.remove(meshes); -}; - -DMACell.prototype._buildMesh = function(){//called from every subclass +DMACell.prototype._buildMesh = function(){ var geometry = this._getGeometry(); var meshes = []; var mesh = new THREE.Mesh(geometry, cellMaterial); mesh.name = "cell"; + meshes.push(mesh); var wireframe = this._buildWireframe(mesh, geometry); + if (!wireframe) return meshes; wireframe.name = "cell"; - meshes.push(mesh); meshes.push(wireframe); return meshes; }; @@ -78,6 +51,17 @@ DMACell.prototype._initParts = function(){ return [];//override in subclasses }; +DMACell.prototype.setSuperCell = function(superCell, index){ + this.superCell = superCell; + this.superCellIndex = index; + + if (this.superCellIndex == this.superCell.getLength()) this.mesh.rotateZ(Math.PI); + if (globals.appState.get("cellMode")=="part") { + this.parts = this.__initParts(); + this.draw(); + } +}; + DMACell.prototype.setMode = function(mode){ if (mode === undefined) mode = globals.appState.get("cellMode"); @@ -107,30 +91,6 @@ DMACell.prototype.setMode = function(mode){ }); }; -DMACell.prototype.hide = function(){ - this.object3D.visible = false; -}; - -DMACell.prototype.show = function(mode){ - this.object3D.visible = true; - this.setMode(mode); -}; - -DMACell.prototype.setOpacity = function(opacity){ -}; - -DMACell.prototype.getPosition = function(){ - return this.object3D.position.clone(); -}; - -DMACell.prototype.getQuaternion = function(){ - return this.object3D.quaternion.clone(); -}; - -DMACell.prototype.getEuler = function(){ - return this.object3D.rotation.clone(); -}; - DMACell.prototype.xScale = function(){ return globals.lattice.xScale(0); }; diff --git a/js/cells/DMAParentCell.js b/js/cells/DMAParentCell.js new file mode 100644 index 0000000000000000000000000000000000000000..20f9cbf999994c0632a2dd8f436448d34ff16325 --- /dev/null +++ b/js/cells/DMAParentCell.js @@ -0,0 +1,62 @@ +/** + * Created by aghassaei on 5/27/15. + */ + + +//common methods between cell and supercell classes + +function DMAParentCell(){ +} + +DMAParentCell.prototype._rotateCell = function(object3D){ + return object3D;//by default, no mesh transformations +}; + +DMAParentCell.prototype._translateCell = function(object3D){ + var position = globals.lattice.getPositionForIndex(this.indices); + object3D.position.set(position.x, position.y, position.z); + return object3D; +}; + +DMAParentCell.prototype._addMeshes = function(meshes, object3D){//accepts an array or a single mesh + this._addRemoveMeshes(true, meshes, object3D); +}; + +DMAParentCell.prototype._removeMeshes = function(meshes, object3D){//accepts an array or a single mesh + this._addRemoveMeshes(false, meshes, object3D); +}; + +DMAParentCell.prototype._addRemoveMeshes = function(shouldAdd, meshes, object3D){//accepts an array or a single mesh + if (object3D === undefined) object3D = this.object3D; + if (meshes.constructor === Array){ + _.each(meshes, function(mesh){ + if (shouldAdd) object3D.add(mesh); + else object3D.remove(mesh); + }); + } else if (shouldAdd) object3D.add(meshes); + else object3D.remove(meshes); +}; + +DMAParentCell.prototype.hide = function(){ + this.object3D.visible = false; +}; + +DMAParentCell.prototype.show = function(mode){ + this.object3D.visible = true; + this.setMode(mode); +}; + +DMAParentCell.prototype.setOpacity = function(opacity){ +}; + +DMAParentCell.prototype.getPosition = function(){ + return this.object3D.position.clone(); +}; + +DMAParentCell.prototype.getQuaternion = function(){ + return this.object3D.quaternion.clone(); +}; + +DMAParentCell.prototype.getEuler = function(){ + return this.object3D.rotation.clone(); +}; \ No newline at end of file diff --git a/js/cells/DMASuperCell.js b/js/cells/DMASuperCell.js deleted file mode 100644 index d4ea4fd69d7dd24c167baee931a7494ce6516955..0000000000000000000000000000000000000000 --- a/js/cells/DMASuperCell.js +++ /dev/null @@ -1,101 +0,0 @@ -/** - * Created by aghassaei on 5/15/15. - */ - - -var cellBrassMaterial = new THREE.MeshLambertMaterial({color:"#b5a642"}); -var cellFiberGlassMaterial = new THREE.MeshLambertMaterial({color:"#fff68f"}); - -DMASuperCell = function(length, range, cells){ - if (range) var shouldRotate = range.max.x == range.min.x; - this.material = globals.lattice.get("materialType"); - this.mesh = this._buildSuperCellMesh(length, shouldRotate); - this.setVisibility(globals.appState.get("cellMode")=="cell"); - if (range) this.indices = _.clone(range.max); - this.cells = cells; - this.setScale(); - globals.three.sceneAdd(this.mesh); -}; - -DMASuperCell.prototype._buildSuperCellMesh = function(length, shouldRotate){ - var superCellGeo = new THREE.BoxGeometry(1,1,1); - superCellGeo.applyMatrix(new THREE.Matrix4().makeScale(length, 1, 1)); - superCellGeo.applyMatrix(new THREE.Matrix4().makeTranslation(-length/2+0.5, 0, 0)); - if (shouldRotate) superCellGeo.applyMatrix(new THREE.Matrix4().makeRotationZ(Math.PI/2)); - var mesh = THREE.SceneUtils.createMultiMaterialObject(superCellGeo, [this.getMaterialType()]); - var wireframe = new THREE.BoxHelper(mesh.children[0]); - wireframe.material.color.set(0x000000); - mesh.children.push(wireframe); - return mesh; -}; - -DMASuperCell.prototype.getMaterialType = function(){ - var material = cellBrassMaterial; - if (this.material == "fiberGlass") material = cellFiberGlassMaterial; - return material; -}; - -DMASuperCell.prototype._setPosition = function(index){ - if (!index) return; - var position = globals.lattice.getPositionForIndex(index); - this.mesh.position.set(position.x, position.y, position.z); -}; - -DMASuperCell.prototype.setScale = function(scale){ - if (!scale) scale = globals.lattice.get("scale"); - this.mesh.scale.set(scale, scale, scale); - this._setPosition(this.indices); -}; - -DMASuperCell.prototype.setVisibility = function(visible){ - this.mesh.visible = visible; -}; - -DMASuperCell.prototype.getLength = function(){ - return this.cells.length-1; -}; - -DMASuperCell.prototype.hide = function(){//only used in the context of stock simulation - this.setVisibility(false); - _.each(this.cells, function(cell){ - if (cell) cell.hide(); - }); -}; - -DMASuperCell.prototype.draw = function(scale, cellMode, partType){ - if (this.hideForStockSimulation) return; - if (!scale) scale = globals.lattice.get("scale"); - var partMode = cellMode == "part"; - -// this.updateForScale(scale, cellMode, partType); - - //set visibility - this.setVisibility(!partMode); - _.each(this.cells, function(cell){ - if (cell) cell.draw(scale, cellMode, partType); - }); -}; - -DMASuperCell.prototype.getPosition = function(){ - return this.mesh.position.clone(); -}; - -DMASuperCell.prototype.moveTo = function(position, axis){//used for stock simulations - this.mesh.position[axis] = position; - _.each(this.cells, function(cell){ - if (cell) cell.moveTo(position, axis); - }); -}; - -DMASuperCell.prototype.destroy = function(){ - if (this.destroyStarted) return;//prevents loop destroy from cells - this.destroyStarted = true; - globals.three.sceneRemove(this.mesh); - _.each(this.cells, function(cell){ - if (cell && !cell.destroyStarted) globals.lattice.removeCell(cell); - }); - this.cells = null; - this.mesh = null; - this.indices = null; - this.material = null; -} \ No newline at end of file diff --git a/js/cells/GIKCell.js b/js/cells/GIKCell.js index 35489cdf67d03ea4422b99ce4e76f86972042e62..5093ccae643cc278e8edf54968ddf0b4fd231d17 100644 --- a/js/cells/GIKCell.js +++ b/js/cells/GIKCell.js @@ -5,32 +5,16 @@ (function () { - function GIKCell(indices, cellMode, partType){ - CubeCell.call(this, indices, cellMode, partType); + function GIKCell(indices){ + this.superCell = true; + CubeCell.call(this, indices, true); } GIKCell.prototype = Object.create(CubeCell.prototype); - GIKCell.prototype._buildMesh = function(){ - return DMACubeCell.prototype._buildMesh.call(this, cellMaterial); - }; - - GIKCell.prototype._doMeshTransformations = function(mesh){ - if (this.indices && this.indices.z%2 != 0) mesh.rotateZ(Math.PI/2); - }; - - GIKCell.prototype._setCellMeshVisibility = function(visible){ - this.mesh.visible = false; - if (this.superCell) this.superCell.setVisibility(visible); - }; - GIKCell.prototype.setSuperCell = function(superCell, index){ this.superCell = superCell; this.superCellIndex = index; - if (this.superCellIndex == this.superCell.getLength()) this.mesh.rotateZ(Math.PI); - if (globals.appState.get("cellMode")=="part") { - this.parts = this.__initParts(); - this.draw(); - } + if (this.superCellIndex == this.superCell.getLength()) this.object3D.rotateZ(Math.PI); }; GIKCell.prototype.getMaterialType = function(){ diff --git a/js/lattice/GIKLattice.js b/js/lattice/GIKLattice.js index 377aee1be6b8fb4dea74659766cf8e5889eee46e..6cb2826ecf0e24bf1a79490a5f95ca19ef3131ec 100644 --- a/js/lattice/GIKLattice.js +++ b/js/lattice/GIKLattice.js @@ -48,7 +48,7 @@ latticeSubclasses["GIKLattice"] = { } } if (cells.length < 1) return null; - var superCell = new DMASuperCell(length, range, cells); + var superCell = new GIKSuperCell(length, range, cells); _.each(cells, function(cell, index){ cell.setSuperCell(superCell, index); }); diff --git a/js/supercells/GIKSuperCell.js b/js/supercells/GIKSuperCell.js new file mode 100644 index 0000000000000000000000000000000000000000..244fad6ca92b79e73a72faa6334cdb14996e4981 --- /dev/null +++ b/js/supercells/GIKSuperCell.js @@ -0,0 +1,107 @@ +/** + * Created by aghassaei on 5/26/15. + */ + + +var cellBrassMaterial = new THREE.MeshLambertMaterial({color:"#b5a642"}); +var cellFiberGlassMaterial = new THREE.MeshLambertMaterial({color:"#fff68f"}); + +GIKSuperCell = function(length, range, cells){ + if (range) this.indices = _.clone(range.max); + this.material = globals.lattice.get("materialType"); + this.cells = cells; + + this.object3D = this._buildObject3D(); + this._addMeshes(this._buildMesh(length), this.object3D); + globals.three.sceneAdd(this.object3D); + + +}; +GIKSuperCell.prototype = Object.create(DMAParentCell.prototype); + +GIKSuperCell.prototype._buildObject3D = function(){ + return this._translateCell(this._rotateCell(new THREE.Object3D())); +}; + +GIKSuperCell.prototype._rotateCell = function(object3D){ + if (this.indices && this.indices.z%2 != 0) object3D.rotateZ(Math.PI/2); + return object3D; +}; + +GIKSuperCell.prototype._buildMesh = function(length){ + var meshes = []; + var superCellGeo = new THREE.BoxGeometry(1,1,1); + superCellGeo.applyMatrix(new THREE.Matrix4().makeScale(length, 1, 1)); + superCellGeo.applyMatrix(new THREE.Matrix4().makeTranslation(-length/2+0.5, 0, 0)); + var mesh = new THREE.Mesh(superCellGeo, this.getMaterialType()); + mesh.name = "supercell"; + meshes.push(mesh); + var wireframe = this._buildWireframe(mesh); + if (!wireframe) return meshes; + wireframe.name = "supercell"; + meshes.push(wireframe); + return meshes; +}; + +GIKSuperCell.prototype._buildWireframe = function(mesh){ + var wireframe = new THREE.BoxHelper(mesh); + wireframe.material.color.set(0x000000); + wireframe.matrixWorld = mesh.matrixWorld; + wireframe.matrixAutoUpdate = true; + return wireframe; +}; + +GIKSuperCell.prototype.getMaterialType = function(){ + var material = cellBrassMaterial; + if (this.material == "fiberGlass") material = cellFiberGlassMaterial; + return material; +}; + +GIKSuperCell.prototype.setMode = function(mode){ + + if (mode === undefined) mode = globals.appState.get("cellMode"); + if (mode == "cell") mode = "supercell"; + + switch(mode) { + case "supercell": + break; + case "cell": + break; + case "part": + if (!this.parts) { + this.parts = this._initParts(); + var self = this; + _.each(this.parts, function(part){ + self._addMeshes(part.getMesh()); + }); + } + break; + case "beam": + if (!this.beams) this.beams = this._initBeams(); + break; + case "node": + if (!this.nodes) this.nodes = this._initNodes(); + break; + } + + _.each(this.object3D.children, function(child){ + child.visible = child.name == mode; + }); +}; + +GIKSuperCell.prototype.getLength = function(){ + return this.cells.length-1; +}; + +GIKSuperCell.prototype.destroy = function(){ + if (this.destroyStarted) return;//prevents loop destroy from cells + this.destroyStarted = true; + globals.three.sceneRemove(this.object3D); + this.object3D = null; + _.each(this.cells, function(cell){ + if (cell && !cell.destroyStarted) globals.lattice.removeCell(cell); + }); + this.cells = null; + this.indices = null; + this.material = null; +} \ No newline at end of file