Skip to content

Commit

Permalink
Changed toXn to use pointer casting (entirely valid as ofVec3f == XnP…
Browse files Browse the repository at this point in the history
…oint3D) for speed increase. Added getDepthRawPixels() and associated guff. This commit is a little more 'controversial'
  • Loading branch information
elliotwoods committed Oct 14, 2011
1 parent f872d1a commit 611ff90
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/ofxOpenNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ void ofxOpenNI::allocateDepthBuffers(){
}
}

//----------------------------------------
void ofxOpenNI::allocateDepthRawBuffers(){
if(g_bIsDepthRawOnOption){
depthRawPixels[0].allocate(640,480,OF_PIXELS_MONO);
depthRawPixels[1].allocate(640,480,OF_PIXELS_MONO);
currentDepthRawPixels = &depthRawPixels[0];
backDepthRawPixels = &depthRawPixels[1];
}
}

//----------------------------------------
void ofxOpenNI::allocateRGBBuffers(){
if(g_bIsImageOn){
Expand Down Expand Up @@ -164,6 +174,8 @@ void ofxOpenNI::openCommon(){
g_bIsIROn = false;
g_bIsAudioOn = false;
g_bIsPlayerOn = false;

g_bIsDepthRawOnOption = false;

NodeInfoList list;
nRetVal = g_Context.EnumerateExistingNodes(list);
Expand All @@ -180,6 +192,7 @@ void ofxOpenNI::openCommon(){
case XN_NODE_TYPE_DEPTH:
ofLogVerbose(LOG_NAME) << "Creating depth generator";
g_bIsDepthOn = true;
g_bIsDepthRawOnOption = true;
(*it).GetInstance(g_Depth);
break;
case XN_NODE_TYPE_IMAGE:
Expand Down Expand Up @@ -211,6 +224,7 @@ void ofxOpenNI::openCommon(){

initConstants();
allocateDepthBuffers();
allocateDepthRawBuffers();
allocateRGBBuffers();

readFrame();
Expand Down Expand Up @@ -319,14 +333,21 @@ void ofxOpenNI::readFrame(){
if(g_bIsDepthOn){
generateDepthPixels();
}

if(g_bIsImageOn){
generateImagePixels();
}

lock();
if(g_bIsDepthOn){
ofPixels * auxP = backDepthPixels;
backDepthPixels = currentDepthPixels;
currentDepthPixels = auxP;
if (g_bIsDepthRawOnOption) {
ofShortPixels * auxP = backDepthRawPixels;
backDepthRawPixels = currentDepthRawPixels;
currentDepthRawPixels = auxP;
}
}
if(g_bIsImageOn){
ofPixels * auxP = backRGBPixels;
Expand Down Expand Up @@ -460,6 +481,10 @@ void ofxOpenNI::generateDepthPixels(){

if (g_DepthMD.FrameID() == 0) return;

// copy raw values
if (g_bIsDepthRawOnOption)
backDepthRawPixels->setFromPixels(depth, 640, 480, 1);

// copy depth into texture-map
float max;
for (XnUInt16 y = g_DepthMD.YOffset(); y < g_DepthMD.YRes() + g_DepthMD.YOffset(); y++) {
Expand Down Expand Up @@ -661,6 +686,17 @@ ofPixels & ofxOpenNI::getDepthPixels(){
return *currentDepthPixels;
}

//----------------------------------------
ofShortPixels & ofxOpenNI::getDepthRawPixels(){
Poco::ScopedLock<ofMutex> lock(mutex);

if (!g_bIsDepthRawOnOption) {
ofLogWarning(LOG_NAME) << "g_bIsDepthRawOnOption was disabled, enabling raw pixels";
g_bIsDepthRawOnOption = true;
}
return *currentDepthRawPixels;
}

//----------------------------------------
ofPixels & ofxOpenNI::getRGBPixels(){
Poco::ScopedLock<ofMutex> lock(mutex);
Expand All @@ -686,6 +722,7 @@ float ofxOpenNI::getWidth(){
}else if(g_bIsIROn){
return g_irMD.XRes();
}else{
ofLogWarning(LOG_NAME) << "getWidth() : We haven't yet initialised any generators, so this value returned is returned as 0";
return 0;
}
}
Expand All @@ -699,6 +736,7 @@ float ofxOpenNI::getHeight(){
}else if(g_bIsIROn){
return g_irMD.YRes();
}else{
ofLogWarning(LOG_NAME) << "getHeight() : We haven't yet initialised any generators, so this value returned is returned as 0";
return 0;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/ofxOpenNI.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ofxOpenNI: public ofThread{
void setUseTexture(bool useTexture);

ofPixels & getDepthPixels();
ofShortPixels & getDepthRawPixels();
ofPixels & getRGBPixels();

ofTexture & getDepthTextureReference();
Expand Down Expand Up @@ -81,6 +82,7 @@ class ofxOpenNI: public ofThread{
void generateDepthPixels();
void generateImagePixels();
void allocateDepthBuffers();
void allocateDepthRawBuffers();
void allocateRGBBuffers();

static void XN_CALLBACK_TYPE onErrorStateChanged(XnStatus errorState, void* pCookie);
Expand Down Expand Up @@ -113,6 +115,8 @@ class ofxOpenNI: public ofThread{
bool g_bIsIROn;
bool g_bIsAudioOn;
bool g_bIsPlayerOn;

bool g_bIsDepthRawOnOption;

xn::Device g_Device;
xn::DepthGenerator g_Depth;
Expand Down Expand Up @@ -143,6 +147,10 @@ class ofxOpenNI: public ofThread{
ofPixels * backDepthPixels, * currentDepthPixels;
DepthColoring depth_coloring;
float max_depth;

// depth raw
ofShortPixels depthRawPixels[2];
ofShortPixels * backDepthRawPixels, * currentDepthRawPixels;

// rgb
ofTexture rgbTexture;
Expand Down
9 changes: 9 additions & 0 deletions src/ofxOpenNIUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,23 @@ void YUV422ToRGB888(const XnUInt8* pYUVImage, XnUInt8* pRGBImage, XnUInt32 nYUVS
ofLogNotice(LOG_NAME) << what << "status:" << xnGetStatusString(rc);

inline ofPoint toOf(const XnPoint3D & p){
return *(ofPoint*)&p;
/*
this is more future safe, but currently unnecessary and slower:
return ofPoint(p.X,p.Y,p.Z);
*/
}

inline XnPoint3D toXn(const ofPoint & p){

return *(XnPoint3D*)&p;
/*
this is more future safe, but currently unnecessary and slower:
XnPoint3D r;
r.X = p.x;
r.Y = p.y;
r.Z = p.z;
return r;
*/
}
#endif /* OFXOPENNIUTILS_H_ */

0 comments on commit 611ff90

Please sign in to comment.