forked from cassiodezotti/Rabisco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommand.pde
555 lines (454 loc) · 13.9 KB
/
Command.pde
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
public abstract class Command
{
private Object actor;
private Method method;
protected Object[] args;
public Command(Object actor, Method method)
{
this.actor = actor;
this.method = method;
}
public void run()
{
try
{
if(args == null)
{
method.invoke(actor);
}
else
{
method.invoke(actor, args);
}
}
catch (Exception e)
{
println("Erro de reflexão ao executar comando");
e.printStackTrace();
}
}
public abstract void changeArgs(Object obj);
}
public class TriggerCommand extends Command
{
public TriggerCommand(Object actor, Method method)
{
super(actor, method);
args = null;
}
public void changeArgs(Object obj)
{
}
}
public class PointerCommand extends Command
{
public PointerCommand(Object actor, Method method)
{
super(actor, method);
args = new Object[1];
args[0] = new PVector();
}
public void changeArgs(PVector position)
{
args[0] = position;
}
public void changeArgs(Object object)
{
PVector p = (PVector) object;
changeArgs(p);
}
}
public class AxisCommand extends Command
{
public AxisCommand(Object actor, Method method)
{
super(actor, method);
args = new Object[1];
args[0] = 0.0;
}
public void changeArgs(float value)
{
args[0] = value;
}
public void changeArgs(Object object)
{
float d = (float)object;
changeArgs(d);
}
}
public interface InputIdentifier
{
}
public abstract class InputManager
{
private HashMap<InputIdentifier, ArrayList<Command>> commandMap = new HashMap<InputIdentifier, ArrayList<Command>>();
public void addCommand(InputIdentifier identifier, Command command)
{
ArrayList<Command> list = commandMap.get(identifier);
if(list == null)
{
list = new ArrayList<Command>();
}
list.add(command);
commandMap.put(identifier, list);
}
public void removeCommand(InputIdentifier identifier)
{
commandMap.remove(identifier);
}
public void clearCommands()
{
commandMap.clear();
}
public abstract boolean haveInput(InputIdentifier identifier);
public abstract void run();
protected void runCommand(InputIdentifier identifier)
{
ArrayList<Command> list = commandMap.get(identifier);
if(list == null)
{
return;
}
for(Command command : list)
{
if (command != null)
{
command.run();
}
}
}
protected void changeArgs(InputIdentifier identifier, Object arg)
{
ArrayList<Command> list = commandMap.get(identifier);
if(list == null)
{
return;
}
for(Command command : list)
{
if (command != null)
{
command.changeArgs(arg);
}
}
}
}
public enum MouseInputID implements InputIdentifier
{
LEFT_BUTTON, RIGHT_BUTTON, POINTER,
LEFT_BUTTON_RELEASE, RIGHT_BUTTON_RELEASE;
};
public class MouseInputManager extends InputManager
{
public void run()
{
if(mousePressed && mouseButton == LEFT)
{
runCommand(MouseInputID.LEFT_BUTTON);
}
if(mousePressed && mouseButton == RIGHT)
{
runCommand(MouseInputID.RIGHT_BUTTON);
}
PVector position = new PVector(mouseX, mouseY);
changeArgs(MouseInputID.POINTER, position);
runCommand(MouseInputID.POINTER);
}
public boolean haveInput(InputIdentifier identifier)
{
boolean result = false;
for(MouseInputID mouseInputID : MouseInputID.values())
{
if(identifier.equals(mouseInputID))
{
result = true;
break;
}
}
return result;
}
}
public enum BodyInputID implements InputIdentifier
{
HEAD, NOSE,
EYE_R, EYE_L, EYE_L_INNER, EYE_R_INNER, EYE_L_OUTER, EYE_R_OUTER,
EAR_R, EAR_L,
MOUTH_R, MOUTH_L,
NECK, SHOULDER_R, SHOULDER_L, SPINE_SHOULDER, SPINE_MID, SPINE_BASE,
ELBOW_R, ELBOW_L,
WRIST_R, WRIST_L, HAND_R, HAND_L,
HAND_THUMB_L, HAND_THUMB_R,
HIP_R, HIP_L, KNEE_R, KNEE_L, ANKLE_R, ANKLE_L, FOOT_L, FOOT_R,
THUMB_CMC_L, THUMB_MCP_L, THUMB_IP_L, THUMB_TIP_L, INDEX_FINGER_MCP_L, INDEX_FINGER_PIP_L, INDEX_FINGER_DIP_L, INDEX_FINGER_TIP_L, MIDDLE_FINGER_MCP_L, MIDDLE_FINGER_PIP_L, MIDDLE_FINGER_DIP_L, MIDDLE_FINGER_TIP_L, RING_FINGER_MCP_L, RING_FINGER_PIP_L, RING_FINGER_DIP_L, RING_FINGER_TIP_L, PINKY_MCP_L, PINKY_PIP_L, PINKY_DIP_L, PINKY_TIP_L,
THUMB_CMC_R, THUMB_MCP_R, THUMB_IP_R, THUMB_TIP_R, INDEX_FINGER_MCP_R, INDEX_FINGER_PIP_R, INDEX_FINGER_DIP_R, INDEX_FINGER_TIP_R, MIDDLE_FINGER_MCP_R, MIDDLE_FINGER_PIP_R, MIDDLE_FINGER_DIP_R, MIDDLE_FINGER_TIP_R, RING_FINGER_MCP_R, RING_FINGER_PIP_R, RING_FINGER_DIP_R, RING_FINGER_TIP_R, PINKY_MCP_R, PINKY_PIP_R, PINKY_DIP_R, PINKY_TIP_R,
FOOT_R_INDEX, FOOT_L_INDEX;
};
public class RelativePoseID implements InputIdentifier
{
private BodyInputID kp1, kp2;
int axis1, axis2;
boolean lessThan;
public RelativePoseID(BodyInputID kp1, BodyInputID kp2, int axis1, int axis2, boolean lessThan)
{
this.kp1 = kp1;
this.kp2 = kp2;
this.axis1 = axis1;
this.axis2 = axis2;
this.lessThan = lessThan;
}
public String getKp1Name()
{
return kp1.name();
}
public String getKp2Name()
{
return kp2.name();
}
public boolean was_triggered(float kp1_position[], float kp2_position[])
{
boolean bigger_than = false;
if(kp1_position[axis1] > kp2_position[axis2])
{
bigger_than = true;
}
if(lessThan)
{
return !bigger_than;
}
else
{
return bigger_than;
}
}
}
public enum GestureInputID implements InputIdentifier
{
FIVE, FIST, PEACE, ONE, TWO, THREE, FOUR, OK, UNKOWN,
FIVE_LOADING, FIST_LOADING, PEACE_LOADING, ONE_LOADING, TWO_LOADING, THREE_LOADING, FOUR_LOADING,
OK_LOADING, UNKOWN_LOADING;
public static GestureInputID valueof(String text)
{
for(GestureInputID id : GestureInputID.values())
{
if(id.toString().equals(text))
{
return (Rabisco.GestureInputID) id;
}
}
return GestureInputID.UNKOWN;
}
};
public class BodyInputManager extends InputManager
{
private MessageManager msgManager;
private boolean enableInterpolation;
private BodyPose previousPose, currentPose;
private GestureInputID currentGesture;
private float gesture_timer, previous_gesture_time;
private float width, height;
private boolean enableSlerp;
private ArrayList<RelativePoseID> relativePoseID;
private static final float GESTURE_TIME = 2500.0;
public BodyInputManager(MessageManager msgManager, float width, float height)
{
this.msgManager = msgManager;
this.width = width;
this.height = height;
gesture_timer = 0.0;
enableSlerp = false;
relativePoseID = new ArrayList<RelativePoseID>();
}
public void run()
{
if(msgManager.hasMessage(BodyPose.class))
{
BodyPose bodypose = msgManager.getData(BodyPose.class);
handleBodyPose(bodypose);
handleRelativePose();
}
if(msgManager.hasMessage(Gesture.class))
{
Gesture gesture = msgManager.getData(Gesture.class);
handleGesture(gesture);
}
}
private void handleBodyPose(BodyPose bodyPose)
{
estimateKeypointsPosition(bodyPose);
for(String name : bodyPose.keypoints_names)
{
float[] kp = this.currentPose.keypoints.get(name);
if(kp != null)
{
PVector position = new PVector(kp[0], kp[1]);
changeArgs(BodyInputID.valueOf(name), position);
runCommand(BodyInputID.valueOf(name));
}
}
}
private void handleRelativePose()
{
for(RelativePoseID rp : relativePoseID)
{
float[] kp1, kp2;
kp1 = currentPose.keypoints.get(rp.getKp1Name());
kp2 = currentPose.keypoints.get(rp.getKp2Name());
if(kp1 == null || kp2 == null)
{
continue;
}
if(rp.was_triggered(kp1, kp2))
{
runCommand(rp);
}
}
}
private InputIdentifier createRelativeTrigger(BodyInputID kp1, BodyInputID kp2, int axis1, int axis2, boolean lessThan)
{
RelativePoseID newID = new RelativePoseID(kp1, kp2, axis1, axis2, lessThan);
relativePoseID.add(newID);
return newID;
}
private void estimateKeypointsPosition(BodyPose newPose)
{
this.previousPose = this.currentPose;
this.currentPose = new BodyPose();
this.currentPose.time = newPose.time;
this.currentPose.keypoints_names = newPose.keypoints_names;
this.currentPose.user_id = newPose.user_id;
this.currentPose.frame_id = newPose.frame_id;
this.currentPose.pixel_space = newPose.pixel_space;
this.currentPose.keypoints = new HashMap<String, float[]>();
for(String name : newPose.keypoints_names)
{
float[] kp = newPose.keypoints.get(name);
if(kp == null || Float.isInfinite(kp[0]))
{
continue;
}
PVector mapped = new PVector((map(kp[0], 0, 100, 1, 0)*width), (map(kp[1], 0, 100, 0, 1)*height));
if(previousPose == null || !enableInterpolation)
{
if(kp != null)
{
float[] mapped_float = {mapped.x, mapped.y, 1.0};
currentPose.keypoints.put(name, mapped_float);
}
}
else
{
PVector estimate = new PVector();
if(kp != null)
{
float[] kp_previous = this.previousPose.keypoints.get(name);
if(enableSlerp)
{
estimate = slerp(new PVector(kp_previous[0], kp_previous[1]), mapped, 0.5);
}
else
{
estimate.x = lerp(kp_previous[0], mapped.x, 0.5);
estimate.y = lerp(kp_previous[1], mapped.y, 0.5);
}
}
float[] estimate_float = {estimate.x, estimate.y, 1.0};
currentPose.keypoints.put(name, estimate_float);
}
}
}
private void handleGesture(Gesture gesture)
{
if(gesture == null)
{
return;
}
else if (gesture.gesture == null)
{
return;
}
if(currentGesture == null || !GestureInputID.valueof(gesture.gesture).equals(currentGesture))
{
if(!GestureInputID.valueof(gesture.gesture).equals(currentGesture) && currentGesture != null)
{
GestureInputID loadingID = GestureInputID.valueof(currentGesture.toString()+"_LOADING");
changeArgs(loadingID, 0.0);
runCommand(loadingID);
}
currentGesture = GestureInputID.valueOf(gesture.gesture);
gesture_timer = 0.0;
}
else
{
if(gesture_timer == 0.0)
{
previous_gesture_time = millis()-0.1;
}
gesture_timer += millis() - previous_gesture_time;
previous_gesture_time = millis();
GestureInputID loadingID = GestureInputID.valueof(currentGesture.toString()+"_LOADING");
changeArgs(loadingID, gesture_timer/GESTURE_TIME);
runCommand(loadingID);
}
if(gesture_timer>=GESTURE_TIME)
{
GestureInputID loadingID = GestureInputID.valueof(currentGesture.toString()+"_LOADING");
changeArgs(loadingID, gesture_timer/GESTURE_TIME);
runCommand(loadingID);
runCommand(currentGesture);
gesture_timer = 0.0;
}
}
public boolean haveInput(InputIdentifier identifier)
{
if(identifier instanceof RelativePoseID)
{
return true;
}
boolean result = false;
for(BodyInputID bodyInputID : BodyInputID.values())
{
if(identifier.equals(bodyInputID))
{
result = true;
break;
}
}
if(result == true)
{
return result;
}
for(GestureInputID gestureInputID : GestureInputID.values())
{
if(identifier.equals(gestureInputID))
{
result = true;
break;
}
}
return result;
}
public float[] getKP(BodyInputID kp_id)
{
if(currentPose == null)
{
return null;
}
return currentPose.keypoints.get(kp_id.name());
}
}
/**
* Spherical linear interpolation/extrapolation for PVectors.
* @param v1 PVector 1.
* @param v2 PVector 2.
* @param step percentage of path from v1 to v2.
* @return PVector between v1 and v2 (if 0<step<1).
*/
PVector slerp(PVector v1, PVector v2, float step) {
float theta = PVector.angleBetween(v1, v2);
if (sin(theta)==0) {
return v1;
}
float v1Multiplier = sin((1-step)*theta)/sin(theta);
float v2Multiplier = sin(step*theta)/sin(theta);
return PVector.add(PVector.mult(v1, v1Multiplier), PVector.mult(v2, v2Multiplier));
}