void newFrameHandler(Leap.Frame frame)
        {
            HandList hands = frame.Hands;


            this.handCountValue.Content = hands.Count;
            //get the front most hand
            if (hands.Count > 0 && hands[0] != null)
            {
                Hand firstHand = hands[0];

                Leap.Vector direction  = firstHand.Direction;
                Leap.Vector PalmNormal = firstHand.PalmNormal;
                Leap.Vector center     = firstHand.PalmPosition;
                Leap.Vector moveRate   = firstHand.PalmVelocity;


                this.directionValue.Content    = direction;
                this.palmPositionValue.Content = center;
                this.normalValue.Content       = PalmNormal;

                FingerList fingers = firstHand.Fingers;
                this.fingerCountValue.Content = fingers.Count();
            }

            if (hands.Count > 1 && hands[1] != null)
            {
                Hand secondHand = hands[1];

                Leap.Vector direction  = secondHand.Direction;
                Leap.Vector PalmNormal = secondHand.PalmNormal;
                Leap.Vector center     = secondHand.PalmPosition;
                Leap.Vector moveRate   = secondHand.PalmVelocity;


                this.secondDirectionValue.Content    = direction;
                this.secondPalmPositionValue.Content = center;
                this.secondNormalValue.Content       = PalmNormal;

                //pass finger number in front hand to label( it is always five, with the new Leap Motion Hand model)
                FingerList fingers = secondHand.Fingers;
                this.secondFingerCountValue.Content = fingers.Count();
            }


            this.idValue.Content        = frame.Id.ToString();
            this.frameRateValue.Content = frame.CurrentFramesPerSecond.ToString();
            this.isValidValue.Content   = frame.IsValid.ToString();
        }
        List <Matrix <float> > transform_points(FingerList leap_points, Leap.Hand hand)
        {
            int pointsnum = leap_points.Count();
            List <Matrix <float> > new_leap = new List <Matrix <float> >();

            for (int i = 0; i < pointsnum; i++)
            {
                Matrix <float> leap_point = new Matrix <float>(3, 1);
                //leap_point.Data[0, 0] = leap_points[i].StabilizedTipPosition.x/1000;
                //leap_point.Data[1, 0] = leap_points[i].StabilizedTipPosition.y/1000;
                //leap_point.Data[2, 0] = leap_points[i].StabilizedTipPosition.z/1000;
                leap_point.Data[0, 0] = leap_points[i].TipPosition.x / 1000;
                leap_point.Data[1, 0] = leap_points[i].TipPosition.y / 1000;
                leap_point.Data[2, 0] = leap_points[i].TipPosition.z / 1000;
                Matrix <float> trans_points = new Matrix <float>(3, 1);
                trans_points = R * leap_point + T;
                new_leap.Add(trans_points);//5 points
            }

            Matrix <float> leap_palm = new Matrix <float>(3, 1);

            leap_palm.Data[0, 0] = hand.PalmPosition.x / 1000;
            leap_palm.Data[1, 0] = hand.PalmPosition.z / 1000;
            leap_palm.Data[2, 0] = hand.PalmPosition.y / 1000;

            Matrix <float> trans_palm = new Matrix <float>(3, 1);

            trans_palm = R * leap_palm + T;
            new_leap.Add(trans_palm);// 1 points

            for (int i = 0; i < pointsnum; i++)
            {
                Bone bone;
                foreach (Bone.BoneType boneType in (Bone.BoneType[])Enum.GetValues(typeof(Bone.BoneType)))
                {
                    Matrix <float> leap_point = new Matrix <float>(3, 1);
                    bone = hand.Fingers[i].Bone(boneType);
                    leap_point.Data[0, 0] = bone.PrevJoint.x / 1000;
                    leap_point.Data[1, 0] = bone.PrevJoint.y / 1000;
                    leap_point.Data[2, 0] = bone.PrevJoint.z / 1000;
                    Matrix <float> trans_points = new Matrix <float>(3, 1);
                    trans_points = R * leap_point + T;
                    new_leap.Add(trans_points);// 4 * 5 = 20 points
                }
            }
            return(new_leap);
        }
        public void CountNumber(Leap.Frame frame)
        {
            //create handlist
            HandList hands = frame.Hands;

            //get the front most hand
            Hand frontmost = hands.Frontmost;

            //pass the front most hand type to label
            if (frontmost.IsLeft)
            {
                frontmostType.Content = "Left";
            }
            else
            {
                frontmostType.Content = "Right";
            }

            //pass the hand's basic information to its labels
            foreach (Hand hand in hands)
            {
                int         handCount  = hands.Count();
                Leap.Vector direction  = hand.Direction;
                Leap.Vector PalmNormal = hand.PalmNormal;
                Leap.Vector center     = hand.PalmPosition;
                Leap.Vector moveRate   = hand.PalmVelocity;

                HandCount.Content    = handCount;
                Direction.Content    = direction;
                PalmPosition.Content = center;
                normalVector.Content = PalmNormal;

                //pass finger number in front hand to label( it is always five, with the new Leap Motion Hand model)
                FingerList fingers = hand.Fingers;
                FingerCount.Content = fingers.Count();
            }
        }
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            if (controller.IsConnected)
            {
                //clear fingers
                fingerPoints.Clear();
                var frame = controller.Frame();

                debugLine = "";

                SafeWriteLine("Frame id: " + frame.Id
                        + ", timestamp: " + frame.Timestamp
                        + ", hands: " + frame.Hands.Count
                        + ", fingers: " + frame.Fingers.Count
                        + ", tools: " + frame.Tools.Count
                        + ", gestures: " + frame.Gestures().Count);

                if (!(frame.Hands.Count() == 0 ))
                {
                    // Get the first hand
                    hand = frame.Hands[0];

                    firstHandLoc = new Vector2(NormalizeWidth(hand.SphereCenter.x), NormalizeHeight( hand.SphereCenter.y));
                    // Check if the hand has any fingers
                    fingers = hand.Fingers;
                    if (!(fingers.Count() == 0))
                    {
                        // Calculate the hand's average finger tip position
                        Vector avgPos = Vector.Zero;
                        foreach (Finger finger in fingers)
                        {
                            fingerPoints.Add(new Vector2(
                                NormalizeWidth(finger.TipPosition.x),
                                NormalizeHeight(finger.TipPosition.y)
                                )
                            );
                            avgPos += finger.TipPosition;
                        }
                        avgPos /= fingers.Count;

                        SafeWriteLine("Hand has " + fingers.Count
                                    + " fingers, average finger tip position: " + avgPos);

                    }

                    // Get the hand's sphere radius and palm position

                    SafeWriteLine("Hand sphere radius: " + hand.SphereRadius.ToString("n2")
                                + " mm, palm position: " + hand.PalmPosition);

                    // Get the hand's normal vector and direction
                    Vector normal = hand.PalmNormal;
                    Vector direction = hand.Direction;

                    // Calculate the hand's pitch, roll, and yaw angles

                    SafeWriteLine("Hand pitch: " + direction.Pitch * 180.0f / (float)Math.PI + " degrees, "
                                + "roll: " + normal.Roll * 180.0f / (float)Math.PI + " degrees, "
                                + "yaw: " + direction.Yaw * 180.0f / (float)Math.PI + " degrees");

                }

                // Get gestures
                gestures = frame.Gestures();
                Gesture gesture;
                for (int i = 0; i < gestures.Count; i++)
                {
                    gesture = gestures[i];

                    switch (gesture.Type)
                    {
                        case Gesture.GestureType.TYPECIRCLE:
                            CircleGesture circle = new CircleGesture(gesture);

                            // Calculate clock direction using the angle between circle normal and pointable
                            String clockwiseness;
                            if (circle.Pointable.Direction.AngleTo(circle.Normal) <= Math.PI / 4)
                            {
                                //Clockwise if angle is less than 90 degrees
                                clockwiseness = "clockwise";
                            }
                            else
                            {
                                clockwiseness = "counterclockwise";
                            }

                            float sweptAngle = 0;

                            // Calculate angle swept since last frame
                            if (circle.State != Gesture.GestureState.STATESTART)
                            {
                                CircleGesture previousUpdate = new CircleGesture(controller.Frame(1).Gesture(circle.Id));
                                sweptAngle = (circle.Progress - previousUpdate.Progress) * 360;
                            }

                            SafeWriteLine("Circle id: " + circle.Id
                                           + ", " + circle.State
                                           + ", progress: " + circle.Progress
                                           + ", radius: " + circle.Radius
                                           + ", angle: " + sweptAngle
                                           + ", " + clockwiseness);

                            break;
                        case Gesture.GestureType.TYPESWIPE:
                            SwipeGesture swipe = new SwipeGesture(gesture);

                            SafeWriteLine("Swipe id: " + swipe.Id
                                           + ", " + swipe.State
                                           + ", position: " + swipe.Position
                                           + ", direction: " + swipe.Direction
                                           + ", speed: " + swipe.Speed);

                            break;
                        case Gesture.GestureType.TYPEKEYTAP:
                            KeyTapGesture keytap = new KeyTapGesture(gesture);

                            SafeWriteLine("Tap id: " + keytap.Id
                                           + ", " + keytap.State
                                           + ", position: " + keytap.Position
                                           + ", direction: " + keytap.Direction);

                            break;
                        case Gesture.GestureType.TYPESCREENTAP:
                            ScreenTapGesture screentap = new ScreenTapGesture(gesture);

                            SafeWriteLine("Tap id: " + screentap.Id
                                           + ", " + screentap.State
                                           + ", position: " + screentap.Position
                                           + ", direction: " + screentap.Direction);

                            break;
                        default:
                            SafeWriteLine("Unknown gesture type.");
                            break;
                    }
                }

                if (!(frame.Hands.Count() == 0) || !(frame.Gestures().Count ==0))
                {
                    //SafeWriteLine("");
                }
            }
            base.Update(gameTime);
        }
