This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
PointDrawer.cpp
397 lines (340 loc) · 10 KB
/
PointDrawer.cpp
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
//added for OSX compilation
#define USE_GLUT
/****************************************************************************
* *
* Nite 1.3 - Point Viewer *
* *
* Author: Oz Magal *
* *
****************************************************************************/
/****************************************************************************
* *
* Nite 1.3 *
* Copyright (C) 2006 PrimeSense Ltd. All Rights Reserved. *
* *
* This file has been provided pursuant to a License Agreement containing *
* restrictions on its use. This data contains valuable trade secrets *
* and proprietary information of PrimeSense Ltd. and is protected by law. *
* *
****************************************************************************/
#include "PointDrawer.h"
#include "XnVDepthMessage.h"
#include <XnVHandPointContext.h>
#ifdef USE_GLUT
#if (XN_PLATFORM == XN_PLATFORM_MACOSX)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#else
#include "opengles.h"
#endif
// Constructor. Receives the number of previous positions to store per hand,
// and a source for depth map
XnVPointDrawer::XnVPointDrawer(XnUInt32 nHistory, xn::DepthGenerator depthGenerator) :
XnVPointControl("XnVPointDrawer"),
m_nHistorySize(nHistory), m_DepthGenerator(depthGenerator), m_bDrawDM(false), m_bFrameID(false)
{
m_pfPositionBuffer = new XnFloat[nHistory*3];
}
// Destructor. Clear all data structures
XnVPointDrawer::~XnVPointDrawer()
{
std::map<XnUInt32, std::list<XnPoint3D> >::iterator iter;
for (iter = m_History.begin(); iter != m_History.end(); ++iter)
{
iter->second.clear();
}
m_History.clear();
delete []m_pfPositionBuffer;
}
// Change whether or not to draw the depth map
void XnVPointDrawer::SetDepthMap(XnBool bDrawDM)
{
m_bDrawDM = bDrawDM;
}
// Change whether or not to print the frame ID
void XnVPointDrawer::SetFrameID(XnBool bFrameID)
{
m_bFrameID = bFrameID;
}
// Handle creation of a new hand
static XnBool bShouldPrint = false;
void XnVPointDrawer::OnPointCreate(const XnVHandPointContext* cxt)
{
printf("** %d\n", cxt->nID);
// Create entry for the hand
m_History[cxt->nID].clear();
bShouldPrint = true;
OnPointUpdate(cxt);
bShouldPrint = true;
}
// Handle new position of an existing hand
void XnVPointDrawer::OnPointUpdate(const XnVHandPointContext* cxt)
{
// positions are kept in projective coordinates, since they are only used for drawing
XnPoint3D ptProjective(cxt->ptPosition);
if (bShouldPrint)printf("Point (%f,%f,%f)", ptProjective.X, ptProjective.Y, ptProjective.Z);
m_DepthGenerator.ConvertRealWorldToProjective(1, &ptProjective, &ptProjective);
if (bShouldPrint)printf(" -> (%f,%f,%f)\n", ptProjective.X, ptProjective.Y, ptProjective.Z);
// Add new position to the history buffer
m_History[cxt->nID].push_front(ptProjective);
// Keep size of history buffer
if (m_History[cxt->nID].size() > m_nHistorySize)
m_History[cxt->nID].pop_back();
bShouldPrint = false;
}
// Handle destruction of an existing hand
void XnVPointDrawer::OnPointDestroy(XnUInt32 nID)
{
// No need for the history buffer
m_History.erase(nID);
}
#define MAX_DEPTH 10000
float g_pDepthHist[MAX_DEPTH];
unsigned int getClosestPowerOfTwo(unsigned int n)
{
unsigned int m = 2;
while(m < n) m<<=1;
return m;
}
GLuint initTexture(void** buf, int& width, int& height)
{
GLuint texID = 0;
glGenTextures(1,&texID);
width = getClosestPowerOfTwo(width);
height = getClosestPowerOfTwo(height);
*buf = new unsigned char[width*height*4];
glBindTexture(GL_TEXTURE_2D,texID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return texID;
}
GLfloat texcoords[8];
void DrawRectangle(float topLeftX, float topLeftY, float bottomRightX, float bottomRightY)
{
GLfloat verts[8] = { topLeftX, topLeftY,
topLeftX, bottomRightY,
bottomRightX, bottomRightY,
bottomRightX, topLeftY
};
glVertexPointer(2, GL_FLOAT, 0, verts);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glFlush();
}
void DrawTexture(float topLeftX, float topLeftY, float bottomRightX, float bottomRightY)
{
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
DrawRectangle(topLeftX, topLeftY, bottomRightX, bottomRightY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
void DrawDepthMap(const xn::DepthMetaData& dm)
{
static bool bInitialized = false;
static GLuint depthTexID;
static unsigned char* pDepthTexBuf;
static int texWidth, texHeight;
float topLeftX;
float topLeftY;
float bottomRightY;
float bottomRightX;
float texXpos;
float texYpos;
if(!bInitialized)
{
XnUInt16 nXRes = dm.XRes();
XnUInt16 nYRes = dm.YRes();
texWidth = getClosestPowerOfTwo(nXRes);
texHeight = getClosestPowerOfTwo(nYRes);
depthTexID = initTexture((void**)&pDepthTexBuf,texWidth, texHeight) ;
bInitialized = true;
topLeftX = nXRes;
topLeftY = 0;
bottomRightY = nYRes;
bottomRightX = 0;
texXpos =(float)nXRes/texWidth;
texYpos =(float)nYRes/texHeight;
memset(texcoords, 0, 8*sizeof(float));
texcoords[0] = texXpos, texcoords[1] = texYpos, texcoords[2] = texXpos, texcoords[7] = texYpos;
}
unsigned int nValue = 0;
unsigned int nHistValue = 0;
unsigned int nIndex = 0;
unsigned int nX = 0;
unsigned int nY = 0;
unsigned int nNumberOfPoints = 0;
XnUInt16 g_nXRes = dm.XRes();
XnUInt16 g_nYRes = dm.YRes();
unsigned char* pDestImage = pDepthTexBuf;
const XnUInt16* pDepth = dm.Data();
// Calculate the accumulative histogram
memset(g_pDepthHist, 0, MAX_DEPTH*sizeof(float));
for (nY=0; nY<g_nYRes; nY++)
{
for (nX=0; nX<g_nXRes; nX++)
{
nValue = *pDepth;
if (nValue != 0)
{
g_pDepthHist[nValue]++;
nNumberOfPoints++;
}
pDepth++;
}
}
for (nIndex=1; nIndex<MAX_DEPTH; nIndex++)
{
g_pDepthHist[nIndex] += g_pDepthHist[nIndex-1];
}
if (nNumberOfPoints)
{
for (nIndex=1; nIndex<MAX_DEPTH; nIndex++)
{
g_pDepthHist[nIndex] = (unsigned int)(256 * (1.0f - (g_pDepthHist[nIndex] / nNumberOfPoints)));
}
}
pDepth = dm.Data();
{
XnUInt32 nIndex = 0;
// Prepare the texture map
for (nY=0; nY<g_nYRes; nY++)
{
for (nX=0; nX < g_nXRes; nX++, nIndex++)
{
nValue = *pDepth;
if (nValue != 0)
{
nHistValue = g_pDepthHist[nValue];
pDestImage[0] = nHistValue;
pDestImage[1] = nHistValue;
pDestImage[2] = nHistValue;
}
else
{
pDestImage[0] = 0;
pDestImage[1] = 0;
pDestImage[2] = 0;
}
pDepth++;
pDestImage+=3;
}
pDestImage += (texWidth - g_nXRes) *3;
}
}
glBindTexture(GL_TEXTURE_2D, depthTexID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, pDepthTexBuf);
// Display the OpenGL texture map
glColor4f(0.5,0.5,0.5,1);
glEnable(GL_TEXTURE_2D);
DrawTexture(dm.XRes(),dm.YRes(),0,0);
glDisable(GL_TEXTURE_2D);
}
void glPrintString(void *font, char *str)
{
int i,l = strlen(str);
for(i=0; i<l; i++)
{
glutBitmapCharacter(font,*str++);
}
}
void DrawFrameID(XnUInt32 nFrameID)
{
glColor4f(1,0,0,1);
glRasterPos2i(20, 50);
XnChar strLabel[20];
sprintf(strLabel, "%d", nFrameID);
glPrintString(GLUT_BITMAP_HELVETICA_18, strLabel);
}
// Colors for the points
XnFloat Colors[][3] =
{
{1,0,0}, // Red
{0,1,0}, // Green
{0,0.5,1}, // Light blue
{1,1,0}, // Yellow
{1,0.5,0}, // Orange
{1,0,1}, // Purple
{1,1,1} // White. reserved for the primary point
};
XnUInt32 nColors = 6;
void XnVPointDrawer::Draw() const
{
std::map<XnUInt32, std::list<XnPoint3D> >::const_iterator PointIterator;
// Go over each existing hand
for (PointIterator = m_History.begin();
PointIterator != m_History.end();
++PointIterator)
{
// Clear buffer
XnUInt32 nPoints = 0;
XnUInt32 i = 0;
XnUInt32 Id = PointIterator->first;
// Go over all previous positions of current hand
std::list<XnPoint3D>::const_iterator PositionIterator;
for (PositionIterator = PointIterator->second.begin();
PositionIterator != PointIterator->second.end();
++PositionIterator, ++i)
{
// Add position to buffer
XnPoint3D pt(*PositionIterator);
m_pfPositionBuffer[3*i] = pt.X;
m_pfPositionBuffer[3*i + 1] = pt.Y;
m_pfPositionBuffer[3*i + 2] = 0;//pt.Z();
}
// Set color
XnUInt32 nColor = Id % nColors;
XnUInt32 nSingle = GetPrimaryID();
if (Id == GetPrimaryID())
nColor = 6;
// Draw buffer:
glColor4f(Colors[nColor][0],
Colors[nColor][1],
Colors[nColor][2],
1.0f);
glPointSize(2);
glVertexPointer(3, GL_FLOAT, 0, m_pfPositionBuffer);
glDrawArrays(GL_LINE_STRIP, 0, i);
glPointSize(8);
glDrawArrays(GL_POINTS, 0, 1);
glFlush();
}
}
// Handle a new Message
void XnVPointDrawer::Update(XnVMessage* pMessage)
{
// PointControl's Update calls all callbacks for each hand
XnVPointControl::Update(pMessage);
if (m_bDrawDM)
{
// Draw depth map
xn::DepthMetaData depthMD;
m_DepthGenerator.GetMetaData(depthMD);
DrawDepthMap(depthMD);
}
if (m_bFrameID)
{
// Print out frame ID
xn::DepthMetaData depthMD;
m_DepthGenerator.GetMetaData(depthMD);
DrawFrameID(depthMD.FrameID());
}
// Draw hands
Draw();
}
void PrintSessionState(SessionState eState)
{
glColor4f(1,0,1,1);
glRasterPos2i(20, 20);
XnChar strLabel[200];
switch (eState)
{
case IN_SESSION:
sprintf(strLabel, "Tracking hands"); break;
case NOT_IN_SESSION:
sprintf(strLabel, "Perform click or wave gestures to track hand"); break;
case QUICK_REFOCUS:
sprintf(strLabel, "Raise your hand for it to be identified, or perform click or wave gestures"); break;
}
glPrintString(GLUT_BITMAP_HELVETICA_18, strLabel);
}