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;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.IO.Compression;
using System.Reflection; using System.Reflection;
using OpenMetaverse; using OpenMetaverse;
@ -102,6 +103,11 @@ namespace OpenSim.Framework
// "ushort compressedHeight = (ushort)(height * compressionFactor);" // "ushort compressedHeight = (ushort)(height * compressionFactor);"
// The dimensions are presumed to be multiples of 16 and, more likely, multiples of 256. // The dimensions are presumed to be multiples of 16 and, more likely, multiples of 256.
Compressed2D = 27, 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'. // A revision that is not listed above or any revision greater than this value is 'Legacy256'.
RevisionHigh = 1234 RevisionHigh = 1234
} }
@ -120,7 +126,8 @@ namespace OpenSim.Framework
public override float this[int x, int y] public override float this[int x, int y]
{ {
get { return FromCompressedHeight(m_heightmap[x, y]); } get { return FromCompressedHeight(m_heightmap[x, y]); }
set { set
{
ushort newVal = ToCompressedHeight(value); ushort newVal = ToCompressedHeight(value);
if (m_heightmap[x, y] != newVal) if (m_heightmap[x, y] != newVal)
{ {
@ -202,8 +209,8 @@ namespace OpenSim.Framework
} }
else else
{ {
DBRevisionCode = (int)DBTerrainRevision.Compressed2D; DBRevisionCode = (int)DBTerrainRevision.Compressed2DuGzip;
blob = ToCompressedTerrainSerialization(); blob = ToCompressedTerrainSerializationGzip();
ret = true; ret = true;
} }
return ret; return ret;
@ -276,6 +283,11 @@ namespace OpenSim.Framework
// to make for two decimal points). // to make for two decimal points).
public ushort ToCompressedHeight(double pHeight) 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); return (ushort)(pHeight * CompressionFactor);
} }
@ -322,7 +334,8 @@ namespace OpenSim.Framework
ClearLand(0f); 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; m_compressionFactor = pCompressionFactor;
int ind = 0; int ind = 0;
@ -333,12 +346,21 @@ namespace OpenSim.Framework
} }
// Create a heighmap from a database blob // 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) 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); 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); m_log.DebugFormat("{0} HeightmapTerrainData create from Compressed2D serialization. Size=<{1},{2}>", LogHeader, SizeX, SizeY);
break; break;
default: default:
@ -379,6 +401,8 @@ namespace OpenSim.Framework
// In case database info doesn't match real terrain size, initialize the whole terrain. // In case database info doesn't match real terrain size, initialize the whole terrain.
ClearLand(); ClearLand();
try
{
using (MemoryStream mstr = new MemoryStream(pBlob)) using (MemoryStream mstr = new MemoryStream(pBlob))
{ {
using (BinaryReader br = new BinaryReader(mstr)) using (BinaryReader br = new BinaryReader(mstr))
@ -388,24 +412,61 @@ namespace OpenSim.Framework
for (int yy = 0; yy < (int)Constants.RegionSize; yy++) for (int yy = 0; yy < (int)Constants.RegionSize; yy++)
{ {
float val = (float)br.ReadDouble(); float val = (float)br.ReadDouble();
if (xx < SizeX && yy < SizeY) if (xx < SizeX && yy < SizeY)
m_heightmap[xx, yy] = ToCompressedHeight(val); m_heightmap[xx, yy] = ToCompressedHeight(val);
} }
} }
} }
}
}
catch
{
ClearLand();
}
ClearTaint(); 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. // See the reader below.
public Array ToCompressedTerrainSerialization() public Array ToCompressedTerrainSerialization()
{ {
Array ret = null; Array ret = null;
using (MemoryStream str = new MemoryStream((3 * sizeof(Int32)) + (SizeX * SizeY * sizeof(ushort)))) try
{
using (MemoryStream str = new MemoryStream((4 * sizeof(Int32)) + (SizeX * SizeY * sizeof(ushort))))
{ {
using (BinaryWriter bw = new BinaryWriter(str)) using (BinaryWriter bw = new BinaryWriter(str))
{ {
bw.Write((Int32)DBTerrainRevision.Compressed2D); bw.Write((Int32)DBTerrainRevision.Compressed2Du);
bw.Write((Int32)SizeX); bw.Write((Int32)SizeX);
bw.Write((Int32)SizeY); bw.Write((Int32)SizeY);
bw.Write((Int32)CompressionFactor); bw.Write((Int32)CompressionFactor);
@ -417,6 +478,55 @@ namespace OpenSim.Framework
} }
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; return ret;
} }
@ -426,10 +536,54 @@ namespace OpenSim.Framework
// the forth int is the compression factor for the following int16s // 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 // 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. // 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; Int32 hmFormatCode, hmSizeX, hmSizeY, hmCompressionFactor;
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++)
{
short val = br.ReadInt16();
if (val < 0)
val = 0;
if (xx < SizeX && yy < SizeY)
m_heightmap[xx, yy] = (ushort)val;
}
}
}
ClearTaint();
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 (MemoryStream mstr = new MemoryStream(pBlob))
{ {
using (BinaryReader br = new BinaryReader(mstr)) using (BinaryReader br = new BinaryReader(mstr))
@ -454,11 +608,48 @@ namespace OpenSim.Framework
} }
} }
} }
}
}
catch (Exception e)
{
ClearTaint();
m_log.ErrorFormat("{0} Read (compressed2Dus) terrain error: {1} - terrain may be damaged",
LogHeader,e.Message);
return;
}
ClearTaint(); 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 terrain. Heightmap size=<{1},{2}>. Region size=<{3},{4}>. CompFact={5}",
LogHeader, hmSizeX, hmSizeY, SizeX, SizeY, hmCompressionFactor); 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 desiredMin = (double)args[0];
double desiredMax = (double)args[1]; 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 // determine desired scaling factor
double desiredRange = desiredMax - desiredMin; double desiredRange = desiredMax - desiredMin;
//m_log.InfoFormat("Desired {0}, {1} = {2}", new Object[] { desiredMin, desiredMax, desiredRange }); //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) 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; int x, y;
for (x = 0; x < m_channel.Width; x++) for (x = 0; x < m_channel.Width; x++)
for (y = 0; y < m_channel.Height; y++) 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) private void InterfaceMultiplyTerrain(Object[] args)
{ {
int x, y; int x, y;
double val = (double)args[0];
for (x = 0; x < m_channel.Width; x++) for (x = 0; x < m_channel.Width; x++)
for (y = 0; y < m_channel.Height; y++) 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) private void InterfaceLowerTerrain(Object[] args)
{ {
int x, y; int x, y;
double val = (double)args[0];
if (val < 0 || val > 655.35)
for (x = 0; x < m_channel.Width; x++) for (x = 0; x < m_channel.Width; x++)
for (y = 0; y < m_channel.Height; y++) 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) public void InterfaceFillTerrain(Object[] args)
{ {
int x, y; 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 (x = 0; x < m_channel.Width; x++)
for (y = 0; y < m_channel.Height; y++) 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) private void InterfaceMinTerrain(Object[] args)
{ {
int x, y; 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 (x = 0; x < m_channel.Width; x++)
{ {
for (y = 0; y < m_channel.Height; y++) 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) private void InterfaceMaxTerrain(Object[] args)
{ {
int x, y; 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 (x = 0; x < m_channel.Width; x++)
{ {
for (y = 0; y < m_channel.Height; y++) 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)) if (Double.IsNaN(value) || Double.IsInfinity(value))
return; return;
if (value < 0)
value = 0;
else
if (value > 655.35)
value = 655.35;
m_terrainData[x, y] = (float)value; m_terrainData[x, y] = (float)value;
} }
} }