예제 #1
0
 private void ExecuteGesture(Gesture g, bool keyboardEmulation = true)
 {
     if (keyboardEmulation)
     {
         KeyboardUtils.HitKey(g);
     }
     HighlightGestureOnList(g);
 }
예제 #2
0
        private void SkeletonFrameReady_Detect(SkeletonFrameReadyEventArgs e, HighlightFramesDelegate highlightFrames, DeHighlightFramesDelegate deHighlightFrames, bool keyboardEmulation) //TODO: refactor into smaller methods
        {
            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
            {
                // Get data from stream
                if (skeletonFrame == null)
                {
                    return;
                }
                Skeleton[] skeletons = new Skeleton[kinect.SkeletonStream.FrameSkeletonArrayLength];
                skeletonFrame.CopySkeletonDataTo(skeletons);

                // Mind only the tracked, return if none
                Skeleton skeleton = skeletons.FirstOrDefault(s => s.TrackingState == SkeletonTrackingState.Tracked);
                if (skeleton == null)
                {
                    return;
                }

                // Init stuff
                SkeletonPoint centroid = skeleton.Joints[JointType.HipCenter].Position;

                // Get the data
                // Check each gesture in collection
                for (int gi = 0; gi < GestureCollection.Count; gi++)
                {
                    Gesture g = GestureCollection[gi];
                    // Get coordinates of tracked joint
                    // SkeletonPoint structure contains X. Y, Z values in meters, relative to the sensor
                    // We'll convert that to a Point3D that contains values in cm, relative to the centroid
                    SkeletonPoint trackedJoint = skeleton.Joints[g.Joint].Position;
                    int           jX           = (int)((trackedJoint.X - centroid.X) * 100);
                    int           jY           = (int)((trackedJoint.Y - centroid.Y) * 100);
                    int           jZ           = (int)((centroid.Z - trackedJoint.Z) * 100);
                    // Check each frame in gesture for collision
                    CollisionStates[gi][0] = CollisionStates[gi][1];
                    CollisionStates[gi][1] = JointCollisionStates.OutThere;
                    for (int fi = 0; fi < g.Frames.Count; fi++)
                    {
                        GestureFrame f = g.Frames[fi];
                        // Check for collision
                        foreach (GestureFrameCell fc in f.FrontCells.Where(c => c.IsHotspot))
                        {
                            if (jX <= fc.RightCM &&
                                jX >= fc.LeftCM &&
                                jY <= fc.TopCM &&
                                jY >= fc.BottomCM)
                            {
                                foreach (GestureFrameCell sc in f.SideCells.Where(c => c.IsHotspot))
                                {
                                    if (jZ <= sc.RightCM &&
                                        jZ >= sc.LeftCM &&
                                        jY <= sc.TopCM &&
                                        jY >= sc.BottomCM)
                                    {
                                        // Record collision
                                        CollisionTimes[gi][fi] = DateTime.Now;
                                        if (fi + 1 == g.Frames.Count)
                                        {
                                            CollisionStates[gi][1] = JointCollisionStates.InUltimateHotspot;
                                        }
                                        else
                                        {
                                            CollisionStates[gi][1] = JointCollisionStates.InHotspot;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    // Handle 1-frame gestures
                    // Keyboard emulation, List highlight and 3D highlights
                    if (g.Frames.Count == 1)
                    {
                        if (CollisionStates[gi][0] == JointCollisionStates.OutThere && CollisionStates[gi][1] == JointCollisionStates.InUltimateHotspot)
                        {
                            if (keyboardEmulation)
                            {
                                KeyboardUtils.HitKey(g);
                            }
                            HighlightGestureOnList(g);
                            highlightFrames(g, new List <GestureFrame>()
                            {
                                g.Frames[0]
                            });
                        }
                        else if (CollisionStates[gi][0] == JointCollisionStates.InUltimateHotspot && CollisionStates[gi][1] == JointCollisionStates.OutThere)
                        {
                            if (keyboardEmulation)
                            {
                                KeyboardUtils.ReleaseKey(g);
                            }
                            DeHighlightGestureOnList(g);
                            deHighlightFrames(g);
                        }
                    }

                    // Handle multi-frame gestures
                    else
                    {
                        // Keyboard emulation and List highlight
                        if (CollisionStates[gi][0] == JointCollisionStates.InUltimateHotspot && !(CollisionStates[gi][1] == JointCollisionStates.InUltimateHotspot))
                        {
                            if (keyboardEmulation)
                            {
                                KeyboardUtils.ReleaseKey(g);
                            }
                            DeHighlightGestureOnList(g);
                        }
                        else if (!(CollisionStates[gi][0] == JointCollisionStates.InUltimateHotspot) && CollisionStates[gi][1] == JointCollisionStates.InUltimateHotspot)
                        {
                            for (int i = 1; i < CollisionTimes[gi].Count; i++)
                            {
                                if (CollisionTimes[gi][i] - CollisionTimes[gi][i - 1] > CollisionTimeout)
                                {
                                    break;
                                }
                                if (i + 1 == CollisionTimes[gi].Count)
                                {
                                    if (keyboardEmulation)
                                    {
                                        KeyboardUtils.HitKey(g);
                                    }
                                    HighlightGestureOnList(g);
                                }
                            }
                        }

                        // 3D highlights
                        List <GestureFrame> FramesToHighlight = new List <GestureFrame>();
                        if (CollisionStates[gi][0] == JointCollisionStates.OutThere && CollisionStates[gi][1] == JointCollisionStates.OutThere)
                        {
                            deHighlightFrames(g);
                        }
                        else if (CollisionStates[gi][0] == JointCollisionStates.OutThere && CollisionStates[gi][1] == JointCollisionStates.InHotspot)
                        {
                            if (CollisionTimes[gi].IndexOf(CollisionTimes[gi].Max()) == 0)
                            {
                                highlightFrames(g, new List <GestureFrame>()
                                {
                                    g.Frames[0]
                                });
                            }
                            else
                            {
                                deHighlightFrames(g);
                            }
                        }
                        else if ((CollisionStates[gi][0] == JointCollisionStates.OutThere && CollisionStates[gi][1] == JointCollisionStates.InUltimateHotspot) ||
                                 (CollisionStates[gi][0] == JointCollisionStates.InHotspot && CollisionStates[gi][1] == JointCollisionStates.OutThere))
                        {
                            deHighlightFrames(g);
                        }
                        else if (CollisionStates[gi][0] == JointCollisionStates.InHotspot && CollisionStates[gi][1] == JointCollisionStates.InHotspot)
                        {
                            if (CollisionTimes[gi].IndexOf(CollisionTimes[gi].Max()) == 0)
                            {
                                highlightFrames(g, new List <GestureFrame>()
                                {
                                    g.Frames[0]
                                });
                            }
                            else
                            {
                                FramesToHighlight = new List <GestureFrame>();
                                FramesToHighlight.Add(g.Frames[0]);
                                for (int i = 1; i < CollisionTimes[gi].Count; i++)
                                {
                                    TimeSpan ts = CollisionTimes[gi][i] - CollisionTimes[gi][i - 1];
                                    if (ts.Ticks < 0)
                                    {
                                        break;
                                    }
                                    else if (ts > CollisionTimeout)
                                    {
                                        deHighlightFrames(g);
                                        FramesToHighlight = new List <GestureFrame>();
                                        break;
                                    }
                                    else
                                    {
                                        FramesToHighlight.Add(g.Frames[i]);
                                    }
                                }
                                highlightFrames(g, FramesToHighlight);
                            }
                        }
                        else if (CollisionStates[gi][0] == JointCollisionStates.InHotspot && CollisionStates[gi][1] == JointCollisionStates.InUltimateHotspot)
                        {
                            FramesToHighlight = new List <GestureFrame>();
                            for (int i = 0; i < CollisionTimes[gi].Count - 1; i++)
                            {
                                TimeSpan ts = CollisionTimes[gi][i + 1] - CollisionTimes[gi][i];
                                if (CollisionTimes[gi][i] > CollisionTimes[gi][i + 1] || ts > CollisionTimeout)
                                {
                                    FramesToHighlight = new List <GestureFrame>();
                                    break;
                                }
                                FramesToHighlight.Add(g.Frames[i]);
                                if (!FramesToHighlight.Contains(g.Frames.Last()))
                                {
                                    FramesToHighlight.Add(g.Frames.Last());
                                }
                            }
                            highlightFrames(g, FramesToHighlight);
                        }
                        else if (CollisionStates[gi][0] == JointCollisionStates.InUltimateHotspot && !(CollisionStates[gi][1] == JointCollisionStates.InUltimateHotspot))
                        {
                            deHighlightFrames(g);
                        }
                    }
                }
            } // using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
        }     // SkeletonFrameReady_Detect method