diff --git a/analysis/recommendationChain.js b/analysis/recommendationChain.js index c52ea56..aeb94aa 100755 --- a/analysis/recommendationChain.js +++ b/analysis/recommendationChain.js @@ -42,7 +42,7 @@ function normalize(mat) { function main(path) { // Generated with ./test.pgsql | tail -n+3 | head -n-2 | cut -d '|' -f 2 | sort -g | tr '\n' ' ' | tr -s ' ' | tr ' ' ',' - var recoExps = [10,27,28,57,68,83,127,129,145,192,205,206,209,210,212,214,236,240]; + var recoExps = [10,27,28,57,68,83,127,129,145,192,205,206,209,210,212,214,236,240,247,259]; var db = lib.loadFromFile(path); var mat1 = zeros(12); // Bombomb diff --git a/analysis/replay/main.js b/analysis/replay/main.js new file mode 100644 index 0000000..5b60d53 --- /dev/null +++ b/analysis/replay/main.js @@ -0,0 +1,151 @@ +"use strict"; + +let width = Math.floor(1134/10); +let height = Math.floor(768 /10); + +let XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; +let THREE = require('three'); +let L3D = require('../../static/js/l3d.min.js'); +let math = require('mathjs'); +let fs = require('fs'); + +let scene = new THREE.Object3D(); +let geometry = new THREE.Geometry(); + +let peach = new THREE.Object3D(); +let bobomb = new THREE.Object3D(); +let whomp = new THREE.Object3D(); +let mountain = new THREE.Object3D(); + +let loader = new L3D.ProgressiveLoader( + '/static/data/castle/princess peaches castle (outside).obj', + peach, + null +); + +let id = 1; +main(); + +var colors = [[0,0,0]]; + +for (let i = 0; i < 856; i++) { + colors.push([ + Math.floor(255*Math.random()), + Math.floor(255*Math.random()), + Math.floor(255*Math.random()) + ]); +} + +function main() { + let xhr = new XMLHttpRequest(); + xhr.open("GET", "http://localhost:4000/prototype/replay-info/" + id, true); + + xhr.onreadystatechange = function() { + if (xhr.readyState == 4 && xhr.status == 200) { + loader.load(function(){ + process.stderr.write('Loading complete.\n\n'); + init(JSON.parse(xhr.responseText)) + }); + } + }; + xhr.send(); +} + +var camera; + +var finished = false; + +function init(data) { + camera = new L3D.ReplayCamera(50, width / height, 0.01, 100000, [], data, () => finished = true); + camera.resetElements = L3D.resetPeachElements(); + camera.start(); + setTimeout(loop, 0); +} + +function printVector(vec) { console.log(`(${vec.x},${vec.y},${vec.z})`); } + +var finished = false; +let frame = 0; +let raycaster = new THREE.Raycaster(); + +function loop() { + + for (let i = 0; i < 10; i++) + camera.update(20); + + camera.look(); + // printVector(camera.position); + + let buf = []; + let buf2 = []; + + let colorsOccurence = []; + let max = 0; + for (let i = 0; i < 256; i++) { colorsOccurence[i] = 0; } + + for (let i = 0; i < width; i++) { + + buf[i] = []; + + let x = (i / width) * 2 - 1; + + // process.stderr.write('\b\r' + Math.floor(100*(i / width)) + '%\n'); + + for (let j = 0; j < height; j++) { + + let y = (j / height) * 2 - 1; + + raycaster.setFromCamera({x:x, y:y}, camera); + + let intersects = raycaster.intersectObjects(peach.children, true); + + let grey = 0; + try { + grey = intersects[0].faceIndex + 1; + buf[i][j] = grey; + } catch (e) { + + } + // if( ++colorsOccurence[grey] > max) { + // max = colorsOccurence[grey]; + // } + + buf[i][j] = grey; + + } + + } + + // for (let i = 0; i < 255; i++) { + // colorsOccurence[i+1] += colorsOccurence[i]; + // } + + var buffer = ''; + buffer += ('P3\n'); + buffer += (width+'\n'); + buffer += (height+'\n'); + buffer += (255+'\n'); + + for (let i = 0; i < height; i++) { + for (let j = 0; j < width; j++) { + var grey = buf[j][height - i - 1]; + if (colors[grey] === undefined) + buffer += '0 0 0\n'; + else { + buffer += (colors[grey][0] + ' ' + colors[grey][1] + ' ' + colors[grey][2] + '\n'); + } + } + } + + fs.writeFileSync(`img/${frame}.ppm`, buffer); + + + // Write image buffer to disk + frame++; + + console.log(frame); + + if (!finished) + setTimeout(loop, 0); +} + diff --git a/controllers/prototype/views/prototype_replays.jade b/controllers/prototype/views/prototype_replays.jade index e714908..ba73f7c 100644 --- a/controllers/prototype/views/prototype_replays.jade +++ b/controllers/prototype/views/prototype_replays.jade @@ -3,6 +3,9 @@ extends ../../../views/withjs block title title #{title} - Prototype +block prepend js + script DB_DISABLED = true; + block extrajs script Recommendation = L3D.ArrowRecommendation; script var params = params || {}; params.get = params.get || {}; params.get.id = #{id}; diff --git a/js/l3d/src/cameras/ReplayCamera.js b/js/l3d/src/cameras/ReplayCamera.js index 877b011..bbdb842 100644 --- a/js/l3d/src/cameras/ReplayCamera.js +++ b/js/l3d/src/cameras/ReplayCamera.js @@ -13,6 +13,7 @@ L3D.ReplayCamera = function() { this.newTarget = new THREE.Vector3(); this.data = arguments[5]; + this.callback = arguments[6]; this.started = true; this.path = this.data.events; @@ -94,11 +95,15 @@ L3D.ReplayCamera.prototype.nextEvent = function() { // Finished if (this.counter >= this.path.length) { this.started = false; - console.log('The replay is finished'); + // console.log('The replay is finished'); + if (typeof this.callback === 'function') { + this.callback(); + } return; } this.event = this.path[this.counter]; + // console.log(this.event.type); if (this.event.type == 'camera') { this.move(this.event); diff --git a/js/l3d/src/loaders/ProgressiveLoader.js b/js/l3d/src/loaders/ProgressiveLoader.js index 8a4dedd..72ea38d 100644 --- a/js/l3d/src/loaders/ProgressiveLoader.js +++ b/js/l3d/src/loaders/ProgressiveLoader.js @@ -163,13 +163,13 @@ var ProgressiveLoader = function(path, scene, camera, callback, log) { * Loader for the material file * @type {THREE.MTLLoader} */ - this.loader = new THREE.MTLLoader(this.texturesPath); + this.loader = typeof THREE.MTLLoader === 'function' ? new THREE.MTLLoader(this.texturesPath) : null; /** * Socket to connect to get the mesh * @type {socket} */ - this.socket = io(); + this.socket = typeof io === 'function' ? io() : require('socket.io-client').connect('http://localhost:4000'); this.initIOCallbacks(); @@ -210,19 +210,27 @@ var ProgressiveLoader = function(path, scene, camera, callback, log) { /** * Starts the loading of the mesh */ -ProgressiveLoader.prototype.load = function() { +ProgressiveLoader.prototype.load = function(callback) { var self = this; + this._callback = callback; - this.loader.load(self.mtlPath, function(materialCreator) { + if (this.loader !== null) { + this.loader.load(self.mtlPath, function(materialCreator) { - self.materialCreator = materialCreator; + self.materialCreator = materialCreator; - materialCreator.preload(); + materialCreator.preload(); + + self.start(); + + }); + + } else { self.start(); - }); + } }; /** @@ -287,7 +295,7 @@ ProgressiveLoader.prototype.initIOCallbacks = function() { // Create mesh material var material; - if (elt.materialName === null) { + if (elt.materialName === null || self.materialCreator === undefined) { // If no material, create a default material material = new THREE.MeshLambertMaterial({color: 'red'}); @@ -378,7 +386,6 @@ ProgressiveLoader.prototype.initIOCallbacks = function() { }); this.socket.on('disconnect', function() { - console.log('Finished !'); if (typeof self.log === 'function') self.log(self.numberOfFacesReceived, self.numberOfFaces); self.finished = true; @@ -387,6 +394,10 @@ ProgressiveLoader.prototype.initIOCallbacks = function() { L3D.ProgressiveLoader.onFinished(); } + if (typeof self._callback === 'function') { + self._callback(); + } + }); }; diff --git a/js/l3d/src/utils/Logger.js b/js/l3d/src/utils/Logger.js index 58a2f90..b348775 100644 --- a/js/l3d/src/utils/Logger.js +++ b/js/l3d/src/utils/Logger.js @@ -41,7 +41,9 @@ L3D.DB.Private.sendData = function(url, data, force) { * @memberof L3D.DB.Private * @type {Boolean} */ -L3D.DB.Private.enabled = !window.DB_DISABLED; +if (typeof module === 'object') + DB_DISABLED = true; +L3D.DB.Private.enabled = !DB_DISABLED; /** * Enables the requests