-
Notifications
You must be signed in to change notification settings - Fork 12
/
Handy.js
1220 lines (867 loc) · 30.7 KB
/
Handy.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
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
// Copyright © 2020–2021 Stewart Smith. See LICENSE for details.
// 👉 𝖨𝗇𝗍𝗋𝗈𝖽𝗎𝖼𝗂𝗇𝗀 𝗛𝗮𝗻𝗱𝘆.𝗷𝘀 👈
//
// Want to add hand pose recognition to your WebXR project?
// Handy makes defining and using custom hand poses a snap!
// Why use hand-held contollers when you can use your bare
// hands? 👋 Built on Three.js and tested on the Oculus Quest,
// Handy recognizes over 100 hand poses right out of the box --
// including the American Sign Language (ASL) alphabet.
// And yes, full hand gesture recognition is on the roadmap 👍
//
// Requires OculusBrowser/11.1.0.1.64 or later.
import { poses as posesLeft } from './Handy-poses-left.js'
import { poses as posesRight } from './Handy-poses-right.js'
import {
Vector3,
MathUtils,
Object3D,
Mesh,
BoxBufferGeometry,
MeshBasicMaterial,
} from 'three';
const Handy = {
// What revision of Handy is this?
// I don’t have strict critera for requiring a version bump
// but when something biggish changes I’ll bump this number.
REVISION: 5,
// The following list of joint names mirrors the constants list
// of window.XRHand. So why duplicate that?
// Because right now XRHand only exists in the Oculus browser
// and we want to be able to reason around XRHand stuff --
// perhaps even playback recorded hand motions --
// right here on your desktop machine where XRHand does not exist.
// Here’s the proposed spec for joint indexes:
// https://github.com/immersive-web/webxr-hand-input/blob/master/explainer.md#appendix-proposed-idl
// We use this Array to look up joint names by index value.
// What’s at joint index #7?
// Just ask for Handy.jointNames[ 7 ]
// and you’ll get the value 'index-finger-phalanx-intermediate'.
// We also use this Array to append constants directly
// onto the Handy{} object like so:
// Handy[ 'index-finger-phalanx-intermediate' ] === 7.
// This exactly mirrors XRHand:
// Handy[ 'index-finger-phalanx-intermediate' ] === XRHand[ 'index-finger-phalanx-intermediate' ].
jointNames: [
'wrist', // 0
'thumb-metacarpal', // 1
'thumb-phalanx-proximal', // 2
'thumb-phalanx-distal', // 3
'thumb-tip', // 4
'index-finger-metacarpal', // 5
'index-finger-phalanx-proximal', // 6
'index-finger-phalanx-intermediate', // 7
'index-finger-phalanx-distal', // 8
'index-finger-tip', // 9
'middle-finger-metacarpal', // 10
'middle-finger-phalanx-proximal', // 11
'middle-finger-phalanx-intermediate',// 12
'middle-finger-phalanx-distal', // 13
'middle-finger-tip', // 14
'ring-finger-metacarpal', // 15
'ring-finger-phalanx-proximal', // 16
'ring-finger-phalanx-intermediate', // 17
'ring-finger-phalanx-distal', // 18
'ring-finger-tip', // 19
'pinky-finger-metacarpal', // 20
'pinky-finger-phalanx-proximal', // 21
'pinky-finger-phalanx-intermediate', // 22
'pinky-finger-phalanx-distal', // 23
'pinky-finger-tip' // 24
],
// These are not part of the XRHand spec
// but come in handy -- no pun intended.
digitNames: [
'thumb',
'index',
'middle',
'ring',
'pinky'
],
digitTipNames: [
'thumb-tip', // 4
'index-finger-tip', // 9
'middle-finger-tip',// 14
'ring-finger-tip', // 19
'pinky-finger-tip' // 24
],
isDigitTipIndex: function( i ){
return (
i > 0 &&
i < 25 &&
!(( i + 1 ) % 5 )
)
},
fingerNames: [
'index',
'middle',
'ring',
'pinky'
],
isFingerTipIndex: function( i ){
return (
i > 4 &&
i < 25 &&
!(( i + 1 ) % 5 )
)
},
// We’ll repeatedly use this
// so let’s just create it once
// and reference it from here on.
VECTOR3_ZERO: new Vector3(),
// Here’s the data goods;
// Poses for the left hand to match
// and poses for the right hand to match.
poses: {
left: posesLeft,
right: posesRight
},
// Maximum duration in milliseconds
// that we’ll allow per update() loop
// PER HAND for a pose search.
// Remember, we want to get out of the way
// as quickly as possible!
searchLoopDurationLimit: 6,
// JavaScript doesn’t need classes.
// (You may want to read that again.)
// Here we’re going to REFERENCE (not copy)
// functionality from one object onto another,
// eg. the ‘hand’ that we will makeHandy().
// To make this process more efficient
// when creating more than one hand
// we’ll define the methods just once,
// store them in this ‘protos’ object,
// then add REFERENCES to them on “handy” objects.
// eg. hands.left.reportFingers === hands.right.reportFingers
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Comparing_objects
protos: {},
// We’re going to keep a reference
// to every object that we “make handy”
// so we can iterate through them later
// and also detect interactions BETWEEN them.
// (That is coming soon!)
hands: [],
// Now we can update all individual hands
// just by calling Handy.update()!
update: function( callbackForAllHands ){
this.hands.forEach( function( hand ){
hand.update( callbackForAllHands )
})
},
// Handy.makeHandy() expects an instance of THREE.Object3D,
// or anything that inherits from THREE.Object3D,
// and then injects additional functionality into that object.
// The intended use is with THREE’s XRHand model like so:
//
// hand0 = renderer.xr.getHand( 0 )
// Handy.makeHandy( hand0 )// This is the magic line.
//
// Now ‘hand0’ is handy! It’s that easy!
// Just remember to call Handy.update() within your update loop!
// Handy.update() will in turn update all handified objects.
makeHandy: function( obj ){
obj.name = 'hand'
// We need to find the THREE camera used for this scene
// in order to have our data display frames
// always lookAt() the camera.
// In the future we might need this to be more robust
// or just pass in the intended camera via update().
// NOTE. THIS IS VEY BRITTLE!
// THANKFULLY THIS IS ONLY FOR RECORDING,
// AND NOT FOR REGULAR USE.
const scene = obj.parent
// NOTE. THIS DOES NOT SEEM TO FUNCTION AS EXPECTED
// BECAUSE OF THE XR CAMERA RIG. COME BACK AND INVESTIGATE.
obj.camera = scene.children.find( function( child ){
return child.type === 'PerspectiveCamera'
})
// Glob on the methods. No classes required :)
// Note that these will be added as REFERENCES
// rather than clones. Very little memory overhead!
Object.entries( Handy.protos )
.forEach( function( entry ){
if( obj[ entry[ 0 ]] === undefined ) obj[ entry[ 0 ]] = entry[ 1 ]
})
// Let’s keep a list of all handified objects
// which down the road will allow us to detect interactions
// like claps, time freeze, picture framing, etc.
Handy.hands.push( obj )
}
}
// This is where we make good on our promise above
// to append constants directly onto the Handy{} object like so:
// Handy.INDEX_PHALANX_INTERMEDIATE === 7.
// This exactly mirrors XRHand:
// Handy.INDEX_PHALANX_INTERMEDIATE === XRHand.INDEX_PHALANX_INTERMEDIATE.
Handy.jointNames.forEach( function( name, i ){
Handy[ name ] = i
})
// Handy.hands is an Array of unlabeled hands
// as we do not immediately know the handedness of a hand.
// In your own update functions you may wish to do this:
// var left = Handy.hands.getLeft()
// var right = Handy.hands.getRight()
Object.assign( Handy.hands, {
getLeft: function(){
return this.find( function( hand ){
return hand.handedness === 'left'
})
},
getRight: function(){
return this.find( function( hand ){
return hand.handedness === 'right'
})
}
})
////////////////
// //
// Protos //
// //
////////////////
// Let’s define all the methods we want to
// glob on to any object that we “make handy”.
// We’ll store them in Handy’s ‘protos’ object.
// https://en.wikipedia.org/wiki/Prototype-based_programming
Object.assign( Handy.protos, {
// Traverse down this THREE.Group to find
// a child with an ‘xrInputSource’ property,
// which should have a ‘handedness’ property.
// This will both assign that value to this Handy object
// (if such a value is found)
// and return the current ‘handedness’ of this Handy object.
// NOTE: Is there a more efficient way to do this??
checkHandedness: function(){
const hand = this
this.traverse( function( obj ){
if( obj.xrInputSource !== undefined &&
obj.xrInputSource.handedness !== undefined ){
hand.handedness = obj.xrInputSource.handedness
hand.name = 'hand '+ hand.handedness
}
})
return this.handedness
},
// Find the distance (in CENTIMETERS!) between two joints
// by using joint name Strings.
// You can use the constant style ‘INDEX_PHALANX_INTERMEDIATE’
// or a more friendly lowercase-and-spaces style:
// “index phalanx intermediate”. Both are valid styles here.
// This makes writing the pose detection logic super legible.
// Here’s some pinch detection logic:
//
// return this.distanceBetweenJoints(
//
// 'index phalanx tip',
// 'thumb phalanx tip'
//
// ) < 3
//
// Easy, right?! Now you can write your own! :)
distanceBetweenJoints: function( jointNameA, jointNameB ){
if( this.joints.length === 0 ) return NaN
const
hand = this,
[ jointA, jointB ] = [ jointNameA, jointNameB ]
.map( function( name ){
return hand.joints[
// Handy[ name.toUpperCase().replace( /\s+/g, '_' )]
name.toLowerCase().replace( /\s+/g, '-' )
]
})
if( jointA.position &&
jointB.position &&
( !jointA.position.equals( jointB.position ))){
return jointA.position.distanceTo( jointB.position ) * 100
}
else return NaN
},
// Find the angle (in DEGREES!) from a finger’s base to its tip.
// Here’s how to check if your index finger is extended:
//
// return this.digitAngle( 'index' ) < 20
//
// Not bad, eh?
digitAngle: function( fingerName ){
fingerName = fingerName.toLowerCase()
const
fingerTip = fingerName === 'thumb' ?
this.joints[ 'thumb-tip' ] :
this.joints[ fingerName +'-finger-tip' ],
fingerProximal = fingerName === 'thumb' ?
this.joints[ 'thumb-phalanx-proximal' ] :
this.joints[ fingerName +'-finger-phalanx-proximal' ]
if( fingerTip &&
fingerProximal &&
fingerTip.quaternion &&
fingerProximal.quaternion ){
return MathUtils.radToDeg(
fingerProximal.quaternion.angleTo( fingerTip.quaternion )
)
}
return NaN
},
// Some useful helper functions that
// check the angle from digit base to digit tip
// to determine if that digit is extended
// or contracted.
digitIsExtended: function( digitName ){
return this.digitAngle( digitName ) < 45
},
digitIsContracted: function( digitName ){
return this.digitAngle( digitName ) > 110
},
// Useful for assessing
// what values you may want to use
// in your detection functions.
reportDigits: function(){
const hand = this
Handy.digitNames
.forEach( function( digitName ){
const
proximalName = digitName === 'thumb' ?
'thumb-phalanx-proximal' :
digitName +'-finger-phalanx-proximal',
tipName = digitName === 'thumb' ?
'thumb-tip' :
digitName + '-finger-tip',
distance = hand.distanceBetweenJoints(
proximalName,
tipName
),
digitAngle = hand.digitAngle( digitName )
console.log(
hand.handedness,
digitName +'.',
'angle:',
Math.round( digitAngle )+'˚',
'distance:',
( Math.round( distance * 10 ) / 10 ) +'cm',
hand.digitIsExtended( digitName ) ?
'is extended' :
'is contracted'
)
})
},
////////////////
// //
// Record //
// //
////////////////
// Take a snapshot of this hand’s pose.
readLivePoseData: function(){
const
hand = this,
wrist = hand.joints[ 'wrist' ],
jointPositions = [],
digitTipPositions = [],
// Take a position in global space,
// and make it relative to the wrist joint position
// also taking into account the wrist’s rotation.
// (So we cannot simply subtract position vectors here!
// We must multiply the full transform matrices!)
// Also, let’s round these positions to the nearest
// millimeter to make things tidier to look at
// and save string space when stored as JSON data.
preparePosition = function( joint ){
const
jointMatrix = joint.matrix
.clone()
.premultiply(
// new THREE.Matrix4().copy( wrist.matrixWorld.invert() )
wrist.matrixWorld.clone().invert()
)
// Extract the X, Y, Z positions from the resulting matrix
// and return this as a flat Array
// with distances rounded to the nearest millimeter.
return [
Math.round( jointMatrix.elements[ 12 ] * 1000 ),
Math.round( jointMatrix.elements[ 13 ] * 1000 ),
Math.round( jointMatrix.elements[ 14 ] * 1000 )
]
},
// Store head (camera) position relative to the wrist.
// In the future we’ll use this to identify hand gestures
// that relate to the position of the head / body.
// NOTE: Camera position is unreliable because of XR camera rig.
// Need to come back and investigate alternatives.
headPosition =
wrist !== undefined && !wrist.position.equals( Handy.VECTOR3_ZERO )
? preparePosition( hand.camera )
: null,
headRotation =
headPosition === null
? null
: hand.camera.quaternion.toArray()
// Store the positions of each joint relative to the wrist.
// Note that if a position is not “ready”
// then that entry in the Array will be undefined.
// This is important during pose detection:
// Undefined elements will NOT accrue “distance”, ie.
// If the pinky finger positions don’t matter to a particular
// hand pose, you can just delete those entries!
Object.values( hand.joints )
.forEach( function( joint, i ){
// console.log( i, 'joint', joint )
if( joint !== undefined &&
joint.position !== undefined &&
joint.position.equals( Handy.VECTOR3_ZERO ) === false ){
const preparedPosition = preparePosition( joint )
jointPositions[ i ] = preparedPosition
if( Handy.isDigitTipIndex( i )){
digitTipPositions.push( preparedPosition )
}
}
})
// for( let i = 0; i < hand.joints.length; i ++ ){
// const joint = hand.joints[ i ]
// if( joint !== undefined &&
// joint.position !== undefined &&
// joint.position.equals( Handy.VECTOR3_ZERO ) === false ){
// const preparedPosition = preparePosition( joint )
// jointPositions[ i ] = preparedPosition
// if( Handy.isDigitTipIndex( i )){
// digitTipPositions.push( preparedPosition )
// }
// }
// }
// Package it up and send it off.
return {
headPosition,
headRotation,
jointPositions,
digitTipPositions
}
},
// Grab a snapshot of the live hand pose,
// output its data to the JavaScript console
// (so you can copy and paste it into your poses file),
// and also add it to the poses list
// so you can query for it immediately :)
recordLivePose: function( name, showIt ){
const
hand = this,
handedness = hand.checkHandedness(),
pose = Object.assign(
{
names: [ name ],
handedness,
handyRevision: Handy.REVISION,
time: Date.now()
},
hand.readLivePoseData()
)
console.log( '\n\nPOSE DEFINITION\n\n'+ JSON.stringify( pose ) +',\n\n\n' )
Handy.poses[ handedness ].push( pose )
if( showIt ) hand.showPose( pose, hand.joints[ 0 ].matrixWorld )
return pose
},
// Did your pose record correctly just now?
// This is a quick and dirty way to see
// (within XR!) if it’s roughly correct.
showPose: function( pose, matrix ){
const
hand = this,
handRoot = new Object3D(),
size = 0.02
pose.jointPositions
.forEach( function( position ){
const box = new Mesh(
new BoxBufferGeometry( size, size, size ),
new MeshBasicMaterial()
)
box.position.fromArray( position ).multiplyScalar( 0.001 )
if( matrix !== undefined ){
box.updateMatrix()
box.matrix.multiply( matrix )
}
else {
box.position.y += 1
}
handRoot.add( box )
})
handRoot.position.copy( hand.position )
hand.camera.parent.add( handRoot )
},
// We can also show previously recorded poses.
showPoseByName: function( poseName, matrix ){
const
hand = this,
pose = Handy.poses[ hand.handedness ]
.find( function( pose ){
return pose.names.includes( poseName )
})
if( pose ) hand.showPose( pose, matrix )
},
////////////////
// //
// Search //
// //
////////////////
// Upon casually discussing Handy with a good friend of mine,
// Robert Gerard Pietrusko (http://warning-office.org),
// he suggessted I try recording hand poses and measuring the
// Euclidean distance between them.
// https://en.wikipedia.org/wiki/K-means_clustering
// This turned out to be very efficient! Sort of like Word2Vec,
// but for hands. https://en.wikipedia.org/wiki/Word2vec
//
// Question is, do we try Cosine Distance in the future?
// https://cmry.github.io/notes/euclidean-v-cosine
livePoseData: [],
searchLoopBeganAt: null,
searchLoopsCounter: 0,
searchLoopsCounterMax: 0,
searchPoseIndex: 0,
searchResultsBuffer: [],
searchResults: [],
searchResultsHistory: [],// For future use. (Will add gesture recognition.)
searchMethod: 'jointPositions',
lastSearchResult: { name: 'null' },
search: function(){
const
hand = this,
handedness = hand.checkHandedness(),
poses = Handy.poses[ handedness ],
method = hand.searchMethod
// Is our handedness undefined?
// Do we have zero poses to detect?
// If so, bail immediately!
if( poses === undefined || poses.length === 0 ) return
// We’re going to do some serious “Array clutching” here.
// That means we may NOT finish looping through the Array
// before we “run out of time.” Why do this? Because if we’re
// running at 72fps or 90fps, etc. and we really only need
// to do a full poses search a few times per second,
// then we have render loops to spare and we ought to get
// out of the way as quickly as possible so that YOU can
// use that render loop time for yourself :)
// If you want more performance than this, then it’s time
// for Web Workers. But for now this seems to do the trick.
// https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API
hand.searchLoopBeganAt = window.performance.now()
for( let
i = hand.searchPoseIndex;
i < poses.length;
i ++
){
// If we’re just beginning a new search
// we need to reset our results buffer
// and ask for new live hand pose data.
if( i === 0 ){
hand.searchLoopsCounter = 0
hand.searchResultsBuffer = []
hand.livePoseData = hand.readLivePoseData()
// If there’s no joint position data
// or if the wrist position of this hand is EXACTLY zero
// (in which case it’s likely ALL joint positions are zero)
// then this live data is useless. (So bail!)
if( hand.livePoseData.jointPositions.length === 0 ||
(
hand.livePoseData.jointPositions[ 0 ][ 0 ] === 0 &&
hand.livePoseData.jointPositions[ 0 ][ 1 ] === 0 &&
hand.livePoseData.jointPositions[ 0 ][ 2 ] === 0
)){
return
}
// These flags assert that we are
// NOT taking the square root of each distance.
// As this might change in the future
// I wanted a way for you to query / write logic
// around that.
hand.searchResultsBuffer.distancesAreSquared = true
hand.searchResultsBuffer.distancesAreRooted = false
}
// Go about our normal business.
// eg, evaluate the distance between this hand pose
// and the current-ish state of our real hand.
const pose = poses[ i ]
// Currently we have two methods for detecting poses.
// (Down from FOUR in a previous iteration! Sadly,
// the angles between wrist quaternion and digit tip
// weren’t sufficient once we added all of ASL.)
// We may eventually remove this digitTipPositions method
// as [all] jointPositions is obviously more accurate
// and seems speedy enough.
if( method === 'digitTipPositions' ){
hand.searchResultsBuffer.push({
pose,
distance: pose.digitTipPositions
.reduce( function( distance, digitTipPosition, i ){
if( digitTipPosition.length !== undefined &&
hand.livePoseData.digitTipPositions[ i ] !== undefined &&
hand.livePoseData.digitTipPositions[ i ].length > 0 ){
// The “correct” way to do this is to take the square root
// of this sum. But find a square root is inherently slow.
// Thankfully we can do just as well by NOT taking the root.
// I leave it here (commented out) for your edification ;)
distance += //Math.sqrt(
Math.pow( digitTipPosition[ 0 ] - hand.livePoseData.digitTipPositions[ i ][ 0 ], 2 ) +
Math.pow( digitTipPosition[ 1 ] - hand.livePoseData.digitTipPositions[ i ][ 1 ], 2 ) +
Math.pow( digitTipPosition[ 2 ] - hand.livePoseData.digitTipPositions[ i ][ 2 ], 2 )
//)
}
return distance
}, 0 )
})
}
else if( method === 'jointPositions' ){
hand.searchResultsBuffer.push({
pose,
distance: pose.jointPositions
.reduce( function( distance, jointPosition, i ){
if( jointPosition.length !== undefined &&
hand.livePoseData.jointPositions[ i ] !== undefined &&
hand.livePoseData.jointPositions[ i ].length > 0 ){
// The “correct” way to do this is to take the square root
// of this sum. But find a square root is inherently slow.
// Thankfully we can do just as well by NOT taking the root.
// I leave it here (commented out) for your edification ;)
distance += //Math.sqrt(
Math.pow( jointPosition[ 0 ] - hand.livePoseData.jointPositions[ i ][ 0 ], 2 ) +
Math.pow( jointPosition[ 1 ] - hand.livePoseData.jointPositions[ i ][ 1 ], 2 ) +
Math.pow( jointPosition[ 2 ] - hand.livePoseData.jointPositions[ i ][ 2 ], 2 )
//)
}
return distance
}, 0 )
})
}
// Let’s keep track of how many loops it’s taking
// to finish searching through our whole poses library;
// accessible with something like:
// Handy.hands.getLeft().searchLoopsCounterMax
hand.searchLoopsCounter ++
hand.searchLoopsCounterMax = Math.max(
hand.searchLoopsCounterMax,
hand.searchLoopsCounter
)
// Are we done? (If so, shut it down.)
if( i === poses.length - 1 ){
hand.searchResults = hand.searchResultsBuffer
.sort( function( a, b ){
return a.distance - b.distance
})
const searchResult = hand.searchResults[ 0 ]
// Does this search result differ from the previous one?
if( hand.lastSearchResult.pose !== searchResult.pose ){
if( hand.lastSearchResult && hand.lastSearchResult.pose ){
// Fire custom events.
// We need to fire events for each name
// that is associated with this pose.
// Why would there be multiple names??
// For example, “ASL_2” is the same as “Peace”.
// Someone unfamiliar with American Sign Language
// and only concerned with recognizing “peace”
// ought to have that convenience.
// (And the other way ’round as well!)
hand.lastSearchResult.pose.names
.forEach( function( poseName ){
hand.dispatchEvent({
type: poseName +' pose ended',
hand,
pose: hand.lastSearchResult.pose,
// Open question here:
// Should this “distance” property be from this pose’s
// previous top-result status (as it is currently)
// or should it be from its new not-top-result status?
distance: hand.lastSearchResult.distance,
message: hand.handedness.toUpperCase() +
' hand “'+ poseName +'” pose ended'+
' at a Euclidean distance of '+ hand.lastSearchResult.distance +'mm.'
})
})
// Should you need it,
// here’s an easy way to get a “from / to” alert.
// NOTE: Do we need to include distances in here too?
hand.dispatchEvent({
type: 'pose changed',
hand,
resultWas: hand.lastSearchResult,
resultIs: searchResult,
message: hand.handedness.toUpperCase() +
' hand pose changed from '+
JSON.stringify( hand.lastSearchResult.pose.names ) +
' to '+
JSON.stringify( searchResult.pose.names ) +'.'
})
}
searchResult.pose.names
.forEach( function( poseName ){
hand.dispatchEvent({
type: poseName +' pose began',
hand,
pose: searchResult.pose,
distance: searchResult.distance,
message: hand.handedness.toUpperCase() +
' hand “'+ poseName +'” pose began'+
' at a Euclidean distance of '+ searchResult.distance +'mm.'
})
})
// We’re ready to make it final.
// Replace the prior searh result
// with the current search result.
hand.lastSearchResult = searchResult
}
else {
// console.log( 'Same hand pose as last time' )
}
// Get things ready for next search.
hand.searchIndex = 0
hand.searchResultsBuffer = []
// Bail both from this local “for” loop
// and from this entire function.
return searchResult
}
// If we’re not done with our search,
// check if this search is taking too long per update() loop.
else {
// If we’re taking too long