23 #include "handtracker_thread.h" 24 #include "utils/setup.h" 25 #include "utils/conversions.h" 27 #include <core/threading/mutex_locker.h> 28 #include <interfaces/ObjectPositionInterface.h> 44 :
Thread(
"OpenNiHandTrackerThread",
Thread::OPMODE_WAITFORWAKEUP),
56 static void XN_CALLBACK_TYPE
57 cb_hand_create(xn::HandsGenerator &generator, XnUserID user,
58 const XnPoint3D *position, XnFloat time,
void *cookie)
64 static void XN_CALLBACK_TYPE
65 cb_hand_destroy(xn::HandsGenerator &generator, XnUserID user,
66 XnFloat time,
void *cookie)
72 static void XN_CALLBACK_TYPE
73 cb_hand_update(xn::HandsGenerator &generator, XnUserID user,
74 const XnPoint3D *position, XnFloat time,
void *cookie)
81 static void XN_CALLBACK_TYPE
82 cb_gesture_recognized(xn::GestureGenerator &generator,
83 const XnChar *gesture_name,
const XnPoint3D *position,
84 const XnPoint3D *end_position,
void *cookie)
90 static void XN_CALLBACK_TYPE
91 cb_gesture_progress(xn::GestureGenerator &generator,
92 const XnChar *gesture_name,
const XnPoint3D *position,
93 XnFloat progress,
void *cookie)
105 __hand_gen =
new xn::HandsGenerator();
106 #if __cplusplus >= 201103L 107 std::unique_ptr<xn::HandsGenerator> handgen_uniqueptr(__hand_gen);
108 std::unique_ptr<xn::GestureGenerator> gesturegen_uniqueptr(__gesture_gen);
109 std::unique_ptr<xn::DepthGenerator> depthgen_uniqueptr(__depth_gen);
111 std::auto_ptr<xn::HandsGenerator> handgen_uniqueptr(__hand_gen);
112 std::auto_ptr<xn::GestureGenerator> gesturegen_uniqueptr(__gesture_gen);
113 std::auto_ptr<xn::DepthGenerator> depthgen_uniqueptr(__depth_gen);
116 __gesture_gen =
new xn::GestureGenerator();
117 __depth_gen =
new xn::DepthGenerator();
121 fawkes::openni::get_resolution(
config, __width, __height);
123 fawkes::openni::find_or_create_node(
openni, XN_NODE_TYPE_HANDS, __hand_gen);
124 fawkes::openni::find_or_create_node(
openni, XN_NODE_TYPE_DEPTH, __depth_gen);
126 fawkes::openni::find_or_create_node(
openni, XN_NODE_TYPE_GESTURE, __gesture_gen);
128 st = __hand_gen->RegisterHandCallbacks(cb_hand_create, cb_hand_update,
129 cb_hand_destroy,
this, __hand_cb_handle);
130 if (st != XN_STATUS_OK) {
131 throw Exception(
"Failed to register hand callbacks (%s)",
132 xnGetStatusString(st));
135 st = __gesture_gen->RegisterGestureCallbacks(cb_gesture_recognized,
137 this, __gesture_cb_handle);
138 if (st != XN_STATUS_OK) {
139 throw Exception(
"Failed to register gesture callbacks (%s)",
140 xnGetStatusString(st));
145 for (
unsigned int i = 0; i < num_g; ++i) {
146 gest[i] =
new XnChar[64];
148 if ((st = __gesture_gen->EnumerateAllGestures(gest, 64, num_g)) != XN_STATUS_OK)
151 xnGetStatusString(st));
153 for (
unsigned int i = 0; i < num_g; ++i) {
158 for (
unsigned int i = 0; i < num_g; ++i) {
163 __gesture_gen->AddGesture(
"Wave", NULL);
164 __enabled_gesture[
"Wave"] =
true;
166 __gesture_gen->AddGesture(
"Click", NULL);
167 __enabled_gesture[
"Click"] =
true;
169 __hand_gen->StartGenerating();
170 __gesture_gen->StartGenerating();
182 handgen_uniqueptr.release();
183 depthgen_uniqueptr.release();
184 gesturegen_uniqueptr.release();
192 for (i = __hands.begin(); i != __hands.end(); ++i) {
193 __hand_gen->StopTracking(i->first);
194 i->second->set_visible(
false);
195 i->second->set_valid(
false);
201 std::map<std::string, bool>::iterator g;
202 for (g = __enabled_gesture.begin(); g != __enabled_gesture.end(); ++g) {
204 __gesture_gen->RemoveGesture(g->first.c_str());
211 delete __gesture_gen;
218 if (! __hand_gen->IsDataNew())
return;
221 for (i = __hands.begin(); i != __hands.end(); ++i) {
222 if (__needs_write[i->first]) {
224 __needs_write[i->first] =
false;
230 OpenNiHandTrackerThread::update_hand(XnUserID &user,
const XnPoint3D *position)
233 __hands[user]->set_visible(
true);
234 __hands[user]->set_relative_x( position->Z * 0.001);
235 __hands[user]->set_relative_y(-position->X * 0.001);
236 __hands[user]->set_relative_z( position->Y * 0.001);
239 fawkes::openni::world2projection(__depth_gen, 1, position, &proj,
241 __hands[user]->set_world_x(proj.X);
242 __hands[user]->set_world_y(proj.Y);
243 __hands[user]->set_world_z(user);
245 __needs_write[user] =
true;
263 if (__hands.find(user) != __hands.end()) {
269 if (asprintf(&ifid,
"OpenNI Hand %u", user) == -1) {
271 "interface ID", user);
278 update_hand(user, position);
297 if (__hands.find(user) == __hands.end()) {
302 update_hand(user, position);
314 if (__hands.find(user) == __hands.end()) {
321 __hands[user]->set_visible(
false);
322 __hands[user]->write();
325 user, __hands[user]->uid());
328 __needs_write.erase(user);
331 std::map<std::string, bool>::iterator i;
332 for (i = __enabled_gesture.begin(); i != __enabled_gesture.end(); ++i) {
336 __gesture_gen->AddGesture(i->first.c_str(), NULL);
349 const XnPoint3D *position,
350 const XnPoint3D *end_position)
355 std::map<std::string, bool>::iterator i;
356 for (i = __enabled_gesture.begin(); i != __enabled_gesture.end(); ++i) {
360 __gesture_gen->RemoveGesture(i->first.c_str());
363 __hand_gen->StartTracking(*end_position);
374 const XnPoint3D *position,
LockPtr< xn::Context > openni
Central OpenNI context.
virtual void finalize()
Finalize the thread.
OpenNiHandTrackerThread()
Constructor.
ObjectPositionInterface Fawkes BlackBoard Interface.
virtual void init()
Initialize the thread.
Fawkes library namespace.
virtual ~OpenNiHandTrackerThread()
Destructor.
OpenNI Hand Tracker Thread.
Thread class encapsulation of pthreads.
void gesture_progress(const XnChar *gesture_name, const XnPoint3D *position, XnFloat progress)
Notify of gesture progress.
Logger * logger
This is the Logger member used to access the logger.
Thread aspect to use blocked timing.
Base class for exceptions in Fawkes.
Mutex * objmutex_ptr() const
Get object mutex.
void gesture_recognized(const XnChar *gesture_name, const XnPoint3D *position, const XnPoint3D *end_position)
Notify of recognized gesture.
const char * name() const
Get name of thread.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
virtual void loop()
Code to execute in the thread.
void hand_destroy(XnUserID &user, XnFloat &time)
Notify of disappeared hand.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
void hand_create(XnUserID &user, const XnPoint3D *position, XnFloat &time)
Notify of new hand.
Configuration * config
This is the Configuration member used to access the configuration.
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for writing.
void hand_update(XnUserID &user, const XnPoint3D *position, XnFloat &time)
Notify of hand update.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
virtual void close(Interface *interface)=0
Close interface.