add TerrainTaintsArray, a specialized BitArray for terrain taints. Start using it

This commit is contained in:
UbitUmarov
2022-03-29 22:18:28 +01:00
parent c11827e1e2
commit 3f30f17187
3 changed files with 388 additions and 127 deletions

View File

@@ -74,9 +74,12 @@ namespace OpenSim.Framework
private float[,] m_heightmap;
// Remember subregions of the heightmap that has changed.
private BitArray m_taint;
private TerrainTaintsArray m_taints;
private readonly int m_taintSizeX;
private readonly int m_taintSizeY;
private readonly int m_mapStride;
private readonly int m_mapPatchsStride;
// legacy CompressionFactor
public float CompressionFactor { get; private set; }
@@ -107,7 +110,7 @@ namespace OpenSim.Framework
{
m_heightmap[x, y] = value;
int yy = y / Constants.TerrainPatchSize;
m_taint[x / Constants.TerrainPatchSize + yy * m_taintSizeX] = true;
m_taints.Set(x / Constants.TerrainPatchSize + yy * m_taintSizeX, true);
}
}
}
@@ -118,19 +121,24 @@ namespace OpenSim.Framework
set { this[x, y] = value; }
}
public TerrainTaintsArray GetTaints()
{
return m_taints;
}
public void ClearTaint()
{
m_taint.SetAll(false);
m_taints.SetAll(false);
}
public void TaintAllTerrain()
{
m_taint.SetAll(true);
m_taints.SetAll(true);
}
private void SetAllTaint(bool setting)
{
m_taint.SetAll(setting);
m_taints.SetAll(setting);
}
public void ClearLand()
@@ -145,6 +153,12 @@ namespace OpenSim.Framework
m_heightmap[xx, yy] = pHeight;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public bool IsTainted()
{
return m_taints.IsTaited();
}
// Return 'true' of the patch that contains these region coordinates has been modified.
// Note that checking the taint clears it.
// There is existing code that relies on this feature.
@@ -153,59 +167,51 @@ namespace OpenSim.Framework
{
yy /= Constants.TerrainPatchSize;
int indx = xx / Constants.TerrainPatchSize + yy * m_taintSizeX;
bool ret = m_taint[indx];
if (ret && clearOnTest)
m_taint[indx] = false;
return ret;
return m_taints.Get(indx, clearOnTest);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public bool IsTaintedAt(int xx, int yy)
{
yy /= Constants.TerrainPatchSize;
return m_taint[xx / Constants.TerrainPatchSize + yy * m_taintSizeX];
return m_taints.Get(xx / Constants.TerrainPatchSize + yy * m_taintSizeX);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public bool IsTaintedAtPatch(int xx, int yy, bool clearOnTest)
{
int indx = xx + yy * m_taintSizeX;
bool ret = m_taint[indx];
if (ret && clearOnTest)
m_taint[indx] = false;
return ret;
return m_taints.Get(indx, clearOnTest);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public bool IsTaintedAtPatch(int xx, int yy)
{
return m_taint[xx + yy * m_taintSizeX];
return m_taints.Get(xx + yy * m_taintSizeX);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public bool IsTaintedAtPatch(int indx, bool clearOnTest)
{
bool ret = m_taint[indx];
if (ret && clearOnTest)
m_taint[indx] = false;
return ret;
return m_taints.Get(indx, clearOnTest);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public bool IsTaintedAtPatchWithClear(int indx)
{
if(m_taint[indx])
{
m_taint[indx] = false;
return true;
}
return false;
return m_taints.GetAndClear(indx);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public bool IsTaintedAtPatch(int indx)
{
return m_taint[indx];
return m_taints.Get(indx);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public int GetAndClearNextTaint(int startIndex)
{
return m_taints.GetAndClearNextTrue(startIndex);
}
// TerrainData.GetDatabaseBlob
// The user wants something to store in the database.
@@ -233,6 +239,7 @@ namespace OpenSim.Framework
{
TerrainData ret = new TerrainData(SizeX, SizeY, SizeZ);
ret.m_heightmap = (float[,])this.m_heightmap.Clone();
return ret;
}
@@ -266,14 +273,12 @@ namespace OpenSim.Framework
zmax = float.MinValue;
zmin = float.MaxValue;
int stride = m_heightmap.GetLength(1);
int mstride = 16 * stride;
int mpy = 16 * py;
int mpy = Constants.TerrainPatchSize * py;
fixed (float* map = m_heightmap)
{
float* p = map + px * mstride;
float* pend = p + mstride;
float* p = map + px * m_mapPatchsStride;
float* pend = p + m_mapPatchsStride;
while (p < pend)
{
float* yt = p + mpy;
@@ -287,7 +292,7 @@ namespace OpenSim.Framework
zmin = val;
yt++;
}
p += stride;
p += m_mapStride;
}
}
}
@@ -295,12 +300,9 @@ namespace OpenSim.Framework
public unsafe void GetPatchBlock(float* block, int px, int py, float sub, float premult)
{
int k = 0;
int stride = m_heightmap.GetLength(1);
int mstride = 16 * stride;
int startX = px * mstride;
int endX = startX + mstride;
int mpy = 16 * py;
int startX = px * m_mapPatchsStride;
int endX = startX + m_mapPatchsStride;
int mpy = py * Constants.TerrainPatchSize;
fixed (float* map = m_heightmap)
{
float* yp = map + mpy;
@@ -308,7 +310,7 @@ namespace OpenSim.Framework
while (yp < yend)
{
for (int x = startX; x < endX; x += stride)
for (int x = startX; x < endX; x += m_mapStride)
{
block[k++] = (yp[x] - sub) * premult;
}
@@ -362,9 +364,13 @@ namespace OpenSim.Framework
m_taintSizeX = SizeX / Constants.TerrainPatchSize;
m_taintSizeY = SizeY / Constants.TerrainPatchSize;
m_mapStride = SizeY;
m_mapPatchsStride = m_mapStride * Constants.TerrainPatchSize;
SizeZ = (int)Constants.RegionHeight;
CompressionFactor = 100.0f;
m_heightmap = new float[SizeX, SizeY];
for (int ii = 0; ii < SizeX; ii++)
{
@@ -376,7 +382,7 @@ namespace OpenSim.Framework
}
// m_log.DebugFormat("{0} new by doubles. sizeX={1}, sizeY={2}, sizeZ={3}", LogHeader, SizeX, SizeY, SizeZ);
m_taint = new BitArray(m_taintSizeX * m_taintSizeY, false);
m_taints = new TerrainTaintsArray(m_taintSizeX * m_taintSizeY);
}
// Create underlying structures but don't initialize the heightmap assuming the caller will immediately do that
@@ -387,9 +393,12 @@ namespace OpenSim.Framework
SizeZ = pZ;
m_taintSizeX = SizeX / Constants.TerrainPatchSize;
m_taintSizeY = SizeY / Constants.TerrainPatchSize;
m_mapStride = SizeY;
m_mapPatchsStride = m_mapStride * Constants.TerrainPatchSize;
CompressionFactor = 100.0f;
m_heightmap = new float[SizeX, SizeY];
m_taint = new BitArray(m_taintSizeX * m_taintSizeY, false);
m_taints = new TerrainTaintsArray(m_taintSizeX * m_taintSizeY);
// m_log.DebugFormat("{0} new by dimensions. sizeX={1}, sizeY={2}, sizeZ={3}", LogHeader, SizeX, SizeY, SizeZ);
ClearLand(0f);