Exemplo n.º 1
0
        /// <summary>
        /// Deserializes an shapes index from the given stream.
        /// </summary>
        public static ShapesArray CreateFrom(Stream stream, bool copy, out long size, bool hasElevation = false)
        {
            var initialPosition = stream.Position;

            size = 0;
            var longBytes = new byte[8];

            stream.Read(longBytes, 0, 8);
            size += 8;
            var indexLength = BitConverter.ToInt64(longBytes, 0);

            stream.Read(longBytes, 0, 8);
            size += 8;
            var coordinatesLength = BitConverter.ToInt64(longBytes, 0);

            ArrayBase <ulong> index;
            ArrayBase <float> coordinates;
            ArrayBase <short> elevation = null;

            if (copy)
            { // just create arrays and read the data.
                index = Context.ArrayFactory.CreateMemoryBackedArray <ulong>(indexLength);
                index.CopyFrom(stream);
                size       += indexLength * 8;
                coordinates = Context.ArrayFactory.CreateMemoryBackedArray <float>(coordinatesLength);
                size       += coordinatesLength * 4;
                coordinates.CopyFrom(stream);
                if (hasElevation)
                {
                    elevation = Context.ArrayFactory.CreateMemoryBackedArray <short>(coordinatesLength / 2);
                    size     += coordinatesLength;
                    elevation.CopyFrom(stream);
                }
            }
            else
            { // create accessors over the exact part of the stream that represents vertices/edges.
                var position = stream.Position;
                var map1     = new MemoryMapStream(new CappedStream(stream, position, indexLength * 8));
                index = new Array <ulong>(map1.CreateUInt64(indexLength));
                size += indexLength * 8;
                var map2 = new MemoryMapStream(new CappedStream(stream, position + indexLength * 8,
                                                                coordinatesLength * 4));
                coordinates = new Array <float>(map2.CreateSingle(coordinatesLength));
                size       += coordinatesLength * 4;
                if (hasElevation)
                {
                    var map3 = new MemoryMapStream(new CappedStream(stream, position + indexLength * 8 + coordinatesLength * 4,
                                                                    coordinatesLength));
                    elevation = new Array <short>(map3.CreateInt16(coordinatesLength / 2));
                    size     += coordinatesLength;
                }
            }

            // make stream is positioned correctly.
            stream.Seek(initialPosition + size, System.IO.SeekOrigin.Begin);

            return(new ShapesArray(index, coordinates, elevation));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Deserializes an index from the given stream.
        /// </summary>
        public static Index Deserialize(Stream stream, ArrayProfile profile = null)
        {
            var bytes = new byte[8];

            stream.Read(bytes, 0, 8);
            var size = BitConverter.ToInt64(bytes, 0);

            ArrayBase <ulong> data;

            if (profile == null)
            { // just create arrays and read the data.
                data = new MemoryArray <ulong>(size);
                data.CopyFrom(stream);
            }
            else
            { // create accessors over the exact part of the stream that represents vertices/edges.
                var position = stream.Position;
                var map1     = new MemoryMapStream(new CappedStream(stream, position, size * 8));
                data = new Array <ulong>(map1.CreateUInt64(size), profile);
            }

            return(new Index(data));
        }