Exemple #1
0
//--------------------------------------------------------------
void testApp::recordHandXML (Hand & hand, int handIndex){
	
	if (bRecordingThisFrame){
		if (hand.isValid()){
			
			int handTagNum = XML.addTag("H");
			if( XML.pushTag("H", handTagNum) ){
				XML.setValue("INDEX", handIndex, handTagNum);
				XML.setValue("ID", hand.id(), handTagNum);
				XML.setValue("TYPE", (hand.isLeft() ? "LEFT" : "RIGHT"), handTagNum);
				XML.setValue("CONF", hand.confidence(), handTagNum);
				
				FingerList fingers = hand.fingers();
				for (int f=0; f<fingers.count(); f++){
					const Finger & finger = fingers[f];
					
					if (finger.isValid()){
						recordFingerXML (finger);
					}
				}
				
				recordArmXML (hand);
				XML.popTag(); // pop H(AND)
			}
		}
	}
}
ofPoint LeapVisualizer::getIndexFingertip(ofxLeapMotion & leap){
    
    vector <Hand> hands = leap.getLeapHands();
    Hand hand;
    
	if (hands.size() > 0){
		for (int h=0; h<hands.size(); h++){
			hand = hands[h];
			if (hand.isValid()) break;
		}
	}
	
	// For each finger in the Hand,
	FingerList fingers = hand.fingers();
	for (int f=0; f<fingers.count(); f++)
    {
		// Get the current finger, and it's type (index, thumb, etc.);
		const Finger & finger = fingers[f];
		Finger::Type fingerType = finger.type();
		if (finger.isValid() && fingerType == Finger::TYPE_INDEX)
        {
            return ofPoint(finger.tipPosition().x, finger.tipPosition().y, finger.tipPosition().z);
		}
	}
    cout << "FINGER NOT FOUND" << endl;
    return ofPoint(-1.0f,-1.0f,-1.0f);
}
Exemple #3
0
Frame::FingerList
Frame::fingers() const
{
    FingerList fingers;

    for (auto finger : _leapFrame->fingers())
    {
        fingers.push_back(std::shared_ptr<Finger>(new Finger(finger)));
    }

    return fingers;
}
//callback function
void glListener::onFrame(const Controller& controller){
	
	/*memset(pitches, 0,sizeof(pitches));
	memset(rolls, 0,sizeof(rolls));
	memset(yaws, 0,sizeof(yaws));*/
	//Get the most recent frame and report some basic information
	const Frame frame = controller.frame();
	if(!frame.hands().empty()) 
	{
		//get the first hand
		const Hand hand = frame.hands()[0];
		//wrist
		pitches[0] = hand.palmNormal().pitch();
		rolls[0] = hand.palmNormal().roll();
		yaws[0] = hand.palmNormal().yaw();

		/*pitches[0] = hand.direction().pitch();
		rolls[0] = hand.direction().roll();
		yaws[0] = hand.direction().yaw();*/

		//check if the hand has any fingers
		const FingerList fingers = hand.fingers();
		//如果检测到手指且手指的数目等于5
		if (!fingers.empty() && fingers.count() == 5) {
			
			/*for(int i = 0; i < fingers.count(); i++) {
				
			}*/
			//Thumb
			yaws[2] = -fingers.leftmost().direction().pitch();
			//Index
			yaws[5] = -fingers[2].direction().pitch();
			//Middle
			yaws[8]= -fingers[0].direction().pitch();
			//Ring
			yaws[11]= -fingers[1].direction().pitch();
			//Little
			yaws[14]= -fingers.rightmost().direction().pitch();
			float temp = 0;
			//if index is at the right of ring
			if(fingers[2].tipPosition().x > fingers[1].tipPosition().x) {
				//swap the pitch angle of index and ring
				temp = yaws[5];
				yaws[5] = yaws[11];
				yaws[11] = temp;
			}
		}
	}
	pModel->animation(pitches,rolls,yaws);
}
Exemple #5
0
bool Detect::getGesture(std::string& ret)
{
	if(false == LeapController->isConnected())
	{
		return false;
	}
	HandList HL = LeapController->frame().hands();
	Hand     hand = HL[0];
	FingerList fingers = hand.fingers();
	GestureModel * gm = new GestureModel;
	Leap::Matrix handTransform = hand.basis();
	handTransform.origin = hand.palmPosition();
	handTransform = handTransform.rigidInverse();
	//get gesture data
	int i = 0;
	for (FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl) {
		const Finger finger = *fl;
		// Get finger bones
		for (int b = 0; b < 4; ++b) {
			Bone::Type boneType = static_cast<Bone::Type>(b);
			Bone bone = finger.bone(boneType);
			Leap::Vector ori = bone.prevJoint();
			Leap::Vector tP = handTransform.transformPoint(ori);
			gm->GestureData[i][b][0][0] = tP[0];
			gm->GestureData[i][b][0][1] = tP[1];
			gm->GestureData[i][b][0][2] = tP[2];
			tP = handTransform.transformPoint(bone.nextJoint());
			gm->GestureData[i][b][1][0] = tP[0];
			gm->GestureData[i][b][1][1] = tP[1];
			gm->GestureData[i][b][1][2] = tP[2];
			tP = handTransform.transformPoint(bone.direction());
			gm->GestureData[i][b][2][0] = gm->GestureData[i][b][1][0] - gm->GestureData[i][b][0][0];
			gm->GestureData[i][b][2][1] = gm->GestureData[i][b][1][1] - gm->GestureData[i][b][0][1];
			gm->GestureData[i][b][2][2] = gm->GestureData[i][b][1][2] - gm->GestureData[i][b][0][2];
		}
		i++;
	}

	//compare
	for (int index = 0; index < GestureList.size(); index++)
	{
		if (true == compareGesture(GestureList[index], gm))
		{
			ret = GestureList[index]->GestureName;
			return true;
		}
	}
	ret = " ";
	return false;
}
//--------------------------------------------------------------
void LeapVisualizer::drawFingers (Hand & hand,ofxLeapMotion & leap){
	
	// For each finger in the Hand,
	FingerList fingers = hand.fingers();
	for (int f=0; f<fingers.count(); f++){
		
		// Get the current finger, and it's type (index, thumb, etc.);
		const Finger & finger = fingers[f];
		if (finger.isValid()){
			
			drawFinger(finger,leap);
			
		} //end if finger isValid()
	} // end for each finger
}
void LeapBrowser::updateCursorLocation()
{
	bool is_index_found = false;

	Frame frame = leap_controller.frame();	
	HandList hands = frame.hands();
	for( int h=0; h < hands.count(); h++ )
	{
		Hand hand = hands[h];
		FingerList fingers = hand.fingers();
		for( int f=0; f < fingers.count(); f++ )
		{
			Finger finger = fingers[f];
			if( finger.type() == Finger::Type::TYPE_INDEX )
			{
				is_index_found = true;

				Vector position = finger.tipPosition();
				cursor_location = math::position(position.x, position.y, position.z) * leap_transform;

				break;
			}
		}
		if( is_index_found )
		{
			break;
		}
	}

	if( !is_index_found )
	{
		cursor_location = math::position(0,0,0);
		is_cursor_located = false;
	}
	else
	{
		is_cursor_located = true;
	}
}
void LeapController::onFrame(const Controller& controller) {
	//console() << "Leap: onFrame" << std::endl;
	// Get the most recent frame and report some basic information
	const Frame frame = controller.frame();
  
	/*
	console() << "Frame id: " << frame.id()
			<< ", timestamp: " << frame.timestamp()
			<< ", hands: " << frame.hands().count()
			<< ", fingers: " << frame.fingers().count()
			<< ", tools: " << frame.tools().count()
			<< ", gestures: " << frame.gestures().count() << std::endl;
	*/

	if (!frame.hands().empty()) {
		const FingerList fingers = frame.fingers();
		for(int k = 0; k < fingers.count(); k++){
			if (!fingers.empty()){
				// Calculate the hand's average finger tip position
				for (int i = 0; i < fingers.count(); ++i) {
					ci::Vec2f currentFingerPos(fingers[i].tipPosition().x, fingers[i].tipPosition().z*-1.0);
					fingerPositions[i] = currentFingerPos;

					ci::Vec2f currentFingerVel(fingers[i].tipVelocity().x, (fingers[i].tipVelocity().z));
					fingerVelocities[i] = currentFingerVel;
				}
				avgPos = fingers[0].tipPosition(); 
				numActiveFingers = fingers.count();
				hasFingers = true;
			}
			else{
				hasFingers = false;
			}
		}

		/*
		// Get the first hand
		const Hand hand = frame.hands()[0];

		// Check if the hand has any fingers
		const FingerList fingers = hand.fingers();
		if (!fingers.empty()){
			// Calculate the hand's average finger tip position
			
			for (int i = 0; i < fingers.count(); ++i) {
				ci::Vec2f currentFingerPos(fingers[i].tipPosition().x, fingers[i].tipPosition().y);
				fingerPositions[i] = currentFingerPos;

				ci::Vec2f currentFingerVel(fingers[i].tipVelocity().x, (fingers[i].tipVelocity().y*-1.0));
				fingerVelocities[i] = currentFingerVel;
			}
			avgPos = fingers[0].tipPosition(); 
			numActiveFingers = fingers.count();
			//console() << "Hand has " << fingers.count() << " fingers, average finger tip position" << avgPos << std::endl;

			hasFingers = true;
		}
		else{
			hasFingers = false;
		}
		*/

		/*
		// Get the hand's sphere radius and palm position
		console() << "Hand sphere radius: " << hand.sphereRadius()
				  << " mm, palm position: " << hand.palmPosition() << std::endl;

		// Get the hand's normal vector and direction
		const Vector normal = hand.palmNormal();
		const Vector direction = hand.direction();

		// Calculate the hand's pitch, roll, and yaw angles
		console() << "Hand pitch: " << direction.pitch() * RAD_TO_DEG << " degrees, "
				  << "roll: " << normal.roll() * RAD_TO_DEG << " degrees, "
				  << "yaw: " << direction.yaw() * RAD_TO_DEG << " degrees" << std::endl;
		*/
	}
	else{
		hasFingers = false;
	}

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

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

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

	if (!frame.hands().empty() || !gestures.empty()) {
		console() << std::endl;
	}*/
}
//--------------------------------------------------------------
void LeapVisualizer::drawPalm (Hand & hand,ofxLeapMotion & leap){
	// This draws the palm as a gray region.
	
	// Collect the palm vertices into an ofMesh.
	ofMesh palmMesh;
	int nVertices = 0;
	float averageBoneWidth = 0;
	
	// For each finger,
	FingerList fingers = hand.fingers();
	for (int f=0; f<fingers.count(); f++){
		
		// Get the current finger, and it's type (index, thumb, etc.);
		const Finger & finger = fingers[f];
		if (finger.isValid()){
			
			Finger::Type fingerType = finger.type();
			Bone bone;
			if (fingerType == Finger::TYPE_THUMB){
				bone = finger.bone(Bone::TYPE_PROXIMAL);
			} else {
				bone = finger.bone(Bone::TYPE_METACARPAL);
			}
			
			// If we've found the bones we want, add their vertices to the mesh.
			if (bone.isValid()){
				float boneLength = bone.length();
				if (boneLength > 0){
					
					ofPoint pt0 = leap.getofPoint ( bone.prevJoint());
					ofPoint pt1 = leap.getofPoint ( bone.nextJoint());
					
					palmMesh.addVertex(pt0);
					palmMesh.addVertex(pt1);
					
					averageBoneWidth += bone.width();
					nVertices += 2;
				}
			}
		}
	}
	averageBoneWidth /= (nVertices/2);
	
	// Render the palm as a triangle strip surface,
	// (optionally) bordered by cylinders.
	if (nVertices > 3){
		ofSetColor(ofColor::gray);
		
		// Draw the palm as a mesh of triangles.
		int nPalmMeshVertices = palmMesh.getNumVertices();
		for (int i=0; i<(nPalmMeshVertices-2); i++){
			palmMesh.addTriangle(i, i+1, i+2);
		}
		palmMesh.drawFaces();
		
		// Add optional cylinders.
		if (!bDrawSimple){
			float rad = averageBoneWidth / 2.0;
			
			if (nPalmMeshVertices == 10){
				for (int i=0; i<4; i++){
					ofVec3f p0 = palmMesh.getVertex( i   *2);
					ofVec3f p1 = palmMesh.getVertex((i+1)*2);
					drawOrientedCylinder (p0, p1, 10);
					ofDrawSphere(p0, rad);
					ofDrawSphere(p1, rad);
				}
				
				for (int i=0; i<4; i++){
					ofVec3f p0 = palmMesh.getVertex( i   *2 + 1);
					ofVec3f p1 = palmMesh.getVertex((i+1)*2 + 1);
					drawOrientedCylinder (p0, p1, 10);
					ofDrawSphere(p0, rad);
					ofDrawSphere(p1, rad);
				}
			}
		}
		
	}
}
void LeapListener::onFrame(const Controller & controller) {
    // Get the most recent frame and report some basic information
    const 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;

    HandList hands = frame.hands();
    for (HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl) {
        // Get the first hand
        const 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 Vector normal = hand.palmNormal();
        const Vector direction = hand.direction();

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

        // Get the Arm bone
        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 FingerList fingers = hand.fingers();
        for (FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl) {
            const 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) {
                Bone::Type boneType = static_cast<Bone::Type>(b);
                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 ToolList tools = frame.tools();
    for (ToolList::const_iterator tl = tools.begin(); tl != tools.end(); ++tl) {
        const Tool tool = *tl;
        std::cout << std::string(2, ' ') <<  "Tool, id: " << tool.id()
        << ", position: " << tool.tipPosition()
        << ", direction: " << tool.direction() << std::endl;
    }

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

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

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

                // Calculate angle swept since last frame
                float sweptAngle = 0;
                if (circle.state() != Gesture::STATE_START) {
                    CircleGesture previousUpdate = CircleGesture(controller.frame(1).gesture(circle.id()));
                    sweptAngle = (circle.progress() - previousUpdate.progress()) * 2 * PI;
                }
                std::cout << std::string(2, ' ')
                << "Circle id: " << gesture.id()
                << ", state: " << stateNames[gesture.state()]
                << ", progress: " << circle.progress()
                << ", radius: " << circle.radius()
                << ", angle " << sweptAngle * RAD_TO_DEG
                <<  ", " << clockwiseness << std::endl;
                break;
            }
            case Gesture::TYPE_SWIPE: {
                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 Gesture::TYPE_KEY_TAP: {
                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 Gesture::TYPE_SCREEN_TAP: {
                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;
}
Exemple #11
0
void QTVS_Leap::FingerLogic(handIndex hIndex)
{

  if (ui.checkBox_gesturesParangus->isChecked())
    ParangusGestureLogic();

  if (ui.checkBox_gesturesLeap->isChecked())
    LeapGestureLogic();

//DEBUG: Atm we cancel
  // return;

  // 0 -> 4 = left hand
  // 5 -> 9 = right hand
  // So lets use a shift and no redundant code
  int iHandToFingerShift = 0;
  if (hIndex == handRight)
    iHandToFingerShift += 5;

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

    // Convert Leap::Vector position to Screen Coords
    QPoint screenPosition = FingerCursorPosition(position);


    // Lerp coords for smoothness if required
    int xCoord = lerp(fingerTraces.at(finger.type() + iHandToFingerShift)->geometry().left(), screenPosition.x(), dMouseLerpValue);
    int yCoord = lerp(fingerTraces.at(finger.type() + iHandToFingerShift)->geometry().top(), screenPosition.y(), dMouseLerpValue);

    // Qt Doesn't allow different threads to overwride gui locations.
    // Therefore, the usage of events are well, used.
    // This updates visual finger representation to correct location
    QMoveEvent *tEvent = new QMoveEvent(QPoint(xCoord, yCoord), fingerTraces.at(finger.type() + iHandToFingerShift)->pos());
    QCoreApplication::postEvent(fingerTraces.at(finger.type() + iHandToFingerShift), tEvent);

    // Z axis does NOT use stabilized position.
    // Stabilized position is generally used for X/Y movement (2D display)
    // Therefore Z is updated quite poorely. So we used unStabalized position instead
    // Setup position limits (manual testing for values
    position = finger.tipPosition();
    float zFinger = position.z < 0 ? 0 : position.z > 200 ? 200 : position.z;

    // Convert to percentages
    // trim lowerend of percentages
    zFinger /= 200;
    if (zFinger < 0.1) zFinger = 0.1;

    if (hIndex == handLeft)
    {
        if(ui.checkBox_FingerDragsWindows->isChecked())
        {
      // We're on index finger and its close to screen / center of Z-plane on leap motion
      if (finger.type() == leapIndex && zFinger < 0.12)
      {
        POINT pt;
        pt.x = screenPosition.x();
        pt.y = screenPosition.y();


        // if our rect was reset
        // Therefore, we just started to drag
        if (debugWindowDrag_Left.left == -1)
        {
          // Find window under point
          // Find window's dimmensions
          // Find difference between player's finger coords and window's coords
          debugWindowHWND_Left = GetRealParent(WindowFromPoint(pt));

          // Set it as active window if need be.
          if(debugWindowHWND_Left != GetForegroundWindow() && ui.checkBox_DragSetsActive->isChecked())
          {
            SetForegroundWindow(debugWindowHWND_Left);
            // Backup incase SetForegroundWindow fails
            // TODO: Perhaps replace in future? since foreground window DOES faile occasionally.
            // SetWindowPos(debugWindowHWND_Left,HWND_TOPMOST,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE);
          }

          // Restore window if maximized Section
          WINDOWPLACEMENT wndpl = { sizeof(WINDOWPLACEMENT) };
          GetWindowPlacement(debugWindowHWND_Left, &wndpl);

          // Determine if window is maximized to begin with
          if (wndpl.showCmd == SW_MAXIMIZE)
          {
            // Setup the restore command and send it
            wndpl.showCmd = SW_RESTORE;
            SetWindowPlacement(debugWindowHWND_Left, &wndpl);

            // Center restored window around player's fingers
            int iTempWindowWidth = wndpl.rcNormalPosition.right - wndpl.rcNormalPosition.left;
            int iTempWindowHeight = wndpl.rcNormalPosition.bottom - wndpl.rcNormalPosition.top;
            MoveWindow(debugWindowHWND_Left, pt.x - iTempWindowWidth / 2, pt.y - iTempWindowHeight / 2,
                       iTempWindowWidth, iTempWindowHeight, true);
          }

          GetWindowRect(debugWindowHWND_Left, &debugWindowDrag_Left);
          debugWindowDrag_Left.left = pt.x - debugWindowDrag_Left.left;
          debugWindowDrag_Left.top = pt.y -  debugWindowDrag_Left.top;
          debugWindowDrag_Left.bottom = pt.y;//debugWindowDrag_Left.bottom - pt.y;
          debugWindowDrag_Left.right = pt.x;//debugWindowDrag_Left.bottom - pt.y;
        }

        // Setup temp rect for window's width/height
        RECT tRect;
        GetWindowRect(debugWindowHWND_Left, &tRect);

        debugDisplayString = QString::number(pt.y);

        // If left hand and right hand have different HWNDs on drag
        // Proceed with normal drag
        if (debugWindowHWND_Left != debugWindowHWND_Right)
        {
          WINDOWPLACEMENT wndpl = { sizeof(WINDOWPLACEMENT) };
          GetWindowPlacement(debugWindowHWND_Left, &wndpl);

          // AeroSnap time

          // Maximize window on top
          if (pt.y <= 30 && wndpl.showCmd != SW_MAXIMIZE)
          {
            iDebugWindowRestore_LeftHand = 1;
            iDebug_PreviousWindowWidth_LeftHand = tRect.right - tRect.left;
            iDebug_PreviousWindowHeight_LeftHand = tRect.bottom - tRect.top;
            wndpl.showCmd = SW_SHOWMAXIMIZED;
            SetWindowPlacement(debugWindowHWND_Left, &wndpl);
          }

          // AeroSnap Left
          else if (pt.x <= 30)
          {
            if (iDebugWindowRestore_LeftHand == 0)
            {
              iDebugWindowRestore_LeftHand = 1;

              iDebug_PreviousWindowWidth_LeftHand = tRect.right - tRect.left;
              iDebug_PreviousWindowHeight_LeftHand = tRect.bottom - tRect.top;

              int wndwidth = (rcMonitor.right - rcMonitor.left) / 2;
              int wndheight = rcMonitor.bottom - rcMonitor.top;//max(min((rcMonitor.bottom-rcMonitor.top), state.mmi.ptMaxTrackSize.y), state.mmi.ptMinTrackSize.y);
              int posx = rcMonitor.left;
              int posy = rcMonitor.top + (rcMonitor.bottom - rcMonitor.top) / 2 - wndheight / 2; // Center vertically (if window has a max height)
              MoveWindow(debugWindowHWND_Left, posx, posy,
                         wndwidth, wndheight, true);
            }
          }

          // AeroSnap Left
          else if (pt.x >= (rcMonitor.right - rcMonitor.left) - 30)
          {
            if (iDebugWindowRestore_LeftHand == 0)
            {
              iDebugWindowRestore_LeftHand = 1;

              iDebug_PreviousWindowWidth_LeftHand = tRect.right - tRect.left;
              iDebug_PreviousWindowHeight_LeftHand = tRect.bottom - tRect.top;

              int wndwidth = (rcMonitor.right - rcMonitor.left) / 2;
              int wndheight = rcMonitor.bottom - rcMonitor.top;//max(min((rcMonitor.bottom-rcMonitor.top), state.mmi.ptMaxTrackSize.y), state.mmi.ptMinTrackSize.y);
              int posx = rcMonitor.right - wndwidth;
              int posy = rcMonitor.top + (rcMonitor.bottom - rcMonitor.top) / 2 - wndheight / 2; // Center vertically (if window has a max height)
              MoveWindow(debugWindowHWND_Left, posx, posy,
                         wndwidth, wndheight, true);
            }
          }


          // Normal Drag
          else if ( pt.y > 30 && pt.x > 30  && (pt.x < (rcMonitor.right - rcMonitor.left) - 30 ))
          {

            // some other custom snap is in effect
            if (iDebugWindowRestore_LeftHand == 1)
            {
              iDebugWindowRestore_LeftHand = 0;

              if (wndpl.showCmd == SW_MAXIMIZE)
              {
                // Setup the restore command and send it
                wndpl.showCmd = SW_RESTORE;
                SetWindowPlacement(debugWindowHWND_Left, &wndpl);
              }
              MoveWindow(debugWindowHWND_Left, pt.x - iDebug_PreviousWindowWidth_LeftHand / 2, pt.y - iDebug_PreviousWindowHeight_LeftHand / 2,
                         iDebug_PreviousWindowWidth_LeftHand, iDebug_PreviousWindowHeight_LeftHand, true);
            }

            // Nope, just normal drag
            else if (wndpl.showCmd != SW_MAXIMIZE)
              MoveWindow(debugWindowHWND_Left, pt.x - debugWindowDrag_Left.left, pt.y - debugWindowDrag_Left.top, tRect.right - tRect.left, tRect.bottom - tRect.top, true);
          }

        }

        // Left and Right hand have same HWNDs
        // Resize mode engadged
        else if(ui.checkBox_ResizeWindows->isChecked()) {
          // Left hand's overview:
          // Horizontal movement = simultaneious change in window's coords + change in width.
          // Vertical movement = change in window's height only.

          // Left reference points:
          // X = hwnd.left
          // Y = hwnd.bottom

          RECT resizeRect;

          resizeRect.top = tRect.top;
          resizeRect.left = tRect.left + (pt.x - debugWindowDrag_Left.right);// / 5;
          resizeRect.right = (tRect.right - resizeRect.left);// - (pt.x - debugWindowDrag_Left.left) ;/// 5;
          resizeRect.bottom = (tRect.bottom - tRect.top) + (pt.y - debugWindowDrag_Left.bottom);// / 5;

          debugWindowDrag_Left.right = pt.x;
          debugWindowDrag_Left.bottom = pt.y;

          MoveWindow(debugWindowHWND_Left, resizeRect.left, resizeRect.top, resizeRect.right , resizeRect.bottom, true);

        }

      }
      else if (finger.type() == leapIndex && zFinger > 0.15)
      {
        debugWindowDrag_Left.left = -1;
        debugWindowHWND_Left = NULL;
      }
    }
    }

    if (hIndex == handRight)
    {

        if(ui.checkBox_FingerDragsWindows->isChecked())
        {
      if (finger.type() == leapIndex && zFinger < 0.12)
      {

        // debugDisplayString = "TRYIGN";
        POINT pt;
        pt.x = screenPosition.x() - 15;
        pt.y = screenPosition.y() - 15;


        if (debugWindowDrag_Right.left == -1)
        {
          debugWindowHWND_Right = GetRealParent(WindowFromPoint(pt));
        
          if(debugWindowHWND_Right != GetForegroundWindow() && ui.checkBox_DragSetsActive->isChecked())
          {
            SetForegroundWindow(debugWindowHWND_Right);
            // Backup incase SetForegroundWindow fails
            // TODO: Perhaps replace in future? since foreground window DOES faile occasionally.
            // SetWindowPos(debugWindowHWND_Left,HWND_TOPMOST,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE);
          }

          GetWindowRect(debugWindowHWND_Right, &debugWindowDrag_Right);
          debugWindowDrag_Right.left = pt.x - debugWindowDrag_Right.left;
          debugWindowDrag_Right.top = pt.y -  debugWindowDrag_Right.top;
          debugWindowDrag_Right.bottom = pt.y;//debugWindowDrag_Left.bottom - pt.y;
          debugWindowDrag_Right.right = pt.x;//debugWindowDrag_Left.bottom - pt.y;
        }

        RECT tRect;
        GetWindowRect(debugWindowHWND_Right, &tRect);

        if (debugWindowHWND_Left != debugWindowHWND_Right)
        {
          WINDOWPLACEMENT wndpl = { sizeof(WINDOWPLACEMENT) };
          GetWindowPlacement(debugWindowHWND_Right, &wndpl);

          // AeroSnap time

          // Maximize window on top
          if (pt.y <= 30 && wndpl.showCmd != SW_MAXIMIZE)
          {
            iDebugWindowRestore_RightHand = 1;
            iDebug_PreviousWindowWidth_RightHand = tRect.right - tRect.left;
            iDebug_PreviousWindowHeight_RightHand = tRect.bottom - tRect.top;
            wndpl.showCmd = SW_SHOWMAXIMIZED;
            SetWindowPlacement(debugWindowHWND_Right, &wndpl);
          }

          // AeroSnap Left
          else if (pt.x <= 30)
          {
            if (iDebugWindowRestore_RightHand == 0)
            {
              iDebugWindowRestore_RightHand = 1;

              iDebug_PreviousWindowWidth_RightHand = tRect.right - tRect.left;
              iDebug_PreviousWindowHeight_RightHand = tRect.bottom - tRect.top;

              int wndwidth = (rcMonitor.right - rcMonitor.left) / 2;
              int wndheight = rcMonitor.bottom - rcMonitor.top;//max(min((rcMonitor.bottom-rcMonitor.top), state.mmi.ptMaxTrackSize.y), state.mmi.ptMinTrackSize.y);
              int posx = rcMonitor.left;
              int posy = rcMonitor.top + (rcMonitor.bottom - rcMonitor.top) / 2 - wndheight / 2; // Center vertically (if window has a max height)
              MoveWindow(debugWindowHWND_Right, posx, posy,
                         wndwidth, wndheight, true);
            }
          }

          // AeroSnap Left
          else if (pt.x >= (rcMonitor.right - rcMonitor.left) - 30)
          {
            if (iDebugWindowRestore_RightHand == 0)
            {
              iDebugWindowRestore_RightHand = 1;

              iDebug_PreviousWindowWidth_RightHand = tRect.right - tRect.left;
              iDebug_PreviousWindowHeight_RightHand = tRect.bottom - tRect.top;

              int wndwidth = (rcMonitor.right - rcMonitor.left) / 2;
              int wndheight = rcMonitor.bottom - rcMonitor.top;//max(min((rcMonitor.bottom-rcMonitor.top), state.mmi.ptMaxTrackSize.y), state.mmi.ptMinTrackSize.y);
              int posx = rcMonitor.right - wndwidth;
              int posy = rcMonitor.top + (rcMonitor.bottom - rcMonitor.top) / 2 - wndheight / 2; // Center vertically (if window has a max height)
              MoveWindow(debugWindowHWND_Right, posx, posy,
                         wndwidth, wndheight, true);
            }
          }


          // Normal Drag
          else if ( pt.y > 30 && pt.x > 30  && (pt.x < (rcMonitor.right - rcMonitor.left) - 30 ))
          {

            // some other custom snap is in effect
            if (iDebugWindowRestore_RightHand == 1)
            {
              iDebugWindowRestore_RightHand = 0;

              if (wndpl.showCmd == SW_MAXIMIZE)
              {
                // Setup the restore command and send it
                wndpl.showCmd = SW_RESTORE;
                SetWindowPlacement(debugWindowHWND_Right, &wndpl);
              }
              MoveWindow(debugWindowHWND_Right, pt.x - iDebug_PreviousWindowWidth_RightHand / 2, pt.y - iDebug_PreviousWindowHeight_RightHand / 2,
                         iDebug_PreviousWindowWidth_RightHand, iDebug_PreviousWindowHeight_RightHand, true);
            }

            // Nope, just normal drag
            else if (wndpl.showCmd != SW_MAXIMIZE)
              MoveWindow(debugWindowHWND_Right, pt.x - debugWindowDrag_Right.left, pt.y - debugWindowDrag_Right.top, tRect.right - tRect.left, tRect.bottom - tRect.top, true);
          }
        }
        else if (ui.checkBox_ResizeWindows->isChecked()){
          // Right hand's overview:
          // Horizontal movement = change in width.
          // Vertical movement = simultaneious change in window's coords + change in window's height only.

          // debug_iReferencePointX_Right =  tRect.left + debugWindowDrag_Right.left;// + debug_iReferencePointY_Left;
          // debug_iReferencePointY_Right =  tRect.top + debugWindowDrag_Right.top;// + debug_iReferencePointY_Left;



          // RECT resizeRect;

          // resizeRect.top = tRect.top;
          // resizeRect.left = tRect.left + (pt.x - debugWindowDrag_Left.right);// / 5;
          // resizeRect.right = (tRect.right - resizeRect.left);// - (pt.x - debugWindowDrag_Left.left) ;/// 5;
          // resizeRect.bottom = (tRect.bottom - tRect.top) + (pt.y - debugWindowDrag_Left.bottom);// / 5;

          // debugWindowDrag_Left.right = pt.x;
          // debugWindowDrag_Left.bottom = pt.y;



          RECT resizeRect;

          resizeRect.top = tRect.top + (pt.y - debugWindowDrag_Right.bottom);
          resizeRect.left = tRect.left;
          resizeRect.right = (tRect.right - tRect.left) + (pt.x - debugWindowDrag_Right.right);
          resizeRect.bottom = (tRect.bottom - resizeRect.top);// - (pt.y - debug_iReferencePointY_Right) / 5;


          // debugWindowDrag_Right.left += (pt.x - debug_iReferencePointX_Right) / 5;
          // debugWindowDrag_Right.top += (pt.y - debug_iReferencePointY_Right) / 5;
          debugWindowDrag_Right.right = pt.x;
          debugWindowDrag_Right.bottom = pt.y;


          MoveWindow(debugWindowHWND_Right, resizeRect.left, resizeRect.top, resizeRect.right , resizeRect.bottom, false);

        }
        // debugDisplayString = QString::number(finger.tipPosition().z);
      }
      else if (finger.type() == leapIndex && zFinger > 0.15)
      {
        debugWindowDrag_Right.left = -1;
        debugWindowHWND_Right = NULL;
      }
        }
    }
    zFinger *= 75.0;

    QResizeEvent *rEvent = new QResizeEvent(QSize(zFinger, zFinger), fingerTraces.at(finger.type() + iHandToFingerShift)->size());
    QCoreApplication::postEvent(fingerTraces.at(finger.type() + iHandToFingerShift), rEvent);


    // switch (finger.type())
    // {
    // case leapThumb:
    // {
    //   // if(finger.isExtended())
    //   // {
    //     // QShowEvent * sEvent = new QShowEvent();
    //     // QCoreApplication::postEvent(thumbTrace, sEvent);
    //   int xCoord = lerp(thumbTrace->geometry().left(), screenPosition.x(), dMouseLerpValue);
    //   int yCoord = lerp(thumbTrace->geometry().top(), screenPosition.y(), dMouseLerpValue);

    //   QMoveEvent *tEvent = new QMoveEvent(QPoint(xCoord, yCoord), thumbTrace->pos());
    //   QCoreApplication::postEvent(thumbTrace, tEvent);
    //   // }
    //   // else
    //   // {
    //     // QHideEvent * hEvent = new QHideEvent();
    //     // QCoreApplication::postEvent(thumbTrace, hEvent);
    //   // }
    //   // thumbTrace->move(xCoord, yCoord);
    // }
    //   // indexTrace;
    //   // middleTrace;
    //   // ringTrace;
    //   // pinkieTrace;

    // break;

    // case leapIndex:

    // {
    //   int xCoord = lerp(indexTrace->geometry().left(), screenPosition.x(), dMouseLerpValue);
    //   int yCoord = lerp(indexTrace->geometry().top(), screenPosition.y(), dMouseLerpValue);

    //   QMoveEvent *tEvent = new QMoveEvent(QPoint(xCoord, yCoord), indexTrace->pos());
    //   QCoreApplication::postEvent(indexTrace, tEvent);
    //   // thumbTrace->move(xCoord, yCoord);
    // }
    // break;

    // case leapMiddle:

    // {
    //   int xCoord = lerp(middleTrace->geometry().left(), screenPosition.x(), dMouseLerpValue);
    //   int yCoord = lerp(middleTrace->geometry().top(), screenPosition.y(), dMouseLerpValue);

    //   QMoveEvent *tEvent = new QMoveEvent(QPoint(xCoord, yCoord), middleTrace->pos());
    //   QCoreApplication::postEvent(middleTrace, tEvent);
    //   // thumbTrace->move(xCoord, yCoord);
    // }
    // break;

    // case leapRing:

    // {
    //   int xCoord = lerp(ringTrace->geometry().left(), screenPosition.x(), dMouseLerpValue);
    //   int yCoord = lerp(ringTrace->geometry().top(), screenPosition.y(), dMouseLerpValue);

    //   QMoveEvent *tEvent = new QMoveEvent(QPoint(xCoord, yCoord), ringTrace->pos());
    //   QCoreApplication::postEvent(ringTrace, tEvent);
    //   // thumbTrace->move(xCoord, yCoord);
    // }
    // break;

    // case leapPinkie:

    // {
    //   int xCoord = lerp(pinkieTrace->geometry().left(), screenPosition.x(), dMouseLerpValue);
    //   int yCoord = lerp(pinkieTrace->geometry().top(), screenPosition.y(), dMouseLerpValue);

    //   QMoveEvent *tEvent = new QMoveEvent(QPoint(xCoord, yCoord), pinkieTrace->pos());
    //   QCoreApplication::postEvent(pinkieTrace, tEvent);
    //   // thumbTrace->move(xCoord, yCoord);
    // }
    // break;
    // }
  }
}
Exemple #12
0
void LeapListener::onFrame(const Controller &controller){
	const Frame frame = controller.frame();
	HandList hands = frame.hands();

	std::vector<HandModel> myhands;
	BoneModel mybone;
	HandModel myhand;
	//printf("frame: %d\n", frame.id());
	float scale = 0.025;
	float disp = 3.5f;
	bool record = true;
	for (int i = 0; i < hands.count(); i++){
		FingerList fingers = hands[i].fingers();
		myhand.bones.clear();
		myhand.references.clear();

		Vector handPosition = hands[i].palmPosition();
		handPosition *= scale;
		handPosition.y -= disp;

		Vector wrist = hands[i].wristPosition();
		wrist *= scale;
		wrist.y -= disp;

		myhand.palmPosition = handPosition;
		myhand.palmNormal = hands[i].palmNormal();
		myhand.direction = hands[i].direction();

		for (int j = 0; j < fingers.count(); j++){
			Bone bone;
			Bone::Type boneType;

			Vector currentPosition = fingers[j].tipPosition();
			currentPosition *= scale;
			currentPosition.y -= disp;

			Vector lastPosition = m_lastFrame.finger(fingers[j].id()).tipPosition();
			lastPosition *= scale;
			lastPosition.y -= disp;

			Vector diff = currentPosition - lastPosition;

			//printf("%f, %f, %f\n", abs(diff.x), abs(diff.y), abs(diff.z));
			
			//if (abs(diff.x) > 0.2 || abs(diff.y) > 0.2 || abs(diff.z) > 0.2) record = false;
			if (abs(diff.x) < 0.0001 || abs(diff.y) < 0.0001 || abs(diff.z) < 0.0001) record = false;

			for (int k = 0; k < 4; k++){
				boneType = static_cast<Bone::Type>(k);
				bone = fingers[j].bone(boneType);

				if (fingers[j].type() == Finger::Type::TYPE_THUMB && k == 0) continue;

				Vector prevPos = bone.prevJoint();
				prevPos *= scale;
				prevPos.y -= disp;

				Vector nextPos = bone.nextJoint();
				nextPos *= scale;
				nextPos.y -= disp;

				mybone.direction = nextPos - prevPos;
				mybone.position = (prevPos + nextPos) / 2;
				mybone.prevJoint = prevPos;
				mybone.nextJoint = nextPos;
				mybone.length = bone.length() * scale;

				if (boneType == Bone::Type::TYPE_PROXIMAL){
					if (fingers[j].type() == Finger::Type::TYPE_THUMB){
						myhand.thumb = prevPos;
						//myhand.references.push_back(nextPos);
					}
					else{
						myhand.references.push_back(prevPos);
					}
				}
				
				myhand.bones.push_back(mybone);
			}
		}

		myhand.base = myhand.references[3] - myhand.references[0];
		Vector disp = myhand.base.normalized() * 0.3f;
		myhand.references[3] += disp;
		myhand.references[0] -= disp;

		//for (int i = 1; i < 5; i++){
		//	printf("%f\n", myhand.references[i - 1].distanceTo(myhand.references[i]));
		//}

		myhands.push_back(myhand);
	}

	if (record){
		m_hands.clear();
		for (int i = 0; i < myhands.size(); i++){
			m_hands.push_back(myhands[i]);
		}
	}

	m_lastFrame = frame;
}
Exemple #13
0
int main(int argc, char** argv)
{
	struct sockaddr_in si_other;
	int s, slen = sizeof(si_other);
	char buf[BUFLEN];
	char message[BUFLEN];
	WSADATA wsa;

	//Initialise winsock
	printf("\nInitialising Winsock...");
	if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
	{
		printf("Failed. Error Code : %d", WSAGetLastError());
		exit(EXIT_FAILURE);
	}
	printf("Initialised.\n");

	//create socket
	//if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR)
	if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET)
	{
		printf("socket() failed with error code : %d", WSAGetLastError());
		exit(EXIT_FAILURE);
	}
	char str[INET_ADDRSTRLEN];
	//setup address structure
	memset((char *)&si_other, 0, sizeof(si_other));
	si_other.sin_family = AF_INET;
	si_other.sin_port = htons(PORT);
	//si_other.sin_addr.S_un.S_addr = inet_pton(AF_INET, SERVER, &(si_other.sin_addr));
	inet_pton(AF_INET, SERVER, &(si_other.sin_addr.S_un.S_addr));
	/*if (inet_pton(AF_INET, SERVER, &(si_other.sin_addr.S_un.S_addr)))
	{
		inet_ntop(AF_INET, &(si_other.sin_addr.S_un.S_addr), str, INET_ADDRSTRLEN);
		std::cout << ("%s\n", str);
	}*/

	// We catch any exceptions that might occur below -- see the catch statement for more details.
	try {

		// First, we create a Hub with our application identifier. Be sure not to use the com.example namespace when
		// publishing your application. The Hub provides access to one or more Myos.
		myo::Hub hub("om.example.emg-data-sample");

		std::cout << "Attempting to find a Myo..." << std::endl;

		// Next, we attempt to find a Myo to use. If a Myo is already paired in Myo Connect, this will return that Myo
		// immediately.
		// waitForMyo() takes a timeout value in milliseconds. In this case we will try to find a Myo for 10 seconds, and
		// if that fails, the function will return a null pointer.
		myo::Myo* myo = hub.waitForMyo(10000);

		// If waitForMyo() returned a null pointer, we failed to find a Myo, so exit with an error message.
		if (!myo) {
			throw std::runtime_error("Unable to find a Myo!");
		}

		// We've found a Myo.
		std::cout << "Connected to a Myo armband!" << std::endl << std::endl;

		// Next we enable EMG streaming on the found Myo.
		myo->setStreamEmg(myo::Myo::streamEmgEnabled);

		// Create a sample listener and controller for Leap Motion
		SampleListener listener;
		Controller controller;

		// Next we construct an instance of our DeviceListener, so that we can register it with the Hub.
		DataCollector collector;
		double timeElasped = 0.000;
		const double minMax[10] = { 32, 85, 36, 100, 37, 107, 36, 100, 36, 90 }; //T.I.M.R.P

		// Hub::addListener() takes the address of any object whose class inherits from DeviceListener, and will cause
		// Hub::run() to send events to all registered device listeners.
		hub.addListener(&collector);

		//controller.addListener(listener);

		if (argc > 1 && strcmp(argv[1], "--bg") == 0)
			controller.setPolicy(Leap::Controller::POLICY_BACKGROUND_FRAMES);
		
		myfile << std::fixed;
		myfile << std::setprecision(2);

		// Finally we enter our main loop.
		while (1) {
			//collector.tic();
			// In each iteration of our main loop, we run the Myo event loop for a set number of milliseconds.
			// In this case, we wish to update our display 50 times a second, so we run for 1000/20 milliseconds.
			hub.run(1000 / 100);
			// After processing events, we call the print() member function we defined above to print out the values we've
			// obtained from any events that have occurred.
			collector.print();
			int i = 0;
			int j = 1;
			int h = 0;
			double fingDis[5];
			const Frame frame = controller.frame();
			HandList hands = frame.hands();
			for (HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl) {
				// Get the first hand
				const Hand hand = *hl;
				// Get fingers
				const FingerList fingers = hand.fingers();
				
				for (FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl) {
					const Finger finger = *fl;

					//myfile << " " << hand.palmPosition().distanceTo(finger.tipPosition());
					/*myfile << std::string(4, ' ') << fingerNames[finger.type()]
						<< ": " << listener.mapping(hand.palmPosition().distanceTo(finger.tipPosition()), minMax[i + i], minMax[i + j]);*/
					fingDis[h] = listener.mapping(hand.palmPosition().distanceTo(finger.tipPosition()), minMax[i + i], minMax[i + j]);
					//fingDis[h] = hand.palmPosition().distanceTo(finger.tipPosition());
					i++;
					j++;
					h++;
						if (i == 5 && j == 6 && h == 5)
						{
							string tmp = to_string(fingDis[0]) + " " + to_string(fingDis[1]) + " " + to_string(fingDis[2]) + " " + to_string(fingDis[3]) + " " + to_string(fingDis[4]);
							//string tmp = to_string('0');
							strcpy_s(message, tmp.c_str());
							//send message
							if (sendto(s, message, strlen(message), 0, (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
							{
								printf("sendto() failed with error code : %d", WSAGetLastError());
								//exit(EXIT_FAILURE);
							}
							std::cout << "Data Sent";
							i = 0;
							j = 1;
							h = 0;
						}
				}
			}

			//timeElasped = timeElasped + ((double)(clock() - tictoc_stack.top())) / CLOCKS_PER_SEC;
			/*myfile << " Time elapsed: "
				<< ((double)(clock() - tictoc_stack.top())) / CLOCKS_PER_SEC;*/
			//tictoc_stack.pop();
			//myfile << " " << timeElasped << endl;

		}
		
		// If a standard exception occurred, we print out its message and exit.
	}
	catch (const std::exception& e) {
		std::cerr << "Error: " << e.what() << std::endl;
		std::cerr << "Press enter to continue.";
		std::cin.ignore();
		return 1;
	}
	closesocket(s);
	WSACleanup();
}
Exemple #14
0
void LeapHander::frame(pugi::xml_node &frameNode){
	Leap::Frame currentFrame = m_sampleListener.frame();
	m_lock.lock();
	frameNode.append_attribute("id").set_value(currentFrame.id());
	pugi::xml_node handList = frameNode.append_child("hands");
	HandList hands = currentFrame.hands();
	
	for (HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl) {
		// Get the first hand
		const Hand hand = *hl;
		pugi::xml_node handNode = handList.append_child("hand");
		handNode.append_attribute("id").set_value(hand.id());
		std::string handType;
		if (hand.isLeft()) {
			handType = "Left";
		}
		else {
			handType = "Right";
		}
		handNode.append_attribute("type").set_value(handType.c_str());
		
		pugi::xml_node positionNode = handNode.append_child("position");
		positionToXml(positionNode, hand.palmPosition());
		
		/*pugi::xml_node normalNode = handNode.append_child("normal");
		positionToXml(normalNode, hand.palmNormal());

		pugi::xml_node directionNode = handNode.append_child("direction");
		positionToXml(directionNode, hand.direction());

		pugi::xml_node rotationNode = handNode.append_child("basis");
		rotationToXml(rotationNode, hand.basis());*/
		//// Get fingers
		pugi::xml_node fingerList = handNode.append_child("fingers");
		const FingerList fingers = hand.fingers();
		for (FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl) {
			const Finger finger = *fl;
			pugi::xml_node fingerNode = fingerList.append_child("finger");
			fingerNode.append_attribute("id").set_value(finger.id());
			fingerNode.append_attribute("name").set_value(fingerNames[finger.type()].c_str());
			pugi::xml_node boneList = fingerNode.append_child("bones");
			

			// Get finger bones
			for (int b = 0; b < 4; ++b) {
				Bone::Type boneType = static_cast<Bone::Type>(b);
				Bone bone = finger.bone(boneType);
				pugi::xml_node boneNode = boneList.append_child("bone");
				boneNode.append_attribute("length").set_value(bone.length());
				boneNode.append_attribute("name").set_value(boneNames[boneType].c_str());

				pugi::xml_node prevJoint = boneNode.append_child("prevJoint");
				positionToXml(prevJoint, bone.prevJoint());

				pugi::xml_node nextJoint = boneNode.append_child("nextJoint");
				positionToXml(nextJoint, bone.nextJoint());

				/*pugi::xml_node rotation = boneNode.append_child("basis");
				rotationToXml(rotation, bone.basis());*/
			}
		}
	}
	m_lock.unlock();
}
void LeapBrowser::drawHands()
{
	Frame frame = leap_controller.frame();

	math::vector trans = leap_transform.translation;
	math::quater rot_quat = leap_transform.rotation;
	math::vector rot_vect = math::ln( rot_quat );
	
	double angle = rot_vect.length() * 2.0;
	math::vector axis = ( angle != 0 ? rot_vect / angle : rot_vect );

	glMatrixMode( GL_MODELVIEW );
	glPushMatrix();
	glTranslatef( trans.x(), trans.y(), trans.z() );
	if( angle != 0 )
	{
		glRotatef( angle * 180.0 / M_PI, axis.x(), axis.y(), axis.z() );
	}

	HandList hands = frame.hands();
	for( HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl )
	{
		const Hand hand = *hl;

		float joint_radius = 5.0f;
		float bone_radius = 4.5f;

		Vector prev_palm_start(0,0,0);
		Vector prev_palm_end(0,0,0);

		int f = 0;
		const FingerList fingers = hand.fingers();
		for( FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl, ++f )
		{
			const Finger finger = *fl;

			Vector curr_palm_start(0,0,0);
			Vector curr_palm_end(0,0,0);

			for( int b=0; b < 4; b++ )
			{
				Bone::Type bone_type = static_cast<Bone::Type>( b );
				Bone bone = finger.bone( bone_type );

				Vector start = bone.prevJoint();
				Vector end = bone.nextJoint();

				math::position p0( start.x, start.y, start.z );
				math::position p1( end.x, end.y, end.z );

				if( is_tracking_pose == true && finger.type() == Finger::Type::TYPE_INDEX && b==3 )
				{
					drawing_tool.setColor( 1, 0, 0, 1 );
				}
				else
				{
					drawing_tool.setColor( 0.5, 0.7, 0.5, 1 );
				}
				drawing_tool.drawSphere( p1, joint_radius );
				
				drawing_tool.setColor( 0.5, 0.7, 0.5, 1 );
				drawing_tool.drawSphere( p0, joint_radius );
				drawing_tool.drawCylinder( p0, p1, bone_radius );

				//
				if( b == 0 && fl != fingers.begin() || b == 1 && fl == fingers.begin() )
				{
					curr_palm_start = start;
					curr_palm_end = end;
				}
			}

			if( f > 1 )	//fl != fingers.begin() )
			{
				drawing_tool.setColor( 0.5, 0.7, 0.5, 1 );
				drawing_tool.applyColor();

				glBegin( GL_QUADS );
				glVertex3f( prev_palm_start.x, prev_palm_start.y, prev_palm_start.z );
				glVertex3f( prev_palm_end.x, prev_palm_end.y, prev_palm_end.z );
				glVertex3f( curr_palm_end.x, curr_palm_end.y, curr_palm_end.z );
				glVertex3f( curr_palm_start.x, curr_palm_start.y, curr_palm_start.z );
				glEnd();
			}

			prev_palm_start = curr_palm_start;
			prev_palm_end = curr_palm_end;
		}
	}

	glPopMatrix();
}
Exemple #16
0
void SampleListener::onFrame(const Controller& controller)
{
    bool thand, index = false;

    // Get the most recent frame and report some basic information
    const Frame frame = controller.frame();

    // Receive Hand List
    HandList hands = frame.hands();

    // For all hands
    for (HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl)
    {
        // Get the first hand
        const Hand hand         = *hl;

        // Only for right hand
        if( hand.isRight() )
        {
            // Get fingers
            const FingerList fingers = hand.fingers();
            // For all fingers in the list
            for (FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl)
            {
                      Bone      bone;
                const Finger    finger = *fl;

                // If thamb or Index finger
                if( finger.type() == Finger::TYPE_THUMB )
                {
                    // Receive nided bone
                    bone        = finger.bone(Bone::TYPE_DISTAL);
                    m_vThand    = bone.center();

                    thand = true;
                }
                else if( finger.type() == Finger::TYPE_INDEX )
                {
                    // Receive nided bone
                    bone        = finger.bone(Bone::TYPE_DISTAL);
                    m_vIndex    = bone.center();

                    index = true;
                }
            }
        }
    }

    bool newState;
    if( !thand || !index )
    {
        newState = false;
    }
    else
    {
        float distance = m_vThand.distanceTo(m_vIndex);
        qDebug () << "Distance: " << distance;

        if( distance < 40 )
        {
            newState = true;
        }
        else
        {
            newState = false;
        }
    }

    if( newState != m_bLastState )
    {
        Q_EMIT releySignal( newState );
        m_bLastState = newState;
    }
}
Exemple #17
0
void LeapHands::onFrame(const Controller& controller) 
{
	//Game::Instance()->PrintFloat("Hand children: ", childrenMap.size());	
	std::map<string, BoneData> tempBoneData;
	HandList hands = controller.frame().hands();
	int handId = 0;

	hands[0].fingers()[(int) Finger::Type::TYPE_THUMB];

	for (HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl) {
		// Get the first hand
		const Hand hand = *hl;
		// Get fingers
		const FingerList fingers = hand.fingers();
		int fingerId = 0;
		bool firstFinger = true;
		Finger previousFinger;

		stringstream ass;
		ass << "Arm: 0 Hand: " << handId;
		tempBoneData[ass.str()] = BoneData(ass.str(), LeapToGlVec3(hand.arm().wristPosition()), LeapToGlVec3(hand.arm().elbowPosition()), true);
		ass.clear();

		glm::vec3 thumbBone = LeapToGlVec3(hand.fingers()[Finger::Type::TYPE_THUMB].bone(Bone::Type::TYPE_DISTAL).nextJoint());
		glm::vec3 indexBone = LeapToGlVec3(hand.fingers()[Finger::Type::TYPE_INDEX].bone(Bone::Type::TYPE_DISTAL).nextJoint());
		pinchDist = glm::length(thumbBone - indexBone);
		if (pinchDist < 5.0f)
		{
			pinch = true;
		}
		else
		{
			pinch = false;
		}

		for (FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl) {
			const Finger finger = *fl;

			// Get finger bones
			for (int b = 0; b < 4; ++b) {
				Bone::Type boneType = static_cast<Bone::Type>(b);
				Bone bone = finger.bone(boneType);
				stringstream ss;
				ss << "Hand: " << handId << " Finger: " << fingerId << " Bone: " << b;
				tempBoneData[ss.str()] = BoneData(ss.str(), LeapToGlVec3(bone.prevJoint()), LeapToGlVec3(bone.nextJoint()), true);


			}
			// Draw some other bits of the hand
			if (!firstFinger)
			{
				for (int b = 0; b < 2; ++b)
				{
					stringstream ss;
					ss << "Hand: " << handId << "Finger: " << (fingerId - 1) << "Finger: " << (fingerId) << " Bone: " << b;
					Bone startBone = previousFinger.bone(static_cast<Bone::Type>(b));
					Bone endBone = finger.bone(static_cast<Bone::Type>(b));
					if ((b == 1) && (fingerId == 1))
					{
						tempBoneData[ss.str()] = BoneData(ss.str(), LeapToGlVec3(startBone.nextJoint()), LeapToGlVec3(endBone.prevJoint()), false);
					}
					else
					{
						tempBoneData[ss.str()] = BoneData(ss.str(), LeapToGlVec3(startBone.prevJoint()), LeapToGlVec3(endBone.prevJoint()), false);
					}
				}
			}

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

				switch (gesture.type())
				{
					case Gesture::TYPE_CIRCLE:
					{
						CircleGesture circle = gesture;
						if (gesture.durationSeconds() > 1)
						{
							if (circle.pointable().direction().angleTo(circle.normal()) <= PI / 2) {
								
								spawn = vehicle;
							}
							else {
								spawn = model;
							}
						}
					}
				}
			}

			previousFinger = finger;
			firstFinger = false;
			++fingerId;
		}
		++handId;
	}	
	EnterCriticalSection(&criticalSection);
	trackedHands = handId;
	map<string, BoneData>::iterator it = tempBoneData.begin();
	boneData = tempBoneData;
	LeaveCriticalSection(&criticalSection);
}
Exemple #18
0
void QTVS_Leap::ParangusGestureLogic()
{
  if (ui.checkBox_PalmForSwipes->isChecked())
  {

    Leap::Vector palmPosition = controller.frame(3).hands()[0].palmPosition();

    float previousPalmYPosAndDifference = palmPosition.y;
    float previousPalmXPosAndDifference = palmPosition.x;

    previousPalmYPosAndDifference = hand.palmPosition().y - previousPalmYPosAndDifference;
    previousPalmXPosAndDifference = hand.palmPosition().x - previousPalmXPosAndDifference;

    // std::cout << previousFingerXPosAndDifference << "\n";

    if (previousPalmYPosAndDifference < -2.5 && handCache.bGestureToggle)
    {
      std::cout << "down \n";
      handCache.bGestureToggle = false;
      QtConcurrent::run(this, &QTVS_Leap::ParangusGesture, NULL, swipe_Down);
    }

    if (previousPalmYPosAndDifference > 2.5 && handCache.bGestureToggle)
    {
      std::cout << "swipe_Up \n";
      handCache.bGestureToggle = false;
      QtConcurrent::run(this, &QTVS_Leap::ParangusGesture, NULL, swipe_Up);
    }

    if (previousPalmXPosAndDifference < -2.5 && handCache.bGestureToggle)
    {
      std::cout << "swipe_Left \n";
      handCache.bGestureToggle = false;
      QtConcurrent::run(this, &QTVS_Leap::ParangusGesture, NULL, swipe_Left);
    }

    if (previousPalmXPosAndDifference > 2.5 && handCache.bGestureToggle)
    {
      std::cout << "swipe_Right \n";
      handCache.bGestureToggle = false;
      QtConcurrent::run(this, &QTVS_Leap::ParangusGesture, NULL, swipe_Right);
    }


    if (!handCache.bGestureToggle &&
        abs(previousPalmYPosAndDifference) <= 0.5 &&
        abs(previousPalmXPosAndDifference) <= 0.5 )
      handCache.bGestureToggle = true;
    return;
  }

  // if palm for swipes isn't checked, we go for fingers instead:
  for (FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl)
  {
    const Finger finger = *fl;

//To simplify things down the road
    int leapFingerIndex = finger.type();

    if (finger.isExtended())
    {
      Leap::Vector fingerPosition = controller.frame(3).fingers()[leapFingerIndex].stabilizedTipPosition();

      float previousFingerYPosAndDifference = fingerPosition.y;
      float previousFingerXPosAndDifference = fingerPosition.x;

      previousFingerYPosAndDifference = finger.stabilizedTipPosition().y - previousFingerYPosAndDifference;
      previousFingerXPosAndDifference = finger.stabilizedTipPosition().x - previousFingerXPosAndDifference;

      // std::cout << previousFingerXPosAndDifference << "\n";

      if (previousFingerYPosAndDifference < -10 && handCache.fingers_p[leapFingerIndex].bGestureToggle)
      {
        handCache.fingers_p[leapFingerIndex].bGestureToggle = false;
        QtConcurrent::run(this, &QTVS_Leap::ParangusGesture, leapFingerIndex, swipe_Down);
      }

      if (previousFingerYPosAndDifference > 10 && handCache.fingers_p[leapFingerIndex].bGestureToggle)
      {
        handCache.fingers_p[leapFingerIndex].bGestureToggle = false;
        QtConcurrent::run(this, &QTVS_Leap::ParangusGesture, leapFingerIndex, swipe_Up);
      }

      if (previousFingerXPosAndDifference < -10 && handCache.fingers_p[leapFingerIndex].bGestureToggle)
      {
        handCache.fingers_p[leapFingerIndex].bGestureToggle = false;
        QtConcurrent::run(this, &QTVS_Leap::ParangusGesture, leapFingerIndex, swipe_Left);
      }

      if (previousFingerXPosAndDifference > 10 && handCache.fingers_p[leapFingerIndex].bGestureToggle)
      {
        handCache.fingers_p[leapFingerIndex].bGestureToggle = false;
        QtConcurrent::run(this, &QTVS_Leap::ParangusGesture, leapFingerIndex, swipe_Right);
      }


      if (!handCache.fingers_p[leapFingerIndex].bGestureToggle &&
          abs(previousFingerYPosAndDifference) <= 0.1 &&
          abs(previousFingerXPosAndDifference) <= 0.1 )
        handCache.fingers_p[leapFingerIndex].bGestureToggle = true;
    }
  }
}