Esta página está en construcción: perdonen los errores, repeticiones y temas inacabados.
This page is being developed: I am sorry for errors, duplications and unfinished subjects.
Proyecto Danzante. Paso de C# a VB (incluido en Danzante) Nos topamos con C# de manera ideludible al migrar nuestro proyecto Dueto a Windows 10 y las Ejemplos de progrmacion para Kinect for Windows, que usa la cámara XBOX ONE. Primero comparamos dos programas correspondientes a una ventana WPF. La traducción la ejecuta y ofrece http://converter.telerik.com/
//------------------------------------------------------------------------------ // <copyright file="MainWindow.xaml.cs" company="Microsoft"> // Copyright (c) Microsoft Corporation. All rights reserved. // </copyright> //------------------------------------------------------------------------------ namespace Microsoft.Samples.Kinect.BodyBasics { using System; using System.Collections.Generic; using System.Compontion; using System.IO; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; using Microsoft.Kinect; /// <summary> /// Interaction logic for MainWindow /// </summary> public partial class MainWindow : Window, INotifyPropertyChanged { /// <summary> /// Radius of drawn hand circles /// </summary> private const double HandSize = 30; /// <summary> /// Thickness of drawn joint lines /// </summary> private const double JointThickness = 3; /// <summary> /// Thickness of clip edge rectangles /// </summary> private const double ClipBoundsThickness = 10; /// <summary> /// Constant for clamping Z values of camera space points from being negative /// </summary> private const float InferredZPositionClamp = 0.1f; /// <summary> /// Brush used for drawing hands that are currently tracked as closed /// </summary> private readonly Brush handClosedBrush = new SolidColorBrush(Color.FromArgb(128, 255, 0, 0)); /// <summary> /// Brush used for drawing hands that are currently tracked as opened /// </summary> private readonly Brush handOpenBrush = new SolidColorBrush(Color.FromArgb(128, 0, 255, 0)); /// <summary> /// Brush used for drawing hands that are currently tracked as in lasso (pointer) position /// </summary> private readonly Brush handLassoBrush = new SolidColorBrush(Color.FromArgb(128, 0, 0, 255)); /// <summary> /// Brush used for drawing joints that are currently tracked /// </summary> private readonly Brush trackedJointBrush = new SolidColorBrush(Color.FromArgb(255, 68, 192, 68)); /// <summary> /// Brush used for drawing joints that are currently inferred /// </summary> private readonly Brush inferredJointBrush = Brushes.Yellow; /// <summary> /// Pen used for drawing bones that are currently inferred /// </summary> private readonly Pen inferredBonePen = new Pen(Brushes.Gray, 1); /// <summary> /// Drawing group for body rendering output /// </summary> private DrawingGroup drawingGroup; /// <summary> /// Drawing image that we will display /// </summary> private DrawingImage imageSource; /// <summary> /// Active Kinect sensor /// </summary> private KinectSensor kinectSensor = null; /// <summary> /// Coordinate mapper to map one type of point to another /// </summary> private CoordinateMapper coordinateMapper = null; /// <summary> /// Reader for body frames /// </summary> private BodyFrameReader bodyFrameReader = null; /// <summary> /// Array for the bodies /// </summary> private Body[] bodies = null; /// <summary> /// definition of bones /// </summary> private List<Tuple<JointType, JointType>> bones; /// <summary> /// Width of display (depth space) /// </summary> private int displayWidth; /// <summary> /// Height of display (depth space) /// </summary> private int displayHeight; /// <summary> /// List of colors for each body tracked /// </summary> private List<Pen> bodyColors; /// <summary> /// Current status text to display /// </summary> private string statusText = null; /// <summary> /// Initializes a new instance of the MainWindow class. /// </summary> public MainWindow() { // one sensor is currently supported this.kinectSensor = KinectSensor.GetDefault(); // get the coordinate mapper this.coordinateMapper = this.kinectSensor.CoordinateMapper; // get the depth (display) extents FrameDescription frameDescription = this.kinectSensor.DepthFrameSource.FrameDescription; // get size of joint space this.displayWidth = frameDescription.Width; this.displayHeight = frameDescription.Height; // open the reader for the body frames this.bodyFrameReader = this.kinectSensor.BodyFrameSource.OpenReader(); // a bone defined as a line between two joints this.bones = new List<Tuple<JointType, JointType>>(); // Torso this.bones.Add(new Tuple<JointType, JointType>(JointType.Head, JointType.Neck)); this.bones.Add(new Tuple<JointType, JointType>(JointType.Neck, JointType.SpineShoulder)); this.bones.Add(new Tuple<JointType, JointType>(JointType.SpineShoulder, JointType.SpineMid)); this.bones.Add(new Tuple<JointType, JointType>(JointType.SpineMid, JointType.SpineBase)); this.bones.Add(new Tuple<JointType, JointType>(JointType.SpineShoulder, JointType.ShoulderRight)); this.bones.Add(new Tuple<JointType, JointType>(JointType.SpineShoulder, JointType.ShoulderLeft)); this.bones.Add(new Tuple<JointType, JointType>(JointType.SpineBase, JointType.HipRight)); this.bones.Add(new Tuple<JointType, JointType>(JointType.SpineBase, JointType.HipLeft)); // Right Arm this.bones.Add(new Tuple<JointType, JointType>(JointType.ShoulderRight, JointType.ElbowRight)); this.bones.Add(new Tuple<JointType, JointType>(JointType.ElbowRight, JointType.WristRight)); this.bones.Add(new Tuple<JointType, JointType>(JointType.WristRight, JointType.HandRight)); this.bones.Add(new Tuple<JointType, JointType>(JointType.HandRight, JointType.HandTipRight)); this.bones.Add(new Tuple<JointType, JointType>(JointType.WristRight, JointType.ThumbRight)); // Left Arm this.bones.Add(new Tuple<JointType, JointType>(JointType.ShoulderLeft, JointType.ElbowLeft)); this.bones.Add(new Tuple<JointType, JointType>(JointType.ElbowLeft, JointType.WristLeft)); this.bones.Add(new Tuple<JointType, JointType>(JointType.WristLeft, JointType.HandLeft)); this.bones.Add(new Tuple<JointType, JointType>(JointType.HandLeft, JointType.HandTipLeft)); this.bones.Add(new Tuple<JointType, JointType>(JointType.WristLeft, JointType.ThumbLeft)); // Right Leg this.bones.Add(new Tuple<JointType, JointType>(JointType.HipRight, JointType.KneeRight)); this.bones.Add(new Tuple<JointType, JointType>(JointType.KneeRight, JointType.AnkleRight)); this.bones.Add(new Tuple<JointType, JointType>(JointType.AnkleRight, JointType.FootRight)); // Left Leg this.bones.Add(new Tuple<JointType, JointType>(JointType.HipLeft, JointType.KneeLeft)); this.bones.Add(new Tuple<JointType, JointType>(JointType.KneeLeft, JointType.AnkleLeft)); this.bones.Add(new Tuple<JointType, JointType>(JointType.AnkleLeft, JointType.FootLeft)); // populate body colors, one for each BodyIndex this.bodyColors = new List<Pen>(); this.bodyColors.Add(new Pen(Brushes.Red, 6)); this.bodyColors.Add(new Pen(Brushes.Orange, 6)); this.bodyColors.Add(new Pen(Brushes.Green, 6)); this.bodyColors.Add(new Pen(Brushes.Blue, 6)); this.bodyColors.Add(new Pen(Brushes.Indigo, 6)); this.bodyColors.Add(new Pen(Brushes.Violet, 6)); // set IsAvailableChanged event notifier this.kinectSensor.IsAvailableChanged += this.Sensor_IsAvailableChanged; // open the sensor this.kinectSensor.Open(); // set the status text this.StatusText = this.kinectSensor.IsAvailable ? Properties.Resources.RunningStatusText : Properties.Resources.NoSensorStatusText; // Create the drawing group we'll use for drawing this.drawingGroup = new DrawingGroup(); // Create an image source that we can use in our image control this.imageSource = new DrawingImage(this.drawingGroup); // use the window object as the view model in this simple example this.DataContext = this; // initialize the components (controls) of the window this.InitializeComponent(); } /// <summary> /// INotifyPropertyChangedPropertyChanged event to allow window controls to bind to changeable data /// </summary> public event PropertyChangedEventHandler PropertyChanged; /// <summary> /// Gets the bitmap to display /// </summary> public ImageSource ImageSource { get { return this.imageSource; } } /// <summary> /// Gets or sets the current status text to display /// </summary> public string StatusText { get { return this.statusText; } set { if (this.statusText != value) { this.statusText = value; // notify any bound elements that the text has changed if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs("StatusText")); } } } } /// <summary> /// Execute start up tasks /// </summary> /// <param name="sender">object sending the event</param> /// <param name="e">event arguments</param> private void MainWindow_Loaded(object sender, RoutedEventArgs e) { if (this.bodyFrameReader != null) { this.bodyFrameReader.FrameArrived += this.Reader_FrameArrived; } } /// <summary> /// Execute shutdown tasks /// </summary> /// <param name="sender">object sending the event</param> /// <param name="e">event arguments</param> private void MainWindow_Closing(object sender, CancelEventArgs e) { if (this.bodyFrameReader != null) { // BodyFrameReader is IDisposable this.bodyFrameReader.Dispose(); this.bodyFrameReader = null; } if (this.kinectSensor != null) { this.kinectSensor.Close(); this.kinectSensor = null; } } /// <summary> /// Handles the body frame data arriving from the sensor /// </summary> /// <param name="sender">object sending the event</param> /// <param name="e">event arguments</param> private void Reader_FrameArrived(object sender, BodyFrameArrivedEventArgs e) { bool dataReceived = false; using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame()) { if (bodyFrame != null) { if (this.bodies == null) { this.bodies = new Body[bodyFrame.BodyCount]; } // The first time GetAndRefreshBodyData is called, Kinect will allocate each Body in the array. // As long as those body objects are not disposed and not set to null in the array, // those body objects will be re-used. bodyFrame.GetAndRefreshBodyData(this.bodies); dataReceived = true; } } if (dataReceived) { using (DrawingContext dc = this.drawingGroup.Open()) { // Draw a transparent background to set the render size dc.DrawRectangle(Brushes.Black, null, new Rect(0.0, 0.0, this.displayWidth, this.displayHeight)); int penIndex = 0; foreach (Body body in this.bodies) { Pen drawPen = this.bodyColors[penIndex++]; if (body.IsTracked) { this.DrawClippedEdges(body, dc); IReadOnlyDictionary<JointType, Joint> joints = body.Joints; // convert the joint points to depth (display) space Dictionary<JointType, Point> jointPoints = new Dictionary<JointType, Point>(); foreach (JointType jointType in joints.Keys) { // sometimes the depth(Z) of an inferred joint may show as negative // clamp down to 0.1f to prevent coordinatemapper from returning (-Infinity, -Infinity) CameraSpacePoint position = joints[jointType].Position; if (position.Z < 0) { position.Z = InferredZPositionClamp; } DepthSpacePoint depthSpacePoint = this.coordinateMapper.MapCameraPointToDepthSpace(position); jointPoints[jointType] = new Point(depthSpacePoint.X, depthSpacePoint.Y); } this.DrawBody(joints, jointPoints, dc, drawPen); this.DrawHand(body.HandLeftState, jointPoints[JointType.HandLeft], dc); this.DrawHand(body.HandRightState, jointPoints[JointType.HandRight], dc); } } // prevent drawing outside of our render area this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, this.displayWidth, this.displayHeight)); } } } /// <summary> /// Draws a body /// </summary> /// <param name="joints">joints to draw</param> /// <param name="jointPoints">translated positions of joints to draw</param> /// <param name="drawingContext">drawing context to draw to</param> /// <param name="drawingPen">specifies color to draw a specific body</param> private void DrawBody(IReadOnlyDictionary<JointType, Joint> joints, IDictionary<JointType, Point> jointPoints, DrawingContext drawingContext, Pen drawingPen) { // Draw the bones foreach (var bone in this.bones) { this.DrawBone(joints, jointPoints, bone.Item1, bone.Item2, drawingContext, drawingPen); } // Draw the joints foreach (JointType jointType in joints.Keys) { Brush drawBrush = null; TrackingState trackingState = joints[jointType].TrackingState; if (trackingState == TrackingState.Tracked) { drawBrush = this.trackedJointBrush; } else if (trackingState == TrackingState.Inferred) { drawBrush = this.inferredJointBrush; } if (drawBrush != null) { drawingContext.DrawEllipse(drawBrush, null, jointPoints[jointType], JointThickness, JointThickness); } } } /// <summary> /// Draws one bone of a body (joint to joint) /// </summary> /// <param name="joints">joints to draw</param> /// <param name="jointPoints">translated positions of joints to draw</param> /// <param name="jointType0">first joint of bone to draw</param> /// <param name="jointType1">second joint of bone to draw</param> /// <param name="drawingContext">drawing context to draw to</param> /// /// <param name="drawingPen">specifies color to draw a specific bone</param> private void DrawBone(IReadOnlyDictionary<JointType, Joint> joints, IDictionary<JointType, Point> jointPoints, JointType jointType0, JointType jointType1, DrawingContext drawingContext, Pen drawingPen) { Joint joint0 = joints[jointType0]; Joint joint1 = joints[jointType1]; // If we can't find either of these joints, exit if (joint0.TrackingState == TrackingState.NotTracked || joint1.TrackingState == TrackingState.NotTracked) { return; } // We assume all drawn bones are inferred unless BOTH joints are tracked Pen drawPen = this.inferredBonePen; if ((joint0.TrackingState == TrackingState.Tracked) && (joint1.TrackingState == TrackingState.Tracked)) { drawPen = drawingPen; } drawingContext.DrawLine(drawPen, jointPoints[jointType0], jointPoints[jointType1]); } /// <summary> /// Draws a hand symbol if the hand is tracked: red circle = closed, green circle = opened; blue circle = lasso /// </summary> /// <param name="handState">state of the hand</param> /// <param name="handPosition">position of the hand</param> /// <param name="drawingContext">drawing context to draw to</param> private void DrawHand(HandState handState, Point handPosition, DrawingContext drawingContext) { switch (handState) { case HandState.Closed: drawingContext.DrawEllipse(this.handClosedBrush, null, handPosition, HandSize, HandSize); break; case HandState.Open: drawingContext.DrawEllipse(this.handOpenBrush, null, handPosition, HandSize, HandSize); break; case HandState.Lasso: drawingContext.DrawEllipse(this.handLassoBrush, null, handPosition, HandSize, HandSize); break; } } /// <summary> /// Draws indicators to show which edges are clipping body data /// </summary> /// <param name="body">body to draw clipping information for</param> /// <param name="drawingContext">drawing context to draw to</param> private void DrawClippedEdges(Body body, DrawingContext drawingContext) { FrameEdges clippedEdges = body.ClippedEdges; if (clippedEdges.HasFlag(FrameEdges.Bottom)) { drawingContext.DrawRectangle( Brushes.Red, null, new Rect(0, this.displayHeight - ClipBoundsThickness, this.displayWidth, ClipBoundsThickness)); } if (clippedEdges.HasFlag(FrameEdges.Top)) { drawingContext.DrawRectangle( Brushes.Red, null, new Rect(0, 0, this.displayWidth, ClipBoundsThickness)); } if (clippedEdges.HasFlag(FrameEdges.Left)) { drawingContext.DrawRectangle( Brushes.Red, null, new Rect(0, 0, ClipBoundsThickness, this.displayHeight)); } if (clippedEdges.HasFlag(FrameEdges.Right)) { drawingContext.DrawRectangle( Brushes.Red, null, new Rect(this.displayWidth - ClipBoundsThickness, 0, ClipBoundsThickness, this.displayHeight)); } } /// <summary> /// Handles the event which the sensor becomes unavailable (E.g. paused, closed, unplugged). /// </summary> /// <param name="sender">object sending the event</param> /// <param name="e">event arguments</param> private void Sensor_IsAvailableChanged(object sender, IsAvailableChangedEventArgs e) { // on failure, set the status text this.StatusText = this.kinectSensor.IsAvailable ? Properties.Resources.RunningStatusText : Properties.Resources.SensorNotAvailableStatusText; } } }
'------------------------------------------------------------------------------ ' <copyright file="MainWindow.xaml.cs" company="Microsoft"> ' Copyright (c) Microsoft Corporation. All rights reserved. ' </copyright> '------------------------------------------------------------------------------ Imports System.Collections.Generic Imports System.ComponentModel Imports System.Diagnostics Imports System.Globalization Imports System.IO Imports System.Windows Imports System.Windows.Media Imports System.Windows.Media.Imaging Imports Microsoft.Kinect Namespace Microsoft.Samples.Kinect.BodyBasics ''' <summary> ''' Interaction logic for MainWindow ''' </summary> Public Partial Class MainWindow Inherits Window Implements INotifyPropertyChanged ''' <summary> ''' Radius of drawn hand circles ''' </summary> Private Const HandSize As Double = 30 ''' <summary> ''' Thickness of drawn joint lines ''' </summary> Private Const JointThickness As Double = 3 ''' <summary> ''' Thickness of clip edge rectangles ''' </summary> Private Const ClipBoundsThickness As Double = 10 ''' <summary> ''' Constant for clamping Z values of camera space points from being negative ''' </summary> Private Const InferredZPositionClamp As Single = 0.1F ''' <summary> ''' Brush used for drawing hands that are currently tracked as closed ''' </summary> Private ReadOnly handClosedBrush As Brush = New SolidColorBrush(Color.FromArgb(128, 255, 0, 0)) ''' <summary> ''' Brush used for drawing hands that are currently tracked as opened ''' </summary> Private ReadOnly handOpenBrush As Brush = New SolidColorBrush(Color.FromArgb(128, 0, 255, 0)) ''' <summary> ''' Brush used for drawing hands that are currently tracked as in lasso (pointer) position ''' </summary> Private ReadOnly handLassoBrush As Brush = New SolidColorBrush(Color.FromArgb(128, 0, 0, 255)) ''' <summary> ''' Brush used for drawing joints that are currently tracked ''' </summary> Private ReadOnly trackedJointBrush As Brush = New SolidColorBrush(Color.FromArgb(255, 68, 192, 68)) ''' <summary> ''' Brush used for drawing joints that are currently inferred ''' </summary> Private ReadOnly inferredJointBrush As Brush = Brushes.Yellow ''' <summary> ''' Pen used for drawing bones that are currently inferred ''' </summary> Private ReadOnly inferredBonePen As New Pen(Brushes.Gray, 1) ''' <summary> ''' Drawing group for body rendering output ''' </summary> Private drawingGroup As DrawingGroup ''' <summary> ''' Drawing image that we will display ''' </summary> Private m_imageSource As DrawingImage ''' <summary> ''' Active Kinect sensor ''' </summary> Private kinectSensor As KinectSensor = Nothing ''' <summary> ''' Coordinate mapper to map one type of point to another ''' </summary> Private coordinateMapper As CoordinateMapper = Nothing ''' <summary> ''' Reader for body frames ''' </summary> Private bodyFrameReader As BodyFrameReader = Nothing ''' <summary> ''' Array for the bodies ''' </summary> Private bodies As Body() = Nothing ''' <summary> ''' definition of bones ''' </summary> Private bones As List(Of Tuple(Of JointType, JointType)) ''' <summary> ''' Width of display (depth space) ''' </summary> Private displayWidth As Integer ''' <summary> ''' Height of display (depth space) ''' </summary> Private displayHeight As Integer ''' <summary> ''' List of colors for each body tracked ''' </summary> Private bodyColors As List(Of Pen) ''' <summary> ''' Current status text to display ''' </summary> Private m_statusText As String = Nothing ''' <summary> ''' Initializes a new instance of the MainWindow class. ''' </summary> Public Sub New() ' one sensor is currently supported Me.kinectSensor = KinectSensor.GetDefault() ' get the coordinate mapper Me.coordinateMapper = Me.kinectSensor.CoordinateMapper ' get the depth (display) extents Dim frameDescription As FrameDescription = Me.kinectSensor.DepthFrameSource.FrameDescription ' get size of joint space Me.displayWidth = frameDescription.Width Me.displayHeight = frameDescription.Height ' open the reader for the body frames Me.bodyFrameReader = Me.kinectSensor.BodyFrameSource.OpenReader() ' a bone defined as a line between two joints Me.bones = New List(Of Tuple(Of JointType, JointType))() ' Torso Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.Head, JointType.Neck)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.Neck, JointType.SpineShoulder)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.SpineShoulder, JointType.SpineMid)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.SpineMid, JointType.SpineBase)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.SpineShoulder, JointType.ShoulderRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.SpineShoulder, JointType.ShoulderLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.SpineBase, JointType.HipRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.SpineBase, JointType.HipLeft)) ' Right Arm Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.ShoulderRight, JointType.ElbowRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.ElbowRight, JointType.WristRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.WristRight, JointType.HandRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.HandRight, JointType.HandTipRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.WristRight, JointType.ThumbRight)) ' Left Arm Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.ShoulderLeft, JointType.ElbowLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.ElbowLeft, JointType.WristLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.WristLeft, JointType.HandLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.HandLeft, JointType.HandTipLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.WristLeft, JointType.ThumbLeft)) ' Right Leg Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.HipRight, JointType.KneeRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.KneeRight, JointType.AnkleRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.AnkleRight, JointType.FootRight)) ' Left Leg Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.HipLeft, JointType.KneeLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.KneeLeft, JointType.AnkleLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.AnkleLeft, JointType.FootLeft)) ' populate body colors, one for each BodyIndex Me.bodyColors = New List(Of Pen)() Me.bodyColors.Add(New Pen(Brushes.Red, 6)) Me.bodyColors.Add(New Pen(Brushes.Orange, 6)) Me.bodyColors.Add(New Pen(Brushes.Green, 6)) Me.bodyColors.Add(New Pen(Brushes.Blue, 6)) Me.bodyColors.Add(New Pen(Brushes.Indigo, 6)) Me.bodyColors.Add(New Pen(Brushes.Violet, 6)) ' set IsAvailableChanged event notifier AddHandler Me.kinectSensor.IsAvailableChanged, AddressOf Me.Sensor_IsAvailableChanged ' open the sensor Me.kinectSensor.Open() ' set the status text Me.StatusText = If(Me.kinectSensor.IsAvailable, Properties.Resources.RunningStatusText, Properties.Resources.NoSensorStatusText) ' Create the drawing group we'll use for drawing Me.drawingGroup = New DrawingGroup() ' Create an image source that we can use in our image control Me.m_imageSource = New DrawingImage(Me.drawingGroup) ' use the window object as the view model in this simple example Me.DataContext = Me ' initialize the components (controls) of the window Me.InitializeComponent() End Sub ''' <summary> ''' INotifyPropertyChangedPropertyChanged event to allow window controls to bind to changeable data ''' </summary> Public Event PropertyChanged As PropertyChangedEventHandler ''' <summary> ''' Gets the bitmap to display ''' </summary> Public ReadOnly Property ImageSource() As ImageSource Get Return Me.m_imageSource End Get End Property ''' <summary> ''' Gets or sets the current status text to display ''' </summary> Public Property StatusText() As String Get Return Me.m_statusText End Get Set If Me.m_statusText <> value Then Me.m_statusText = value ' notify any bound elements that the text has changed RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("StatusText")) End If End Set End Property ''' <summary> ''' Execute start up tasks ''' </summary> ''' <param name="sender">object sending the event</param> ''' <param name="e">event arguments</param> Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) If Me.bodyFrameReader IsNot Nothing Then AddHandler Me.bodyFrameReader.FrameArrived, AddressOf Me.Reader_FrameArrived End If End Sub ''' <summary> ''' Execute shutdown tasks ''' </summary> ''' <param name="sender">object sending the event</param> ''' <param name="e">event arguments</param> Private Sub MainWindow_Closing(sender As Object, e As CancelEventArgs) If Me.bodyFrameReader IsNot Nothing Then ' BodyFrameReader is IDisposable Me.bodyFrameReader.Dispose() Me.bodyFrameReader = Nothing End If If Me.kinectSensor IsNot Nothing Then Me.kinectSensor.Close() Me.kinectSensor = Nothing End If End Sub ''' <summary> ''' Handles the body frame data arriving from the sensor ''' </summary> ''' <param name="sender">object sending the event</param> ''' <param name="e">event arguments</param> Private Sub Reader_FrameArrived(sender As Object, e As BodyFrameArrivedEventArgs) Dim dataReceived As Boolean = False Using bodyFrame As BodyFrame = e.FrameReference.AcquireFrame() If bodyFrame IsNot Nothing Then If Me.bodies Is Nothing Then Me.bodies = New Body(bodyFrame.BodyCount - 1) {} End If ' The first time GetAndRefreshBodyData is called, Kinect will allocate each Body in the array. ' As long as those body objects are not disposed and not set to null in the array, ' those body objects will be re-used. bodyFrame.GetAndRefreshBodyData(Me.bodies) dataReceived = True End If End Using If dataReceived Then Using dc As DrawingContext = Me.drawingGroup.Open() ' Draw a transparent background to set the render size dc.DrawRectangle(Brushes.Black, Nothing, New Rect(0.0, 0.0, Me.displayWidth, Me.displayHeight)) Dim penIndex As Integer = 0 For Each body As Body In Me.bodies Dim drawPen As Pen = Me.bodyColors(System.Math.Max(System.Threading.Interlocked.Increment(penIndex),penIndex - 1)) If body.IsTracked Then Me.DrawClippedEdges(body, dc) Dim joints As IReadOnlyDictionary(Of JointType, Joint) = body.Joints ' convert the joint points to depth (display) space Dim jointPoints As New Dictionary(Of JointType, Point)() For Each jointType__1 As JointType In joints.Keys ' sometimes the depth(Z) of an inferred joint may show as negative ' clamp down to 0.1f to prevent coordinatemapper from returning (-Infinity, -Infinity) Dim position As CameraSpacePoint = joints(jointType__1).Position If position.Z < 0 Then position.Z = InferredZPositionClamp End If Dim depthSpacePoint As DepthSpacePoint = Me.coordinateMapper.MapCameraPointToDepthSpace(position) jointPoints(jointType__1) = New Point(depthSpacePoint.X, depthSpacePoint.Y) Next Me.DrawBody(joints, jointPoints, dc, drawPen) Me.DrawHand(body.HandLeftState, jointPoints(JointType.HandLeft), dc) Me.DrawHand(body.HandRightState, jointPoints(JointType.HandRight), dc) End If Next ' prevent drawing outside of our render area Me.drawingGroup.ClipGeometry = New RectangleGeometry(New Rect(0.0, 0.0, Me.displayWidth, Me.displayHeight)) End Using End If End Sub ''' <summary> ''' Draws a body ''' </summary> ''' <param name="joints">joints to draw</param> ''' <param name="jointPoints">translated positions of joints to draw</param> ''' <param name="drawingContext">drawing context to draw to</param> ''' <param name="drawingPen">specifies color to draw a specific body</param> Private Sub DrawBody(joints As IReadOnlyDictionary(Of JointType, Joint), jointPoints As IDictionary(Of JointType, Point), drawingContext As DrawingContext, drawingPen As Pen) ' Draw the bones For Each bone As var In Me.bones Me.DrawBone(joints, jointPoints, bone.Item1, bone.Item2, drawingContext, drawingPen) Next ' Draw the joints For Each jointType As JointType In joints.Keys Dim drawBrush As Brush = Nothing Dim trackingState__1 As TrackingState = joints(jointType).TrackingState If trackingState__1 = TrackingState.Tracked Then drawBrush = Me.trackedJointBrush ElseIf trackingState__1 = TrackingState.Inferred Then drawBrush = Me.inferredJointBrush End If If drawBrush IsNot Nothing Then drawingContext.DrawEllipse(drawBrush, Nothing, jointPoints(jointType), JointThickness, JointThickness) End If Next End Sub ''' <summary> ''' Draws one bone of a body (joint to joint) ''' </summary> ''' <param name="joints">joints to draw</param> ''' <param name="jointPoints">translated positions of joints to draw</param> ''' <param name="jointType0">first joint of bone to draw</param> ''' <param name="jointType1">second joint of bone to draw</param> ''' <param name="drawingContext">drawing context to draw to</param> ''' /// <param name="drawingPen">specifies color to draw a specific bone</param> Private Sub DrawBone(joints As IReadOnlyDictionary(Of JointType, Joint), jointPoints As IDictionary(Of JointType, Point), jointType0 As JointType, jointType1 As JointType, drawingContext As DrawingContext, drawingPen As Pen) Dim joint0 As Joint = joints(jointType0) Dim joint1 As Joint = joints(jointType1) ' If we can't find either of these joints, exit If joint0.TrackingState = TrackingState.NotTracked OrElse joint1.TrackingState = TrackingState.NotTracked Then Return End If ' We assume all drawn bones are inferred unless BOTH joints are tracked Dim drawPen As Pen = Me.inferredBonePen If (joint0.TrackingState = TrackingState.Tracked) AndAlso (joint1.TrackingState = TrackingState.Tracked) Then drawPen = drawingPen End If drawingContext.DrawLine(drawPen, jointPoints(jointType0), jointPoints(jointType1)) End Sub ''' <summary> ''' Draws a hand symbol if the hand is tracked: red circle = closed, green circle = opened; blue circle = lasso ''' </summary> ''' <param name="handState">state of the hand</param> ''' <param name="handPosition">position of the hand</param> ''' <param name="drawingContext">drawing context to draw to</param> Private Sub DrawHand(handState__1 As HandState, handPosition As Point, drawingContext As DrawingContext) Select Case handState__1 Case HandState.Closed drawingContext.DrawEllipse(Me.handClosedBrush, Nothing, handPosition, HandSize, HandSize) Exit Select Case HandState.Open drawingContext.DrawEllipse(Me.handOpenBrush, Nothing, handPosition, HandSize, HandSize) Exit Select Case HandState.Lasso drawingContext.DrawEllipse(Me.handLassoBrush, Nothing, handPosition, HandSize, HandSize) Exit Select End Select End Sub ''' <summary> ''' Draws indicators to show which edges are clipping body data ''' </summary> ''' <param name="body">body to draw clipping information for</param> ''' <param name="drawingContext">drawing context to draw to</param> Private Sub DrawClippedEdges(body As Body, drawingContext As DrawingContext) Dim clippedEdges As FrameEdges = body.ClippedEdges If clippedEdges.HasFlag(FrameEdges.Bottom) Then drawingContext.DrawRectangle(Brushes.Red, Nothing, New Rect(0, Me.displayHeight - ClipBoundsThickness, Me.displayWidth, ClipBoundsThickness)) End If If clippedEdges.HasFlag(FrameEdges.Top) Then drawingContext.DrawRectangle(Brushes.Red, Nothing, New Rect(0, 0, Me.displayWidth, ClipBoundsThickness)) End If If clippedEdges.HasFlag(FrameEdges.Left) Then drawingContext.DrawRectangle(Brushes.Red, Nothing, New Rect(0, 0, ClipBoundsThickness, Me.displayHeight)) End If If clippedEdges.HasFlag(FrameEdges.Right) Then drawingContext.DrawRectangle(Brushes.Red, Nothing, New Rect(Me.displayWidth - ClipBoundsThickness, 0, ClipBoundsThickness, Me.displayHeight)) End If End Sub ''' <summary> ''' Handles the event which the sensor becomes unavailable (E.g. paused, closed, unplugged). ''' </summary> ''' <param name="sender">object sending the event</param> ''' <param name="e">event arguments</param> Private Sub Sensor_IsAvailableChanged(sender As Object, e As IsAvailableChangedEventArgs) ' on failure, set the status text Me.StatusText = If(Me.kinectSensor.IsAvailable, Properties.Resources.RunningStatusText, Properties.Resources.SensorNotAvailableStatusText) End Sub End Class End Namespace '======================================================= 'Service provided by Telerik (www.telerik.com) 'Conversion powered by NRefactory. 'Twitter: @telerik 'Facebook: facebook.com/telerik '=======================================================
comparo con otra traducción PARECEN IDENTICAS '------------------------------------------------------------------------------ ' <copyright file="MainWindow.xaml.cs" company="Microsoft"> ' Copyright (c) Microsoft Corporation. All rights reserved. ' </copyright> '------------------------------------------------------------------------------ Imports System.Collections.Generic Imports System.ComponentModel Imports System.Diagnostics Imports System.Globalization Imports System.IO Imports System.Windows Imports System.Windows.Media Imports System.Windows.Media.Imaging Imports Microsoft.Kinect Namespace Microsoft.Samples.Kinect.BodyBasics ''' <summary> ''' Interaction logic for MainWindow ''' </summary> Public Partial Class MainWindow Inherits Window Implements INotifyPropertyChanged ''' <summary> ''' Radius of drawn hand circles ''' </summary> Private Const HandSize As Double = 30 ''' <summary> ''' Thickness of drawn joint lines ''' </summary> Private Const JointThickness As Double = 3 ''' <summary> ''' Thickness of clip edge rectangles ''' </summary> Private Const ClipBoundsThickness As Double = 10 ''' <summary> ''' Constant for clamping Z values of camera space points from being negative ''' </summary> Private Const InferredZPositionClamp As Single = 0.1F ''' <summary> ''' Brush used for drawing hands that are currently tracked as closed ''' </summary> Private ReadOnly handClosedBrush As Brush = New SolidColorBrush(Color.FromArgb(128, 255, 0, 0)) ''' <summary> ''' Brush used for drawing hands that are currently tracked as opened ''' </summary> Private ReadOnly handOpenBrush As Brush = New SolidColorBrush(Color.FromArgb(128, 0, 255, 0)) ''' <summary> ''' Brush used for drawing hands that are currently tracked as in lasso (pointer) position ''' </summary> Private ReadOnly handLassoBrush As Brush = New SolidColorBrush(Color.FromArgb(128, 0, 0, 255)) ''' <summary> ''' Brush used for drawing joints that are currently tracked ''' </summary> Private ReadOnly trackedJointBrush As Brush = New SolidColorBrush(Color.FromArgb(255, 68, 192, 68)) ''' <summary> ''' Brush used for drawing joints that are currently inferred ''' </summary> Private ReadOnly inferredJointBrush As Brush = Brushes.Yellow ''' <summary> ''' Pen used for drawing bones that are currently inferred ''' </summary> Private ReadOnly inferredBonePen As New Pen(Brushes.Gray, 1) ''' <summary> ''' Drawing group for body rendering output ''' </summary> Private drawingGroup As DrawingGroup ''' <summary> ''' Drawing image that we will display ''' </summary> Private m_imageSource As DrawingImage ''' <summary> ''' Active Kinect sensor ''' </summary> Private kinectSensor As KinectSensor = Nothing ''' <summary> ''' Coordinate mapper to map one type of point to another ''' </summary> Private coordinateMapper As CoordinateMapper = Nothing ''' <summary> ''' Reader for body frames ''' </summary> Private bodyFrameReader As BodyFrameReader = Nothing ''' <summary> ''' Array for the bodies ''' </summary> Private bodies As Body() = Nothing ''' <summary> ''' definition of bones ''' </summary> Private bones As List(Of Tuple(Of JointType, JointType)) ''' <summary> ''' Width of display (depth space) ''' </summary> Private displayWidth As Integer ''' <summary> ''' Height of display (depth space) ''' </summary> Private displayHeight As Integer ''' <summary> ''' List of colors for each body tracked ''' </summary> Private bodyColors As List(Of Pen) ''' <summary> ''' Current status text to display ''' </summary> Private m_statusText As String = Nothing ''' <summary> ''' Initializes a new instance of the MainWindow class. ''' </summary> Public Sub New() ' one sensor is currently supported Me.kinectSensor = KinectSensor.GetDefault() ' get the coordinate mapper Me.coordinateMapper = Me.kinectSensor.CoordinateMapper ' get the depth (display) extents Dim frameDescription As FrameDescription = Me.kinectSensor.DepthFrameSource.FrameDescription ' get size of joint space Me.displayWidth = frameDescription.Width Me.displayHeight = frameDescription.Height ' open the reader for the body frames Me.bodyFrameReader = Me.kinectSensor.BodyFrameSource.OpenReader() ' a bone defined as a line between two joints Me.bones = New List(Of Tuple(Of JointType, JointType))() ' Torso Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.Head, JointType.Neck)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.Neck, JointType.SpineShoulder)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.SpineShoulder, JointType.SpineMid)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.SpineMid, JointType.SpineBase)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.SpineShoulder, JointType.ShoulderRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.SpineShoulder, JointType.ShoulderLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.SpineBase, JointType.HipRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.SpineBase, JointType.HipLeft)) ' Right Arm Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.ShoulderRight, JointType.ElbowRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.ElbowRight, JointType.WristRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.WristRight, JointType.HandRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.HandRight, JointType.HandTipRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.WristRight, JointType.ThumbRight)) ' Left Arm Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.ShoulderLeft, JointType.ElbowLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.ElbowLeft, JointType.WristLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.WristLeft, JointType.HandLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.HandLeft, JointType.HandTipLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.WristLeft, JointType.ThumbLeft)) ' Right Leg Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.HipRight, JointType.KneeRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.KneeRight, JointType.AnkleRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.AnkleRight, JointType.FootRight)) ' Left Leg Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.HipLeft, JointType.KneeLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.KneeLeft, JointType.AnkleLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.AnkleLeft, JointType.FootLeft)) ' populate body colors, one for each BodyIndex Me.bodyColors = New List(Of Pen)() Me.bodyColors.Add(New Pen(Brushes.Red, 6)) Me.bodyColors.Add(New Pen(Brushes.Orange, 6)) Me.bodyColors.Add(New Pen(Brushes.Green, 6)) Me.bodyColors.Add(New Pen(Brushes.Blue, 6)) Me.bodyColors.Add(New Pen(Brushes.Indigo, 6)) Me.bodyColors.Add(New Pen(Brushes.Violet, 6)) ' set IsAvailableChanged event notifier AddHandler Me.kinectSensor.IsAvailableChanged, AddressOf Me.Sensor_IsAvailableChanged ' open the sensor Me.kinectSensor.Open() ' set the status text Me.StatusText = If(Me.kinectSensor.IsAvailable, Properties.Resources.RunningStatusText, Properties.Resources.NoSensorStatusText) ' Create the drawing group we'll use for drawing Me.drawingGroup = New DrawingGroup() ' Create an image source that we can use in our image control Me.m_imageSource = New DrawingImage(Me.drawingGroup) ' use the window object as the view model in this simple example Me.DataContext = Me ' initialize the components (controls) of the window Me.InitializeComponent() End Sub ''' <summary> ''' INotifyPropertyChangedPropertyChanged event to allow window controls to bind to changeable data ''' </summary> Public Event PropertyChanged As PropertyChangedEventHandler ''' <summary> ''' Gets the bitmap to display ''' </summary> Public ReadOnly Property ImageSource() As ImageSource Get Return Me.m_imageSource End Get End Property ''' <summary> ''' Gets or sets the current status text to display ''' </summary> Public Property StatusText() As String Get Return Me.m_statusText End Get Set If Me.m_statusText <> value Then Me.m_statusText = value ' notify any bound elements that the text has changed RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("StatusText")) End If End Set End Property ''' <summary> ''' Execute start up tasks ''' </summary> ''' <param name="sender">object sending the event</param> ''' <param name="e">event arguments</param> Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) If Me.bodyFrameReader IsNot Nothing Then AddHandler Me.bodyFrameReader.FrameArrived, AddressOf Me.Reader_FrameArrived End If End Sub ''' <summary> ''' Execute shutdown tasks ''' </summary> ''' <param name="sender">object sending the event</param> ''' <param name="e">event arguments</param> Private Sub MainWindow_Closing(sender As Object, e As CancelEventArgs) If Me.bodyFrameReader IsNot Nothing Then ' BodyFrameReader is IDisposable Me.bodyFrameReader.Dispose() Me.bodyFrameReader = Nothing End If If Me.kinectSensor IsNot Nothing Then Me.kinectSensor.Close() Me.kinectSensor = Nothing End If End Sub ''' <summary> ''' Handles the body frame data arriving from the sensor ''' </summary> ''' <param name="sender">object sending the event</param> ''' <param name="e">event arguments</param> Private Sub Reader_FrameArrived(sender As Object, e As BodyFrameArrivedEventArgs) Dim dataReceived As Boolean = False Using bodyFrame As BodyFrame = e.FrameReference.AcquireFrame() If bodyFrame IsNot Nothing Then If Me.bodies Is Nothing Then Me.bodies = New Body(bodyFrame.BodyCount - 1) {} End If ' The first time GetAndRefreshBodyData is called, Kinect will allocate each Body in the array. ' As long as those body objects are not disposed and not set to null in the array, ' those body objects will be re-used. bodyFrame.GetAndRefreshBodyData(Me.bodies) dataReceived = True End If End Using If dataReceived Then Using dc As DrawingContext = Me.drawingGroup.Open() ' Draw a transparent background to set the render size dc.DrawRectangle(Brushes.Black, Nothing, New Rect(0.0, 0.0, Me.displayWidth, Me.displayHeight)) Dim penIndex As Integer = 0 For Each body As Body In Me.bodies Dim drawPen As Pen = Me.bodyColors(System.Math.Max(System.Threading.Interlocked.Increment(penIndex),penIndex - 1)) If body.IsTracked Then Me.DrawClippedEdges(body, dc) Dim joints As IReadOnlyDictionary(Of JointType, Joint) = body.Joints ' convert the joint points to depth (display) space Dim jointPoints As New Dictionary(Of JointType, Point)() For Each jointType__1 As JointType In joints.Keys ' sometimes the depth(Z) of an inferred joint may show as negative ' clamp down to 0.1f to prevent coordinatemapper from returning (-Infinity, -Infinity) Dim position As CameraSpacePoint = joints(jointType__1).Position If position.Z < 0 Then position.Z = InferredZPositionClamp End If Dim depthSpacePoint As DepthSpacePoint = Me.coordinateMapper.MapCameraPointToDepthSpace(position) jointPoints(jointType__1) = New Point(depthSpacePoint.X, depthSpacePoint.Y) Next Me.DrawBody(joints, jointPoints, dc, drawPen) Me.DrawHand(body.HandLeftState, jointPoints(JointType.HandLeft), dc) Me.DrawHand(body.HandRightState, jointPoints(JointType.HandRight), dc) End If Next ' prevent drawing outside of our render area Me.drawingGroup.ClipGeometry = New RectangleGeometry(New Rect(0.0, 0.0, Me.displayWidth, Me.displayHeight)) End Using End If End Sub ''' <summary> ''' Draws a body ''' </summary> ''' <param name="joints">joints to draw</param> ''' <param name="jointPoints">translated positions of joints to draw</param> ''' <param name="drawingContext">drawing context to draw to</param> ''' <param name="drawingPen">specifies color to draw a specific body</param> Private Sub DrawBody(joints As IReadOnlyDictionary(Of JointType, Joint), jointPoints As IDictionary(Of JointType, Point), drawingContext As DrawingContext, drawingPen As Pen) ' Draw the bones For Each bone As var In Me.bones Me.DrawBone(joints, jointPoints, bone.Item1, bone.Item2, drawingContext, drawingPen) Next ' Draw the joints For Each jointType As JointType In joints.Keys Dim drawBrush As Brush = Nothing Dim trackingState__1 As TrackingState = joints(jointType).TrackingState If trackingState__1 = TrackingState.Tracked Then drawBrush = Me.trackedJointBrush ElseIf trackingState__1 = TrackingState.Inferred Then drawBrush = Me.inferredJointBrush End If If drawBrush IsNot Nothing Then drawingContext.DrawEllipse(drawBrush, Nothing, jointPoints(jointType), JointThickness, JointThickness) End If Next End Sub ''' <summary> ''' Draws one bone of a body (joint to joint) ''' </summary> ''' <param name="joints">joints to draw</param> ''' <param name="jointPoints">translated positions of joints to draw</param> ''' <param name="jointType0">first joint of bone to draw</param> ''' <param name="jointType1">second joint of bone to draw</param> ''' <param name="drawingContext">drawing context to draw to</param> ''' /// <param name="drawingPen">specifies color to draw a specific bone</param> Private Sub DrawBone(joints As IReadOnlyDictionary(Of JointType, Joint), jointPoints As IDictionary(Of JointType, Point), jointType0 As JointType, jointType1 As JointType, drawingContext As DrawingContext, drawingPen As Pen) Dim joint0 As Joint = joints(jointType0) Dim joint1 As Joint = joints(jointType1) ' If we can't find either of these joints, exit If joint0.TrackingState = TrackingState.NotTracked OrElse joint1.TrackingState = TrackingState.NotTracked Then Return End If ' We assume all drawn bones are inferred unless BOTH joints are tracked Dim drawPen As Pen = Me.inferredBonePen If (joint0.TrackingState = TrackingState.Tracked) AndAlso (joint1.TrackingState = TrackingState.Tracked) Then drawPen = drawingPen End If drawingContext.DrawLine(drawPen, jointPoints(jointType0), jointPoints(jointType1)) End Sub ''' <summary> ''' Draws a hand symbol if the hand is tracked: red circle = closed, green circle = opened; blue circle = lasso ''' </summary> ''' <param name="handState">state of the hand</param> ''' <param name="handPosition">position of the hand</param> ''' <param name="drawingContext">drawing context to draw to</param> Private Sub DrawHand(handState__1 As HandState, handPosition As Point, drawingContext As DrawingContext) Select Case handState__1 Case HandState.Closed drawingContext.DrawEllipse(Me.handClosedBrush, Nothing, handPosition, HandSize, HandSize) Exit Select Case HandState.Open drawingContext.DrawEllipse(Me.handOpenBrush, Nothing, handPosition, HandSize, HandSize) Exit Select Case HandState.Lasso drawingContext.DrawEllipse(Me.handLassoBrush, Nothing, handPosition, HandSize, HandSize) Exit Select End Select End Sub ''' <summary> ''' Draws indicators to show which edges are clipping body data ''' </summary> ''' <param name="body">body to draw clipping information for</param> ''' <param name="drawingContext">drawing context to draw to</param> Private Sub DrawClippedEdges(body As Body, drawingContext As DrawingContext) Dim clippedEdges As FrameEdges = body.ClippedEdges If clippedEdges.HasFlag(FrameEdges.Bottom) Then drawingContext.DrawRectangle(Brushes.Red, Nothing, New Rect(0, Me.displayHeight - ClipBoundsThickness, Me.displayWidth, ClipBoundsThickness)) End If If clippedEdges.HasFlag(FrameEdges.Top) Then drawingContext.DrawRectangle(Brushes.Red, Nothing, New Rect(0, 0, Me.displayWidth, ClipBoundsThickness)) End If If clippedEdges.HasFlag(FrameEdges.Left) Then drawingContext.DrawRectangle(Brushes.Red, Nothing, New Rect(0, 0, ClipBoundsThickness, Me.displayHeight)) End If If clippedEdges.HasFlag(FrameEdges.Right) Then drawingContext.DrawRectangle(Brushes.Red, Nothing, New Rect(Me.displayWidth - ClipBoundsThickness, 0, ClipBoundsThickness, Me.displayHeight)) End If End Sub ''' <summary> ''' Handles the event which the sensor becomes unavailable (E.g. paused, closed, unplugged). ''' </summary> ''' <param name="sender">object sending the event</param> ''' <param name="e">event arguments</param> Private Sub Sensor_IsAvailableChanged(sender As Object, e As IsAvailableChangedEventArgs) ' on failure, set the status text Me.StatusText = If(Me.kinectSensor.IsAvailable, Properties.Resources.RunningStatusText, Properties.Resources.SensorNotAvailableStatusText) End Sub End Class End Namespace '======================================================= 'Service provided by Telerik (www.telerik.com) 'Conversion powered by NRefactory. 'Twitter: @telerik 'Facebook: facebook.com/telerik '=======================================================
'------------------------------------------------------------------------------ ' <copyright file="MainWindow.xaml.cs" company="Microsoft"> ' Copyright (c) Microsoft Corporation. All rights reserved. ' </copyright> '------------------------------------------------------------------------------ Imports System.Collections.Generic Imports System.ComponentModel Imports System.Diagnostics Imports System.Globalization Imports System.IO Imports System.Windows Imports System.Windows.Media Imports System.Windows.Media.Imaging Imports Microsoft.Kinect Namespace Microsoft.Samples.Kinect.BodyBasics ''' <summary> ''' Interaction logic for MainWindow ''' </summary> Public Partial Class MainWindow Inherits Window Implements INotifyPropertyChanged ''' <summary> ''' Radius of drawn hand circles ''' </summary> Private Const HandSize As Double = 30 ''' <summary> ''' Thickness of drawn joint lines ''' </summary> Private Const JointThickness As Double = 3 ''' <summary> ''' Thickness of clip edge rectangles ''' </summary> Private Const ClipBoundsThickness As Double = 10 ''' <summary> ''' Constant for clamping Z values of camera space points from being negative ''' </summary> Private Const InferredZPositionClamp As Single = 0.1F ''' <summary> ''' Brush used for drawing hands that are currently tracked as closed ''' </summary> Private ReadOnly handClosedBrush As Brush = New SolidColorBrush(Color.FromArgb(128, 255, 0, 0)) ''' <summary> ''' Brush used for drawing hands that are currently tracked as opened ''' </summary> Private ReadOnly handOpenBrush As Brush = New SolidColorBrush(Color.FromArgb(128, 0, 255, 0)) ''' <summary> ''' Brush used for drawing hands that are currently tracked as in lasso (pointer) position ''' </summary> Private ReadOnly handLassoBrush As Brush = New SolidColorBrush(Color.FromArgb(128, 0, 0, 255)) ''' <summary> ''' Brush used for drawing joints that are currently tracked ''' </summary> Private ReadOnly trackedJointBrush As Brush = New SolidColorBrush(Color.FromArgb(255, 68, 192, 68)) ''' <summary> ''' Brush used for drawing joints that are currently inferred ''' </summary> Private ReadOnly inferredJointBrush As Brush = Brushes.Yellow ''' <summary> ''' Pen used for drawing bones that are currently inferred ''' </summary> Private ReadOnly inferredBonePen As New Pen(Brushes.Gray, 1) ''' <summary> ''' Drawing group for body rendering output ''' </summary> Private drawingGroup As DrawingGroup ''' <summary> ''' Drawing image that we will display ''' </summary> Private m_imageSource As DrawingImage ''' <summary> ''' Active Kinect sensor ''' </summary> Private kinectSensor As KinectSensor = Nothing ''' <summary> ''' Coordinate mapper to map one type of point to another ''' </summary> Private coordinateMapper As CoordinateMapper = Nothing ''' <summary> ''' Reader for body frames ''' </summary> Private bodyFrameReader As BodyFrameReader = Nothing ''' <summary> ''' Array for the bodies ''' </summary> Private bodies As Body() = Nothing ''' <summary> ''' definition of bones ''' </summary> Private bones As List(Of Tuple(Of JointType, JointType)) ''' <summary> ''' Width of display (depth space) ''' </summary> Private displayWidth As Integer ''' <summary> ''' Height of display (depth space) ''' </summary> Private displayHeight As Integer ''' <summary> ''' List of colors for each body tracked ''' </summary> Private bodyColors As List(Of Pen) ''' <summary> ''' Current status text to display ''' </summary> Private m_statusText As String = Nothing ''' <summary> ''' Initializes a new instance of the MainWindow class. ''' </summary> Public Sub New() ' one sensor is currently supported Me.kinectSensor = KinectSensor.GetDefault() ' get the coordinate mapper Me.coordinateMapper = Me.kinectSensor.CoordinateMapper ' get the depth (display) extents Dim frameDescription As FrameDescription = Me.kinectSensor.DepthFrameSource.FrameDescription ' get size of joint space Me.displayWidth = frameDescription.Width Me.displayHeight = frameDescription.Height ' open the reader for the body frames Me.bodyFrameReader = Me.kinectSensor.BodyFrameSource.OpenReader() ' a bone defined as a line between two joints Me.bones = New List(Of Tuple(Of JointType, JointType))() ' Torso Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.Head, JointType.Neck)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.Neck, JointType.SpineShoulder)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.SpineShoulder, JointType.SpineMid)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.SpineMid, JointType.SpineBase)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.SpineShoulder, JointType.ShoulderRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.SpineShoulder, JointType.ShoulderLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.SpineBase, JointType.HipRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.SpineBase, JointType.HipLeft)) ' Right Arm Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.ShoulderRight, JointType.ElbowRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.ElbowRight, JointType.WristRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.WristRight, JointType.HandRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.HandRight, JointType.HandTipRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.WristRight, JointType.ThumbRight)) ' Left Arm Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.ShoulderLeft, JointType.ElbowLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.ElbowLeft, JointType.WristLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.WristLeft, JointType.HandLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.HandLeft, JointType.HandTipLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.WristLeft, JointType.ThumbLeft)) ' Right Leg Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.HipRight, JointType.KneeRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.KneeRight, JointType.AnkleRight)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.AnkleRight, JointType.FootRight)) ' Left Leg Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.HipLeft, JointType.KneeLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.KneeLeft, JointType.AnkleLeft)) Me.bones.Add(New Tuple(Of JointType, JointType)(JointType.AnkleLeft, JointType.FootLeft)) ' populate body colors, one for each BodyIndex Me.bodyColors = New List(Of Pen)() Me.bodyColors.Add(New Pen(Brushes.Red, 6)) Me.bodyColors.Add(New Pen(Brushes.Orange, 6)) Me.bodyColors.Add(New Pen(Brushes.Green, 6)) Me.bodyColors.Add(New Pen(Brushes.Blue, 6)) Me.bodyColors.Add(New Pen(Brushes.Indigo, 6)) Me.bodyColors.Add(New Pen(Brushes.Violet, 6)) ' set IsAvailableChanged event notifier AddHandler Me.kinectSensor.IsAvailableChanged, AddressOf Me.Sensor_IsAvailableChanged ' open the sensor Me.kinectSensor.Open() ' set the status text Me.StatusText = If(Me.kinectSensor.IsAvailable, Properties.Resources.RunningStatusText, Properties.Resources.NoSensorStatusText) ' Create the drawing group we'll use for drawing Me.drawingGroup = New DrawingGroup() ' Create an image source that we can use in our image control Me.m_imageSource = New DrawingImage(Me.drawingGroup) ' use the window object as the view model in this simple example Me.DataContext = Me ' initialize the components (controls) of the window Me.InitializeComponent() End Sub ''' <summary> ''' INotifyPropertyChangedPropertyChanged event to allow window controls to bind to changeable data ''' </summary> Public Event PropertyChanged As PropertyChangedEventHandler ''' <summary> ''' Gets the bitmap to display ''' </summary> Public ReadOnly Property ImageSource() As ImageSource Get Return Me.m_imageSource End Get End Property ''' <summary> ''' Gets or sets the current status text to display ''' </summary> Public Property StatusText() As String Get Return Me.m_statusText End Get Set If Me.m_statusText <> value Then Me.m_statusText = value ' notify any bound elements that the text has changed RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("StatusText")) End If End Set End Property ''' <summary> ''' Execute start up tasks ''' </summary> ''' <param name="sender">object sending the event</param> ''' <param name="e">event arguments</param> Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) If Me.bodyFrameReader IsNot Nothing Then AddHandler Me.bodyFrameReader.FrameArrived, AddressOf Me.Reader_FrameArrived End If End Sub ''' <summary> ''' Execute shutdown tasks ''' </summary> ''' <param name="sender">object sending the event</param> ''' <param name="e">event arguments</param> Private Sub MainWindow_Closing(sender As Object, e As CancelEventArgs) If Me.bodyFrameReader IsNot Nothing Then ' BodyFrameReader is IDisposable Me.bodyFrameReader.Dispose() Me.bodyFrameReader = Nothing End If If Me.kinectSensor IsNot Nothing Then Me.kinectSensor.Close() Me.kinectSensor = Nothing End If End Sub ''' <summary> ''' Handles the body frame data arriving from the sensor ''' </summary> ''' <param name="sender">object sending the event</param> ''' <param name="e">event arguments</param> Private Sub Reader_FrameArrived(sender As Object, e As BodyFrameArrivedEventArgs) Dim dataReceived As Boolean = False Using bodyFrame As BodyFrame = e.FrameReference.AcquireFrame() If bodyFrame IsNot Nothing Then If Me.bodies Is Nothing Then Me.bodies = New Body(bodyFrame.BodyCount - 1) {} End If ' The first time GetAndRefreshBodyData is called, Kinect will allocate each Body in the array. ' As long as those body objects are not disposed and not set to null in the array, ' those body objects will be re-used. bodyFrame.GetAndRefreshBodyData(Me.bodies) dataReceived = True End If End Using If dataReceived Then Using dc As DrawingContext = Me.drawingGroup.Open() ' Draw a transparent background to set the render size dc.DrawRectangle(Brushes.Black, Nothing, New Rect(0.0, 0.0, Me.displayWidth, Me.displayHeight)) Dim penIndex As Integer = 0 For Each body As Body In Me.bodies Dim drawPen As Pen = Me.bodyColors(System.Math.Max(System.Threading.Interlocked.Increment(penIndex),penIndex - 1)) If body.IsTracked Then Me.DrawClippedEdges(body, dc) Dim joints As IReadOnlyDictionary(Of JointType, Joint) = body.Joints ' convert the joint points to depth (display) space Dim jointPoints As New Dictionary(Of JointType, Point)() For Each jointType__1 As JointType In joints.Keys ' sometimes the depth(Z) of an inferred joint may show as negative ' clamp down to 0.1f to prevent coordinatemapper from returning (-Infinity, -Infinity) Dim position As CameraSpacePoint = joints(jointType__1).Position If position.Z < 0 Then position.Z = InferredZPositionClamp End If Dim depthSpacePoint As DepthSpacePoint = Me.coordinateMapper.MapCameraPointToDepthSpace(position) jointPoints(jointType__1) = New Point(depthSpacePoint.X, depthSpacePoint.Y) Next Me.DrawBody(joints, jointPoints, dc, drawPen) Me.DrawHand(body.HandLeftState, jointPoints(JointType.HandLeft), dc) Me.DrawHand(body.HandRightState, jointPoints(JointType.HandRight), dc) End If Next ' prevent drawing outside of our render area Me.drawingGroup.ClipGeometry = New RectangleGeometry(New Rect(0.0, 0.0, Me.displayWidth, Me.displayHeight)) End Using End If End Sub ''' <summary> ''' Draws a body ''' </summary> ''' <param name="joints">joints to draw</param> ''' <param name="jointPoints">translated positions of joints to draw</param> ''' <param name="drawingContext">drawing context to draw to</param> ''' <param name="drawingPen">specifies color to draw a specific body</param> Private Sub DrawBody(joints As IReadOnlyDictionary(Of JointType, Joint), jointPoints As IDictionary(Of JointType, Point), drawingContext As DrawingContext, drawingPen As Pen) ' Draw the bones For Each bone As var In Me.bones Me.DrawBone(joints, jointPoints, bone.Item1, bone.Item2, drawingContext, drawingPen) Next ' Draw the joints For Each jointType As JointType In joints.Keys Dim drawBrush As Brush = Nothing Dim trackingState__1 As TrackingState = joints(jointType).TrackingState If trackingState__1 = TrackingState.Tracked Then drawBrush = Me.trackedJointBrush ElseIf trackingState__1 = TrackingState.Inferred Then drawBrush = Me.inferredJointBrush End If If drawBrush IsNot Nothing Then drawingContext.DrawEllipse(drawBrush, Nothing, jointPoints(jointType), JointThickness, JointThickness) End If Next End Sub ''' <summary> ''' Draws one bone of a body (joint to joint) ''' </summary> ''' <param name="joints">joints to draw</param> ''' <param name="jointPoints">translated positions of joints to draw</param> ''' <param name="jointType0">first joint of bone to draw</param> ''' <param name="jointType1">second joint of bone to draw</param> ''' <param name="drawingContext">drawing context to draw to</param> ''' /// <param name="drawingPen">specifies color to draw a specific bone</param> Private Sub DrawBone(joints As IReadOnlyDictionary(Of JointType, Joint), jointPoints As IDictionary(Of JointType, Point), jointType0 As JointType, jointType1 As JointType, drawingContext As DrawingContext, drawingPen As Pen) Dim joint0 As Joint = joints(jointType0) Dim joint1 As Joint = joints(jointType1) ' If we can't find either of these joints, exit If joint0.TrackingState = TrackingState.NotTracked OrElse joint1.TrackingState = TrackingState.NotTracked Then Return End If ' We assume all drawn bones are inferred unless BOTH joints are tracked Dim drawPen As Pen = Me.inferredBonePen If (joint0.TrackingState = TrackingState.Tracked) AndAlso (joint1.TrackingState = TrackingState.Tracked) Then drawPen = drawingPen End If drawingContext.DrawLine(drawPen, jointPoints(jointType0), jointPoints(jointType1)) End Sub ''' <summary> ''' Draws a hand symbol if the hand is tracked: red circle = closed, green circle = opened; blue circle = lasso ''' </summary> ''' <param name="handState">state of the hand</param> ''' <param name="handPosition">position of the hand</param> ''' <param name="drawingContext">drawing context to draw to</param> Private Sub DrawHand(handState__1 As HandState, handPosition As Point, drawingContext As DrawingContext) Select Case handState__1 Case HandState.Closed drawingContext.DrawEllipse(Me.handClosedBrush, Nothing, handPosition, HandSize, HandSize) Exit Select Case HandState.Open drawingContext.DrawEllipse(Me.handOpenBrush, Nothing, handPosition, HandSize, HandSize) Exit Select Case HandState.Lasso drawingContext.DrawEllipse(Me.handLassoBrush, Nothing, handPosition, HandSize, HandSize) Exit Select End Select End Sub ''' <summary> ''' Draws indicators to show which edges are clipping body data ''' </summary> ''' <param name="body">body to draw clipping information for</param> ''' <param name="drawingContext">drawing context to draw to</param> Private Sub DrawClippedEdges(body As Body, drawingContext As DrawingContext) Dim clippedEdges As FrameEdges = body.ClippedEdges If clippedEdges.HasFlag(FrameEdges.Bottom) Then drawingContext.DrawRectangle(Brushes.Red, Nothing, New Rect(0, Me.displayHeight - ClipBoundsThickness, Me.displayWidth, ClipBoundsThickness)) End If If clippedEdges.HasFlag(FrameEdges.Top) Then drawingContext.DrawRectangle(Brushes.Red, Nothing, New Rect(0, 0, Me.displayWidth, ClipBoundsThickness)) End If If clippedEdges.HasFlag(FrameEdges.Left) Then drawingContext.DrawRectangle(Brushes.Red, Nothing, New Rect(0, 0, ClipBoundsThickness, Me.displayHeight)) End If If clippedEdges.HasFlag(FrameEdges.Right) Then drawingContext.DrawRectangle(Brushes.Red, Nothing, New Rect(Me.displayWidth - ClipBoundsThickness, 0, ClipBoundsThickness, Me.displayHeight)) End If End Sub ''' <summary> ''' Handles the event which the sensor becomes unavailable (E.g. paused, closed, unplugged). ''' </summary> ''' <param name="sender">object sending the event</param> ''' <param name="e">event arguments</param> Private Sub Sensor_IsAvailableChanged(sender As Object, e As IsAvailableChangedEventArgs) ' on failure, set the status text Me.StatusText = If(Me.kinectSensor.IsAvailable, Properties.Resources.RunningStatusText, Properties.Resources.SensorNotAvailableStatusText) End Sub End Class End Namespace