-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathHandMessageListener.h
More file actions
156 lines (134 loc) · 4.72 KB
/
Copy pathHandMessageListener.h
File metadata and controls
156 lines (134 loc) · 4.72 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
/*
* HandMessageListener.h
* OpenniTry
*
* Created by Roy Shilkrot on 9/30/11.
* Copyright 2011 MIT. All rights reserved.
*
*/
#ifndef _HANDMESSAGELISTENER_H
#define _HANDMESSAGELISTENER_H
#include <XnCppWrapper.h>
#include <XnVPointControl.h>
#include <XnVFlowRouter.h>
#include <XnVSwipeDetector.h>
#include <XnVSelectableSlider1D.h>
#include <XnVSteadyDetector.h>
#include <XnVBroadcaster.h>
#include <XnVPushDetector.h>
#include <XnVWaveDetector.h>
#include <XnVSessionManager.h>
#include <XnVCircleDetector.h>
#include <sstream>
using namespace std;
void send_event(const string& etype, const string& edata);
void send_log(const std::string& s);
class HandPointControl : public XnVPointControl {
public:
HandPointControl(xn::DepthGenerator depthGenerator, XnVSessionManager* sessionManager):m_DepthGenerator(depthGenerator),m_SessionManager(sessionManager) {
// m_pInnerFlowRouter = new XnVFlowRouter;
m_pPushDetector = new XnVPushDetector;
m_pSwipeDetector = new XnVSwipeDetector;
m_pSwipeDetector->SetMotionSpeedThreshold(0.8); //swipes should be faster then regular movement
// m_pSteadyDetector = new XnVSteadyDetector;
// m_pWaveDetector = new XnVWaveDetector;
m_pCircleDetector = new XnVCircleDetector;
m_pCircleDetector->SetMinRadius(80); //circles should be big enough to prevent confusion with random movement
// m_pInnerFlowRouter->SetActive(m_pPushDetector);
// Add the push detector and flow manager to the broadcaster
// m_Broadcaster.AddListener(m_pInnerFlowRouter);
// m_Broadcaster.AddListener(m_pPushDetector);
// Push
m_pPushDetector->RegisterPush(this, &Push_Pushed);
m_pCircleDetector->RegisterCircle(this, &ACircle);
//m_pWaveDetector->RegisterWave(this, &Wave_Waved);
m_pSwipeDetector->RegisterSwipeLeft(this, &Swipe_Left);
m_pSwipeDetector->RegisterSwipeRight(this, &Swipe_Right);
m_pSwipeDetector->RegisterSwipeUp(this, &Swipe_Up);
m_pSwipeDetector->RegisterSwipeDown(this, &Swipe_Down);
send_log("HandPointControl() DONE");
}
void Update(XnVMessage* pMessage)
{
XnVPointControl::Update(pMessage);
//m_Broadcaster.Update(pMessage);
m_pPushDetector->Update(pMessage);
// m_pWaveDetector->Update(pMessage);
m_pCircleDetector->Update(pMessage);
m_pSwipeDetector->Update(pMessage);
}
static void XN_CALLBACK_TYPE Swipe_Left(XnFloat fVelocity, XnFloat fAngle, void* pUserCxt) {
send_event("SwipeLeft", "");
}
static void XN_CALLBACK_TYPE Swipe_Right(XnFloat fVelocity, XnFloat fAngle, void* pUserCxt) {
send_event("SwipeRight", "");
}
static void XN_CALLBACK_TYPE Swipe_Up(XnFloat fVelocity, XnFloat fAngle, void* pUserCxt) {
send_event("SwipeUp", "");
}
static void XN_CALLBACK_TYPE Swipe_Down(XnFloat fVelocity, XnFloat fAngle, void* pUserCxt) {
send_event("SwipeDown", "");
}
// Push detector
static void XN_CALLBACK_TYPE Push_Pushed(XnFloat fVelocity, XnFloat fAngle, void* cxt)
{
printf("Push!\n");
send_event("Push", "");
}
static void XN_CALLBACK_TYPE ACircle(XnFloat fTimes, XnBool bConfident, const XnVCircle* pCircle, void* cxt) {
if(bConfident) {
printf("Bye Bye!\n");
((HandPointControl*)cxt)->KillSession();
}
}
// Wave detector
static void XN_CALLBACK_TYPE Wave_Waved(void* cxt)
{
printf("Bye Bye!\n");
((HandPointControl*)cxt)->KillSession();
}
void KillSession() { m_SessionManager->EndSession(); }
/**
* Handle creation of a new point
*/
void OnPointCreate(const XnVHandPointContext* cxt) {
printf("** %d\n", cxt->nID);
send_event("Register", "");
}
/**
* Handle new position of an existing point
*/
void OnPointUpdate(const XnVHandPointContext* cxt) {
// positions are kept in projective coordinates, since they are only used for drawing
XnPoint3D ptProjective(cxt->ptPosition);
// printf("Point (%f,%f,%f)", ptProjective.X, ptProjective.Y, ptProjective.Z);
m_DepthGenerator.ConvertRealWorldToProjective(1, &ptProjective, &ptProjective);
// printf(" -> (%f,%f,%f)\n", ptProjective.X, ptProjective.Y, ptProjective.Z);
//move to [0->100,0->100,0->2048]
stringstream ss;
ss << "\"x\":" << (int)(100.0*ptProjective.X/640.0)
<< ",\"y\":" << (int)(100.0*ptProjective.Y/480.0)
<< ",\"z\":" << (int)ptProjective.Z;
//cout << "move: " << ss.str() << endl;
send_event("Move", ss.str());
}
/**
* Handle destruction of an existing point
*/
void OnPointDestroy(XnUInt32 nID) {
printf("OnPointDestroy\n");
send_event("Unregister", "");
}
private:
xn::DepthGenerator m_DepthGenerator;
XnVSessionManager* m_SessionManager;
// XnVBroadcaster m_Broadcaster;
XnVPushDetector* m_pPushDetector;
XnVSwipeDetector* m_pSwipeDetector;
// XnVSteadyDetector* m_pSteadyDetector;
// XnVFlowRouter* m_pInnerFlowRouter;
//
// XnVWaveDetector* m_pWaveDetector;
XnVCircleDetector* m_pCircleDetector;
};
#endif