Exemplo n.º 1
0
void ScrollListener::onFrame(const Controller& controller) {
    const Frame frame = controller.frame();
    if(!frame.hands().empty()){
        const Hand hand = frame.hands()[0];
        float pitch = hand.direction().pitch()*RAD_TO_DEG;
//        float roll = hand.palmNormal().roll()*RAD_TO_DEG;
//        float yaw = hand.palmNormal().yaw()*RAD_TO_DEG + 180;
        emit window->getXSlider()->valueChanged(pitch*52);
//        emit window->getYSlider()->valueChanged(roll*52);
//        emit window->getZSlider()->valueChanged(yaw*16);
    }

}
Exemplo n.º 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;
        }
    }
}
Exemplo n.º 3
0
void LeapMotion_Listener::onFrame(const Controller& controller) {

	boolean debug = false;
	//std::cin.get();

	const float min_roll_left = 15.0f, max_roll_left = std::numeric_limits<float>::max();
	const float min_roll_right = std::numeric_limits<float>::lowest(), max_roll_right = -15.0f;
	
	const float min_pitch_up = 25.0f, max_pitch_up = std::numeric_limits<float>::max();
	const float min_pitch_down = std::numeric_limits<float>::lowest(), max_pitch_down = -5.0f;

	const float min_y_down = 0.0f, max_y_down = 70.0f;
	const float min_y_up = 190.0f, max_y_up = std::numeric_limits<float>::max();

	const float min_x_left = std::numeric_limits<float>::lowest(), max_x_left = -90.0f;
	const float min_x_right = 90.0f, max_x_right = std::numeric_limits<float>::max();

	//const float min_yaw_left_finger = std::numeric_limits<float>::lowest(), max_yaw_left_finger = -45.0f;
	//const float min_yaw_right_finger = 10.0f, max_yaw_right_finger = std::numeric_limits<float>::max();

	
	// Get the most recent frame and report some basic information
	const Frame frame = controller.frame();
	
	if (!frame.hands().isEmpty()) 
	{
		const Hand hand = frame.hands()[0];
		const Leap::Vector normal = hand.palmNormal();
		const Leap::Vector direction = hand.direction();
	
		const float pitch = direction.pitch() * RAD_TO_DEG;
		const float roll = normal.roll() * RAD_TO_DEG;
		const float yaw = direction.yaw() * RAD_TO_DEG;

		const float y_palm = hand.palmPosition().y;
		const float x_palm = hand.palmPosition().x;

		float x_drone = 0.0f, y_drone = 0.0f, z_drone = 0.0f, turn_drone = 0.0f;

		if(y_palm>min_y_down && y_palm<max_y_down)
			y_drone = -1.0f;
		else if(y_palm>min_y_up && y_palm<max_y_up)
			y_drone = 1.0f;

		if(x_palm>min_x_left && x_palm<max_x_left)
			turn_drone = -1.0f;
		else if(x_palm>min_x_right && x_palm<max_x_right)
			turn_drone = 1.0f;

		if(pitch>min_pitch_up && pitch<max_pitch_up)
			z_drone = 1.0f;
		else if(pitch>min_pitch_down && pitch<max_pitch_down)
			z_drone = -1.0f;

		if(roll>min_roll_right && roll< max_roll_right)
			x_drone = 1.0f;
		else if(roll>min_roll_left && roll< max_roll_left)
			x_drone = -1.0f;

		/*if(roll>min_yaw_left_finger && roll<max_yaw_left_finger)
			std::cout<<"turn_left";
		else if(roll>min_yaw_right_finger && roll<max_yaw_right_finger)
			std::cout<<"turn_right";
			*/

		if(x_drone != 0.0f || y_drone != 0.0f || z_drone != 0.0f || turn_drone != 0.0f)
		{
			int _speed_drone = 10;
			At_Command _command; 


			if(x_drone !=0.0f &&  z_drone != 0.0f)
			{
				float margin = 0.25f;
				(x_drone < 0)? x_drone += margin : x_drone -= margin;
				(z_drone < 0)? z_drone += margin : z_drone -= margin;
			}

			if(y_drone != 0.0f)
				_command = At_Command(0, y_drone, 0, 0, Point(_speed_drone, 100), _input_type);
			
			else if(turn_drone != 0.0f)
				_command = At_Command(0, 0, 0, turn_drone, Point(_speed_drone, 100),  _input_type);

			else
				_command = At_Command(x_drone, y_drone, z_drone, turn_drone, Point(_speed_drone, 100),  _input_type);

			_multiple_Input_Devices->prepare_to_send(_command);

			if(debug)
			{
				if(x_drone < 0.0f)
					cout<<"Go_Left ";
				else if(x_drone > 0.0f)
					cout<<"Go_Right ";

				if(y_drone > 0.0f)
					cout<<"UP ";
				else if(y_drone < 0.0f)
					cout<<"DOWN ";

				if(z_drone > 0.0f)
					cout<<"FW ";
				else if(z_drone < 0.0f)
					cout<<"BW ";

				if(turn_drone > 0.0f)
					cout<<"turn_Right ";
				else if(turn_drone < 0.0f)
					cout<<"Turn_Left ";
				std::cout<<std::endl;
			}
		}
	}	
}
Exemplo n.º 4
0
void Window::paintGL()
{
  // Clear
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   //LeapMotion
   bool drawflag = true;
   bool drawflag2 = true;
   if( m_controller.isConnected()) //controller is a Controller object
   {
       Frame frame = m_controller.frame(); //The latest frame
       if (!frame.hands().isEmpty())
       {
           Leap::HandList hands = frame.hands();
           Hand firstHand = hands[0];

           Leap::Vector handCenter = firstHand.palmPosition();
           m_transform.setToIdentity();
           //m_transform.rotate(180,QVector3D(0,0,-1));
           m_transform.translate(QVector3D(-handCenter.x,handCenter.y,handCenter.z));
           float yaw = firstHand.direction().yaw();
           m_transform.rotate(-(yaw/3.14)*180,QVector3D(0,-1,0));
           float roll = firstHand.palmNormal().roll();
           m_transform.rotate((roll/3.14)*180,QVector3D(0,0,-1));
           //Frame previous = controller.frame(1); //The previous frame

           if (firstHand.grabStrength()>0.5) {
               drawflag = false;

           }


           Hand secondHand = hands[1];


           Leap::Vector handCenter2 = secondHand.palmPosition();
           m_righthandtransform.setToIdentity();
           //m_transform.rotate(180,QVector3D(0,0,-1));
           m_righthandtransform.translate(QVector3D(-handCenter2.x,handCenter2.y,handCenter2.z));
           float yaw2 = secondHand.direction().yaw();
           m_righthandtransform.rotate(-(yaw2/3.14)*180,QVector3D(0,-1,0));
           float roll2 = secondHand.palmNormal().roll();
           m_righthandtransform.rotate((roll2/3.14)*180,QVector3D(0,0,-1));
           //Frame previous = controller.frame(1); //The previous frame

           if (secondHand.grabStrength()>0.5) {
               drawflag2 = false;


           }
       }
       else
       {
           m_transform.rotate(2,QVector3D(1,1,0));
           m_transform.rotate(2,QVector3D(0,1,0));
           m_transform.setTranslation(QVector3D(40,150,0));
           //m_transform.setScale(QVector3D(5,5,5));
           m_righthandtransform.rotate(2,QVector3D(0,1,0));
           m_righthandtransform.setTranslation(QVector3D(-30,150,0));
          //m_righthandtransform.setScale(QVector3D(250,250,250));
       }

   }
   else
   {
        m_transform.setTranslation(QVector3D(0,150,0));
   }
   //create dynamic enviroment mapping
   calcuSSBB(m_transform.getMatrix(),m_modelview,m_projection);
   //opencv
   Mat opencv_image = cvQueryFrame(capture);
   Mat dest;
   cvtColor(opencv_image, dest,CV_BGR2RGB);
   QImage image((uchar*)dest.data, dest.cols, dest.rows,QImage::Format_RGB888);


  //m_backTexture->bind(0);
   //m_backTexture->setData(image,QOpenGLTexture::DontGenerateMipMaps);

   //glActiveTexture(GL_TEXTURE0);
    //generate FBO



    // bind a framebuffer object
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_backfbo);
    //glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    // Set Drawing buffers
    GLuint attachments[1] = {GL_COLOR_ATTACHMENT0};
    glDrawBuffers(1,  attachments);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glViewport(0,0,1280,720);
    //glViewport(0,0,1280*2,720*2);
    m_backTexture->bind();
    m_backTexture->load(image);
    m_backprogram->bind();
    m_backprogram->setUniformValue("ourTexture", 0);
    m_backprogram->setUniformValue(u_worldToCameraFloor, m_camera.getMatrix());
    m_backprogram->setUniformValue(u_cameraToViewFloor, m_backgroundProjection);
    if(m_back!= NULL)
    {
       m_back->draw();
    }
    m_backprogram->release();
    m_backTexture->unbind();
    if(drawflag2){
    m_noise->bind();
       //glEnable(GL_TEXTURE_3D);
    m_righthandprogram->bind();
       //m_program->setUniformValue(u_worldToCamera, m_camera.getMatrix());
    m_righthandprogram->setUniformValue(u_worldToCamera, m_modelview);
    m_righthandprogram->setUniformValue(u_cameraToView, m_projection);
    m_righthandprogram->setUniformValue(u_modelToWorld, m_righthandtransform.getMatrix());
    m_righthandprogram->setUniformValue("noise", 0);
    m_bunny->draw();
    m_righthandprogram->release();
    m_noise->unbind();}
       //m_environment->unbind();
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo);
    //glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    GLuint attachments1[1] = {GL_COLOR_ATTACHMENT0};
    glDrawBuffers(1,  attachments1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glViewport(0,0,512,512);
    //+X
    //m_backTexture->bind();
    //m_backTexture->load(image);
    glBindTexture(GL_TEXTURE_2D, backFBO);
    m_cubefaceprogram->bind();
    m_cubeface->create(
            m_ssbb[2],m_ssbb[3],
            m_ssbb[2] + ( ( 1.0f - m_ssbb[2] ) / 2.0f ) ,m_ssbb[3] + ( ( 1.0f - m_ssbb[3] ) / 2.0f ),
            m_ssbb[2] + ( ( 1.0f - m_ssbb[2] ) / 2.0f ) ,m_ssbb[1] / 2.0f,
            m_ssbb[2],m_ssbb[1]
            );

    m_cubefaceprogram->setUniformValue("screenTexture", 0);
    m_cubeface->draw();
    m_cubefaceprogram->release();
    //m_backTexture->unbind();
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, texFBO, 0);
    //-X
    //m_backTexture->bind();
    //m_backTexture->load(image);
    glBindTexture(GL_TEXTURE_2D, backFBO);
    m_cubefaceprogram->bind();
    m_cubeface->create(
            m_ssbb[0] / 2.0f,m_ssbb[3] + ( ( 1.0f - m_ssbb[3] ) / 2.0f ),
            m_ssbb[0],m_ssbb[3],
            m_ssbb[0],m_ssbb[1],
            m_ssbb[0]/2.0f,m_ssbb[1] / 2.0f
            );
    m_cubefaceprogram->setUniformValue("screenTexture", 0);
    m_cubeface->draw();
    m_cubefaceprogram->release();
    //m_backTexture->unbind();


