-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
673 lines (472 loc) · 20.5 KB
/
index.html
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
<!doctype html>
<html lang="en">
<head>
<title>MyoWebToolkit</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link rel="stylesheet" type="text/css" href="css/stylefile.css">
</head>
<body>
<!-- 3rd party javascript libraries -->
<!-- Threejs -->
<script src="libs/threejs/76/build/three.js"></script>
<!-- Threejs libraries -->
<script src="libs/threejs/76/examples/js/Detector.js"></script>
<script src="libs/threejs/76/examples/js/libs/stats.min.js"></script>
<script src="libs/threejs/76/examples/js/controls/OrbitControls.js"></script>
<script src="libs/threejs/76/examples/js/exporters/OBJExporter.js"></script>
<script src="libs/threejs/76/examples/js/loaders/OBJLoader.js"></script>
<!-- Jquery -->
<script src="libs/jquery-1.12.0.min.js"></script>
<script src="libs/jquery-ui_1.12.0.js"></script>
<!-- Plotting signals -->
<script src="libs/Flot/jquery.flot.js"></script>
<!-- Physics --->
<script type="text/javascript" src="libs/Physijs/physi.js"></script>
<script>
'use strict';
// Physics library
Physijs.scripts.worker = 'libs/Physijs/physijs_worker.js';
Physijs.scripts.ammo = 'ammo.js';
</script>
<!-- THREEX : Gaming interface capabilities -->
<script src='libs/THREEx/THREEx.WindowResize.js'></script>
<script src="libs/THREEx/THREEx.FullScreen.js"></script>
<!-- Math operations -->
<script src="libs/math.min.js"></script>
<script src="libs/numeric-1.2.6.js"></script>
<script src="libs/nnls.js"></script>
<script src="libs/pcnnls.js"></script>
<script src="libs/matrixOperations.js"></script>
<!-- Save parameters in a txt file -->
<script src="libs/FileSaver.js"></script>
<!-- Interactive pass js variables to html -->
<script src='libs/dat.gui.js'></script>
<!-- Video feature -->
<script src="libs/webcamera.js"></script>
<!-- This does the CSG -->
<script src="libs/tiansijie/index.js"></script>
<!-- MyoWebToolkit libs -->
<!-- Class where Numerical and path data are stored here -->
<script src="simulation/StorageData.js"></script>
<!-- Class to make 3D object of Myo armband -->
<script src="simulation/MyoArmband.js"></script>
<!-- Class to load muscles, bones and constraints -->
<script src="simulation/MusclesBonesConstraints.js"></script>
<!-- Class to load Muscle Forces -->
<script src="simulation/MusclesForces.js"></script>
<script src="simulation/FlexorsGroup.js"></script>
<script src="simulation/ExtensorsGroup.js"></script>
<script src="simulation/DeviatorsGroup.js"></script>
<script src="simulation/PronatorsSupinatorsGroup.js"></script>
<!-- Scene and related (camera, lights) -->
<script src="simulation/Enviromentals.js"></script>
<!-- Various -->
<script src="simulation/utilities.js"></script>
<!-- Materials -->
<script src="simulation/materials.js"></script>
<!-- Sprites -->
<script src="simulation/annotationSprites.js"></script>
<!-- My Hard Keys handler : needs mousetrap 3rd party library -->
<script src="libs/keyboard/mousetrap.min.js"></script>
<script src="simulation/hardKeys.js"></script>
<!-- Myo libraries -->
<script src='myo/myo.js'></script>
<script src='myo/imu_emg_Graphs.js'></script>
<script src='myo/myoFunct.js'></script>
<!-- All 3d loaded here -->
<div id="container"></div>
<!-- make params gui draggable -->
<script>
$( function() {
$("#my-gui-container" ).draggable();
} );
</script>
<!-- Myo graphs -->
<div id='pod0' class='pod1' style=''>Pod 1</div>
<div id='pod1' class='pod2' style=''>Pod 2</div>
<div id='pod2' class='pod3' style=''>Pod 3</div>
<div id='pod3' class='pod4' style=''>Pod 4</div>
<div id='pod4' class='pod5' style=''>Pod 5</div>
<div id='pod5' class='pod6' style=''>Pod 6</div>
<div id='pod6' class='pod7' style=''>Pod 7</div>
<div id='pod7' class='pod8' style=''>Pod 8</div>
<!-- DatGUI -->
<div id="my-gui-container" draggable=true></div>
<div id="infophp">
<div id="progress">
<span id="message">Intersecting ...</span>
<div id="progressbar" class="shadow">
<div id="bar" class="shadow" style="width:0px"></div>
</div>
</div>
<div class="result"></div>
<div class="result2"></div>
<div id="result_download"></div>
</div>
<div id="toggleMyoButton">
<button onclick='
if (this.value == "OFF") {
this.value = "ON";
Myo.connect();
this.innerHTML = "Myo Off";
} else {
this.value = "OFF";
this.innerHTML = "Myo On";
Myo.disconnect();
}'>
Toggle Myo sensor on/off </button>
</div>
<div id="togglePhysicsButton">
<button onclick='
if (this.value == "OFF") {
this.value = "ON";
this.innerHTML = "Physics Off";
isPhysicsPaused = false;
scene.onSimulationResume();
startPhysics();
} else {
this.value = "OFF";
this.innerHTML = "Physics On";
isPhysicsPaused = true;
scene.removeEventListener("update", updPh);
}'>
Toggle Physics on/off </button>
</div>
<!-- Video Camera -->
<!--<video autoplay></video> -->
<script>
var isPhysicsPaused = false;
//======== Muscle Activity =================================
var muscleActivity = new Array(15).fill(0);
//================== ENVIRONMENTALS 3D ===================
var myEnv = new Enviromentals();
//======================== FORCES ==========================
// ---- EXTENSION FORCES -------
var extensorsGroup = new ExtensorsGroup();
// --- FLEX FORCES ---
var flexGroup = new FlexorsGroup();
//------ Deviation FORCES ---------------
var deviatorsGroup = new DeviatorsGroup();
//------ Pronation FORCES ---------------
var pronatorsSupinatorsGroup = new PronatorsSupinatorsGroup();
// Another level of coefficients
var activLevel_FingersFlex = 0;
var activLevel_FingersExtent = 0;
var activLevel_FingersSmExt = 0;
var activLevel_CarpiFlex = 0;
var activLevel_CarpiExtent = 0;
var activLevel_UlnarDeviation = 0;
var activLevel_RadialDeviation = 0;
var activLevel_Pronation = 0;
var activLevel_Supination = 0;
//var sign = -1;
//var stopFlag = false;
// --------------
//var start = null;
// Get current time in ms
var timestamp = performance.now();
var cachedMuscles = true;
//var frameDelta = 0;
//var clock = new THREE.Clock();
// Construct 3D Myo Armband
leftMyoArmband = new MyoArmband();
// Bones and Muscles from MuscleBonesPath.js
var musclesBonesConstraints = new MuscleBonesConstraints();
var myModelBones = StorageData.getBonesFNs();
var myModelMuscles = StorageData.getMusclesFNs();
var nMuscles = myModelMuscles.length;
var nBones = myModelBones.length;
var myoMusclesWeightsMatrix = [] ;
//----------------------- MAIN ---------------------------------
//var spriteLabelMuscles = new Array(nMuscles);
var colors = randomColors(nMuscles);
var mshColor = new Array(nMuscles);
for (var i = 0; i < nMuscles; i++)
mshColor[i] = new THREE.MeshLambertMaterial({color: colors[i], transparent: true, opacity: 0.8, side: THREE.DoubleSide});
var meshSkinnedBone = [];
var container, scene, camera, renderer, controls, render_stats, physics_stats, ground, render;
var hanger;
var rbonesObjs = [];
var rbonesBoxes = [];
var arm_group = new THREE.Object3D();
var rotation_matrix;
// Muscles positions
var musclesInterCentroidsGlobal = new Array(nMuscles);
// Muscles volumes
var musclesInterVolumeGlobal = new Array(nMuscles);
// Myo positions
var myoPositionsGlobal = new Array(nMuscles);
var id_animation_frame;
var M_PI = Math.PI;
//------------------- DATGUI ---------------------------
// Init gui controls variables
var gui_controls = new function(){
for (var i = 0; i < nMuscles; i++)
eval('this.muscle_activation_'+i+'= 1');
};
// GUI controls
var gui = new dat.GUI( {autoPlace: false} );
gui.close();
// Video control
var video_stream_open = {open:false};
var show_video_controller = gui.add( video_stream_open, "open" );
show_video_controller.name( "Show Video" );
setOnOffCameraSwitch(show_video_controller);
// Muscles folder
var folderMuscles = gui.addFolder( "Muscles" );
folderMuscles.open();
// Put it
var customContainer = document.getElementById('my-gui-container');
customContainer.appendChild(gui.domElement);
var dg_controller = new Array(nMuscles);
//------- INITIALIZATION FUNCTION -------------------------------
function init()
{
// Enviromentals.js
scene = myEnv.setScene(scene);
camera = myEnv.setCamera(camera);
scene.add(camera);
renderer = myEnv.setRenderer(renderer);
container = document.getElementById( 'container' );
container.appendChild( renderer.domElement );
// Events
THREEx.WindowResize(renderer, camera);
THREEx.FullScreen.bindKey({ charCode : 'm'.charCodeAt(0) }); // toggle full-screen on given key press
// CONTROLS
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.target.set(500, 500, 1000); // REM
//controls.target.set(130, -80, 500); // REM
// Statistics rendering
myEnv.setStats(container);
// LIGHTs
var light = myEnv.makeLights(0, 1000, 0, 1);
scene.add(light);
//scene.add(makeLightHelper(light));
var lightB = myEnv.makeLights(0, - 1200, 0, 1);
scene.add(lightB);
var lightC = myEnv.makeLights(100, 400, 2500, 0.3);
scene.add(lightC);
//scene.add(makeLightHelper(lightC));
// Information GUI
//$("#progress").get(0).style.display = "block";
// ---- Green ground -----
//ground = myEnv.setGround(ground);
//scene.add( ground );
//------------- BOX GEOMETRY for Volume intersection -------------
//crossBoxBSP = makeCrossBox();
// ========= Load Manager Bones ======================
var managerBones = new THREE.LoadingManager();
var managerMuscles = new THREE.LoadingManager();
managerBones.onProgress = function ( item, loaded, total ) {
// var percentComplete = loaded / total * 100;
// var bar = $("#progressbar").get(0).offsetWidth;
// bar = Math.floor( bar * loaded / total );
// $("#bar").get(0).style.width = bar + "px";
$(".result").get(0).innerHTML = "Estimated: " + loaded + " / " + total + ' Bones';
//console.log(" Manager:" + Math.round(percentComplete, 2) + '% downloaded' );
};
managerMuscles.onProgress = function ( item, loaded, total ) {
$(".result2").get(0).innerHTML = "Estimated: " + loaded + " / " + total + ' Muscles';
};
var loaderMuscles = new THREE.OBJLoader( managerMuscles );
managerBones.onLoad = function (){
// Relate bones with each other and add to scene
musclesBonesConstraints.loadBoneConstraints();
// Load Myo Pods and its annotation labels
leftMyoArmband.loadMyoPods();
// Load async Muscles to
musclesBonesConstraints.asyncLoadMuscles(loaderMuscles, nMuscles, myModelMuscles);
};
var loaderBones = new THREE.OBJLoader( managerBones );
// OnLoad: execute after everything is loaded
managerMuscles.onLoad = function (){
//$("#message").get(0).innerHTML = "Completed";
$("#infophp").get(0).style.display = "none";
nMuscles = Object.keys(musclesInterVolumeGlobal).length;
// Iterate over all muscles
for (var i = 0; i < nMuscles; i++){ //
var muscleName = StorageData.getMuscleShortNames()[i];
var muscleDistArr = [];
// Calculate distances of MyoPods to Muscles
for (var j = 0; j < musclesBonesConstraints.nPods; j++){ //
//--------- Distance --------------------------------
var distVec = new THREE.Vector3();
distVec.subVectors(musclesBonesConstraints.myoPodPositions[j], musclesInterCentroidsGlobal[muscleName]);
var dist = Math.sqrt(distVec.x*distVec.x + distVec.y*distVec.y + distVec.z*distVec.z);
// console.log("Pod:", j, "Muscle:", muscleName, "dist:", dist);
//var coef = Math.round(1 / dist / dist*100)/100;
var coef = musclesInterVolumeGlobal[muscleName] / dist;
//console.log("Myo:", (j+1), "MUSCLE:", muscleName, "COEF:", coef);
muscleDistArr.push(coef);
}
//console.log(muscleName, muscleDistArr);
myoMusclesWeightsMatrix.push(muscleDistArr);
//-------------------- DATGUI Listen -------------------------------
dg_controller[i] = folderMuscles.add( gui_controls, "muscle_activation_"+i, 0, 15, 1).name(muscleName).listen();
//------------- on Dat.GUI changes then change muscles colors ------------
// When gui values changes then stop animating else won't be able to type with keyboard
dg_controller[i].onChange(function(value) {
// cancelAnimationFrame( id_animation_frame );
var str = this.domElement.parentElement.outerText;
var mname = str.substring(0, str.length- 2);
var strName = "outline_"+mname;
var myMuscle = scene.getObjectByName( strName );
if (typeof myMuscle != 'undefined'){
myMuscle.material = new THREE.MeshBasicMaterial( { color: new THREE.Color(value/50, 0, 0), side: THREE.BackSide } );
}
var p = StorageData.fmuscle(mname);
muscleActivity[p] = value;
// /animatize(timestamp);
}
);
}
// console.log(myoMusclesWeightsMatrix);
// --- Normalize rows of alpha ---
// Left Hand
var weightsLeft = numeric.transpose(myoMusclesWeightsMatrix);
// // weightsRight Estimation
// var weightsRight = [weightsLeft[6], weightsLeft[5], weightsLeft[4], weightsLeft[3],
// weightsLeft[0], weightsLeft[1], weightsLeft[2], weightsLeft[7]];
//
//
//// console.log(weightsRight);
////
//// console.log("[");
//// for (var i = 0; i<8; i++)
//// console.log("[" + weightsRight[i] + "]");
//// console.log("]");
//
//
//
// // Old approach
var weightsNormLeft = normalizeRows(myoMusclesWeightsMatrix, nMuscles);
//// // -- Mirrorize for Right Hand ------
//// var weightsNormRight = [];
////
//// for (var i = 0; i < nMuscles; i++){ //
//// var muscleName = StorageData.getMuscleShortNames()[i];
////
//// // 0 <-> 6
//// // 1 <-> 5
//// // 2 <-> 4
////
//// //console.log("LEFT HAND", muscleName, weightsNormLeft[i]);
////
////
//// var rightTransW = [weightsNormLeft[i][6], weightsNormLeft[i][5], weightsNormLeft[i][4], weightsNormLeft[i][3],
//// weightsNormLeft[i][0], weightsNormLeft[i][1], weightsNormLeft[i][2], weightsNormLeft[i][7]];
////
//// // "Right hand"
//// //console.log(muscleName, rightTransW);
////
//// weightsNormRight.push(rightTransW);
//// }
//
// //-----------------------------------
//console.log(weightsNormLeft);
var mcoefs = numeric.transpose(weightsNormLeft);
var my_nnls = new pcnnls(weightsNormLeft);
MyoActivationFunctions(musclesBonesConstraints.nPods, mcoefs, my_nnls);
animatize(timestamp);
startPhysics();
//============= Force Visualizers ======================
// extensorsGroup.addExtensorsToScene(scene);
// flexGroup.addFlexorsToScene(scene);
// deviatorsGroup.addDeviatorsToScene(scene);
// pronatorsSupinatorsGroup.addSupinatorsPronatorsToScene(scene);
}; // End of manager
//============ Load Bones ====================
// Load async Bones to ->>> rbonesObjs[i]
musclesBonesConstraints.asyncLoadBones(loaderBones, nBones, myModelBones);
}
// ---- start --------
updPh = function () {
if (!isPhysicsPaused) {
applyForce();
scene.simulate(undefined, 1);
physics_stats.update();
}
}
function startPhysics() {
scene.simulate();
controls.update();
physics_stats.update();
scene.addEventListener('update', updPh);
}
function renderdelayed(){
setTimeout( function() {
id_animation_frame = requestAnimationFrame( animatize );
}, 1000 / 25 );
}
function animatize()
{
renderdelayed();
//=================
//================
renderer.render( scene, camera );
render_stats.update();
}
// Apply force every iteration
applyForce = function() {
// ToDo: Quaternions gyro move
// ToDo: Optimize computational resources
// 0 'brachioradialis', // Supinate@pronationState // Pronate@supinationState // Flex Elbow
// 1 'Hum. hea. lef. pro. ter.', // Pronate@any
// 2 'Uln. hea. lef. pro. ter.', // Pronate@any
// 3 'ext. car. rad. lon.', // ExtentCarpi@any // RadialDeviate@any
// 4 'supinator', // Supinate@any
// 5 'ext. car. rad. bre.', // ExtentCarpi@any
// 6 'ext. dig.', // ExtentCarpi@any // ExtentFingers@any
// 7 'ext. dig. min.', // ExtentCarpi@any // ExtentLittleFinger@any
// 8 'ext. car. uln.', // ExtentCarpi@any // UlnarDeviate@any
// 9 'Uln. hea. lef. fle. car. uln.', // FlexCarpi@any // UlnarDeviate@any
// 10 'Hum. hea. lef. fle. car. uln.', // FlexCarpi@any // UlnarDeviate@any
// 11 'fle. dig. pro.', // FlexCarpi@any // FlexFingersDistal@any
// 12 'palmaris longus', // FlexCarpi@any
// 13 'fle. dig. sup.', // FlexCarpi@any // FlexFingersMiddle@any
// 14 'fle. car. rad.' // FlexCarpi@any // RadialDeviate@any
activLevel_CarpiFlex = 0.002 * (0.1 * 20.1 * muscleActivity[9] + 0.1 * 25.7 * muscleActivity[10] +
0.1 * 3.5 * muscleActivity[11] + 0.3 * 16.8 * muscleActivity[12] +
0.4 * 16.2 * muscleActivity[13] + 0.4 * 20.9 * muscleActivity[14]);
activLevel_CarpiExtent = 0.001 * (0.5 * 24.6 * muscleActivity[3] + 0.2 * 20 * muscleActivity[5] +
0.3 * 29.8 * muscleActivity[6] + 0.3 * 7.8 * muscleActivity[7] +
0.6 * 10.6 * muscleActivity[8]); // bad influence from 9. uln head left fle
if (muscleActivity[13] > 0.7*muscleActivity[6]) {
activLevel_FingersFlex = 0.005 * ( 0.5 * 53.5 * muscleActivity[11] + 0.5 * 16.21 * muscleActivity[13] );
activLevel_FingersExtent = 0;
} else {
activLevel_FingersExtent = 0.001 * ( 0.5 * 29.8 * muscleActivity[6] + 0.5 * 7.8 * muscleActivity[7]);
activLevel_FingersFlex = 0;
}
activLevel_FingersSmExt = 0.01*0.5*7.8*muscleActivity[7];
activLevel_UlnarDeviation = 0.01 *(10.65*muscleActivity[8] + 20.11*muscleActivity[9] + 25.73*muscleActivity[10]);
activLevel_RadialDeviation = 0.01*( 0.5 * 24.62*muscleActivity[3] + 0.2 * 20.98*muscleActivity[14]);
activLevel_Pronation = 0.05*( 0.2 * 12.2*muscleActivity[1] + 0.2 * 5.56*muscleActivity[2]);
activLevel_Supination = 0.2*(11.22*muscleActivity[4]);
//------------ APPLY EXTENSION FORCES ---
extensorsGroup.applyForces_FingersExtent(activLevel_FingersExtent, activLevel_FingersSmExt, musclesBonesConstraints);
// --------------- FINGERS FLEXORS ------
flexGroup.applyForces_FingersFlex(activLevel_FingersFlex, musclesBonesConstraints);
//------------- Carpi FLEXORS -------------
flexGroup.applyForces_CarpiFlex(activLevel_CarpiFlex, musclesBonesConstraints);
//------------- Carpi EXTENSORS -------------
extensorsGroup.applyForces_CarpiExtent(activLevel_CarpiExtent, musclesBonesConstraints);
//------------ Deviator Ulna --------------
deviatorsGroup.applyForces_UlnarDeviation(activLevel_UlnarDeviation, musclesBonesConstraints);
//------------ Deviator Ulna --------------
deviatorsGroup.applyForces_RadialDeviation(activLevel_RadialDeviation, musclesBonesConstraints);
//------------- Pronation ------------------
pronatorsSupinatorsGroup.applyForces_Pronation(activLevel_Pronation, musclesBonesConstraints);
//------------- Supination ----------------
pronatorsSupinatorsGroup.applyForces_Supination(activLevel_Supination, musclesBonesConstraints);
};
window.onload = init();
// ! Important Close Myo when closing window
window.onbeforeunload = function() {
Myo.socket.close();
return null; //here also can be string, that will be shown to the user
}
</script>
</body>
</html>