Skip to content

Commit

Permalink
Changed 'heights', 'areas', 'cons', and 'regs' arrays to byte arrays …
Browse files Browse the repository at this point in the history
…for uniformity and efficiency in DtTileCacheLayer
  • Loading branch information
ikpil committed Jun 10, 2024
1 parent 3417eca commit 65c572a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Changed
- Changed to reuse samples and edges list in BuildPolyDetail()
- Changed 'heights', 'areas', 'cons', and 'regs' arrays to byte arrays for uniformity and efficiency in DtTileCacheLayer

### Removed
- Removed RcMeshDetails.VdistSq2(float[], float[])
Expand Down
6 changes: 3 additions & 3 deletions src/DotRecast.Detour.TileCache/DtLayerMonotoneRegion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class DtLayerMonotoneRegion
public const int DT_LAYER_MAX_NEIS = 16;

public int area;
public List<int> neis = new List<int>(DT_LAYER_MAX_NEIS);
public int regId;
public int areaId;
public List<byte> neis = new List<byte>(DT_LAYER_MAX_NEIS);
public byte regId;
public byte areaId;
};
}
50 changes: 25 additions & 25 deletions src/DotRecast.Detour.TileCache/DtTileCacheBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static void BuildTileCacheRegions(DtTileCacheLayer layer, int walkableCli
int w = layer.header.width;
int h = layer.header.height;

Array.Fill(layer.regs, (short)0x00FF);
Array.Fill(layer.regs, (byte)0xFF);
int nsweeps = w;
RcLayerSweepSpan[] sweeps = new RcLayerSweepSpan[nsweeps];
for (int i = 0; i < sweeps.Length; i++)
Expand All @@ -53,7 +53,7 @@ public static void BuildTileCacheRegions(DtTileCacheLayer layer, int walkableCli

// Partition walkable area into monotone regions.
int[] prevCount = new int[256];
int regId = 0;
byte regId = 0;

for (int y = 0; y < h; ++y)
{
Expand Down Expand Up @@ -92,7 +92,7 @@ public static void BuildTileCacheRegions(DtTileCacheLayer layer, int walkableCli
int yidx = x + (y - 1) * w;
if (y > 0 && IsConnected(layer, idx, yidx, walkableClimb))
{
int nr = layer.regs[yidx];
byte nr = layer.regs[yidx];
if (nr != 0xff)
{
// Set neighbour when first valid neighbour is
Expand Down Expand Up @@ -146,12 +146,12 @@ public static void BuildTileCacheRegions(DtTileCacheLayer layer, int walkableCli
{
int idx = x + y * w;
if (layer.regs[idx] != 0xff)
layer.regs[idx] = (short)sweeps[layer.regs[idx]].id;
layer.regs[idx] = sweeps[layer.regs[idx]].id;
}
}

// Allocate and init layer regions.
int nregs = regId;
byte nregs = regId;
DtLayerMonotoneRegion[] regs = new DtLayerMonotoneRegion[nregs];

for (int i = 0; i < nregs; ++i)
Expand All @@ -166,7 +166,7 @@ public static void BuildTileCacheRegions(DtTileCacheLayer layer, int walkableCli
for (int x = 0; x < w; ++x)
{
int idx = x + y * w;
int ri = layer.regs[idx];
byte ri = layer.regs[idx];
if (ri == 0xff)
continue;

Expand All @@ -178,7 +178,7 @@ public static void BuildTileCacheRegions(DtTileCacheLayer layer, int walkableCli
int ymi = x + (y - 1) * w;
if (y > 0 && IsConnected(layer, idx, ymi, walkableClimb))
{
int rai = layer.regs[ymi];
byte rai = layer.regs[ymi];
if (rai != 0xff && rai != ri)
{
AddUniqueLast(regs[ri].neis, rai);
Expand All @@ -188,7 +188,7 @@ public static void BuildTileCacheRegions(DtTileCacheLayer layer, int walkableCli
}
}

for (int i = 0; i < nregs; ++i)
for (byte i = 0; i < nregs; ++i)
regs[i].regId = i;

for (int i = 0; i < nregs; ++i)
Expand Down Expand Up @@ -217,15 +217,15 @@ public static void BuildTileCacheRegions(DtTileCacheLayer layer, int walkableCli
if (merge != -1)
{
int oldId = reg.regId;
int newId = regs[merge].regId;
byte newId = regs[merge].regId;
for (int j = 0; j < nregs; ++j)
if (regs[j].regId == oldId)
regs[j].regId = newId;
}
}

// Compact ids.
int[] remap = new int[256];
byte[] remap = new byte[256];
// Find number of unique regions.
regId = 0;
for (int i = 0; i < nregs; ++i)
Expand All @@ -242,11 +242,11 @@ public static void BuildTileCacheRegions(DtTileCacheLayer layer, int walkableCli
for (int i = 0; i < w * h; ++i)
{
if (layer.regs[i] != 0xff)
layer.regs[i] = (short)regs[layer.regs[i]].regId;
layer.regs[i] = regs[layer.regs[i]].regId;
}
}

public static void AddUniqueLast(List<int> a, int v)
public static void AddUniqueLast(List<byte> a, byte v)
{
int n = a.Count;
if (n > 0 && a[n - 1] == v)
Expand Down Expand Up @@ -1800,7 +1800,7 @@ public static DtTileCachePolyMesh BuildTileCachePolyMesh(DtTileCacheContourSet l
return mesh;
}

public static void MarkCylinderArea(DtTileCacheLayer layer, RcVec3f orig, float cs, float ch, RcVec3f pos, float radius, float height, int areaId)
public static void MarkCylinderArea(DtTileCacheLayer layer, RcVec3f orig, float cs, float ch, RcVec3f pos, float radius, float height, byte areaId)
{
RcVec3f bmin = new RcVec3f();
RcVec3f bmax = new RcVec3f();
Expand Down Expand Up @@ -1856,12 +1856,12 @@ public static void MarkCylinderArea(DtTileCacheLayer layer, RcVec3f orig, float
int y = layer.heights[x + z * w];
if (y < miny || y > maxy)
continue;
layer.areas[x + z * w] = (short)areaId;
layer.areas[x + z * w] = areaId;
}
}
}

public static void MarkBoxArea(DtTileCacheLayer layer, RcVec3f orig, float cs, float ch, RcVec3f bmin, RcVec3f bmax, int areaId)
public static void MarkBoxArea(DtTileCacheLayer layer, RcVec3f orig, float cs, float ch, RcVec3f bmin, RcVec3f bmax, byte areaId)
{
int w = layer.header.width;
int h = layer.header.height;
Expand Down Expand Up @@ -1900,7 +1900,7 @@ public static void MarkBoxArea(DtTileCacheLayer layer, RcVec3f orig, float cs, f
int y = layer.heights[x + z * w];
if (y < miny || y > maxy)
continue;
layer.areas[x + z * w] = (short)areaId;
layer.areas[x + z * w] = areaId;
}
}
}
Expand Down Expand Up @@ -1975,22 +1975,22 @@ public static DtTileCacheLayer DecompressTileCacheLayer(IRcCompressor comp, byte

int gridSize = layer.header.width * layer.header.height;
byte[] grids = comp.Decompress(compressed, buf.Position(), compressed.Length - buf.Position(), gridSize * 3);
layer.heights = new short[gridSize];
layer.areas = new short[gridSize];
layer.cons = new short[gridSize];
layer.regs = new short[gridSize];
layer.heights = new byte[gridSize];
layer.areas = new byte[gridSize];
layer.cons = new byte[gridSize];
layer.regs = new byte[gridSize];
for (int i = 0; i < gridSize; i++)
{
layer.heights[i] = (short)(grids[i] & 0xFF);
layer.areas[i] = (short)(grids[i + gridSize] & 0xFF);
layer.cons[i] = (short)(grids[i + gridSize * 2] & 0xFF);
layer.heights[i] = (byte)(grids[i] & 0xFF);
layer.areas[i] = (byte)(grids[i + gridSize] & 0xFF);
layer.cons[i] = (byte)(grids[i + gridSize * 2] & 0xFF);
}

return layer;
}

public static void MarkBoxArea(DtTileCacheLayer layer, RcVec3f orig, float cs, float ch, RcVec3f center, RcVec3f extents,
float[] rotAux, int areaId)
float[] rotAux, byte areaId)
{
int w = layer.header.width;
int h = layer.header.height;
Expand Down Expand Up @@ -2043,7 +2043,7 @@ public static void MarkBoxArea(DtTileCacheLayer layer, RcVec3f orig, float cs, f
int y = layer.heights[x + z * w];
if (y < miny || y > maxy)
continue;
layer.areas[x + z * w] = (short)areaId;
layer.areas[x + z * w] = areaId;
}
}
}
Expand Down
13 changes: 5 additions & 8 deletions src/DotRecast.Detour.TileCache/DtTileCacheLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ namespace DotRecast.Detour.TileCache
public class DtTileCacheLayer
{
public DtTileCacheLayerHeader header;
public int regCount;

/// < Region count.
public short[] heights; // char

public short[] areas; // char
public short[] cons; // char
public short[] regs; // char
public byte regCount; // < Region count.
public byte[] heights; // unsigned char
public byte[] areas; // unsigned char
public byte[] cons; // unsigned char
public byte[] regs; // unsigned char
}
}
4 changes: 2 additions & 2 deletions src/DotRecast.Recast/RcLayerSweepSpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class RcLayerSweepSpan
{
public int ns; // number samples
public int id; // region id
public int nei; // neighbour id
public byte id; // region id
public byte nei; // neighbour id
};
}
14 changes: 7 additions & 7 deletions src/DotRecast.Recast/RcLayers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ public static bool BuildHeightfieldLayers(RcContext ctx, RcCompactHeightfield ch
int w = chf.width;
int h = chf.height;

int[] srcReg = new int[chf.spanCount];
Array.Fill(srcReg, 0xFF);
byte[] srcReg = new byte[chf.spanCount];
Array.Fill(srcReg, (byte)0xFF);

int nsweeps = chf.width; // Math.Max(chf.width, chf.height);
int nsweeps = chf.width;
RcLayerSweepSpan[] sweeps = new RcLayerSweepSpan[nsweeps];
for (int i = 0; i < sweeps.Length; i++)
{
Expand All @@ -92,14 +92,14 @@ public static bool BuildHeightfieldLayers(RcContext ctx, RcCompactHeightfield ch

// Partition walkable area into monotone regions.
int[] prevCount = new int[256];
int regId = 0;
byte regId = 0;

// Sweep one line at a time.
for (int y = borderSize; y < h - borderSize; ++y)
{
// Collect spans from this row.
Array.Fill(prevCount, 0);
int sweepId = 0;
byte sweepId = 0;

for (int x = borderSize; x < w - borderSize; ++x)
{
Expand All @@ -111,7 +111,7 @@ public static bool BuildHeightfieldLayers(RcContext ctx, RcCompactHeightfield ch
if (chf.areas[i] == RC_NULL_AREA)
continue;

int sid = 0xFF;
byte sid = 0xFF;

// -x
if (GetCon(ref s, 0) != RC_NOT_CONNECTED)
Expand All @@ -136,7 +136,7 @@ public static bool BuildHeightfieldLayers(RcContext ctx, RcCompactHeightfield ch
int ax = x + GetDirOffsetX(3);
int ay = y + GetDirOffsetY(3);
int ai = chf.cells[ax + ay * w].index + GetCon(ref s, 3);
int nr = srcReg[ai];
byte nr = srcReg[ai];
if (nr != 0xff)
{
// Set neighbour when first valid neighbour is encoutered.
Expand Down

0 comments on commit 65c572a

Please sign in to comment.