diff --git a/js/highlighter/CubeHighlighter.js b/js/highlighter/CubeHighlighter.js new file mode 100644 index 0000000000000000000000000000000000000000..6eec813f4c2e3de4cca9f909859241194607031a --- /dev/null +++ b/js/highlighter/CubeHighlighter.js @@ -0,0 +1,14 @@ +/** + * Created by aghassaei on 6/2/15. + */ + + +define(['highlighter', 'three'], function(Highlighter, THREE){ + + return Highlighter.extend({ + + _makeGeometry: function(){ + return new THREE.BoxGeometry(1,1,0.01); + } + }); +}); \ No newline at end of file diff --git a/js/highlighter/DefaultHighlighter.js b/js/highlighter/DefaultHighlighter.js new file mode 100644 index 0000000000000000000000000000000000000000..8d4bc019f97cabf0169f72dc0f38299a23841f3e --- /dev/null +++ b/js/highlighter/DefaultHighlighter.js @@ -0,0 +1,17 @@ +/** + * Created by aghassaei on 6/2/15. + */ + + +define(['highlighter', 'three'], function(Highlighter, THREE){ + + return Highlighter.extend({ + + _makeGeometry: function(){ + return new THREE.SphereGeometry(0.2); + }, + + _setRotation: function(){} + + }); +}); \ No newline at end of file diff --git a/js/highlighter/GIKHighlighter.js b/js/highlighter/GIKHighlighter.js new file mode 100644 index 0000000000000000000000000000000000000000..c438a96fd1dac4720d150fa3a8612214f0e12cf1 --- /dev/null +++ b/js/highlighter/GIKHighlighter.js @@ -0,0 +1,50 @@ +/** + * Created by aghassaei on 6/2/15. + */ + +define(['highlighter', 'three', 'appState', 'lattice', 'threeModel'], function(Highlighter, THREE, appState, lattice, three){ + + return Highlighter.extend({ + + _makeGeometry: function(){ + return new THREE.BoxGeometry(1,1,lattice.zScale(0)); + }, + + _setPosition: function(position, direction){ + this.mesh.position.set(position.x+direction.x/2, position.y+direction.y/2, position.z+lattice.zScale()*direction.z/2); + }, + + _setRotation: function(direction){ + if (!this.highlightedObject) return; + var index = this.highlightedObject.getIndex(); + 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); + else this.mesh.rotation.set(0,0,0); + this.mesh.translateX(superCellIndex - this.mesh.scale.x/2 + 0.5); + }, + + updateGikLength: function(){ + if (!this.mesh) return; + this.mesh.scale.set(lattice.get("superCellRange").x, lattice.get("superCellRange").y, lattice.get("superCellRange").z); + three.render(); + if (!this.direction) return; + this._setPosition(this.position, this.direction);//position of center point + this._setRotation(this.direction); + three.render(); + }, + + _getNextCellPosition: function(){//add direction vector to current index + var newIndex = this.highlightedObject.getIndex(); + var direction = this.direction; + _.each(_.keys(newIndex), function(key){ + newIndex[key] = Math.round(newIndex[key] + direction[key]); + }); + + var offset = appState.get("superCellIndex"); + if (newIndex.z%2 == 0) newIndex.x -= offset; + else newIndex.y -= offset; + return newIndex; + } + }); +}); \ No newline at end of file diff --git a/js/highlighter/Highlighter.js b/js/highlighter/Highlighter.js new file mode 100644 index 0000000000000000000000000000000000000000..d049bf3be37cda03294db17f291ae705fe159604 --- /dev/null +++ b/js/highlighter/Highlighter.js @@ -0,0 +1,155 @@ +/** + * Created by aghassaei on 2/1/15. + */ + + +define(['threeModel', 'appState', 'lattice'], function(three, appState, lattice){ + + return Backbone.View.extend({ + + mesh: null, + highlightedObject: null, + direction: null, + + initialize: function(){ + + var geometry = this._makeGeometry(); + this.mesh = new THREE.Mesh(geometry, + new THREE.MeshBasicMaterial({ + // side:THREE.DoubleSide, + transparent:true, + opacity:0.4, + color:0xffffff + // vertexColors:THREE.FaceColors + })); + + three.sceneAdd(this.mesh, "highlighter"); + this.hide(); + + //bind events + this.listenTo(lattice, "change:superCellRange", this._superCellParamDidChange); + this.listenTo(appState, "change:superCellIndex", this._superCellParamDidChange); + }, + + /////////////////////////////////////////////////////////////////////////////////// + /////////////////////////////VISIBILITY//////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////////////// + + hide: function(){ + this._setVisibility(false); + }, + + show: function(forceRender){ + this._setVisibility(true, forceRender); + }, + + _setVisibility: function(visible, forceRender){ + visible = appState.get("highlighterIsVisible") && visible; + if (forceRender || this.isVisible() != visible){ + this.mesh.visible = visible; + three.render(); + } + this.mesh.visible = visible; + }, + + isVisible: function(){ + return this.mesh.visible; + }, + + setNothingHighlighted: function(){ + this.highlightedObject = null; + this.direction = null; + this.position = null; + this.hide(); + }, + + highlight: function(intersection){ + if (!intersection.object) return; + var highlighted = intersection.object; + if (!(highlighted.parent instanceof THREE.Scene)) highlighted = highlighted.parent;//cell mesh parent is object3d + if (!highlighted.myParent) { + console.warn("no parent for highlighted object"); + return; + } + + this.highlightedObject = highlighted.myParent; + + 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(); + return; + } + this.direction = highlightedPos.direction; + this._setPosition(highlightedPos.position, this.direction);//position of center point + this._setRotation(this.direction); + + this.show(true); + }, + + /////////////////////////////////////////////////////////////////////////////////// + /////////////////////////////POSITION/SCALE//////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////////////// + + getHighlightedObjectPosition: function(){//origin selection + if (this.highlightedObject instanceof DMACell) { + var position = this.highlightedObject.getPosition(); + return { + x:parseFloat(position.x.toFixed(4)), + y:parseFloat(position.y.toFixed(4)), + z:parseFloat(position.z.toFixed(4)) + }; + } + return null; + }, + + _setPosition: function(position){ + this.mesh.position.set(position.x, position.y, position.z); + }, + + _setRotation: function(direction){ + this.mesh.rotation.set(direction.y*Math.PI/2, direction.x*Math.PI/2, 0); + }, + + _superCellParamDidChange: function(){ + if (this.updateGikLength) this.updateGikLength(); + }, + + /////////////////////////////////////////////////////////////////////////////////// + /////////////////////////////ADD REMOVE//////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////////////// + + _getNextCellPosition: function(){//add direction vector to current index + var newIndex = _.clone(this.highlightedObject.getIndex()); + var direction = this.direction; + _.each(_.keys(newIndex), function(key){ + newIndex[key] = Math.round(newIndex[key] + direction[key]); + }); + return newIndex; + }, + + addRemoveVoxel: function(shouldAdd){ + if (shouldAdd){ + if (!this.isVisible() || !this.highlightedObject) return; + if (lattice.get("connectionType") == "freeformFace"){ + //todo make this work for baseplane + lattice.addFreeFormCell(this.mesh.position.clone(), this.highlightedObject.getOrientation(), this.direction, this.highlightedObject.getType()); + return; + } + lattice.addCellAtIndex(this._getNextCellPosition()); + } else { + if (!this.highlightedObject) return; + if (!(this.highlightedObject instanceof DMACell)) return; + lattice.removeCell(this.highlightedObject); + } + this.setNothingHighlighted(); + }, + + destroy: function(){ + this.setNothingHighlighted(); + three.sceneRemove(this.mesh, null); + this.mesh = null; + this.stopListening(); + } + }); +}); \ No newline at end of file diff --git a/js/highlighter/OctaFaceHighlighter.js b/js/highlighter/OctaFaceHighlighter.js new file mode 100644 index 0000000000000000000000000000000000000000..9e8fbe8e934a99e31f7c96e86d012fabadb55da4 --- /dev/null +++ b/js/highlighter/OctaFaceHighlighter.js @@ -0,0 +1,23 @@ +/** + * Created by aghassaei on 6/2/15. + */ + + +define(['highlighter', 'three'], function(Highlighter, THREE){ + + return Highlighter.extend({ + + _makeGeometry: function(){ + + var rad = 1/Math.sqrt(3); + var geometry = new THREE.CylinderGeometry(rad, rad, 0.01, 3);//short triangular prism + geometry.applyMatrix(new THREE.Matrix4().makeRotationX(-Math.PI/2)); + return geometry; + }, + + _setRotation: function(){ + this.mesh.rotation.set(0,0,(this.highlightedObject.getIndex().z+1)%2*Math.PI); + } + + }); +}); \ No newline at end of file diff --git a/js/highlighter/TruncatedCubeHighlighter.js b/js/highlighter/TruncatedCubeHighlighter.js new file mode 100644 index 0000000000000000000000000000000000000000..5664815a0285bf00916e867e1238a33299300e8f --- /dev/null +++ b/js/highlighter/TruncatedCubeHighlighter.js @@ -0,0 +1,18 @@ +/** + * Created by aghassaei on 6/2/15. + */ + + +define(['highlighter', 'three'], function(Highlighter, THREE){ + + return Highlighter.extend({ + + _makeGeometry: function(){ + return new THREE.BoxGeometry(1,1,0.01); + }, + + _setRotation: function(direction){ + this.mesh.rotation.set(direction.y*Math.PI/2, direction.x*Math.PI/2, Math.PI/4); + } + }); +}); \ No newline at end of file diff --git a/js/lattice/CubeLattice.js b/js/lattice/CubeLattice.js index 527fa4236592245845606a20c19058b6413c5cce..91e5decf0ca1a3aa51281875de59b4bb5c9a34ef 100644 --- a/js/lattice/CubeLattice.js +++ b/js/lattice/CubeLattice.js @@ -2,45 +2,54 @@ * Created by aghassaei on 5/26/15. */ -latticeSubclasses = latticeSubclasses || {}; - -latticeSubclasses["CubeLattice"] = { - - _initLatticeType: function(){ - globals.basePlane = new SquareBasePlane(); - globals.highlighter = new CubeHighlighter(); - }, - - getIndexForPosition: function(absPosition){ - return this._indexForPosition(absPosition); - }, - - getPositionForIndex: function(index){ - return this._positionForIndex(index); - }, - - xScale: function(cellSeparation){ - if (cellSeparation === undefined) cellSeparation = this.get("cellSeparation").xy; - return 1+2*cellSeparation; - }, - - yScale: function(cellSeparation){ - return this.xScale(cellSeparation); - }, - - zScale: function(cellSeparation){ - if (cellSeparation === undefined) cellSeparation = this.get("cellSeparation").z; - return 1+2*cellSeparation; - }, - - makeCellForLatticeType: function(indices){ - return new CubeCell(indices); - }, - - _undo: function(){//remove all the mixins, this will help with debugging later - var self = this; - _.each(_.keys(this.CubeLattice), function(key){ - self[key] = null; - }); +define(['lattice', 'globals'], function(lattice, globals){ + + _.extend(lattice, { + + CubeLattice: { + + _initLatticeType: function(){ + require(['squareBaseplane'], function(SquareBasePlane){ + globals.basePlane = new SquareBasePlane(); + }); + require(['cubeHighlighter'], function(CubeHighlighter){ + globals.highlighter = new CubeHighlighter(); + }); + }, + + getIndexForPosition: function(absPosition){ + return this._indexForPosition(absPosition); + }, + + getPositionForIndex: function(index){ + return this._positionForIndex(index); + }, + + xScale: function(cellSeparation){ + if (cellSeparation === undefined) cellSeparation = this.get("cellSeparation").xy; + return 1+2*cellSeparation; + }, + + yScale: function(cellSeparation){ + return this.xScale(cellSeparation); + }, + + zScale: function(cellSeparation){ + if (cellSeparation === undefined) cellSeparation = this.get("cellSeparation").z; + return 1+2*cellSeparation; + }, + + makeCellForLatticeType: function(indices){ + return new CubeCell(indices); + }, + + _undo: function(){//remove all the mixins, this will help with debugging later + var self = this; + _.each(_.keys(this.CubeLattice), function(key){ + self[key] = null; + }); + } } - } + }); +}); + diff --git a/js/lattice/GIKLattice.js b/js/lattice/GIKLattice.js index cc50834cb554e8b341f4a767c262b6f62755dbf5..0e92a52b575834ad1f50c647dfd14810b56ccb33 100644 --- a/js/lattice/GIKLattice.js +++ b/js/lattice/GIKLattice.js @@ -2,84 +2,50 @@ * Created by aghassaei on 5/26/15. */ -latticeSubclasses = latticeSubclasses || {}; - -latticeSubclasses["GIKLattice"] = { - - _initLatticeType: function(){ - globals.basePlane = new SquareBasePlane(); - globals.highlighter = new GIKHighlighter(); - globals.highlighter.updateGikLength(); - }, - - getIndexForPosition: function(absPosition){ - return this._indexForPosition(absPosition); - }, - - getPositionForIndex: function(index){ - return this._positionForIndex(index); - }, - - xScale: function(cellSeparation){ - if (cellSeparation === undefined) cellSeparation = this.get("cellSeparation").xy; - return 1+2*cellSeparation; - }, - - yScale: function(cellSeparation){ - return this.xScale(cellSeparation); - }, - - zScale: function(cellSeparation){ - if (cellSeparation === undefined) cellSeparation = this.get("cellSeparation").z; - return 1.28*(1+2*cellSeparation); - }, - - makeCellForLatticeType: function(indices){ - return new GIKSuperCell(indices); - }, - -// _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)){ -// for (var k=this._getRasterLoopInit(var3);this._getRasterLoopCondition(k,var3);k+=this._getRasterLoopIterator(var3)){ -// var numToSkip = {x:0,y:0}; -// if (var1.order == 0){ -// if (var2.order == 1) numToSkip = this._doSuperCellCallback(cells, i, j, k, callback); -// else if (var2.order == 2) numToSkip = this._doSuperCellCallback(cells, i, k, j, callback); -// } else if (var1.order == 1){ -// if (var2.order == 0) numToSkip = this._doSuperCellCallback(cells, j, i, k, callback); -// else if (var2.order == 2) numToSkip = this._doSuperCellCallback(cells, k, i, j, callback); -// } else { -// if (var2.order == 0) { -// numToSkip = this._doSuperCellCallback(cells, j, k, i, callback); -// } -// else if (var2.order == 1) { -// numToSkip = this._doSuperCellCallback(cells, k, j, i, callback); -// } -// } -// -// } -// } -// } -// }, -// -// _doSuperCellCallback: function(cells, x, y, z, callback){ -// var cell = cells[x][y][z]; -// if (z%2 != 0) cell = cells[y][x][z]; -// if (!cell) { -// callback(null, x, y, z); -// return {x:0,y:0}; -// } -// var superCell = cell.superCell; -// callback(superCell, x, y, z); -// if (z%2 != 0) return {x:0, y:superCell.getLength()}; -// return {x:superCell.getLength(), y:0}; -// }, - - _undo: function(){//remove all the mixins, this will help with debugging later - var self = this; - _.each(_.keys(this.GIKLattice), function(key){ - self[key] = null; - }); +define(['lattice', 'globals'], function(lattice, globals){ + + _.extend(lattice, { + + GIKLattice: { + + _initLatticeType: function(){ + globals.basePlane = new SquareBasePlane(); + globals.highlighter = new GIKHighlighter(); + globals.highlighter.updateGikLength(); + }, + + getIndexForPosition: function(absPosition){ + return this._indexForPosition(absPosition); + }, + + getPositionForIndex: function(index){ + return this._positionForIndex(index); + }, + + xScale: function(cellSeparation){ + if (cellSeparation === undefined) cellSeparation = this.get("cellSeparation").xy; + return 1+2*cellSeparation; + }, + + yScale: function(cellSeparation){ + return this.xScale(cellSeparation); + }, + + zScale: function(cellSeparation){ + if (cellSeparation === undefined) cellSeparation = this.get("cellSeparation").z; + return 1.28*(1+2*cellSeparation); + }, + + makeCellForLatticeType: function(indices){ + return new GIKSuperCell(indices); + }, + + _undo: function(){//remove all the mixins, this will help with debugging later + var self = this; + _.each(_.keys(this.GIKLattice), function(key){ + self[key] = null; + }); + } } - } + }); +}); diff --git a/js/lattice/Lattice.js b/js/lattice/Lattice.js index d44739c66f7cdb0c7a0c50d61942156f76db8980..1c6802d594a9a00ece6d25e89cd71e3298e06a98 100644 --- a/js/lattice/Lattice.js +++ b/js/lattice/Lattice.js @@ -3,7 +3,7 @@ */ -define(['appState', 'plist', 'three', 'threeModel'], function(appState, plist, THREE, three){ +define(['appState', 'plist', 'three', 'threeModel', 'globals'], function(appState, plist, THREE, three, globals){ var Lattice = Backbone.Model.extend({ @@ -35,17 +35,15 @@ define(['appState', 'plist', 'three', 'threeModel'], function(appState, plist, T //bind events this.listenTo(this, "change:partType", this._updatePartType); - this.listenTo(this, "change:cellType change:connectionType", this._updateLatticeType); + this.listenTo(this, "change:cellType change:connectionType", function(){ + this._updateLatticeType(false); + }); this.listenTo(this, "change:cellSeparation", this._updateCellSeparation); this.listenTo(appState, "change:cellMode", this._updateForMode); this.listenTo(appState, "change:cellsVisible", this._setCellVisibility); - - }, - - delayedInit: function(){ - this._updateLatticeType(); + this._updateLatticeType(false); }, //////////////////////////////////////////////////////////////////////////////////// @@ -368,7 +366,7 @@ define(['appState', 'plist', 'three', 'threeModel'], function(appState, plist, T ///////////////////////////////CONNECTION TYPE////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// - _updateLatticeType: function(arg1, arg2, arg3, loadingFromFile){//do not clear cells if loading from file (cells array contains important metadata) + _updateLatticeType: function(loadingFromFile){//do not clear cells if loading from file (cells array contains important metadata) if (this.previous("connectionType") == "gik") this.clearCells(); @@ -383,38 +381,13 @@ define(['appState', 'plist', 'three', 'threeModel'], function(appState, plist, T _.extend(this, this._getSubclassForLatticeType(loadingFromFile)); this._initLatticeType(); - - - - //todo refactor this eventually - var self = this; + //copy over cells to new lattice type var cells = this.get("cells"); - this._loopCells(cells, function(cell, x, y, z){ + this._loopCells(cells, function(cell, x, y, z, self){ if (!cell) return; - - var index = _.clone(cell.indices); - //var parts = null; - //if (loadingFromFile) parts = _.clone(cell.parts); - if (cell.parentOrientation) var parentOrientation = new THREE.Quaternion(cell.parentOrientation._x, cell.parentOrientation._y, cell.parentOrientation._z, cell.parentOrientation._w); - if (cell.parentPosition) var parentPos = cell.parentPosition; - if (cell.direction) var direction = new THREE.Vector3(cell.direction.x, cell.direction.y, cell.direction.z); - if (cell.parentType) var parentType = cell.parentType; - if (cell.type) var type = cell.type; - + var index = _.clone(cell.index); if (cell.destroy) cell.destroy(); - var newCell = self.makeCellForLatticeType(index, parentPos, parentOrientation, direction, parentType, type); - - //if (parts) { - // //todo make this better - // newCell.parts = newCell._initParts(); - // for (var i=0;i<newCell.parts.length;i++){ - // if (!parts[i]) { - // newCell.parts[i].destroy(); - // newCell.parts[i] = null; - // } - // } - //} - cells[x][y][z] = newCell; + cells[x][y][z] = self.makeCellForLatticeType(index);// parentPos, parentOrientation, direction, parentType, type) }); three.render(); }, @@ -487,7 +460,7 @@ define(['appState', 'plist', 'three', 'threeModel'], function(appState, plist, T for (var x=0;x<cells.length;x++){ for (var y=0;y<cells[0].length;y++){ for (var z=0;z<cells[0][0].length;z++){ - callback(cells[x][y][z], x, y, z); + callback(cells[x][y][z], x, y, z, this); } } } diff --git a/js/main.js b/js/main.js index daad85d418e294d02cfb3ac75c7216a985138cec..7dc4bf8b47b9d5d24c7f1d9a50dd838315f4bd7a 100644 --- a/js/main.js +++ b/js/main.js @@ -18,6 +18,7 @@ require.config({ threeView: 'three/ThreeView', //models + globals: 'models/globals', plist: 'models/PList', appState: 'models/AppState', fileSaver: 'models/FileSaver', diff --git a/js/models/FileSaver.js b/js/models/FileSaver.js index 0679ec7b888c101d113c8c1148cbc908a27b8617..c321da8b07cf2d634df01e3044594b038a85f6f0 100644 --- a/js/models/FileSaver.js +++ b/js/models/FileSaver.js @@ -55,7 +55,7 @@ define(['fileSaverLib'], function(saveAs){ function loadFile(data){//todo make this better globals.lattice.clearCells(); _setData(data, false); - globals.lattice._updateLatticeType(null, null, null, true); + globals.lattice._updateLatticeType(true); globals.lattice.trigger("change:scale"); } diff --git a/js/models/Globals.js b/js/models/Globals.js new file mode 100644 index 0000000000000000000000000000000000000000..fed044454058ac650002c795dcf400d333c5d224 --- /dev/null +++ b/js/models/Globals.js @@ -0,0 +1,12 @@ +/** + * Created by aghassaei on 6/2/15. + */ + +//globals namespace, not sure if there's a way to get around this + +define(function() { + return { + baseplane: null, + highlighter: null + }; +}); \ No newline at end of file diff --git a/js/three/Highlighter.js b/js/three/Highlighter.js deleted file mode 100644 index 44ac5424910682f56096916bf2e9c7046fa5ab27..0000000000000000000000000000000000000000 --- a/js/three/Highlighter.js +++ /dev/null @@ -1,268 +0,0 @@ -/** - * Created by aghassaei on 2/1/15. - */ - -Highlighter = Backbone.View.extend({ - - mesh: null, - highlightedObject: null, - direction: null, - - initialize: function(){ - - var geometry = this._makeGeometry(); - this.mesh = new THREE.Mesh(geometry, - new THREE.MeshBasicMaterial({ -// side:THREE.DoubleSide, - transparent:true, - opacity:0.4, - color:0xffffff -// vertexColors:THREE.FaceColors - })); - - globals.three.sceneAdd(this.mesh, "highlighter"); - this.hide(); - - //bind events - this.listenTo(globals.lattice, "change:superCellRange", this._superCellParamDidChange); - this.listenTo(globals.appState, "change:superCellIndex", this._superCellParamDidChange); - }, - - /////////////////////////////////////////////////////////////////////////////////// - /////////////////////////////VISIBILITY//////////////////////////////////////////// - /////////////////////////////////////////////////////////////////////////////////// - - hide: function(){ - this._setVisibility(false); - }, - - show: function(forceRender){ - this._setVisibility(true, forceRender); - }, - - _setVisibility: function(visible, forceRender){ - visible = globals.appState.get("highlighterIsVisible") && visible; - if (forceRender || this.isVisible() != visible){ - this.mesh.visible = visible; - globals.three.render(); - } - this.mesh.visible = visible; - }, - - isVisible: function(){ - return this.mesh.visible; - }, - - setNothingHighlighted: function(){ - this.highlightedObject = null; - this.direction = null; - this.position = null; - this.hide(); - }, - - highlight: function(intersection){ - if (!intersection.object) return; - var highlighted = intersection.object; - if (!(highlighted.parent instanceof THREE.Scene)) highlighted = highlighted.parent;//cell mesh parent is object3d - if (!highlighted.myParent) { - console.warn("no parent for highlighted object"); - return; - } - - this.highlightedObject = highlighted.myParent; - - 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(); - return; - } - this.direction = highlightedPos.direction; - this._setPosition(highlightedPos.position, this.direction);//position of center point - this._setRotation(this.direction); - - this.show(true); - }, - - /////////////////////////////////////////////////////////////////////////////////// - /////////////////////////////POSITION/SCALE//////////////////////////////////////// - /////////////////////////////////////////////////////////////////////////////////// - - getHighlightedObjectPosition: function(){//origin selection - if (this.highlightedObject instanceof DMACell) { - var position = this.highlightedObject.getPosition(); - return { - x:parseFloat(position.x.toFixed(4)), - y:parseFloat(position.y.toFixed(4)), - z:parseFloat(position.z.toFixed(4)) - }; - } - return null; - }, - - _setPosition: function(position){ - this.mesh.position.set(position.x, position.y, position.z); - }, - - _setRotation: function(direction){ - this.mesh.rotation.set(direction.y*Math.PI/2, direction.x*Math.PI/2, 0); - }, - - _superCellParamDidChange: function(){ - if (this.updateGikLength) this.updateGikLength(); - }, - - /////////////////////////////////////////////////////////////////////////////////// - /////////////////////////////ADD REMOVE//////////////////////////////////////////// - /////////////////////////////////////////////////////////////////////////////////// - - _getNextCellPosition: function(){//add direction vector to current index - var newIndex = _.clone(this.highlightedObject.getIndex()); - var direction = this.direction; - _.each(_.keys(newIndex), function(key){ - newIndex[key] = Math.round(newIndex[key] + direction[key]); - }); - return newIndex; - }, - - addRemoveVoxel: function(shouldAdd){ - if (shouldAdd){ - if (!this.isVisible() || !this.highlightedObject) return; - if (globals.lattice.get("connectionType") == "freeformFace"){ - //todo make this work for baseplane - globals.lattice.addFreeFormCell(this.mesh.position.clone(), this.highlightedObject.getOrientation(), this.direction, this.highlightedObject.getType()); - return; - } - globals.lattice.addCellAtIndex(this._getNextCellPosition()); - } else { - if (!this.highlightedObject) return; - if (!(this.highlightedObject instanceof DMACell)) return; - globals.lattice.removeCell(this.highlightedObject); - } - this.setNothingHighlighted(); - }, - - destroy: function(){ - this.setNothingHighlighted(); - globals.three.sceneRemove(this.mesh, null); - this.mesh = null; - this.stopListening(); - } -}); - -/////////////////////////////////////////////////////////////////////////////////// -/////////////////////////////////OCTA////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////////// - -OctaFaceHighlighter = Highlighter.extend({ - - _makeGeometry: function(){ - - var rad = 1/Math.sqrt(3); - var geometry = new THREE.CylinderGeometry(rad, rad, 0.01, 3);//short triangular prism - geometry.applyMatrix(new THREE.Matrix4().makeRotationX(-Math.PI/2)); - return geometry; - }, - - _setRotation: function(){ - this.mesh.rotation.set(0,0,(this.highlightedObject.getIndex().z+1)%2*Math.PI); - } - -}); - -OctaEdgeHighlighter = Highlighter.extend({ - - _makeGeometry: function(){ - return new THREE.SphereGeometry(0.2); - }, - - _setRotation: function(){} - -}); - -OctaVertexHighlighter = Highlighter.extend({ - - _makeGeometry: function(){ - return new THREE.SphereGeometry(0.2); - } - -}); - -OctaFreeFormHighlighter = Highlighter.extend({ - - _makeGeometry: function(){ - return new THREE.SphereGeometry(0.2); - }, - - _setRotation: function(){} -}); - -/////////////////////////////////////////////////////////////////////////////////// -/////////////////////////////CUBE ///////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////////// - -CubeHighlighter = Highlighter.extend({ - - _makeGeometry: function(){ - return new THREE.BoxGeometry(1,1,0.01); - } - -}); - -GIKHighlighter = Highlighter.extend({ - - _makeGeometry: function(){ - return new THREE.BoxGeometry(1,1,globals.lattice.zScale(0)); - }, - - _setPosition: function(position, direction){ - 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){ - 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); - else this.mesh.rotation.set(0,0,0); - this.mesh.translateX(superCellIndex - this.mesh.scale.x/2 + 0.5); - }, - - updateGikLength: function(){ - if (!this.mesh) return; - 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); - globals.three.render(); - }, - - _getNextCellPosition: function(){//add direction vector to current index - var newIndex = this.highlightedObject.getIndex(); - var direction = this.direction; - _.each(_.keys(newIndex), function(key){ - newIndex[key] = Math.round(newIndex[key] + direction[key]); - }); - - var offset = globals.appState.get("superCellIndex"); - if (newIndex.z%2 == 0) newIndex.x -= offset; - else newIndex.y -= offset; - return newIndex; - } - -}); - - -TruncatedCubeHighlighter = Highlighter.extend({ - - _makeGeometry: function(){ - return new THREE.BoxGeometry(1,1,0.01); - }, - - _setRotation: function(direction){ - this.mesh.rotation.set(direction.y*Math.PI/2, direction.x*Math.PI/2, Math.PI/4); - } - -}); \ No newline at end of file diff --git a/js/three/ThreeView.js b/js/three/ThreeView.js index 6792ff94d659c42131126496441546c02c02ffe8..b5341cce9c6a651d2f6de69da10241b6bb5a6b53 100644 --- a/js/three/ThreeView.js +++ b/js/three/ThreeView.js @@ -2,7 +2,7 @@ * Created by aghassaei on 1/16/15. */ -define(['backbone', 'threeModel', 'orbitControls', 'appState'], function(Backbone, three, THREE, appState){ +define(['backbone', 'threeModel', 'orbitControls', 'appState', 'globals'], function(Backbone, three, THREE, appState, globals){ return Backbone.View.extend({