terrain stored as ushorts with gzip compression

avinationmerge
UbitUmarov 2015-08-25 13:36:45 +01:00
parent 50d73873db
commit 64d05bab0f
3 changed files with 271 additions and 39 deletions

View File

@ -28,6 +28,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Reflection;
using OpenMetaverse;
@ -102,6 +103,11 @@ namespace OpenSim.Framework
// "ushort compressedHeight = (ushort)(height * compressionFactor);"
// The dimensions are presumed to be multiples of 16 and, more likely, multiples of 256.
Compressed2D = 27,
// as Compressed2D but using ushort[] in place of int16[]
Compressed2Du = 28,
// as Compressed2D but using ushort[] in place of int16[] with Gzip compression
Compressed2DuGzip = 29,
// A revision that is not listed above or any revision greater than this value is 'Legacy256'.
RevisionHigh = 1234
}
@ -120,7 +126,8 @@ namespace OpenSim.Framework
public override float this[int x, int y]
{
get { return FromCompressedHeight(m_heightmap[x, y]); }
set {
set
{
ushort newVal = ToCompressedHeight(value);
if (m_heightmap[x, y] != newVal)
{
@ -177,7 +184,7 @@ namespace OpenSim.Framework
{
int tx = xx / Constants.TerrainPatchSize;
int ty = yy / Constants.TerrainPatchSize;
bool ret = m_taint[tx, ty];
bool ret = m_taint[tx, ty];
if (ret && clearOnTest)
m_taint[tx, ty] = false;
return ret;
@ -202,8 +209,8 @@ namespace OpenSim.Framework
}
else
{
DBRevisionCode = (int)DBTerrainRevision.Compressed2D;
blob = ToCompressedTerrainSerialization();
DBRevisionCode = (int)DBTerrainRevision.Compressed2DuGzip;
blob = ToCompressedTerrainSerializationGzip();
ret = true;
}
return ret;
@ -276,6 +283,11 @@ namespace OpenSim.Framework
// to make for two decimal points).
public ushort ToCompressedHeight(double pHeight)
{
// clamp into valid range
if (pHeight < 0)
pHeight = 0;
else if (pHeight > 655.35f)
pHeight = 655.35f;
return (ushort)(pHeight * CompressionFactor);
}
@ -322,7 +334,8 @@ namespace OpenSim.Framework
ClearLand(0f);
}
public HeightmapTerrainData(ushort[] cmap, float pCompressionFactor, int pX, int pY, int pZ) : this(pX, pY, pZ)
public HeightmapTerrainData(ushort[] cmap, float pCompressionFactor, int pX, int pY, int pZ)
: this(pX, pY, pZ)
{
m_compressionFactor = pCompressionFactor;
int ind = 0;
@ -333,12 +346,21 @@ namespace OpenSim.Framework
}
// Create a heighmap from a database blob
public HeightmapTerrainData(int pSizeX, int pSizeY, int pSizeZ, int pFormatCode, byte[] pBlob) : this(pSizeX, pSizeY, pSizeZ)
public HeightmapTerrainData(int pSizeX, int pSizeY, int pSizeZ, int pFormatCode, byte[] pBlob)
: this(pSizeX, pSizeY, pSizeZ)
{
switch ((DBTerrainRevision)pFormatCode)
{
case DBTerrainRevision.Compressed2D:
case DBTerrainRevision.Compressed2DuGzip:
FromCompressedTerrainSerializationGZip(pBlob);
m_log.DebugFormat("{0} HeightmapTerrainData create from Gzip Compressed2D (unsigned shorts) serialization. Size=<{1},{2}>", LogHeader, SizeX, SizeY);
break;
case DBTerrainRevision.Compressed2Du:
FromCompressedTerrainSerialization(pBlob);
m_log.DebugFormat("{0} HeightmapTerrainData create from Compressed2D (unsigned shorts) serialization. Size=<{1},{2}>", LogHeader, SizeX, SizeY);
break;
case DBTerrainRevision.Compressed2D:
FromCompressedTerrainSerialization2D(pBlob);
m_log.DebugFormat("{0} HeightmapTerrainData create from Compressed2D serialization. Size=<{1},{2}>", LogHeader, SizeX, SizeY);
break;
default:
@ -379,44 +401,132 @@ namespace OpenSim.Framework
// In case database info doesn't match real terrain size, initialize the whole terrain.
ClearLand();
using (MemoryStream mstr = new MemoryStream(pBlob))
try
{
using (BinaryReader br = new BinaryReader(mstr))
using (MemoryStream mstr = new MemoryStream(pBlob))
{
for (int xx = 0; xx < (int)Constants.RegionSize; xx++)
using (BinaryReader br = new BinaryReader(mstr))
{
for (int yy = 0; yy < (int)Constants.RegionSize; yy++)
for (int xx = 0; xx < (int)Constants.RegionSize; xx++)
{
float val = (float)br.ReadDouble();
if (xx < SizeX && yy < SizeY)
m_heightmap[xx, yy] = ToCompressedHeight(val);
for (int yy = 0; yy < (int)Constants.RegionSize; yy++)
{
float val = (float)br.ReadDouble();
if (xx < SizeX && yy < SizeY)
m_heightmap[xx, yy] = ToCompressedHeight(val);
}
}
}
}
ClearTaint();
}
catch
{
ClearLand();
}
ClearTaint();
}
public Array ToCompressedTerrainSerialization2D()
{
Array ret = null;
try
{
using (MemoryStream str = new MemoryStream((4 * sizeof(Int32)) + (SizeX * SizeY * sizeof(ushort))))
{
using (BinaryWriter bw = new BinaryWriter(str))
{
bw.Write((Int32)DBTerrainRevision.Compressed2D);
bw.Write((Int32)SizeX);
bw.Write((Int32)SizeY);
bw.Write((Int32)CompressionFactor);
for (int yy = 0; yy < SizeY; yy++)
for (int xx = 0; xx < SizeX; xx++)
{
bw.Write((Int16)m_heightmap[xx, yy]);
}
}
ret = str.ToArray();
}
}
catch
{
}
return ret;
}
// See the reader below.
public Array ToCompressedTerrainSerialization()
{
Array ret = null;
using (MemoryStream str = new MemoryStream((3 * sizeof(Int32)) + (SizeX * SizeY * sizeof(ushort))))
try
{
using (BinaryWriter bw = new BinaryWriter(str))
using (MemoryStream str = new MemoryStream((4 * sizeof(Int32)) + (SizeX * SizeY * sizeof(ushort))))
{
bw.Write((Int32)DBTerrainRevision.Compressed2D);
bw.Write((Int32)SizeX);
bw.Write((Int32)SizeY);
bw.Write((Int32)CompressionFactor);
for (int yy = 0; yy < SizeY; yy++)
for (int xx = 0; xx < SizeX; xx++)
{
bw.Write((ushort)m_heightmap[xx, yy]);
}
using (BinaryWriter bw = new BinaryWriter(str))
{
bw.Write((Int32)DBTerrainRevision.Compressed2Du);
bw.Write((Int32)SizeX);
bw.Write((Int32)SizeY);
bw.Write((Int32)CompressionFactor);
for (int yy = 0; yy < SizeY; yy++)
for (int xx = 0; xx < SizeX; xx++)
{
bw.Write((ushort)m_heightmap[xx, yy]);
}
}
ret = str.ToArray();
}
ret = str.ToArray();
}
catch
{
}
return ret;
}
// as above with Gzip compression
public Array ToCompressedTerrainSerializationGzip()
{
Array ret = null;
try
{
using (MemoryStream inp = new MemoryStream((4 * sizeof(Int32)) + (SizeX * SizeY * sizeof(ushort))))
{
using (BinaryWriter bw = new BinaryWriter(inp))
{
bw.Write((Int32)DBTerrainRevision.Compressed2DuGzip);
bw.Write((Int32)SizeX);
bw.Write((Int32)SizeY);
bw.Write((Int32)CompressionFactor);
for (int yy = 0; yy < SizeY; yy++)
for (int xx = 0; xx < SizeX; xx++)
{
bw.Write((ushort)m_heightmap[xx, yy]);
}
bw.Flush();
inp.Seek(0,SeekOrigin.Begin);
using (MemoryStream outputStream = new MemoryStream())
{
using (GZipStream compressionStream = new GZipStream(outputStream, CompressionMode.Compress))
{
inp.CopyStream(compressionStream, int.MaxValue);
compressionStream.Close();
ret = outputStream.ToArray();
}
}
}
}
}
catch
{
}
m_log.InfoFormat("{0} terrain GZiped to {1} bytes (compressed2Dus)",
LogHeader, ret.Length);
return ret;
}
@ -426,7 +536,7 @@ namespace OpenSim.Framework
// the forth int is the compression factor for the following int16s
// This is just sets heightmap info. The actual size of the region was set on this instance's
// creation and any heights not initialized by theis blob are set to the default height.
public void FromCompressedTerrainSerialization(byte[] pBlob)
public void FromCompressedTerrainSerialization2D(byte[] pBlob)
{
Int32 hmFormatCode, hmSizeX, hmSizeY, hmCompressionFactor;
@ -448,17 +558,98 @@ namespace OpenSim.Framework
{
for (int xx = 0; xx < hmSizeX; xx++)
{
ushort val = br.ReadUInt16();
short val = br.ReadInt16();
if (val < 0)
val = 0;
if (xx < SizeX && yy < SizeY)
m_heightmap[xx, yy] = val;
m_heightmap[xx, yy] = (ushort)val;
}
}
}
ClearTaint();
m_log.InfoFormat("{0} Read compressed 2d heightmap. Heightmap size=<{1},{2}>. Region size=<{3},{4}>. CompFact={5}",
m_log.InfoFormat("{0} Read (compressed2D) heightmap. Heightmap size=<{1},{2}>. Region size=<{3},{4}>. CompFact={5}",
LogHeader, hmSizeX, hmSizeY, SizeX, SizeY, hmCompressionFactor);
}
}
// Initialize heightmap from blob consisting of:
// int32, int32, int32, int32, ushort[]
// where the first int32 is format code, next two int32s are the X and y of heightmap data and
// the forth int is the compression factor for the following int16s
// This is just sets heightmap info. The actual size of the region was set on this instance's
// creation and any heights not initialized by theis blob are set to the default height.
public void FromCompressedTerrainSerialization(byte[] pBlob)
{
Int32 hmFormatCode, hmSizeX, hmSizeY, hmCompressionFactor;
try
{
using (MemoryStream mstr = new MemoryStream(pBlob))
{
using (BinaryReader br = new BinaryReader(mstr))
{
hmFormatCode = br.ReadInt32();
hmSizeX = br.ReadInt32();
hmSizeY = br.ReadInt32();
hmCompressionFactor = br.ReadInt32();
m_compressionFactor = hmCompressionFactor;
// In case database info doesn't match real terrain size, initialize the whole terrain.
ClearLand();
for (int yy = 0; yy < hmSizeY; yy++)
{
for (int xx = 0; xx < hmSizeX; xx++)
{
ushort val = br.ReadUInt16();
if (xx < SizeX && yy < SizeY)
m_heightmap[xx, yy] = val;
}
}
}
}
}
catch (Exception e)
{
ClearTaint();
m_log.ErrorFormat("{0} Read (compressed2Dus) terrain error: {1} - terrain may be damaged",
LogHeader,e.Message);
return;
}
ClearTaint();
m_log.InfoFormat("{0} Read compressed2D terrain. Heightmap size=<{1},{2}>. Region size=<{3},{4}>. CompFact={5}",
LogHeader, hmSizeX, hmSizeY, SizeX, SizeY, hmCompressionFactor);
}
// as above but Gzip compressed
public void FromCompressedTerrainSerializationGZip(byte[] pBlob)
{
m_log.InfoFormat("{0} GZip {1} bytes for terrain",
LogHeader,pBlob.Length);
byte[] gzipout = null;
try
{
using (MemoryStream inputStream = new MemoryStream(pBlob))
{
using (GZipStream decompressionStream = new GZipStream(inputStream, CompressionMode.Decompress))
{
using (MemoryStream outputStream = new MemoryStream())
{
decompressionStream.Flush();
decompressionStream.CopyTo(outputStream);
gzipout = outputStream.ToArray();
}
}
}
}
catch
{
}
FromCompressedTerrainSerialization(gzipout);
}
}
}

View File

@ -1349,6 +1349,13 @@ namespace OpenSim.Region.CoreModules.World.Terrain
double desiredMin = (double)args[0];
double desiredMax = (double)args[1];
if (desiredMin < 0 || desiredMin > 655.35
|| desiredMax < 0 || desiredMax > 655.35)
{
m_log.Error("desired Min and Max must be in range 0.0 to 655.0m");
return;
}
// determine desired scaling factor
double desiredRange = desiredMax - desiredMin;
//m_log.InfoFormat("Desired {0}, {1} = {2}", new Object[] { desiredMin, desiredMax, desiredRange });
@ -1405,45 +1412,69 @@ namespace OpenSim.Region.CoreModules.World.Terrain
private void InterfaceElevateTerrain(Object[] args)
{
double val = (double)args[0];
if (val < 0 || val > 655.35)
{
m_log.Error("elevation must be in range 0.0 to 655.0m");
return;
}
int x, y;
for (x = 0; x < m_channel.Width; x++)
for (y = 0; y < m_channel.Height; y++)
m_channel[x, y] += (double) args[0];
m_channel[x, y] += val;
}
private void InterfaceMultiplyTerrain(Object[] args)
{
int x, y;
double val = (double)args[0];
for (x = 0; x < m_channel.Width; x++)
for (y = 0; y < m_channel.Height; y++)
m_channel[x, y] *= (double) args[0];
m_channel[x, y] *= val;
}
private void InterfaceLowerTerrain(Object[] args)
{
int x, y;
double val = (double)args[0];
if (val < 0 || val > 655.35)
for (x = 0; x < m_channel.Width; x++)
for (y = 0; y < m_channel.Height; y++)
m_channel[x, y] -= (double) args[0];
m_channel[x, y] -= val;
}
public void InterfaceFillTerrain(Object[] args)
{
int x, y;
double val = (double)args[0];
if (val < 0 || val > 655.35)
{
m_log.Error("height must be in range 0.0 to 655.0m");
return;
}
for (x = 0; x < m_channel.Width; x++)
for (y = 0; y < m_channel.Height; y++)
m_channel[x, y] = (double) args[0];
m_channel[x, y] = val;
}
private void InterfaceMinTerrain(Object[] args)
{
int x, y;
double val = (double)args[0];
if (val < 0 || val > 655.35)
{
m_log.Error("minimum must be in range 0.0 to 655.0m");
return;
}
for (x = 0; x < m_channel.Width; x++)
{
for (y = 0; y < m_channel.Height; y++)
{
m_channel[x, y] = Math.Max((double)args[0], m_channel[x, y]);
m_channel[x, y] = Math.Max(val, m_channel[x, y]);
}
}
}
@ -1451,11 +1482,17 @@ namespace OpenSim.Region.CoreModules.World.Terrain
private void InterfaceMaxTerrain(Object[] args)
{
int x, y;
double val = (double)args[0];
if (val < 0 || val > 655.35)
{
m_log.Error("maximum must be in range 0.0 to 655.0m");
return;
}
for (x = 0; x < m_channel.Width; x++)
{
for (y = 0; y < m_channel.Height; y++)
{
m_channel[x, y] = Math.Min((double)args[0], m_channel[x, y]);
m_channel[x, y] = Math.Min(val, m_channel[x, y]);
}
}
}

View File

@ -157,7 +157,11 @@ namespace OpenSim.Region.Framework.Scenes
{
if (Double.IsNaN(value) || Double.IsInfinity(value))
return;
if (value < 0)
value = 0;
else
if (value > 655.35)
value = 655.35;
m_terrainData[x, y] = (float)value;
}
}