//    //+Y
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, texFBO, 0);
    //m_backTexture->bind();
    //m_backTexture->load(image);
    glBindTexture(GL_TEXTURE_2D, backFBO);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    m_cubefaceprogram->bind();
    m_cubeface->create(
            m_ssbb[2] + ( ( 1.0f - m_ssbb[2] ) / 2.0f ),m_ssbb[3] + ( ( 1.0f - m_ssbb[3] ) / 2.0f ),
            m_ssbb[0] / 2.0f,m_ssbb[3] + ( ( 1.0f - m_ssbb[3] ) / 2.0f ),
            m_ssbb[0],m_ssbb[3],
            m_ssbb[2],m_ssbb[3]
            );
    m_cubefaceprogram->setUniformValue("screenTexture", 0);
    m_cubeface->draw();
    m_cubefaceprogram->release();
   // m_backTexture->unbind();
//
//    //-Y
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, texFBO, 0);
    //m_backTexture->bind();
    //m_backTexture->load(image);
    glBindTexture(GL_TEXTURE_2D, backFBO);
    m_cubefaceprogram->bind();
    m_cubeface->create(
                        m_ssbb[2],m_ssbb[1],
                        m_ssbb[0],m_ssbb[1],
                        m_ssbb[0]/2.0f,m_ssbb[1] / 2.0f,
                        m_ssbb[2] + ( ( 1.0f - m_ssbb[2] ) / 2.0f ) ,( m_ssbb[1] / 2.0f )
                        );
    m_cubefaceprogram->setUniformValue("screenTexture", 0);
    m_cubeface->draw();
    m_cubefaceprogram->release();
    m_backTexture->unbind();
    //+Z
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, texFBO, 0);
    //m_backTexture->bind();
    //m_backTexture->load(image);
    glBindTexture(GL_TEXTURE_2D, backFBO);
    m_cubefaceprogram->bind();
    m_cubeface->create( m_ssbb[0],m_ssbb[3],
                        m_ssbb[2],m_ssbb[3],
                        m_ssbb[2],m_ssbb[1],
                        m_ssbb[0],m_ssbb[1]);
    m_cubefaceprogram->setUniformValue("screenTexture", 0);
    m_cubeface->draw();
    m_cubefaceprogram->release();
    //m_backTexture->unbind();
