-
Notifications
You must be signed in to change notification settings - Fork 1
/
Skeleton.pde
190 lines (156 loc) · 4.85 KB
/
Skeleton.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
import SimpleOpenNI.*;
class Skeleton {
PApplet applet;
SimpleOpenNI kinect;
int userID;
boolean handedness;
boolean userCalibrated;
float confidence;
final static boolean LEFT_HANDED = true;
final static boolean RIGHT_HANDED = false;
PVector head, neck, torso, shoulderL, shoulderR, elbowL, elbowR, handL, handR, hipL, hipR;
PVector [] joints;
PVector drawingHand, secondaryHand;
PVector offset, flip;
/**
* Create a new User
* @param k Kinect to get user data from
* @param u User ID
* @param h Which hand to use for drawing
*/
Skeleton( PApplet p, SimpleOpenNI k, int u, boolean h ) {
applet = p;
kinect = k;
userID = u;
handedness = h;
joints = new PVector[11];
head = new PVector();
neck = new PVector();
torso = new PVector();
shoulderL = new PVector();
shoulderR = new PVector();
elbowL = new PVector();
elbowR = new PVector();
handL = new PVector();
handR = new PVector();
hipL = new PVector();
hipR = new PVector();
drawingHand = new PVector();
secondaryHand = new PVector();
userCalibrated = false;
}
boolean update(PVector c) {
if (kinect.isTrackingSkeleton( userID )) {
kinect.getJointPositionSkeleton(userID, SimpleOpenNI.SKEL_HEAD, head);
kinect.getJointPositionSkeleton(userID, SimpleOpenNI.SKEL_NECK, neck);
kinect.getJointPositionSkeleton(userID, SimpleOpenNI.SKEL_TORSO, torso);
kinect.getJointPositionSkeleton(userID, SimpleOpenNI.SKEL_RIGHT_SHOULDER, shoulderL);
kinect.getJointPositionSkeleton(userID, SimpleOpenNI.SKEL_LEFT_SHOULDER, shoulderR);
kinect.getJointPositionSkeleton(userID, SimpleOpenNI.SKEL_RIGHT_ELBOW, elbowL);
kinect.getJointPositionSkeleton(userID, SimpleOpenNI.SKEL_LEFT_ELBOW, elbowR);
float confidenceL = kinect.getJointPositionSkeleton(userID, SimpleOpenNI.SKEL_RIGHT_HAND, handL);
float confidenceR = kinect.getJointPositionSkeleton(userID, SimpleOpenNI.SKEL_LEFT_HAND, handR);
kinect.getJointPositionSkeleton(userID, SimpleOpenNI.SKEL_RIGHT_HIP, hipL);
kinect.getJointPositionSkeleton(userID, SimpleOpenNI.SKEL_LEFT_HIP, hipR);
//Based on hand setting choose where to draw the drawingHand
if( handedness == LEFT_HANDED ) {
drawingHand.set( handL );
secondaryHand.set( handR );
confidence = confidenceL;
} else {
drawingHand.set( handR );
secondaryHand.set( handL );
confidence = confidenceR;
}
userCalibrated = true;
} else {
userCalibrated = false;
}
c.set(drawingHand);
return userCalibrated;
}
void display() {
display( true, 20, 0 );
}
void display(boolean showSkeleton, float cursorSize, int cursorColor) {
if( userCalibrated ) {
noFill();
strokeWeight(2);
stroke( 0 );
if( showSkeleton ) {
//Head
// line( head, neck );
//Body square
line( shoulderL, shoulderR );
line( shoulderR, hipR );
line( hipR, hipL );
line( hipL, shoulderL );
//Arms
line( shoulderL, elbowL );
line( elbowL, handL );
line( shoulderR, elbowR );
line( elbowR, handR );
float shoulderWidth = PVector.sub(shoulderL, shoulderR).mag();
//Draw a simple head
pushMatrix();
translate( head.x, head.y + (shoulderWidth * -0.5) / 2, head.z );
ellipse(0, 0, shoulderWidth * 0.5, shoulderWidth * 0.5 );
popMatrix();
}
//Draw hands
//fill(skeleton.getConfidence() * 200, 0, 0);
fill(cursorColor);
noStroke();
pushMatrix();
translate(drawingHand.x, drawingHand.y, drawingHand.z);
sphere( cursorSize );
popMatrix();
// fill( 150 );
// pushMatrix();
// translate(secondaryHand.x, secondaryHand.y, secondaryHand.z);
// sphere( 20 );
// popMatrix();
}
}
void changeHand() {
handedness = !handedness;
}
void setHand( boolean hand ) {
handedness = hand;
}
boolean getHand() {
return handedness;
}
void line( PVector a, PVector b ) {
applet.line( a.x, a.y, a.z, b.x, b.y, b.z );
}
void setUser(int u ) {
userID = u;
}
void nextUser() {
int numberOfUsers = kinect.getUsers().length;
if( numberOfUsers > 1 ) {
userID = (userID + 1 ) % 10;
}
println("Current users is " + userID);
}
PVector getDrawingHand() {
return drawingHand;
}
void getDrawingHand(PVector target) {
target.set(drawingHand);
}
void getSecondaryHand(PVector target) {
if( handedness == LEFT_HANDED ) {
target.set(handR);
} else {
target.set(handL);
}
}
void reset() {
kinect.stopTrackingSkeleton(userID);
}
float getConfidence() {
return confidence;
}
}