2013年1月12日 星期六

[ Kinect ultra ] 設計就從建構抽象化類別開始

抽象化在設計程式中是很重要的觀念,可以參考維基百科中的抽象化的定義,抽象化就是縮減,去掉不必要的行為,只保留基本的屬性及行為。抽象化在計算機科學是指穩藏實作細節,降低程式的複雜性。

在kinect ultra專案中,就可看到下列幾個重要的抽象化類別:
1. AbstractOpenGLRenderer:渲染(Render)是應用程式將模型生成影像的過成,OpenGL(全名是Open Graphics Library)是一個跨程式語言及跨平台的程式介面。在AbstractOpenGLRenderer類別中定義一個m_rctx物件,其類別為RenderingContext。


class AbstractOpenGLRenderer
{
protected:
RenderingContext* m_rctx;

public:
AbstractOpenGLRenderer(RenderingContext* rctx);
virtual ~AbstractOpenGLRenderer() = 0;
};


2. AbstractTextureRenderer:抽象材質貼圖渲染類號,此類別有兩個父類別,分別是AbstractOpenGLRenderer和Configurable。


class AbstractTextureRenderer : public AbstractOpenGLRenderer, protected Configurable
{
protected:
int m_textureWidth;
int m_textureHeight;

XuColorPixel* m_textureData;
cv::Rect m_imageRect;
GLuint m_textureID;

GLBatch m_batch;
M3DMatrix44f m_orthoProjectionMatrix;
bool m_isLocked;

public:
AbstractTextureRenderer(RenderingContext* rctx);
virtual ~AbstractTextureRenderer() = 0;
virtual void draw();

void lock(bool value);
bool isLocked() { return m_isLocked; }

protected:
void init(const cv::Rect& imageRect);
void setupBatch();

// optionally overridable
virtual void setupTexture();
virtual void executeDraw();

// need to override
virtual void setupCopy() = 0;
virtual void copyRow(XuColorPixel* dst, int srcOffset) = 0;
virtual void finalizeCopy() = 0;
};


3. AbstractElementRenderer:抽象元件渲染類別也是AbstractOpenGLRenderer的子類別,在該類別使串列(List)結構來儲存各元件。

template class AbstractElementRenderer : public AbstractOpenGLRenderer
{
protected:
GLuint m_textureID;
TimeTicker m_ticker;
std::list m_elements;

float m_gravity;

public:
AbstractElementRenderer(RenderingContext* rctx, const char* alphaTextureFile, float gravity)
: AbstractOpenGLRenderer(rctx)
{
m_textureID = readAlphaTexture(alphaTextureFile);
m_gravity = gravity;
}

virtual ~AbstractElementRenderer()
{
}

virtual void draw()
{
executeDraw();

float dt = m_ticker.tick();
if (dt > 0.0f) {
progress(dt);
}
}

protected:
void setupTextureParameters()
{
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, m_textureID);
}

virtual void executeDraw() = 0;

void progress(float dt)
{
std::list::iterator i = m_elements.begin();
while (i != m_elements.end()) {
i->p += i->v * dt;
i->v.Y -= m_gravity * dt;

onProgress(&(*i), dt);

i->lifeTime -= dt;
if (i->lifeTime <= 0.0f) {
i = m_elements.erase(i);
} else {
i++;
}
}
}

virtual void onProgress(ElementType* element, float dt)
{
}
};

4. AbstractPoseDetector:抽象姿勢偵測類別。

class AbstractPoseDetector
{
protected:
UserDetector* m_userDetector;

private:
// TODO: should be time instead of frame count
float m_requiredPosingStability;
float m_posingTime;

TimeTicker m_ticker;

public:
AbstractPoseDetector(UserDetector* userDetector);
virtual ~AbstractPoseDetector();

virtual void detect();

protected:
void setRequiredPosingStability(float value) { m_requiredPosingStability = value; }

virtual bool isPosing(float dt);
virtual void onPoseDetected(float dt);
virtual void onDetectPre(float dt);
virtual void onDetectPost(float dt);
};


5. AbstractPowerPoseDetector:抽象變身偵測類別

class AbstractPowerPoseDetector : public AbstractPoseDetector
{
protected:
HenshinDetector* m_henshinDetector;

public:
AbstractPowerPoseDetector(HenshinDetector* henshinDetector);
virtual ~AbstractPowerPoseDetector();

virtual void detect();
};



6. AbstractEmeriumBeamDetector:艾美利母光束照射偵測

class AbstractEmeriumBeamDetector : public AbstractPowerPoseDetector, protected Configurable
{
private:
AbstractSimpleBeamRenderer* m_beamRenderer;

public:
AbstractEmeriumBeamDetector(DepthProvider* depthProvider, HenshinDetector* henshinDetector, AbstractSimpleBeamRenderer* beamRenderer);
virtual ~AbstractEmeriumBeamDetector();

protected:
void shootBeam(const XV3& p, const XV3& dv);
};


7. AbstractSensorDataProvider:抽象感測資料提供類別

class AbstractSensorDataProvider
{
private:
static const DWORD TIMEOUT = 1000;

protected:
INuiSensor* m_pSensor;
HANDLE m_hNextFrameEvent;
bool m_isLocked;

public:
AbstractSensorDataProvider(INuiSensor* pSensor);
virtual ~AbstractSensorDataProvider() = 0;

bool waitForNextFrameAndLock();
void unlock();

protected:
virtual bool waitForNextFrameAndLockImpl(DWORD timeout) = 0;
virtual void unlockImpl() = 0;
};



8. AbstractImageStreamProvider:抽象影像串流提供類別

class AbstractImageStreamProvider : public AbstractSensorDataProvider
{
protected:
HANDLE m_hStream;
NUI_IMAGE_FRAME m_frame;
NUI_LOCKED_RECT m_lockedRect;

public:
AbstractImageStreamProvider(INuiSensor* pSensor) : AbstractSensorDataProvider(pSensor), m_hStream(NULL)
{
}

virtual ~AbstractImageStreamProvider() {
}
};


沒有留言:

張貼留言