00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00035 #include "Depth.h"
00036 #include "User.h"
00037 #include "../core/Exception.h"
00038 #include "../DeviceManager.h"
00039
00040 namespace NIDevice
00041 {
00042 namespace Generator
00043 {
00044 Depth::Depth(NIDevice::Objects::KinectDevice* pDevice) : NIDevice::Core::GenericGenerator<xn::DepthGenerator, XN_NODE_TYPE_DEPTH>(pDevice)
00045 {
00046 iDistCutoff = 10000;
00047 fAngCutoff = 0.0f;
00048 SetOutputMode(XN_VGA_X_RES, XN_VGA_Y_RES, 30);
00049 depthData = (unsigned short*)malloc(XN_VGA_X_RES * XN_VGA_Y_RES * sizeof(XnDepthPixel));
00050 memset(depthData, 0, XN_VGA_X_RES * XN_VGA_Y_RES * sizeof(XnDepthPixel));
00051 }
00052
00053 Depth::Depth() : NIDevice::Core::GenericGenerator<xn::DepthGenerator, XN_NODE_TYPE_DEPTH>(0)
00054 {
00055 }
00056
00057 Depth::~Depth()
00058 {
00059 if (depthData)
00060 free(depthData);
00061 }
00062
00063 int Depth::SetOutputMode(int xRes, int yRes, int nFps)
00064 {
00065 XnMapOutputMode map;
00066 map.nXRes = xRes;
00067 map.nYRes = yRes;
00068 map.nFPS = nFps;
00069 nRetVal = GetGenerator().SetMapOutputMode(map);
00070 if (nRetVal != XN_STATUS_OK)
00071 {
00072
00073 KINECT_EXCEPT(NIDevice::Core::ExceptionKinect, GetNiDeviceMessage(), GetObjectTypeAsString());
00074 return nRetVal;
00075 }
00076
00077 return XN_STATUS_OK;
00078 }
00079
00080 bool Depth::UpdateData()
00081 {
00082 if (GetGenerator().IsValid())
00083 {
00084 GetGenerator().GetMetaData(g_depthMD);
00085 if (g_depthMD.IsDataNew())
00086 {
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 DepthCutoff();
00101 memcpy(depthData, g_depthMD.Data(), g_depthMD.XRes() * g_depthMD.YRes() * sizeof(XnDepthPixel));
00102 return true;
00103 }
00104 }
00105 return false;
00106 }
00107
00108 const unsigned short* Depth::GetDepthBuffer(bool useMutex)
00109 {
00110 if (useMutex)
00111 boost::mutex::scoped_lock lock(GetParentDevice()->m_mutex);
00112
00113 if (UpdateData())
00114 {
00115 xnOSMemSet(g_pDepthHist, 0, MAX_DEPTH*sizeof(float));
00116 int nNumberOfPoints = 0;
00117
00118 XnDepthPixel nValue;
00119
00120 xn::DepthGenerator pDepthGen = GetGenerator();
00121
00122 XnUInt64 nTimeStamp = pDepthGen.GetTimestamp();
00123 XnUInt32 nDataSize = pDepthGen.GetDataSize();
00124 const XnDepthPixel* pDepth = pDepthGen.GetDepthMap();
00125 const XnDepthPixel* pDepthEnd = pDepth + (nDataSize / sizeof(XnDepthPixel));
00126
00127 while (pDepth != pDepthEnd)
00128 {
00129 nValue = *pDepth;
00130
00131 XN_ASSERT(nValue <= MAX_DEPTH);
00132
00133 if (nValue != 0)
00134 {
00135 g_pDepthHist[nValue]++;
00136 nNumberOfPoints++;
00137 }
00138
00139 pDepth++;
00140 }
00141
00142 XnUInt32 nIndex;
00143 for (nIndex=1; nIndex<MAX_DEPTH; nIndex++)
00144 {
00145 g_pDepthHist[nIndex] += g_pDepthHist[nIndex-1];
00146 }
00147 for (nIndex=1; nIndex<MAX_DEPTH; nIndex++)
00148 {
00149 if (g_pDepthHist[nIndex] != 0)
00150 {
00151 g_pDepthHist[nIndex] = (nNumberOfPoints-g_pDepthHist[nIndex]) / nNumberOfPoints;
00152 }
00153 }
00154 }
00155 return depthData;
00156 }
00157
00158 float* Depth::GetHistory()
00159 {
00160 return g_pDepthHist;
00161 }
00162
00163 void Depth::GetDepthSize(int &w, int &h)
00164 {
00165 width = g_depthMD.XRes();
00166 height = g_depthMD.YRes();
00167 w = width;
00168 h = height;
00169 }
00170
00171 void Depth::SetDistCutoff(int dist)
00172 {
00173 iDistCutoff = dist;
00174 }
00175
00176 int Depth::GetDistCutoff()
00177 {
00178 return iDistCutoff;
00179 }
00180
00181 void Depth::SetAngCutoff(float ang)
00182 {
00183 fAngCutoff = ang;
00184 }
00185
00186 float Depth::GetAngCutoff()
00187 {
00188 return fAngCutoff;
00189 }
00190
00191 void Depth::DepthCutoff()
00192 {
00193 XnDepthPixel val2Replace = 0;
00194 XnDepthPixel* depth = g_depthMD.WritableData();
00195 int xcut = g_depthMD.XRes();
00196
00197
00198 if ((fAngCutoff > 0.0f) && (fAngCutoff < 57.7f))
00199 xcut = (g_depthMD.XRes() / 57.8) * fAngCutoff;
00200
00201 int minxcut = (g_depthMD.XRes() / 2) - (xcut / 2);
00202 int maxxcut = (g_depthMD.XRes() / 2) + (xcut / 2);
00203 for(int y(0); y < g_depthMD.YRes(); ++y)
00204 {
00205 for(int x(0); x < g_depthMD.XRes(); ++x)
00206 {
00207 int pos = x+y*g_depthMD.XRes();
00208
00209 if((x < minxcut) || (x > maxxcut) || (iDistCutoff < depth[pos]))
00210 depth[pos] = val2Replace;
00211 }
00212 }
00213 }
00214 }
00215 }
00216