Skip to content

Instantly share code, notes, and snippets.

@tatsuro-ueda
Created January 31, 2012 06:07
Show Gist options
  • Save tatsuro-ueda/1709188 to your computer and use it in GitHub Desktop.
Save tatsuro-ueda/1709188 to your computer and use it in GitHub Desktop.
simple-OpenNI sample CircleCtrl
// 手で円を描き、その時の回転角を取得して矢印を描画する
import SimpleOpenNI.*;
SimpleOpenNI context;
// NITEモジュール
XnVSessionManager sessionManager;
XnVCircleDetector circleDetector;
CircleCtrlElement circleCtrl;
float ctrlRadius=200;
void setup()
{
context = new SimpleOpenNI(this);
// 鏡像を有効にする
context.setMirror(true);
// 深度カメラを有効にする
if(context.enableDepth() == false)
{
println("Can't open the depthMap, maybe the camera is not connected!");
exit();
return;
}
// 手の検出とジェスチャー検出を有効にする
context.enableGesture();
context.enableHands();
// NITEの準備
sessionManager = context.createSessionManager("Wave", "RaiseHand");
circleDetector = new XnVCircleDetector();
circleDetector.RegisterCircle(this);
circleDetector.RegisterNoCircle(this);
circleDetector.RegisterPrimaryPointCreate(this);
circleDetector.RegisterPrimaryPointDestroy(this);
circleDetector.RegisterPointUpdate(this);
sessionManager.AddListener(circleDetector);
size(context.depthWidth(), context.depthHeight());
smooth();
// GUI要素を初期化する
circleCtrl = new CircleCtrlElement(context,width/2,height/2,ctrlRadius);
// テキスト情報
println("-------------------------------");
println("1. Wave till the circle gets green");
println("2. Make one 360Deg+ movement with your hand, till the circle gets blue");
println("3. Now you should be able to change the angle");
println("-------------------------------");
}
void draw()
{
background(200,0,0);
// カメラ情報を更新する
context.update();
// NITEモジュール情報を更新する
context.update(sessionManager);
// 深度マップを描画する
image(context.depthImage(),0,0);
// 円形コントロールを描画する
circleCtrl.drawHandsCtrl();
circleCtrl.draw();
}
////////////////////////////////////////////////////////////////////////////////////////////
// セッションコールバック
void onStartSession(PVector pos)
{
println("onStartSession: " + pos);
circleCtrl.setState(CircleCtrlElement.CTRL_FOCUS);
}
void onEndSession()
{
println("onEndSession: ");
circleCtrl.setState(CircleCtrlElement.CTRL_DEF);
}
void onFocusSession(String strFocus,PVector pos,float progress)
{
println("onFocusSession: focus=" + strFocus + ",pos=" + pos + ",progress=" + progress);
}
////////////////////////////////////////////////////////////////////////////////////////////
// XnVCircleDetectorのコールバック
void onCircle(float fTimes,boolean bConfident,XnVCircle circle)
{
// println("onCircle: " + fTimes + " , bConfident=" + bConfident);
circleCtrl.setState(CircleCtrlElement.CTRL_ACTIVE);
circleCtrl.setCtrl(fTimes,circle);
}
void onNoCircle(float fTimes,int reason)
{
println("onNoCircle: " + fTimes + " , reason= " + reason);
circleCtrl.setState(CircleCtrlElement.CTRL_FOCUS);
}
void onPrimaryPointCreate(XnVHandPointContext pContext,XnPoint3D ptFocus)
{
println("onPrimaryPointCreate:");
}
void onPrimaryPointDestroy(int nID)
{
println("onPrimaryPointDestroy: " + nID);
}
void onPointUpdate(XnVHandPointContext pContext)
{
circleCtrl.setHandPos(pContext.getPtPosition().getX(),pContext.getPtPosition().getY(),pContext.getPtPosition().getZ());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment