-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.ts
executable file
·294 lines (236 loc) · 8.49 KB
/
main.ts
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
const controllerOptions = {};
class Finger {
extended: boolean;
joints: boolean[] = [];
}
class Hand {
orientation: { below: boolean } = {below: null};
palm: { facing: { front: boolean } } = {facing: {front: null}};
fingers: Finger[] = [new Finger(), new Finger(), new Finger()];
semipinched: boolean;
indexMiddleFingerSeparated: boolean;
thumbAndIndexPinched: boolean;
thumbTouchedMiddleFinger: boolean;
indexOverMiddle: boolean;
}
class ConditionNode {
constructor(public yes: ConditionNode | string,
public no: ConditionNode | string,
public condition: (Hand) => boolean) {
}
handle(hand:Hand):string{
//console.log((this.condition as any).name);
if(this.condition(hand)){
if(this.yes instanceof ConditionNode){
return this.yes.handle(hand);
} else {
return this.yes as string;
}
} else {
if (this.no instanceof ConditionNode) {
return this.no.handle(hand);
} else {
return this.no as string;
}
}
}
}
function simon(hand, pointables) {
console.log(pointables[0].touchDistance);
if (hand.fingers[2].dipPosition[0] == pointables[0].touchZone || hand.fingers[2].carpPosition == pointables[0].touchZone ||
hand.fingers[2].mcpPosition == pointables[0].touchZone || hand.fingers[2].pipPosition == pointables[0].touchZone)
return true;
return false;
}
function conditionA(hand: Hand): boolean {
return hand.palm.facing.front;
}
function conditionB(hand: Hand): boolean {
return !hand.orientation.below;
}
function conditionC(hand: Hand): boolean {
for (let i = 0; i < 5; i++) {
if (!hand.fingers[i].extended) return true;
}
return false;
}
function conditionD(hand: Hand): boolean {
for (let i = 1; i <= 4; i++) {
if (!hand.fingers[i].extended) return false;
}
if (hand.fingers[0].extended) return false;
return true;
}
function conditionE(hand: Hand): boolean {
return !hand.palm.facing.front;
}
function conditionF(hand: Hand): boolean {
return hand.orientation.below;
}
function conditionG(hand: Hand): boolean {
// for (let i = 0; i <= 4; i++) {
// for (let j = 1; j <= 2; j++) {
// if (!hand.fingers[i].joints[j]) return false;
// }
// }
// return true;
return hand.semipinched;
}
function conditionH(hand: Hand): boolean {
return hand.fingers[0].extended;
}
function conditionI(hand: Hand): boolean {
return hand.fingers[1].extended;
}
function conditionJ(hand: Hand): boolean {
return hand.fingers[2].extended;
}
function conditionK(hand: Hand): boolean {
return hand.fingers[3].extended;
}
function conditionL(hand: Hand): boolean {
return hand.fingers[4].extended;
}
function conditionM(hand: Hand): boolean {
return hand.thumbTouchedMiddleFinger;
}
function conditionN(hand: Hand): boolean {
return hand.indexOverMiddle;
}
function conditionO(hand: Hand): boolean {
return hand.indexMiddleFingerSeparated;
}
function conditionP(hand: Hand): boolean {
return !hand.thumbAndIndexPinched;
}
const m = new ConditionNode('K', 'U', conditionM);
const b = new ConditionNode(m, 'H', conditionB);
const n = new ConditionNode('R', b, conditionN);
const o = new ConditionNode('V', n, conditionO);
const m2 = new ConditionNode('P', 'N', conditionM);
const f = new ConditionNode(m2, o, conditionF);
const l = new ConditionNode('B', 'W', conditionL);
const f2 = new ConditionNode('M', l, conditionF);
const i = new ConditionNode(f2, 'F', conditionI);
const k = new ConditionNode(i, f, conditionK);
const h = new ConditionNode('Y', 'I', conditionH);
const e = new ConditionNode('G', 'A', conditionE);
const l2 = new ConditionNode(h, e, conditionL);
const h2 = new ConditionNode('L', 'D', conditionH);
const e2 = new ConditionNode('T', h2, conditionE);
const f3 = new ConditionNode('Q', e2, conditionF);
const i2 = new ConditionNode(f3, l2, conditionI);
const j = new ConditionNode(k, i2, conditionJ);
const p = new ConditionNode('C', 'S', conditionP);
const c = new ConditionNode('O', p, conditionC);
const a = new ConditionNode(c, 'X', conditionA);
const d = new ConditionNode('E', a, conditionD);
const g = new ConditionNode(d, j, conditionG);
function analyze(hand: Hand) {
let result = g.handle(hand);
(window as any).updateUI(result);
return result;
}
function checkLibrary() {
}
let controller = new Leap.Controller();
controller.on('connect', () => {
setInterval(() => {
let frame = controller.frame();
if (frame.hands.length > 0) {
let hand = frame.hands[0];
let parsed = parseHand(hand, frame.pointables);
// console.log(hand);
console.log(analyze(parsed));
//console.log(parsed.palm.facing);
}
}, 1000);
});
function angle(dir1: number[], dir2: number[]) {
let mag1 = Math.sqrt(dir1[0] * dir1[0] + dir1[1] * dir1[1] + dir1[2] * dir1[2]);
let mag2 = Math.sqrt(dir2[0] * dir2[0] + dir2[1] * dir2[1] + dir2[2] * dir2[2]);
return Math.acos((dir1[0] * dir2[0] + dir1[1] * dir2[1] + dir1[2] * dir2[2]) / (mag1 * mag2));
}
function distance(pos1: number[], pos2: number[]) {
let result = 0;
for (let i = 0; i < 3; i++) {
let delta = pos1[i] - pos2[i];
result += delta * delta;
}
return Math.sqrt(result);
}
function hack(hand, parsedHand) {
// console.log(hand.direction[0],parsedHand.palm.facing.front, hand.fingers[1].extended,hand.fingers[2].extended);
// return hand.direction[0] < - 0.2 && !parsedHand.palm.facing.front && (hand.fingers[1].extended || hand.fingers[2].extended);
// console.log(hand.direction);
// if (hand.direction[2] < -0.3) {
// console.log('going up');
// return false;
// }
if (hand.direction[0] <= -0.1) {
// console.log('going left');
// parsedHand.fingers[1].extended = true;
// parsedHand.fingers[2].extended = true;
parsedHand.indexMiddleFingerSeparated = false;
return true;
}
}
function touching(finger1, finger2): boolean {
function close(pos1: number[], pos2: number[]) {
return distance(pos1, pos2) < 50;
}
for (let b = 0; b < finger1.bones.length; b++) {
for (let t = 0; t < 10; t++) {
let pos = [];
finger1.bones[b].lerp(pos, t / 10);
if (close(pos, finger2.distal.center())) {
return true;
}
}
}
return false;
}
function touchingComplete(finger1, finger2): boolean {
return touching(finger1, finger2) || touching(finger2, finger1);
}
function parseHand(hand, pointables) {
//console.log(hand);
let parsedHand = new Hand();
parsedHand.palm.facing.front = hand.palmNormal[1] < 0;
//console.log(parsedHand.palm.facing.front);
parsedHand.orientation.below = hand.arm.basis[2][0] < 0;
//console.log(parsedHand.orientation);
// console.log(hand.fingers);
// if(hand.fingers[1].extended && hand.fingers[2].extended) {
const indexFingerDirection = hand.fingers[1].direction;
const middleFingerDirection = hand.fingers[2].direction;
parsedHand.indexMiddleFingerSeparated = angle(indexFingerDirection, middleFingerDirection) > 0.2;
hack(hand, parsedHand);
parsedHand.fingers = [];
hand.fingers.forEach((finger) => {
let tmp = new Finger();
tmp.extended = finger.extended;
for (let i = 0; i < 4; i++) {
tmp.joints.push(finger.extended);
}
parsedHand.fingers.push(tmp);
});
// console.log(hand.fingers[1]);
parsedHand.thumbTouchedMiddleFinger = touchingComplete(hand.fingers[0], hand.fingers[2]);
// console.log(parsedHand.thumbTouchedMiddleFinger);
// console.log(parsedHand.indexMiddleFingerSeparated);
parsedHand.thumbAndIndexPinched = hand.pinchStrength > 0.95;
// console.log(angle(indexFingerDirection, middleFingerDirection), hand.fingers[1].extended, hand.fingers[2].extended);
parsedHand.semipinched = hand.pinchStrength > 0.5 && hand.pinchStrength < 1 && hand.grabStrength < 1;
// console.log(hand.pinchStrength, hand.grabStrength, parsedHand.semipinched);
/*
if (pointables != null && pointables.length > 0) {
parsedHand.indexOverMiddle = simon(hand, pointables);
} else {
parsedHand.indexOverMiddle = false;
}*/
parsedHand.indexOverMiddle = angle(hand.fingers[1],hand.fingers[2])<0.10;
return parsedHand;
}
controller.connect();
console.log('PLEB init');