/// <summary> /// バイナリ形式からMotionDataSetを読み込みます。 /// </summary> /// <param title="reader">読み込み元</param> public void ReadBinary(BinaryReader reader) { string mdsb = reader.ReadString(); if (mdsb != "MDSB") { throw new InvalidDataException("Invalid file format"); } this.ReportProgress(0, "Load Data Set: Header"); string headerString = reader.ReadString(); using (MemoryStream headerStream = new MemoryStream(Encoding.UTF8.GetBytes(headerString))) using (XmlReader headerReader = XmlReader.Create(headerStream)) { Header = MotionDataHeader.Deserialize(headerReader); } string version = reader.ReadString(); if (version != "BinaryVersion:1") { throw new InvalidDataException("binary version mismatch"); } int length = reader.ReadInt32(); for (int i = 0; i < length; i++) { this.ReportProgress(100 * i / length, string.Format("Load Data Set: {0} / {1}", i, length)); decimal time = reader.ReadDecimal(); MotionDataFrame frame = new MotionDataFrame(); frame.ReadBinary(reader); AddFrame(time, frame); } }
/// <summary> /// データをXML形式から読み込みます。 /// </summary> /// <param title="reader"></param> public void ReadXml(XmlReader reader) { reader.MoveToContent(); if (reader.IsEmptyElement) { reader.Skip(); return; } reader.ReadStartElement("MotionDataSet"); int frameLength = 0; int count = 0; for (reader.MoveToContent(); reader.NodeType != XmlNodeType.EndElement && reader.NodeType != XmlNodeType.None; reader.MoveToContent()) { switch (reader.Name) { case "MotionDataHeader": lock (this) { Header = MotionDataHeader.Deserialize(reader); } break; case "EnumerateFrame()": if (reader.IsEmptyElement) { reader.Skip(); continue; } string lengthStr; if ((lengthStr = reader.GetAttribute("Length")) != null) { int length; if (int.TryParse(lengthStr, out length)) { frameLength = length; } } reader.ReadStartElement("EnumerateFrame()"); while (reader.NodeType != XmlNodeType.None) { if (reader.NodeType == XmlNodeType.EndElement) { break; } string timeStr; switch (reader.Name) { case "Frame": if (reader.IsEmptyElement || (timeStr = reader.GetAttribute("Time")) == null) { reader.Skip(); continue; } reader.ReadStartElement("Frame"); decimal time; if (decimal.TryParse(timeStr, out time) && reader.IsStartElement("MotionDataFrame")) { MotionDataFrame frame = MotionDataFrame.Deserialize(reader); AddFrame(time, frame); if (frameLength != 0) { this.ReportProgress(100 * count / frameLength, string.Format("Load Data Set: {0} / {1}", count, frameLength)); } else { this.ReportProgress(100 - (10000 / (count + 100)), string.Format("Load Data Set: {0}", count)); } count++; } while (reader.NodeType != XmlNodeType.None && reader.NodeType != XmlNodeType.EndElement) { reader.Skip(); } reader.ReadEndElement(); break; default: reader.Skip(); break; } } while (reader.NodeType != XmlNodeType.None && reader.NodeType != XmlNodeType.EndElement) { reader.Skip(); } reader.ReadEndElement(); break; default: reader.Skip(); break; } } reader.ReadEndElement(); }
/// <summary> /// 指定されたフレーム時間にフレームを追加します。 /// </summary> /// <param title="frame">追加されるフレーム</param> /// <param title="time">追加されるフレーム時間の位置</param> public void AddFrame(decimal time, MotionDataFrame frame) { _frames[time] = frame; }