Fawkes API  Fawkes Development Version
usertracker_thread.h
00001 
00002 /***************************************************************************
00003  *  usertracker_thread.h - OpenNI user tracker thread
00004  *
00005  *  Created: Sun Feb 27 17:52:26 2011
00006  *  Copyright  2006-2011  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 #ifndef __PLUGINS_OPENNI_USERTRACKER_THREAD_H_
00024 #define __PLUGINS_OPENNI_USERTRACKER_THREAD_H_
00025 
00026 #include "utils/version.h"
00027 
00028 #include <core/threading/thread.h>
00029 #include <core/utils/lockptr.h>
00030 #include <aspect/logging.h>
00031 #include <aspect/configurable.h>
00032 #include <aspect/clock.h>
00033 #include <aspect/blackboard.h>
00034 #include <aspect/blocked_timing.h>
00035 #include <plugins/openni/aspect/openni.h>
00036 
00037 #include <XnCppWrapper.h>
00038 
00039 #include <map>
00040 
00041 namespace fawkes {
00042   class HumanSkeletonInterface;
00043   class HumanSkeletonProjectionInterface;
00044 }
00045 namespace firevision {
00046   class SharedMemoryImageBuffer;
00047 }
00048 
00049 class OpenNiUserTrackerThread
00050 : public fawkes::Thread,
00051   public fawkes::BlockedTimingAspect,
00052   public fawkes::LoggingAspect,
00053   public fawkes::ConfigurableAspect,
00054   public fawkes::ClockAspect,
00055   public fawkes::BlackBoardAspect,
00056   public fawkes::OpenNiAspect
00057 {
00058  public:
00059   OpenNiUserTrackerThread();
00060   virtual ~OpenNiUserTrackerThread();
00061 
00062   virtual void init();
00063   virtual void loop();
00064   virtual void finalize();
00065 
00066   void new_user(XnUserID id);
00067   void lost_user(XnUserID id);
00068   void pose_start(XnUserID id, const char *pose_name);
00069   void pose_end(XnUserID id, const char *pose_name);
00070   void calibration_start(XnUserID id);
00071   void calibration_end(XnUserID id, bool success);
00072 
00073  /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
00074  protected: virtual void run() { Thread::run(); }
00075 
00076  private:
00077   /** Per user info struct. */
00078   typedef struct {
00079     bool                                       valid;   /**< true if valid */
00080     fawkes::HumanSkeletonInterface            *skel_if; /**< Skeleton interface */
00081     fawkes::HumanSkeletonProjectionInterface  *proj_if; /**< Projection interface. */
00082   } UserInfo;
00083 
00084   typedef std::map<XnUserID, UserInfo>  UserMap;
00085 
00086   void update_user(XnUserID id, UserInfo &user);
00087   void update_com(XnUserID id, UserInfo &user);
00088 
00089  private:
00090   xn::UserGenerator               *__user_gen;
00091   xn::DepthGenerator              *__depth_gen;
00092 
00093   xn::SceneMetaData               *__scene_md;
00094   xn::SkeletonCapability          *__skelcap;
00095 
00096   XnCallbackHandle                 __user_cb_handle;
00097 #if XN_VERSION_GE(1,3,2,0)
00098   XnCallbackHandle                 __pose_start_cb_handle;
00099   XnCallbackHandle                 __pose_end_cb_handle;
00100   XnCallbackHandle                 __calib_start_cb_handle;
00101   XnCallbackHandle                 __calib_complete_cb_handle;
00102 #else
00103   XnCallbackHandle                 __pose_cb_handle;
00104   XnCallbackHandle                 __calib_cb_handle;
00105 #endif
00106 
00107   char                             __calib_pose_name[32];
00108   bool                             __skel_need_calib_pose;
00109 
00110   UserMap                          __users;
00111 
00112   firevision::SharedMemoryImageBuffer *__label_buf;
00113   size_t                               __label_bufsize;
00114 };
00115 
00116 #endif