void AbstractLeapGestureManager::update(){
	cout << "update\n";
	pastFrame = frame;
	frame = controller.frame();
	HandList hands = frame.hands();
		pastLeft = left;
		pastRight = right;
		for (HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl){
			Hand hand = *hl;
			if (hand.isLeft()){
				left = hand;

			}
			else{
				right = hand;

			}
		
	}
	
}
Example #2
0
void MyListener::onFrame(const Controller& controller) 
{
    const Frame frame = controller.frame();
    const GestureList gestures = frame.gestures();
    HandList hands = frame.hands();
    
    for (HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl) {
        const Hand hand = *hl;
        // Smoothing and stabilization is performed in order to make this 
        // value more suitable for interaction with 2D content. The stabilized 
        // position lags behind the palm position by a variable amount, 
        // depending primarily on the speed of movement.
        Vector position = hand.stabilizedPalmPosition();

        if (m_positionChanged)
            m_positionChanged(position[0], position[1], position[2], 
                              frame.fingers().extended().count(), 
                              hand.direction(),
                              hand.palmVelocity());

        if (m_pinch)
            m_pinch(hand.pinchStrength());

        if (m_grab)
            m_grab(hand.grabStrength());
    }

    for (int g = 0; g < gestures.count(); ++g) {
        Gesture gesture = gestures[g];
        switch (gesture.type()) {
        case Gesture::TYPE_KEY_TAP:
        case Gesture::TYPE_SCREEN_TAP:
            if (m_tapped)
                m_tapped();
            break;
        case Gesture::TYPE_SWIPE:
            break;
        }
    }
}
Example #3
0
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 SampleListener::onFrame(const Controller& controller) {

    // Get the most recent frame and report some basic information
    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;
        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();

        // Get fingers
        const FingerList fingers = hand.fingers();
        const Finger finger = *(++fingers.begin()); // get Index finger
        std::cout << std::string(4, ' ') << fingerNames[finger.type()]
            << " finger, id: " << finger.id()
            << ", length: " << finger.length()
            << "mm, width: " << finger.width() << std::endl;

        Bone::Type boneType = static_cast<Bone::Type>(3); // Get distal bone for character recognization
        Bone bone = finger.bone(boneType);
        //output_file << finger.id()
        //<< ", " << bone.nextJoint().x << ", " << bone.nextJoint().y << ", " << bone.nextJoint().z << std::endl;

        // 2D-3D transformation
        if (finger.id() != finger_id) {
            finger_id = finger.id(); // reset the data
            array_counter = 0;
        }
        else array_counter++;
        finger_array_x[array_counter] = bone.nextJoint().x;
        finger_array_y[array_counter] = bone.nextJoint().y;
        finger_array_z[array_counter] = bone.nextJoint().z;
    }
}
Example #5
0
void Leap::LeapListener::onFrame( const Controller& controller )
{
	Frame frame = controller.frame();
	HandList hands = frame.hands();
	Leap::DirectionDetector::Direction direction;
	//bool handExtended;
	Hand leftHand;
	Hand rightHand;

	//jurik
	//takin just first gesture (gestures are defined for each finger)
	Gesture gesture = frame.gestures()[0];


	if ( arMode ) {
		for ( int i=0; i< hands.count(); ++i ) {
			if ( hands[i].isRight() ) {
				rightHand = hands[i];
			}
			else {
				leftHand = hands[i];
			}
		}
		leapActions->updateARHands( leftHand,rightHand );
	}
	else {
		for ( int i=0; i< hands.count(); ++i ) {
			if ( hands[i].isRight() ) {
				direction = Leap::DirectionDetector::getPalmDirection( hands[i] );
				//using cameramanipulator
				//leapActions->changeViewAngle( direction );
				//using pickhandler class
				leapActions->rotateAruco( direction );

				if ( gesture.type() == Gesture::TYPE_KEY_TAP ) {
					leapActions->scaleNodes( true );
				}
			}
			else {
				direction = Leap::DirectionDetector::getPalmDirection( hands[i] );
				//leapActions.changeViewAngle( direction );
				leapActions->scaleEdges( direction );
				if ( gesture.type() == Gesture::TYPE_KEY_TAP ) {
					leapActions->scaleNodes( false );
				}

				/*handExtended = Leap::FingerPositionDetector::isHandExtended( hands[i] );
				if ( handExtended ) {
					leapActions->startMovingForward();
				}
				else {
					leapActions->stopMovingForward();
				}*/
			}
		}
	}

	//std::cout << "id: " << frame.id();
	/*
	    const GestureList gestures = frame.gestures();
	      for (int g = 0; g < gestures.count(); ++g) {
	        Gesture gesture = gestures[g];

	        HandList hands = gesture.hands();
	        Hand firstHand = hands[0];

	        switch (gesture.type()) {
	          case Gesture::TYPE_CIRCLE:
	          {
				leapActions->zoomGraph(gesture);
	            break;
	          }
	          case Gesture::TYPE_SWIPE:
	          {
	            if(firstHand.isRight()){
					if(leapActions->isCameraMoving)
						leapActions->moveCamera(gesture);
	                else
					  leapActions->rotateGraph(gesture);
	            }
	            break;
	          }
	          case Gesture::TYPE_KEY_TAP:
	          {
	            if(firstHand.isLeft())
					leapActions->onKeyTap(gesture);
	            break;
	          }
	          case Gesture::TYPE_SCREEN_TAP:
	          {
				leapActions->onScreenTap(gesture);
	            break;
	          }
	          default:
	            qDebug() << "Unknown gesture type.";
	            break;
	        }
	      }*/


}
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;
}
Example #7
0
//metodo en bucle que actualiza la informacion de frecuencia, volumen y handsGl
void leapMotion::onFrame(const Controller& controller) {
	
	const Frame frame = controller.frame();

	int j = 0;
	float mejor = 1000;
	float mejorVol = 1000;
	int nada=-1000;
	Vector vNada = Vector(nada,nada,nada);
	Matrix mNada = Matrix(vNada,vNada,vNada);

	HandList hands = frame.hands();
	numManos = hands.count();

	for(int i=0; i<numManos; i++){
	
		palma[i] = hands[i].palmPosition();
		//anchuraPalmas[i] = hands[i].palmWidth();
		direccionMano[i] = hands[i].basis();

		float distVol=palma[i].y;

		if((palma[i].x/50)<-4 && (palma[i].x/50)>-7 && distVol<=mejorVol) mejorVol=distVol;
		
		//if(distVol<=mejorVol) mejorVol=distVol;

		for (int f = 0; f < hands[i].fingers().count(); f++) {
			Finger finger = hands[i].fingers()[f];
      
			for (int b = 0; b < 4; b++) {
				Bone bone = finger.bone(static_cast<Leap::Bone::Type>(b));
				handsGl[i*20+f*4+b]=bone.nextJoint();

				float dist=sqrt(pow(4.125-(handsGl[i*20+f*4+b].x/50),2)+pow((-0.75)-(handsGl[i*20+f*4+b].z/50),2));
				if(dist<=mejor) mejor=dist;

				centroHuesos[i*20+f*4+b]=bone.nextJoint();
				direccionHuesos[i*20+f*4+b]=bone.basis();
				longitudHuesos[i*20+f*4+b]=bone.length();
				anchuraHuesos[i*20+f*4+b]=bone.width()/2;
			}
		}
	}

	for(int i=numManos*20;i<40;i++) handsGl[i] = vNada;
	for(int i=numManos; i<2; i++) palma[i] = vNada;
	//for(int i=numManos; i<2; i++) anchuraPalmas[i] = 0;
	for(int i=numManos; i<2; i++) direccionMano[i] = mNada;
	for(int i=numManos*20; i<40; i++) direccionHuesos[i] = mNada;
	for(int i=numManos*20; i<40; i++) centroHuesos[i] = vNada;
	for(int i=numManos*20; i<40; i++) longitudHuesos[i] = 0;
	for(int i=numManos*20; i<40; i++) anchuraHuesos[i] = 0;

	if(mejor<=0.1) freq=7902.13; //se supone que al tocar la antena del theremin, se produce una frecuencia de sonido muy alta
	else if(mejor<=5){
		freq=4000*(1-mejor/5);
	}
	else freq=0;

	if(mejorVol>=250 && mejorVol<=350) volumen=1*((mejorVol-250)/100);
	else if(mejorVol<250) volumen=0;
	else volumen=1;
	
}
Example #8
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;
    }
}
Example #9
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;
}
Example #10
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();
}
Example #11
0
void SampleListener::onFrame(const Controller& controller) {
	//tictoc_stack.push(clock());
	// Get the most recent frame and report some basic information
	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;
		std::string handType = hand.isLeft() ? "Left hand" : "Right hand";
		
		const Vector normal = hand.palmNormal();
		const Vector direction = hand.direction();

		// Get the Arm bone
		Arm arm = hand.arm();

		// Get fingers
		const FingerList fingers = hand.fingers();
		for (FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl) {
			const Finger finger = *fl;
	
			myfile << std::string(4, ' ') << fingerNames[finger.type()]
				<< ": " << hand.palmPosition().distanceTo(finger.tipPosition());

			// Get finger bones
			for (int b = 0; b < 4; ++b) {
				Bone::Type boneType = static_cast<Bone::Type>(b);
				Bone bone = finger.bone(boneType);
			}
		}
	}

	// Get tools
	const ToolList tools = frame.tools();
	for (ToolList::const_iterator tl = tools.begin(); tl != tools.end(); ++tl) {
		const Tool tool = *tl;
	}

	// 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;
			}
			break;
		}
		case Gesture::TYPE_SWIPE:
		{
			SwipeGesture swipe = gesture;
			break;
		}
		case Gesture::TYPE_KEY_TAP:
		{
			KeyTapGesture tap = gesture;
			break;
		}
		case Gesture::TYPE_SCREEN_TAP:
		{
			ScreenTapGesture screentap = gesture;
			break;
		}
		default:
			break;
		}
	}

	if (!frame.hands().isEmpty() || !gestures.isEmpty()) {
		std::cout << std::endl;
	}
	myfile << " Time elapsed: "
		<< ((double)(clock() - tictoc_stack.top())) / CLOCKS_PER_SEC;
	tictoc_stack.pop();
	myfile << endl;

}
Example #12
0
/* ----------------------------------------------------------------------------------------- */
void CListener::onFrame(const Controller& controller) {
  usleep(timeout);        // timeout to move the arm close to real time

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

  if(frame.hands().count() == 0)    // do nothing if no hand is involved
    return;

  if(frame.hands().count() > 1) {     // do nothing if more than one hand is involved
    //std::cout << "More than one hand detected, returning to center position" << std::endl;
    //rarm.set_to_mid();
    return;
  }
  
  // ------------------------ MAIN LOGIC
  // ------------------------
  // GRIPPER
  new_pos[GRIPPER] = Robot_arm::ranges[GRIPPER][MAX] - frame.fingers().extended().count() * Robot_arm::gripper_offset;  // move GRIPPER in safe ranges
  //std::cout <<  "new_pos[GRIPPER]: " << new_pos[GRIPPER] << 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;
    Arm arm = hand.arm();
    const Vector direction = hand.direction();
    const Vector normal = hand.palmNormal();

    // ------------------------------
    // ELBOW
    new_pos[ELBOW] = Robot_arm::ranges[ELBOW][MAX] - int(Robot_arm::elbow_offset * hand.palmPosition()[Y]);
    //std::cout <<  "new_pos[ELBOW]: " << new_pos[ELBOW] << std::endl;


    // ------------------------------
    // BASE
    if(arm.direction()[X] < 0)
      new_pos[BASE] = Robot_arm::ranges[BASE][CENTER] - int(Robot_arm::base_offset * fabs(arm.direction()[X]));
    else
      new_pos[BASE] = Robot_arm::ranges[BASE][CENTER] + int(Robot_arm::base_offset * fabs(arm.direction()[X]));
    //std::cout <<  "new_pos[BASE]: " << new_pos[BASE] << std::endl;
 

    // ------------------------------
    // WRIST ROTATE
    if(direction.pitch() * RAD_TO_DEG < 0)
      new_pos[WRIST_ROTATE] = Robot_arm::ranges[WRIST_ROTATE][CENTER] - int(Robot_arm::wrist_rotate_offset * fabs(direction.pitch() * RAD_TO_DEG));
    else
      new_pos[WRIST_ROTATE] = Robot_arm::ranges[WRIST_ROTATE][CENTER] + int(Robot_arm::wrist_rotate_offset * fabs(direction.pitch() * RAD_TO_DEG));
    //std::cout <<  "new_pos[WRIST_ROTATE]: " << new_pos[WRIST_ROTATE] << std::endl;


    // ------------------------------
    // WRIST
    if(normal.roll() * RAD_TO_DEG < 0)
      new_pos[WRIST] = Robot_arm::ranges[WRIST][CENTER] + int(Robot_arm::wrist_offset * fabs(normal.roll() * RAD_TO_DEG));
    else
      new_pos[WRIST] = Robot_arm::ranges[WRIST][CENTER] - int(Robot_arm::wrist_offset * fabs(normal.roll() * RAD_TO_DEG));
    //std::cout <<  "new_pos[WRIST]: " << new_pos[WRIST] << std::endl;


    // ------------------------------
    // SHOULDER
    new_pos[SHOULDER] = Robot_arm::ranges[SHOULDER][MIN] + int(Robot_arm::shoulder_offset * arm.elbowPosition()[Z]);
    //std::cout <<  "new_pos[SHOULDER]: " << new_pos[SHOULDER] << std::endl;
  }

  // filtration
  if(filter != 0) {
    int * tmp_pos = rarm.get_pos();

    for(int i = 0; i < Robot_arm::num_servos; i++) {
      if(fabs((tmp_pos[i] - new_pos[i]) / (double)filter_const) > filter) {      // move only if greater than filter
        rarm.move(new_pos);
        break;
      }
    }
    return;
  }

  rarm.move(new_pos);

}
Example #13
0
void LeapInput::onFrame(const Controller& controller)
{
	Frame frame = controller.frame();
	HandList hands = frame.hands();
	this->hands = hands.count();
	if(hands.count() == 2)
	{
		Hand leftHand = hands.leftmost();
		Hand rightHand = hands.rightmost();
		leftHandY = leftHand.palmPosition().y;
		rightHandY = rightHand.palmPosition().y;
		leftFingers = leftHand.fingers().count();
		rightFingers = rightHand.fingers().count();

		float threshold = tanf(5* 3.141592f / 180.0f);
		float maxTan = tanf(45 * 3.141592f / 180.0f);
		if(leftHand.palmPosition().x != rightHand.palmPosition().x){
			float tanValue = fabs((leftHand.palmPosition().y - rightHand.palmPosition().y) / (leftHand.palmPosition().x - rightHand.palmPosition().x));
			if(tanValue > threshold){
				curve = tanValue  / maxTan;
				if(curve > 1)
					curve = 1;
			}else
				curve = 0;
		}else
			curve = 0;
	}
	else
	{
		leftHandY = rightHandY = 0;
	}

	boost = false;
	GestureList gestureList = frame.gestures();
	for(GestureList::const_iterator i = gestureList.begin(); i != gestureList.end(); ++i)
	{
		Gesture gesture = *i;
		if(gesture.state() != Gesture::State::STATE_INVALID)
		{
			switch(gesture.type())
			{
			case Gesture::Type::TYPE_SCREEN_TAP:
				boost = true;
				break;
			}
			
		}
	}

	back = false;
	if(hands.count() == 2 && leftFingers == 1 && rightFingers == 1 &&
		hands.leftmost().fingers()[0].direction().z > 0 &&
		hands.rightmost().fingers()[0].direction().z > 0)
	{
		back = true;
	}

	//accelGesture = brakeGesture = false;
	//GestureList gestureList = frame.gestures();
	//for(GestureList::const_iterator i = gestureList.begin(); i != gestureList.end(); ++i)
	//{
	//	Gesture gesture = *i;
	//	if(gesture.state() != Gesture::State::STATE_INVALID)
	//	{
	//		if(typeid(gesture) == typeid(MyGesture))
	//		{
	//			accelGesture = true;
	//		}
	//		
	//	}
	//}
}
Example #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();
}
Example #15
0
void MangoListener::onFrame(const Controller& controller) {
  const Frame frame = controller.frame();
    HandList hands = frame.hands();
    int extendedFingers = 0;
    for (HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl) {
        const Hand hand = *hl;
        for (int i = 0; i < hand.fingers().count(); i++)
        {
            Finger finger = hand.fingers()[i];
            if(finger.isExtended()) extendedFingers++;
        }

        if (!onGesture && !frame.hands().isEmpty() && frame.hands().count() == 1 && extendedFingers == 0)
        {
            preGestureCounter++;
            std::cout << preGestureCounter << std::endl;
            if (preGestureCounter > MAX_PREGESTURE && frameCount == 0)
            {
                onGesture = true;
                preGestureCounter = 0;
                frameCount++;
                note.show();
            }
        }
        else if (preGestureCounter > 0)
        {
            preGestureCounter--;
        }
    }
        if (onGesture && frameCount < MAX_FRAMECOUNT) {

            switch (extendedFingers)
            {
                case 1:
                {
                    std::string command = commands.getCommand("FING3");
                    std::cout << command << std::endl;
                    break;
                }
                case 2:
                {
                    break;
                }
                case 3:
                {
                    break;
                }
                default:
                {
                    break;
                }
            }

            frameCount++;
            const GestureList gestures = frame.gestures();
            for (int g = 0; g < gestures.count(); ++g) {
                Gesture gesture = gestures[g]; //need to move finger detection in here too
                switch (gesture.type()) {
                    case Gesture::TYPE_CIRCLE:
                    {
                        CircleGesture circle = gesture;
                        std::string clockwiseness; //probably simplfy to a bool

                        if (circle.pointable().direction().angleTo(circle.normal()) <= PI/4)
                        {
                          clockwiseness = "clockwise";
                          std::cout << "CIRCLE CLOCKWISE" << std::endl;
                        }
                        else
                        {
                          clockwiseness = "counterclockwise";
                          std::cout << "CIRCLE COUNTERCLOCKWISE" << std::endl;
                        }
                        break;
                    }
                    case Gesture::TYPE_SWIPE:
                    {
                        SwipeGesture swipe = gesture;
                        std::cout << "SWIPE" << std::endl;
                        break;
                    }
                    case Gesture::TYPE_KEY_TAP:
                    {
                        KeyTapGesture tap = gesture;
                        std::cout << "KEY TAP" << std::endl;
                        break;
                    }
                    case Gesture::TYPE_SCREEN_TAP:
                    {
                        ScreenTapGesture screentap = gesture;
                        std::cout << "SCREEN TAP" << std::endl;
                        break;
                    }
                    default:
                    {
                        std::cout << std::string(2, ' ')  << "Unknown gesture type." << std::endl;
                        break;
                    }
                }
                float seconds = gesture.durationSeconds();
                std::cout << seconds << std::endl;
            }
        }
        else if (onGesture == true && frameCount > MAX_FRAMECOUNT)
        {
            note.hide();
            onGesture = false;
            frameCount = 0;
        }
        else if (onGesture)
        {
            frameCount++;
        }
}
Example #16
0
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();
}
Example #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);
}
Example #18
0
void QTVS_Leap::HandLogic()
{
  //TODO: Fix this
  if (hands.count() == 1)
  {
    int iHandToFingerShift = hand.isLeft() ? 5 : 0;

    for (int iFingerCounter = iHandToFingerShift;
         iFingerCounter <= iHandToFingerShift + 4;
         iFingerCounter ++)
      QCoreApplication::postEvent(fingerTraces.at(iFingerCounter), new QHideEvent());

    iHandToFingerShift = hand.isLeft() ? 0 : 5;

if(ui.checkBox_ShowFingers->isChecked())
{
    for (int iFingerCounter = iHandToFingerShift;
         iFingerCounter <= iHandToFingerShift + 4;
         iFingerCounter ++)
      QCoreApplication::postEvent(fingerTraces.at(iFingerCounter), new QShowEvent());
}

  }
  else if (hands.isEmpty())
  {
    foreach (FingerTraceWindow * fTrace, fingerTraces)
      QCoreApplication::postEvent(fTrace, new QHideEvent());
    // QCoreApplication::postEvent(thumbTrace, new QHideEvent());
    // QCoreApplication::postEvent(indexTrace, new QHideEvent());
    // QCoreApplication::postEvent(middleTrace, new QHideEvent());
    // QCoreApplication::postEvent(ringTrace, new QHideEvent());
    // QCoreApplication::postEvent(pinkieTrace, new QHideEvent());
  }
  else
  {
    if(ui.checkBox_ShowFingers->isChecked())
    {
    foreach (FingerTraceWindow * fTrace, fingerTraces)
      QCoreApplication::postEvent(fTrace, new QShowEvent());      
    }
    // QCoreApplication::postEvent(thumbTrace, new QShowEvent());
    // QCoreApplication::postEvent(indexTrace, new QShowEvent());
    // QCoreApplication::postEvent(middleTrace, new QShowEvent());
    // QCoreApplication::postEvent(ringTrace, new QShowEvent());
    // QCoreApplication::postEvent(pinkieTrace, new QShowEvent());

  }

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

//TODO: Perhaps move this to gestures?
    if (ui.checkBox_Crunch->isChecked())
    {
      if (hands.count() == 2)
      {
        // we check if one hand's dragging and the other's closed
        if (hand.isLeft())
        {
          // if this hand is left, and the other hand (right) is dragging something..
          if (debugWindowDrag_Right.left != -1)
          {
            debugDisplayString = QString::number(hand.grabStrength());
            //pretty much closed
            if (hand.grabStrength() >= 0.7)
            {
              SendMessage(debugWindowHWND_Right, WM_SYSCOMMAND, SC_CLOSE, 0);
              // DestroyWindow();
            }
          }
        }
        else
        {
          // if this hand is left, and the other hand (right) is dragging something..
          if (debugWindowDrag_Left.left != -1)
          {
            //pretty much closed
            if (hand.grabStrength() >= 0.7)
            {
              // DestroyWindow(debugWindowHWND_Left);
              SendMessage(debugWindowHWND_Left, WM_SYSCOMMAND, SC_CLOSE, 0);
            }
          }
        }
      }
    }
    // 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
    // debugDisplayString = QString(", palm position: " + QString(hand.palmPosition().toString().data() ));

    // debugDisplayString = QString::number(hand.palmPosition().x); //20
    // debugDisplayString.append("\n");
    // debugDisplayString.append(QString::number(hand.palmPosition().y)); // 5
    // debugDisplayString.append("\n");
    // debugDisplayString.append(QString::number(hand.palmPosition().z));
    // debugDisplayString.append("\n");
    // debugDisplayString.append("roll:" + QString::number(normal.roll()));
///--------------------------------------------------

    if (debug_extendedFingerCounter != 0 && ui.checkBox_palmMouse->isChecked())
      HandCursorPosition(hand.stabilizedPalmPosition());


    if (ui.checkBox_HandRollDrag->isChecked())
    {
      if (bDebug_HandRollDrag)
      {
        if (normal.roll() > 0.7)
        {
          bDebug_HandRollDrag = false;

          if (debug_extendedFingerCounter != 0)
            MouseKeyboardEmulation::MouseLeftClickDown();
        }
      }
    }
    if (!bDebug_HandRollDrag)
    {
      if (normal.roll() < 0.5)
      {
        bDebug_HandRollDrag = true;
        MouseKeyboardEmulation::MouseLeftClickUp();
      }
    }

///-----------------------------------------------

    debug_extendedFingerCounter = 0;
    foreach (Finger finger, fingers)
    {
      if (finger.isExtended())
        debug_extendedFingerCounter++;
    }

    // Fist Scrolling

    if (debug_extendedFingerCounter == 0 && ui.checkBox_palmScroll->isChecked())
    {

      if (fFistPositionY == 0)
        fFistPositionY = hand.palmPosition().y;


      if (hand.palmPosition().y > fFistPositionY + 10)
      {
        float fDifference = hand.palmPosition().y - fFistPositionY + 10;
        fDifference /= 10;
        MouseKeyboardEmulation::MouseWheelUp(fDifference);
      }
      else if (hand.palmPosition().y < fFistPositionY - 10)
      {
        float fDifference = abs(hand.palmPosition().y - fFistPositionY - 10);
        fDifference /= 10;
        MouseKeyboardEmulation::MouseWheelUp(-1 * fDifference);
      }

      //              debugDisplayString = QString::number(ThumbMiddleDifference_X); //20
      //    debugDisplayString.append("\n");
      //       debugDisplayString.append(QString::number(abs(middleFinger.stabilizedTipPosition().y - indexFinger.stabilizedTipPosition().y))); // 5
      //    debugDisplayString.append("\n");
      //       debugDisplayString.append(QString::number(debug_extendedFingerCounter));

      //works
      // if(direction.pitch() * RAD_TO_DEG > 30)
      // MouseKeyboardEmulation::MouseWheelUp((direction.pitch() * RAD_TO_DEG)/10);
      // if(direction.pitch() * RAD_TO_DEG < 30)
      // MouseKeyboardEmulation::MouseWheelDown(5);

    }
    else
    {

      if (fFistPositionY != 0)
        fFistPositionY = 0;
    }
    // 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
    fingers = hand.fingers();
    FingerLogic(hand.isLeft() ? handLeft : handRight);

  }

}