KINECT STATS GENERATOR FOR SPORTS VISUALISATION
1.0
|
00001 #include "Kinect.h" 00002 #include "Mutex.h" 00003 #include <cmath> 00004 #include <iostream> 00005 /* thanks to Yoda---- from IRC */ 00006 Kinect::Kinect(freenect_context *_ctx, int _index) :FreenectDevice(_ctx, _index) 00007 { 00008 00009 m_buffer_depth.resize(FREENECT_VIDEO_RGB_SIZE); 00010 m_buffer_video.resize(FREENECT_VIDEO_RGB_SIZE); 00011 m_gamma.resize(2048); 00012 m_new_rgb_frame=false; 00013 m_new_depth_frame=false; 00014 00015 for( unsigned int i = 0 ; i < 2048 ; i++) 00016 { 00017 float v = i/2048.0; 00018 v = std::pow(v, 3)* 6; 00019 m_gamma[i] = v*6*256; 00020 } 00021 00022 //m_device = &freenect.createDevice(0); 00023 //m_device->startVideo(); 00024 //m_device->startDepth(); 00025 00026 } 00027 00028 // Do not call directly even in child 00029 void Kinect::VideoCallback(void* _rgb, uint32_t timestamp) 00030 { 00031 // std::cout << "RGB callback" << std::endl; 00032 m_rgb_mutex.lock(); 00033 uint8_t* rgb = static_cast<uint8_t*>(_rgb); 00034 std::copy(rgb, rgb+FREENECT_VIDEO_RGB_SIZE, m_buffer_video.begin()); 00035 m_new_rgb_frame = true; 00036 m_rgb_mutex.unlock(); 00037 }; 00038 // Do not call directly even in child 00039 void Kinect::DepthCallback(void* _depth, uint32_t timestamp) 00040 { 00041 // std::cout << "Depth callback" << std::endl; 00042 m_depth_mutex.lock(); 00043 uint16_t* depth = static_cast<uint16_t*>(_depth); 00044 for( unsigned int i = 0 ; i < FREENECT_FRAME_PIX ; i++) { 00045 int pval = m_gamma[depth[i]]; 00046 int lb = pval & 0xff; 00047 switch (pval>>8) { 00048 case 0: 00049 m_buffer_depth[3*i+0] = 255; 00050 m_buffer_depth[3*i+1] = 255-lb; 00051 m_buffer_depth[3*i+2] = 255-lb; 00052 break; 00053 case 1: 00054 m_buffer_depth[3*i+0] = 255; 00055 m_buffer_depth[3*i+1] = lb; 00056 m_buffer_depth[3*i+2] = 0; 00057 break; 00058 case 2: 00059 m_buffer_depth[3*i+0] = 255-lb; 00060 m_buffer_depth[3*i+1] = 255; 00061 m_buffer_depth[3*i+2] = 0; 00062 break; 00063 case 3: 00064 m_buffer_depth[3*i+0] = 0; 00065 m_buffer_depth[3*i+1] = 255; 00066 m_buffer_depth[3*i+2] = lb; 00067 break; 00068 case 4: 00069 m_buffer_depth[3*i+0] = 0; 00070 m_buffer_depth[3*i+1] = 255-lb; 00071 m_buffer_depth[3*i+2] = 255; 00072 break; 00073 case 5: 00074 m_buffer_depth[3*i+0] = 0; 00075 m_buffer_depth[3*i+1] = 0; 00076 m_buffer_depth[3*i+2] = 255-lb; 00077 break; 00078 default: 00079 m_buffer_depth[3*i+0] = 0; 00080 m_buffer_depth[3*i+1] = 0; 00081 m_buffer_depth[3*i+2] = 0; 00082 break; 00083 } 00084 } 00085 m_new_depth_frame = true; 00086 m_depth_mutex.unlock(); 00087 } 00088 00089 bool Kinect::getRGB(std::vector<uint8_t> &buffer) 00090 { 00091 m_rgb_mutex.lock(); 00092 if(m_new_rgb_frame) { 00093 buffer.swap(m_buffer_video); 00094 m_new_rgb_frame = false; 00095 m_rgb_mutex.unlock(); 00096 return true; 00097 } else { 00098 m_rgb_mutex.unlock(); 00099 return false; 00100 } 00101 } 00102 00103 bool Kinect::getDepth(std::vector<uint8_t> &buffer) 00104 { 00105 m_depth_mutex.lock(); 00106 if(m_new_depth_frame) { 00107 buffer.swap(m_buffer_depth); 00108 m_new_depth_frame = false; 00109 m_depth_mutex.unlock(); 00110 return true; 00111 } else { 00112 m_depth_mutex.unlock(); 00113 return false; 00114 } 00115 } 00116 00117 void Kinect::setAngle(double _angle) 00118 { 00119 if(_angle > 30) 00120 { 00121 _angle = 30; 00122 } 00123 else if(_angle <-30) 00124 { 00125 _angle=-30; 00126 } 00127 this->setTiltDegrees(_angle); 00128 00129 } 00130 void Kinect::setRedLed() 00131 { 00132 this->setLed(LED_RED); 00133 } 00134 void Kinect::setGreenLed() 00135 { 00136 this->setLed(LED_GREEN); 00137 } 00138 void Kinect::setYellowLed() 00139 { 00140 this->setLed(LED_YELLOW); 00141 } 00142 void Kinect::setRedLedFlash() 00143 { 00144 this->setLed(LED_BLINK_RED_YELLOW); 00145 } 00146 void Kinect::setGreenLedFlash() 00147 { 00148 this->setLed(LED_BLINK_GREEN); 00149 00150 } 00151 void Kinect::setYellowLedFlash() 00152 { 00153 this->setLed(LED_BLINK_YELLOW); 00154 00155 } 00156 void Kinect::setVideoMode(int _mode) 00157 { 00158 00159 } 00160 void Kinect::resetAngle() 00161 { 00162 this->setTiltDegrees(0); 00163 }