void LeapFishyApp::processGesture() { Leap::Frame frame = m_LeapController.frame(); if( m_LastFrame == frame ) return; Leap::GestureList gestures = m_LastFrame.isValid() ? frame.gestures( m_LastFrame ) : frame.gestures(); m_LastFrame = frame; for( int i = 0; i < gestures.count(); i++ ) { if( gestures[i].type() == Leap::Gesture::TYPE_SWIPE ) { Leap::SwipeGesture swipe = gestures[i]; Leap::Vector diff = 0.006f*(swipe.position() - swipe.startPosition()); Vec2f curSwipe(diff.x, -diff.y); m_pPlayer->AddVelocity( curSwipe ); } else if( gestures[i].type() == Leap::Gesture::TYPE_KEY_TAP || gestures[i].type() == Leap::Gesture::TYPE_SCREEN_TAP ) { m_pPlayer->KillVelocity(); } } }
void Listener::onFrame(const Leap::Controller &controller) { Leap::Frame curFrame = controller.frame(); if (firstFrameLeap == 0 && ++frameCount > 10) { gettimeofday(&firstFrameAbs, NULL); firstFrameLeap = curFrame.timestamp(); cout << "First frame clock time: " << tv_to_usec(firstFrameAbs) << endl; cout << "First frame leap time: " << firstFrameLeap << endl; } // use current active gesture recognizers to locate gestures // and then trigger appropriate note/controls // feed frames to recognizers vector<GesturePtr> recognizers = gestureRecognizers(); for (vector<GesturePtr>::iterator it = recognizers.begin(); it != recognizers.end(); ++it) { // get controls recognized from gestures GesturePtr gesture = *it; std::vector<ControlPtr> gestureControls; // controls from this gesture gesture->recognizedControls(controller, gestureControls); if (! gestureControls.size()) continue; // call gesture recognized callback onGestureRecognized(controller, gesture); for (vector<ControlPtr>::iterator ctl = gestureControls.begin(); ctl != gestureControls.end(); ++ctl) { ControlPtr control = *ctl; onControlUpdated(controller, gesture, control); } } }
void MenuController::updateLeapPointer(const Leap::Controller& controller, const Leap::Frame& frame) { pointer_ = frame.fingers().extended().frontmost(); Leap::Vector n = frame.interactionBox().normalizePoint(pointer_.stabilizedTipPosition()); leap.x = n.x * viewport_.width + viewport_.x; leap.y = n.y * viewport_.height + viewport_.y; }
void Quickstart::onFrame(const Leap::Controller &controller) { // returns the most recent frame. older frames can be accessed by passing in // a "history" parameter to retrieve an older frame, up to about 60 // (exact number subject to change) const Leap::Frame frame = controller.frame(); // do nothing unless hands are detected if (frame.hands().empty()) return; // first detected hand const Leap::Hand firstHand = frame.hands()[0]; // first pointable object (finger or tool) const Leap::PointableList pointables = firstHand.pointables(); if (pointables.empty()) return; const Leap::Pointable firstPointable = pointables[0]; // print velocity on the X axis cout << "Pointable X velocity: " << firstPointable.tipVelocity()[0] << endl; const Leap::FingerList fingers = firstHand.fingers(); if (fingers.empty()) return; for (int i = 0; i < fingers.count(); i++) { const Leap::Finger finger = fingers[i]; std::cout << "Detected finger " << i << " at position (" << finger.tipPosition().x << ", " << finger.tipPosition().y << ", " << finger.tipPosition().z << ")" << std::endl; } }
void HandController::drawHands() { glPushMatrix(); glTranslatef(translation_); glScalef(scale_, scale_, scale_); Leap::Frame frame = controller_.frame(); for (int h = 0; h < frame.hands().count(); ++h) { Leap::Hand hand = frame.hands()[h]; for (int f = 0; f < hand.fingers().count(); ++f) { Leap::Finger finger = hand.fingers()[f]; // Draw first joint inside hand. Leap::Bone mcp = finger.bone(Leap::Bone::Type::TYPE_METACARPAL); drawJoint(mcp.prevJoint()); for (int b = 0; b < 4; ++b) { Leap::Bone bone = finger.bone(static_cast<Leap::Bone::Type>(b)); drawJoint(bone.nextJoint()); drawBone(bone); } } } glPopMatrix(); }
void ExampleTool::onLeapFrame( Leap::Frame const &aFrame ) { mHands = aFrame.hands().count(); mFingers = 0; for( int i = 0; i < mHands; ++ i ) mFingers += aFrame.hands()[ i ].fingers().count(); }
void LeapController::CopyHandsData( LeapFrame & a_frame_internal, Leap::Frame & a_frame_external ) { const Leap::Device & device = m_ctrl.devices()[0]; a_frame_internal.hands.clear(); a_frame_internal.hands.resize(a_frame_external.hands().count()); a_frame_internal.fingers.clear(); for ( int i_hand = 0; i_hand < a_frame_external.hands().count(); ++i_hand ) { const Leap::Hand & hand_ext = a_frame_external.hands()[i_hand]; // setup hand LeapHand & hand = a_frame_internal.hands[i_hand]; hand.id = hand_ext.id(); hand.fingers_count = 0; hand.dir = toV3( hand_ext.direction() ); hand.palm_normal = toV3( hand_ext.palmNormal() ); hand.palm_pos = toV3( hand_ext.palmPosition() ) * LEAP_SCALE; hand.palm_vel = toV3( hand_ext.palmVelocity() ) * LEAP_SCALE; hand.sphere_center = toV3( hand_ext.sphereCenter() ) * LEAP_SCALE; hand.sphere_radius = hand_ext.sphereRadius() * LEAP_SCALE; hand.age = hand_ext.timeVisible(); hand.dist_to_boundary = device.distanceToBoundary( hand_ext.palmPosition() ) * LEAP_SCALE; int hand_ext_finger_count = hand_ext.fingers().count(); // clamp the number of fingers if LEAP bugs out and returns hand with more than 5 fingers hand_ext_finger_count = max( hand_ext_finger_count, LEAP_MAX_NUM_FINGERS_PER_HAND ); //ASSERT( hand_ext_finger_count <= LEAP_MAX_NUM_FINGERS_PER_HAND ); for( int i_finger = 0; i_finger < hand_ext_finger_count; ++i_finger ) { const Leap::Finger & finger_ext = hand_ext.fingers()[i_finger]; if ( finger_ext.isValid() ) { // alloc new finger a_frame_internal.fingers.resize( a_frame_internal.fingers.size()+1 ); LeapFinger &finger = a_frame_internal.fingers.back(); finger.id = finger_ext.id(); finger.hand_id = hand.id; finger.hand_index = (int)i_hand; finger.finger_size = v2( finger_ext.width(), finger_ext.length() ) * LEAP_SCALE; finger.dir = toV3( finger_ext.direction() ); finger.tip_pos = toV3( finger_ext.tipPosition() ) * LEAP_SCALE; finger.tip_vel = toV3( finger_ext.tipVelocity() ) * LEAP_SCALE; finger.age = finger_ext.timeVisible(); finger.tip_pos_stabilized = toV3( finger_ext.stabilizedTipPosition() ) * LEAP_SCALE; finger.dist_to_boundary = device.distanceToBoundary( finger_ext.tipPosition() ) * LEAP_SCALE; // add finger hand.fingers_index[hand.fingers_count] = (int)a_frame_internal.fingers.size()-1; ++hand.fingers_count; } } } }
static VALUE frame_timestamp(VALUE self) { Leap::Frame * f; Data_Get_Struct(self, Leap::Frame, f); return INT2NUM(f->timestamp()); }
static VALUE frame_id(VALUE self) { Leap::Frame * f; Data_Get_Struct(self, Leap::Frame, f); return INT2NUM(f->id()); }
static VALUE frame_to_s(VALUE self) { Leap::Frame * f; const char * string; Data_Get_Struct(self, Leap::Frame, f); string = f->toString().c_str(); return rb_str_new2(string); }
static VALUE frame_hands(VALUE self) { Leap::Frame * f; Leap::HandList * list; Data_Get_Struct(self, Leap::Frame, f); list = new Leap::HandList(f->hands()); return WrapHandList(list); }
static VALUE frame_invalid(VALUE self) { Leap::Frame * f; Leap::Frame * invalid; Data_Get_Struct(self, Leap::Frame, f); invalid = new Leap::Frame(f->invalid()); return Data_Wrap_Struct(cFrame, 0, 0, invalid); }
void ManipTool::onLeapFrame( Leap::Frame const &aFrame ) { if( (aFrame.timestamp() - mLastExaminedFrame ) < 16*1000 ) return; if( aFrame.hands().count() > 2 ) return; mLastExaminedFrame = aFrame.timestamp(); U16 curFrame = getNextFrameNo( mLastStoredFrame ); Fingers &curFingers = mFingersPerFrame[ curFrame ]; U16 curFinger = 0; curFingers.clear(); curFingers.mTimestamp = mLastExaminedFrame; Leap::HandList hands = aFrame.hands(); for( int i = 0; i < hands.count(); ++i ) { for( int j = 0; j < hands[i].fingers().count(); ++j ) { Leap::Finger oFinger( hands[i].fingers()[j] ); Finger &oF = curFingers.mFingers[ curFinger++ ]; oF.mId = oFinger.id(); oF.mTimestamp = mLastExaminedFrame; copy( oFinger.direction(), oF.mDir ); copy( oFinger.tipPosition(), oF.mTip ); oF.mWidth = oFinger.width(); oF.mLength = oFinger.length(); } } curFingers.mStoredFingers = curFinger; if( mTotalStoredFrames > 0 ) { Fingers &prevFingers = mFingersPerFrame[ mLastStoredFrame ]; for( U16 i = 0; i < curFingers.mStoredFingers; ++i ) { Finger &curFinger = curFingers.mFingers[i]; Finger const *prevFinger = prevFingers.getFinger( curFinger.mId ); if( !prevFinger ) continue; subtract( curFinger.mTip, prevFinger->mTip, curFinger.mFromLast ); curFinger.mLenFromLast = normalize( curFinger.mFromLast ); } } mLastStoredFrame = curFrame; ++mTotalStoredFrames; }
fdata get_finger_positions() { Leap::Frame frame = control.frame(); Leap::FingerList fingers = frame.fingers(); Leap::ToolList tools = frame.tools(); Leap::HandList hands = frame.hands(); //std::vector<std::pair<cl_float4, int>> positions; fdata hand_data; int p = 0; for(int i=0; i<40; i++) { hand_data.fingers[i] = 0.0f; } ///will explode if more than 2 for(int i=0; i<hands.count(); i++) { const Leap::Hand hand = hands[i]; Leap::FingerList h_fingers = hand.fingers(); float grab_strength = hand.grabStrength(); hand_data.grab_confidence[i] = grab_strength; for(int j=0; j<h_fingers.count(); j++) { const Leap::Finger finger = h_fingers[j]; float mfingerposx = finger.tipPosition().x; float mfingerposy = finger.tipPosition().y; float mfingerposz = finger.tipPosition().z; //cl_float4 ps = {mfingerposx, mfingerposy, mfingerposz, 0.0f}; //cl_float4 ps = {mfingerposx, mfingerposy, mfingerposz, 0.0f}; int id = finger.id(); hand_data.fingers[p++] = mfingerposx; hand_data.fingers[p++] = mfingerposy; hand_data.fingers[p++] = mfingerposz; hand_data.fingers[p++] = 0.0f; //positions.push_back(std::pair<cl_float4, int>(ps, id)); } } return hand_data; }
static VALUE valid_p(VALUE self) { Leap::Frame * f; Data_Get_Struct(self, Leap::Frame, f); if (true == f->isValid()) { return Qtrue; } return Qfalse; }
void LeapCinderVectorFieldApp::processFingers() { Leap::Frame frame = m_LeapController.frame(); Leap::FingerList fingers = frame.fingers(); m_VectorField->ClearPointTo(); for( int i = 0; i < fingers.count(); i++ ) { m_VectorField->CheckPointTo( normalizeCoords( fingers[i].tipPosition() ) ); } }
void eleap::eleap_t::impl_t::onFrame(const Leap::Controller& controller) { const Leap::Frame frame = controller.frame(); if (frame.isValid()) { // send the known hands in this frame, this will handle hands coming and going const Leap::HandList hands = frame.hands(); { unsigned long long time_encoded = (0&0xffffffff)<<8 | (DATA_KNOWN_HANDS&0xff); float *f; unsigned char *dp; piw::data_nb_t d = ctx_.allocate_host(time_encoded,INT32_MAX,INT32_MIN,0,BCTVTYPE_INT,sizeof(int32_t),&dp,hands.count(),&f); memset(f,0,hands.count()*sizeof(int32_t)); *dp = 0; for(int i = 0; i < hands.count(); ++i) { const Leap::Hand hand = hands[i]; if(hand.isValid() && hand.fingers().count() > 1) { ((int32_t *)f)[i] = hand.id(); } } enqueue_fast(d,1); } // handle the actual data for the detected hands for(int i = 0; i < hands.count(); ++i) { const Leap::Hand hand = hands[i]; if(hand.isValid() && hand.fingers().count() > 1) { unsigned long long time_encoded = (hand.id()&0xffffffff)<<8 | (DATA_PALM_POSITION&0xff); const Leap::Vector palm_pos = hand.palmPosition(); float *f; unsigned char *dp; piw::data_nb_t d = ctx_.allocate_host(time_encoded,600,-600,0,BCTVTYPE_FLOAT,sizeof(float),&dp,3,&f); memset(f,0,3*sizeof(float)); *dp = 0; f[0] = piw::normalise(600,-600,0,palm_pos.x); f[1] = piw::normalise(600,-600,0,palm_pos.y); f[2] = piw::normalise(600,-600,0,palm_pos.z); enqueue_fast(d,1); } } } }
virtual void onFrame (const Leap::Controller&) { const Leap::Frame frame(m_Controller.frame(0)); const Leap::Hand hand(frame.hands().rightmost()); if (!hand.isValid()) { m_LastNormalizedPos.reset(); return; } const Leap::Vector pos(hand.palmPosition()); m_LastNormalizedPos = frame.interactionBox().normalizePoint(pos); }
int StateKen::eventShake(StateContext& context, const Leap::Controller& controller) { std::cout << "けん\n" << std::endl; const Leap::Frame frame = controller.frame(); const Leap::Hand hand = frame.hands()[0]; Leap::Vector position = hand.palmPosition(); JankenApp::getInstance()->setShakeStartPosition(position); int ret = context.changeState(StatePon::getInstance()); return ret; }
pinch_list HandController::getPinches() { Leap::Frame frame = controller_.frame(); pinch_list pinches; for (int i = 0; i < frame.hands().count(); ++i) { float pinch_strength = frame.hands()[i].pinchStrength(); Leap::Vector tip = frame.hands()[i].fingers()[1].tipPosition(); Vec3f transformed_tip = scale_ * ToVec3f(tip) + translation_; pinches.push_back(std::pair<Vec3f, float>(transformed_tip, pinch_strength)); } return pinches; }
virtual void onFrame(const Leap::Controller& controller) { Leap::Frame frame = controller.frame(); const Leap::HandList& hands = frame.hands(); if (hands.count() > 0) { Leap::Vector newTarget = hands[0].stabilizedPalmPosition(); cout << "New Target: " << newTarget << endl; if (inMotion) { //TODO: must merge with currentTarget, to account for if the //target hand moved...for now, do nothing } else { inMotion = true; currentTarget = WorldVector(newTarget, leapPos); } } }
//Handle Leap Gesture processing. //Trigger the corresponding effects in the particle field. void GesturesDemo::processGestures() { Leap::Frame frame = controller.frame(); if ( lastFrame == frame ) { return; } Leap::GestureList gestures = lastFrame.isValid() ? frame.gestures(lastFrame) : frame.gestures(); lastFrame = frame; size_t numGestures = gestures.count(); for (size_t i=0; i < numGestures; i++) { if (gestures[i].type() == Leap::Gesture::TYPE_SCREEN_TAP) { printf("screen screen tap gesture"); Leap::ScreenTapGesture tap = gestures[i]; ci::Vec3f tapLoc = normalizeCoords(tap.position()); field.Repel(tap.id(), ci::Vec2f(tapLoc.x, tapLoc.y), 3.0); } else if (gestures[i].type() == Leap::Gesture::TYPE_KEY_TAP) { printf("screen key tap gesture"); Leap::KeyTapGesture tap = gestures[i]; ci::Vec3f tapLoc = normalizeCoords(tap.position()); field.Repel(tap.id(), ci::Vec2f(tapLoc.x, tapLoc.y), -3.0); } else if (gestures[i].type() == Leap::Gesture::TYPE_SWIPE) { printf(" swipe gesture"); Leap::SwipeGesture swipe = gestures[i]; Leap::Vector diff = 0.004f*(swipe.position() - swipe.startPosition()); ci::Vec3f curSwipe(diff.x, -diff.y, diff.z); field.Translate(swipe.id(), curSwipe); } else if (gestures[i].type() == Leap::Gesture::TYPE_CIRCLE) { printf(" circle gesture"); Leap::CircleGesture circle = gestures[i]; float progress = circle.progress(); if (progress >= 1.0f) { ci::Vec3f center = normalizeCoords(circle.center()); ci::Vec3f normal(circle.normal().x, circle.normal().y, circle.normal().z); double curAngle = 6.5; if (normal.z < 0) { curAngle *= -1; } field.Rotate(circle.id(), ci::Vec2f(center.x, center.y), circle.radius()/250, curAngle); } } } }
void MouseController::onFrame(const Leap::Controller &controller) { // get list of detected screens const Leap::ScreenList screens = controller.calibratedScreens(); // make sure we have a detected screen if (screens.empty()) return; const Leap::Screen screen = screens[0]; // find the first finger or tool const Leap::Frame frame = controller.frame(); const Leap::HandList hands = frame.hands(); if (hands.empty()) return; const Leap::PointableList pointables = hands[0].pointables(); if (pointables.empty()) return; const Leap::Pointable firstPointable = pointables[0]; // get x, y coordinates on the first screen const Leap::Vector intersection = screen.intersect( firstPointable, true, // normalize 1.0f // clampRatio ); // if the user is not pointing at the screen all components of // the returned vector will be Not A Number (NaN) // isValid() returns true only if all components are finite if (! intersection.isValid()) return; unsigned int x = screen.widthPixels() * intersection.x; // flip y coordinate to standard top-left origin unsigned int y = screen.heightPixels() * (1.0f - intersection.y); CGPoint destPoint = CGPointMake(x, y); CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, destPoint); }
// Runs update logic void TracerApp::update() { // Update frame rate mFrameRate = getAverageFps(); // Process hand data const Leap::HandList& hands = mFrame.hands(); for ( Leap::HandList::const_iterator handIter = hands.begin(); handIter != hands.end(); ++handIter ) { const Leap::Hand& hand = *handIter; const Leap::PointableList& pointables = hand.pointables(); for ( Leap::PointableList::const_iterator pointIter = pointables.begin(); pointIter != pointables.end(); ++pointIter ) { const Leap::Pointable& pointable = *pointIter; int32_t id = pointable.id(); if ( mRibbons.find( id ) == mRibbons.end() ) { Vec3f v = randVec3f() * 0.01f; v.x = math<float>::abs( v.x ); v.y = math<float>::abs( v.y ); v.z = math<float>::abs( v.z ); Colorf color( ColorModel::CM_RGB, v ); Ribbon ribbon( id, color ); mRibbons[ id ] = ribbon; } float width = math<float>::abs( pointable.tipVelocity().y ) * 0.0025f; width = math<float>::max( width, 5.0f ); mRibbons[ id ].addPoint( LeapMotion::toVec3f( pointable.tipPosition() ), width ); } } // Update ribbons for ( RibbonMap::iterator iter = mRibbons.begin(); iter != mRibbons.end(); ++iter ) { iter->second.update(); } }
Vec2 denormalize(const Viewport& viewport, const Leap::Frame& frame, const Leap::Vector& vector) { Leap::Vector v = frame.interactionBox().normalizePoint(vector); float x = viewport.width * v.x; float y = viewport.height * v.y; return{ x, y }; }
void CinderProjectApp::draw() { // Clear window gl::setViewport( getWindowBounds() ); gl::clear( Colorf::black() ); gl::setMatrices( mCamera ); // Iterate through hands const Leap::HandList& hands = mFrame.hands(); for ( Leap::HandList::const_iterator handIter = hands.begin(); handIter != hands.end(); ++handIter ) { const Leap::Hand& hand = *handIter; // Draw palm gl::color(1, .2, .4, 1); gl::drawSphere(LeapMotion::toVec3f(hand.palmPosition()),20,12); // Pointables const Leap::PointableList& pointables = hand.pointables(); for ( Leap::PointableList::const_iterator pointIter = pointables.begin(); pointIter != pointables.end(); ++pointIter ) { const Leap::Pointable& pointable = *pointIter; Vec3f dir = LeapMotion::toVec3f( pointable.direction() ); float length = pointable.length(); Vec3f tipPos = LeapMotion::toVec3f( pointable.tipPosition() ); Vec3f basePos = tipPos + dir * length; gl::drawColorCube( tipPos, Vec3f( 20, 20, 20 ) ); gl::color( ColorAf::gray( 0.8f ) ); gl::drawLine( basePos, tipPos ); } } }
void LeapMotionFrame::copyFromFrame(const Leap::Frame& frame, const F32& maxHandAxisRadius) { // This also resets all counters clear(); // Retrieve frame information mFrameValid = frame.isValid(); mFrameId = frame.id(); mFrameTimeStamp = frame.timestamp(); mFrameInternalId = smNextInternalFrameId; ++smNextInternalFrameId; mFrameSimTime = Sim::getCurrentTime(); mFrameRealTime = Platform::getRealMilliseconds(); if(!mFrameValid) { return; } // Retrieve hand information mHandCount = frame.hands().count(); if(mHandCount > 0) { copyFromFrameHands(frame.hands(), maxHandAxisRadius); } // Retrieve pointable information mPointableCount = frame.pointables().count(); if(mPointableCount > 0) { copyFromFramePointables(frame.pointables()); } }
// Runs update logic void UiApp::update() { // Update frame rate mFrameRate = getAverageFps(); // Toggle fullscreen if ( mFullScreen != isFullScreen() ) { setFullScreen( mFullScreen ); } // Interact with first hand const Leap::HandList& hands = mFrame.hands(); if ( hands.isEmpty() ) { mCursorType = CursorType::NONE; } else { const Leap::Hand& hand = *hands.begin(); // Update cursor position mCursorPositionTarget = warpVector( hand.palmPosition() ); if ( mCursorType == CursorType::NONE ) { mCursorPosition = mCursorPositionTarget; } // Choose cursor type based on number of exposed fingers switch ( hand.fingers().count() ) { case 0: mCursorType = CursorType::GRAB; // Slider if ( mSlider.getBounds().contains( mCursorPosition - mSliderPosition ) ) { float x1 = mTrackPosition.x; float x2 = mTrackPosition.x + (float)( mTrack.getWidth() - mSlider.getWidth() ); mSliderPosition.x = math<float>::clamp( mCursorPosition.x, x1, x2 ); } break; case 1: mCursorType = CursorType::TOUCH; // Buttons mFingerTipPosition = warpPointable( *hand.fingers().begin() ); for ( size_t i = 0; i < 3; ++i ) { mButtonState[ i ] = false; if ( mButton[ 0 ].getBounds().contains( mFingerTipPosition - mButtonPosition[ i ] ) ) { mButtonState[ i ] = true; } } break; default: mCursorType = CursorType::HAND; break; } } // Smooth cursor animation mCursorPosition = mCursorPosition.lerp( 0.21f, mCursorPositionTarget ); }
/** Returns 1 if abs(xvel) <= 1/2 abs(yvel) 0 otherwise */ virtual int evaluate(const Leap::Frame &frame, const std::string& nodeid) { Leap::Hand h = frame.hand(0); if (h.isValid()) { Leap::Vector vel = h.palmVelocity(); return (abs(vel.x) <= 0.5 * abs(vel.y)) ? 1 : 0; } else return 0; }
void ClipController::clip2H(const Leap::Frame& frame) { Hand hand = frame.hands().rightmost(); Hand other_hand = frame.hands().leftmost(); if (other_hand.grabStrength() > 0.95f) { VolumeController& vc = MainController::getInstance().volumeController(); const Mat4& inv = vc.getCamera().viewInverse(); Vec3 n = inv * hand.palmNormal().toVector4<Vec4>(); Vec3 p = frame.interactionBox().normalizePoint(hand.palmPosition()).toVector4<Vec4>(); p = (p - 0.5f) * 2.0f * 0.75f; p = inv * Vec4(p.x, p.y, p.z, 0.0f); selectedPlane().normal(n); selectedPlane().point(p); vc.markDirty(); } }