From 66f346ef081ca7929478dcdaccf9aa9223178806 Mon Sep 17 00:00:00 2001 From: Amanda Ghassaei <amandaghassaei@gmail.com> Date: Mon, 1 Jun 2015 20:00:31 -0700 Subject: [PATCH] supercell parent class --- index.html | 1 + js/cells/DMACell.js | 62 +++++++++++------------ js/cells/GIKCell.js | 9 ++-- js/cells/supercells/DMASuperCell.js | 76 +++++++++++++++++++++++++++++ js/cells/supercells/GIKSuperCell.js | 71 +++++---------------------- js/lattice/GIKLattice.js | 14 ------ js/lattice/Lattice.js | 2 +- js/menus/LatticeMenuView.js | 2 +- js/models/AppState.js | 10 ++-- js/three/BasePlane.js | 9 ++-- js/three/Highlighter.js | 25 +++++----- 11 files changed, 152 insertions(+), 129 deletions(-) create mode 100644 js/cells/supercells/DMASuperCell.js diff --git a/index.html b/index.html index 8baea581..89b37990 100644 --- a/index.html +++ b/index.html @@ -52,6 +52,7 @@ <script src="js/fea/DmaBeam.js"></script> <script src="js/cells/DMACell.js"></script> + <script src="js/cells/supercells/DMASuperCell.js"></script> <script src="js/cells/OctaFaceCell.js"></script> <script src="js/cells/OctaEdgeCell.js"></script> <script src="js/cells/OctaRotEdgeCell.js"></script> diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js index 1701f538..c3bc06a8 100644 --- a/js/cells/DMACell.js +++ b/js/cells/DMACell.js @@ -8,7 +8,7 @@ var wireframeMaterial = new THREE.MeshBasicMaterial({color:0x000000, wireframe:t function DMACell(index, superCell){ - this.index = index; + this.index = new THREE.Vector3(index.x, index.y, index.z); if (superCell) this.superCell = superCell; //object 3d is parent to all 3d elements related to cell, parts, beams, nodes, etc @@ -16,7 +16,7 @@ function DMACell(index, superCell){ this._addChildren(this._buildMesh(), this.object3D);//build cell meshes if (superCell === undefined) { - if (this.index) globals.three.sceneAdd(this.object3D, "cell"); + if (this.index) globals.three.sceneAdd(this.object3D, this._getSceneName()); else this.hide();//stock cell } @@ -26,15 +26,44 @@ function DMACell(index, superCell){ DMACell.prototype._buildObject3D = function(){ var object3D = this._translateCell(this._rotateCell(new THREE.Object3D())); object3D.myParent = this;//reference to get mouse raycasting back - object3D.name = "object3D"; + object3D.name = this._getModeName(); return object3D; }; +DMACell.prototype._getModeName = function(){ + return "object3D"; +}; + +DMACell.prototype._getSceneName = function(){ + return "cell"; +}; + DMACell.prototype.getObject3D = function(){//careful, used for stock sim and supercell only for now todo need this? return this.object3D; }; +DMACell.prototype.getIndex = function(){ + var index = this.index.clone(); + console.log(index); + index = this.getAbsoluteOrientation(index); + if (this.superCell) index.add(this.superCell.getIndex()); + return index; +}; + +DMACell.prototype.getPosition = function(){ + var position = this.object3D.position.clone(); + position = this.getAbsoluteOrientation(position); + if (this.superCell) position.add(this.superCell.getPosition()); + return position; +}; + +DMACell.prototype.getAbsoluteOrientation = function(vector){ + vector.applyQuaternion(this.getQuaternion()); + if (this.superCell) return this.superCell.getAbsoluteOrientation(vector); + return vector; +}; + @@ -141,19 +170,6 @@ DMACell.prototype.setMode = function(mode){ }); }; -DMACell.prototype.getPosition = function(){ - if (this.superCell && this.index) return this.superCell.getSubCellPosition(_.clone(this.index)); - return this.object3D.position.clone(); -}; - -DMACell.prototype.getSubCellPosition = function(subIndex){ - var index = _.clone(this.index); - _.each(_.keys(index), function(key){ - index[key] += subIndex[key]; - }); - return globals.lattice.getIndexForPosition(index); -}; - DMACell.prototype.getQuaternion = function(){ return this.object3D.quaternion.clone(); }; @@ -231,20 +247,6 @@ DMACell.prototype.toJSON = function(){ }; - -//DMACell.prototype.moveTo = function(position, axis){//used for stock simulations -// this.object3D.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._initNodes = function(vertices){ // var position = this.getPosition(); // var orientation = this.getOrientation(); diff --git a/js/cells/GIKCell.js b/js/cells/GIKCell.js index 07443ce3..bd83f8f3 100644 --- a/js/cells/GIKCell.js +++ b/js/cells/GIKCell.js @@ -19,7 +19,7 @@ GIKCell.prototype._translateCell = function(object3D){ if (this.index) { - var offset = this.index-this.superCell.getLength(); + var offset = this.index.x-this.superCell.getLength(); object3D.position.set(offset*this.xScale(),0, 0); } return object3D; @@ -27,7 +27,7 @@ GIKCell.prototype._rotateCell = function(object3D){ var length = this.superCell.getLength(); - if (this.index.x == length || this.index.y == length) object3D.rotateZ(Math.PI); + if (this.index.x == length) object3D.rotateZ(Math.PI); return object3D; }; @@ -38,7 +38,8 @@ GIKCell.prototype._initParts = function(){ if (!this.superCell) return null; var parts = []; - var isEnd = this.superCellIndex == 0 || this.superCellIndex == this.superCell.getLength(); + var isEnd = false; +// var isEnd = this.superCellIndex == 0 || this.superCellIndex == this.superCell.getLength(); if (globals.lattice.get("partType") == "lego") { if (isEnd) parts.push(new DMAGIKEndPart(0, this)); else parts.push(new DMAGIKPart(0, this)); @@ -52,7 +53,7 @@ GIKCell.prototype.calcHighlighterPosition = function(face){ var direction = face.normal.clone().applyEuler(this.object3D.rotation).applyEuler(this.object3D.parent.rotation); - var position = globals.lattice.getPositionForIndex(this.index); + var position = this.getPosition(); var self = this; _.each(_.keys(position), function(key){ position[key] += direction[key]*self.axisScale(key)/2; diff --git a/js/cells/supercells/DMASuperCell.js b/js/cells/supercells/DMASuperCell.js new file mode 100644 index 00000000..b2feb87b --- /dev/null +++ b/js/cells/supercells/DMASuperCell.js @@ -0,0 +1,76 @@ +/** + * Created by aghassaei on 6/1/15. + */ + + +DMASuperCell = function(index, superCell){//supercells might have supercells + + DMACell.call(this, index, superCell); + var range = globals.lattice.get("superCellRange"); + this.material = globals.lattice.get("materialType");//todo move to dmacell + this.cells = this._makeChildCells(index, range);//three dimensional array + + var self = this; + _.each(this.cells, function(cell){ + self._addChildren(cell.getObject3D()); + }); + + this.setMode(); +}; +DMASuperCell.prototype = Object.create(DMACell.prototype); + +DMASuperCell.prototype._makeChildCells = function(index, range){ + var cells = [];//todo make cells a 3d array? + for (var x=0;i<range.x;x++){ + for (var y=0;y<range.y;y++){ + for (var z=0;i<range.z;z++){ + cells.push(this._makeSubCellForIndex({x:x, y:y, z:z})); + } + } + } + return cells; +}; + +DMASuperCell.prototype._makeSubCellForIndex = function(index){ + return null;//override in subclasses +}; + +DMASuperCell.prototype._getModeName = function(){ + return ""; +}; + +DMASuperCell.prototype._getSceneName = function(){ + return "supercell"; +}; + +DMASuperCell.prototype.setMode = function(mode){ + + if (mode === undefined) mode = globals.appState.get("cellMode"); + + _.each(this.cells, function(cell){ + cell.setMode(mode); + }); + + if (mode == "cell") mode = "supercell"; + if (mode == "part") mode = "object3D"; + + _.each(this.object3D.children, function(child){ + child.visible = child.name == mode; + }); +}; + +DMASuperCell.prototype.getLength = function(){ + return this.cells.length-1; +}; + +DMASuperCell.prototype.destroy = function(){ + this.object3D.myParent = null; + globals.three.sceneRemove(this.object3D, this._getSceneName()); + this.object3D = null; + _.each(this.cells, function(cell){ + if (cell) cell.destroy(); + }); + this.cells = null; + this.index = null; + this.material = null; +}; \ No newline at end of file diff --git a/js/cells/supercells/GIKSuperCell.js b/js/cells/supercells/GIKSuperCell.js index eaea5ba0..be95d73f 100644 --- a/js/cells/supercells/GIKSuperCell.js +++ b/js/cells/supercells/GIKSuperCell.js @@ -32,40 +32,13 @@ function changeGikMaterials(){ }); } - -GIKSuperCell = function(index, length){ - if (index) this.index = index; - if (length === undefined) length = globals.lattice.get("gikLength"); - this.material = globals.lattice.get("materialType"); - this.cells = this._makeChildCells(index, length); - - this.object3D = this._buildObject3D(); - this._addChildren(this._buildMesh(length), this.object3D); - var self = this; - _.each(this.cells, function(cell){ - self._addChildren(cell.getObject3D()); - }); - - if (this.index) globals.three.sceneAdd(this.object3D, "supercell"); - else (this.hide()); - - this.setMode(); +GIKSuperCell = function(index, superCell){ + DMASuperCell.call(this, index, superCell); }; -GIKSuperCell.prototype = Object.create(DMACell.prototype); +GIKSuperCell.prototype = Object.create(DMASuperCell.prototype); -GIKSuperCell.prototype._makeChildCells = function(index, length){ - var cells = []; - for (var i=0;i<length;i++){ - var childIndices = {x:0, y:0, z:0}; - if (index.z == 0) childIndices.x += i; - else childIndices.y += i; - cells.push(new GIKCell(childIndices, this)); - } - return cells; -}; - -GIKSuperCell.prototype._buildObject3D = function(){//todo merge this with dma cell - return this._translateCell(this._rotateCell(new THREE.Object3D())); +GIKSuperCell.prototype._makeSubCellForIndex = function(index){ + return new GIKCell(index, this); }; GIKSuperCell.prototype._rotateCell = function(object3D){ @@ -73,7 +46,8 @@ GIKSuperCell.prototype._rotateCell = function(object3D){ return object3D; }; -GIKSuperCell.prototype._buildMesh = function(length){ +GIKSuperCell.prototype._buildMesh = function(){ + var length = globals.lattice.get("gikLength"); var meshes = []; var superCellGeo = new THREE.BoxGeometry(1,1,1.28); superCellGeo.applyMatrix(new THREE.Matrix4().makeScale(length, 1, 1)); @@ -100,37 +74,14 @@ GIKSuperCell.prototype.getMaterial = function(){ return allGIKMaterials[this.material]; }; -GIKSuperCell.prototype.setMode = function(mode){ - - if (mode === undefined) mode = globals.appState.get("cellMode"); - - _.each(this.cells, function(cell){ - cell.setMode(mode); - }); - - if (mode == "cell") mode = "supercell"; - if (mode == "part") mode = "object3D"; - - _.each(this.object3D.children, function(child){ - child.visible = child.name == mode; - }); - - console.log(this.object3D); -}; - -GIKSuperCell.prototype.getLength = function(){ - return globals.lattice.get("gikLength"); -}; - GIKSuperCell.prototype.destroy = function(){ - if (this.destroyStarted) return;//prevents loop destroy from cells - this.destroyStarted = true; - globals.three.sceneRemove(this.object3D); + this.object3D.myParent = null; + globals.three.sceneRemove(this.object3D, this._getSceneName()); this.object3D = null; _.each(this.cells, function(cell){ - if (cell && cell.index && !cell.destroyStarted) globals.lattice.removeCell(cell); + if (cell) cell.destroy(); }); this.cells = null; this.index = null; this.material = null; -} \ No newline at end of file +}; \ No newline at end of file diff --git a/js/lattice/GIKLattice.js b/js/lattice/GIKLattice.js index a5eb6b4f..cc50834c 100644 --- a/js/lattice/GIKLattice.js +++ b/js/lattice/GIKLattice.js @@ -38,20 +38,6 @@ latticeSubclasses["GIKLattice"] = { return new GIKSuperCell(indices); }, -// makeSuperCell: function(range){ -// var length = this.get("gikLength"); -// var cells; -// if (range) cells = this.addCellsInRange(range); -// else {//this is for assembler stock only -// cells = []; -// for (var i=0;i<length;i++){ -// cells.push(this.makeCellForLatticeType(null)); -// } -// } -// if (cells.length < 1) return null; -// return new GIKSuperCell(length, range, cells); -// }, - // _rasterGikCells: function(order, callback, var1, var2, var3, cells){ // for (var i=this._getRasterLoopInit(var1);this._getRasterLoopCondition(i,var1);i+=this._getRasterLoopIterator(var1)){ // for (var j=this._getRasterLoopInit(var2);this._getRasterLoopCondition(j,var2);j+=this._getRasterLoopIterator(var2)){ diff --git a/js/lattice/Lattice.js b/js/lattice/Lattice.js index bd5b8fde..8882bfde 100644 --- a/js/lattice/Lattice.js +++ b/js/lattice/Lattice.js @@ -25,7 +25,7 @@ Lattice = Backbone.Model.extend({ connectionType: "gik", partType: "lego", materialType: "fiberGlass", - gikLength: 4 + superCellRange: new THREE.Vector3(4,1,1) }, //pass in fillGeometry diff --git a/js/menus/LatticeMenuView.js b/js/menus/LatticeMenuView.js index f2c04856..ba485f5e 100644 --- a/js/menus/LatticeMenuView.js +++ b/js/menus/LatticeMenuView.js @@ -56,7 +56,7 @@ LatticeMenuView = Backbone.View.extend({ <br/><br/>\ <% } %>\ <% if (connectionType == "gik") { %>\ - GIK Length: <input data-property="gikLength" value="<%= gikLength %>" placeholder="GIK length" class="form-control intInput lattice" type="text"><br/>\ + GIK Length: <input data-property="superCellRange" data-key="x" value="<%= superCellRange.x %>" placeholder="GIK length" class="form-control intInput lattice" type="text"><br/>\ <br/>\ <% } %>\ <a href="#" class="clearCells btn btn-block btn-lg btn-danger">Clear All Cells</a><br/>\ diff --git a/js/models/AppState.js b/js/models/AppState.js index cfa6390b..10dbaf11 100644 --- a/js/models/AppState.js +++ b/js/models/AppState.js @@ -184,19 +184,23 @@ AppState = Backbone.Model.extend({ case 56: case 57: if (globals.lattice.get("connectionType") != "gik") break; - if (state) globals.lattice.set("gikLength", e.keyCode-48); + if (state) { + var range = globals.lattice.get("superCellRange").clone(); + range.x = e.keyCode-48; + globals.lattice.set("superCellRange", range); + } break; case 81://q - increase supercell index if (state) { var index = this.get("superCellIndex")+1; - if (index > globals.lattice.get("gikLength")-1) index = 0; + if (index > globals.lattice.get("superCellRange").x-1) index = 0; this.set("superCellIndex", index); } break; case 65://a - decrease supercell index if (state) { var index = this.get("superCellIndex")-1; - if (index < 0) index = globals.lattice.get("gikLength")-1; + if (index < 0) index = globals.lattice.get("superCellRange").x-1; this.set("superCellIndex", index); } break; diff --git a/js/three/BasePlane.js b/js/three/BasePlane.js index 654231d8..9ebc08ed 100644 --- a/js/three/BasePlane.js +++ b/js/three/BasePlane.js @@ -43,9 +43,9 @@ BasePlane = Backbone.Model.extend({ globals.three.render(); }, - /////////////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////DEALLOC//////////////////////////////////// - /////////////////////////////////////////////////////////////////////////////////// + getIndex: function(){ + return this.index.clone(); + }, _removeMesh: function(){ this.get("object3D").myParent = null; @@ -166,6 +166,7 @@ OctaBasePlane = BasePlane.extend({ index.z = this.get("zIndex") - 1;//pretend we're on the top of the cell underneath the baseplane var position = globals.lattice.getPositionForIndex(index); position.z += globals.lattice.zScale()/2; + this.index = new THREE.Vector3(index.x, index.y, index.z);//todo no!!! return {index: index, direction: new THREE.Vector3(0,0,1), position:position}; } @@ -221,6 +222,7 @@ SquareBasePlane = BasePlane.extend({ index.z = this.get("zIndex") - 1;//pretend we're on the top of the cell underneath the baseplane var latticePosition = globals.lattice.getPositionForIndex(index); latticePosition.z += globals.lattice.zScale()/2; + this.index = new THREE.Vector3(index.x, index.y, index.z);//todo no!!! return {index: index, direction: new THREE.Vector3(0,0,1), position:latticePosition}; } @@ -240,6 +242,7 @@ RotEdgeOctaBasePlane = SquareBasePlane.extend({ var latticePosition = globals.lattice.getPositionForIndex(index); latticePosition.x -= globals.lattice.xScale()/2; latticePosition.y -= globals.lattice.yScale()/2; + this.index = new THREE.Vector3(index.x, index.y, index.z);//todo no!!! return {index: index, direction: new THREE.Vector3(0,0,1), position:latticePosition}; } }); \ No newline at end of file diff --git a/js/three/Highlighter.js b/js/three/Highlighter.js index 3634e4f6..44ac5424 100644 --- a/js/three/Highlighter.js +++ b/js/three/Highlighter.js @@ -6,7 +6,6 @@ Highlighter = Backbone.View.extend({ mesh: null, highlightedObject: null, - index: null, direction: null, initialize: function(){ @@ -25,7 +24,7 @@ Highlighter = Backbone.View.extend({ this.hide(); //bind events - this.listenTo(globals.lattice, "change:gikLength", this._superCellParamDidChange); + this.listenTo(globals.lattice, "change:superCellRange", this._superCellParamDidChange); this.listenTo(globals.appState, "change:superCellIndex", this._superCellParamDidChange); }, @@ -56,7 +55,6 @@ Highlighter = Backbone.View.extend({ setNothingHighlighted: function(){ this.highlightedObject = null; - this.index = null; this.direction = null; this.position = null; this.hide(); @@ -73,8 +71,7 @@ Highlighter = Backbone.View.extend({ this.highlightedObject = highlighted.myParent; - var highlightedPos = highlighted.myParent.calcHighlighterPosition(intersection.face, intersection.point); - this.index = highlightedPos.index; + var highlightedPos = this.highlightedObject.calcHighlighterPosition(intersection.face, intersection.point); this.position = highlightedPos.position;//todo used just for gik if (!highlightedPos.direction) {//may be hovering over a face that we shouldn't highlight this.hide(); @@ -82,7 +79,7 @@ Highlighter = Backbone.View.extend({ } this.direction = highlightedPos.direction; this._setPosition(highlightedPos.position, this.direction);//position of center point - this._setRotation(this.direction, this.index); + this._setRotation(this.direction); this.show(true); }, @@ -91,7 +88,7 @@ Highlighter = Backbone.View.extend({ /////////////////////////////POSITION/SCALE//////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////// - getHighlightedObjectPosition: function(){ + getHighlightedObjectPosition: function(){//origin selection if (this.highlightedObject instanceof DMACell) { var position = this.highlightedObject.getPosition(); return { @@ -120,7 +117,7 @@ Highlighter = Backbone.View.extend({ /////////////////////////////////////////////////////////////////////////////////// _getNextCellPosition: function(){//add direction vector to current index - var newIndex = _.clone(this.index); + var newIndex = _.clone(this.highlightedObject.getIndex()); var direction = this.direction; _.each(_.keys(newIndex), function(key){ newIndex[key] = Math.round(newIndex[key] + direction[key]); @@ -168,7 +165,7 @@ OctaFaceHighlighter = Highlighter.extend({ }, _setRotation: function(){ - this.mesh.rotation.set(0,0,(this.index.z+1)%2*Math.PI); + this.mesh.rotation.set(0,0,(this.highlightedObject.getIndex().z+1)%2*Math.PI); } }); @@ -222,7 +219,9 @@ GIKHighlighter = Highlighter.extend({ this.mesh.position.set(position.x+direction.x/2, position.y+direction.y/2, position.z+globals.lattice.zScale()*direction.z/2); }, - _setRotation: function(direction, index){ + _setRotation: function(direction){ + if (!this.highlightedObject) return; + var index = this.highlightedObject.getIndex(); var superCellIndex = globals.appState.get("superCellIndex"); if ((index.z%2 == 0 && Math.abs(direction.z) > 0.9) || (index.z%2 != 0 && Math.abs(direction.z) < 0.1)) this.mesh.rotation.set(0, 0, Math.PI/2); @@ -232,16 +231,16 @@ GIKHighlighter = Highlighter.extend({ updateGikLength: function(){ if (!this.mesh) return; - this.mesh.scale.set(globals.lattice.get("gikLength"), 1, 1); + this.mesh.scale.set(globals.lattice.get("superCellRange").x, globals.lattice.get("superCellRange").y, globals.lattice.get("superCellRange").z); globals.three.render(); if (!this.direction) return; this._setPosition(this.position, this.direction);//position of center point - this._setRotation(this.direction, this.index); + this._setRotation(this.direction); globals.three.render(); }, _getNextCellPosition: function(){//add direction vector to current index - var newIndex = _.clone(this.index); + var newIndex = this.highlightedObject.getIndex(); var direction = this.direction; _.each(_.keys(newIndex), function(key){ newIndex[key] = Math.round(newIndex[key] + direction[key]); -- GitLab