rearrange source code
parent
2b0587770a
commit
e6f675b81b
|
@ -65,7 +65,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP
|
||||||
private const int NEGATIVE_VALUE = 0x7;
|
private const int NEGATIVE_VALUE = 0x7;
|
||||||
|
|
||||||
|
|
||||||
private static readonly float[] CosineTable16 = new float[Constants.TerrainPatchSize * Constants.TerrainPatchSize];
|
// private static readonly float[] CosineTable16 = new float[Constants.TerrainPatchSize * Constants.TerrainPatchSize];
|
||||||
private static readonly int[] CopyMatrix16 = new int[Constants.TerrainPatchSize * Constants.TerrainPatchSize];
|
private static readonly int[] CopyMatrix16 = new int[Constants.TerrainPatchSize * Constants.TerrainPatchSize];
|
||||||
|
|
||||||
private static readonly float[] QuantizeTable16 =
|
private static readonly float[] QuantizeTable16 =
|
||||||
|
@ -77,7 +77,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP
|
||||||
{
|
{
|
||||||
// Initialize the decompression tables
|
// Initialize the decompression tables
|
||||||
BuildDequantizeTable16();
|
BuildDequantizeTable16();
|
||||||
SetupCosines16();
|
// SetupCosines16();
|
||||||
BuildCopyMatrix16();
|
BuildCopyMatrix16();
|
||||||
BuildQuantizeTable16();
|
BuildQuantizeTable16();
|
||||||
}
|
}
|
||||||
|
@ -140,6 +140,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP
|
||||||
|
|
||||||
return header;
|
return header;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int[] CompressPatch(float[] patchData, TerrainPatch.Header header, int prequant, out int wbits)
|
private static int[] CompressPatch(float[] patchData, TerrainPatch.Header header, int prequant, out int wbits)
|
||||||
{
|
{
|
||||||
float[] block = new float[Constants.TerrainPatchSize * Constants.TerrainPatchSize];
|
float[] block = new float[Constants.TerrainPatchSize * Constants.TerrainPatchSize];
|
||||||
|
@ -189,7 +190,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP
|
||||||
|
|
||||||
for (int i = 0; i < x.Length; i++)
|
for (int i = 0; i < x.Length; i++)
|
||||||
{
|
{
|
||||||
CreatePatchFromHeightmap(bitpack, terrData, x[i], y[i]);
|
CreatePatchFromTerrainData(bitpack, terrData, x[i], y[i]);
|
||||||
if (bitpack.BytePos > 1000 && i != x.Length - 1)
|
if (bitpack.BytePos > 1000 && i != x.Length - 1)
|
||||||
{
|
{
|
||||||
//finish this packet
|
//finish this packet
|
||||||
|
@ -219,22 +220,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
public static void CreatePatchFromTerrainData(BitPack output, TerrainData terrData, int patchX, int patchY)
|
||||||
/// 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, TerrainData terrData, int patchX, int patchY)
|
|
||||||
{
|
{
|
||||||
float frange;
|
float frange;
|
||||||
TerrainPatch.Header header = PrescanPatch(terrData, patchX, patchY, out frange);
|
TerrainPatch.Header header = PrescanPatch(terrData, patchX, patchY, out frange);
|
||||||
|
@ -284,8 +270,9 @@ namespace OpenSim.Region.ClientStack.LindenUDP
|
||||||
private static TerrainPatch.Header PrescanPatch(TerrainData terrData, int patchX, int patchY, out float frange)
|
private static TerrainPatch.Header PrescanPatch(TerrainData terrData, int patchX, int patchY, out float frange)
|
||||||
{
|
{
|
||||||
TerrainPatch.Header header = new TerrainPatch.Header();
|
TerrainPatch.Header header = new TerrainPatch.Header();
|
||||||
float zmax = -99999999.0f;
|
float zmax = float.MinValue;
|
||||||
float zmin = 99999999.0f;
|
float zmin = float.MaxValue;
|
||||||
|
|
||||||
int startx = patchX * Constants.TerrainPatchSize;
|
int startx = patchX * Constants.TerrainPatchSize;
|
||||||
int starty = patchY * Constants.TerrainPatchSize;
|
int starty = patchY * Constants.TerrainPatchSize;
|
||||||
|
|
||||||
|
@ -306,29 +293,6 @@ namespace OpenSim.Region.ClientStack.LindenUDP
|
||||||
return header;
|
return header;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TerrainPatch.Header DecodePatchHeader(BitPack bitpack)
|
|
||||||
{
|
|
||||||
TerrainPatch.Header header = new TerrainPatch.Header { QuantWBits = bitpack.UnpackBits(8) };
|
|
||||||
|
|
||||||
// Quantized word bits
|
|
||||||
if (header.QuantWBits == END_OF_PATCHES)
|
|
||||||
return header;
|
|
||||||
|
|
||||||
// DC offset
|
|
||||||
header.DCOffset = bitpack.UnpackFloat();
|
|
||||||
|
|
||||||
// Range
|
|
||||||
header.Range = bitpack.UnpackBits(16);
|
|
||||||
|
|
||||||
// Patch IDs (10 bits)
|
|
||||||
header.PatchIDs = bitpack.UnpackBits(10);
|
|
||||||
|
|
||||||
// Word bits
|
|
||||||
header.WordBits = (uint)((header.QuantWBits & 0x0f) + 2);
|
|
||||||
|
|
||||||
return header;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void EncodePatchHeader(BitPack output, TerrainPatch.Header header, int[] patch, bool largeRegion, ref int wbits)
|
private static void EncodePatchHeader(BitPack output, TerrainPatch.Header header, int[] patch, bool largeRegion, ref int wbits)
|
||||||
{
|
{
|
||||||
if (wbits > 17)
|
if (wbits > 17)
|
||||||
|
@ -348,85 +312,6 @@ namespace OpenSim.Region.ClientStack.LindenUDP
|
||||||
output.PackBits(header.PatchIDs, 10);
|
output.PackBits(header.PatchIDs, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void IDCTColumn16(float[] linein, float[] lineout, int column)
|
|
||||||
{
|
|
||||||
for (int n = 0; n < Constants.TerrainPatchSize; n++)
|
|
||||||
{
|
|
||||||
float total = OO_SQRT2 * linein[column];
|
|
||||||
|
|
||||||
for (int u = 1; u < Constants.TerrainPatchSize; u++)
|
|
||||||
{
|
|
||||||
int usize = u * Constants.TerrainPatchSize;
|
|
||||||
total += linein[usize + column] * CosineTable16[usize + n];
|
|
||||||
}
|
|
||||||
|
|
||||||
lineout[Constants.TerrainPatchSize * n + column] = total;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void IDCTLine16(float[] linein, float[] lineout, int line)
|
|
||||||
{
|
|
||||||
const float oosob = 2.0f / Constants.TerrainPatchSize;
|
|
||||||
int lineSize = line * Constants.TerrainPatchSize;
|
|
||||||
|
|
||||||
for (int n = 0; n < Constants.TerrainPatchSize; n++)
|
|
||||||
{
|
|
||||||
float total = OO_SQRT2 * linein[lineSize];
|
|
||||||
|
|
||||||
for (int u = 1; u < Constants.TerrainPatchSize; u++)
|
|
||||||
{
|
|
||||||
total += linein[lineSize + u] * CosineTable16[u * Constants.TerrainPatchSize + n];
|
|
||||||
}
|
|
||||||
|
|
||||||
lineout[lineSize + n] = total * oosob;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void DecodePatch(int[] patches, BitPack bitpack, TerrainPatch.Header header, int size)
|
|
||||||
{
|
|
||||||
for (int n = 0; n < size * size; n++)
|
|
||||||
{
|
|
||||||
// ?
|
|
||||||
int temp = bitpack.UnpackBits(1);
|
|
||||||
if (temp != 0)
|
|
||||||
{
|
|
||||||
// Value or EOB
|
|
||||||
temp = bitpack.UnpackBits(1);
|
|
||||||
if (temp != 0)
|
|
||||||
{
|
|
||||||
// Value
|
|
||||||
temp = bitpack.UnpackBits(1);
|
|
||||||
if (temp != 0)
|
|
||||||
{
|
|
||||||
// Negative
|
|
||||||
temp = bitpack.UnpackBits((int)header.WordBits);
|
|
||||||
patches[n] = temp * -1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Positive
|
|
||||||
temp = bitpack.UnpackBits((int)header.WordBits);
|
|
||||||
patches[n] = temp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Set the rest to zero
|
|
||||||
// TODO: This might not be necessary
|
|
||||||
for (int o = n; o < size * size; o++)
|
|
||||||
{
|
|
||||||
patches[o] = 0;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
patches[n] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void EncodePatch(BitPack output, int[] patch, int postquant, int wbits)
|
private static void EncodePatch(BitPack output, int[] patch, int postquant, int wbits)
|
||||||
{
|
{
|
||||||
int maxwbitssize = (1 << wbits) - 1;
|
int maxwbitssize = (1 << wbits) - 1;
|
||||||
|
@ -493,6 +378,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP
|
||||||
int prequant, out int wbits)
|
int prequant, out int wbits)
|
||||||
{
|
{
|
||||||
float[] block = new float[Constants.TerrainPatchSize * Constants.TerrainPatchSize];
|
float[] block = new float[Constants.TerrainPatchSize * Constants.TerrainPatchSize];
|
||||||
|
int[] iout = new int[Constants.TerrainPatchSize * Constants.TerrainPatchSize];
|
||||||
|
|
||||||
float oozrange = 1.0f / header.Range;
|
float oozrange = 1.0f / header.Range;
|
||||||
float invprequat = (1 << prequant);
|
float invprequat = (1 << prequant);
|
||||||
|
@ -505,27 +391,16 @@ namespace OpenSim.Region.ClientStack.LindenUDP
|
||||||
header.QuantWBits |= wordsize << 4;
|
header.QuantWBits |= wordsize << 4;
|
||||||
|
|
||||||
int k = 0;
|
int k = 0;
|
||||||
|
int startX = patchX * Constants.TerrainPatchSize;
|
||||||
int yPatchLimit = patchY >= (terrData.SizeY / Constants.TerrainPatchSize) ?
|
int startY = patchY * Constants.TerrainPatchSize;
|
||||||
(terrData.SizeY - Constants.TerrainPatchSize) / Constants.TerrainPatchSize : patchY;
|
for (int y = startY; y < startY + Constants.TerrainPatchSize; y++)
|
||||||
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++)
|
for (int x = startX; x < startX + Constants.TerrainPatchSize; x++)
|
||||||
{
|
{
|
||||||
block[k++] = (terrData[xx, yy] - sub) * premult;
|
block[k++] = (terrData[x, y] - sub) * premult;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
float[] ftemp = new float[Constants.TerrainPatchSize * Constants.TerrainPatchSize];
|
|
||||||
int[] iout = new int[Constants.TerrainPatchSize * Constants.TerrainPatchSize];
|
|
||||||
|
|
||||||
wbits = (prequant >> 1);
|
wbits = (prequant >> 1);
|
||||||
|
|
||||||
dct16x16(block, iout, ref wbits);
|
dct16x16(block, iout, ref wbits);
|
||||||
|
@ -558,19 +433,6 @@ namespace OpenSim.Region.ClientStack.LindenUDP
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void SetupCosines16()
|
|
||||||
{
|
|
||||||
const float hposz = (float)Math.PI * 0.5f / Constants.TerrainPatchSize;
|
|
||||||
|
|
||||||
for (int u = 0; u < Constants.TerrainPatchSize; u++)
|
|
||||||
{
|
|
||||||
for (int n = 0; n < Constants.TerrainPatchSize; n++)
|
|
||||||
{
|
|
||||||
CosineTable16[u * Constants.TerrainPatchSize + n] = (float)Math.Cos((2.0f * n + 1.0f) * u * hposz);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void BuildCopyMatrix16()
|
private static void BuildCopyMatrix16()
|
||||||
{
|
{
|
||||||
bool diag = false;
|
bool diag = false;
|
||||||
|
@ -622,7 +484,11 @@ namespace OpenSim.Region.ClientStack.LindenUDP
|
||||||
|
|
||||||
#endregion Initialization
|
#endregion Initialization
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#region DCT
|
#region DCT
|
||||||
|
|
||||||
/* DCT (Discrete Cosine Transform)
|
/* DCT (Discrete Cosine Transform)
|
||||||
adaptation from
|
adaptation from
|
||||||
General Purpose 2D,3D FFT (Fast Fourier Transform) Package
|
General Purpose 2D,3D FFT (Fast Fourier Transform) Package
|
||||||
|
@ -677,15 +543,14 @@ namespace OpenSim.Region.ClientStack.LindenUDP
|
||||||
float xr, xi;
|
float xr, xi;
|
||||||
float ftmp;
|
float ftmp;
|
||||||
|
|
||||||
|
int fullSize = Constants.TerrainPatchSize * Constants.TerrainPatchSize;
|
||||||
int itmp;
|
int itmp;
|
||||||
int j, k;
|
int j, k;
|
||||||
int indx;
|
int indx;
|
||||||
|
|
||||||
const int maxwbits = 17; // per header encoding
|
const int maxwbits = 17; // per header encoding
|
||||||
bool dowbits = wbits < 17;
|
|
||||||
int wbitsMaxValue = 1 << wbits;
|
int wbitsMaxValue = 1 << wbits;
|
||||||
|
bool dowbits = wbits < 17;
|
||||||
int fullSize = Constants.TerrainPatchSize * Constants.TerrainPatchSize;
|
|
||||||
|
|
||||||
for (j = 0, k = 0; j < fullSize; j += Constants.TerrainPatchSize, k++)
|
for (j = 0, k = 0; j < fullSize; j += Constants.TerrainPatchSize, k++)
|
||||||
{
|
{
|
||||||
|
@ -1166,7 +1031,132 @@ namespace OpenSim.Region.ClientStack.LindenUDP
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* not in use, and still not fixed
|
#endregion DCT
|
||||||
|
|
||||||
|
#region Decode
|
||||||
|
/*
|
||||||
|
public static TerrainPatch.Header DecodePatchHeader(BitPack bitpack)
|
||||||
|
{
|
||||||
|
TerrainPatch.Header header = new TerrainPatch.Header { QuantWBits = bitpack.UnpackBits(8) };
|
||||||
|
|
||||||
|
// Quantized word bits
|
||||||
|
if (header.QuantWBits == END_OF_PATCHES)
|
||||||
|
return header;
|
||||||
|
|
||||||
|
// DC offset
|
||||||
|
header.DCOffset = bitpack.UnpackFloat();
|
||||||
|
|
||||||
|
// Range
|
||||||
|
header.Range = bitpack.UnpackBits(16);
|
||||||
|
|
||||||
|
// Patch IDs (10 bits)
|
||||||
|
header.PatchIDs = bitpack.UnpackBits(10);
|
||||||
|
|
||||||
|
// Word bits
|
||||||
|
header.WordBits = (uint)((header.QuantWBits & 0x0f) + 2);
|
||||||
|
|
||||||
|
return header;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
public static void DecodePatch(int[] patches, BitPack bitpack, TerrainPatch.Header header, int size)
|
||||||
|
{
|
||||||
|
for (int n = 0; n < size * size; n++)
|
||||||
|
{
|
||||||
|
// ?
|
||||||
|
int temp = bitpack.UnpackBits(1);
|
||||||
|
if (temp != 0)
|
||||||
|
{
|
||||||
|
// Value or EOB
|
||||||
|
temp = bitpack.UnpackBits(1);
|
||||||
|
if (temp != 0)
|
||||||
|
{
|
||||||
|
// Value
|
||||||
|
temp = bitpack.UnpackBits(1);
|
||||||
|
if (temp != 0)
|
||||||
|
{
|
||||||
|
// Negative
|
||||||
|
temp = bitpack.UnpackBits((int)header.WordBits);
|
||||||
|
patches[n] = temp * -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Positive
|
||||||
|
temp = bitpack.UnpackBits((int)header.WordBits);
|
||||||
|
patches[n] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Set the rest to zero
|
||||||
|
// TODO: This might not be necessary
|
||||||
|
for (int o = n; o < size * size; o++)
|
||||||
|
{
|
||||||
|
patches[o] = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
patches[n] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
#region IDCT
|
||||||
|
/* not in use
|
||||||
|
private static void IDCTColumn16(float[] linein, float[] lineout, int column)
|
||||||
|
{
|
||||||
|
for (int n = 0; n < Constants.TerrainPatchSize; n++)
|
||||||
|
{
|
||||||
|
float total = OO_SQRT2 * linein[column];
|
||||||
|
|
||||||
|
for (int u = 1; u < Constants.TerrainPatchSize; u++)
|
||||||
|
{
|
||||||
|
int usize = u * Constants.TerrainPatchSize;
|
||||||
|
total += linein[usize + column] * CosineTable16[usize + n];
|
||||||
|
}
|
||||||
|
|
||||||
|
lineout[Constants.TerrainPatchSize * n + column] = total;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void IDCTLine16(float[] linein, float[] lineout, int line)
|
||||||
|
{
|
||||||
|
const float oosob = 2.0f / Constants.TerrainPatchSize;
|
||||||
|
int lineSize = line * Constants.TerrainPatchSize;
|
||||||
|
|
||||||
|
for (int n = 0; n < Constants.TerrainPatchSize; n++)
|
||||||
|
{
|
||||||
|
float total = OO_SQRT2 * linein[lineSize];
|
||||||
|
|
||||||
|
for (int u = 1; u < Constants.TerrainPatchSize; u++)
|
||||||
|
{
|
||||||
|
total += linein[lineSize + u] * CosineTable16[u * Constants.TerrainPatchSize + n];
|
||||||
|
}
|
||||||
|
|
||||||
|
lineout[lineSize + n] = total * oosob;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
private static void SetupCosines16()
|
||||||
|
{
|
||||||
|
const float hposz = (float)Math.PI * 0.5f / Constants.TerrainPatchSize;
|
||||||
|
|
||||||
|
for (int u = 0; u < Constants.TerrainPatchSize; u++)
|
||||||
|
{
|
||||||
|
for (int n = 0; n < Constants.TerrainPatchSize; n++)
|
||||||
|
{
|
||||||
|
CosineTable16[u * Constants.TerrainPatchSize + n] = (float)Math.Cos((2.0f * n + 1.0f) * u * hposz);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
//not in use, and still not fixed
|
||||||
|
/*
|
||||||
static void idct16x16(float[] a)
|
static void idct16x16(float[] a)
|
||||||
{
|
{
|
||||||
int j;
|
int j;
|
||||||
|
@ -1328,7 +1318,8 @@ namespace OpenSim.Region.ClientStack.LindenUDP
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
#endregion DCT
|
#endregion IDCT
|
||||||
|
#endregion Decode
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue