-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSample.java
More file actions
162 lines (141 loc) · 7.02 KB
/
Sample.java
File metadata and controls
162 lines (141 loc) · 7.02 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
/******************************************************************************\
* Copyright (C) 2012-2013 Leap Motion, Inc. All rights reserved. *
* Leap Motion proprietary and confidential. Not for distribution. *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
\******************************************************************************/
import java.io.IOException;
import java.lang.Math;
import com.leapmotion.leap.*;
import com.leapmotion.leap.Gesture.State;
class SampleListener extends Listener {
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();
System.out.println("Frame id: " + frame.id()
+ ", timestamp: " + frame.timestamp()
+ ", hands: " + frame.hands().count()
+ ", fingers: " + frame.fingers().count()
+ ", tools: " + frame.tools().count()
+ ", gestures " + frame.gestures().count());
if (!frame.hands().empty()) {
// Get the first hand
Hand hand = frame.hands().get(0);
// Check if the hand has any fingers
FingerList fingers = hand.fingers();
if (!fingers.empty()) {
// Calculate the hand's average finger tip position
Vector avgPos = Vector.zero();
for (Finger finger : fingers) {
avgPos = avgPos.plus(finger.tipPosition());
}
avgPos = avgPos.divide(fingers.count());
System.out.println("Hand has " + fingers.count()
+ " fingers, average finger tip position: " + avgPos);
}
// Get the hand's sphere radius and palm position
System.out.println("Hand sphere radius: " + hand.sphereRadius()
+ " mm, palm position: " + hand.palmPosition());
// Get the hand's normal vector and direction
Vector normal = hand.palmNormal();
Vector direction = hand.direction();
// Calculate the hand's pitch, roll, and yaw angles
System.out.println("Hand pitch: " + Math.toDegrees(direction.pitch()) + " degrees, "
+ "roll: " + Math.toDegrees(normal.roll()) + " degrees, "
+ "yaw: " + Math.toDegrees(direction.yaw()) + " degrees");
}
GestureList gestures = frame.gestures();
for (int i = 0; i < gestures.count(); i++) {
Gesture gesture = gestures.get(i);
switch (gesture.type()) {
case TYPE_CIRCLE:
CircleGesture circle = new CircleGesture(gesture);
// Calculate clock direction using the angle between circle normal and pointable
String clockwiseness;
if (circle.pointable().direction().angleTo(circle.normal()) <= Math.PI/4) {
// Clockwise if angle is less than 90 degrees
clockwiseness = "clockwise";
} else {
clockwiseness = "counterclockwise";
}
// Calculate angle swept since last frame
double sweptAngle = 0;
if (circle.state() != State.STATE_START) {
CircleGesture previousUpdate = new CircleGesture(controller.frame(1).gesture(circle.id()));
sweptAngle = (circle.progress() - previousUpdate.progress()) * 2 * Math.PI;
}
System.out.println("Circle id: " + circle.id()
+ ", " + circle.state()
+ ", progress: " + circle.progress()
+ ", radius: " + circle.radius()
+ ", angle: " + Math.toDegrees(sweptAngle)
+ ", " + clockwiseness);
break;
case TYPE_SWIPE:
SwipeGesture swipe = new SwipeGesture(gesture);
System.out.println("Swipe id: " + swipe.id()
+ ", " + swipe.state()
+ ", position: " + swipe.position()
+ ", direction: " + swipe.direction()
+ ", speed: " + swipe.speed());
break;
case TYPE_SCREEN_TAP:
ScreenTapGesture screenTap = new ScreenTapGesture(gesture);
System.out.println("Screen Tap id: " + screenTap.id()
+ ", " + screenTap.state()
+ ", position: " + screenTap.position()
+ ", direction: " + screenTap.direction());
break;
case TYPE_KEY_TAP:
KeyTapGesture keyTap = new KeyTapGesture(gesture);
System.out.println("Key Tap id: " + keyTap.id()
+ ", " + keyTap.state()
+ ", position: " + keyTap.position()
+ ", direction: " + keyTap.direction());
break;
default:
System.out.println("Unknown gesture type.");
break;
}
}
if (!frame.hands().empty() || !gestures.empty()) {
System.out.println();
}
}
}
class Sample {
public static void main(String[] args) {
// Create a sample listener and controller
SampleListener listener = new SampleListener();
Controller controller = new Controller();
// Have the sample listener receive events from the controller
controller.addListener(listener);
// Keep this process running until Enter is pressed
System.out.println("Press Enter to quit...");
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
// Remove the sample listener when done
controller.removeListener(listener);
}
}