/// <summary>
 /// Returns a KinectMotion of a single frame.
 /// </summary>
 /// <returns></returns>
 public KinectMotion takeSnapshot()
 {
     KinectMotion km = new KinectMotion();
     km.addKeyframe(lastPlayer.copyPlayer());
     return km;
 }
 /// <summary>
 /// Begins recording from the Kinect. Keyframes made at timeInterval specified. Anything under 150
 /// will likely not work.
 /// </summary>
 /// <param name="timeInterval"></param>
 public void startRecording(int timeInterval)
 {
     // Create the thread object. This does not start the thread.
     currentRecordingInterval = timeInterval;
     System.Threading.Thread recordingThread = new System.Threading.Thread(Record);
     lastMotion = new KinectMotion(timeInterval);
     isRecording = true;
     startTime = DateTime.Now;
     recordingThread.Start();
 }
        /// <summary>
        /// Plays back motion at speed recorded.
        /// </summary>
        /// <param name="motion"></param>
        public void playbackMotion(KinectMotion motion)
        {
            float secs = motion.seconds;
            if (secs == 0)
                secs = motion.keyframes * .2f; //200ms per keyframe

            playbackMotion(convertMotionToAngles(motion), secs);
        }
        /// <summary>
        /// Reads from XML format outlined above.
        /// </summary>
        /// <param name="root"></param>
        /// <returns></returns>
        public static KinectMotion readFromXML(XElement root)
        {
            List<XElement> dataList = root.Descendants("Data").ToList<XElement>();
            if(dataList == null)
                return null;
            XElement dataElement = dataList[0];

            KinectMotion motion = new KinectMotion(int.Parse(dataElement.Attribute("Interval").Value),
                float.Parse(dataElement.Attribute("Length").Value), int.Parse(dataElement.Attribute("Keyframes").Value));

            IEnumerable<XElement> frames = dataElement.Descendants("Frame");

            Player newFrame;
            foreach (XElement frame in frames)
            {
                newFrame = new Player();

                foreach (XElement joint in frame.Descendants("JointData")){
                    JointAngles jangles = KinectMotion.parseJoint(joint);

                    XElement nameNode = joint.Descendants("Name").ToList<XElement>()[0];

                    switch (nameNode.Value){
                        case "Head":
                            jangles.name = "Head";
                            newFrame.head = jangles;
                            break;
                        case "Neck":
                            jangles.name = "Neck";
                            newFrame.neck = jangles;
                            break;
                        case "Spine":
                            jangles.name = "Spine";
                            newFrame.spine = jangles;
                            break;
                        case "Left Shoulder":
                            jangles.name = "Left Shoulder";
                            newFrame.lShoulder = jangles;
                            break;
                        case "Right Shoulder":
                            jangles.name = "Right Shoulder";
                            newFrame.rShoulder = jangles;
                            break;
                        case "Left Elbow":
                            jangles.name = "Left Elbow";
                            newFrame.lElbow = jangles;
                            break;
                        case "Right Elbow":
                            jangles.name = "Right Elbow";
                            newFrame.rElbow = jangles;
                            break;
                        case "Right Hand":
                            jangles.name = "Right Hand";
                            newFrame.rHand = jangles;
                            break;
                        case "Left Hand":
                            jangles.name = "Left Hand";
                            newFrame.lHand = jangles;
                            break;
                        case "Right Hip":
                            jangles.name = "Right Hip";
                            newFrame.rHip = jangles;
                            break;
                        case "Left Hip":
                            jangles.name = "Left Hip";
                            newFrame.lHip = jangles;
                            break;
                        case "Center Hip":
                            jangles.name = "Center Hip";
                            newFrame.cHip = jangles;
                            break;
                        case "Left Knee":
                            jangles.name = "Left Knee";
                            newFrame.lKnee = jangles;
                            break;
                        case "Right Knee":
                            jangles.name = "Right Knee";
                            newFrame.rKnee = jangles;
                            break;
                        case "Pelvis":
                            jangles.name = "Pelvis";
                            newFrame.pelvis = jangles;
                            break;
                        case "Right Ankle":
                            jangles.name = "Right Ankle";
                            newFrame.rAnkle = jangles;
                            break;
                        case "Left Ankle":
                            jangles.name = "Left Ankle";
                            newFrame.lAnkle = jangles;
                            break;
                    }
                }

                //have to copy to keep list references?
                motion.addKeyframe(newFrame.copyPlayer());
            }

            return motion;
        }
        /// <summary>
        /// Converts motion to angles needed by Nao.
        /// </summary>
        /// <param name="motion"></param>
        /// <returns></returns>
        public List<List<float>> convertMotionToAngles(KinectMotion motion)
        {
            List<List<float>> returnAngles = new List<List<float>>();
            for (int n = 0; n < 10; n++)//10 for number of joints
            {
                returnAngles.Add(new List<float>());
            }

            foreach (Player player in motion.poses)
            {
                float[] lastPoseAngles = copyAnglesToArray(player);
                for (int i = 0; i < lastPoseAngles.Length; i++)
                {
                    returnAngles[i].Add(lastPoseAngles[i]);
                }
            }
            return returnAngles;
        }