changeset 3:7e112b536f0a

track moving of a hand
author kazz <kazz@cr.ie.u-ryukyu.ac.jp>
date Tue, 01 Feb 2011 06:37:06 +0900
parents 3b5465899da9
children edf80c055589
files KinectTrack.cc KinectTrack.h kinect.xml main.cc
diffstat 4 files changed, 137 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/KinectTrack.cc	Tue Feb 01 03:01:22 2011 +0900
+++ b/KinectTrack.cc	Tue Feb 01 06:37:06 2011 +0900
@@ -1,7 +1,10 @@
+#include <stdio.h>
 #include <XnVNite.h>
 
 #include "KinectTrack.h"
 
+#define N_COLORS 6
+
 XnVPointDrawer::XnVPointDrawer(XnUInt32 nHistory, xn::DepthGenerator depthGenerator) :
 	XnVPointControl("XnVPointDrawer"),
 	mNHistorySize(nHistory), mDepthGenerator(depthGenerator), mBDrawDM(false), mBFrameID(false) {
@@ -23,3 +26,119 @@
 	mBDrawDM = bDrawDM;
 }
 
+void XnVPointDrawer::Update(XnVMessage* pMessage) {
+	// PointControl's Update calls all callbacks for each hand
+	XnVPointControl::Update(pMessage);
+
+	if (mBDrawDM)
+	{
+		// Draw depth map
+		xn::DepthMetaData depthMD;
+		mDepthGenerator.GetMetaData(depthMD);
+		//drawDepthMap(depthMD);
+	}
+	if (mBFrameID)
+	{
+		// Print out frame ID
+		xn::DepthMetaData depthMD;
+		mDepthGenerator.GetMetaData(depthMD);
+		//DrawFrameID(depthMD.FrameID());
+	}
+
+	// Draw hands
+	Draw();
+}
+
+int XnVPointDrawer::getPosition(float &x, float &y, float &z) const {
+	std::map<XnUInt32, std::list<XnPoint3D> >::const_iterator itr;
+	itr = mHistory.begin();
+	if (itr == mHistory.end()) return -1;
+	std::list<XnPoint3D>::const_iterator itr2;
+	itr2 = itr->second.begin();
+	if (itr2 == itr->second.end()) return -1;
+	XnPoint3D pt(*itr2);
+	x = pt.X;
+	y = pt.Y;
+	z = pt.Z;
+	return 1;
+}
+void XnVPointDrawer::Draw() const {
+	std::map<XnUInt32, std::list<XnPoint3D> >::const_iterator itr;
+	itr = mHistory.begin();
+	if (itr == mHistory.end()) return;
+	std::list<XnPoint3D>::const_iterator itr2;
+	itr2 = itr->second.begin();
+	if (itr2 == itr->second.end()) return;
+	XnPoint3D pt(*itr2);
+//	printf("%f, %f, %f\n", pt.X, pt.Y, pt.Z);
+	
+
+	// std::map<XnUInt32, std::list<XnPoint3D> >::const_iterator PointIterator;
+	
+	// // Go over each existing hand
+	// for (PointIterator = mHistory.begin();
+	// 	PointIterator != mHistory.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);
+	// 		printf("%f, %f, %f\n", pt.X, pt.Y, pt.Z);
+	// 		mPFPositionBuffer[3*i] = pt.X;
+	// 		mPFPositionBuffer[3*i + 1] = pt.Y;
+	// 		mPFPositionBuffer[3*i + 2] = 0;//pt.Z();
+	// 		break;
+	// 	}
+		
+	// 	// Set color
+	// 	XnUInt32 nColor = Id % N_COLORS;
+	// 	//XnUInt32 nSingle = GetPrimaryID();
+	// 	if (Id == GetPrimaryID())
+	// 		nColor = 6;
+	// }
+}
+
+static XnBool bShouldPrint = false;
+void XnVPointDrawer::OnPointCreate(const XnVHandPointContext* cxt)
+{
+	printf("** %d\n", cxt->nID);
+	// Create entry for the hand
+	mHistory[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);
+	mDepthGenerator.ConvertRealWorldToProjective(1, &ptProjective, &ptProjective);
+	if (bShouldPrint)printf(" -> (%f,%f,%f)\n", ptProjective.X, ptProjective.Y, ptProjective.Z);
+
+	// Add new position to the history buffer
+	mHistory[cxt->nID].push_front(ptProjective);
+	// Keep size of history buffer
+	if (mHistory[cxt->nID].size() > mNHistorySize)
+		mHistory[cxt->nID].pop_back();
+	bShouldPrint = false;
+}
+
+// Handle destruction of an existing hand
+void XnVPointDrawer::OnPointDestroy(XnUInt32 nID)
+{
+	// No need for the history buffer
+	mHistory.erase(nID);
+}
--- a/KinectTrack.h	Tue Feb 01 03:01:22 2011 +0900
+++ b/KinectTrack.h	Tue Feb 01 06:37:06 2011 +0900
@@ -24,13 +24,14 @@
 public:
 	XnVPointDrawer(XnUInt32 nHistorySize, xn::DepthGenerator depthGenerator);
 	virtual ~XnVPointDrawer();
