void LeapMotionListener::onFrame(const Leap::Controller & controller)
{
    const Leap::Frame frame = controller.frame();

    Leap::HandList hands = frame.hands();
    for (Leap::HandList::const_iterator hl = hands.begin(); hl!=hands.end();hl++)
    {
        const Leap::Hand hand = *hl;
        QString handType = hand.isLeft() ? "Left hand" : "Right hand";
        qDebug()<<handType<<"id: "<<hand.id()<<"palm position: "
               <<hand.palmPosition().x<<hand.palmPosition().y<<hand.palmPosition().z;

        QFile f("share.dat");
        if(!f.open(QIODevice::WriteOnly | QIODevice::Text))
            return;
        QTextStream out(&f);
        out<<QString("%1 %2 %3").arg(hand.palmPosition().x)
          .arg(hand.palmPosition().y).arg(hand.palmPosition().z);

        f.close();

    }

}
Esempio n. 2
0
void jester::LeapMotionImpl::processLeapFrame(Leap::Frame frame) {
	Leap::HandList handList = frame.hands();
	std::vector<Leap::Hand> hands;
	Leap::Hand right, left;
	bool foundRight = false, foundLeft = false;

	clearJointData();

	//put all of the hands in a std::vector
	for (Leap::HandList::const_iterator handsIter = handList.begin(); handsIter != handList.end(); handsIter++)
		hands.push_back(*handsIter);

	std::sort(hands.begin(), hands.end(), finger_count_comparator);

	//search to see if last known hand Ids are still available, assign hand, and delete from available hands
	// also, delete any unknown hands that have no fingers
	for (unsigned int i = 0; i < hands.size(); i++)
		if (hands[i].id() == kRightHandId) {
			foundRight = true;
			right = hands[i];
			hands.erase(hands.begin() + i);
		} else if (hands[i].id() == kLeftHandId) {
			foundLeft = true;
			left = hands[i];
			hands.erase(hands.begin() + i);
		} else if (hands[i].fingers().count() == 0){
			hands.erase(hands.begin() + i);
		}

	//figure out how many hands we need
	int neededHands = 0;
	if (!foundLeft) neededHands++;
	if (!foundRight) neededHands++;

	//assume that the highest finger count objects are the most likely to be hands
	//only keep as many hands as we need (bounded by the number actually available)
	hands.erase(hands.begin() + std::min((int) hands.size(), neededHands), hands.end());

	//sort by x coordinate
	std::sort(hands.begin(), hands.end(), hand_x_coordinate_comparator);

	if (hands.size() == 0 || neededHands == 0) { //if we have or need no hands, do nothing here
	} else if (neededHands > (int) hands.size()) { //if we need two and only have 1
		//assume left side is left, right side is right
		if (hands[0].palmPosition()[0] < 0) {
			foundLeft = true;
			left = hands[0];
			kLeftHandId = left.id();
		} else {
			foundRight = true;
			right = hands[0];
			kRightHandId = right.id();
		}
	} else if (foundLeft == false && neededHands == 1) { //if we need left
		foundLeft = true;
		left = hands[0];
		kLeftHandId = left.id();
	} else if (foundRight == false && neededHands == 1) { //if we need right
		foundRight = true;
		right = hands[hands.size() - 1];
		kRightHandId = right.id();
	} else { //if we need both and have two
		foundLeft = foundRight = true;
		left = hands[0];
		kLeftHandId = left.id();
		right = hands[1];
		kRightHandId = right.id();
	}
	if (foundLeft)
		processHand(left, LeapHand::LEFT);
	if (foundRight)
		processHand(right, LeapHand::RIGHT);

	kController->suggestJointInfo(this, kJointData);
}
Esempio n. 3
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;
  }

}