A lot of stuff, I'm completely lost 😢

This commit is contained in:
Thomas FORGIONE 2015-10-26 15:56:18 +01:00
parent 8ed2b18d49
commit 4768a3056c
14 changed files with 274 additions and 96 deletions

View File

@ -251,12 +251,27 @@ Lib.durationBetweenCoins = function(exp) {
};
Lib.toLaTeXMatrix = function(mat) {
var str = 'x,y,r\n';
var str = 'x,y,r,g\n';
var colSums = [];
for (var i = 0; i < mat.length; i++) {
colSums[i] = 0;
for (var j = 0; j < mat[i].length; j++) {
colSums[i] += mat[i][j];
}
}
for (var i = 0; i < mat.length; i++) {
for (var j = 0; j < mat[i].length; j++) {
str += i + ',' + j + ',' + mat[i][j] + '\n';
str += i + ',' + j + ',' + mat[i][j] + ',' + (mat[i][j] > colSums[i] / 3 ? '1' : '0' ) + ' \n';
}
// str += i === mat.length - 1 ? '' : '\n';

View File

@ -49,7 +49,8 @@ function main(path) {
// console.log(lib.toMatlabArray('X', lib.range(0, groups.length)));
var header =
'\\begin{axis}[\n'
'% This file has been automatically generated\n'
+ '\\begin{axis}[\n'
+ ' ybar,\n'
+ ' enlargelimits=0.05,\n'
+ ' legend style={at={(0.5,-0.15)},\n'
@ -61,13 +62,12 @@ function main(path) {
for (var i = 0; i < groups.length; i++) {
header += i + (i === groups.length -1 ? '' : ',');
}
1,2,3,4,5,6,7,8,9,10
header += '},\n'
+ ' xtick=data,\n'
+ ' nodes near coords,\n'
+ ' width=32cm,\n'
+ ' height=10cm,\n'
+ ' width=60cm,\n'
+ ' height=20cm,\n'
+ ' % nodes near coords align5={vertical},\n'
+ ']\n';

View File

@ -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,247,259];
var recoExps = [10,27,28,57,68,83,127,129,145,192,205,206,209,210,212,214,236,240,247,259,270,271,286,302,303];
var db = lib.loadFromFile(path);
var mat1 = zeros(12); // Bombomb

View File

@ -1,5 +1,6 @@
"use strict";
// REQUIRES AND INITS
let width = Math.floor(1134/10);
let height = Math.floor(768 /10);
@ -17,16 +18,18 @@ 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',
// '/static/data/bobomb/bobomb battlefeild.obj',
scene,
null
);
var finished = false;
let frame = 0;
let raycaster = new THREE.Raycaster();
let id = 1;
main();
var total = 0;
let loader, progLoader;
let id = 56;
// INIT COLORS
var colors = [[0,0,0]];
for (let i = 0; i < 856; i++) {
@ -37,6 +40,51 @@ for (let i = 0; i < 856; i++) {
]);
}
var camera; var finished = false;
var Recommendation = L3D.BaseRecommendation;
var forceFinished = false;
main();
// FUNCTIONS
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) {
var data = JSON.parse(xhr.responseText);
console.log('%sceneId = ' + data.sceneInfo.sceneId);
console.log('%recoStyle = ' + data.sceneInfo.recommendationStyle);
console.log('%expId = ' + id);
console.log();
console.log('M = [');
camera = new L3D.ReplayCamera(50, width / height, 0.01, 100000, [], data, () => finished = true);
var path = initElements(camera, data.sceneInfo);
loader = new L3D.ProgressiveLoader(
path, new THREE.Object3D(), null
);
progLoader = new L3D.ProgressiveLoader(
path, scene, camera, null, null, true
);
loader.load(function(){
process.stderr.write('Loading complete.\n');
progLoader.load(function() {
process.stderr.write("Loading complete\n");
forceFinished = true;
});
init(data);
});
}
};
xhr.send();
}
function testDistance(old, newP) {
return (
@ -45,60 +93,70 @@ function testDistance(old, newP) {
);
}
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();
function initElements(camera, sceneInfo) {
switch (sceneInfo.sceneId) {
case 1:
camera.resetElements = L3D.resetPeachElements();
camera.cameras = L3D.createPeachRecommendations(width, height);
camera.speed = 0.001;
return '/static/data/castle/princess peaches castle (outside).obj';
case 2:
camera.resetElements = L3D.resetBobombElements();
camera.cameras = L3D.createBobombRecommendations(width, height);
camera.speed = 0.005;
return '/static/data/bobomb/bobomb battlefeild.obj';
case 3:
camera.resetElements = L3D.resetMountainElements();
camera.cameras = L3D.createMountainRecommendations(width, height);
camera.speed = 0.005;
return '/static/data/mountain/coocoolmountain.obj';
case 4:
camera.resetElements = L3D.resetWhompElements();
camera.cameras = L3D.createWhompRecommendations(width, height);
camera.speed = 0.002;
return '/static/data/whomp/Whomps Fortress.obj';
default:
console.err('This sceneId doesn\'t exist');
process.exit(-1);
}
}
var camera;
var finished = false;
function init(data) {
camera = new L3D.ReplayCamera(50, width / height, 0.01, 100000, [], data, () => finished = true);
scene.add(camera);
camera.resetElements = L3D.resetPeachElements();
camera.reset();
camera.speed = 0.001;
camera.start();
setTimeout(loop, 0);
}
function equalFaces(face1, face2) { return face1.a === face2.a && face1.b === face2.b && face1.c === face2.c; };
function printVector(vec) { console.log(`(${vec.x},${vec.y},${vec.z})`); }
var finished = false;
let frame = 0;
let raycaster = new THREE.Raycaster();
let old;
var total = 0;
function loop() {
let old;
old = {
position: new THREE.Vector3().copy(camera.position),
target: new THREE.Vector3().copy(camera.target)
};
do {
for (let i = 0; i < 10; i++)
finished = camera.update(20);
for (let i = 0; i < 10; i++)
camera.update(20);
total++;
total++;
console.log(total);
if (!testDistance(old, camera) && !finished && !forceFinished) {
process.nextTick(loop);
finish();
return;
}
} while (!testDistance(old, camera) && !finished);
camera.look();
camera.updateMatrixWorld(true);
@ -106,11 +164,9 @@ function loop() {
// printVector(camera.position);
let buf = [];
let buf2 = [];
// let buf2 = [];
let colorsOccurence = [];
let max = 0;
for (let i = 0; i < 256; i++) { colorsOccurence[i] = 0; }
let score = 0;
for (let i = 0; i < width; i++) {
@ -126,25 +182,28 @@ function loop() {
raycaster.setFromCamera({x:x, y:y}, camera);
let intersects = raycaster.intersectObjects(scene.children, true);
let grey = 0;
try {
grey = intersects[0].faceIndex + 1;
buf[i][j] = grey;
} catch (e) {
let intersectsProg = raycaster.intersectObject(progLoader.obj, true);
let intersects = raycaster.intersectObject(loader.obj, true);
if (intersectsProg.length === 0 && intersects.length === 0) {
score++;
} else if (intersectsProg.length !== intersects.length) {
// Not good
} else if (equalFaces(intersectsProg[0].face, intersects[0].face)) {
score++;
}
if (intersectsProg.length > 0) {
buf[i][j] = intersectsProg[0].faceIndex;
}
// if( ++colorsOccurence[grey] > max) {
// max = colorsOccurence[grey];
// }
buf[i][j] = grey;
}
}
score /= width * height;
// for (let i = 0; i < 255; i++) {
// colorsOccurence[i+1] += colorsOccurence[i];
// }
@ -173,9 +232,14 @@ function loop() {
// Write image buffer to disk
frame++;
console.log(score);
process.stderr.write('Frame : ' + frame++ + ', score = ' + score + '\n');
if (!finished)
loop();
if (!finished && !forceFinished)
setTimeout(loop,50);
else
finish();
}
function finish() { console.log('];') }

View File

@ -96,7 +96,8 @@ DBReq.Info = function(id, finishAction) {
hovered: false,
pointerLocked: false,
switchedLockOption: false,
redCoins: false
redCoins: false,
sceneInfo: false
};
/**
@ -150,6 +151,7 @@ DBReq.Info.prototype.execute = function() {
this.loadSwitchedLockOption();
this.loadPointerLocked();
this.loadRedCoins();
this.loadSceneInfo();
};
/**
@ -209,6 +211,8 @@ DBReq.Info.prototype.merge = function() {
for (i = 0; i < this.redCoins.length; i++) {
this.finalResult.redCoins.push(this.redCoins[i]);
}
this.finalResult.sceneInfo = this.sceneInfo;
};
/**
@ -529,6 +533,30 @@ DBReq.Info.prototype.loadRedCoins = function() {
);
};
DBReq.Info.prototype.loadSceneInfo = function() {
var self = this;
this.client.query(
'SELECT Experiment.recommendation_style AS "recommendationStyle", \n' +
' CoinCombination.scene_id AS "sceneId" \n' +
'FROM Experiment, CoinCombination \n' +
'WHERE Experiment.coin_combination_id = CoinCombination.id AND Experiment.id = $1;',
[self.id],
function(err, result) {
if (err !== null) {
Log.dberror(err + ' in loadSceneInfo');
console.log(err);
} else {
self.sceneInfo = {
recommendationStyle : result.rows[0].recommendationStyle,
sceneId : result.rows[0].sceneId
};
}
self.ready.sceneInfo = true;
self.tryMerge();
}
);
};
/**
* Class that creates a user
* @param workerId {string} the name of the person doing the experiment

View File

@ -121,9 +121,9 @@ geo.MeshStreamer.prototype.faceComparator = function(camera) {
return function(face1, face2) {
var center1 = {
x: (self.vertices[face1.a].x + self.vertices[face1.b].x + self.vertices[face1.b].x) / 3,
y: (self.vertices[face1.a].y + self.vertices[face1.b].y + self.vertices[face1.b].y) / 3,
z: (self.vertices[face1.a].z + self.vertices[face1.b].z + self.vertices[face1.b].z) / 3
x: (self.mesh.vertices[face1.a].x + self.mesh.vertices[face1.b].x + self.mesh.vertices[face1.b].x) / 3,
y: (self.mesh.vertices[face1.a].y + self.mesh.vertices[face1.b].y + self.mesh.vertices[face1.b].y) / 3,
z: (self.mesh.vertices[face1.a].z + self.mesh.vertices[face1.b].z + self.mesh.vertices[face1.b].z) / 3
};
@ -142,9 +142,9 @@ geo.MeshStreamer.prototype.faceComparator = function(camera) {
var dot1 = direction.x * dir1.x + direction.y * dir1.y + direction.z * dir1.z;
var center2 = {
x: (self.vertices[face2.a].x + self.vertices[face2.b].x + self.vertices[face2.b].x) / 3,
y: (self.vertices[face2.a].y + self.vertices[face2.b].y + self.vertices[face2.b].y) / 3,
z: (self.vertices[face2.a].z + self.vertices[face2.b].z + self.vertices[face2.b].z) / 3
x: (self.mesh.vertices[face2.a].x + self.mesh.vertices[face2.b].x + self.mesh.vertices[face2.b].x) / 3,
y: (self.mesh.vertices[face2.a].y + self.mesh.vertices[face2.b].y + self.mesh.vertices[face2.b].y) / 3,
z: (self.mesh.vertices[face2.a].z + self.mesh.vertices[face2.b].z + self.mesh.vertices[face2.b].z) / 3
};
var dir2 = {
@ -184,7 +184,11 @@ geo.MeshStreamer.prototype.start = function(socket) {
var self = this;
socket.on('request', function(path) {
socket.on('request', function(path, laggy) {
if (laggy === true) {
self.chunk = 1;
}
self.mesh = geo.availableMeshes[path];
@ -337,13 +341,16 @@ geo.MeshStreamer.prototype.nextElements = function(_camera, force) {
var sent = 0;
var data = [];
// Sort faces
var mightBeCompletetlyFinished = true;
for (var meshIndex = 0; meshIndex < this.mesh.meshes.length; meshIndex++) {
var currentMesh = this.mesh.meshes[meshIndex];
// Sort faces
// if (camera !== undefined && camera !== null)
// currentMesh.faces.sort(this.faceComparator(camera))
if (this.isFinished(meshIndex)) {
continue;
@ -506,7 +513,7 @@ geo.MeshStreamer.prototype.nextElements = function(_camera, force) {
sent++;
if (sent > 500) {
if (sent > this.chunk) {
return {data: data, finished: false};

View File

@ -139,6 +139,10 @@ function resizeElements() {
var obj = arguments[i];
if (obj == null) {
return;
}
if (obj instanceof Element) {
obj.style.width = width + 'px';
@ -162,7 +166,8 @@ function appendTo(container) {
for (var i = 0; i < arguments.length; i++) {
container.appendChild(arguments[i].domElement);
if (arguments[i] != null)
container.appendChild(arguments[i].domElement);
}

View File

@ -14,7 +14,9 @@ L3D.ProgressiveLoader.onFinished = function() {
coins[i].mesh.visible = true;
}
loadingCanvas.clear();
if (initMainScene !== L3D.initSponza)
loadingCanvas.clear();
L3D.DB.enable();
camera1.reset();
};
@ -34,6 +36,7 @@ var coins = [];
var previousTime;
var pointer;
var startCanvas;
var loadingCanvas;
window.onbeforeunload = function() {
@ -74,7 +77,8 @@ function main() {
Coin.update(true);
loadingCanvas.render();
if (initMainScene !== L3D.initSponza)
loadingCanvas.render();
if (locked !== undefined && locked)
startCanvas.render();
@ -146,7 +150,8 @@ function initCanvases() {
// Init start canvas
startCanvas = new L3D.StartCanvas(camera1);
loadingCanvas = new L3D.LoadingCanvas();
if (initMainScene !== L3D.initSponza)
loadingCanvas = new L3D.LoadingCanvas();
}
function initModels() {

View File

@ -52,6 +52,8 @@ L3D.ReplayCamera.prototype.update = function(time) {
// // Nothing to do
// }
}
return this.finished;
};
L3D.ReplayCamera.prototype.linearMotion = function(time) {
@ -95,6 +97,7 @@ L3D.ReplayCamera.prototype.nextEvent = function() {
// Finished
if (this.counter >= this.path.length) {
this.started = false;
this.finished = true;
// console.log('The replay is finished');
if (typeof this.callback === 'function') {
this.callback();
@ -113,21 +116,23 @@ L3D.ReplayCamera.prototype.nextEvent = function() {
if (this.coins[i].id === this.event.id)
this.coins[i].get();
}
this.nextEvent();
// Wait a little before launching nextEvent
(function(self) {
setTimeout(function() {
self.nextEvent();
},500);
})(this);
// (function(self) {
// setTimeout(function() {
// self.nextEvent();
// },500);
// })(this);
} else if (this.event.type == 'arrow') {
this.moveHermite(this.cameras[this.event.id]);
} else if (this.event.type == 'reset') {
this.reset();
(function (self) {
setTimeout(function() {
self.nextEvent();
},500);
})(this);
this.nextEvent();
//(function (self) {
// setTimeout(function() {
// self.nextEvent();
// },500);
//})(this);
} else if (this.event.type == 'previousnext') {
this.move(this.event);
} else {
@ -205,3 +210,38 @@ L3D.ReplayCamera.prototype.moveHermite = function(recommendation) {
};
L3D.ReplayCamera.prototype.save = function() {};
/**
* Creates a list containing all the elements to send to the server to stream visible part
* @return {Array} A list containing <ol start="0">
* <li>the position of the camera</li>
* <li>the target of the camera</li>
* <li>and planes defining the frustum of the camera (a,b,c, and d from ax+by+cz+d=0)</li>
* </ol>
*/
L3D.ReplayCamera.prototype.toList = function() {
this.updateMatrix();
this.updateMatrixWorld();
var frustum = new THREE.Frustum();
var projScreenMatrix = new THREE.Matrix4();
projScreenMatrix.multiplyMatrices(this.projectionMatrix, this.matrixWorldInverse);
frustum.setFromMatrix(new THREE.Matrix4().multiplyMatrices(this.projectionMatrix, this.matrixWorldInverse));
var ret =
[[this.position.x, this.position.y, this.position.z],
[this.target.x, this.target.y, this.target.z]];
for (var i = 0; i < frustum.planes.length; i++) {
var p = frustum.planes[i];
ret.push([
p.normal.x, p.normal.y, p.normal.z, p.constant
]);
}
return ret;
};

View File

@ -88,7 +88,7 @@ var _parseList = function(arr) {
* @constructor
* @memberOf L3D
*/
var ProgressiveLoader = function(path, scene, camera, callback, log) {
var ProgressiveLoader = function(path, scene, camera, callback, log, laggy) {
/**
* Path to the .obj file
@ -118,6 +118,11 @@ var ProgressiveLoader = function(path, scene, camera, callback, log) {
*/
this.callback = callback;
/**
* Boolean indicate that we want extra lag for testing purposes
*/
this.laggy = laggy;
/**
* Group where the sub-objects will be added
* @type {THREE.Object3D}
@ -169,7 +174,7 @@ var ProgressiveLoader = function(path, scene, camera, callback, log) {
* Socket to connect to get the mesh
* @type {socket}
*/
this.socket = typeof io === 'function' ? io() : require('socket.io-client').connect('http://localhost:4000');
this.socket = typeof io === 'function' ? io() : require('socket.io-client').connect('http://localhost:4000', {multiplex: false});
this.initIOCallbacks();
@ -240,7 +245,7 @@ ProgressiveLoader.prototype.getCamera = function() {
if (this.camera === null)
return null;
return this.toList();
return this.camera.toList();
};
/**
@ -365,7 +370,7 @@ ProgressiveLoader.prototype.initIOCallbacks = function() {
self.meshes[elt.mesh].geometry.normalsNeedUpdate = true;
self.meshes[elt.mesh].geometry.groupsNeedUpdate = true;
if (self.meshes[elt.mesh].faceNumber === self.meshes[elt.mesh].geometry.faces.length) {
if (self.meshes[elt.mesh].faceNumber === self.meshes[elt.mesh].geometry.faces.length || typeof module === 'object') {
self.meshes[elt.mesh].geometry.computeBoundingSphere();
@ -382,7 +387,11 @@ ProgressiveLoader.prototype.initIOCallbacks = function() {
}
// Ask for next elements
self.socket.emit('next', self.getCamera());
if (!self.laggy) {
self.socket.emit('next', self.getCamera());
} else {
setTimeout(function() { self.socket.emit('next', self.getCamera());}, 100);
}
});
this.socket.on('disconnect', function() {
@ -405,7 +414,7 @@ ProgressiveLoader.prototype.initIOCallbacks = function() {
* Starts the communication with the server
*/
ProgressiveLoader.prototype.start = function() {
this.socket.emit('request', this.objPath);
this.socket.emit('request', this.objPath, this.laggy);
};
return ProgressiveLoader;

View File

@ -25,7 +25,7 @@ t = [0,1];
f = [0,1];
fp = [-1,-1];
var hermite = new Hermite.special.Polynom(0, 1, -1);
var hermite = new L3D.Hermite.special.Polynom(0, 1, -1);
print('M = [');
for (var t = 0; t < 1; t += 0.01) {

View File

@ -8,6 +8,8 @@ L3D.BaseRecommendation = function() {
THREE.Object3D.apply(this);
this.camera = new THREE.PerspectiveCamera();
this.camera.position.copy(arguments[4]);
this.camera.target = arguments[5];
};

View File

@ -1,3 +1,6 @@
var Reco = (typeof Recommendation !== 'undefined' ? Recommendation : L3D.BaseRecommendation);
var Recommendation = Reco;
L3D.LogFunction = function(a,b) {
var val = 100*a/b;
$('.progress-bar').css('width', val+'%').attr('aria-valuenow', val);
@ -112,7 +115,7 @@ L3D.initZeldaScene = function(scene, collidableObjects, loader) {
L3D.createPeachRecommendations = function(width, height, rec) {
var recos = [];
var Reco = rec !== undefined ? rec : Recommendation;
var Reco = rec !== undefined ? rec : (typeof Recommendation !== 'undefined' ? Recommendation : L3D.BaseRecommendation);
var createRecommendation = function(position, target) {
return new Reco(

View File

@ -43,7 +43,7 @@ L3D.DB.Private.sendData = function(url, data, force) {
*/
if (typeof module === 'object')
DB_DISABLED = true;
L3D.DB.Private.enabled = !DB_DISABLED;
L3D.DB.Private.enabled = typeof DB_DISABLED === 'undefined' ? true : !DB_DISABLED;
/**
* Enables the requests