-	void update(XnVMessage *pMessage);
-	void onPointCreate(const XnVHandPointContext *cxt);
-	void onPointUpdate(const XnVHandPointContext *cxt);
-	void onPointDestroy(XnUInt32 nID);
-	void draw() const;
+	void Update(XnVMessage *pMessage);
+	void OnPointCreate(const XnVHandPointContext *cxt);
+	void OnPointUpdate(const XnVHandPointContext *cxt);
+	void OnPointDestroy(XnUInt32 nID);
+	void Draw() const;
 	void setDepthMap(XnBool bDrawDM);
 	void setFrameID(XnBool bFrameID);
+	int getPosition(float &x, float &y, float &z) const;
 protected:
 	XnUInt32 mNHistorySize;
 	std::map<XnUInt32, std::list<XnPoint3D> > mHistory;
--- a/kinect.xml	Tue Feb 01 03:01:22 2011 +0900
+++ b/kinect.xml	Tue Feb 01 06:37:06 2011 +0900
@@ -1,6 +1,6 @@
 <OpenNI>
         <Licenses>
-                <License vendor="PrimeSense" key="insert key here"/>
+                <License vendor="PrimeSense" key="0KOIk2JeIBYClPWVnMoRKn5cdY4="/>
         </Licenses>
         <Log writeToConsole="true" writeToFile="false">
                 <!-- 0 - Verbose, 1 - Info, 2 - Warning, 3 - Error (default) -->
--- a/main.cc	Tue Feb 01 03:01:22 2011 +0900
+++ b/main.cc	Tue Feb 01 06:37:06 2011 +0900
@@ -24,7 +24,7 @@
 SessionState NIState::gSessionState = NOT_IN_SESSION;
 XnBool NIState::gBDrawDepthMap = true;
 void NIState::XN_CALLBACK_TYPE sessionStarting(const XnPoint3D &ptPosition, void *userCxt) {
-	printf("Session start: (%f, %f, %f)\n)", ptPosition.X,  ptPosition.Y,  ptPosition.Z);
+	printf("Session start: (%f, %f, %f)\n", ptPosition.X,  ptPosition.Y,  ptPosition.Z);
 	gSessionState = IN_SESSION;
 }
 void NIState::XN_CALLBACK_TYPE sessionEnding(void *userCxt) {
@@ -33,7 +33,7 @@
 }
 void NIState::XN_CALLBACK_TYPE focusProgress(const XnChar *strFocus, const XnPoint3D &ptPosition,
 										   XnFloat fProgress, void *userCxt) {
-	//printf("Focus progress: %s @(%f, %f, %f): %f\n)", strFocus, ptPosition.X,  ptPosition.Y,  ptPosition.Z, fProgress);
+	printf("Focus progress: %s @(%f, %f, %f): %f\n", strFocus, ptPosition.X,  ptPosition.Y,  ptPosition.Z, fProgress);
 }
 void NIState::XN_CALLBACK_TYPE noHands(void *UserCxt) {
 	if (gSessionState != NOT_IN_SESSION) {
@@ -75,7 +75,15 @@
 	checkRC(rc, "StartGenerating");
 
 	while (true) {
-		usleep(100);
+		gContext.WaitAndUpdateAll();
+		gPSessionManager->Update(&gContext);
+		float x, y, z;
+		int error = gPDrawer->getPosition(x, y, z);
+		if (error > 0) {
+			printf("%f, %f, %f\n", x, y, z);
+		}
+		
+		//usleep(10);
 	}
 
 	return 0;