/// <summary> /// Serializes all children of the given node. /// </summary> /// <param name="typeModel"></param> /// <param name="nodeBase"></param> /// <returns></returns> private Stream Serialize(RuntimeTypeModel typeModel, RTreeMemoryIndex <T> .Node nodeBase) { var stream = _streamCache.CreateNew(); if (nodeBase.Children is List <RTreeMemoryIndex <T> .Node> ) { // the node is not a leaf. long position = 0; var node = (nodeBase as RTreeMemoryIndex <T> .Node); var childrenIndex = new ChildrenIndex(); childrenIndex.IsLeaf = new bool[nodeBase.Children.Count]; childrenIndex.MinX = new float[nodeBase.Children.Count]; childrenIndex.MinY = new float[nodeBase.Children.Count]; childrenIndex.MaxX = new float[nodeBase.Children.Count]; childrenIndex.MaxY = new float[nodeBase.Children.Count]; childrenIndex.Starts = new int[nodeBase.Children.Count]; var serializedChildren = new List <Stream>(); for (int idx = 0; idx < nodeBase.Children.Count; idx++) { var child = (node.Children[idx] as RTreeMemoryIndex <T> .Node); BoxF2D box = node.Boxes[idx]; childrenIndex.MinX[idx] = (float)box.Min[0]; childrenIndex.MinY[idx] = (float)box.Min[1]; childrenIndex.MaxX[idx] = (float)box.Max[0]; childrenIndex.MaxY[idx] = (float)box.Max[1]; childrenIndex.IsLeaf[idx] = ( child.Children is List <T>); Stream childSerialized = this.Serialize(typeModel, child); serializedChildren.Add(childSerialized); childrenIndex.Starts[idx] = (int)position; position = position + childSerialized.Length; } childrenIndex.End = (int)position; // serialize this index object. var indexStream = new MemoryStream(); typeModel.Serialize(indexStream, childrenIndex); byte[] indexBytes = indexStream.ToArray(); // START WRITING THE DATA TO THE TARGET STREAM HERE! // 1: write the type of data. byte[] leafFlag = new[] { (byte)0 }; stream.Write(leafFlag, 0, 1); // 2: Write the length of the meta data. byte[] indexLength = BitConverter.GetBytes(indexBytes.Length); stream.Write(indexLength, 0, indexLength.Length); // 3: write the meta data or the node-index. stream.Write(indexBytes, 0, indexBytes.Length); // 4: write the actual children. for (int idx = 0; idx < serializedChildren.Count; idx++) { serializedChildren[idx].Seek(0, SeekOrigin.Begin); serializedChildren[idx].CopyTo(stream); _streamCache.Dispose(serializedChildren[idx]); serializedChildren[idx] = null; } } else if (nodeBase.Children is List <T> ) { // the node is a leaf node. // START WRITING THE DATA TO THE TARGET STREAM HERE! // 1: write the type of data. byte[] leafFlag = new[] { (byte)1 }; stream.Write(leafFlag, 0, 1); // 2: write the leaf data. byte[] data = this.Serialize(typeModel, nodeBase.Children as List <T>, nodeBase.Boxes); stream.Write(data, 0, data.Length); } else { throw new Exception("Unknown node type!"); } return(stream); }
private Stream Serialize(RuntimeTypeModel typeModel, RTreeMemoryIndex <T> .Node nodeBase) { Stream destination = this._streamCache.CreateNew(); if (nodeBase.Children is List <RTreeMemoryIndex <T> .Node> ) { long num = 0; RTreeMemoryIndex <T> .Node node = nodeBase; ChildrenIndex childrenIndex = new ChildrenIndex(); childrenIndex.IsLeaf = new bool[nodeBase.Children.Count]; childrenIndex.MinX = new float[nodeBase.Children.Count]; childrenIndex.MinY = new float[nodeBase.Children.Count]; childrenIndex.MaxX = new float[nodeBase.Children.Count]; childrenIndex.MaxY = new float[nodeBase.Children.Count]; childrenIndex.Starts = new int[nodeBase.Children.Count]; List <Stream> streamList = new List <Stream>(); for (int index = 0; index < nodeBase.Children.Count; ++index) { RTreeMemoryIndex <T> .Node child = node.Children[index] as RTreeMemoryIndex <T> .Node; BoxF2D box = node.Boxes[index]; childrenIndex.MinX[index] = (float)box.Min[0]; childrenIndex.MinY[index] = (float)box.Min[1]; childrenIndex.MaxX[index] = (float)box.Max[0]; childrenIndex.MaxY[index] = (float)box.Max[1]; childrenIndex.IsLeaf[index] = child.Children is List <T>; Stream stream = this.Serialize(typeModel, child); streamList.Add(stream); childrenIndex.Starts[index] = (int)num; num += stream.Length; } childrenIndex.End = (int)num; MemoryStream memoryStream = new MemoryStream(); ((TypeModel)typeModel).Serialize((Stream)memoryStream, (object)childrenIndex); byte[] array = memoryStream.ToArray(); byte[] buffer = new byte[1]; destination.Write(buffer, 0, 1); byte[] bytes = BitConverter.GetBytes(array.Length); destination.Write(bytes, 0, bytes.Length); destination.Write(array, 0, array.Length); for (int index = 0; index < streamList.Count; ++index) { streamList[index].Seek(0L, SeekOrigin.Begin); streamList[index].CopyTo(destination); this._streamCache.Dispose(streamList[index]); streamList[index] = (Stream)null; } } else { if (!(nodeBase.Children is List <T>)) { throw new Exception("Unknown node type!"); } byte[] buffer1 = new byte[1] { (byte)1 }; destination.Write(buffer1, 0, 1); byte[] buffer2 = this.Serialize(typeModel, nodeBase.Children as List <T>, nodeBase.Boxes); destination.Write(buffer2, 0, buffer2.Length); } return(destination); }