-
Notifications
You must be signed in to change notification settings - Fork 2
/
GestureThread.cpp
236 lines (225 loc) · 8.58 KB
/
GestureThread.cpp
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
#include <QMessageBox>
#include <QDebug>
#include "VirtualTouchScreen.h"
#include "GestureThread.h"
#include "GestureAlgos.h"
class MyPipeline : public UtilPipeline
{
public:
explicit MyPipeline(VirtualTouchScreen *p) : UtilPipeline(), pres(p)
{
//for some reason only default values are accepted
//EnableImage(PXCImage::IMAGE_TYPE_DEPTH, 320, 240);
}
void PXCAPI OnGesture(PXCGesture::Gesture *data)
{
if (data->label & PXCGesture::Gesture::LABEL_NAV_SWIPE_LEFT) {
qDebug() << "Gesture detected: swipe left";
if (NULL != pres)
{
pres->onSwipe(VK_RIGHT);//more natural swipe
}
} else if (data->label & PXCGesture::Gesture::LABEL_NAV_SWIPE_RIGHT) {
qDebug() << "Gesture detected: swipe right";
if (NULL != pres)
{
pres->onSwipe(VK_LEFT);
}
} else if (data->label & PXCGesture::Gesture::LABEL_HAND_CIRCLE) {
qDebug() << "Gesture detected: circle";
}
}
void PXCAPI OnAlert(PXCGesture::Alert *alert)
{
if (alert->label & PXCGesture::Alert::LABEL_FOV_BOTTOM) {
qDebug() << "The tracking object touches the bottom field of view";
} else if (alert->label & PXCGesture::Alert::LABEL_FOV_LEFT) {
qDebug() << "The tracking object touches the left field of view";
} else if (alert->label & PXCGesture::Alert::LABEL_FOV_RIGHT) {
qDebug() << "The tracking object touches the right field of view";
} else if (alert->label & PXCGesture::Alert::LABEL_FOV_TOP) {
qDebug() << "The tracking object touches the top field of view";
} else if (alert->label & PXCGesture::Alert::LABEL_FOV_OK) {
qDebug() << "The tracking object is within field of view";
} else if (alert->label & PXCGesture::Alert::LABEL_FOV_BLOCKED) {
qDebug() << "The field of view is blocked";
} else if (alert->label & PXCGesture::Alert::LABEL_GEONODE_ACTIVE) {
qDebug() << "The tracking object is found";
} else if (alert->label & PXCGesture::Alert::LABEL_GEONODE_INACTIVE) {
qDebug() << "The tracking object is lost";
}
}
private:
VirtualTouchScreen *pres;
};
GestureThread::GestureThread(VirtualTouchScreen *obj) : QThread(),
mainWnd(obj)
{
setupPipeline();
connect(this, SIGNAL(moveHand()), mainWnd, SLOT(onMoveHand()));
connect(this, SIGNAL(showThumb(bool)), mainWnd, SLOT(onShowThumb(bool)));
//link touch signals with the corresponding slots in the main window
connect(this, SIGNAL(touchDown(const QPoint&, const QPoint&)), mainWnd,
SLOT(onTouchDown(const QPoint&, const QPoint&)));
connect(this, SIGNAL(touchDown(const QPoint&)), mainWnd,
SLOT(onTouchDown(const QPoint&)));
connect(this, SIGNAL(touchUp(const QPoint&, const QPoint&)), mainWnd,
SLOT(onTouchUp(const QPoint&, const QPoint&)));
connect(this, SIGNAL(touchUp(const QPoint&)), mainWnd,
SLOT(onTouchUp(const QPoint&)));
}
GestureThread::~GestureThread()
{
delete pipeline;
}
void GestureThread::run()
{
while (true)
{
if(pipeline->AcquireFrame(true))
{
gesture = pipeline->QueryGesture();
bool updateHand = true;
//hand position
PXCGesture::GeoNode handNode;
if((!mainWnd->hideThumb_) &&
(gesture->QueryNodeData(0, PXCGesture::GeoNode::LABEL_BODY_HAND_PRIMARY,
&handNode) != PXC_STATUS_ITEM_UNAVAILABLE))
{
//check hand status
if (handNode.opennessState & PXCGesture::GeoNode::Openness::LABEL_OPEN) {
qDebug() << "hand open";
emit showThumb(true);
} else if (handNode.opennessState & PXCGesture::GeoNode::Openness::LABEL_CLOSE) {
qDebug() << "hand closed";
emit showThumb(false);
}
}
//finger position
PXCGesture::GeoNode fingerNode[VirtualTouchScreen::POINTS];
int i = 0;
if ( gesture->QueryNodeData(0,
PXCGesture::GeoNode::LABEL_BODY_HAND_PRIMARY | PXCGesture::GeoNode::LABEL_FINGER_THUMB,
VirtualTouchScreen::POINTS, fingerNode) != PXC_STATUS_ITEM_UNAVAILABLE) {
for (i = 0; i < VirtualTouchScreen::POINTS; ++i) {
switch (fingerNode[i].body & PXCGesture::GeoNode::LABEL_MASK_DETAILS) {
case PXCGesture::GeoNode::LABEL_FINGER_THUMB:
qDebug() << "thumb: x = " << fingerNode[i].positionImage.x <<
", y = " << fingerNode[i].positionImage.y <<
", depth = " << fingerNode[i].positionWorld.y;
break;
case PXCGesture::GeoNode::LABEL_FINGER_INDEX:
qDebug() << "index: x = " << fingerNode[i].positionImage.x <<
", y = " << fingerNode[i].positionImage.y <<
", depth = " << fingerNode[i].positionWorld.y;
break;
default:
updateHand = false;
}
}
} else {
updateHand = false;
}
if (updateHand) {
//fingers
mainWnd->skeletonPointMutex_.lock();
qreal depth[VirtualTouchScreen::POINTS];
for (i = 0; i < VirtualTouchScreen::POINTS; ++i) {
mainWnd->handSkeletonPoints_[i] = QPointF(fingerNode[i].positionImage.x, fingerNode[i].positionImage.y);
mainWnd->gestureAlgos->imageToScreen(mainWnd->handSkeletonPoints_[i]);
depth[i] = static_cast<qreal>(fingerNode[i].positionWorld.y);
}
//Kalman filter for coordinates
mainWnd->gestureAlgos->filterKalman(
mainWnd->handSkeletonPoints_[VirtualTouchScreen::THUMB],
mainWnd->handSkeletonPoints_[VirtualTouchScreen::INDEX]);
mainWnd->skeletonPointMutex_.unlock();
//low pass filter for the depth
mainWnd->gestureAlgos->filterLowPass(depth[0], depth[1]);
//hand position update
emit moveHand();
//detect touch gesture
QPoint ptThumb;
QPoint ptIndex;
switch (mainWnd->gestureAlgos->isTouch(depth[VirtualTouchScreen::THUMB],
depth[VirtualTouchScreen::INDEX]))
{
case GestureAlgos::TouchType::DOUBLE_DOWN:
qDebug() << "sending double touch down";
emit touchDown(mainWnd->handSkeletonPoints_[VirtualTouchScreen::THUMB].toPoint(),
mainWnd->handSkeletonPoints_[VirtualTouchScreen::INDEX].toPoint());
break;
case GestureAlgos::TouchType::INDEX_DOWN:
qDebug() << "sending index touch down";
emit touchDown(mainWnd->handSkeletonPoints_[VirtualTouchScreen::INDEX].toPoint());
break;
case GestureAlgos::TouchType::THUMB_DOWN:
qDebug() << "sending thumb touch down";
emit touchDown(mainWnd->handSkeletonPoints_[VirtualTouchScreen::THUMB].toPoint());
break;
case GestureAlgos::TouchType::DOUBLE_UP:
qDebug() << "sending double touch up";
ptThumb = mainWnd->handSkeletonPoints_[VirtualTouchScreen::THUMB].toPoint();
ptIndex = mainWnd->handSkeletonPoints_[VirtualTouchScreen::INDEX].toPoint();
emit touchDown(ptThumb, ptIndex);//needed by touch up
emit touchUp(ptThumb, ptIndex);
break;
case GestureAlgos::TouchType::INDEX_UP:
qDebug() << "sending index touch up";
ptIndex = mainWnd->handSkeletonPoints_[VirtualTouchScreen::INDEX].toPoint();
emit touchDown(ptIndex);
emit touchUp(ptIndex);
break;
case GestureAlgos::TouchType::THUMB_UP:
qDebug() << "sending thumb touch up";
ptThumb = mainWnd->handSkeletonPoints_[VirtualTouchScreen::THUMB].toPoint();
emit touchDown(ptThumb);
emit touchUp(ptThumb);
break;
case GestureAlgos::TouchType::INDEX_DOWN_THUMB_UP:
qDebug() << "sending index down thumb up";
ptThumb = mainWnd->handSkeletonPoints_[VirtualTouchScreen::THUMB].toPoint();
ptIndex = mainWnd->handSkeletonPoints_[VirtualTouchScreen::INDEX].toPoint();
emit touchDown(ptThumb, ptIndex);
emit touchUp(ptThumb, ptIndex);
emit touchDown(ptIndex);
break;
case GestureAlgos::TouchType::INDEX_UP_THUMB_DOWN:
qDebug() << "sending index up thumb down";
ptThumb = mainWnd->handSkeletonPoints_[VirtualTouchScreen::THUMB].toPoint();
ptIndex = mainWnd->handSkeletonPoints_[VirtualTouchScreen::INDEX].toPoint();
emit touchDown(ptThumb, ptIndex);
emit touchUp(ptThumb, ptIndex);
emit touchDown(ptThumb);
break;
case GestureAlgos::TouchType::NONE:
qDebug() << "touch type is none";
break;
default:
qDebug() << "Error: unknown touch type";
}
}
// we must release the frame
pipeline->ReleaseFrame();
}
}
}
void GestureThread::setupPipeline()
{
pipeline = new MyPipeline(mainWnd);
pipeline->EnableGesture();
if (pipeline->Init())
{
pxcU32 imgWidth;
pxcU32 imgHeight;
if (!pipeline->QueryImageSize(PXCImage::IMAGE_TYPE_DEPTH, imgWidth, imgHeight))
{
imgWidth = imgHeight = -1;
}
mainWnd->gestureAlgos->setImageSize(QSize(imgWidth, imgHeight));
} else
{
QMessageBox::warning(NULL, "Presenter Helper", "Cannot init gesture camera");
::TerminateProcess(::GetCurrentProcess(), EXIT_FAILURE);
}
}