/* -------------------------------------------------------------------------- * SimpleOpenNI Intersection Test * -------------------------------------------------------------------------- * Processing Wrapper for the OpenNI/Kinect library * http://code.google.com/p/simple-openni * -------------------------------------------------------------------------- * prog: Max Rheiner / Interaction Design / zhdk / http://iad.zhdk.ch/ * date: 05/14/2012 (m/d/y) * ---------------------------------------------------------------------------- * Gives an example how to use intersections(rayTriangle) * ---------------------------------------------------------------------------- */ import SimpleOpenNI.*; SimpleOpenNI context; float zoomF =0.5f; float rotX = radians(180); // by default rotate the hole scene 180deg around the x-axis, // the data from openni comes upside down float rotY = radians(0); boolean autoCalib=true; PVector bodyCenter = new PVector(); PVector bodyDir = new PVector(); PVector[] triangle1 = new PVector[3]; PVector[] triangle2 = new PVector[3]; PVector sphere1; float sphere1Radius; void setup() { size(1024,768,P3D); context = new SimpleOpenNI(this); // disable mirror context.setMirror(false); // enable depthMap generation if(context.enableDepth() == false) { println("Can't open the depthMap, maybe the camera is not connected!"); exit(); return; } // enable skeleton generation for all joints context.enableUser(SimpleOpenNI.SKEL_PROFILE_ALL); stroke(255,255,255); smooth(); perspective(radians(45), float(width)/float(height), 10,150000); // setup the triangles triangle1[0] = new PVector(500,0,1500); triangle1[1] = new PVector(-500,0,1500); triangle1[2] = new PVector(0,500,1500); triangle2[0] = new PVector(1000,0,2500); triangle2[1] = new PVector(1000,0,3500); triangle2[2] = new PVector(1000,500,3000); sphere1 = new PVector(-1000,500,3000); sphere1Radius = 200; } void draw() { // update the cam context.update(); background(0,0,0); // set the scene pos translate(width/2, height/2, 0); rotateX(rotX); rotateY(rotY); scale(zoomF); int[] depthMap = context.depthMap(); int steps = 3; // to speed up the drawing, draw every third point int index; PVector realWorldPoint; translate(0,0,-1000); // set the rotation center of the scene 1000 infront of the camera stroke(100); for(int y=0;y < context.depthHeight();y+=steps) { for(int x=0;x < context.depthWidth();x+=steps) { index = x + y * context.depthWidth(); if(depthMap[index] > 0) { // draw the projected point realWorldPoint = context.depthMapRealWorld()[index]; point(realWorldPoint.x,realWorldPoint.y,realWorldPoint.z); } } } // draw the skeleton if it's available int[] userList = context.getUsers(); for(int i=0;i 0) { // draw hit point strokeWeight(7); stroke(255,0,255); point(hit1.x,hit1.y,hit1.z); if(intersectionSphere > 1) point(hit2.x,hit2.y,hit2.z); strokeWeight(3); } else strokeWeight(1); stroke(255,255,0); pushMatrix(); translate(sphere1.x,sphere1.y,sphere1.z); sphereDetail(10); sphere(sphere1Radius); popMatrix(); popStyle(); } // ----------------------------------------------------------------- // SimpleOpenNI user events void onNewUser(int userId) { println("onNewUser - userId: " + userId); println(" start pose detection"); if(autoCalib) context.requestCalibrationSkeleton(userId,true); else context.startPoseDetection("Psi",userId); } void onLostUser(int userId) { println("onLostUser - userId: " + userId); } void onExitUser(int userId) { println("onExitUser - userId: " + userId); } void onReEnterUser(int userId) { println("onReEnterUser - userId: " + userId); } void onStartCalibration(int userId) { println("onStartCalibration - userId: " + userId); } void onEndCalibration(int userId, boolean successfull) { println("onEndCalibration - userId: " + userId + ", successfull: " + successfull); if (successfull) { println(" User calibrated !!!"); context.startTrackingSkeleton(userId); } else { println(" Failed to calibrate user !!!"); println(" Start pose detection"); context.startPoseDetection("Psi",userId); } } void onStartPose(String pose,int userId) { println("onStartdPose - userId: " + userId + ", pose: " + pose); println(" stop pose detection"); context.stopPoseDetection(userId); context.requestCalibrationSkeleton(userId, true); } void onEndPose(String pose,int userId) { println("onEndPose - userId: " + userId + ", pose: " + pose); } // ----------------------------------------------------------------- // Keyboard events void keyPressed() { switch(key) { case ' ': context.setMirror(!context.mirror()); break; } switch(keyCode) { case LEFT: rotY += 0.1f; break; case RIGHT: // zoom out rotY -= 0.1f; break; case UP: if(keyEvent.isShiftDown()) zoomF += 0.01f; else rotX += 0.1f; break; case DOWN: if(keyEvent.isShiftDown()) { zoomF -= 0.01f; if(zoomF < 0.01) zoomF = 0.01; } else rotX -= 0.1f; break; } } void getBodyDirection(int userId,PVector centerPoint,PVector dir) { PVector jointL = new PVector(); PVector jointH = new PVector(); PVector jointR = new PVector(); float confidence; // draw the joint position confidence = context.getJointPositionSkeleton(userId,SimpleOpenNI.SKEL_LEFT_SHOULDER,jointL); confidence = context.getJointPositionSkeleton(userId,SimpleOpenNI.SKEL_HEAD,jointH); confidence = context.getJointPositionSkeleton(userId,SimpleOpenNI.SKEL_RIGHT_SHOULDER,jointR); // take the neck as the center point confidence = context.getJointPositionSkeleton(userId,SimpleOpenNI.SKEL_NECK,centerPoint); /* // manually calc the centerPoint PVector shoulderDist = PVector.sub(jointL,jointR); centerPoint.set(PVector.mult(shoulderDist,.5)); centerPoint.add(jointR); */ PVector up = new PVector(); PVector left = new PVector(); up.set(PVector.sub(jointH,centerPoint)); left.set(PVector.sub(jointR,centerPoint)); dir.set(up.cross(left)); dir.normalize(); }