diff --git a/src/ofxBase3DVideo.h b/src/ofxBase3DVideo.h new file mode 100644 index 0000000..3f2922d --- /dev/null +++ b/src/ofxBase3DVideo.h @@ -0,0 +1,41 @@ +/*============================================================================== + + Copyright (c) 2010, 2011 ofxKinect Team + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +==============================================================================*/ +#pragma once + +/// \class ofxBase3DVideo +/// +/// a base class for 3D video devices +class ofxBase3DVideo: public ofBaseVideo { + +public: + + /// get the pixels of the most recent depth frame + virtual unsigned char* getDepthPixels()=0; + + /// get the distance in millimeters to a given point as a float array + virtual float* getDistancePixels()=0; + + /// get the grayscale depth texture + virtual ofTexture& getDepthTextureReference()=0; +}; diff --git a/src/ofxOpenNI.cpp b/src/ofxOpenNI.cpp index b1eab42..969bf74 100644 --- a/src/ofxOpenNI.cpp +++ b/src/ofxOpenNI.cpp @@ -366,6 +366,7 @@ void ofxOpenNI::stop(){ depthTexture.clear(); depthPixels[0].clear(); depthPixels[1].clear(); + distancePixels.clear(); cout << LOG_NAME << ": releasing image texture & pixels" << endl; imageTexture.clear(); @@ -377,6 +378,14 @@ void ofxOpenNI::stop(){ cout << LOG_NAME << ": full stopped" << endl; } +void ofxOpenNI::close() { + stop(); +} + +bool ofxOpenNI::isFrameNew() { + return isNewFrame(); +} + //-------------------------------------------------------------- void ofxOpenNI::stopCommon(){ ofxOpenNIScopedLock scopedLock(bIsThreaded, mutex); @@ -999,6 +1008,8 @@ void ofxOpenNI::allocateDepthBuffers(){ depthPixels[0].allocate(width, height, OF_IMAGE_COLOR_ALPHA); depthPixels[1].allocate(width, height, OF_IMAGE_COLOR_ALPHA); backgroundPixels.allocate(getWidth(), getHeight(), OF_IMAGE_COLOR_ALPHA); + distancePixels.allocate(width, height, 1); + distancePixels.set(0); currentDepthPixels = &depthPixels[0]; backDepthPixels = &depthPixels[1]; if(bUseTexture) depthTexture.allocate(width, height, GL_RGBA); @@ -1372,6 +1383,9 @@ void ofxOpenNI::updateDepthPixels(){ unsigned char * texture = backDepthPixels->getPixels() + y * g_DepthMD.XRes() * 4 + g_DepthMD.XOffset() * 4; for (int x = 0; x < g_DepthMD.XRes(); x++, depth++, texture += 4){ + float* pDistancePixels = distancePixels.getPixels(); + pDistancePixels[x+y*g_DepthMD.XRes()] = *depth; + ofColor depthColor; // bool bUseSubtraction = false; @@ -2580,7 +2594,8 @@ bool ofxOpenNI::isNewFrame(){ *************************************************************/ //-------------------------------------------------------------- -ofPixels& ofxOpenNI::getDepthPixels(){ + +ofPixels& ofxOpenNI::getDepthPixelsRef(){ ofxOpenNIScopedLock scopedLock(bIsThreaded, mutex); if(bUseBackBuffer){ return *currentDepthPixels; @@ -2589,6 +2604,10 @@ ofPixels& ofxOpenNI::getDepthPixels(){ } } +unsigned char* ofxOpenNI::getDepthPixels(){ + return getDepthPixelsRef().getPixels(); +} + //-------------------------------------------------------------- ofShortPixels& ofxOpenNI::getDepthRawPixels(){ //ofxOpenNIScopedLock scopedLock(bIsThreaded, mutex); @@ -2613,6 +2632,24 @@ ofPixels& ofxOpenNI::getImagePixels(){ } } +ofPixels& ofxOpenNI::getPixelsRef() { + return getImagePixels(); +} + +unsigned char* ofxOpenNI::getPixels() { + return getImagePixels().getPixels(); +} + +ofFloatPixels& ofxOpenNI::getDistancePixelsRef() { + return distancePixels; +} + +float* ofxOpenNI::getDistancePixels() { + return distancePixels.getPixels(); +} + + + //-------------------------------------------------------------- ofTexture& ofxOpenNI::getDepthTextureReference(){ //ofxOpenNIScopedLock scopedLock(bIsThreaded, mutex); diff --git a/src/ofxOpenNI.h b/src/ofxOpenNI.h index f1a2a50..9f13cfc 100644 --- a/src/ofxOpenNI.h +++ b/src/ofxOpenNI.h @@ -42,9 +42,11 @@ static int instanceCount = -1; #include "ofxOpenNITypes.h" #include "ofxOpenNIUtils.h" +#include "ofxBase3DVideo.h" + using namespace xn; -class ofxOpenNI : public ofThread { +class ofxOpenNI : public ofThread, public ofxBase3DVideo { public: @@ -270,7 +272,7 @@ class ofxOpenNI : public ofThread { void setSync(bool b); bool getSync(); - ofPixels& getDepthPixels(); + ofPixels& getDepthPixelsRef(); ofShortPixels& getDepthRawPixels(); ofPixels& getImagePixels(); @@ -310,6 +312,19 @@ class ofxOpenNI : public ofThread { ofEvent gestureEvent; ofEvent handEvent; + // Functions to implement interfaces ofBaseVideo and ofxBase3DVideo + void close(); + bool isFrameNew(); + unsigned char* getPixels(); + ofPixels& getPixelsRef(); + + unsigned char* getDepthPixels(); + ofFloatPixels& getDistancePixelsRef(); + float* getDistancePixels(); + + + + protected: void threadedFunction(); @@ -410,6 +425,8 @@ class ofxOpenNI : public ofThread { ofShortPixels* currentDepthRawPixels; ofShortPixels backgroundPixels; + ofFloatPixels distancePixels; + const XnDepthPixel* backgroundDepthPixels; // image