コード例 #1
0
void jester::LeapMotionImpl::processFingers(Leap::Hand hand, Bone::BoneId fingerOne, LeapHand whichHand) {
	Leap::FingerList fingerList = hand.fingers();
	std::vector<Leap::Finger> fingers;
	FingerData *fingerIds = (whichHand == LeapHand::LEFT ? kLeftFingerIds : kRightFingerIds);
	bool fingersFound[] = {false, false, false, false, false};

	//project finger tip positions onto palm plane and calculate angle
	for (Leap::FingerList::const_iterator fingerIter = fingerList.begin(); fingerIter != fingerList.end(); fingerIter++) {
		fingers.push_back(*fingerIter);
	}

	//if this is the first time we've seen the full hand, calibrate the finger lengths
	if (fingerIds[0].id == -1 && fingers.size() == 5) {
		if (whichHand == LeapHand::LEFT)
			std::sort(fingers.begin(), fingers.end(), left_hand_finger_x_coordinate_comparator);
		else if (whichHand == LeapHand::RIGHT)
			std::sort(fingers.begin(), fingers.end(), right_hand_finger_x_coordinate_comparator);
		else
			return;
		fingersFound[0] = fingersFound[1] = fingersFound[2] = fingersFound[3] = fingersFound[4] = true;

		for (int i = 0; i < 5; i++) {
			fingerIds[i].id = fingers[i].id();
			fingerIds[i].length = fingers[i].length();
		}
	} else if (fingerIds[0].id == -1)
		return;

	//search for all of the known finger ids
	for (unsigned int i = 0; i < 5; i++) {
		for (unsigned int j = 0; j < fingers.size(); j++) {
			if (fingers[j].id() == fingerIds[i].id) {
				fingersFound[i] = true;
				setFingerInJointData(fingers[j], static_cast<Bone::BoneId>(fingerOne + i));
				fingers.erase(fingers.begin() + j);
			}
		}
	}

	//match all unclaimed fingers based on closest size
	for (unsigned int i = 0; i < fingers.size(); i++) {
		float closestLengthDelta = FLT_MAX;
		int closestFingerIx = -1;

		for (int j = 0; j < 5; j++) {
			if (!fingersFound[j]) {
				float lengthDelta = abs(fingerIds[j].length - fingers[i].length());

				if (lengthDelta < closestLengthDelta) {
					closestLengthDelta = lengthDelta;
					closestFingerIx = j;
				}
			}
		}
		if (closestFingerIx >= 0) {
			fingersFound[closestFingerIx] = true;
			fingerIds[closestFingerIx].id = fingers[i].id();
			setFingerInJointData(fingers[i], static_cast<Bone::BoneId>(fingerOne + closestFingerIx));
		}
	}
}
コード例 #2
0
ファイル: fc_leap.cpp プロジェクト: RobDeBagel/fadecandy
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;
  }

}