IL 2016 SS
import peasy.test.*;
import peasy.org.apache.commons.math.*;
import peasy.*;
import peasy.org.apache.commons.math.geometry.*;
PeasyCam camera;
import SimpleOpenNI.*;
PImage boardImg;
SimpleOpenNI context;
color[] userClr = new color[] {
color(255, 0, 0),
color(0, 255, 0),
color(0, 0, 255),
color(255, 255, 0),
color(255, 0, 255),
color(0, 255, 255)
};
PVector com = new PVector();
PVector com2d = new PVector();
PVector rightHand = new PVector();
PVector rightHand2D = new PVector();
void setup() {
size(800, 600, P3D);
context = new SimpleOpenNI(this);
if (context.isInit() == false) {
println("Can't init SimpleOpenNI, maybe the camera is not connected!");
exit();
return;
}
boardImg = createCheckerBoard();
context.setMirror(true);
camera = new PeasyCam(this, 1000);
context.enableDepth();
context.enableUser();
stroke(0, 0, 255);
strokeWeight(10);
smooth();
}
void draw() {
background(30);
context.update();
translate(-width*0.5, -height*0.5, 0);
camera.beginHUD();
image(context.userImage(), 0, 0, 120, 90);
camera.endHUD();
translate(width/2, height/2, 0);
rotateX(radians(180));
rotateY(radians(0));
scale(0.5);
pushMatrix();
box(50);
popMatrix();
pushMatrix();
rotateX(radians(90));
translate(-600, 0, 800);
image(boardImg, 0, 0);
popMatrix();
// draw the skeleton if it's available
int[] userList = context.getUsers();
for (int i=0; i<userList.length; i++) {
if (context.isTrackingSkeleton(userList[i])) {
stroke(userClr[ (userList[i] - 1) % userClr.length ] );
drawSkeleton(userList[i]);
}
// draw the center of mass
if (context.getCoM(userList[i], com)) {
stroke(100, 255, 0);
strokeWeight(10);
beginShape(LINES);
vertex(com.x, com.y - 5, com.z);
vertex(com.x, com.y + 5, com.z);
vertex(com.x - 5, com.y, com.z);
vertex(com.x + 5, com.y, com.z);
endShape();
// Draw Rectangle Right Hand !!!
pushMatrix();
translate(rightHand.x, rightHand.y, rightHand.z);
rect(0, 0, 20, 20);
popMatrix();
}
}
}
void drawSkeleton(int userId) {
// to get the 3d joint data
/*
PVector jointPos = new PVector();
context.getJointPositionSkeleton(userId,SimpleOpenNI.SKEL_NECK,jointPos);
println(jointPos);
*/
//
// Right Hand Data
context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_HAND, rightHand);
drawLimb(userId, SimpleOpenNI.SKEL_HEAD, SimpleOpenNI.SKEL_NECK);
drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_LEFT_SHOULDER);
drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_LEFT_ELBOW);
drawLimb(userId, SimpleOpenNI.SKEL_LEFT_ELBOW, SimpleOpenNI.SKEL_LEFT_HAND);
drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_RIGHT_SHOULDER);
drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_RIGHT_ELBOW);
drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW, SimpleOpenNI.SKEL_RIGHT_HAND);
drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_LEFT_HIP);
drawLimb(userId, SimpleOpenNI.SKEL_LEFT_HIP, SimpleOpenNI.SKEL_LEFT_KNEE);
drawLimb(userId, SimpleOpenNI.SKEL_LEFT_KNEE, SimpleOpenNI.SKEL_LEFT_FOOT);
drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_RIGHT_HIP);
drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_HIP, SimpleOpenNI.SKEL_RIGHT_KNEE);
drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_KNEE, SimpleOpenNI.SKEL_RIGHT_FOOT);
}
void drawLimb(int userId, int jointType1, int jointType2) {
PVector jointPos1 = new PVector();
PVector jointPos2 = new PVector();
float confidence;
// draw the joint position
confidence = context.getJointPositionSkeleton(userId, jointType1, jointPos1);
confidence = context.getJointPositionSkeleton(userId, jointType2, jointPos2);
stroke(255, 0, 0, confidence * 200 + 55);
line(jointPos1.x, jointPos1.y, jointPos1.z,
jointPos2.x, jointPos2.y, jointPos2.z);
}
// -----------------------------------------------------------------
// SimpleOpenNI events
void onNewUser(SimpleOpenNI curContext, int userId)
{
println("onNewUser - userId: " + userId);
println("\tstart tracking skeleton");
curContext.startTrackingSkeleton(userId);
}
void onLostUser(SimpleOpenNI curContext, int userId)
{
println("onLostUser - userId: " + userId);
}
void onVisibleUser(SimpleOpenNI curContext, int userId)
{
//println("onVisibleUser - userId: " + userId);
}
void keyPressed()
{
switch(key)
{
case ' ':
context.setMirror(!context.mirror());
break;
}
}
//https://forum.processing.org/one/topic/code-to-generate-a-checkerboard-pattern.html
PImage createCheckerBoard() {
final short w = 1200, h = 1200, d = 100;
final color on = 0200, off = 0100, a = 0300;
return createCheckerBoard(w, h, d, on, off, a);
}
PImage createCheckerBoard(int w, int h, int d, color on, color off, color a) {
final int cols = w/d, rows = h/d;
final PGraphics pg = createGraphics(w, h, P3D);
pg.beginDraw();
pg.rectMode(CORNER);
pg.noStroke();
pg.fill(on, a);
pg.background(off, a);
for (int r = 0; r != rows; ++r) for (int c = 0; c != cols; ++c)
if ( (r+c & 1) == 0 ) pg.rect(c*d, r*d, d, d);
pg.endDraw();
return pg.get();
}