-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom.js
382 lines (330 loc) · 12.2 KB
/
custom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
scene = document.querySelector('a-scene');
scaling = 20;
sceneOffset = new THREE.Vector3(1,1,0);
showSpeed = false;
showDir = false;
currentAnimStep=0;
//last added replay
listOfDPs = new Array();
//document.getElementById("cursoranim").addEventListener("animationbegin", function(){ alert("Hello World!"); });
//register animation end to add new position
document.getElementById("cursor").addEventListener("animationend", nextAnimationStep);
function Datapoint(){
this.timestamp = 0.0; //time in ms
this.position = new THREE.Vector3();//in m(?)
this.rotation = new THREE.Quaternion();//in m(?)
this.distanceToNext = Number.POSITIVE_INFINITY;
}
var coordinates = AFRAME.utils.coordinates;
AFRAME.registerComponent('line', {
// Allow line component to accept vertices and color.
schema: {
color: { default: '#333333' },
path: {
default: [
{ x: -0.5, y: 0, z: 0 },
{ x: 0.5, y: 0, z: 0 }
],
// Deserialize path in the form of comma-separated vec3s: `0 0 0, 1 1 1, 2 0 3`.
parse: function (value) {
return value.map(coordinates.parse);//removed split(",") because object creation does fail if set via setattribute
},
// Serialize array of vec3s in case someone does
// setAttribute('line', 'path', [...]).
stringify: function (data) {
return data.map(coordinates.stringify).join(',');
}
}
},
// Create or update the line geometry.
update: function (oldData) {
// Set color with material.
var material = new THREE.LineBasicMaterial({
color: this.data.color,
transparent : true,
opacity: 0.6
});
// Add vertices to geometry.
var geometry = new THREE.Geometry();
this.data.path.forEach(function (vec3) {
geometry.vertices.push(
new THREE.Vector3(vec3.x, vec3.y, vec3.z)
);
});
// Apply mesh.
this.el.setObject3D('mesh', new THREE.Line(geometry, material));
},
remove: function () {
this.el.removeObject3D('mesh');
}
});
AFRAME.registerComponent('segmentline', {
// Allow line component to accept vertices and color.
schema: {
color: {
default : ['#333333'],
},
path: {
default: [
{ x: -0.5, y: 0, z: 0 },
{ x: 0.5, y: 0, z: 0 }
],
// Deserialize path in the form of comma-separated vec3s: `0 0 0, 1 1 1, 2 0 3`.
parse: function (value) {
return value.map(coordinates.parse);//removed split(",") because object creation does fail if set via setattribute
},
// Serialize array of vec3s in case someone does
// setAttribute('line', 'path', [...]).
stringify: function (data) {
return data.map(coordinates.stringify).join(',');
}
}
},
// Create or update the line geometry.
update: function (oldData) {
var group = new THREE.Object3D();//create an empty container
for (i=0;i<this.data.path.length-1;i++) {
var geometry = new THREE.Geometry();
geometry.vertices.push( new THREE.Vector3(this.data.path[i].x, this.data.path[i].y, this.data.path[i].z));
geometry.vertices.push( new THREE.Vector3(this.data.path[i+1].x, this.data.path[i+1].y, this.data.path[i+1].z));
group.add( new THREE.Line(geometry, new THREE.LineBasicMaterial({
color: this.data.color[i],
transparent : true,
opacity: 1.0
})) );//add a mesh with geometry to it
}
this.el.setObject3D('mesh', group);
},
remove: function () {
this.el.removeObject3D('mesh');
}
});
/*
Adds the left and right target to scene.
*/
function addTargetsToDOM(){
//add left target
var matrix = new THREE.Matrix4();
matrix.set(0.001, 0.000, 0.000, -0.175,0.000, 0.001, 0.000, 0.000,0.000, 0.000, 0.001, 0.000,0.000, 0.000, 0.000, 1.000);
var position = new THREE.Vector3();
var quaternion = new THREE.Quaternion();
var scale = new THREE.Vector3();
matrix.decompose( position, quaternion, scale );
position.multiplyScalar(scaling);
position.add(sceneOffset);
var targetL = document.createElement("a-sphere");
targetL.setAttribute("position",position);
targetL.setAttribute("radius",0.03)
scene.appendChild(targetL);
//add right target
matrix = new THREE.Matrix4();
matrix.set(0.001, 0.000, 0.000, 0.175,0.000, 0.001, 0.000, 0.000,0.000, 0.000, 0.001, 0.000,0.000, 0.000, 0.000, 1.000);
position = new THREE.Vector3();
quaternion = new THREE.Quaternion();
scale = new THREE.Vector3();
matrix.decompose( position, quaternion, scale );
position.multiplyScalar(scaling);
position.add(sceneOffset);
var targetR = document.createElement("a-sphere");
targetR.setAttribute("position",position);
targetR.setAttribute("radius",0.03)
scene.appendChild(targetR);
}
/*
adds one trial of one user to the DOM for the A-Frame framework
taskID: the id of the task (1-4)
trialId: user id
color: color of line
*/
function addTrialToDOM(pathToReplay, taskId, trialId, color) {
//start reading file
var linesOfDataFile ="";
var rawFile = new XMLHttpRequest();
rawFile.open("GET", pathToReplay, false);
rawFile.onreadystatechange = function (){
if(rawFile.readyState === 4 && (rawFile.status === 200 || rawFile.status == 0)){
parseText(taskId, trialId, color, rawFile.responseText)
}
}
rawFile.send(null);
}
//when file is loaded, parse it
function parseText(taskId, trialId, color, text) {
lines = text.split('\n');
listOfDPs = [];//clear list of datapoints because we will get a new one
var lastDP = null;
var maxDistance = 0;
var maxDt = 0;
var maxDv = 0;
var newFormat = false;
var duplicatesCount = 0;
var scaleDump = new THREE.Vector3();//dump values
for (var i = 0; i < lines.length; i++) {
//check if new format
if (i==0 && lines[0].startsWith("taskSet")) {
i += 1;
newFormat = true;
console.log("using new format");
}
//create new datapoint with timestamp and matrix
if (newFormat || lines[i].startsWith("t:")) {
dp = new Datapoint();
if (!newFormat) {
dp.timestamp = parseFloat(lines[i].substring(2, lines[i].length));
i += 1; //skip next line
var matrix = new THREE.Matrix4();
var splitted = lines[i].split(' ');
var j = 0;
//row major, fromArray() is column-major
matrix.set(
parseFloat(splitted[j++]),
parseFloat(splitted[j++]),
parseFloat(splitted[j++]),
parseFloat(splitted[j++]),
parseFloat(splitted[j++]),
parseFloat(splitted[j++]),
parseFloat(splitted[j++]),
parseFloat(splitted[j++]),
parseFloat(splitted[j++]),
parseFloat(splitted[j++]),
parseFloat(splitted[j++]),
parseFloat(splitted[j++]),
parseFloat(splitted[j++]),
parseFloat(splitted[j++]),
parseFloat(splitted[j++]),
parseFloat(splitted[j++])
);
//var quaternion = new THREE.Quaternion();
matrix.decompose( dp.position, dp.rotation, scaleDump );
} else {
var splitted = lines[i].split(',');
dp.timestamp = parseFloat(splitted[2]);
dp.position.x = parseFloat(splitted[3]);
dp.position.y = parseFloat(splitted[4]);
dp.position.z = parseFloat(splitted[5]);
dp.rotation.x = parseFloat(splitted[6]);
dp.rotation.y = parseFloat(splitted[7]);
dp.rotation.z = parseFloat(splitted[8]);
dp.rotation.w = parseFloat(splitted[9]);
}
//dp.position = new THREE.Vector3(1, 1, 1).applyMatrix4(matrix);
//dp.rotation = new THREE.Quaternion().setFromRotationMatrix(matrix.extractRotation());
//shift data to view, then add to path
//dp.position.applyQuaternion(quaternion);
dp.position.multiplyScalar(scaling);
dp.position.add(sceneOffset);
//current one is duplicate
if (listOfDPs.length > 0 && dp.position.equals(listOfDPs[listOfDPs.length-1].position) && dp.rotation.equals(listOfDPs[listOfDPs.length-1].rotation)) {
duplicatesCount++;
//current one is the last one
listOfDPs[listOfDPs.length-1].timestamp = dp.timestamp;//refrehs timestamp to last know timestamp at this pos
dp = listOfDPs[listOfDPs.length-1];
} else {
listOfDPs.push(dp);
}
//calcualte distance of last DP to the current one
if (showSpeed && lastDP != null) {
lastDP.distanceToNext = lastDP.position.distanceTo(dp.position);
var dt = dp.timestamp - lastDP.timestamp;
var dv = lastDP.distanceToNext / dt;
if (lastDP.distanceToNext > maxDistance)
maxDistance = lastDP.distanceToNext;
if (dt > maxDt)
maxDt = dt;
if (dv > maxDv)
maxDv = dv;
}
lastDP = dp;
}
}
console.log("Done parsing to "+ listOfDPs.length + " elements. Max dv:"+maxDv+" maxDistance: "+maxDistance+" max dt: "+ maxDt+"s. Removed "+duplicatesCount+" duplicate points.");
//add to one DOM object
var lineDOMObject = document.createElement("a-entity");
lineDOMObject.setAttribute("class",taskId+"-"+trialId);
var path = [];
if (!showSpeed){
lineDOMObject.setAttribute("line", "path", path);
lineDOMObject.setAttribute("line","color", color)
} else {
lineDOMObject.setAttribute("segmentline", "path", path);
color = [];
lineDOMObject.setAttribute("segmentline","color", color)
}
var up = new THREE.Vector3(0,1,0);
//add every position value
for (var i = 0; i < listOfDPs.length; i++) {
//if option for delta speed is enabled show with color
if (showSpeed && maxDv > 0 && i < listOfDPs.length-1) {
var dt = listOfDPs[i+1].timestamp - listOfDPs[i].timestamp;//time
var dv = listOfDPs[i].distanceToNext / dt;//velocity
var hexBrightness = (parseInt(255*dv / maxDv)).toString(16);
if (hexBrightness.length == 1)//avoid invalid color
hexBrightness="0".concat(hexBrightness);
//make red if above 80% of max speed
var redfilter=hexBrightness.concat(hexBrightness);
if (dt > 2*maxDt / 5){
redfilter = "0000";
}
color.push("#".concat(hexBrightness).concat(redfilter));
}
path.push(listOfDPs[i].position);
//add cones for direction
if (showDir && i % 3 == 0 && i < listOfDPs.length-1) { //skip some elements
//add cone for direction
var cone = document.createElement("a-cone");
//cone.setAttribute("geometry","primitive","cone");
cone.setAttribute("class", taskId+"-"+trialId)
cone.setAttribute("color", color);
cone.setAttribute("radius-bottom", 0.008);
cone.setAttribute("radius-top", 0);
cone.setAttribute("open-ended", false);
cone.setAttribute("geometry", "segmentsRadial", 4);
cone.setAttribute("geometry", "segmentsHeight", 4);
cone.setAttribute("height", 0.04);
cone.setAttribute("position", listOfDPs[i].position);
var norm = new THREE.Vector3().copy(listOfDPs[i + 1].position).sub(listOfDPs[i].position).normalize();
var euler = new THREE.Euler(0, 0, 0, 'XYZ');
euler.setFromQuaternion(new THREE.Quaternion().setFromUnitVectors ( up, norm));
cone.setAttribute("rotation", (euler.x * 180 / Math.PI) + " " + (euler.y * 180 / Math.PI) + " "+ (euler.z * 180 / Math.PI));
scene.appendChild(cone);
}
}
scene.appendChild(lineDOMObject);
console.log("Done adding to DOM.");
currentAnimStep=0;
nextAnimationStep();
}
function nextAnimationStep(){
if (listOfDPs.length>0) {
//has more animation steps
currentAnimStep += 1;
currentAnimStep %= listOfDPs.length-1;
//remove last animation
var cursorNode = document.getElementById("cursor");
while (cursorNode.hasChildNodes()) {
cursorNode.removeChild(cursorNode.lastChild);
}
var dur = listOfDPs[currentAnimStep+1].timestamp - listOfDPs[currentAnimStep].timestamp;
var from = AFRAME.utils.coordinates.stringify(listOfDPs[currentAnimStep].position);
var to = AFRAME.utils.coordinates.stringify(listOfDPs[currentAnimStep+1].position);
if (from != to) {
var anim = document.createElement("a-animation");
anim.setAttribute("attribute", "position");
anim.setAttribute("from", from);
anim.setAttribute("to", to);
anim.setAttribute("dur", dur);
cursorNode.appendChild(anim);
}
var euler1=new THREE.Euler(0,0,0, 'XYZ' ).setFromQuaternion(listOfDPs[currentAnimStep].rotation);
var euler2 = new THREE.Euler(0,0,0, 'XYZ' ).setFromQuaternion(listOfDPs[currentAnimStep+1].rotation);
if (!euler1.equals(euler2)) {
var anim = document.createElement("a-animation");
anim.setAttribute("attribute", "rotation");
anim.setAttribute("from", (euler1.x * 180 / Math.PI) + " " + (euler1.y * 180 / Math.PI) + " "+ (euler1.z * 180 / Math.PI));
anim.setAttribute("to", (euler2.x * 180 / Math.PI) + " " + (euler2.y * 180 / Math.PI) + " "+ (euler2.z * 180 / Math.PI));
anim.setAttribute("dur", dur);
cursorNode.appendChild(anim);
}
}
}
addTargetsToDOM();