//    //-Z
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, texFBO, 0);
    m_backTexture->bind();
    m_backTexture->load(image);
    glBindTexture(GL_TEXTURE_2D, backFBO);
    m_cubefaceprogram->bind();
    m_cubeface->create(
            m_ssbb[0] / 2.0f,m_ssbb[3] + ( ( 1.0f - m_ssbb[3] ) / 2.0f ),
            m_ssbb[0],m_ssbb[3],
            m_ssbb[0],m_ssbb[1],
            m_ssbb[0]/2.0f,m_ssbb[1] / 2.0f
            );
    m_cubefaceprogram->setUniformValue("screenTexture", 0);
    m_cubeface->draw();
    m_cubefaceprogram->release();
    m_backTexture->unbind();
//
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    glViewport(0,0,1280,720);
    m_backTexture->bind();
    m_backTexture->load(image);
    //glBindTexture(GL_TEXTURE_2D, backFBO);
    m_backprogram->bind();
    m_backprogram->setUniformValue("ourTexture", 0);
    m_backprogram->setUniformValue(u_worldToCameraFloor, m_camera.getMatrix());
    m_backprogram->setUniformValue(u_cameraToViewFloor, m_backgroundProjection);
    if(m_back!= NULL)
    {
       m_back->draw();
    }
    m_backprogram->release();
    m_backTexture->unbind();

   //m_environment->bind();
   glBindTexture(GL_TEXTURE_CUBE_MAP, texFBO);
   //glEnable(GL_TEXTURE_2D);
   m_program->bind();
   //m_program->setUniformValue(u_worldToCamera, m_camera.getMatrix());
   m_program->setUniformValue(u_worldToCamera, m_modelview);
   m_program->setUniformValue(u_cameraToView, m_projection);
   m_program->setUniformValue(u_modelToWorld, m_transform.getMatrix());
   m_program->setUniformValue("envTex", 0);
   if(drawflag)
        m_model->draw();
   m_program->release();
   //m_environment->unbind();

   m_noise->bind();
   m_righthandprogram->bind();
   m_righthandprogram->setUniformValue(u_worldToCamera, m_modelview);
   m_righthandprogram->setUniformValue(u_cameraToView, m_projection);
   m_righthandprogram->setUniformValue(u_modelToWorld, m_righthandtransform.getMatrix());
   m_righthandprogram->setUniformValue("noise", 0);
   if(drawflag2)
           m_bunny->draw();
   m_righthandprogram->release();
   m_noise->unbind();
      //m_environment->unbind();

}
Exemplo n.º 5
0
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;
}
Exemplo n.º 6
0
void SampleListener::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()
            << ", fingers: " << frame.fingers().count()
            << ", tools: " << frame.tools().count()
            << ", gestures: " << frame.gestures().count() << std::endl;

  if (!frame.hands().isEmpty()) {
    // Get the first hand
    const Hand hand = frame.hands()[0];

    // Check if the hand has any fingers
    const FingerList fingers = hand.fingers();
    if (!fingers.isEmpty()) {
      // Calculate the hand's average finger tip position
//      Vector avgPos;
      for (int i = 0; i < fingers.count(); ++i) {
        avgPos += fingers[i].tipPosition();
      }
      avgPos /= (float)fingers.count();
      std::cout << "Hand has " << fingers.count()
                << " fingers, average finger tip position" << avgPos << std::endl;
    }

    // Get the hand's sphere radius and palm position
    std::cout << "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
    std::cout << "Hand pitch: " << direction.pitch() * RAD_TO_DEG << " degrees, "
              << "roll: " << normal.roll() * RAD_TO_DEG << " degrees, "
              << "yaw: " << direction.yaw() * RAD_TO_DEG << " degrees" << 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/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;
        }
        std::cout << "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;
        std::cout << "Swipe id: " << gesture.id()
          << ", state: " << gesture.state()
          << ", direction: " << swipe.direction()
          << ", speed: " << swipe.speed() << std::endl;
        break;
      }
      case Gesture::TYPE_KEY_TAP:
      {
        KeyTapGesture tap = gesture;
        std::cout << "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;
        std::cout << "Screen Tap id: " << gesture.id()
        << ", state: " << gesture.state()
        << ", position: " << screentap.position()
        << ", direction: " << screentap.direction()<< std::endl;
        break;
      }
      default:
        std::cout << "Unknown gesture type." << std::endl;
        break;
    }
  }

  if (!frame.hands().isEmpty() || !gestures.isEmpty()) {
    std::cout << std::endl;
  }
}
Exemplo n.º 7
0
    void m_bang()
    {

        Frame frame;
        t_atom generalInfo[6];
        int num_hands, num_tools, num_gestures;
        frame = dispatcher.frame;
        num_hands = frame.hands().count();
        num_tools = frame.tools().count();
        num_gestures = frame.gestures().count();
        
        if(general_flag){
            SETFLOAT(&generalInfo[0], (float)frame.id());
            SETFLOAT(&generalInfo[1], (float)frame.timestamp());
            SETFLOAT(&generalInfo[2], (float)num_hands);
            SETFLOAT(&generalInfo[3], (float)frame.fingers().count());
            SETFLOAT(&generalInfo[4], (float)frame.tools().count());
            SETFLOAT(&generalInfo[5], (float)frame.gestures().count());
            ToOutList(0, 6, generalInfo);        
        }
        // tools
        for(int i = 0; i<num_tools; i++){
            Tool tool;
            tool = frame.tools()[i];

            t_atom toolInfo[5];
            if(tools_position_flag) {
                SETFLOAT(&toolInfo[0], i);
                SETSYMBOL(&toolInfo[1], gensym("direction"));
                SETFLOAT(&toolInfo[2], tool.direction().x);
                SETFLOAT(&toolInfo[3], tool.direction().y);
                SETFLOAT(&toolInfo[4], tool.direction().z);
                ToOutAnything(1, gensym("tool"), 5, toolInfo);
            }
            if(tools_position_flag) {
                SETFLOAT(&toolInfo[0], i);
                SETSYMBOL(&toolInfo[1], gensym("position"));
                SETFLOAT(&toolInfo[2], tool.tipPosition().x);
                SETFLOAT(&toolInfo[3], tool.tipPosition().y);
                SETFLOAT(&toolInfo[4], tool.tipPosition().z);
                ToOutAnything(1, gensym("tool"), 5, toolInfo);
            }
            if(tools_velocity_flag){
                SETFLOAT(&toolInfo[0], i);
                SETSYMBOL(&toolInfo[1], gensym("velocity"));
                SETFLOAT(&toolInfo[2], tool.tipVelocity().x);
                SETFLOAT(&toolInfo[3], tool.tipVelocity().y);
                SETFLOAT(&toolInfo[4], tool.tipVelocity().z);
                ToOutAnything(1, gensym("tool"), 5, toolInfo);
            }
            if(tools_size_flag){
                SETFLOAT(&toolInfo[0], i); 
                SETSYMBOL(&toolInfo[1], gensym("size"));
                SETFLOAT(&toolInfo[2], tool.width());
                SETFLOAT(&toolInfo[3], tool.length());
                ToOutAnything(1, gensym("tool"), 4, toolInfo);
            }
        }
        // hands and fingers
        for(int i = 0; i<num_hands; i++){
            Hand hand;
            hand = frame.hands()[i];
            int num_fingers = hand.fingers().count();
            int num_tools = hand.tools().count();
            t_atom handInfo[5];

            if(hands_direction_flag){
                // direction
                SETFLOAT(&handInfo[0], i);
                SETSYMBOL(&handInfo[1], gensym("direction"));
                SETFLOAT(&handInfo[2], hand.direction().x);
                SETFLOAT(&handInfo[3], hand.direction().y);
                SETFLOAT(&handInfo[4], hand.direction().z);
                ToOutAnything(1, gensym("hand"), 5, handInfo);
            }
            if(hands_palm_position_flag){
                // position
                SETFLOAT(&handInfo[0], i);
                SETSYMBOL(&handInfo[1], gensym("palm_position"));
                SETFLOAT(&handInfo[2], hand.palmPosition().x);
                SETFLOAT(&handInfo[3], hand.palmPosition().y);
                SETFLOAT(&handInfo[4], hand.palmPosition().z);
                ToOutAnything(1, gensym("hand"), 5, handInfo);
            }
            if(hands_palm_velocity_flag){
                // velocity
                SETFLOAT(&handInfo[0], i);
                SETSYMBOL(&handInfo[1], gensym("palm_velocity"));
                SETFLOAT(&handInfo[2], hand.palmVelocity().x);
                SETFLOAT(&handInfo[3], hand.palmVelocity().y);
                SETFLOAT(&handInfo[4], hand.palmVelocity().z);
                ToOutAnything(1, gensym("hand"), 5, handInfo);
            }
            if(hands_palm_normal_flag){
                // normal
                SETFLOAT(&handInfo[0], i);
                SETSYMBOL(&handInfo[1], gensym("palm_normal"));
                SETFLOAT(&handInfo[2], hand.palmNormal().x);
                SETFLOAT(&handInfo[3], hand.palmNormal().y);
                SETFLOAT(&handInfo[4], hand.palmNormal().z);
                ToOutAnything(1, gensym("hand"), 5, handInfo);
            }
            if(hands_sphere_radius_flag){
                // sphere radius
                SETFLOAT(&handInfo[0], i);
                SETSYMBOL(&handInfo[1], gensym("sphere_radius"));
                SETFLOAT(&handInfo[2], hand.sphereRadius());
                ToOutAnything(1, gensym("hand"), 3, handInfo);
            }
            if(hands_sphere_center_flag){
                // sphere center
                SETFLOAT(&handInfo[0], i);
                SETSYMBOL(&handInfo[1], gensym("sphere_center"));
                SETFLOAT(&handInfo[2], hand.sphereCenter().x);
                SETFLOAT(&handInfo[3], hand.sphereCenter().y);
                SETFLOAT(&handInfo[4], hand.sphereCenter().z);
                ToOutAnything(1, gensym("hand"), 5, handInfo);
            }
            if(hands_finger_count_flag){
                // finger count
                SETFLOAT(&handInfo[0], i);
                SETSYMBOL(&handInfo[1], gensym("finger_count"));
                SETFLOAT(&handInfo[2], num_fingers);
                ToOutAnything(1, gensym("hand"), 3, handInfo);
            }
            if(hands_tool_count_flag){
                // tool count
                SETFLOAT(&handInfo[0], i);
                SETSYMBOL(&handInfo[1], gensym("tool_count"));
                SETFLOAT(&handInfo[2], num_tools);
                ToOutAnything(1, gensym("hand"), 3, handInfo);
            }
            for(int j = 0; j<num_fingers; j++){
                Finger finger;
                finger = hand.fingers()[j];                    
                t_atom fingerInfo[7];
                if(fingers_direction_flag){
                    SETFLOAT(&fingerInfo[0], i); // index
                    SETSYMBOL(&fingerInfo[1], gensym("fingers"));
                    SETFLOAT(&fingerInfo[2], j);
                    SETSYMBOL(&fingerInfo[3], gensym("direction"));
                    SETFLOAT(&fingerInfo[4], finger.direction().x);
                    SETFLOAT(&fingerInfo[5], finger.direction().y);
                    SETFLOAT(&fingerInfo[6], finger.direction().z);
                    ToOutAnything(1, gensym("hand"), 7, fingerInfo);
                }
                if(fingers_position_flag){
                    SETFLOAT(&fingerInfo[0], i); // index
                    SETSYMBOL(&fingerInfo[1], gensym("fingers"));
                    SETFLOAT(&fingerInfo[2], j);
                    SETSYMBOL(&fingerInfo[3], gensym("position"));
                    SETFLOAT(&fingerInfo[4], finger.tipPosition().x);
                    SETFLOAT(&fingerInfo[5], finger.tipPosition().y);
                    SETFLOAT(&fingerInfo[6], finger.tipPosition().z);
                    ToOutAnything(1, gensym("hand"), 7, fingerInfo);
                }
                if(fingers_velocity_flag){
                    SETFLOAT(&fingerInfo[0], i); // index
                    SETSYMBOL(&fingerInfo[1], gensym("fingers"));
                    SETFLOAT(&fingerInfo[2], j);
                    SETSYMBOL(&fingerInfo[3], gensym("velocity"));
                    SETFLOAT(&fingerInfo[4], finger.tipVelocity().x);
                    SETFLOAT(&fingerInfo[5], finger.tipVelocity().y);
                    SETFLOAT(&fingerInfo[6], finger.tipVelocity().z);
                    ToOutAnything(1, gensym("hand"), 7, fingerInfo);
                }
                if(fingers_size_flag){
                    SETFLOAT(&fingerInfo[0], i); // index
                    SETSYMBOL(&fingerInfo[1], gensym("fingers"));
                    SETFLOAT(&fingerInfo[2], j);
                    SETSYMBOL(&fingerInfo[3], gensym("size"));
                    SETFLOAT(&fingerInfo[4], finger.width());
                    SETFLOAT(&fingerInfo[5], finger.length());
                    ToOutAnything(1, gensym("hand"), 6, fingerInfo);
                }
            }
        }
        t_atom gestureCountInfo[1];            
        for(int i = 0;i < num_gestures; i++){
            Gesture gesture;
            gesture = frame.gestures()[i];
            //type
            t_atom gestureTypeInfo[3];
            SETFLOAT(&gestureTypeInfo[0], i);
            SETSYMBOL(&gestureTypeInfo[1], gensym("type"));
            switch(gesture.type())
            {
                case Gesture::TYPE_INVALID:
                    SETSYMBOL(&gestureTypeInfo[2], gensym("TYPE_INVALID"));
                    break;
                case Gesture::TYPE_SWIPE:
                    SETSYMBOL(&gestureTypeInfo[2], gensym("TYPE_SWIPE"));
                    break;
                case Gesture::TYPE_CIRCLE:
                    SETSYMBOL(&gestureTypeInfo[2], gensym("TYPE_CIRCLE"));
                    break;
                case Gesture::TYPE_SCREEN_TAP:
                    SETSYMBOL(&gestureTypeInfo[2], gensym("TYPE_SCREEN_TAP"));
                    break;
                case Gesture::TYPE_KEY_TAP:
                    SETSYMBOL(&gestureTypeInfo[2], gensym("TYPE_KEY_TAP"));
                    break;
            }
            ToOutList(2, 3, gestureTypeInfo);

            //state
            t_atom gestureStateInfo[3];
            SETFLOAT(&gestureStateInfo[0], i);
            SETSYMBOL(&gestureStateInfo[1], gensym("state"));
            switch(gesture.state())
            {
                case Gesture::STATE_INVALID:
                    SETSYMBOL(&gestureStateInfo[2], gensym("STATE_INVALID"));
                    break;
                case Gesture::STATE_START:
                    SETSYMBOL(&gestureStateInfo[2], gensym("TYPE_START"));
                    break;
                case Gesture::STATE_UPDATE:
                    SETSYMBOL(&gestureStateInfo[2], gensym("STATE_UPDATE"));
                    break;
                case Gesture::STATE_STOP:
                    SETSYMBOL(&gestureStateInfo[2], gensym("TYPE_STOP"));
                    break;
            }
            ToOutList(2, 3, gestureStateInfo);

            t_atom gestureDurationInfo[3];
            SETFLOAT(&gestureDurationInfo[0], i);
            SETSYMBOL(&gestureDurationInfo[1], gensym("duration"));
            SETFLOAT(&gestureDurationInfo[2], gesture.duration());
            ToOutList(2, 3, gestureDurationInfo);

            t_atom gestureIdInfo[3];
            SETFLOAT(&gestureIdInfo[0], i);
            SETSYMBOL(&gestureIdInfo[1], gensym("id"));
            SETFLOAT(&gestureIdInfo[2], gesture.id());
            ToOutList(2, 3, gestureIdInfo);

        }
    }
