-
Notifications
You must be signed in to change notification settings - Fork 1
/
HandController.pde
146 lines (113 loc) · 2.98 KB
/
HandController.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
/* ========================
Hand Controller
======================== */
class HandController {
PVector handVec = new PVector();
ArrayList handVecList = new ArrayList();
boolean trackFlag;
boolean moveFlag;
float lastStopTime;
String lastGesture = "";
int LIST_SIZE = 10;
int DOT_STEP = 7;
float STOP_PIXEL = 5.0;
float STOP_TIME = 1.0;
HandController() {
trackFlag = false;
moveFlag = true;
}
public void update() {
context.update();
}
public void display() {
pushMatrix();
translate( width/2, height/2, 0 );
rotateX( radians( 180 ) );
int[] depthMap = context.depthMap();
int index;
PVector realWorldPoint;
// set the rotation center of the scene 1000 infront of the camera
translate( 0, 0, -1000 );
// draw the 3d point depth map
pushStyle();
strokeWeight( 3 );
stroke( 100 );
for( int y = 0; y < context.depthHeight(); y += DOT_STEP ) {
for( int x = 0; x < context.depthWidth(); x += DOT_STEP ) {
index = x + y * context.depthWidth();
if( depthMap[ index ] > 0 ) {
realWorldPoint = context.depthMapRealWorld()[ index ];
point( realWorldPoint.x, realWorldPoint.y, realWorldPoint.z );
}
}
}
popStyle();
// draw the tracked hand
if( trackFlag ) {
pushStyle();
strokeWeight( 3 );
stroke( 255, 255, 255, 80 );
noFill();
Iterator itr = handVecList.iterator();
beginShape();
while( itr.hasNext() ) {
PVector p = (PVector) itr.next();
vertex( p.x, p.y, p.z );
}
endShape();
strokeWeight( 10 );
stroke( 255, 255, 255, 180 );
point( handVec.x, handVec.y, handVec.z );
popStyle();
}
popMatrix();
}
public void createHands( PVector pos ) {
trackFlag = true;
handVec = pos;
handVecList.clear();
handVecList.add( pos );
}
public void updateHands( PVector pos ) {
handVec = pos;
handVecList.add( 0, pos );
if( handVecList.size() >= LIST_SIZE ) {
handVecList.remove( handVecList.size() - 1 );
}
}
public void destroyHands() {
trackFlag = false;
context.addGesture( lastGesture );
}
public void recognizeGesture( String strGesture, PVector idPosition, PVector endPosition ) {
lastGesture = strGesture;
context.removeGesture( strGesture );
context.startTrackingHands( endPosition );
}
public boolean isStop( float time ) {
if( ! trackFlag || handVecList.size() <= 2 ) return false;
PVector curr = (PVector) handVecList.get( 0 );
PVector last = (PVector) handVecList.get( handVecList.size() - 2 );
float diffX = abs( curr.x - last.x );
float diffY = abs( curr.y - last.y );
float diffTime = abs( time - lastStopTime );
if( diffX < STOP_PIXEL && diffY < STOP_PIXEL ) {
if( diffTime > STOP_TIME && moveFlag ) {
lastStopTime = time;
moveFlag = false;
return true;
}
moveFlag = false;
}
else {
moveFlag = true;
}
return false;
}
public boolean isTrack() {
return trackFlag;
}
public ArrayList getHandVecList() {
return handVecList;
}
};