Exemplo n.º 1
0
        /// <summary>
        /// Draw the Kinect Objects onto a Body within a canvas
        /// </summary>
        /// <param name="canvas">The Canvas to paint images on</param>
        /// <param name="sensor">The Kinect sensor</param>
        /// <param name="body">The body to locate the images on</param>
        /// <param name="kinectObjects">The Kinect Object</param>
        public static void DrawKinectObjects(this Canvas canvas, KinectSensor sensor, Body body, List <KinectObject> kinectObjects)
        {
            if (kinectObjects == null)
            {
                return;
            }

            foreach (KinectObject kinectObject in kinectObjects)
            {
                Joint firstJoint = body.Joints[(JointType)kinectObject.FirstJoint],
                                  secondJoint = body.Joints[(JointType)kinectObject.SecondJoint];


                if (firstJoint.TrackingState == TrackingState.Tracked && secondJoint.TrackingState == TrackingState.Tracked)
                {
                    CameraSpacePoint firstJointPos  = firstJoint.Position,
                                     secondJointPos = secondJoint.Position;

                    Point firstPoint  = new Point(),
                          secondPoint = new Point();

                    ColorSpacePoint firstColorPoint  = sensor.CoordinateMapper.MapCameraPointToColorSpace(firstJointPos),
                                    secondColorPoint = sensor.CoordinateMapper.MapCameraPointToColorSpace(secondJointPos);

                    firstPoint.X = float.IsInfinity(firstColorPoint.X) ? 0 : firstColorPoint.X;
                    firstPoint.Y = float.IsInfinity(firstColorPoint.Y) ? 0 : firstColorPoint.Y;

                    secondPoint.X = float.IsInfinity(secondColorPoint.X) ? 0 : secondColorPoint.X;
                    secondPoint.Y = float.IsInfinity(secondColorPoint.Y) ? 0 : secondColorPoint.Y;

                    firstJoint  = firstJoint.ScaleTo(canvas.ActualWidth, canvas.ActualHeight);
                    secondJoint = secondJoint.ScaleTo(canvas.ActualWidth, canvas.ActualHeight);

                    Image bmp = new Image()
                    {
                        Width  = KinectUtils.DistanceBetween(firstJoint, secondJoint) * 1.5,
                        Height = KinectUtils.DistanceBetween(firstJoint, secondJoint) * 1.5
                    };

                    Canvas.SetTop(bmp, (firstPoint.Y + (secondPoint.Y - firstPoint.Y) / 2 - kinectObject.OffsetY) - bmp.Height / 2);
                    Canvas.SetLeft(bmp, (firstPoint.X + (secondPoint.X - firstPoint.X) / 2 + kinectObject.OffsetX) - bmp.Width / 2);

                    bmp.RenderTransformOrigin = new Point(0.5f, 0.5f);

                    var rotateT = new RotateTransform(KinectUtils.AngleBetween(firstJoint, secondJoint) + 90);
                    var skewT   = new SkewTransform(KinectUtils.YAngleBetween(firstJoint, secondJoint), KinectUtils.YAngleBetween(firstJoint, secondJoint));

                    var transformGroup = new TransformGroup();
                    transformGroup.Children.Add(rotateT);
                    transformGroup.Children.Add(skewT);
                    bmp.RenderTransform = transformGroup;

                    //Load the image
                    bmp.Source = kinectObject.Image;
                    canvas.Children.Add(bmp);
                }
            }
        }
 /// <summary>
 /// The Infrared reader event
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void Reader_InfraredFrameArrived(object sender, InfraredFrameArrivedEventArgs e)
 {
     using (var frame = e.FrameReference.AcquireFrame()) {
         if (frame != null)
         {
             InfraredView.Source = KinectUtils.ToBitmap(frame);
         }
     }
 }
        /// <summary>
        /// The Body reader evnet
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Reader_BodyFrameArrived(object sender, BodyFrameArrivedEventArgs e)
        {
            using (var frame = e.FrameReference.AcquireFrame()) {
                if (frame != null && this.maxBodyCount > 0)
                {
                    var bodies = new Body[this.maxBodyCount];
                    frame.GetAndRefreshBodyData(bodies);

                    var closestBody = KinectUtils.GetClosestBody(frame);

                    Canvas.Children.Clear();

                    foreach (Body body in bodies)
                    {
                        if (body.IsTracked && body != null)
                        {
                            //Draw The Skeleton
                            if (this.DrawDots)
                            {
                                KinectDraw.DrawDots(Canvas, kinectSensor, body);
                            }
                            //Draw the images
                            if (this.DrawImages)
                            {
                                KinectDraw.DrawKinectObjects(Canvas, kinectSensor, body, kinectObjects);
                            }
                            //Render 3D Objects
                            if (this.RenderObjects)
                            {
                                KinectDraw.UpdateKinect3DObjects(body, kinect3DObjects);
                            }
                            //Use the Kinect to control the mouse and it is the closest body
                            if (this.KinectAsMouse && closestBody != null && body.TrackingId == closestBody.TrackingId)
                            {
                                CameraSpacePoint mousePoint = body.Joints[Mouse].Position;
                                CameraSpacePoint spinePoint = body.Joints[JointType.SpineBase].Position;
                                //Is the hand in front of the person?
                                if (mousePoint.Z - spinePoint.Z < -0.15f)
                                {
                                    Point curPos = MouseOverride.GetCursorPosition();
                                    MouseOverride.SetCursorPos((int)(curPos.X + (mousePoint.X * 2f * (int)SystemParameters.PrimaryScreenWidth - curPos.X) * 0.4), (int)(curPos.Y + (((spinePoint.Y - mousePoint.Y + 0.5f) + 0.25f) * 2f * (int)SystemParameters.PrimaryScreenHeight - curPos.Y) * 0.4));

                                    if (body.HandRightState == HandState.Closed)
                                    {
                                        if (!grippedHand)
                                        {
                                            // If the hand is closed hold down the left mouse button
                                            MouseOverride.MouseLeftDown();
                                            grippedHand = true;
                                            System.Windows.Input.Mouse.OverrideCursor = Cursor;
                                        }
                                    }
                                    else if (body.HandRightState == HandState.Open)
                                    {
                                        if (grippedHand)
                                        {
                                            // If the hand is opened let go of the left mouse button
                                            MouseOverride.MouseLeftUp();
                                            grippedHand = false;
                                            System.Windows.Input.Mouse.OverrideCursor = null;
                                        }
                                    }
                                }
                                else
                                {
                                    grippedHand = true;
                                }
                            }
                        }
                    }
                }
            }
        }