Exemplo n.º 8
0
 void update(double p, double q, LeapHandMessage &message, const Hand &hand)
 {
   average(p,q,message.at,(1/25.4),hand.palmPosition());
   average(p,q,message.point,1,hand.direction());
   average(p,q,message.down,1,hand.palmNormal());
 }
Exemplo n.º 9
0
    FREObject LNLeapDevice::getFrame() {
        
        Frame frame = controller->frame();
        
        // TODO: Only continue with valid Frame?
        
        FREObject freCurrentFrame;
        FRENewObject( (const uint8_t*) "com.leapmotion.leap.Frame", 0, NULL, &freCurrentFrame, NULL);
        
        FREObject freFrameId;
        FRENewObjectFromInt32((int32_t) frame.id(), &freFrameId);
        FRESetObjectProperty(freCurrentFrame, (const uint8_t*) "id", freFrameId, NULL);
        
        const Vector frameTranslation = frame.translation(lastFrame);
        FRESetObjectProperty(freCurrentFrame, (const uint8_t*) "translationVector", createVector3(frameTranslation.x, frameTranslation.y, frameTranslation.z), NULL);
        
        const Matrix frameRotation = frame.rotationMatrix(lastFrame);
        FRESetObjectProperty(freCurrentFrame, (const uint8_t*) "rotation", createMatrix(
                     createVector3(frameRotation.xBasis[0], frameRotation.xBasis[1], frameRotation.xBasis[2]),
                     createVector3(frameRotation.yBasis[0], frameRotation.yBasis[1], frameRotation.yBasis[2]),
                     createVector3(frameRotation.zBasis[0], frameRotation.zBasis[1], frameRotation.zBasis[2]),
                     createVector3(frameRotation.origin[0], frameRotation.origin[1], frameRotation.origin[2])
        ), NULL);
        
        FREObject freFrameScaleFactor;
        FRENewObjectFromDouble(frame.scaleFactor(lastFrame), &freFrameScaleFactor);
        FRESetObjectProperty(freCurrentFrame, (const uint8_t*) "scaleFactorNumber", freFrameScaleFactor, NULL);
        
        FREObject freTimestamp;
        FRENewObjectFromInt32((int32_t) frame.timestamp(), &freTimestamp);
        FRESetObjectProperty(freCurrentFrame, (const uint8_t*) "timestamp", freTimestamp, NULL);
        
        std::map<int, FREObject> freHandsMap;
        if (!frame.hands().empty()) {
            
            FREObject freHands;
            FREGetObjectProperty(freCurrentFrame, (const uint8_t*) "hands", &freHands, NULL);
            
            for(int i = 0; i < frame.hands().count(); i++) {
                const Hand hand = frame.hands()[i];
                
                FREObject freHand;
                FRENewObject( (const uint8_t*) "com.leapmotion.leap.Hand", 0, NULL, &freHand, NULL);
                FRESetObjectProperty(freHand, (const uint8_t*) "frame", freCurrentFrame, NULL);
                FRESetObjectProperty(freHand, (const uint8_t*) "direction", createVector3(hand.direction()[0], hand.direction()[1], hand.direction()[2]), NULL);
                
                FREObject freHandId;
                FRENewObjectFromInt32(hand.id(), &freHandId);
                FRESetObjectProperty(freHand, (const uint8_t*) "id", freHandId, NULL);
                FRESetObjectProperty(freHand, (const uint8_t*) "palmNormal", createVector3(hand.palmNormal()[0], hand.palmNormal()[1], hand.palmNormal()[2]), NULL);
                FRESetObjectProperty(freHand, (const uint8_t*) "palmPosition", createVector3(hand.palmPosition()[0], hand.palmPosition()[1], hand.palmPosition()[2]), NULL);
                FRESetObjectProperty(freHand, (const uint8_t*) "palmVelocity", createVector3(hand.palmVelocity()[0], hand.palmVelocity()[1], hand.palmVelocity()[2]), NULL);
                
                const Matrix rotation = hand.rotationMatrix(lastFrame);
                FRESetObjectProperty(freHand, (const uint8_t*) "rotation", createMatrix(
                                     createVector3(rotation.xBasis[0], rotation.xBasis[1], rotation.xBasis[2]),
                                     createVector3(rotation.yBasis[0], rotation.yBasis[1], rotation.yBasis[2]),
                                     createVector3(rotation.zBasis[0], rotation.zBasis[1], rotation.zBasis[2]),
                                     createVector3(rotation.origin[0], rotation.origin[1], rotation.origin[2])
                ), NULL);
                
                FREObject freScaleFactor;
                FRENewObjectFromDouble(hand.scaleFactor(lastFrame), &freScaleFactor);
                FRESetObjectProperty(freHand, (const uint8_t*) "scaleFactorNumber", freScaleFactor, NULL);
                
                FRESetObjectProperty(freHand, (const uint8_t*) "sphereCenter", createVector3(hand.sphereCenter()[0], hand.sphereCenter()[1], hand.sphereCenter()[2]), NULL);
                
                FREObject freSphereRadius;
                FRENewObjectFromDouble(hand.sphereRadius(), &freSphereRadius);
                FRESetObjectProperty(freHand, (const uint8_t*) "sphereRadius", freSphereRadius, NULL);
                
                const Vector translation = hand.translation(lastFrame);
                FRESetObjectProperty(freHand, (const uint8_t*) "translationVector", createVector3(translation.x, translation.y, translation.z), NULL);
                
                FRESetArrayElementAt(freHands, i, freHand);
                
                freHandsMap[hand.id()] = freHand;
            }
        }
        
        std::map<int, FREObject> frePointablesMap;
        if(!frame.pointables().empty()) {
            
            FREObject frePointables;
            FREGetObjectProperty(freCurrentFrame, (const uint8_t*) "pointables", &frePointables, NULL);
            
            for(int i = 0; i < frame.pointables().count(); i++) {
                const Pointable pointable = frame.pointables()[i];
                
                FREObject frePointable;
                if(pointable.isTool()) {
                    FRENewObject( (const uint8_t*) "com.leapmotion.leap.Tool", 0, NULL, &frePointable, NULL);
                } else {
                    FRENewObject( (const uint8_t*) "com.leapmotion.leap.Finger", 0, NULL, &frePointable, NULL);
                }
                
                FRESetObjectProperty(frePointable, (const uint8_t*) "frame", freCurrentFrame, NULL);
                
                FREObject frePointableId;
                FRENewObjectFromInt32(pointable.id(), &frePointableId);
                FRESetObjectProperty(frePointable, (const uint8_t*) "id", frePointableId, NULL);
                
                FREObject frePointableLength;
                FRENewObjectFromDouble(pointable.length(), &frePointableLength);
                FRESetObjectProperty(frePointable, (const uint8_t*) "length", frePointableLength, NULL);

                FREObject frePointableWidth;
                FRENewObjectFromDouble(pointable.width(), &frePointableWidth);
                FRESetObjectProperty(frePointable, (const uint8_t*) "width", frePointableWidth, NULL);

                FRESetObjectProperty(frePointable, (const uint8_t*) "direction", createVector3(pointable.direction().x, pointable.direction().y, pointable.direction().z), NULL);
                FRESetObjectProperty(frePointable, (const uint8_t*) "tipPosition", createVector3(pointable.tipPosition().x, pointable.tipPosition().y, pointable.tipPosition().z), NULL);
                FRESetObjectProperty(frePointable, (const uint8_t*) "tipVelocity", createVector3(pointable.tipVelocity().x, pointable.tipVelocity().y, pointable.tipVelocity().z), NULL);
                
                //map to hand & back
                if(pointable.hand().isValid()) {
                    FREObject freHand = freHandsMap[pointable.hand().id()];
                    FRESetObjectProperty(frePointable, (const uint8_t*) "hand", freHand, NULL);
                    
                    FREObject frePointables;
                    FREGetObjectProperty(freHand, (const uint8_t*) "pointables", &frePointables, NULL);
                    
                    uint32_t numPointables;
                    FREGetArrayLength(frePointables, &numPointables);
                    FRESetArrayElementAt(frePointables, numPointables, frePointable);
                    
                    FREObject freSpecificHandPointables;
                    if(pointable.isTool()) {
                        FREGetObjectProperty(freHand, (const uint8_t*) "tools", &freSpecificHandPointables, NULL);
                    } else {
                        FREGetObjectProperty(freHand, (const uint8_t*) "fingers", &freSpecificHandPointables, NULL);
                    }
                    uint32_t numSpecificHandTools;
                    FREGetArrayLength(freSpecificHandPointables, &numSpecificHandTools);
                    FRESetArrayElementAt(freSpecificHandPointables, numSpecificHandTools, frePointable);
                }
                
                //push it in current frame
                FRESetArrayElementAt(frePointables, i, frePointable);
                
                //specific
                FREObject freSpecificPointables;
                if(pointable.isTool()) {
                    FREGetObjectProperty(freCurrentFrame, (const uint8_t*) "tools", &freSpecificPointables, NULL);
                } else {
                    FREGetObjectProperty(freCurrentFrame, (const uint8_t*) "fingers", &freSpecificPointables, NULL);
                }
                uint32_t numSpecificTools;
                FREGetArrayLength(freSpecificPointables, &numSpecificTools);
                FRESetArrayElementAt(freSpecificPointables, numSpecificTools, frePointable);
                frePointablesMap[pointable.id()] = frePointable;
            }
        }
        
        if(!frame.gestures().empty()) {
            
            FREObject freGestures;
            FREGetObjectProperty(freCurrentFrame, (const uint8_t*) "gesturesVector", &freGestures, NULL);
            
            for(int i = 0; i < frame.gestures().count(); i++) {
                const Gesture gesture = frame.gestures()[i];
                
                int state;
                switch (gesture.state()) {
                    case Gesture::STATE_INVALID:
                        state = 0;
                        break;
                    case Gesture::STATE_START:
                        state = 1;
                        break;
                    case Gesture::STATE_UPDATE:
                        state = 2;
                        break;
                    case Gesture::STATE_STOP:
                        state = 3;
                        break;
                        
                    default:
                        break;
                }
                
                int type;
                FREObject freGesture;
                switch (gesture.type()) {
                    case Gesture::TYPE_SWIPE:
                    {
                        type = 5;
                        SwipeGesture swipe = gesture;
                        
                        FRENewObject( (const uint8_t*) "com.leapmotion.leap.SwipeGesture", 0, NULL, &freGesture, NULL);
                        
                        FRESetObjectProperty(freGesture, (const uint8_t*) "direction", createVector3(swipe.direction().x, swipe.direction().y, swipe.direction().z), NULL);
                        FRESetObjectProperty(freGesture, (const uint8_t*) "position", createVector3(swipe.position().x, swipe.position().y, swipe.position().z), NULL);
                        FRESetObjectProperty(freGesture, (const uint8_t*) "startPosition", createVector3(swipe.startPosition().x, swipe.startPosition().y, swipe.startPosition().z), NULL);
                        
                        FREObject freSwipeGestureSpeed;
                        FRENewObjectFromDouble(swipe.speed(), &freSwipeGestureSpeed);
                        FRESetObjectProperty(freGesture, (const uint8_t*) "speed", freSwipeGestureSpeed, NULL);

                        break;
                    }
                    case Gesture::TYPE_CIRCLE:
                    {
                        type = 6;
                        CircleGesture circle = gesture;

                        FRENewObject( (const uint8_t*) "com.leapmotion.leap.CircleGesture", 0, NULL, &freGesture, NULL);
                        
                        FRESetObjectProperty(freGesture, (const uint8_t*) "center", createVector3(circle.center().x, circle.center().y, circle.center().z), NULL);
                        FRESetObjectProperty(freGesture, (const uint8_t*) "normal", createVector3(circle.normal().x, circle.normal().y, circle.normal().z), NULL);
                        
                        FREObject freCircleGestureProgress;
                        FRENewObjectFromDouble(circle.progress(), &freCircleGestureProgress);
                        FRESetObjectProperty(freGesture, (const uint8_t*) "progress", freCircleGestureProgress, NULL);
                        
                        FREObject freCircleGestureRadius;
                        FRENewObjectFromDouble(circle.radius(), &freCircleGestureRadius);
                        FRESetObjectProperty(freGesture, (const uint8_t*) "radius", freCircleGestureRadius, NULL);
                    
                        FREObject frePointable = frePointablesMap[circle.pointable().id()];
                        if(frePointable != NULL)
                        {
                            FRESetObjectProperty(freGesture, (const uint8_t*) "pointable", frePointable, NULL);
                        }

                        break;
                    }
                    case Gesture::TYPE_SCREEN_TAP:
                    {
                        type = 7;
                        ScreenTapGesture screentap = gesture;
                        
                        FRENewObject( (const uint8_t*) "com.leapmotion.leap.ScreenTapGesture", 0, NULL, &freGesture, NULL);

                        FRESetObjectProperty(freGesture, (const uint8_t*) "direction", createVector3(screentap.direction().x, screentap.direction().y, screentap.direction().z), NULL);
                        FRESetObjectProperty(freGesture, (const uint8_t*) "position", createVector3(screentap.position().x, screentap.position().y, screentap.position().z), NULL);

                        break;
                    }
                    case Gesture::TYPE_KEY_TAP:
                    {
                        type = 8;
                        KeyTapGesture tap = gesture;
                        
                        FRENewObject( (const uint8_t*) "com.leapmotion.leap.KeyTapGesture", 0, NULL, &freGesture, NULL);

                        FRESetObjectProperty(freGesture, (const uint8_t*) "direction", createVector3(tap.direction().x, tap.direction().y, tap.direction().z), NULL);
                        FRESetObjectProperty(freGesture, (const uint8_t*) "position", createVector3(tap.position().x, tap.position().y, tap.position().z), NULL);
                        
                        break;
                    }
                    default:
                    {
                        type = 4;
                        FRENewObject( (const uint8_t*) "com.leapmotion.leap.Gesture", 0, NULL, &freGesture, NULL);
                        break;
                    }
                }
                
                FREObject freGestureState;
                FRENewObjectFromInt32(state, &freGestureState);
                FRESetObjectProperty(freGesture, (const uint8_t*) "state", freGestureState, NULL);
                
                FREObject freGestureType;
                FRENewObjectFromInt32(type, &freGestureType);
                FRESetObjectProperty(freGesture, (const uint8_t*) "type", freGestureType, NULL);

                FREObject freGestureDuration;
                FRENewObjectFromInt32((int32_t) gesture.duration(), &freGestureDuration);
                FRESetObjectProperty(freGesture, (const uint8_t*) "duration", freGestureDuration, NULL);
                
                FREObject freGestureDurationSeconds;
                FRENewObjectFromDouble(gesture.durationSeconds(), &freGestureDurationSeconds);
                FRESetObjectProperty(freGesture, (const uint8_t*) "durationSeconds", freGestureDurationSeconds, NULL);
                
                FRESetObjectProperty(freGesture, (const uint8_t*) "frame", freCurrentFrame, NULL);
                
                FREObject freGestureId;
                FRENewObjectFromInt32(gesture.id(), &freGestureId);
                FRESetObjectProperty(freGesture, (const uint8_t*) "id", freGestureId, NULL);
                
                if (!gesture.hands().empty()) {
                    
                    FREObject freGestureHands;
                    FREGetObjectProperty(freGesture, (const uint8_t*) "hands", &freGestureHands, NULL);
                    
                    for(int i = 0; i < gesture.hands().count(); i++) {
                        const Hand hand = gesture.hands()[i];
                        
                        FREObject freHand = freHandsMap[hand.id()];
                        FRESetArrayElementAt(freGestureHands, i, freHand);
                    }
                }
                
                if (!gesture.pointables().empty()) {
                    
                    FREObject freGesturePointables;
                    FREGetObjectProperty(freGesture, (const uint8_t*) "pointables", &freGesturePointables, NULL);
                    
                    for(int i = 0; i < gesture.pointables().count(); i++) {
                        const Pointable pointable = gesture.pointables()[i];
                        
                        FREObject frePointable = frePointablesMap[pointable.id()];
                        FRESetArrayElementAt(freGesturePointables, i, frePointable);
                    }
                }
                
                //push it in current gesture vector
                FRESetArrayElementAt(freGestures, i, freGesture);
            }
        }
        
        lastFrame = frame;
        
        return freCurrentFrame;
    }
Exemplo n.º 10
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);

  }

}