public SkeletonBone(SkeletonNode BeginNode, SkeletonNode EndNode)
      {
         BeginX = BeginNode.X;
         BeginY = BeginNode.Y;
         BeginZ = BeginNode.Z;
         EndX = EndNode.X;
         EndY = EndNode.Y;
         EndZ = EndNode.Z;

      }
      public NodesNormalizer(List<SkeletonNode> nodes)
      {
         Nodes = nodes.Where(n => AVAILABLE_NODES.Contains(n.NodeType));
         SkeletonNode Crotch = Nodes.First(n => n.NodeType == 0);

         NormalizedNodes = new List<SkeletonNode>();
         foreach (SkeletonNode Node in Nodes)
         {
            SkeletonNode NormalizedNode = new SkeletonNode();
            NormalizedNode.NodeType = Node.NodeType;
            NormalizedNode.X = Node.X - Crotch.X;
            NormalizedNode.Y = Crotch.Y - Node.Y;
            NormalizedNode.Z = Node.Z - Crotch.Z;
            NormalizedNodes.Add(NormalizedNode);
         }
      }
        void timer_Tick(object sender, EventArgs e)
        {
            if (OpenedFrames != null)
            {
                var frame = OpenedFrames[index];
                var frame_old = OpenedFrames[index-1];

                double x = frame.X - frame_old.X;
                double y = frame.Y - frame_old.Y;

                canvas1.Children.Clear();
                canvas2.Children.Clear();
                Ellipse el = new Ellipse();
                el.Width = 10;
                el.Height = 10;
                el.Fill = new SolidColorBrush(Colors.Green);
                Canvas.SetLeft(el, off_x +   x / c_max);
                Canvas.SetTop(el, off_y +   y / c_max);
                el.InvalidateVisual();
                canvas1.Children.Add(el);

                int center = (int)JointType.HipCenter;
                var hip = frame.Nodes.FirstOrDefault(o=>o.NodeType==center);


                //frame.Nodes.Where(o=>legs_int.Contains(o.NodeType)).Zip(frame_old.Nodes.Where(o=>legs_int.Contains(o.NodeType)), (e1, e2) =>
                //{

                //    SkeletonNode n = new SkeletonNode();

                //    double deg = Math.PI / 2;

                //    n.X = (e1.X + e1.Z * Math.Tan(deg)) / Math.Sqrt(1 + Math.Tan(deg) * Math.Tan(deg));
                //    n.Y = e1.Y ;


                //    Point a = new Point();
                //    a.X = e1.X - x;
                //    a.Y = e1.Y - y;
                //    //a.X = e1.X - x - hip.X;
                //    //a.Y = e1.Y - y - hip.Y;
                //    //rotation

                //    a.X = n.X - x;
                //    a.Y = n.Y - y;

                //    SolidColorBrush b = new SolidColorBrush(Colors.Black);
                //    if(leg_right.Contains(e1.NodeType))
                //        b = new SolidColorBrush(Colors.Red);
                //    else
                //        b = new SolidColorBrush(Colors.Blue);
                //    return new { X=a.X,Y=a.Y,br=b };
                //}).ToList().ForEach(o =>
                //{
                //    el = new Ellipse();
                //    el.Width = 10;
                //    el.Height = 10;
                //    el.Fill = o.br;
                //    Canvas.SetLeft(el, off_x +  o.X * 100);
                //    Canvas.SetTop(el, off_y -  o.Y * 100);
                //    canvas1.Children.Add(el);
                    
                //});


                frame.Nodes.Where(o => legs_int.Contains(o.NodeType)).ToList().ForEach(o =>
                {
                    SkeletonNode n = new SkeletonNode();
                    SkeletonNode nhip = new SkeletonNode();

                    double deg =  Math.PI / 2;

                    n.X = (o.X + o.Z * Math.Tan(deg)) / Math.Sqrt(1 + Math.Tan(deg) * Math.Tan(deg));
                    n.Y = o.Y;


                    nhip.X = (hip.X + hip.Z * Math.Tan(deg)) / Math.Sqrt(1 + Math.Tan(deg) * Math.Tan(deg));
                    nhip.Y = hip.Y;


                    Point a = new Point();
                    Point a1 = new Point();
                    a.X = o.X - x;
                    a.Y = o.Y - y;
                    a.X = o.X  - hip.X;
                    a.Y = o.Y  - hip.Y;
                    //rotation


                    a1.X = n.X  -nhip.X;
                    a1.Y = n.Y  - nhip.Y;

                    SolidColorBrush b = new SolidColorBrush(Colors.Black);
                   
                    if (leg_right.Contains(o.NodeType))
                        b = new SolidColorBrush(Colors.Red);
                    else
                        b = new SolidColorBrush(Colors.Blue);

                    el = new Ellipse();
                    el.Width = 10;
                    el.Height = 10;
                    el.Fill = b;
                    Canvas.SetLeft(el, off_x + a.X * 100);
                    Canvas.SetTop(el, off_y - a.Y * 100);
                    canvas1.Children.Add(el);

                    el = new Ellipse();
                    el.Width = 10;
                    el.Height = 10;
                    el.Fill = b;
                    Canvas.SetLeft(el, off_x + a1.X * 100);
                    Canvas.SetTop(el, off_y - a1.Y * 100);
                    canvas2.Children.Add(el);

                });

                
                
                label1.Content = index++;
                UpdateLayout();
            }
        }
        private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            if (isRecording)
            {
                DateTime Time = DateTime.Now;
                Skeleton[] skeletons = new Skeleton[0];

                float[] floor = new float[4];

                using (Microsoft.Kinect.SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
                {
                    if (skeletonFrame != null)
                    {
                        skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                        skeletonFrame.CopySkeletonDataTo(skeletons);
                        floor[0] = skeletonFrame.FloorClipPlane.Item1;
                        floor[1] = skeletonFrame.FloorClipPlane.Item2;
                        floor[2] = skeletonFrame.FloorClipPlane.Item3;
                        floor[3] = skeletonFrame.FloorClipPlane.Item4;
                    }
                }
                foreach (Skeleton skel in skeletons)
                {
                    if (skel.TrackingState == SkeletonTrackingState.Tracked)
                    {
                        KinectLib.SkeletonFrame frame = new KinectLib.SkeletonFrame();
                        frame.Date = Time;
                        frame.SkeletonId = skel.TrackingId;
                        frame.Nodes = new List<SkeletonNode>();
                        frame.X = skel.Position.X;
                        frame.Y = skel.Position.Y;
                        frame.Z = skel.Position.Z;
                        frame.Floor = floor;

                        foreach (Joint joint in skel.Joints)
                        {
                            SkeletonNode node = new SkeletonNode();
                            node.NodeType = (int)joint.JointType;
                            node.X = joint.Position.X;
                            node.Y = joint.Position.Y;
                            node.Z = joint.Position.Z;

                            frame.Nodes.Add(node);
                        }
                        Frames.Add(frame);

                        Skeleton DisplaySkeleton = skel;
                        Joint Crotch = DisplaySkeleton.Joints.First(j => j.JointType == JointType.HipCenter);
                        if (Crotch != null)
                        {
                            DrawingGroup DrawingGroup = new DrawingGroup();
                            using (DrawingContext Context = DrawingGroup.Open())
                            {
                                Double CrotchDisplayX = image1.Width / 2;
                                Double CrotchDisplayY = image1.Height / 2;

                                foreach (Joint Node in DisplaySkeleton.Joints)
                                {
                                    double X = (Node.Position.X - Crotch.Position.X) / 2.5 * image1.Width + CrotchDisplayX;
                                    double Y = (-Node.Position.Y + Crotch.Position.Y) / 2.5 * image1.Height + CrotchDisplayY;
                                    Context.DrawEllipse(Brushes.Wheat, new Pen(Brushes.Navy, 2), new Point(X, Y), 7, 7);
                                }

                            }
                            DrawingGroup.Freeze();
                            Dispatcher.Invoke(new Action(() => { image1.Source = new DrawingImage(DrawingGroup); }));
                        }
                    }
                }
            }
        }
        private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            if (isRecording)
            {
                DateTime Time = DateTime.Now;
                Skeleton[] skeletons = new Skeleton[0];

                using (Microsoft.Kinect.SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
                {
                    if (skeletonFrame != null)
                    {
                        skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                        skeletonFrame.CopySkeletonDataTo(skeletons);
                    }
                }
                foreach (Skeleton skel in skeletons)
                {
                    if (skel.TrackingState == SkeletonTrackingState.Tracked)
                    {
                        KinectLib.SkeletonFrame frame = new KinectLib.SkeletonFrame();
                        frame.Date = Time;
                        frame.SkeletonId = skel.TrackingId;
                        frame.Nodes = new List<SkeletonNode>();
                        frame.X = skel.Position.X;
                        frame.Y = skel.Position.Y;
                        frame.Z = skel.Position.Z;

                        foreach (Joint joint in skel.Joints)
                        {
                            SkeletonNode node = new SkeletonNode();
                            node.NodeType = (int)joint.JointType;
                            node.X = joint.Position.X;
                            node.Y = joint.Position.Y;
                            node.Z = joint.Position.Z;

                            frame.Nodes.Add(node);
                        }
                        Frames.Add(frame);
                    }
                }

            }
        }