Esempio n. 1
0
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();
}
Esempio n. 2
0
void HandController::drawBone(const Leap::Bone& bone) {
  static const ColorA bone_color(CM_HSV, 0.5f, 0.0f, 1.0f, 0.5f);
  
  Leap::Vector midpoint = (bone.prevJoint() + bone.prevJoint()) / 2.0;
  
  glPushMatrix();
  glTranslatef(midpoint.x, midpoint.y, midpoint.z);
  glMultMatrixf(GetBoneMatrix(bone));
  glRotatef(90, -1, 0, 0);
  
  glMaterialfv(GL_FRONT, GL_DIFFUSE, bone_color);
  gl::drawCylinder(HAND_CYLINDER_RADIUS, HAND_CYLINDER_RADIUS, bone.length(), 12, 1);
  glPopMatrix();
}
Esempio n. 3
0
void LeapMotionPlugin::processFrame(const Leap::Frame& frame) {
    // Default to uncontrolled.
    for (int i = 0; i < _joints.size(); i++) {
        _joints[i].position = glm::vec3();
    }

    auto hands = frame.hands();
    const int MAX_NUMBER_OF_HANDS = 2;
    for (int i = 0; i < hands.count() && i < MAX_NUMBER_OF_HANDS; i++) {
        auto hand = hands[i];

        int sideSign = hand.isLeft() ? LEFT_SIDE_SIGN : RIGHT_SIDE_SIGN;
        int jointIndex = hand.isLeft() ? LeapMotionJointIndex::LeftHand : LeapMotionJointIndex::RightHand;

        // Hand.
        _joints[jointIndex].position = LeapVectorToVec3(hand.wristPosition());
        _joints[jointIndex].orientation = LeapBasisToQuat(sideSign, hand.basis());

        // Fingers.
        // Leap Motion SDK guarantees full set of fingers and finger joints so can straightforwardly process them all.
        Leap::FingerList fingers = hand.fingers();
        for (int j = Leap::Finger::Type::TYPE_THUMB; j <= Leap::Finger::Type::TYPE_PINKY; j++) {
            Leap::Finger finger;
            finger = fingers[j];
            Leap::Bone bone;
            bone = finger.bone(Leap::Bone::Type::TYPE_PROXIMAL);
            jointIndex++;
            _joints[jointIndex].position = LeapVectorToVec3(bone.prevJoint());
            _joints[jointIndex].orientation = LeapBasisToQuat(sideSign, bone.basis());
            bone = finger.bone(Leap::Bone::Type::TYPE_INTERMEDIATE);
            jointIndex++;
            _joints[jointIndex].position = LeapVectorToVec3(bone.prevJoint());
            _joints[jointIndex].orientation = LeapBasisToQuat(sideSign, bone.basis());
            bone = finger.bone(Leap::Bone::Type::TYPE_DISTAL);
            jointIndex++;
            _joints[jointIndex].position = LeapVectorToVec3(bone.prevJoint());
            _joints[jointIndex].orientation = LeapBasisToQuat(sideSign, bone.basis());
            jointIndex++;
            _joints[jointIndex].position = LeapVectorToVec3(bone.nextJoint());
            _joints[jointIndex].orientation = LeapBasisToQuat(sideSign, bone.basis());
        }
    }
}
Esempio n. 4
0
void SampleListener::onFrame(const Leap::Controller& controller) {
  // Get the most recent frame and report some basic information
  const Leap::Frame frame = controller.frame();
  std::cout << "Frame id: " << frame.id()
            << ", timestamp: " << frame.timestamp()
            << ", hands: " << frame.hands().count()
            << ", extended fingers: " << frame.fingers().extended().count()
            << ", tools: " << frame.tools().count()
            << ", gestures: " << frame.gestures().count() << std::endl;

  Leap::HandList hands = frame.hands();
  for (Leap::HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl) {
    // Get the first hand
    const Leap::Hand hand = *hl;
    std::string handType = hand.isLeft() ? "Left hand" : "Right hand";
    std::cout << std::string(2, ' ') << handType << ", id: " << hand.id()
              << ", palm position: " << hand.palmPosition() << std::endl;
    // Get the hand's normal vector and direction
    const Leap::Vector normal = hand.palmNormal();
    const Leap::Vector direction = hand.direction();

    // Calculate the hand's pitch, roll, and yaw angles
    std::cout << std::string(2, ' ') <<  "pitch: " << direction.pitch() * Leap::RAD_TO_DEG << " degrees, "
              << "roll: " << normal.roll() * Leap::RAD_TO_DEG << " degrees, "
              << "yaw: " << direction.yaw() * Leap::RAD_TO_DEG << " degrees" << std::endl;

    // Get the Arm bone
    Leap::Arm arm = hand.arm();
    std::cout << std::string(2, ' ') <<  "Arm direction: " << arm.direction()
              << " wrist position: " << arm.wristPosition()
              << " elbow position: " << arm.elbowPosition() << std::endl;

    // Get fingers
    const Leap::FingerList fingers = hand.fingers();
    for (Leap::FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl) {
      const Leap::Finger finger = *fl;
      std::cout << std::string(4, ' ') <<  fingerNames[finger.type()]
                << " finger, id: " << finger.id()
                << ", length: " << finger.length()
                << "mm, width: " << finger.width() << std::endl;

      // Get finger bones
      for (int b = 0; b < 4; ++b) {
        Leap::Bone::Type boneType = static_cast<Leap::Bone::Type>(b);
        Leap::Bone bone = finger.bone(boneType);
        std::cout << std::string(6, ' ') <<  boneNames[boneType]
                  << " bone, start: " << bone.prevJoint()
                  << ", end: " << bone.nextJoint()
                  << ", direction: " << bone.direction() << std::endl;
      }
    }
  }

  // Get tools
  const Leap::ToolList tools = frame.tools();
  for (Leap::ToolList::const_iterator tl = tools.begin(); tl != tools.end(); ++tl) {
    const Leap::Tool tool = *tl;
    std::cout << std::string(2, ' ') <<  "Tool, id: " << tool.id()
              << ", position: " << tool.tipPosition()
              << ", direction: " << tool.direction() << std::endl;
  }

  // Get gestures
  const Leap::GestureList gestures = frame.gestures();
  for (int g = 0; g < gestures.count(); ++g) {
    Leap::Gesture gesture = gestures[g];

    switch (gesture.type()) {
      case Leap::Gesture::TYPE_CIRCLE:
      {
        Leap::CircleGesture circle = gesture;
        std::string clockwiseness;

        if (circle.pointable().direction().angleTo(circle.normal()) <= Leap::PI/2) {
          clockwiseness = "clockwise";
        } else {
          clockwiseness = "counterclockwise";
        }

        // Calculate angle swept since last frame
        float sweptAngle = 0;
        if (circle.state() != Leap::Gesture::STATE_START) {
          Leap::CircleGesture previousUpdate = Leap::CircleGesture(controller.frame(1).gesture(circle.id()));
          sweptAngle = (circle.progress() - previousUpdate.progress()) * 2 * Leap::PI;
        }
        std::cout << std::string(2, ' ')
                  << "Circle id: " << gesture.id()
                  << ", state: " << stateNames[gesture.state()]
                  << ", progress: " << circle.progress()
                  << ", radius: " << circle.radius()
                  << ", angle " << sweptAngle * Leap::RAD_TO_DEG
                  <<  ", " << clockwiseness << std::endl;
        break;
      }
      case Leap::Gesture::TYPE_SWIPE:
      {
        Leap::SwipeGesture swipe = gesture;
        std::cout << std::string(2, ' ')
          << "Swipe id: " << gesture.id()
          << ", state: " << stateNames[gesture.state()]
          << ", direction: " << swipe.direction()
          << ", speed: " << swipe.speed() << std::endl;
        break;
      }
      case Leap::Gesture::TYPE_KEY_TAP:
      {
        Leap::KeyTapGesture tap = gesture;
        std::cout << std::string(2, ' ')
          << "Key Tap id: " << gesture.id()
          << ", state: " << stateNames[gesture.state()]
          << ", position: " << tap.position()
          << ", direction: " << tap.direction()<< std::endl;
        break;
      }
      case Leap::Gesture::TYPE_SCREEN_TAP:
      {
        Leap::ScreenTapGesture screentap = gesture;
        std::cout << std::string(2, ' ')
          << "Screen Tap id: " << gesture.id()
          << ", state: " << stateNames[gesture.state()]
          << ", position: " << screentap.position()
          << ", direction: " << screentap.direction()<< std::endl;
        break;
      }
      default:
        std::cout << std::string(2, ' ')  << "Unknown gesture type." << std::endl;
        break;
    }
  }

  if (!frame.hands().isEmpty() || !gestures.isEmpty()) {
    std::cout << std::endl;
  }

}