-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConductorListener.java
339 lines (290 loc) · 12.6 KB
/
ConductorListener.java
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
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.text.DecimalFormat;
import com.leapmotion.leap.Arm;
import com.leapmotion.leap.Controller;
import com.leapmotion.leap.Finger;
import com.leapmotion.leap.FingerList;
import com.leapmotion.leap.Frame;
import com.leapmotion.leap.Gesture;
import com.leapmotion.leap.Hand;
import com.leapmotion.leap.InteractionBox;
import com.leapmotion.leap.Listener;
import com.leapmotion.leap.Tool;
import com.leapmotion.leap.ToolList;
import com.leapmotion.leap.Vector;
/**
*
* Main Listener
*
*/
public class ConductorListener extends Listener {
private LeapViewer viewer;
public Tool globalTool;
public ConductorListener(LeapViewer v)
{ super();
viewer = v;
//this.globalTool = v.globalTool;
}
// instance variables for static left, right, center
protected static double finalLeftMax;
protected static double finalRightMax;
protected static double finalCenter;
int leftStop = 0;
int rightStop = 0;
int centerStop;
double prevPos = 0;
// instance variables for floating left, right, center
protected static double mLeftPos = 0.0;
protected static double mRightPos = 0.0;
protected static double centerPos = 0.0;
private boolean atCenter = true;
private double prevDiff;
private double currentDiff;
private double mLeftDir = 0.0;
private double mRightDir = 0.0;
protected double testInteger = 0;
private double wristHorizStart = 0.0;
private double wristVertStart = 0.0;
private int wristStartMarker = 0;
public String leftOrRight = " ";
public String yesOrNo = "No";
private DecimalFormat form = new DecimalFormat("##.##");
private static PropertyChangeSupport propertyChangeSupport =
new PropertyChangeSupport(CenterListener.class);
public static void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}
public void setLeft(double leftIn) {
double oldValue = CenterListener.mLeftPos;
CenterListener.mLeftPos = leftIn;
propertyChangeSupport.firePropertyChange("leftIn", oldValue, CenterListener.mLeftPos);
}
public double getCenterPos() {
return centerPos;
}
public void onInit(Controller controller) {
System.out.println("Initialized");
}
public void onConnect(Controller controller) {
System.out.println("Connected");
controller.enableGesture(Gesture.Type.TYPE_SWIPE);
controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
controller.enableGesture(Gesture.Type.TYPE_SCREEN_TAP);
controller.enableGesture(Gesture.Type.TYPE_KEY_TAP);
}
public void onDisconnect(Controller controller) {
//Note: not dispatched when running in a debugger.
System.out.println("Disconnected");
}
public void onExit(Controller controller) {
System.out.println("Exited");
}
public void onFrame(Controller controller) {
// Get the most recent frame and report some basic information
Frame frame = controller.frame();
String result = "";
boolean hasTools = frame.tools().count() >= 1;//!frame.tools().isEmpty();
if (frame.hands().isEmpty()) {
mLeftPos = 0.0;
mRightPos = 0.0;
centerPos = 0.0;
finalLeftMax = 0.0;
finalRightMax = 0.0;
finalCenter = 0.0;
leftStop = 0;
rightStop = 0;
centerStop = 0;
mLeftDir = 0.0;
mRightDir = 0.0;
prevPos = 0;
wristStartMarker = 0;
//yesOrNo = "No";
}
else /*(!frame.hands().isEmpty() || hasTools)*/ {
// Get the first hand
Hand hand = frame.hands().get(0);
Tool tool = frame.tools().get(0);
Arm arm = hand.arm();
Vector wrist = arm.wristPosition();
// set wrist starting position
if (wristStartMarker == 0) {
wristStartMarker = 1;
wristHorizStart = wrist.get(0);
wristVertStart = wrist.get(1);
}
// Check if the hand has any fingers
FingerList fingers = hand.fingers(); // <--- add in finger type here
viewer.setFrameInfo(fingers.count(), hasTools);
if ((!fingers.isEmpty()) || hasTools) {
// Calculate the hand's average finger tip position
Vector avgPos = Vector.zero();
for (Finger finger : fingers) {
if (finger.isExtended() || frame.tools().count() >= 1) {
avgPos = avgPos.plus(finger.tipPosition());
Vector direction = finger.direction();
/*
*
* Get tip and base position - Set floating max left, right, and center
*
*/
// gets tip position, formats to two decimal points
double fingPos = finger.tipPosition().get(0);
// gets fartherst left (max left) finger base and tip position and direction
if (finger.tipPosition().get(0) < mLeftPos) {
setLeft(finger.tipPosition().get(0));
}
if (direction.get(0) < mLeftDir) {
mLeftDir = direction.get(0);
}
//result += ", mL: " + form.format(mLeftPos);
// gets fartherst right (max right) finger tip and base position and direction
if (finger.tipPosition().get(0) > mRightPos) {
mRightPos = finger.tipPosition().get(0);
}
if (direction.get(0) > mRightDir) {
mRightDir = direction.get(0);
}
//result += ", mR: " + form.format(mRightPos);
// gets center position of max left and max right and directs left or right to center
centerPos = (mLeftPos + mRightPos) / 2;
//result += ", center: " + form.format(centerPos);
//result += ", finger tip pos: " + truncFingPos;
/*
*
* Set static max left, right, center
* finalLeftMax, finalRightMax, finalCenter
* finalLeftBase, finalRightBase
*
*/
result += "Finger Position: " + (int) fingPos;//form.format(fingPos);
//result += " basePos: " + form.format(basePos);
//result += " initBasePos: " + form.format(initBase);
// fix left
if (prevPos < fingPos && prevPos < -40.0 && leftStop == 0) {
finalLeftMax = prevPos;
leftStop = 1;
}
if (leftStop == 1) {
result += " MaxL: " + (int) finalLeftMax; //form.format(finalLeftMax);
}
// fix right
if (prevPos > fingPos && prevPos > 10.0 && rightStop == 0) {
finalRightMax = prevPos;
rightStop = 1;
}
if (rightStop == 1) {
result += " MaxR: " + (int) finalRightMax; //form.format(finalRightMax);
}
// fix center
if (leftStop == 1 && rightStop == 1) {
finalCenter = (finalRightMax + finalLeftMax) / 2; //centerPos;
centerStop = 1;
}
if (centerStop == 1) {
//result += " Center: " + form.format(finalCenter);
result += " Center is: -";
yesOrNo = " ";
}
prevPos = fingPos;
/*
*
* Commands to user
*
*/
result += "start: (" + (int)wristHorizStart + "," + (int)wristVertStart + ") ";
result += "current: (" + (int)wrist.get(0) + "," + (int)wrist.get(1) + ") ";
result += "Baton: " + (hasTools ? "Yes " : "No ");
if (fingPos > (finalCenter + 5.0) && centerStop == 1) {
result = result.substring(0, result.length() - 1);
result += "L";
atCenter = false;
leftOrRight = " L ";
}
if (fingPos < (finalCenter - 5.0) && centerStop == 1) {
result = result.substring(0, result.length() - 1);
result += "R";
atCenter = false;
leftOrRight = " R ";
}
else {
atCenter = true;
}
result += " " + "CENTER: " + finalCenter;
if (wrist.get(0) > (wristHorizStart + 20) || wrist.get(0) < (wristHorizStart - 20)
|| wrist.get(1) > (wristVertStart + 20) || wrist.get(1) < (wristVertStart - 20)) {
result += "Don't move WRIST!!!";
yesOrNo = "YES";
}
else {
yesOrNo = "No";
}
testInteger++;
}
}
avgPos = avgPos.divide(fingers.count());
}
// else if (hasTools) {
// // enter tool controls here
// }
// resets positions to 0 if fingers leave frame
// else {
// mLeftPos = 0.0;
// mRightPos = 0.0;
// centerPos = 0.0;
// finalLeftMax = 0.0;
// finalRightMax = 0.0;
// finalCenter = 0.0;
// leftStop = 0;
// rightStop = 0;
// centerStop = 0;
// wristStartMarket = 0;
// }
}
System.out.println(result);
examineHand( frame.hands().get(0));
examineFeedback();
}
private void examineHand(Hand hand)
{
int fCount = 0;
Vector avgPos = Vector.zero();
// check if the hand has any fingers
FingerList fingers = hand.fingers();
if (!fingers.isEmpty()) {
// Calculate the hand's average finger tip position
//fCount = fingers.count();
for (Finger finger : fingers)
if (finger.isExtended()) {
fCount++;
avgPos = avgPos.plus(finger.tipPosition());
avgPos = avgPos.divide(fingers.count());
}
}
/* check if finger is deep within the touch zone, which
ranges from 1 to -1 (-1 being nearer the screen) */
boolean isTouched = false;
Finger frontFinger = hand.fingers().frontmost();
float touchDist = frontFinger.touchDistance();
if (touchDist< -0.8f) {
// System.out.println("Pressed: touch distance: " + touchDist);
isTouched = true;
}
// get the hand's normal vector and direction
Vector normal = hand.palmNormal();
// a unit vector pointing orthogonally downwards relative to the palm
Vector direction = hand.direction();
// a unit vector pointing from the palm position to the fingers
// calculate the hand's pitch, roll, and yaw angles
int pitch = (int) Math.round( Math.toDegrees(direction.pitch()));
int roll = (int) Math.round( Math.toDegrees(normal.roll()));
int yaw = (int) Math.round( Math.toDegrees(direction.yaw()));
// convert the palm to interaction box coordinates
//Vector palmIB = ib.normalizePoint(hand.palmPosition());
// report hand info to the GUI
viewer.setHandInfo(fCount, avgPos);
}
private void examineFeedback() {
viewer.setFeedbackInfo(finalCenter, leftOrRight, yesOrNo);
}
}