diff --git a/js/cells/CubeCell.js b/js/cells/CubeCell.js index 9a17f9239487b3c7fd2aa67af998a6335d8e6235..c73ca5c7f6cf520ba5a27777af33579604bb749b 100644 --- a/js/cells/CubeCell.js +++ b/js/cells/CubeCell.js @@ -3,7 +3,7 @@ */ -(function () { +define(['cell'], function(DMACell){ var unitCellGeo = new THREE.BoxGeometry(1,1,1); @@ -35,6 +35,6 @@ return {index: _.clone(this.index), direction:direction, position:position}; }; - self.CubeCell = CubeCell; + return CubeCell; -})(); \ No newline at end of file +}); \ No newline at end of file diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js index c3bc06a824a76297c0a24a2cf3ffedc075102aaf..661eb50773fa7409cd17d539fa4c3536e3594911 100644 --- a/js/cells/DMACell.js +++ b/js/cells/DMACell.js @@ -3,230 +3,240 @@ */ -var cellMaterial = new THREE.MeshNormalMaterial(); -var wireframeMaterial = new THREE.MeshBasicMaterial({color:0x000000, wireframe:true}); +define(['three', 'threeModel', 'lattice', 'appState'], function(THREE, three, lattice, appState){ -function DMACell(index, superCell){ + var cellMaterial = new THREE.MeshNormalMaterial(); + var wireframeMaterial = new THREE.MeshBasicMaterial({color:0x000000, wireframe:true}); - this.index = new THREE.Vector3(index.x, index.y, index.z); - if (superCell) this.superCell = superCell; + function DMACell(index, superCell){ - //object 3d is parent to all 3d elements related to cell, parts, beams, nodes, etc - this.object3D = this._buildObject3D(); - this._addChildren(this._buildMesh(), this.object3D);//build cell meshes + this.index = new THREE.Vector3(index.x, index.y, index.z); + if (superCell) this.superCell = superCell; - if (superCell === undefined) { - if (this.index) globals.three.sceneAdd(this.object3D, this._getSceneName()); - else this.hide();//stock cell + //object 3d is parent to all 3d elements related to cell, parts, beams, nodes, etc + this.object3D = this._buildObject3D(); + this._addChildren(this._buildMesh(), this.object3D);//build cell meshes + + if (superCell === undefined) { + if (this.index) three.sceneAdd(this.object3D, this._getSceneName()); + else this.hide();//stock cell + } + + this.setMode(); } - this.setMode(); -} - -DMACell.prototype._buildObject3D = function(){ - var object3D = this._translateCell(this._rotateCell(new THREE.Object3D())); - object3D.myParent = this;//reference to get mouse raycasting back - 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; -}; - - - - - - -DMACell.prototype._rotateCell = function(object3D){ - return object3D;//by default, no mesh transformations -}; - -DMACell.prototype._translateCell = function(object3D){ - if (!this.index) return object3D; - var position = globals.lattice.getPositionForIndex(this.index); - object3D.position.set(position.x, position.y, position.z); - return object3D; -}; - -DMACell.prototype._buildMesh = function(){ - var geometry = this._getGeometry(); - var meshes = []; - var mesh = new THREE.Mesh(geometry, this.getMaterial()); - mesh.name = "cell"; - meshes.push(mesh); - var wireframe = this._buildWireframe(mesh, geometry); - if (!wireframe) return meshes; - wireframe.name = "cell"; - meshes.push(wireframe); - return meshes; -}; - -DMACell.prototype._buildWireframe = function(mesh, geometry){//abstract mesh representation of cell - return new THREE.Mesh(geometry, wireframeMaterial); -}; - -DMACell.prototype._addChildren = function(children, object3D){//accepts an array or a single mesh - this._addRemoveChildren(true, children, object3D); -}; - -DMACell.prototype._removeChildren = function(children, object3D){//accepts an array or a single mesh - this._addRemoveMeshes(false, children, object3D); -}; - -DMACell.prototype._addRemoveChildren = function(shouldAdd, children, object3D){//accepts an array or a single mesh - if (object3D === undefined) object3D = this.object3D; - if (children.constructor === Array){ - _.each(children, function(child){ - if (shouldAdd) object3D.add(child); - else object3D.remove(child); - }); - } else if (shouldAdd) object3D.add(children); - else object3D.remove(children); -}; + DMACell.prototype._buildObject3D = function(){ + var object3D = this._translateCell(this._rotateCell(new THREE.Object3D())); + object3D.myParent = this;//reference to get mouse raycasting back + object3D.name = this._getModeName(); + return object3D; + }; -DMACell.prototype.hide = function(){ - this.object3D.visible = false; -}; + DMACell.prototype._getModeName = function(){ + return "object3D"; + }; -DMACell.prototype.show = function(mode){ - this.object3D.visible = true; - this.setMode(mode); -}; + DMACell.prototype._getSceneName = function(){ + return "cell"; + }; -DMACell.prototype.getMaterial = function(){ - return cellMaterial; -}; + DMACell.prototype.getObject3D = function(){//careful, used for stock sim and supercell only for now todo need this? + return this.object3D; + }; -DMACell.prototype.setOpacity = function(opacity){ -}; + 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; + }; + DMACell.prototype._rotateCell = function(object3D){ + return object3D;//by default, no mesh transformations + }; + DMACell.prototype._translateCell = function(object3D){ + if (!this.index) return object3D; + var position = lattice.getPositionForIndex(this.index); + object3D.position.set(position.x, position.y, position.z); + return object3D; + }; -DMACell.prototype._initParts = function(){ - return [];//override in subclasses -}; + DMACell.prototype._buildMesh = function(){ + var geometry = this._getGeometry(); + var meshes = []; + var mesh = new THREE.Mesh(geometry, this.getMaterial()); + mesh.name = "cell"; + meshes.push(mesh); + var wireframe = this._buildWireframe(mesh, geometry); + if (!wireframe) return meshes; + wireframe.name = "cell"; + meshes.push(wireframe); + return meshes; + }; + + DMACell.prototype._buildWireframe = function(mesh, geometry){//abstract mesh representation of cell + return new THREE.Mesh(geometry, wireframeMaterial); + }; + + DMACell.prototype._addChildren = function(children, object3D){//accepts an array or a single mesh + this._addRemoveChildren(true, children, object3D); + }; + + DMACell.prototype._removeChildren = function(children, object3D){//accepts an array or a single mesh + this._addRemoveMeshes(false, children, object3D); + }; + + DMACell.prototype._addRemoveChildren = function(shouldAdd, children, object3D){//accepts an array or a single mesh + if (object3D === undefined) object3D = this.object3D; + if (children.constructor === Array){ + _.each(children, function(child){ + if (shouldAdd) object3D.add(child); + else object3D.remove(child); + }); + } else if (shouldAdd) object3D.add(children); + else object3D.remove(children); + }; + + DMACell.prototype.hide = function(){ + this.object3D.visible = false; + }; + + DMACell.prototype.show = function(mode){ + this.object3D.visible = true; + this.setMode(mode); + }; + + DMACell.prototype.getMaterial = function(){ + return cellMaterial; + }; + + DMACell.prototype.setOpacity = function(opacity){ + }; -DMACell.prototype.setMode = function(mode){ - if (mode === undefined) mode = globals.appState.get("cellMode"); - switch(mode) { - case "cell": - break; - case "part": - if (!this.parts) { - this.parts = this._initParts(); - var self = this; - _.each(this.parts, function(part){ - self._addChildren(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; - }); -}; - -DMACell.prototype.getQuaternion = function(){ - return this.object3D.quaternion.clone(); -}; - -DMACell.prototype.getEuler = function(){ - return this.object3D.rotation.clone(); -}; - -DMACell.prototype.axisScale = function(axis){ - switch (axis){ - case "x": - return this.xScale(); - case "y": - return this.yScale(); - case "z": - return this.zScale(); - default: - console.warn(axis + " axis not recognized"); - break; - } -}; - -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); -}; - -DMACell.prototype.destroy = function(){ - if (this.object3D) { - if (this.superCell) this.object3D.parent.remove(this.object3D); - else if (this.index) globals.three.sceneRemove(this.object3D, "cell"); - this.object3D.myParent = null; -// this.object3D.dispose(); -// geometry.dispose(); -// material.dispose(); - this.object3D = null; - } - this.destroyParts(); - this.nodes = null; - this.beams = null; - this.superCell = null; - this.index = null; -}; - -DMACell.prototype.destroyParts = function(){ - _.each(this.parts, function(part){ - if (part) part.destroy(); - }); - this.parts = null; -}; + + + DMACell.prototype._initParts = function(){ + return [];//override in subclasses + }; + + DMACell.prototype.setMode = function(mode){ + + if (mode === undefined) mode = appState.get("cellMode"); + + switch(mode) { + case "cell": + break; + case "part": + if (!this.parts) { + this.parts = this._initParts(); + var self = this; + _.each(this.parts, function(part){ + self._addChildren(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; + }); + }; + + DMACell.prototype.getQuaternion = function(){ + return this.object3D.quaternion.clone(); + }; + + DMACell.prototype.getEuler = function(){ + return this.object3D.rotation.clone(); + }; + + DMACell.prototype.axisScale = function(axis){ + switch (axis){ + case "x": + return this.xScale(); + case "y": + return this.yScale(); + case "z": + return this.zScale(); + default: + console.warn(axis + " axis not recognized"); + break; + } + }; + + DMACell.prototype.xScale = function(){ + return lattice.xScale(0); + }; + + DMACell.prototype.yScale = function(){ + return lattice.yScale(0); + }; + + DMACell.prototype.zScale = function(){ + return lattice.zScale(0); + }; + + DMACell.prototype.destroy = function(){ + if (this.object3D) { + if (this.superCell) this.object3D.parent.remove(this.object3D); + else if (this.index) three.sceneRemove(this.object3D, "cell"); + this.object3D.myParent = null; + // this.object3D.dispose(); + // geometry.dispose(); + // material.dispose(); + this.object3D = null; + } + this.destroyParts(); + this.nodes = null; + this.beams = null; + this.superCell = null; + this.index = null; + }; + + DMACell.prototype.destroyParts = function(){ + _.each(this.parts, function(part){ + if (part) part.destroy(); + }); + this.parts = null; + }; + + DMACell.prototype.toJSON = function(){ + var data = { + index:this.index//todo get rid of this and calculate from min and max + }; + if (this.parts) data.parts = this.parts; + return data; + }; + + return DMACell; +}); + + //DMACell.prototype.removePart = function(index){ // this.parts[index].destroy(); @@ -235,18 +245,8 @@ DMACell.prototype.destroyParts = function(){ // _.each(this.parts, function(part){ // if (part) hasAnyParts = true; // }); -// if (!hasAnyParts) globals.lattice.removeCell(this);//if all parts are gone, remove cell +// if (!hasAnyParts) lattice.removeCell(this);//if all parts are gone, remove cell //}; - -DMACell.prototype.toJSON = function(){ - var data = { - index:this.index//todo get rid of this and calculate from min and max - }; - if (this.parts) data.parts = this.parts; - return data; -}; - - //DMACell.prototype._initNodes = function(vertices){ // var position = this.getPosition(); // var orientation = this.getOrientation(); diff --git a/js/cells/KelvinCell.js b/js/cells/KelvinCell.js index 22bdd58d4c209c70835e09af992d5139dae60909..66f72c18e4388754a35e639fc6a7c22747923601 100644 --- a/js/cells/KelvinCell.js +++ b/js/cells/KelvinCell.js @@ -3,12 +3,12 @@ */ -(function(){ +define(['truncatedCubeCell'], function(TruncatedCubeCell){ var truncOctaRad = Math.sqrt(2); var pyrRad = 1/Math.sqrt(2); - var unitCellGeo = new THREE.Geometry(); - unitCellGeo.vertices = [ + var unitGeo = new THREE.Geometry(); + unitGeo.vertices = [ new THREE.Vector3(pyrRad, 0, truncOctaRad), new THREE.Vector3(0, pyrRad, truncOctaRad), new THREE.Vector3(-pyrRad, 0, truncOctaRad), @@ -39,7 +39,7 @@ new THREE.Vector3(-pyrRad, -truncOctaRad, 0), new THREE.Vector3(0, -truncOctaRad, -pyrRad) ]; - unitCellGeo.faces = [ + unitGeo.faces = [ new THREE.Face3(0,1,3), new THREE.Face3(2,3,1), new THREE.Face3(4,7,5), @@ -95,7 +95,7 @@ new THREE.Face3(22, 7, 23), new THREE.Face3(22, 14, 7), ]; - unitCellGeo.computeFaceNormals(); + unitGeo.computeFaceNormals(); function KelvinCell(index, superCell){ TruncatedCubeCell.call(this, index, superCell); @@ -103,9 +103,8 @@ KelvinCell.prototype = Object.create(TruncatedCubeCell.prototype); KelvinCell.prototype._getGeometry = function(){ - return unitCellGeo; + return unitGeo; }; - self.KelvinCell = KelvinCell; - -})(); \ No newline at end of file + return KelvinCell; +}); \ No newline at end of file diff --git a/js/cells/OctaEdgeCell.js b/js/cells/OctaEdgeCell.js index 91a94a7298fa58189ca61b516d1b66d3e89f0a3c..27d317edca55c18aeded21c2033f0845e8f41720 100644 --- a/js/cells/OctaEdgeCell.js +++ b/js/cells/OctaEdgeCell.js @@ -3,20 +3,25 @@ */ -function OctaEdgeCell(index, superCell){ - OctaFaceCell.call(this, index, superCell); -} -OctaEdgeCell.prototype = Object.create(OctaFaceCell.prototype); +define(['octaFaceCell'], function(OctaFaceCell){ -OctaEdgeCell.prototype._rotateCell = function(object3D){ - return object3D; -}; + function OctaEdgeCell(index, superCell){ + OctaFaceCell.call(this, index, superCell); + } + OctaEdgeCell.prototype = Object.create(OctaFaceCell.prototype); -//todo fix this -OctaEdgeCell.prototype.calcHighlighterPosition = function(face){ - var direction = face.normal.clone(); - direction.applyQuaternion(this.mesh.quaternion); - var position = this.getPosition(); - position.add(direction.clone().multiplyScalar(this.zScale()/2)); - return {index: _.clone(this.index), direction:direction, position:position}; -}; \ No newline at end of file + OctaEdgeCell.prototype._rotateCell = function(object3D){ + return object3D; + }; + + //todo fix this + OctaEdgeCell.prototype.calcHighlighterPosition = function(face){ + var direction = face.normal.clone(); + direction.applyQuaternion(this.mesh.quaternion); + var position = this.getPosition(); + position.add(direction.clone().multiplyScalar(this.zScale()/2)); + return {index: _.clone(this.index), direction:direction, position:position}; + }; + + return OctaEdgeCell; +}); \ No newline at end of file diff --git a/js/cells/OctaFaceCell.js b/js/cells/OctaFaceCell.js index 3e822357dc789536d9d6e900576c303ae3000004..84e18e209f318da886acaba4c06cb4b0f06bc60b 100644 --- a/js/cells/OctaFaceCell.js +++ b/js/cells/OctaFaceCell.js @@ -3,36 +3,41 @@ */ -var unitFaceOctaGeo = new THREE.OctahedronGeometry(1/Math.sqrt(2)); -unitFaceOctaGeo.applyMatrix(new THREE.Matrix4().makeRotationZ(-3*Math.PI/12)); -unitFaceOctaGeo.applyMatrix(new THREE.Matrix4().makeRotationX(Math.asin(2/Math.sqrt(2)/Math.sqrt(3)))); - -function OctaFaceCell(index, superCell){ - DMACell.call(this, index, superCell); -} -OctaFaceCell.prototype = Object.create(DMACell.prototype); - -OctaFaceCell.prototype._initParts = function(){ - var parts = []; - for (var i=0;i<3;i++){ - parts.push(new OctaFaceTriPart(i, this)); +define(['cell'], function(DMACell){ + + var unitGeo = new THREE.OctahedronGeometry(1/Math.sqrt(2)); + unitGeo.applyMatrix(new THREE.Matrix4().makeRotationZ(-3*Math.PI/12)); + unitGeo.applyMatrix(new THREE.Matrix4().makeRotationX(Math.asin(2/Math.sqrt(2)/Math.sqrt(3)))); + + function OctaFaceCell(index, superCell){ + DMACell.call(this, index, superCell); } - return parts; -}; - -OctaFaceCell.prototype._rotateCell = function(object3D){ - if (this.index && this.index.z%2!=0) object3D.rotation.set(0, 0, Math.PI); - return object3D; -}; - -OctaFaceCell.prototype._getGeometry = function(){ - return unitFaceOctaGeo; -}; - -OctaFaceCell.prototype.calcHighlighterPosition = function(face){ - if (face.normal.z<0.99) return {index: _.clone(this.index)};//only highlight horizontal faces - var direction = face.normal; - var position = this.getPosition(); - position.z += face.normal.z*this.zScale()/2; - return {index:_.clone(this.index), direction:direction, position:position}; -}; \ No newline at end of file + OctaFaceCell.prototype = Object.create(DMACell.prototype); + + OctaFaceCell.prototype._initParts = function(){ + var parts = []; + for (var i=0;i<3;i++){ + parts.push(new OctaFaceTriPart(i, this)); + } + return parts; + }; + + OctaFaceCell.prototype._rotateCell = function(object3D){ + if (this.index && this.index.z%2!=0) object3D.rotation.set(0, 0, Math.PI); + return object3D; + }; + + OctaFaceCell.prototype._getGeometry = function(){ + return unitGeo; + }; + + OctaFaceCell.prototype.calcHighlighterPosition = function(face){ + if (face.normal.z<0.99) return {index: _.clone(this.index)};//only highlight horizontal faces + var direction = face.normal; + var position = this.getPosition(); + position.z += face.normal.z*this.zScale()/2; + return {index:_.clone(this.index), direction:direction, position:position}; + }; + + return OctaFaceCell; +}); \ No newline at end of file diff --git a/js/cells/OctaRotEdgeCell.js b/js/cells/OctaRotEdgeCell.js index 8fea993da5eebff82e5538553b6e07ebc64c1425..236d074cb0b4af4f369f54f85291f4fffbe18440 100644 --- a/js/cells/OctaRotEdgeCell.js +++ b/js/cells/OctaRotEdgeCell.js @@ -3,89 +3,94 @@ */ -var unitVertexOcta = new THREE.OctahedronGeometry(1/Math.sqrt(2)); +define(['cell'], function(DMACell){ -function OctaRotEdgeCell(index, superCell){ - DMACell.call(this, index, superCell); -} -OctaRotEdgeCell.prototype = Object.create(DMACell.prototype); + var unitGeo = new THREE.OctahedronGeometry(1/Math.sqrt(2)); -OctaRotEdgeCell.prototype._initParts = function(){ - var type = globals.lattice.get("partType"); - var newParts = []; - if (type == "vox"){ - newParts.push(new OctaEdgeVoxPart(0, this)); - } else if (type == "voxLowPoly"){ - newParts.push(new OctaEdgeVoxPartLowPoly(0, this)); - } else { - console.warn("part type " + type + " not recognized"); - return; + function OctaRotEdgeCell(index, superCell){ + DMACell.call(this, index, superCell); } - return newParts; -}; + OctaRotEdgeCell.prototype = Object.create(DMACell.prototype); -OctaRotEdgeCell.prototype._getGeometry = function(){ - return unitVertexOcta; -}; + OctaRotEdgeCell.prototype._initParts = function(){ + var type = globals.lattice.get("partType"); + var newParts = []; + if (type == "vox"){ + newParts.push(new OctaEdgeVoxPart(0, this)); + } else if (type == "voxLowPoly"){ + newParts.push(new OctaEdgeVoxPartLowPoly(0, this)); + } else { + console.warn("part type " + type + " not recognized"); + return; + } + return newParts; + }; -OctaRotEdgeCell.prototype._rotateCell = function(object3D){ - object3D.rotation.set(0, 0, Math.PI/4); - return object3D; -}; + OctaRotEdgeCell.prototype._getGeometry = function(){ + return unitGeo; + }; -OctaRotEdgeCell.prototype.calcHighlighterPosition = function(face, point){ + OctaRotEdgeCell.prototype._rotateCell = function(object3D){ + object3D.rotation.set(0, 0, Math.PI/4); + return object3D; + }; - var position = this.getPosition(); - var direction = new THREE.Vector3(0,0,0); - var rad = this.xScale()*Math.sqrt(2)/6; + OctaRotEdgeCell.prototype.calcHighlighterPosition = function(face, point){ - var difference = new THREE.Vector3().subVectors(position, point); - difference.divideScalar(this.zScale()); - if (Math.abs(difference.z) < 0.2){ - direction.z = 0; - } else if (point.z < position.z) { - direction.z = -1; - position.z -= rad; - } else { - direction.z = 1; - position.z += rad; - } + var position = this.getPosition(); + var direction = new THREE.Vector3(0,0,0); + var rad = this.xScale()*Math.sqrt(2)/6; - if (direction.z != 0){ - if (this.index.z%2 == 0){ - if (point.x < position.x) { - direction.x -= 1; - position.x -= rad; - } - else position.x += rad; - if (point.y < position.y) { - direction.y -= 1; - position.y -= rad; - } - else position.y += rad; + var difference = new THREE.Vector3().subVectors(position, point); + difference.divideScalar(this.zScale()); + if (Math.abs(difference.z) < 0.2){ + direction.z = 0; + } else if (point.z < position.z) { + direction.z = -1; + position.z -= rad; } else { - if (point.x > position.x) { - direction.x += 1; - position.x += rad; - } - else position.x -= rad; - if (point.y > position.y) { - direction.y += 1; - position.y += rad; - } - else position.y -= rad; + direction.z = 1; + position.z += rad; } - } else { - if (Math.abs(difference.x) > Math.abs(difference.y)){ - if (point.x > position.x) direction.x = 1; - else direction.x = -1; + + if (direction.z != 0){ + if (this.index.z%2 == 0){ + if (point.x < position.x) { + direction.x -= 1; + position.x -= rad; + } + else position.x += rad; + if (point.y < position.y) { + direction.y -= 1; + position.y -= rad; + } + else position.y += rad; + } else { + if (point.x > position.x) { + direction.x += 1; + position.x += rad; + } + else position.x -= rad; + if (point.y > position.y) { + direction.y += 1; + position.y += rad; + } + else position.y -= rad; + } } else { - if (point.y > position.y) direction.y = 1; - else direction.y = -1; + if (Math.abs(difference.x) > Math.abs(difference.y)){ + if (point.x > position.x) direction.x = 1; + else direction.x = -1; + } else { + if (point.y > position.y) direction.y = 1; + else direction.y = -1; + } + position.x += direction.x*this.xScale()/2; + position.y += direction.y*this.yScale()/2; } - position.x += direction.x*this.xScale()/2; - position.y += direction.y*this.yScale()/2; - } - return {index: _.clone(this.index), direction:direction, position:position}; -}; \ No newline at end of file + return {index: _.clone(this.index), direction:direction, position:position}; + }; + + return OctaVertexCell; +}); \ No newline at end of file diff --git a/js/cells/OctaVertexCell.js b/js/cells/OctaVertexCell.js index ad096b920e482d60556b4fa17d2e9de23c0f5412..ba7c8d7b7e6b1924b1297386e085889869dd75b8 100644 --- a/js/cells/OctaVertexCell.js +++ b/js/cells/OctaVertexCell.js @@ -3,49 +3,54 @@ */ -function OctaVertexCell(index, superCell){ - DMACell.call(this, index, superCell); -} -OctaVertexCell.prototype = Object.create(DMACell.prototype); - -OctaVertexCell.prototype._getGeometry = function(){ - return unitVertexOcta; -}; - -OctaVertexCell.prototype.calcHighlighterPosition = function(face, point){ - - var position = this.getPosition(); - var direction = null; - - var xScale = this.xScale(); - if (point.x < position.x+xScale/4 && point.x > position.x-xScale/4){ - if (point.y > position.y-xScale/4 && point.y < position.y+xScale/4){ - if (face.normal.z > 0) { - direction = new THREE.Vector3(0,0,1); - position.z += this.zScale()/2; - } - else { - direction = new THREE.Vector3(0,0,-1); - position.z -= this.zScale()/2; +define(['cell'], function(DMACell){ + + function OctaVertexCell(index, superCell){ + DMACell.call(this, index, superCell); + } + OctaVertexCell.prototype = Object.create(DMACell.prototype); + + OctaVertexCell.prototype._getGeometry = function(){ + return unitVertexOcta; + }; + + OctaVertexCell.prototype.calcHighlighterPosition = function(face, point){ + + var position = this.getPosition(); + var direction = null; + + var xScale = this.xScale(); + if (point.x < position.x+xScale/4 && point.x > position.x-xScale/4){ + if (point.y > position.y-xScale/4 && point.y < position.y+xScale/4){ + if (face.normal.z > 0) { + direction = new THREE.Vector3(0,0,1); + position.z += this.zScale()/2; + } + else { + direction = new THREE.Vector3(0,0,-1); + position.z -= this.zScale()/2; + } + } else { + if (point.y < position.y-xScale/4){ + direction = new THREE.Vector3(0,-1,0); + position.y -= xScale/2; + } else { + direction = new THREE.Vector3(0,1,0); + position.y += xScale/2; + } } } else { - if (point.y < position.y-xScale/4){ - direction = new THREE.Vector3(0,-1,0); - position.y -= xScale/2; + if (point.x < position.x-xScale/4){ + direction = new THREE.Vector3(-1,0,0); + position.x -= xScale/2; } else { - direction = new THREE.Vector3(0,1,0); - position.y += xScale/2; + direction = new THREE.Vector3(1,0,0); + position.x += xScale/2; } } - } else { - if (point.x < position.x-xScale/4){ - direction = new THREE.Vector3(-1,0,0); - position.x -= xScale/2; - } else { - direction = new THREE.Vector3(1,0,0); - position.x += xScale/2; - } - } - return {index: _.clone(this.index), direction:direction, position:position}; -}; \ No newline at end of file + return {index: _.clone(this.index), direction:direction, position:position}; + }; + + return OctaVertexCell; +}); \ No newline at end of file diff --git a/js/cells/TetraEdgeCell.js b/js/cells/TetraEdgeCell.js index ab83cd3614054171b62d7ecf341792625bccff4a..fb26d2f026183b154393bc30a352d07130544fa5 100644 --- a/js/cells/TetraEdgeCell.js +++ b/js/cells/TetraEdgeCell.js @@ -3,9 +3,14 @@ */ -function DMATetraEdgeCell(index, superCell){ - TetraFaceCell.call(this, index, superCell); -} -DMATetraEdgeCell.prototype = Object.create(TetraFaceCell.prototype); - -DMATetraEdgeCell.prototype._rotateCell = function(){}; \ No newline at end of file +define(['tetraFaceCell'], function(TetraFaceCell){ + + function TetraEdgeCell(index, superCell){ + TetraFaceCell.call(this, index, superCell); + } + TetraEdgeCell.prototype = Object.create(TetraFaceCell.prototype); + + TetraEdgeCell.prototype._rotateCell = function(){}; + + return TetraEdgeCell; +}); \ No newline at end of file diff --git a/js/cells/TetraFaceCell.js b/js/cells/TetraFaceCell.js index ef675ce82fb5b0a881b7653bfe09dd319dd93002..ff86b89cb6469abac2fe0e266e1dbde44ca4f53e 100644 --- a/js/cells/TetraFaceCell.js +++ b/js/cells/TetraFaceCell.js @@ -3,26 +3,31 @@ */ -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))); - -var unitCellGeoUpsideDown = unitCellGeo.clone(); -unitCellGeoUpsideDown.applyMatrix(new THREE.Matrix4().makeRotationX(Math.PI)); - -function TetraFaceCell(index, superCell){ - DMACell.call(this, index, superCell); -} -TetraFaceCell.prototype = Object.create(DMACell.prototype); - -TetraFaceCell.prototype._getGeometry = function(){//abstract mesh representation of cell - if (this.index.z%2 ==0) return unitCellGeo;//todo need this? - return unitCellGeoUpsideDown; -}; - -TetraFaceCell.prototype._rotateCell = function(object3D){ - var zIndex = this.index.z; - if (Math.abs(zIndex%4) == 2 || Math.abs(zIndex%4) == 3) object3D.rotateZ(Math.PI/3); - return object3D; -}; \ No newline at end of file +define(['cell'], function(DMACell){ + + var unitGeo = new THREE.TetrahedronGeometry(Math.sqrt(3/8)); + unitGeo.applyMatrix(new THREE.Matrix4().makeRotationZ(Math.PI/4)); + unitGeo.applyMatrix(new THREE.Matrix4().makeRotationX((Math.PI-Math.atan(2*Math.sqrt(2)))/2)); + unitGeo.applyMatrix(new THREE.Matrix4().makeTranslation(0,0,Math.sqrt(3/8)-1/Math.sqrt(6))); + + var unitGeoInverted = unitGeo.clone(); + unitGeoInverted.applyMatrix(new THREE.Matrix4().makeRotationX(Math.PI)); + + function TetraFaceCell(index, superCell){ + DMACell.call(this, index, superCell); + } + TetraFaceCell.prototype = Object.create(DMACell.prototype); + + TetraFaceCell.prototype._getGeometry = function(){//abstract mesh representation of cell + if (this.index.z%2 ==0) return unitGeo;//todo need this? + return unitGeoInverted; + }; + + TetraFaceCell.prototype._rotateCell = function(object3D){ + var zIndex = this.index.z; + if (Math.abs(zIndex%4) == 2 || Math.abs(zIndex%4) == 3) object3D.rotateZ(Math.PI/3); + return object3D; + }; + + return TetraFaceCell; +}); \ No newline at end of file diff --git a/js/cells/TruncatedCubeCell.js b/js/cells/TruncatedCubeCell.js index 679d0dbc41ee4b30b0238c5425cad6c54069d83f..90986e716027c2219e1b84088c749c2761079ca9 100644 --- a/js/cells/TruncatedCubeCell.js +++ b/js/cells/TruncatedCubeCell.js @@ -3,11 +3,11 @@ */ -(function(){ +define(['cell'], function(DMACell){ var truncCubeRad = Math.sqrt(2)/2; - var unitCellGeo = new THREE.Geometry(); - unitCellGeo.vertices = [ + var unitGeo = new THREE.Geometry(); + unitGeo.vertices = [ new THREE.Vector3(truncCubeRad, 0, truncCubeRad), new THREE.Vector3(0, truncCubeRad, truncCubeRad), new THREE.Vector3(-truncCubeRad, 0, truncCubeRad), @@ -23,7 +23,7 @@ new THREE.Vector3(-truncCubeRad, 0, -truncCubeRad), new THREE.Vector3(0, -truncCubeRad, -truncCubeRad) ]; - unitCellGeo.faces = [ + unitGeo.faces = [ new THREE.Face3(1,0,4), new THREE.Face3(2,1,5), new THREE.Face3(3,2,6), @@ -47,7 +47,7 @@ new THREE.Face3(3,11,7), new THREE.Face3(3,6,11) ]; - unitCellGeo.computeFaceNormals(); + unitGeo.computeFaceNormals(); function TruncatedCubeCell(index, superCell){ DMACell.call(this, index, superCell); @@ -55,7 +55,7 @@ TruncatedCubeCell.prototype = Object.create(DMACell.prototype); TruncatedCubeCell.prototype._getGeometry = function(){ - return unitCellGeo; + return unitGeo; }; TruncatedCubeCell.prototype._buildWireframe = function(mesh){//abstract mesh representation of cell @@ -78,6 +78,5 @@ return {index: _.clone(this.index), direction:direction, position:position}; }; - self.TruncatedCubeCell = TruncatedCubeCell; - -})(); \ No newline at end of file + return TruncatedCubeCell; +}); \ No newline at end of file diff --git a/js/lattice/CubeLattice.js b/js/lattice/CubeLattice.js index 59bcf25f397fd3693a68d0ae8097907935b70b8c..4d37a2a4b22b0aa63e32ca485a30e06d4852343a 100644 --- a/js/lattice/CubeLattice.js +++ b/js/lattice/CubeLattice.js @@ -38,7 +38,9 @@ define(['lattice', 'globals'], function(lattice, globals){ }, makeCellForLatticeType: function(indices){ - return new CubeCell(indices); + require(['cubeCell'], function(CubeCell){ + return new CubeCell(indices); + }); }, _undo: function(){//remove all the mixins, this will help with debugging later diff --git a/js/lattice/GIKLattice.js b/js/lattice/GIKLattice.js index 352333cbba2d0f9df949934d30a9df926473f4f9..068c27ff11d632bb387c1ddfbd07f262b0d9f678 100644 --- a/js/lattice/GIKLattice.js +++ b/js/lattice/GIKLattice.js @@ -39,7 +39,9 @@ define(['lattice', 'globals'], function(lattice, globals){ }, makeCellForLatticeType: function(indices){ - return new GIKSuperCell(indices); + require(['gikSuperCell'], function(GIKSuperCell){ + return new GIKSuperCell(indices); + }); }, _undo: function(){//remove all the mixins, this will help with debugging later diff --git a/js/lattice/KelvinLattice.js b/js/lattice/KelvinLattice.js index 56cc2d04f2435c66f02646c6908609774ec35064..f24876340fe984d4da0e63d37d09f8afaaa39c29 100644 --- a/js/lattice/KelvinLattice.js +++ b/js/lattice/KelvinLattice.js @@ -39,7 +39,9 @@ define(['lattice', 'globals'], function(lattice, globals){ }, makeCellForLatticeType: function(indices){ - return new KelvinCell(indices); + require(['kelvinCell'], function(KelvinCell){ + return new KelvinCell(indices); + }); }, _undo: function(){//remove all the mixins, this will help with debugging later diff --git a/js/lattice/Lattice.js b/js/lattice/Lattice.js index 641ed80675b4ddab40c216fa1a383f34b95111ee..888e7067a23dcf59e042b8b7579994bcf4f6ffd1 100644 --- a/js/lattice/Lattice.js +++ b/js/lattice/Lattice.js @@ -84,7 +84,7 @@ define(['appState', 'plist', 'three', 'threeModel', 'globals'], function(appStat if (!cells[index.x][index.y][index.z]) { cells[index.x][index.y][index.z] = this.makeCellForLatticeType(indices); this.set("numCells", this.get("numCells")+1); - if (!noRender) three.render(); + if (!noRender || noRender === undefined) three.render(); } else console.warn("already a cell there"); }, diff --git a/js/lattice/OctaEdgeLattice.js b/js/lattice/OctaEdgeLattice.js index 49957abc18159dfd0e3e67f3b762fab71ab30f1e..ea4f237da3738d4dd347b2e7a20de8251ab91d31 100644 --- a/js/lattice/OctaEdgeLattice.js +++ b/js/lattice/OctaEdgeLattice.js @@ -64,7 +64,9 @@ define(['lattice', 'globals'], function(lattice, globals){ }, makeCellForLatticeType: function(indices){ - return new DMAEdgeOctaCell(indices); + require(['octaEdgeCell'], function(OctaEdgeCell){ + return new OctaEdgeCell(indices); + }); }, _undo: function(){//remove all the mixins, this will help with debugging later diff --git a/js/lattice/OctaFaceLattice.js b/js/lattice/OctaFaceLattice.js index e47d010a058b2a7b9231caf1f0b371a35b99b25e..ffb96ab8b25e362d284fb0e8b8a928da5040f760 100644 --- a/js/lattice/OctaFaceLattice.js +++ b/js/lattice/OctaFaceLattice.js @@ -10,8 +10,8 @@ define(['lattice', 'globals'], function(lattice, globals){ require(['octaBaseplane'], function(OctaBasePlane){ globals.basePlane = new OctaBasePlane(); }); - require(['defaultHighlighter'], function(DefaultHighlighter){ - globals.highlighter = new DefaultHighlighter(); + require(['octaFaceHighlighter'], function(OctaFaceHighlighter){ + globals.highlighter = new OctaFaceHighlighter(); }); }, @@ -47,7 +47,9 @@ define(['lattice', 'globals'], function(lattice, globals){ }, makeCellForLatticeType: function(indices){ - return new OctaFaceCell(indices); + require(['octaFaceCell'], function(OctaFaceCell){ + return new OctaFaceCell(indices); + }); }, _undo: function(){//remove all the mixins @@ -58,5 +60,9 @@ define(['lattice', 'globals'], function(lattice, globals){ } }; + _.each(_.keys(OctaFaceLattice), function(key){ + console.log(key); + }); + return OctaFaceLattice; }); diff --git a/js/lattice/OctaRotEdgeLattice.js b/js/lattice/OctaRotEdgeLattice.js index 5abcec7830fcbb808c9fac0a5c91e6e83cf2cf78..211533e8e21687e3a8e8db1b1479d6b13b3b1038 100644 --- a/js/lattice/OctaRotEdgeLattice.js +++ b/js/lattice/OctaRotEdgeLattice.js @@ -50,7 +50,9 @@ define(['lattice', 'globals'], function(lattice, globals){ }, makeCellForLatticeType: function(indices){ - return new OctaRotEdgeCell(indices); + require(['octaRotEdgeCell'], function(OctaRotEdgeCell){ + return new OctaRotEdgeCell(indices); + }); }, _undo: function(){//remove all the mixins diff --git a/js/lattice/OctaVertexLattice.js b/js/lattice/OctaVertexLattice.js index fe6b85ff54404e2e831019a484918aeea2bfb7b7..68a7eaeb8be5eabf41981d920865787a37777e1d 100644 --- a/js/lattice/OctaVertexLattice.js +++ b/js/lattice/OctaVertexLattice.js @@ -46,7 +46,9 @@ define(['lattice', 'globals'], function(lattice, globals){ }, makeCellForLatticeType: function(indices){ - return new OctaVertexCell(indices); + require(['octaVertexCell'], function(OctaVertexCell){ + return new OctaVertexCell(indices); + }); }, _undo: function(){//remove all the mixins, this will help with debugging later diff --git a/js/lattice/TruncatedCubeLattice.js b/js/lattice/TruncatedCubeLattice.js index c628651a9d654da570c5a86ba219b9f706cef575..3d138462336fd2e5619f5b6410031760a3a08397 100644 --- a/js/lattice/TruncatedCubeLattice.js +++ b/js/lattice/TruncatedCubeLattice.js @@ -39,7 +39,9 @@ define(['lattice', 'globals'], function(lattice, globals){ }, makeCellForLatticeType: function(indices){ - return new TruncatedCubeCell(indices); + require(['truncatedCubeCell'], function(TruncatedCubeCell){ + return new TruncatedCubeCell(indices); + }); }, _undo: function(){//remove all the mixins, this will help with debugging later diff --git a/js/main.js b/js/main.js index f6230a6ca8956e65547ac41e70d4ab24b007e0da..bc3d39ce23b9ab893127e85950ea9b785a3b37e0 100644 --- a/js/main.js +++ b/js/main.js @@ -3,7 +3,9 @@ */ require.config({ + baseUrl: 'js', + paths: { jquery: 'dependencies/jquery-2.1.3', underscore: 'dependencies/underscore', @@ -49,6 +51,19 @@ require.config({ octaFaceHighlighter: 'highlighter/OctaFaceHighlighter', truncatedCubeHighlighter: 'highlighter/TruncatedCubeHighlighter', + //cells + cell: 'cells/DMACell', + octaFaceCell: 'cells/OctaFaceCell', + octaEdgeCell: 'cells/OctaEdgeCell', + octaVertexCell: 'cells/OctaVertexCell', + octaRotEdgeCell: 'cells/OctaRotEdgeCell', + tetraEdgeCell: 'cells/TetraEdgeCell', + tetraFaceCell: 'cells/TetraFaceCell', + truncatedCubeCell: 'cells/TruncatedCubeCell', + kelvinCell: 'cells/KelvinCell', + cubeCell: 'cells/CubeCell', + gikCell: 'cells/GIKCell', + //UI navbar: 'menus/Navbar', navViewMenu: 'menus/NavViewMenu',//view dropdown @@ -68,6 +83,7 @@ require.config({ sendMenu: 'menus/SendMenuView' }, + shim: { three: { exports: 'THREE' @@ -83,6 +99,7 @@ require.config({ deps: ['jquery'] } } + }); //init stuff @@ -97,38 +114,3 @@ require(['appState', 'lattice', 'menuWrapper', 'navbar', 'ribbon', 'threeModel', // if (lattice.get("connectionType") != "gik") lattice.addCellAtIndex({x:0,y:0,z:0});//add a cell }); - - -//setup persistent global variables -//if (typeof globals === "undefined") globals = {}; -// -// -//$(function(){ -// -// //init web workers -//// window.workers = persistentWorkers(8); -// -// //init global singletons -// globals.three = ThreeModel(); -// globals.plist = AppPList(); -// globals.appState = new AppState(); -// globals.lattice = new Lattice(); -// globals.basePlane = null; -// globals.highlighter = null; -// globals.lattice.delayedInit();//todo need this? -// globals.cam = new Cam({appState: globals.appState}); -// globals.fileSaver = GlobalFilesaver(); -// -// //ui -// new MenuWrapper({model: globals.appState}); -// new NavBar({model:globals.appState}); -// new Ribbon({model:globals.appState}); -// new ScriptView({model:globals.appState}); -// -// //threeJS View -// new ThreeView({model:globals.three}); -// -// if (globals.lattice.get("connectionType") != "gik") globals.lattice.addCellAtIndex({x:0,y:0,z:0});//add a cell -// -//// return {globals:globals}; -//}); diff --git a/js/three/ThreeModel.js b/js/three/ThreeModel.js index a2effd9e0d8eb01962b650450478a5d352f504c2..f4c55d745d608162b0c80ac16e168751ddf2ed89 100644 --- a/js/three/ThreeModel.js +++ b/js/three/ThreeModel.js @@ -5,7 +5,7 @@ define(['three'], function(THREE){ - var camera = new THREE.PerspectiveCamera(60, window.innerWidth/window.innerHeight, 1, 10000); + var camera = new THREE.PerspectiveCamera(60, window.innerWidth/window.innerHeight, 0.01, 5000); var scene = new THREE.Scene(); var renderer = new THREE.WebGLRenderer({antialias:true});//antialiasing is not supported in ff and on mac+chrome