Skip to content

Instantly share code, notes, and snippets.

@minusplusminus
Created August 23, 2013 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minusplusminus/6317416 to your computer and use it in GitHub Desktop.
Save minusplusminus/6317416 to your computer and use it in GitHub Desktop.
//
// leapGestures.cpp
// Kadaster Interface
//
// Created by Martijn Mellema on 13-06-13.
//
//
#include "leapGestures.h"
#include "gestureObject.h"
leapGestures::leapGestures(){
twoFingerPos.assign(2, ofVec3f(0,0,0));
bisPinching = false;
typeGesture = TYPE_INVALID;
}
bool leapGestures::bIsZooming(){
}
float leapGestures::getZoom( vector<Leap::Hand> h ){
// Frame frame = getLeap()->;
if ( h.size() >= 1){
Hand hand = h[0];
Vector handVel = h[0].palmVelocity();
if (!handVel.isValid())
handVel = Vector(0, 0, 0);
// Check if the hand has any fingers
Leap::FingerList fingers = hand.Hand::fingers();
if (fingers.count() > 2){
// Set a flag to see whether we detect zoom or pan
bool zoomOrPan = false;
// Create an AutoCAD vector from the hand's velocity
ofPoint handVec = ofPoint(handVel.x, handVel.y, handVel.z);
// Get the largest element and its [absolute] value
int largestDim = handVec.DIM;
float largestVal = handVec.z;
float largestAbs = abs(largestVal);
// Depending on the largest value we know to zoom or pan
float zoom = 0;
switch (largestDim)
{
case 1:
if (largestAbs > 250)
{
cout<<"Zoom "<<(largestVal < 0 ? "in" : "out")<<endl;
zoom = largestVal > 0 ? 1.1 : 0.9;
zoomOrPan = true;
}
break;
default:
if (largestAbs > 100)
{
float x = 0.02 * -handVel.x / largestAbs;
float y = 0.02 * handVel.z / largestAbs;
cout<<
"Pan " <<
(largestDim == 0 ?
(largestVal < 0 ? "left" : "right") :
(largestVal < 0 ? "up" : "down")
)
<<endl;
Pan(x, y);
zoomOrPan = true;
}
break;
}
// If not zoom or pan, we check for orbit
if (!zoomOrPan && largestAbs < 100)
{
// To determine whether we have to orbit, get the normal
// to the hand's palm
Vector handNorm = hand.palmNormal();
if (abs(handNorm.roll()) > 0.4)
{
// Orbit left or right when there is "roll"
cout<< "Orbit " <<(handNorm.roll() < 0 ? "right" : "left")<<endl;
Orbit( 0.5 * handNorm.roll() * PI / 12);
}
else if (handNorm.pitch() < -1.5 || handNorm.pitch() > -1.0)
{
// Orbit up or down when there is "pitch"
float pitch =
handNorm.pitch() < -1.5 ?
handNorm.pitch() + 1.5 :
abs(handNorm.pitch() + 1.0);
cout<<
"Orbit " <<(pitch < 0 ? "up" : "down")
<<endl;
OrbitVertical(0.5 * pitch * PI / 12);
}
}
// Process Windows messages at the end of each frame
// System.Windows.Forms.Application.DoEvents();
}
}
}
//-----------------------------------------------------------------------
void leapGestures::getGestures(){
Leap::GestureList gestures = getLeap()->getLeapGestures();
typeGesture = TYPE_INVALID;
size_t numGestures = gestures.count();
for (size_t i=0; i < numGestures; i++) {
typeGesture = Gesture_Type(gestures[i].type()) ;
if (gestures[i].type() == Leap::Gesture::TYPE_SCREEN_TAP) {
Leap::ScreenTapGesture tap = gestures[i];
tapLoc = ofVec3f(getLeap()->getMappedofPoint( tap.position()));
tapLoc = tapLoc.normalize();
// tap.id();
gestureValues[TYPE_SCREEN_TAP] = ofPoint(tapLoc.x, tapLoc.y);
gestureID[TYPE_SCREEN_TAP] = tap.id();
} else if (gestures[i].type() == Leap::Gesture::TYPE_KEY_TAP) {
Leap::KeyTapGesture tap = gestures[i];
tapLoc = ofVec3f(getLeap()->getMappedofPoint(tap.position()));
tapLoc = tapLoc.normalize();
gestureValues[TYPE_KEY_TAP] = ofPoint(tapLoc.x, tapLoc.y);
gestureID[TYPE_KEY_TAP] = tap.id();
} else if (gestures[i].type() == Leap::Gesture::TYPE_SWIPE) {
Leap::SwipeGesture swipe = gestures[i];
Leap::Vector diff = 0.004f*(swipe.position() - swipe.startPosition());
ofVec3f curSwipe(diff.x, -diff.y, diff.z);
gestureValues[TYPE_SWIPE] = curSwipe;
gestureID[TYPE_SWIPE] = swipe.id();
// field.Translate(swipe.id(), curSwipe);
} else if (gestures[i].type() == Leap::Gesture::TYPE_CIRCLE) {
Leap::CircleGesture circle = gestures[i];
float progress = circle.progress();
if (progress >= 1.0f) {
ofVec3f center ( getLeap()->getMappedofPoint(circle.center()));
//center = center.normalize();
ofVec3f normal(circle.normal().x, circle.normal().y, circle.normal().z);
double curAngle = 6.5;
if (normal.z < 0) {
curAngle *= -1;
}
gestureValues[TYPE_CIRCLE] = ofPoint(center.x+ofGetWidth()/2, center.y+ofGetHeight()/2);
gestureID[TYPE_CIRCLE] = circle.id();
circleTypeRadius = circle.radius();
circleTypeAngle = curAngle;
}
}
}
}
//-----------------------------------------------------------------------
bool leapGestures::isGettingGesture(){
if( typeGesture != TYPE_INVALID){
return true;
}
return false;
}
//-----------------------------------------------------------------------
Gesture_Type leapGestures::getGestureType(){
return typeGesture;
}
//-----------------------------------------------------------------------
map<Gesture_Type, ofVec3f> leapGestures::getGesturePosition(){
return gestureValues;
}
//-----------------------------------------------------------------------
map<Gesture_Type, int> leapGestures::getGestureId(){
return gestureID;
}
//-----------------------------------------------------------------------
float leapGestures::getCircleTypeRadius(){
return circleTypeRadius;
}
//-----------------------------------------------------------------------
float leapGestures::getCircleTypeAngle(){
return circleTypeAngle;
}
void leapGestures::printGestures(){
if(isGettingGesture()){
string printType;
switch( Gesture_Type(typeGesture))
{
case TYPE_KEY_TAP: printType = "TYPE_KEY_TAP"; break;
case TYPE_CIRCLE: printType ="TYPE_CIRCLE"; break;
case TYPE_SCREEN_TAP: printType ="TYPE_SCREEN_TAP"; break;
case TYPE_SWIPE : printType = "TYPE_SWIPE"; break;
case TYPE_INVALID : printType = ""; break;
}
cout<<"leap gesture::"<<printType<<endl;
}
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
void leapGestures::getTwoFinger(vector<Leap::Hand> h){
// Frame frame = getLeap()->;
if ( h.size() == 1){
Hand hand = h[0];
Vector handPos = h[0].palmPosition();
if (!handPos.isValid())
handPos = Vector(0, 0, 0);
<<<<<<< HEAD
bisGettingFinger = false;
}
=======
>>>>>>> parent of b3738dd... fixed demo
// Check if the hand has any fingers
Leap::FingerList fingers = hand.Hand::fingers();
// cout<<fingers.count()<<endl;
if (fingers.count() == 2){
// Set a flag to see whether we detect zoom or pan
bool zoomOrPan = false;
// Create an AutoCAD vector from the hand's velocity
const Finger & fingerOne = fingers[0];
const Finger & fingerTwo = fingers[1];
// cout<<fingerOne.stabilizedTipPosition()<<endl;
// links boven (-329.437, 409.748, 35.8048)
//(276.883, 445.865, 97.6463)
ofVec3f fingerVecOne = getLeap()->getMappedofPoint(fingerOne.stabilizedTipPosition());
ofVec3f fingerVecTwo = getLeap()->getMappedofPoint(fingerTwo.stabilizedTipPosition());
vector<ofVec3f> tempTwo(2,ofVec3f(0,0,0));
// handLeftOrRight
if (fingerVecOne.x > fingerVecTwo.x){
handLeftRight = LEFT;
} else{
handLeftRight = RIGHT;
}
// pinching
pinchDistance = fingerVecOne.distance(fingerVecTwo);
if (pinchDistance < 110.0f)
bisPinching = true;
else
bisPinching = false;
twoFingerPos [0] = ofPoint(fingerVecOne.x+ofGetWidth()/2 , -fingerVecOne.y+ ofGetHeight()/2,fingerVecOne.z);
twoFingerPos [1] = ofPoint(fingerVecTwo.x+ofGetWidth()/2 , -fingerVecTwo.y+ ofGetHeight()/2,fingerVecTwo.z);;
}
}
if ( h.size() == 0){
cout<<"DISABLE PINCHING:"<<endl;
bisPinching = false;
}
}
//-----------------------------------------------------------------------
vector<ofVec3f> leapGestures::twoFingerValues(){
return twoFingerPos;
}
//-----------------------------------------------------------------------
LEAP_HANDS leapGestures::getHandLeftOrRight(){
return handLeftRight;
}
//-----------------------------------------------------------------------
bool leapGestures::isPinching(){
return bisPinching;
bisPinching = false;
}
//-----------------------------------------------------------------------
ofVec3f leapGestures::getPinching(){
return twoFingerPos[0];
}
//-----------------------------------------------------------------------
void leapGestures::Pan(float _leftRight, float _upDown){
leftRight = _leftRight;
upDown = _upDown;
}
void leapGestures::Orbit( double angle)
{
orbitAngle = angle;
// Adjust the ViewTableRecord
ofMatrix3x3 matrix;
// _vtr.ViewDirection =
// _vtr.ViewDirection.TransformBy(
// Matrix3d.Rotation(angle, axis, Point3d.Origin)
// );
// Set it as the current view
// _doc.Editor.SetCurrentView(_vtr);
}
void leapGestures::OrbitVertical(double angle)
{
// Adjust the ViewTableRecord
orbitVerticalAngle = angle;
// _vtr.ViewDirection =
// _vtr.ViewDirection.TransformBy(
// Matrix3d.Rotation(
// angle,
// _vtr.ViewDirection.GetPerpendicularVector(),
// Point3d.Origin
// )
// );
//
// // Set it as the current view
//
// _doc.Editor.SetCurrentView(_vtr);
}
float leapGestures::getPanUpDown(){
return upDown;
}
float leapGestures::getPanLeftRight(){
return leftRight;
}
float leapGestures::getOrbitVertical(){
return orbitVerticalAngle;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment