Пример #1
0
        // TerrainData.Clone
        public HeightMapTerrainData Clone()
        {
            HeightMapTerrainData ret = new HeightMapTerrainData(SizeX, SizeY, SizeZ);

            ret.m_heightmap = (int[, ]) this.m_heightmap.Clone();
            return(ret);
        }
        // Scan the height info we're returning and return a patch packet header for this patch.
        private static TerrainPatch.Header PrescanPatch(HeightMapTerrainData terrData, int patchX, int patchY)
        {
            TerrainPatch.Header header = new TerrainPatch.Header();
            float zmax = -99999999.0f;
            float zmin = 99999999.0f;

            for (int j = patchY * Constants.TerrainPatchSize; j < (patchY + 1) * Constants.TerrainPatchSize; j++)
            {
                for (int i = patchX * Constants.TerrainPatchSize; i < (patchX + 1) * Constants.TerrainPatchSize; i++)
                {
                    float val = terrData[i, j];
                    if (val > zmax)
                    {
                        zmax = val;
                    }
                    if (val < zmin)
                    {
                        zmin = val;
                    }
                }
            }

            header.DCOffset = zmin;
            header.Range    = (int)((zmax - zmin) + 1.0f);

            return(header);
        }
        /// <summary>
        ///     Add a patch of terrain to a BitPacker
        /// </summary>
        /// <param name="output">BitPacker to write the patch to</param>
        /// <param name="heightmap">
        ///     Heightmap of the simulator. Presumed to be an sizeX*sizeY array.
        /// </param>
        /// <param name="patchX">
        ///     X offset of the patch to create.
        /// </param>
        /// <param name="patchY">
        ///     Y offset of the patch to create.
        /// </param>
        /// <param name="pRegionSizeX"></param>
        /// <param name="pRegionSizeY"></param>
        public static void CreatePatchFromHeightmap(BitPack output, HeightMapTerrainData terrData, int patchX, int patchY)
        {
            TerrainPatch.Header header = PrescanPatch(terrData, patchX, patchY);
            header.QuantWBits = 136;

            // If larger than legacy region size, pack patch X and Y info differently.
            if (terrData.SizeX > Constants.RegionSize || terrData.SizeY > Constants.RegionSize)
            {
                header.PatchIDs  = (patchY & 0xFFFF);
                header.PatchIDs += (patchX << 16);
            }
            else
            {
                header.PatchIDs  = (patchY & 0x1F);
                header.PatchIDs += (patchX << 5);
            }

            // m_log.DebugFormat("{0} CreatePatchFromHeightmap. patchX={1}, patchY={2}, DCOffset={3}, range={4}",
            //                         LogHeader, patchX, patchY, header.DCOffset, header.Range);

            // NOTE: No idea what prequant and postquant should be or what they do
            int wbits;

            int[] patch = CompressPatch(terrData, patchX, patchY, header, 10, out wbits);
            wbits = EncodePatchHeader(output, header, patch, (uint)terrData.SizeX, (uint)terrData.SizeY, wbits);
            EncodePatch(output, patch, 0, wbits);
        }
        /// <summary>
        ///     Creates a LayerData packet for compressed land data given a full
        ///     simulator heightmap and an array of indices of patches to compress
        /// </summary>
        /// <param name="terrData">
        ///     Terrain data that can result in a meter square heightmap.
        /// </param>
        /// <param name="x">
        ///     Array of indexes in the grid of patches
        ///     for this simulator.
        ///     If creating a packet for multiple patches, there will be entries in
        ///     both the X and Y arrays for each of the patches.
        ///     For example if patches 1 and 17 are to be sent,
        ///     x[] = {1,1} and y[] = {0,1} which specifies the patches at
        ///     indexes <1,0> and <1,1> (presuming the terrain size is 16x16 patches).
        /// </param>
        /// <param name="y">
        ///     Array of indexes in the grid of patches.
        /// </param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static LayerDataPacket CreateLandPacket(HeightMapTerrainData terrData, int[] x, int[] y, byte type)
        {
            LayerDataPacket layer = new LayerDataPacket {
                LayerID = { Type = type }
            };

            TerrainPatch.GroupHeader header = new TerrainPatch.GroupHeader
            {
                Stride = STRIDE, PatchSize = Constants.TerrainPatchSize
            };

            byte[]  data    = new byte[x.Length * Constants.TerrainPatchSize * Constants.TerrainPatchSize * 2];
            BitPack bitpack = new BitPack(data, 0);

            bitpack.PackBits(header.Stride, 16);
            bitpack.PackBits(header.PatchSize, 8);
            bitpack.PackBits(type, 8);

            for (int i = 0; i < x.Length; i++)
            {
                CreatePatchFromHeightmap(bitpack, terrData, x[i], y[i]);
            }

            bitpack.PackBits(END_OF_PATCHES, 8);

            layer.LayerData.Data = new byte[bitpack.BytePos + 1];
            Buffer.BlockCopy(bitpack.Data, 0, layer.LayerData.Data, 0, bitpack.BytePos + 1);

            return(layer);
        }
        // Create a land packet for a single patch.
        public static LayerDataPacket CreateLandPacket(HeightMapTerrainData terrData, int patchX, int patchY)
        {
            int[] xPieces = new int[1];
            int[] yPieces = new int[1];
            xPieces[0] = patchX;  // patch X dimension
            yPieces[0] = patchY;

            return(CreateLandPacket(terrData, xPieces, yPieces));
        }
        public static LayerDataPacket CreateLandPacket(HeightMapTerrainData terrData, int[] xPieces, int[] yPieces)
        {
            byte landPacketType = (byte)TerrainPatch.LayerType.Land;

            if (terrData.SizeX > Constants.RegionSize || terrData.SizeY > Constants.RegionSize)
            {
                landPacketType = (byte)TerrainPatch.LayerType.LandExtended;
            }

            return(CreateLandPacket(terrData, xPieces, yPieces, landPacketType));
        }
        private static int[] CompressPatch(HeightMapTerrainData terrData, int patchX, int patchY, TerrainPatch.Header header,
                                           int prequant, out int wbits)
        {
            float[] block    = new float[Constants.TerrainPatchSize * Constants.TerrainPatchSize];
            int     wordsize = prequant;
            float   oozrange = 1.0f / header.Range;
            float   range    = (1 << prequant);
            float   premult  = oozrange * range;
            float   sub      = (1 << (prequant - 1)) + header.DCOffset * premult;

            header.QuantWBits  = wordsize - 2;
            header.QuantWBits |= (prequant - 2) << 4;

            int k = 0;

            int yPatchLimit = patchY >= (terrData.SizeY / Constants.TerrainPatchSize) ?
                              (terrData.SizeY - Constants.TerrainPatchSize) / Constants.TerrainPatchSize : patchY;

            yPatchLimit = (yPatchLimit + 1) * Constants.TerrainPatchSize;

            int xPatchLimit = patchX >= (terrData.SizeX / Constants.TerrainPatchSize) ?
                              (terrData.SizeX - Constants.TerrainPatchSize) / Constants.TerrainPatchSize : patchX;

            xPatchLimit = (xPatchLimit + 1) * Constants.TerrainPatchSize;

            for (int yy = patchY * Constants.TerrainPatchSize; yy < yPatchLimit; yy++)
            {
                for (int xx = patchX * Constants.TerrainPatchSize; xx < xPatchLimit; xx++)
                {
                    block[k++] = terrData[xx, yy] * premult - sub;
                }
            }

            float[] ftemp = new float[Constants.TerrainPatchSize * Constants.TerrainPatchSize];
            int[]   itemp = new int[Constants.TerrainPatchSize * Constants.TerrainPatchSize];

            int maxWbits = prequant + 5;

            wbits = (prequant >> 1);

            for (int o = 0; o < Constants.TerrainPatchSize; o++)
            {
                DCTLine16(block, ftemp, o);
            }
            for (int o = 0; o < Constants.TerrainPatchSize; o++)
            {
                wbits = DCTColumn16Wbits(ftemp, itemp, o, wbits, maxWbits);
            }

            return(itemp);
        }
Пример #8
0
 // TerrainData.Clone
 public HeightMapTerrainData Clone()
 {
     HeightMapTerrainData ret = new HeightMapTerrainData(SizeX, SizeY, SizeZ);
     ret.m_heightmap = (int[,])this.m_heightmap.Clone();
     return ret;
 }
        /// <summary>
        /// Stores the terrain map to DB.
        /// </summary>
        /// <param name="terrain">terrain map data.</param>
        /// <param name="regionID">regionID.</param>
        public void StoreTerrain(HeightMapTerrainData terrData, UUID regionID)
        {
            //Delete old terrain map
            string sql = "delete from terrain where RegionUUID=@RegionUUID";
            using (SqlConnection conn = new SqlConnection(m_connectionString))
            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                cmd.Parameters.Add(_Database.CreateParameter("@RegionUUID", regionID));
                conn.Open();
                cmd.ExecuteNonQuery();
            }

            sql = "insert into terrain(RegionUUID, Revision, Heightfield) values(@RegionUUID, @Revision, @Heightfield)";

            int terrainDBRevision;
            Array terrainDBblob;
            terrData.GetDatabaseBlob(out terrainDBRevision, out terrainDBblob);

            using (SqlConnection conn = new SqlConnection(m_connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.Add(_Database.CreateParameter("@RegionUUID", regionID));
                    cmd.Parameters.Add(_Database.CreateParameter("@Revision", terrainDBRevision));
                    cmd.Parameters.Add(_Database.CreateParameter("@Heightfield", terrainDBblob));
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }
            }

            _Log.InfoFormat("{0} Stored terrain revision r={1}", LogHeader, terrainDBRevision);
        }
        /// <summary>
        /// Store a terrain revision in region storage
        /// </summary>
        /// <param name="ter">terrain heightfield</param>
        /// <param name="regionID">region UUID</param>
        public void StoreTerrain(HeightMapTerrainData terrData, UUID regionID)
        {
            lock (this)
            {
                using (
                    SqliteCommand cmd = new SqliteCommand("delete from terrain where RegionUUID=:RegionUUID", m_conn))
                {
                    cmd.Parameters.Add(new SqliteParameter(":RegionUUID", regionID.ToString()));
                    cmd.ExecuteNonQuery();
                }

                // the following is an work around for .NET.  The perf
                // issues associated with it aren't as bad as you think.
                String sql = "insert into terrain(RegionUUID, Revision, Heightfield)" +
                                " values(:RegionUUID, :Revision, :Heightfield)";

                int terrainDBRevision;
                Array terrainDBblob;
                terrData.GetDatabaseBlob(out terrainDBRevision, out terrainDBblob);

                m_log.DebugFormat("{0} Storing terrain revision r {1}", LogHeader, terrainDBRevision);

                using (SqliteCommand cmd = new SqliteCommand(sql, m_conn))
                {
                    cmd.Parameters.Add(new SqliteParameter(":RegionUUID", regionID.ToString()));
                    cmd.Parameters.Add(new SqliteParameter(":Revision", terrainDBRevision));
                    cmd.Parameters.Add(new SqliteParameter(":Heightfield", terrainDBblob));
                    cmd.ExecuteNonQuery();
                }
            }
        }
Пример #11
0
        public static LayerDataPacket CreateLandPacket(HeightMapTerrainData terrData, int[] xPieces, int[] yPieces)
        {
            byte landPacketType = (byte)TerrainPatch.LayerType.Land;
            if (terrData.SizeX > Constants.RegionSize || terrData.SizeY > Constants.RegionSize)
            {
                landPacketType = (byte)TerrainPatch.LayerType.LandExtended;
            }

            return CreateLandPacket(terrData, xPieces, yPieces, landPacketType);
        }
        public void StoreTerrain(HeightMapTerrainData terrData, UUID regionID)
        {
            using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
            {
                dbcon.Open();

                using (MySqlCommand cmd = dbcon.CreateCommand())
                {
                    cmd.CommandText = "delete from terrain where RegionUUID = ?RegionUUID";
                    cmd.Parameters.AddWithValue("RegionUUID", regionID.ToString());

                    ExecuteNonQuery(cmd);

                    int terrainDBRevision;
                    Array terrainDBblob;
                    terrData.GetDatabaseBlob(out terrainDBRevision, out terrainDBblob);

                    m_log.InfoFormat("{0} Storing terrain. X={1}, Y={2}, rev={3}",
                                LogHeader, terrData.SizeX, terrData.SizeY, terrainDBRevision);

                    cmd.CommandText = "insert into terrain (RegionUUID, Revision, Heightfield)"
                    +   "values (?RegionUUID, ?Revision, ?Heightfield)";

                    cmd.Parameters.AddWithValue("Revision", terrainDBRevision);
                    cmd.Parameters.AddWithValue("Heightfield", terrainDBblob);

                    ExecuteNonQuery(cmd);
                }
            }
        }
Пример #13
0
        private static int[] CompressPatch(HeightMapTerrainData terrData, int patchX, int patchY, TerrainPatch.Header header,
                                                               int prequant, out int wbits)
        {
            float[] block = new float[Constants.TerrainPatchSize*Constants.TerrainPatchSize];
            int wordsize = prequant;
            float oozrange = 1.0f/header.Range;
            float range = (1 << prequant);
            float premult = oozrange*range;
            float sub = (1 << (prequant - 1)) + header.DCOffset*premult;

            header.QuantWBits = wordsize - 2;
            header.QuantWBits |= (prequant - 2) << 4;

            int k = 0;

            int yPatchLimit = patchY >= (terrData.SizeY / Constants.TerrainPatchSize) ?
                            (terrData.SizeY - Constants.TerrainPatchSize) / Constants.TerrainPatchSize : patchY;
            yPatchLimit = (yPatchLimit + 1) * Constants.TerrainPatchSize;

            int xPatchLimit = patchX >= (terrData.SizeX / Constants.TerrainPatchSize) ?
                            (terrData.SizeX - Constants.TerrainPatchSize) / Constants.TerrainPatchSize : patchX;
            xPatchLimit = (xPatchLimit + 1) * Constants.TerrainPatchSize;

            for (int yy = patchY * Constants.TerrainPatchSize; yy < yPatchLimit; yy++)
            {
                for (int xx = patchX * Constants.TerrainPatchSize; xx < xPatchLimit; xx++)
                {
                    block[k++] = terrData[xx, yy] * premult - sub;
                }
            }

            float[] ftemp = new float[Constants.TerrainPatchSize*Constants.TerrainPatchSize];
            int[] itemp = new int[Constants.TerrainPatchSize*Constants.TerrainPatchSize];

            int maxWbits = prequant + 5;
            wbits = (prequant >> 1);

            for (int o = 0; o < Constants.TerrainPatchSize; o++)
                DCTLine16(block, ftemp, o);
            for (int o = 0; o < Constants.TerrainPatchSize; o++)
                wbits = DCTColumn16Wbits(ftemp, itemp, o, wbits, maxWbits);

            return itemp;
        }
Пример #14
0
        // Scan the height info we're returning and return a patch packet header for this patch.
        private static TerrainPatch.Header PrescanPatch(HeightMapTerrainData terrData, int patchX, int patchY)
        {
            TerrainPatch.Header header = new TerrainPatch.Header();
            float zmax = -99999999.0f;
            float zmin = 99999999.0f;

            for (int j = patchY*Constants.TerrainPatchSize; j < (patchY + 1)*Constants.TerrainPatchSize; j++)
            {
                for (int i = patchX*Constants.TerrainPatchSize; i < (patchX + 1)*Constants.TerrainPatchSize; i++)
                {
                    float val = terrData[i, j];
                    if (val > zmax) zmax = val;
                    if (val < zmin) zmin = val;
                }
            }

            header.DCOffset = zmin;
            header.Range = (int)((zmax - zmin) + 1.0f);

            return header;
        }
Пример #15
0
        /// <summary>
        ///     Add a patch of terrain to a BitPacker
        /// </summary>
        /// <param name="output">BitPacker to write the patch to</param>
        /// <param name="heightmap">
        ///     Heightmap of the simulator. Presumed to be an sizeX*sizeY array.
        /// </param>
        /// <param name="patchX">
        ///     X offset of the patch to create.
        /// </param>
        /// <param name="patchY">
        ///     Y offset of the patch to create.
        /// </param>
        /// <param name="pRegionSizeX"></param>
        /// <param name="pRegionSizeY"></param>
        public static void CreatePatchFromHeightmap(BitPack output, HeightMapTerrainData terrData, int patchX, int patchY)
        {
            TerrainPatch.Header header = PrescanPatch(terrData, patchX, patchY);
            header.QuantWBits = 136;

            // If larger than legacy region size, pack patch X and Y info differently.
            if (terrData.SizeX > Constants.RegionSize || terrData.SizeY > Constants.RegionSize)
            {
                header.PatchIDs = (patchY & 0xFFFF);
                header.PatchIDs += (patchX << 16);
            }
            else
            {
                header.PatchIDs = (patchY & 0x1F);
                header.PatchIDs += (patchX << 5);
            }

            // m_log.DebugFormat("{0} CreatePatchFromHeightmap. patchX={1}, patchY={2}, DCOffset={3}, range={4}",
            //                         LogHeader, patchX, patchY, header.DCOffset, header.Range);

            // NOTE: No idea what prequant and postquant should be or what they do
            int wbits;
            int[] patch = CompressPatch(terrData, patchX, patchY, header, 10, out wbits);
            wbits = EncodePatchHeader(output, header, patch, (uint)terrData.SizeX, (uint)terrData.SizeY, wbits);
            EncodePatch(output, patch, 0, wbits);
        }
Пример #16
0
        /// <summary>
        ///     Creates a LayerData packet for compressed land data given a full
        ///     simulator heightmap and an array of indices of patches to compress
        /// </summary>
        /// <param name="terrData">
        ///     Terrain data that can result in a meter square heightmap.
        /// </param>
        /// <param name="x">
        ///     Array of indexes in the grid of patches
        ///     for this simulator.
        ///     If creating a packet for multiple patches, there will be entries in
        ///     both the X and Y arrays for each of the patches.
        ///     For example if patches 1 and 17 are to be sent,
        ///     x[] = {1,1} and y[] = {0,1} which specifies the patches at
        ///     indexes <1,0> and <1,1> (presuming the terrain size is 16x16 patches).
        /// </param>
        /// <param name="y">
        ///     Array of indexes in the grid of patches.
        /// </param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static LayerDataPacket CreateLandPacket(HeightMapTerrainData terrData, int[] x, int[] y, byte type)
        {
            LayerDataPacket layer = new LayerDataPacket {LayerID = {Type = type}};

            TerrainPatch.GroupHeader header = new TerrainPatch.GroupHeader
                                                  {Stride = STRIDE, PatchSize = Constants.TerrainPatchSize};

            byte[] data = new byte[x.Length * Constants.TerrainPatchSize * Constants.TerrainPatchSize * 2];
            BitPack bitpack = new BitPack(data, 0);
            bitpack.PackBits(header.Stride, 16);
            bitpack.PackBits(header.PatchSize, 8);
            bitpack.PackBits(type, 8);

            for (int i = 0; i < x.Length; i++)
                CreatePatchFromHeightmap(bitpack, terrData, x[i], y[i]);

            bitpack.PackBits(END_OF_PATCHES, 8);

            layer.LayerData.Data = new byte[bitpack.BytePos + 1];
            Buffer.BlockCopy(bitpack.Data, 0, layer.LayerData.Data, 0, bitpack.BytePos + 1);

            return layer;
        }
Пример #17
0
 // Legacy. Just don't do this.
 public void StoreTerrain(double[,] ter, UUID regionID)
 {
     HeightMapTerrainData terrData = new HeightMapTerrainData(ter);
     StoreTerrain(terrData, regionID);
 }
        /// <summary>
        /// Stores the terrain map to DB.
        /// </summary>
        /// <param name="terrain">terrain map data.</param>
        /// <param name="regionID">regionID.</param>
        public void StoreTerrain(HeightMapTerrainData terrData, UUID regionID)
        {
            //Delete old terrain map
            string sql = @"delete from terrain where ""RegionUUID""=:RegionUUID";
            using (NpgsqlConnection conn = new NpgsqlConnection(m_connectionString))
            {
                using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
                {
                    cmd.Parameters.Add(_Database.CreateParameter("RegionUUID", regionID));
                    conn.Open();
                    cmd.ExecuteNonQuery();

                    _Log.InfoFormat("{0} Deleted terrain revision id = {1}", LogHeader, regionID);
                }
            }

            int terrainDBRevision;
            Array terrainDBblob;
            terrData.GetDatabaseBlob(out terrainDBRevision, out terrainDBblob);

            sql = @"insert into terrain(""RegionUUID"", ""Revision"", ""Heightfield"") values(:RegionUUID, :Revision, :Heightfield)";

            using (NpgsqlConnection conn = new NpgsqlConnection(m_connectionString))
            {
                using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
                {
                    cmd.Parameters.Add(_Database.CreateParameter("RegionUUID", regionID));
                    cmd.Parameters.Add(_Database.CreateParameter("Revision", terrainDBRevision));
                    cmd.Parameters.Add(_Database.CreateParameter("Heightfield", terrainDBblob));
                    conn.Open();
                    cmd.ExecuteNonQuery();

                    _Log.InfoFormat("{0} Stored terrain id = {1}, terrainSize = <{2},{3}>",
                                    LogHeader, regionID, terrData.SizeX, terrData.SizeY);
                }
            }

        }
Пример #19
0
        // Create a land packet for a single patch.
        public static LayerDataPacket CreateLandPacket(HeightMapTerrainData terrData, int patchX, int patchY)
        {
            int[] xPieces = new int[1];
            int[] yPieces = new int[1];
            xPieces[0] = patchX;  // patch X dimension
            yPieces[0] = patchY;

            return CreateLandPacket(terrData, xPieces, yPieces);
        }
 public void StoreTerrain(HeightMapTerrainData terrain, UUID regionID)
 {
     m_database.StoreTerrain(terrain, regionID);
 }
Пример #21
0
 public void StoreTerrain(HeightMapTerrainData ter, UUID regionID)
 {
     m_terrains[regionID] = ter;
 }