From 5f912ac7de92be6af57c342233384861cd5606ff Mon Sep 17 00:00:00 2001 From: Amanda Ghassaei <amandaghassaei@gmail.com> Date: Mon, 8 Jun 2015 19:30:52 -0700 Subject: [PATCH] add supercell --- js/cells/CubeCell.js | 2 +- js/cells/DMACell.js | 5 ++-- js/cells/supercells/DMASuperCell.js | 21 ++++++++-------- js/cells/supercells/GIKSuperCell.js | 33 +++++++++----------------- js/highlighter/Highlighter.js | 2 +- js/highlighter/SuperCellHighlighter.js | 6 ++--- js/lattice/GIKLattice.js | 4 ++-- js/lattice/Lattice.js | 3 +-- js/menus/LatticeMenuView.js | 4 ++-- js/models/AppState.js | 13 +++++----- 10 files changed, 41 insertions(+), 52 deletions(-) diff --git a/js/cells/CubeCell.js b/js/cells/CubeCell.js index ff808f38..5dee4090 100644 --- a/js/cells/CubeCell.js +++ b/js/cells/CubeCell.js @@ -6,7 +6,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'], function(_, THREE, three, lattice, appState, DMACell){ - var unitCellGeo = new THREE.BoxGeometry(1,1,1); + var unitCellGeo = new THREE.BoxGeometry(lattice.xScale(0),lattice.yScale(0),lattice.zScale(0)); function CubeCell(index, superCell){ DMACell.call(this, index, superCell); diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js index cdad1a39..5f53be34 100644 --- a/js/cells/DMACell.js +++ b/js/cells/DMACell.js @@ -8,11 +8,12 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals'], var wireframeMaterial = new THREE.MeshBasicMaterial({color:0x000000, wireframe:true}); - function DMACell(index, superCell){ + function DMACell(index, superCell, material){ if (index) this.index = new THREE.Vector3(index.x, index.y, index.z); if (superCell) this.superCell = superCell; - if (!this.cells) this.material = lattice.get("materialType"); + + this.material = material || lattice.get("materialType"); //object 3d is parent to all 3d elements owned by cell: cell mesh and wireframe, parts, beams, nodes, etc this.object3D = this._buildObject3D(); diff --git a/js/cells/supercells/DMASuperCell.js b/js/cells/supercells/DMASuperCell.js index c1ff7b5a..42fdec52 100644 --- a/js/cells/supercells/DMASuperCell.js +++ b/js/cells/supercells/DMASuperCell.js @@ -7,36 +7,35 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'], function(_, THREE, three, lattice, appState, DMACell){ - function DMASuperCell(index, material, superCell){//supercells might have supercells + function DMASuperCell(index, superCell){//supercells might have supercells - var range = lattice.get("superCellRange"); - this.cells = this._makeChildCells(index, range);//todo three dimensional array? DMACell.call(this, index, superCell); + + var range = appState.get("superCellRange"); + this.cells = this._makeChildCells(range, this.getMaterial()); this.setMode(); } DMASuperCell.prototype = Object.create(DMACell.prototype); - DMASuperCell.prototype._makeChildCells = function(index, range){ + DMASuperCell.prototype._makeChildCells = function(range, material){ var cells = []; for (var x=0;x<range.x;x++){ for (var y=0;y<range.y;y++){ for (var z=0;z<range.z;z++){ - cells.push(this._makeSubCellForIndex({x:x, y:y, z:z}));//child cells add themselves to object3D + //child cells add themselves to object3D + if (material.cells) var cellMaterial = material.cells[x][y][z].material; + if (cells[x][y][z]) cells.push(this._makeSubCellForIndex(new THREE.Vector3(x, y, z), cellMaterial || material)); } } } return cells; }; - DMASuperCell.prototype._makeSubCellForIndex = function(index){ + DMASuperCell.prototype._makeSubCellForIndex = function(index, material){ return null;//override in subclasses }; - DMASuperCell.prototype._getModeName = function(){ - return ""; - }; - DMASuperCell.prototype.setMode = function(mode){ DMACell.prototype.setMode.call(mode); _.each(this.cells, function(cell){ @@ -50,7 +49,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'], DMASuperCell.prototype.getLength = function(){ if (this.cells) return this.cells.length-1; - return lattice.get("superCellRange").x-1; + return appState.get("superCellRange").x-1;//zero indexed }; DMASuperCell.prototype._loopCells = function(callback){ diff --git a/js/cells/supercells/GIKSuperCell.js b/js/cells/supercells/GIKSuperCell.js index e5d07eca..4a25a5a5 100644 --- a/js/cells/supercells/GIKSuperCell.js +++ b/js/cells/supercells/GIKSuperCell.js @@ -4,16 +4,18 @@ -define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'superCell', 'gikCell', 'electronicMaterials'], - function(_, THREE, three, lattice, appState, DMASuperCell, GIKCell, electronicMaterials){ +define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'superCell', 'gikCell'], + function(_, THREE, three, lattice, appState, DMASuperCell, GIKCell){ + + var unitGeo = new THREE.BoxGeometry(lattice.xScale(0),lattice.yScale(0),lattice.zScale(0)); GIKSuperCell = function(index, superCell){ DMASuperCell.call(this, index, superCell); }; GIKSuperCell.prototype = Object.create(DMASuperCell.prototype); - GIKSuperCell.prototype._makeSubCellForIndex = function(index){ - return new GIKCell(index, this); + GIKSuperCell.prototype._makeSubCellForIndex = function(index, material){ + return new GIKCell(index, material, this); }; GIKSuperCell.prototype._rotateCell = function(object3D){ @@ -21,24 +23,11 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'superCell', return object3D; }; - GIKSuperCell.prototype.getMaterial = function(){ - return electronicMaterials.materials[this.material]; - }; - - GIKSuperCell.prototype._buildMesh = function(){ - var length = lattice.get("superCellRange").x; - var meshes = []; - var superCellGeo = new THREE.BoxGeometry(1,1,1.28); - 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.getMaterial()); - 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._getGeometry = function(){ + var geo = unitGeo.clone(); + var length = this.getLength() + 1; + geo.applyMatrix(new THREE.Matrix4().makeScale(length, 1, 1)); + geo.applyMatrix(new THREE.Matrix4().makeTranslation(-length/2+0.5, 0, 0)); }; GIKSuperCell.prototype._buildWireframe = function(mesh){ diff --git a/js/highlighter/Highlighter.js b/js/highlighter/Highlighter.js index b1b937b1..2788d308 100644 --- a/js/highlighter/Highlighter.js +++ b/js/highlighter/Highlighter.js @@ -26,7 +26,7 @@ define(['underscore', 'backbone', 'threeModel', 'appState', 'lattice', 'cell', ' this.hide(); //bind events - this.listenTo(lattice, "change:superCellRange", this._superCellParamDidChange); + this.listenTo(appState, "change:superCellRange", this._superCellParamDidChange); this.listenTo(appState, "change:superCellIndex", this._superCellParamDidChange); this.listenTo(appState, "change:deleteMode", this._updateDeleteMode); diff --git a/js/highlighter/SuperCellHighlighter.js b/js/highlighter/SuperCellHighlighter.js index 02818430..10326a54 100644 --- a/js/highlighter/SuperCellHighlighter.js +++ b/js/highlighter/SuperCellHighlighter.js @@ -17,7 +17,7 @@ define(['underscore', 'backbone', 'threeModel', 'appState', 'lattice', 'cell', ' _setRotation: function(direction){ if (!this.highlightedObject) return; - var index = this.highlightedObject.getIndex(); + var index = this.highlightedObject.getAbsoluteIndex(); var superCellIndex = 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); @@ -27,7 +27,7 @@ define(['underscore', 'backbone', 'threeModel', 'appState', 'lattice', 'cell', ' updateGikLength: function(){ if (!this.mesh) return; - this.mesh.scale.set(lattice.get("superCellRange").x, lattice.get("superCellRange").y, lattice.get("superCellRange").z); + this.mesh.scale.set(appState.get("superCellRange").x, appState.get("superCellRange").y, appState.get("superCellRange").z); three.render(); if (!this.direction) return; this._setPosition(this.position, this.direction);//position of center point @@ -36,7 +36,7 @@ define(['underscore', 'backbone', 'threeModel', 'appState', 'lattice', 'cell', ' }, _getNextCellPosition: function(){//add direction vector to current index - var newIndex = this.highlightedObject.getIndex(); + var newIndex = this.highlightedObject.getAbsoluteIndex(); var direction = this.direction; _.each(_.keys(newIndex), function(key){ newIndex[key] = Math.round(newIndex[key] + direction[key]); diff --git a/js/lattice/GIKLattice.js b/js/lattice/GIKLattice.js index 066c0375..79f9c592 100644 --- a/js/lattice/GIKLattice.js +++ b/js/lattice/GIKLattice.js @@ -11,8 +11,8 @@ define(['underscore', 'backbone', 'appState', 'lattice', 'globals', 'plist', 'th require(['squareBaseplane'], function(SquareBasePlane){ globals.basePlane = new SquareBasePlane(); }); - require(['gikHighlighter'], function(GIKHighlighter){ - globals.highlighter = new GIKHighlighter(); + require(['superCellHighlighter'], function(superCellHighlighter){ + globals.highlighter = new superCellHighlighter(); globals.highlighter.updateGikLength(); }); }, diff --git a/js/lattice/Lattice.js b/js/lattice/Lattice.js index a5cdd3bc..06ba9eed 100644 --- a/js/lattice/Lattice.js +++ b/js/lattice/Lattice.js @@ -26,8 +26,7 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre connectionType: "face", partType: null, materialType: null, - materialClass: "mechanical", - superCellRange: new THREE.Vector3(4,1,1) + materialClass: "mechanical" }, initialize: function(){ diff --git a/js/menus/LatticeMenuView.js b/js/menus/LatticeMenuView.js index 1d75d2af..8d92a818 100644 --- a/js/menus/LatticeMenuView.js +++ b/js/menus/LatticeMenuView.js @@ -15,7 +15,7 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice'], function($, _ }, _makeTemplateJSON: function(){ - return _.extend(lattice.toJSON(), plist); + return _.extend(_.extend(lattice.toJSON(), this.model.toJSON()), plist); }, template: _.template('\ @@ -38,7 +38,7 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice'], function($, _ </ul>\ </div><br/><br/>\ <% if (connectionType == "gik") { %>\ - GIK Length: <input data-property="superCellRange" data-key="x" value="<%= superCellRange.x %>" 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 appState" 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 5684a6ef..a02882b0 100644 --- a/js/models/AppState.js +++ b/js/models/AppState.js @@ -5,7 +5,7 @@ //a class to store global app state, model for navbar and menu wrapper //never deallocated -define(['underscore', 'backbone', 'threeModel'], function(_, Backbone, three){ +define(['underscore', 'backbone', 'threeModel', 'three'], function(_, Backbone, three, THREE){ var AppState = Backbone.Model.extend({ @@ -36,7 +36,8 @@ define(['underscore', 'backbone', 'threeModel'], function(_, Backbone, three){ cellMode: "cell",//supercell, cell, part, node, beam cellsVisible: true, - superCellIndex: 0,//offset of superCell adds todo lattice? + superCellIndex: 0,//offset of superCell adds + superCellRange: new THREE.Vector3(4,1,1), realisticColorScheme: false, @@ -191,22 +192,22 @@ define(['underscore', 'backbone', 'threeModel'], function(_, Backbone, three){ case 57: if (globals.lattice.get("connectionType") != "gik") break; if (state) { - var range = globals.lattice.get("superCellRange").clone(); + var range = this.get("superCellRange").clone(); range.x = e.keyCode-48; - globals.lattice.set("superCellRange", range); + this.set("superCellRange", range); } break; case 81://q - increase supercell index if (state) { var index = this.get("superCellIndex")+1; - if (index > globals.lattice.get("superCellRange").x-1) index = 0; + if (index > this.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("superCellRange").x-1; + if (index < 0) index = this.get("superCellRange").x-1; this.set("superCellIndex", index); } break; -- GitLab