-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzig.js
More file actions
1142 lines (962 loc) · 29.6 KB
/
zig.js
File metadata and controls
1142 lines (962 loc) · 29.6 KB
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
function include(file)
{
var script = document.createElement('script');
script.src = file;
script.type = 'text/javascript';
script.defer = true;
document.getElementsByTagName('head').item(0).appendChild(script);
}
include("sylvester.js");
OrientationX = 0;
OrientationY = 1;
OrientationZ = 2;
var Joints = {
Invalid : 0,
Head : 1,
Neck : 2,
Torso : 3,
Waist : 4,
LeftCollar : 5,
LeftShoulder : 6,
LeftElbow : 7,
LeftWrist : 8,
LeftHand : 9,
LeftFingertip : 10,
RightCollar : 11,
RightShoulder : 12,
RightElbow : 13,
RightWrist : 14,
RightHand : 15,
RightFingertip : 16,
LeftHip : 17,
LeftKnee : 18,
LeftAnkle : 19,
LeftFoot : 20,
RightHip : 21,
RightKnee : 22,
RightAnkle : 23,
RightFoot : 24,
};
//-----------------------------------------------------------------------------
// Helper functions
//-----------------------------------------------------------------------------
function ZigAddHandler(target,eventName,handlerName)
{
if (target.attachEvent) {
target.attachEvent("on" + eventName, handlerName);
} else if ( target.addEventListener ) {
target.addEventListener(eventName, handlerName, false);
} else {
target["on" + eventName] = handlerName;
}
}
//-----------------------------------------------------------------------------
// ZigControlList
// A list of controls getting updates. Can be nested o'mercy
//-----------------------------------------------------------------------------
function ZigControlList()
{
this.listeners = [];
this.focusedControl = undefined;
this.isInHandpointSession = false;
this.focusPoint = [0,0,0];
// public
this.AddControl = function(control)
{
if (undefined === control) return;
this.listeners.push(control);
if (this.isInHandpointSession) {
control.onSessionStart(this.focusPoint);
}
}
this.RemoveControl = function(control)
{
if (undefined === control) return;
var removed = this.listeners.splice(this.listeners.indexOf(control), 1);
if (this.isInHandpointSession) {
removed[0].onSessionEnd();
}
}
// this allows nesting control lists
this.onDoUpdate = function(trackedUser) {
this.listeners.forEach(function(control) { control.onDoUpdate(trackedUser); });
}
this.onSessionStart = function(focuspoint) {
this.isInHandpointSession = true;
this.focusPoint = focuspoint;
this.listeners.forEach(function(control) { control.onSessionStart(focuspoint) });
}
this.onSessionUpdate = function(hands) {
this.listeners.forEach(function(control) { control.onSessionUpdate(hands) });
}
this.onSessionEnd = function() {
this.listeners.forEach(function(control) { control.onSessionEnd() });
this.isInHandpointSession = false;
}
this.SetFocusedControl = function(control) {
if (control == this.focusedControl) return;
this.RemoveControl(this.focusedControl);
this.focusedControl = control;
this.AddControl(control);
}
}
//-----------------------------------------------------------------------------
// Tracked user
// A tracked user is created for every user we "see".
//-----------------------------------------------------------------------------
function ZigTrackedUser(userid)
{
this.controls = new ZigControlList();
this.skeleton = [];
this.hands = [];
this.userid = userid;
this.position = [];
this.centerofmass = [];
this.isInHandpointSession = false;
this.tracked = false;
this.UpdateFullbody = function(centerofmass, tracked, skeleton)
{
this.tracked = tracked;
this.centerofmass = centerofmass;
this.position = centerofmass;
this.skeleton = skeleton;
}
this.UpdateHands = function(hands)
{
this.hands = hands;
}
this.NotifyListeners = function()
{
// if we aren't in session, but should be
if (!this.isInHandpointSession && this.hands.length > 0) {
this.isInHandpointSession = true;
var focusPoint = (undefined != this.hands[0].focusposition) ? this.hands[0].focusposition : this.hands[0].position;
this.controls.onSessionStart(focusPoint);
}
// if we are in session, but shouldn't be
if (this.isInHandpointSession && this.hands.length == 0) {
this.controls.onSessionEnd();
this.isInHandpointSession = false;
}
// at this point we know if we are in a session or not,
// and we sent the start/end notifications. all thats
// left is updating the controls if we're in session
if (this.isInHandpointSession) {
this.controls.onSessionUpdate(this.hands);
}
this.controls.onDoUpdate(this);
}
}
// Fader
function Fader(orientation, size)
{
// defaults
if (undefined === size) {
size = 250;
}
// vars
this.size = size;
this.orientation = orientation;
this.value = 0;
this.center = [0,0,0];
this.itemsCount = 1;
this.hysteresis = 0.1;
this.selectedItem = 0;
this.initialValue = 0.5;
this.flip = false;
// events
// TODO: make real events
this.onItemSelected = function(item){};
this.onItemUnselected = function(item){};
this.onValueChange = function(value) {};
// hand point control callbacks
this.onSessionStart = function(sessionStartPosition) {
this.moveTo(sessionStartPosition, this.initialValue);
this.value = this.initialValue;
this.selectedItem = Math.floor(this.itemsCount * this.value);
this.onItemSelected(this.selectedItem);
}
this.onSessionUpdate = function(hands) {
var position = hands[0].position;
var distanceFromCenter = position[this.orientation] - this.center[this.orientation];
var ret = (distanceFromCenter / this.size) + 0.5;
this.value = this.clamp(ret, 0, 1);
if (this.flip) this.value = 1 - this.value;
var newSelected = this.selectedItem;
var minValue = (this.selectedItem * (1 / this.itemsCount)) - this.hysteresis;
var maxValue = (this.selectedItem + 1) * (1 / this.itemsCount) + this.hysteresis;
this.onValueChange(this.value);
if (this.value > maxValue) {
newSelected++;
}
if (this.value < minValue) {
newSelected--;
}
if (newSelected != this.selectedItem) {
this.onItemUnselected(this.selectedItem);
this.selectedItem = newSelected;
this.onItemSelected(newSelected);
}
}
this.onSessionEnd = function() {
this.onItemUnselected(this.selectedItem);
}
this.onDoUpdate = function() {};
this.moveTo = function(position, value) {
if (this.flip) value = 1 - value;
this.center[this.orientation] = position[this.orientation] + ((0.5 - value) * this.size);
}
this.moveToContain = function(position) {
var distanceFromCenter = position[this.orientation] - this.center[this.orientation];
if (distanceFromCenter > this.size / 2) {
this.center[this.orientation] += distanceFromCenter - (this.size / 2);
} else if (distanceFromCenter < this.size / -2) {
this.center[this.orientation] += distanceFromCenter + (this.size / 2);
}
}
// internal functions
this.clamp = function(x, min, max)
{
if (x < min) return min;
if (x > max) return max;
return x;
}
}
function Fader2D(width, height)
{
this.width = width;
this.height = height;
this.topleft = $V([0,0,0]);
this.initialValue = [0.5, 0.75];
this.value = [0,0];
// events
this.onValueChange = function(value) {}
this.onSessionStart = function(focusPoint) {
this.topleft = $V(focusPoint).subtract($V([this.initialValue[0] * width, this.initialValue[1] * height, 0]));
console.log("Top left is at: " + this.topleft.inspect());
}
this.onSessionUpdate = function(hands) {
var position = hands[0].position;
var distanceFromTopleft = $V(position).subtract(this.topleft);
this.value = [this.clamp(distanceFromTopleft.elements[0] / width,0,1), this.clamp(distanceFromTopleft.elements[1] / height,0,1)];
this.onValueChange(this.value);
}
this.onSessionEnd = function() {}
this.onDoUpdate = function() {}
this.clamp = function(x, min, max)
{
if (x < min) return min;
if (x > max) return max;
return x;
}
}
function PushDetector(size)
{
this.isPushed = false;
this.pushProgress = 0;
this.pushTime = 0;
// events
this.onPush = function() {}
this.onRelease = function() {}
this.onClick = function() {}
if (undefined === size) {
size = 150;
}
this.fader = new Fader(OrientationZ, size);
this.fader.flip = true; // positive Z is backwards by default, so flip it
this.fader.initialValue = 0.2;
// callbacks
this.onSessionStart = function(focusPoint) {
this.fader.onSessionStart(focusPoint);
}
this.onSessionUpdate = function(hands) {
var position = hands[0].position;
this.fader.moveToContain(position);
this.fader.onSessionUpdate(hands);
this.pushProgress = this.fader.value;
if (!this.isPushed) {
if (1.0 == this.pushProgress) {
this.isPushed = true;
this.pushTime = (new Date()).getTime();
this.pushPosition = position;
this.onPush();
}
} else {
if (this.pushProgress < 0.5) {
this.isPushed = false;
if (this.isClick()) {
this.onClick();
}
this.onRelease();
}
}
// drift if not pushed
if (!this.isPushed) {
var delta = this.fader.initialValue - this.pushProgress;
this.fader.moveTo(position, this.pushProgress + delta * 0.05);
}
}
this.onSessionEnd = function() {
this.fader.onSessionEnd();
if (this.isPushed) {
this.isPushed = false;
this.onRelease();
}
}
this.onDoUpdate = function() {}
// internal
this.isClick = function()
{
var delta = (new Date()).getTime() - this.pushTime;
return (delta < 1000);
}
}
function SwipeDetector(orientation, size)
{
// vars
this.isSwiped = false;
// events
this.onSwipeMin = function() {};
this.onSwipeMax = function() {};
this.fader = new Fader(orientation, size);
this.fader.initialValue = 0.5;
// callbacks
this.onSessionStart = function(focusPoint) {
this.fader.onSessionStart(focusPoint);
}
this.onSessionUpdate = function(hands) {
var position = hands[0].position;
// TODO: Move fader to contain current hand point
this.fader.onSessionUpdate(hands);
if (!this.isSwiped) {
if (1 == this.fader.value || 0 == this.fader.value) {
this.isSwiped = true;
this.swipeValue = this.fader.value;
if (1 == this.fader.value) {
this.onSwipeMax();
} else {
this.onSwipeMin();
}
}
} else {
if (Math.abs(this.fader.value - this.swipeValue) >= 0.3) {
this.onSwipeRelease();
this.isSwiped = false;
}
}
// TODO: drift if we aren't pushed
}
this.onSessionEnd = function() {
this.fader.onSessionEnd();
if (this.isSwiped) {
this.isSwiped = false;
this.onSwipeRelease();
}
}
this.onDoUpdate = function() {}
}
function HorizontalSwipeDetector(size)
{
if (undefined === size) {
size = 350;
}
this.onSwipeLeft = function() {}
this.onSwipeRight = function() {}
this.onSwipeRelease = function() {}
var me = this;
this.sd = new SwipeDetector(OrientationX, size);
this.sd.onSwipeMin = function() { me.onSwipeLeft(); }
this.sd.onSwipeMax = function() { me.onSwipeRight(); }
this.sd.onSwipeRelease = function() { me.onSwipeRelease(); }
this.onSessionStart = function(focusPoint) { this.sd.onSessionStart(focusPoint); }
this.onSessionEnd = function() { this.sd.onSessionEnd(); }
this.onSessionUpdate = function(hands) { this.sd.onSessionUpdate(hands); }
this.onDoUpdate = function(user) { this.sd.onDoUpdate(user); }
}
function VerticalSwipeDetector(size)
{
if (undefined === size) {
size = 250;
}
this.onSwipeUp = function() {}
this.onSwipeDown = function() {}
this.onSwipeRelease = function() {}
var me = this;
this.sd = new SwipeDetector(OrientationY, size);
this.sd.onSwipeMin = function() { me.onSwipeUp(); }
this.sd.onSwipeMax = function() { me.onSwipeDown(); }
this.sd.onSwipeRelease = function() { me.onSwipeRelease(); }
this.onSessionStart = function(focusPoint) { this.sd.onSessionStart(focusPoint); }
this.onSessionEnd = function() { this.sd.onSessionEnd(); }
this.onSessionUpdate = function(hands) { this.sd.onSessionUpdate(hands); }
this.onDoUpdate = function(user) { this.sd.onDoUpdate(user); }
}
function TopDownUsersRadar(parentElement, userClass)
{
this.parentElement = parentElement;
this.users = [];
this.onNewUser = function(user) {
var el = document.createElement('div');
el.classList.add(userClass);
this.users[user.userid] = el;
this.parentElement.appendChild(el);
}
this.onLostUser = function(user) {
this.parentElement.removeChild(this.users[user.userid]);
delete this.users[user];
}
this.DoUserEngaged = function(user) {
this.users[user.userid].classList.add('active');
}
this.DoUserDisengaged = function(user) {
this.users[user.userid].classList.remove('active');
}
this.onUpdate = function(trackedUsers)
{
for (var userIndex in trackedUsers) {
var currUser = trackedUsers[userIndex];
var el = this.users[currUser.userid];
var pos = currUser.position;
el.style.left = (((pos[0] / 4000) + 0.5) * this.parentElement.offsetWidth - (el.offsetWidth / 2)) + "px";
el.style.top = ((pos[2] / 4000.0) * this.parentElement.offsetHeight - (el.offsetHeight / 2)) + "px";
}
}
}
function SteadyDetector(maxVariance)
{
if (undefined === maxVariance) {
maxVariance = 50;
}
this.frameCount = 15;
this.pointBuffer = [];
this.steady = false;
this.maxVariance = maxVariance;
this.onSteady = [];
this.useJoint = false;
this.joint = Joints.Invalid;
this.onSteady = function() {}
this.onUnsteady = function() {}
this.sumMatrix = function(mat) {
var sum = 0;
elements = mat.elements
for(var i in elements) {
for(var j in elements[i]) {
sum += elements[i][j];
}
}
return sum;
}
// Reference: Oliver K. Smith: Eigenvalues of a symmetric 3 × 3 matrix. Commun. ACM 4(4): 168 (1961)
// find the eigenvalues of a 3x3 symmetric matrix
this.getEigenvalues = function(mat) {
var m = mat.trace() / 3;
var K = mat.subtract( Matrix.I(3).x(m)); // K = mat - I*tr(mat)
var q = K.determinant() / 2;
var tempForm = K.x(K);
var p = sumMatrix(tempForm) / 6;
// NB in Smith's paper he uses phi = (1/3)*arctan(sqrt(p*p*p - q*q)/q), which is equivalent to below:
var phi = (1/3)*Math.acos(q/Math.sqrt(p*p*p));
if (Math.abs(q) >= Math.abs(Math.sqrt(p*p*p))) {
phi = 0;
}
if (phi < 0) {
phi = phi + Math.PI/3;
}
var eig1 = m + 2*Math.sqrt(p)*Math.cos(phi);
var eig2 = m - Math.sqrt(p)*(Math.cos(phi) + Math.sqrt(3)*Math.sin(phi));
var eig3 = m - Math.sqrt(p)*(Math.cos(phi) - Math.sqrt(3)*Math.sin(phi));
return [eig1, eig2, eig3];
}
this.getCofactorMatrix = function(mat) {
var dims = mat.dimensions();
var xSize = dims.cols;
var ySize = dims.rows;
var output = mat.map(function(x, i, j) { return mat.minor(i+1,j+1,xSize-1, ySize-1).determinant(); } );
return output;
}
this.getStddevs = function(vectors) {
if (vectors.length == 0) { return []; }
var sum = Vector.Zero(vectors[0].dimensions());
for(k in vectors) {
sum = sum.add(vectors[k]);
}
var avg = sum.multiply(1/(vectors.length));
var covarianceMatrix = Matrix.Zero(avg.dimensions(), avg.dimensions());
for(k in vectors) {
var temp = vectors[k].subtract(avg);
covarianceMatrix = covarianceMatrix.map(function(x, i, j) { return x + temp.elements[i-1]*temp.elements[j-1]; } );
}
var values = getEigenvalues(covarianceMatrix);
for (key in values) {
values[key] = Math.sqrt(Math.abs(values[key]));
}
return values;
}
this.clear = function() {
this.pointBuffer = [];
this.steady = false;
}
this.addValue = function(position) {
this.pointBuffer.push($V(position));
while (this.pointBuffer.length > this.frameCount) {
this.pointBuffer.shift();
}
pb = this.pointBuffer;
var steadyThisFrame = true;
var stdDevs = getStddevs(this.pointBuffer);
for(var k in stdDevs) {
steadyThisFrame &= stdDevs[k] < 50;
}
if (steadyThisFrame && (!this.steady)) {
this.steady = true;
this.onSteady();
} else if (!steadyThisFrame && this.steady) {
this.steady = false;
this.onUnsteady();
}
}
this.onSessionStart = function(focusPoint) {
if (!this.useJoint) {
this.clear();
}
}
this.onSessionUpdate = function(hands) {
if (!this.useJoint) {
this.addValue(hands[0].position);
}
}
this.onSessionEnd = function() {}
this.onDoUpdate = function(trackedUser) {
if (this.useJoint && (trackedUser.skeleton[this.joint] !== undefined)) {
this.addValue(trackedUser.skeleton[this.joint].position);
}
}
}
function ZigHandRaiseDetector()
{
this.steadyLeft = new ZigSteadyDetector();
this.steadyLeft.useJoint = true;
this.steadyLeft.joint = Joints.LeftHand;
this.steadyRight = new ZigSteadyDetector();
this.steadyRight.useJoint = true;
this.steadyRight.joint = Joints.RightHand;
function makeSteadyFunction(detector, joint)
{
return function() {
var user = detector.user;
if (user.skeleton[joint].position[1] >= user.skeleton[Joints.Head].position[1]) {
detector.onHandRaise();
}
}
}
this.steadyLeft.onSteady = makeSteadyFunction(this, Joints.LeftHand);
this.steadyRight.onSteady = makeSteadyFunction(this, Joints.RightHand);
this.onHandRaise = function() { console.log("Hand raised"); }
this.onDoUpdate = function(trackedUser) {
this.user = trackedUser;
this.steadyLeft.onDoUpdate(trackedUser);
this.steadyRight.onDoUpdate(trackedUser);
}
this.onSessionStart = function() {}
this.onSessionUpdate = function() {}
this.onSessionEnd = function() {}
}
//-----------------------------------------------------------------------------
// "Engagement" managers
//-----------------------------------------------------------------------------
function ZigEngageSingleUser(userid)
{
// the session manager can be inited with a valid userid
// (for persisting state between pages, etc.)
if (undefined == userid) {
userid = 0;
}
this.PrimaryUser = new ZigControlList();
this.userid = userid;
this.onNewUser = function(trackeduser) {
// not tracking anyone yet?
if (0 == this.userid) {
// start now
this.userid = trackeduser.userid;
trackeduser.controls.AddControl(this.PrimaryUser);
}
}
this.onLostUser = function(trackeduser) {
// lost the engaged user?
if (trackeduser.userid == this.userid) {
// bummer
this.userid = 0;
}
}
this.onUpdate = function() {}
}
// NOTE: This needs to be rewritten; user control that waits till the user is in alone in a "region"
// Init 2 of those per user, one for left and one for right
function ZigEngageSideBySide(usertracker, leftuserid, rightuserid)
{
this.usertracker = usertracker;
this.LeftUser = new ZigControlList();
this.RightUser = new ZigControlList();
this.leftuserid = (leftuserid == undefined ? 0 : leftuserid);
this.rightuserid = (rightuserid == undefined ? 0 : rightuserid);
this.onUsersEngaged = function() {}
this.onUsersMissing = function() {}
this.leftUserIdealPosition = [-1000,1000,2000];
this.rightUserIdealPosition = [1000,1000,2000];
this.getUserClosestTo = function(trackedusers, position)
{
var minDistance = -1;
var ret = 0;
for (userid in trackedusers) {
currDistance = $V(trackedusers[userid].centerofmass).distanceFrom($V(position));
if (-1 == minDistance || currDistance < minDistance) {
minDistance = currDistance;
ret = userid;
}
}
}
this.onUpdate = function() {
// if not all users are engaged
var allusersengaged = this.allUsersEngaged();
var trackedusers = this.usertracker.trackedUsers;
if (!this.allUsersEngaged()) {
// check distance of each user from the "ideal" positions
var closestLeft = this.getUserClosestTo(trackedusers, this.leftUserIdealPosition);
var closestRight = this.getUserClosestTo(trackedusers, this.rightUserIdealPosition);
// if the perfect user for both positions is the same
if (closestLeft == closestRight) {
// keep looking for another one
var dLeft = $V(trackedusers[closestLeft].centerofmass).distanceFrom($V(this.leftUserIdealPosition));
var dRight = $V(trackedusers[closestRight].centerofmass).distanceFrom($V(this.rightUserIdealPosition));
if (dLeft < dRight) {
closestRight = 0;
} else {
closestLeft = 0;
}
}
}
// should we fire the UsersEngaged event?
if (!allusersengaged && this.allUsersEngaged()) {
this.userTracker
this.onUsersEngaged();
}
}
this.onLostUser = function(trackeduser) {
// is the lost user one of our engaged users?
var allusersengaged = this.allUsersEngaged();
if (trackeduser.userid == this.leftuserid) {
this.leftuserid = 0;
}
if (trackeduser.userid == this.rightuserid) {
this.rightuserid = 0;
}
// should we fire the UsersMissing event?
if (allusersengaged && !this.allUsersEngaged()) {
this.onUsersMissing();
}
}
this.allUsersEngaged = function() {
return (this.leftuserid != 0 && this.rightuserid != 0);
}
}
function ZigEngageSingleSession(usertracker, userid)
{
this.controls = new ZigControlList();
// the session manager can be inited with a valid userid
// (for persisting state between pages, etc.)
if (undefined === userid) {
userid = 0;
} else {
usertracker.trackedUsers[userid].controls.AddControl(this.Controls);
}
this.usertracker = usertracker;
this.userid = userid;
// public events
this.onUserEngaged = function(user) {}
this.onUserDisengaged = function(user) {}
this.onNewUser = function(trackeduser) {
// create a hand point control to do our "work" for us
var WaitForSession = function(parent, user) {
this.onSessionStart = function(focuspoint) {
// no active user
if (parent.userid == 0) {
// now we do
parent.userid = user.userid;
user.engaged = true;
user.controls.AddControl(parent.controls);
parent.onUserEngaged(user);
}
}
this.onSessionEnd = function() {
// active user was us
if (parent.userid == user.userid) {
// not anymore
parent.userid = 0;
user.engaged = false;
user.controls.RemoveControl(parent.controls);
parent.onUserDisengaged(user);
}
}
this.onSessionUpdate = function() {}
this.onDoUpdate = function() { }
}
trackeduser.controls.AddControl(new WaitForSession(this, trackeduser));
}
this.onLostUser = function(trackeduser) {
// lost the engaged user?
if (trackeduser.userid == this.userid) {
// bummer
this.userid = 0;
}
}
this.onUpdate = function() {}
this.Reset = function() {
if (this.userid != 0) {
this.userTracker.trackedUsers[this.userid].controls.RemoveControl(this.Controls);
this.userid = 0;
}
}
}
//-----------------------------------------------------------------------------
// Zig object
//-----------------------------------------------------------------------------
var Zig = function() {
// raw data from last frame, directly from acquisition layer
var rawUsers = [];
var rawHands = [];
// mapping of userid's to TrackedUser's
var trackedUsers = [];
// mapping of handid's to userid's
var trackedHands = [];
// do we allow hand point sessions from hand points
// with no associated userid?
var allowHandsForUntrackedUsers = true;
var listeners = [];
// TODO: make real events (listeners for now)
function onNewUser(trackedUser) {
listeners.forEach(function(listener) { listener.onNewUser(trackedUser) });
}
function onLostUser(trackedUser) {
listeners.forEach(function(listener) { listener.onLostUser(trackedUser) });
}
function onUpdate(userTracker) {
listeners.forEach(function(listener) { listener.onUpdate(userTracker) });
}
function init(pluginElement) {
plugin = pluginElement;
ZigAddHandler(pluginElement, "NewFrame", function () { return function(data) { var obj = JSON.parse(data); DoUpdate(obj.users, obj.hands); }}());
log("Zig: inited");
}
function Pause() { plugin.firingEvents = false; }
function Resume() { plugin.firingEvents = true; }
function ProcessNewUser(userid) {
log("Zig: new user " + userid);
// if we aren't tracking this user yet, start now
// (its possible that we are already tracking the user
// from a previous hand point etc.)
if (!isUserTracked(userid)) {
trackedUsers[userid] = new ZigTrackedUser(userid);
onNewUser(trackedUsers[userid]);
}
}
function ProcessLostUser(userid) {
log("Zig: lost user " + userid);
if (isUserTracked(userid)) {
lost = trackedUsers[userid];
delete trackedUsers[userid];
onLostUser(lost);
}
}
function ProcessNewHand(handid, userid) {
log("Zig: new hand " + handid);
// no user id
if (userid <= 0) {
log("Zig: new hand belongs to non-tracked user");
// get out if we dont allow such hands
if (!allowHandsForUntrackedUsers) return;
// otherwise allocate a "fake" user id and use it
userid = getFakeUserId();
}
// add the user if neccessary
if (!isUserTracked(userid)) {
ProcessNewUser(userid);
}
// associate this hand with the user
log("Zig: new hand associated with user " + userid);
trackedHands[handid] = userid;
}
function ProcessLostHand(handid) {
log("Zig: lost hand");
// remove the hand->user association
var userid = trackedHands[handid];
delete trackedHands[handid];
// if this user is "fake" (created for this specific
// hand point) then get rid of it
if (!isRealUser(userid)) {
log("Zig: lost hand was with fake user, removing");
ProcessLostUser(userid);
}
}
function UpdateUsers (users) {
// get rid of old users
for (userid in trackedUsers) {
var curruser = getItemById(users, userid);
if (undefined === curruser && isRealUser(userid)) {
ProcessLostUser(userid);
}
}
// add new users
for (user in users) {
if (!isUserTracked(users[user].id)) {
ProcessNewUser(users[user].id);
}
}
// save raw data before updating the fullbody controls
rawUsers = users;
// update stuff
for (user in users) {
var currjoints = users[user].joints;
var newjoints = [];
for (var i=0; i<currjoints.length; i++) {
newjoints[currjoints[i].id] = currjoints[i];
}
trackedUsers[users[user].id].UpdateFullbody(users[user].centerofmass, users[user].tracked, newjoints);
}
}
function UpdateHands(hands) {
// get rid of old hands
for (handid in trackedHands) {
var currhand = getItemById(hands, handid);
if (undefined === currhand) {
ProcessLostHand(handid);
}
}
// add new hands