diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js index 9795d55a8c5a106cd72fb26fc99f78a9a670dd70..8b8ab7bd3ef9783119003788f50170ad0052253c 100644 --- a/js/cells/DMACell.js +++ b/js/cells/DMACell.js @@ -18,7 +18,7 @@ function DMACell(indices){ this.setMode(); } -DMACell.prototype._getSceneType = function(){ +DMACell.prototype._getSceneType = function(){//todo need this? if (this.indices) return "cell"; return null; }; @@ -74,9 +74,6 @@ DMACell.prototype._initParts = function(){ return [];//override in subclasses }; -DMACell.prototype.changePartType = function(){//override in subclasses -}; - DMACell.prototype.setMode = function(mode){ if (mode === undefined) mode = globals.appState.get("cellMode"); diff --git a/js/cells/DmaCellOld.js b/js/cells/DmaCellOld.js deleted file mode 100644 index 5c185fdfe9f332e34a0afc3346706e506a4ea253..0000000000000000000000000000000000000000 --- a/js/cells/DmaCellOld.js +++ /dev/null @@ -1,236 +0,0 @@ -/** - * Created by aghassaei on 1/14/15. - */ - - -//a Cell, a unit piece of the lattice - -//var cellMaterials = [new THREE.MeshNormalMaterial(), -// new THREE.MeshBasicMaterial({color:0x000000, wireframe:true})]; -// -//function DMACell(indices, cellMode, partType) { -// -// this.indices = indices; -// this.mesh = this._buildMesh(); -// this._doMeshTransformations(this.mesh);//some cell types require transformations -// this._setMeshPosition(this.mesh, this._calcPosition()); -// -// globals.three.sceneAdd(this.mesh,this._sceneType(indices)); -// -// this.draw(cellMode, partType); -// -// this.hideForStockSimulation = false; -//} -// -//DMACell.prototype._sceneType = function(indices){ -// if (!indices || indices == null || indices === undefined) return null; -// return "cell"; -//}; -// -//DMACell.prototype.draw = function(cellMode, partType){ -// if (this.hideForStockSimulation) return; -// if (!cellMode) cellMode = globals.appState.get("cellMode"); -// if (!partType) partType = globals.lattice.get("partType"); -// //var beamMode = partType == "beam"; -// var beamMode = false; -// var partMode = cellMode == "part"; -// -// //init parts/beams if needed -// if (partMode &&!beamMode && !this.parts) this.parts = this._initParts(); -// if (beamMode && !this.beams) { -// this.nodes = this._initNodes(this.mesh.children[0].geometry.vertices); -// this.beams = this._initBeams(this.nodes, this.mesh.children[0].geometry.faces); -// } -// -// //set visibility -// this._setCellMeshVisibility(!partMode); -// _.each(this.parts, function(part){ -// if (part) part.setVisibility(partMode && !beamMode); -// }); -// _.each(this.beams, function(beam){ -// beam.setVisibility(beamMode && partMode); -// }); -//}; -// -//DMACell.prototype.hide = function(){//only used in the context of stock simulation -// this._setCellMeshVisibility(false); -// _.each(this.parts, function(part){ -// if (part) part.setVisibility(false); -// }); -// _.each(this.beams, function(beam){ -// beam.setVisibility(false); -// }); -//}; -// -///////////////////////////////////////////////////////////////////////////////////////////////// -///////////////////////////////////SCALE/POSITION//////////////////////////////////////////////// -///////////////////////////////////////////////////////////////////////////////////////////////// -// -//DMACell.prototype._setMeshPosition = function(mesh, position){ -// mesh.position.x = position.x; -// mesh.position.y = position.y; -// mesh.position.z = position.z; -//}; -// -//DMACell.prototype.moveTo = function(position, axis){//used for stock simulations -// this.mesh.position[axis] = position; -// if (globals.appState.get("cellMode") == "part"){ -// _.each(this.parts, function(part){ -// if (part) part.moveTo(position, axis); -// }); -// } -//}; -// -//DMACell.prototype.getType = function(){ -// return null;//only used in freeform layout -//}; -// -//DMACell.prototype.getPosition = function(){ -// return this.mesh.position.clone(); -//}; -// -//DMACell.prototype.getOrientation = function(){ -// return this.mesh.quaternion.clone(); -//}; -// -//DMACell.prototype.getEulerRotation = function(){ -// return this.mesh.rotation.clone(); -//}; -// -//DMACell.prototype._calcPosition = function(){//need for part relay -// if (this.indices) return globals.lattice.getPositionForIndex(this.indices); -// return this.mesh.position;//used for cam simulation -//}; -// -//DMACell.prototype._setCellMeshVisibility = function(visibility){ -// this.mesh.visible = visibility; -//}; -// -//DMACell.prototype.xScale = function(){ -// return globals.lattice.xScale(0); -//}; -// -//DMACell.prototype.yScale = function(){ -// return globals.lattice.yScale(0); -//}; -// -//DMACell.prototype.zScale = function(){ -// return globals.lattice.zScale(0); -//}; -// -// -///////////////////////////////////////////////////////////////////////////////////////////////// -///////////////////////////////////META////////////////////////////////////////////////////////// -///////////////////////////////////////////////////////////////////////////////////////////////// -// -//DMACell.prototype._buildMesh = function(material){//called from every subclass -// var unitCellGeo = this._getGeometry(); -// if (!material) material = cellMaterials; -// var mesh = THREE.SceneUtils.createMultiMaterialObject(unitCellGeo, material); -// mesh.myParent = this;//we need a reference to this instance from the mesh for intersection selection stuff -// return mesh; -//}; -// -// -//DMACell.prototype._doMeshTransformations = function(mesh){};//by default, no mesh transformations -// -//DMACell.prototype._initParts = function(){ -// return [];//override in subclasses -//}; -// -//DMACell.prototype.changePartType = function(){//override in subclasses -//}; -// -//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) globals.lattice.removeCell(this);//if all parts are gone, remove cell -//}; -// -//DMACell.prototype.destroyParts = function(){ -// _.each(this.parts, function(part){ -// if (part) part.destroy(); -// }); -// this.parts = null; -//}; -// -//DMACell.prototype._initNodes = function(vertices){ -// var position = this.getPosition(); -// var orientation = this.getOrientation(); -// var nodes = []; -// for (var i=0;i<vertices.length;i++){ -// var vertex = vertices[i].clone(); -// vertex.applyQuaternion(orientation); -// vertex.add(position); -// nodes.push(new DmaNode(vertex, i)); -// } -// return nodes; -//}; -// -//DMACell.prototype._initBeams = function(nodes, faces){ -// var beams = []; -// var self = this; -// var addBeamFunc = function(index1, index2){ -// var duplicate = false; -// _.each(beams, function(beam){ -// var index = beam.getIndex(); -// if (index[0] == index1 && index[1] == index2) duplicate = true; -// }); -// if (duplicate) return; -// var diff = nodes[index1].getPosition(); -// diff.sub(nodes[index2].getPosition()); -// if (diff.length() > self.getScale()*1.01) return; -// if (index2>index1) { -// beams.push(new DmaBeam(nodes[index1], nodes[index2], self)); -// } -// }; -// for (var i=0;i<nodes.length;i++){ -// _.each(faces, function(face){ -// if (face.a == i) { -// addBeamFunc(i, face.b); -// addBeamFunc(i, face.c); -// } else if (face.b == i){ -// addBeamFunc(i, face.a); -// addBeamFunc(i, face.c); -// } else if (face.c == i){ -// addBeamFunc(i, face.a); -// addBeamFunc(i, face.b); -// } -// }) -// } -// return beams; -//}; -// -//DMACell.prototype.destroy = function(){ -// if (this.destroyStarted) return; -// this.destroyStarted = true; -// if (this.mesh) { -// globals.three.sceneRemove(this.mesh, this._sceneType(this.indices)); -// this.mesh.myParent = null; -//// this.mesh.dispose(); -//// geometry.dispose(); -//// material.dispose(); -// this.mesh = null; -// } -// this.destroyParts(); -// this.indices = null; -// this.nodes = null; -// this.beams = null; -// if (this.superCell) { -// this.superCell.destroy(); -// this.superCell = null; -// } -// this.superCellIndex = null; -//}; -// -//DMACell.prototype.toJSON = function(){ -// var data = { -// indices:this.indices//todo get rid of this and calculate from min and max -// }; -// if (this.parts) data.parts = this.parts; -// return data; -//}; \ No newline at end of file diff --git a/js/cells/OctaRotEdgeCell.js b/js/cells/OctaRotEdgeCell.js index 44c663e02ebe08c47e3f5f327791a4feeb0e946a..8580a8285543292dd47937dce1575feafe26ee9b 100644 --- a/js/cells/OctaRotEdgeCell.js +++ b/js/cells/OctaRotEdgeCell.js @@ -11,10 +11,7 @@ function OctaRotEdgeCell(indices){ OctaRotEdgeCell.prototype = Object.create(DMACell.prototype); OctaRotEdgeCell.prototype._initParts = function(){ - return this.changePartType(globals.lattice.get("partType")); -}; - -OctaRotEdgeCell.prototype.changePartType = function(type){ + var type = globals.lattice.get("partType"); var newParts = []; if (type == "vox"){ newParts.push(new OctaEdgeVoxPart(0)); @@ -24,9 +21,7 @@ OctaRotEdgeCell.prototype.changePartType = function(type){ console.warn("part type " + type + " not recognized"); return; } - if (!this.parts) return newParts; - this.destroyParts(); - this.parts = newParts; + return newParts; }; OctaRotEdgeCell.prototype._getGeometry = function(){ diff --git a/js/lattice/Lattice.js b/js/lattice/Lattice.js index e349ffa4bd78f5d92268365c2c871b9cf4e622e3..da455ee56a9bf1651776a3929e0e3f466b0dadf9 100644 --- a/js/lattice/Lattice.js +++ b/js/lattice/Lattice.js @@ -37,10 +37,12 @@ Lattice = Backbone.Model.extend({ //bind events this.listenTo(this, "change:gikLength", this._gikLengthDidChange); this.listenTo(globals.appState, "change:superCellIndex", this._gikLengthDidChange); - this.listenTo(globals.appState, "change:cellMode", this._updateForMode); + this.listenTo(this, "change:partType", this._updatePartType); this.listenTo(this, "change:cellType change:connectionType", this._updateLatticeType); this.listenTo(this, "change:cellSeparation", this._updateCellSeparation); + + this.listenTo(globals.appState, "change:cellMode", this._updateForMode); this.listenTo(globals.appState, "change:cellsVisible", this._setCellVisibility); }, @@ -77,7 +79,7 @@ Lattice = Backbone.Model.extend({ return newCells; }, - addCellAtIndex: function(indices, noRender, noCheck){ + addCellAtIndex: function(indices, noRender, noCheck){//no render no check from fill var cells = this.get("cells"); if (!noCheck) this.checkForMatrixExpansion(cells, indices, indices); @@ -154,13 +156,6 @@ Lattice = Backbone.Model.extend({ return {min:min, max:max}; }, - _allAxesScales: function(){ - var xScale = this.xScale(); - var yScale = this.yScale(); - var zScale = this.zScale(); - return {x:xScale, y:yScale, z:zScale}; - }, - //////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////FILL GEOMETRY//////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// @@ -312,14 +307,6 @@ Lattice = Backbone.Model.extend({ return false; }, - _subtract: function(pos1, pos2){ - return {x:pos1.x-pos2.x, y:pos1.y-pos2.y, z:pos1.z-pos2.z}; - }, - - _add: function(pos1, pos2){ - return {x:pos1.x+pos2.x, y:pos1.y+pos2.y, z:pos1.z+pos2.z}; - }, - //////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////EVENTS////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// @@ -355,12 +342,12 @@ Lattice = Backbone.Model.extend({ if (globals.highlighter.updateGikLength) globals.highlighter.updateGikLength(); }, - _setCellVisibility: function(){//todo maybe leave wireframes? + _setCellVisibility: function(){ if (globals.appState.get("cellsVisible")) this.showCells(); else this.hideCells(); }, - //hide show cells during stock simulation + //hide/show cells during stock simulation hideCells: function(){ this._iterCells(this.get("cells"), function(cell){ if (cell) cell.hide(); @@ -392,42 +379,17 @@ Lattice = Backbone.Model.extend({ this._setToDefaultsSilently(); this._setDefaultCellMode(); - if (typeof loadingFromFile == "undefined") loadingFromFile = false; - var cellType = this.get("cellType"); - var connectionType = this.get("connectionType"); + if (loadingFromFile === undefined) loadingFromFile = false; + if (this._undo) this._undo(); if (globals.basePlane) globals.basePlane.destroy(); if (globals.highlighter) globals.highlighter.destroy(); - if (cellType == "octa"){ - if (connectionType == "face"){ - _.extend(this, this.OctaFaceLattice); - } else if (connectionType == "freeformFace"){ - if (!loadingFromFile) this.clearCells(); - _.extend(this, this.OctaFreeFormFaceLattice); - } else if (connectionType == "edge"){ - _.extend(this, this.OctaFaceLattice); - _.extend(this, this.OctaEdgeLattice); - } else if (connectionType == "edgeRot"){ - _.extend(this, this.OctaRotEdgeLattice); - } else if (connectionType == "vertex"){ - _.extend(this, this.OctaVertexLattice); - } - } else if (cellType == "tetra"){ - _.extend(this, this.CubeLattice); - } else if (cellType == "cube"){ - if (connectionType == "face"){ - _.extend(this, this.CubeLattice); - } else if (connectionType == "gik"){ - if (!loadingFromFile) this.clearCells(); - _.extend(this, this.GIKLattice); - } - } else if (cellType == "truncatedCube"){ - _.extend(this, this.TruncatedCubeLattice); - } else if (cellType == "kelvin"){ - _.extend(this, this.KelvinLattice); - } + _.extend(this, this._getSubclassForLatticeType(loadingFromFile)); this._initLatticeType(); + + + //todo refactor this eventually var self = this; var cells = this.get("cells"); @@ -461,6 +423,38 @@ Lattice = Backbone.Model.extend({ globals.three.render(); }, + _getSubclassForLatticeType: function(loadingFromFile){ + var cellType = this.get("cellType"); + var connectionType = this.get("connectionType"); + if (cellType == "octa"){ + if (connectionType == "face"){ + return this.OctaFaceLattice; + } else if (connectionType == "freeformFace"){ + if (!loadingFromFile) this.clearCells(); + return this.OctaFreeFormFaceLattice; + } else if (connectionType == "edge"){ + return this.OctaEdgeLattice; + } else if (connectionType == "edgeRot"){ + return this.OctaRotEdgeLattice; + } else if (connectionType == "vertex"){ + return this.OctaVertexLattice; + } + } else if (cellType == "tetra"){ + return this.CubeLattice; + } else if (cellType == "cube"){ + if (connectionType == "face"){ + return this.CubeLattice; + } else if (connectionType == "gik"){ + if (!loadingFromFile) this.clearCells(); + return this.GIKLattice; + } + } else if (cellType == "truncatedCube"){ + return this.TruncatedCubeLattice; + } else if (cellType == "kelvin"){ + return this.KelvinLattice; + } + }, + _setToDefaultsSilently: function(){ var newCellType = this.get("cellType"); var newConnectionType = this.get("connectionType"); @@ -472,7 +466,7 @@ Lattice = Backbone.Model.extend({ this.set("partType", partType, {silent:true}); }, - _setDefaultCellMode: function(){ + _setDefaultCellMode: function(){//if no part associated with this lattice type if (!globals.plist["allPartTypes"][this.get("cellType")][this.get("connectionType")]){ globals.appState.set("cellMode", "cell"); } @@ -576,6 +570,21 @@ Lattice = Backbone.Model.extend({ return 1; }, + _allAxesScales: function(){ + var xScale = this.xScale(); + var yScale = this.yScale(); + var zScale = this.zScale(); + return {x:xScale, y:yScale, z:zScale}; + }, + + _subtract: function(pos1, pos2){ + return {x:pos1.x-pos2.x, y:pos1.y-pos2.y, z:pos1.z-pos2.z}; + }, + + _add: function(pos1, pos2){ + return {x:pos1.x+pos2.x, y:pos1.y+pos2.y, z:pos1.z+pos2.z}; + }, + //////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////UI/////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// diff --git a/js/lattice/OctaEdgeLattice.js b/js/lattice/OctaEdgeLattice.js index ab4db72a1a0e3486ccaffdebc4f11007829b00e9..803778c34785ea211e78a6e719c7b1733fe50deb 100644 --- a/js/lattice/OctaEdgeLattice.js +++ b/js/lattice/OctaEdgeLattice.js @@ -46,6 +46,20 @@ latticeSubclasses["OctaEdgeLattice"] = { return position; }, + xScale: function(cellSeparation){ + if (cellSeparation === undefined) cellSeparation = this.get("cellSeparation").xy; + return 1+2*cellSeparation; + }, + + yScale: function(cellSeparation){ + return this.xScale(cellSeparation)/2*Math.sqrt(3); + }, + + zScale: function(cellSeparation){ + if (cellSeparation === undefined) cellSeparation = this.get("cellSeparation").z; + return 2/Math.sqrt(6)+2*cellSeparation; + }, + makeCellForLatticeType: function(indices){ return new DMAEdgeOctaCell(indices); }, @@ -55,8 +69,5 @@ latticeSubclasses["OctaEdgeLattice"] = { _.each(_.keys(this.OctaEdgeLattice), function(key){ self[key] = null; }); - _.each(_.keys(this.OctaFaceLattice), function(key){ - self[key] = null; - }); } }