Esempio n. 5
0
        public override void OnFrame(Controller controller)
        {
            //get screens
            screens = controller.CalibratedScreens;

            //calculate fps
            double fps = 1.0 * Stopwatch.Frequency / (stopWatch.ElapsedTicks - lasttime);

            lasttime = stopWatch.ElapsedTicks;

            timeaccum += 1.0 / fps;
            framesaccum++;
            if (timeaccum >= 0.5)
            {
                UpdateText(form.fps_label, "fps: " + Convert.ToString((int)(1.0 * framesaccum / timeaccum)));
                timeaccum  -= 0.5;
                framesaccum = 0;
            }

            bool  wasd = false;
            float scale, yoffset, ws, ad;
            bool  intersect;

            lock (thisLock) //get access to input data
            {
                scale     = (float)form.sens;
                yoffset   = (float)form.yoffset;
                ws        = (float)form.wsval;
                ad        = (float)form.adval;
                intersect = form.intersect;
                wasd      = form.wasd_check.Checked;
            }

            //move phase for keyboard simulation
            phase += par / fps * freq;
            if (phase > 1)
            {
                par   = -1;
                phase = 1;
            }
            if (phase < 0)
            {
                par   = 1;
                phase = 0;
            }

            Pointable point1    = null;
            bool      point1_ok = false;

            // Get the most recent frame
            Frame frame = controller.Frame();

            if (!frame.Tools.Empty)
            {
                //get the nearest tool
                int      nearest    = 0;
                double   nearestval = double.MaxValue;
                ToolList tools      = frame.Tools;
                for (int i = 0; i < tools.Count(); i++)
                {
                    if (tools[i].TipPosition.z < nearestval)
                    {
                        nearest    = i;
                        nearestval = tools[i].TipPosition.z;
                    }
                }
                point1    = tools[nearest];
                point1_ok = true;
            }
            else if (!frame.Hands.Empty)
            {
                // Get the first hand
                Hand hand = frame.Hands[0];

                // Check if the hand has any fingers
                FingerList fingers = hand.Fingers;
                if (!fingers.Empty)
                {
                    //get the finger closest to the screen (smallest z)
                    int    nearest    = 0;
                    double nearestval = double.MaxValue;
                    for (int i = 0; i < fingers.Count(); i++)
                    {
                        if (fingers[i].TipPosition.z < nearestval)
                        {
                            nearest    = i;
                            nearestval = fingers[i].TipPosition.z;
                        }
                    }
                    point1    = fingers[nearest];
                    point1_ok = true;
                }
            }

            if (point1_ok) //there is finger or tool
            {
                PointConverter pc = new PointConverter();
                Point          pt = new Point();

                //wasd not checked
                if (!wasd)
                {
                    //interset/project on screen
                    Vector intersection;
                    if (intersect)
                    {
                        intersection = screens[0].Intersect(point1, true, 4.0f / scale);
                    }
                    else
                    {
                        intersection = screens[0].Project(point1.TipPosition, true, 4.0f / scale);
                    }

                    //scale and offset screen position
                    double scx = (intersection.x - 0.5) * scale + 0.5;
                    double scy = (1 - intersection.y - 0.5) * scale + 0.5 + yoffset;
                    pt.X = (int)(scx * screens[0].WidthPixels);
                    pt.Y = (int)(scy * screens[0].HeightPixels);

                    Cursor.Position = pt;
                }
                //if wasd is checked
                else
                {
                    string str = "";

                    float x = point1.TipPosition.x;
                    float y = point1.TipPosition.y;
                    float z = point1.TipPosition.z;

                    var  hWnd = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
                    Hand hand = frame.Hands[0];

                    double xph = -hand.PalmNormal.Roll * ad * 8;                                    //steering using roll
                    double zph = (Math.Abs(hand.PalmPosition.z - 100) - ws * 200.0) / (200.0 * ws); //acceleration using z

                    //stroke or release given keys

                    if (xph > 0 && Math.Abs(xph) > phase)
                    {
                        str += "D";
                        if (!pD || Math.Abs(xph) > 1)
                        {
                            Stroke(0x20);
                        }
                        pD = true;
                    }
                    else
                    {
                        if (pD)
                        {
                            Release(0x20);
                        }
                        pD = false;
                    }
                    if (xph < 0 && Math.Abs(xph) > phase)
                    {
                        str += "A";
                        if (!pA || Math.Abs(xph) > 1)
                        {
                            Stroke(0x1E);
                        }
                        pA = true;
                    }
                    else
                    {
                        if (pA)
                        {
                            Release(0x1E);
                        }
                        pA = false;
                    }

                    if (z > 0 && zph > phase)
                    {
                        str += "S";
                        if (!pS || zph > 1)
                        {
                            Stroke(0x1F);
                        }
                        pS = true;
                    }
                    else
                    {
                        if (pS)
                        {
                            Release(0x1F);
                        }
                        pS = false;
                    }
                    if (z < 0 && zph > phase)
                    {
                        str += "W";
                        if (!pW || zph > 1)
                        {
                            Stroke(0x11);
                        }
                        pW = true;
                    }
                    else
                    {
                        if (pW)
                        {
                            Release(0x11);
                        }
                        pW = false;
                    }

                    UpdateText(form.debug_label, Convert.ToString(str));
                }
            }
            else
            {
                if (pW)
                {
                    Release(0x11);
                }
                if (pA)
                {
                    Release(0x1E);
                }
                if (pS)
                {
                    Release(0x1F);
                }
                if (pD)
                {
                    Release(0x20);
                }
                pW = false;
                pA = false;
                pS = false;
                pD = false;
            }
        }