From 67fb861ac1628cc9fa1e0e7c1757894bf50a7ee7 Mon Sep 17 00:00:00 2001 From: ikpil Date: Sat, 19 Aug 2023 13:03:00 +0900 Subject: [PATCH] add DtTileCacheLayerBuilder --- ...sult.cs => DtTileCacheLayerBuildResult.cs} | 8 +++---- .../DtTileCacheLayerBuilder.cs | 24 +++++++++---------- .../TestTileLayerBuilder.cs | 6 ++++- 3 files changed, 21 insertions(+), 17 deletions(-) rename src/DotRecast.Detour.TileCache/{DtTileCacheBuildResult.cs => DtTileCacheLayerBuildResult.cs} (55%) diff --git a/src/DotRecast.Detour.TileCache/DtTileCacheBuildResult.cs b/src/DotRecast.Detour.TileCache/DtTileCacheLayerBuildResult.cs similarity index 55% rename from src/DotRecast.Detour.TileCache/DtTileCacheBuildResult.cs rename to src/DotRecast.Detour.TileCache/DtTileCacheLayerBuildResult.cs index eb90d3a..661fb2e 100644 --- a/src/DotRecast.Detour.TileCache/DtTileCacheBuildResult.cs +++ b/src/DotRecast.Detour.TileCache/DtTileCacheLayerBuildResult.cs @@ -3,17 +3,17 @@ using System.Threading.Tasks; namespace DotRecast.Detour.TileCache { - public class DtTileCacheBuildResult + public class DtTileCacheLayerBuildResult { public readonly int tx; public readonly int ty; - public readonly Task> task; + public readonly List layers; - public DtTileCacheBuildResult(int tx, int ty, Task> task) + public DtTileCacheLayerBuildResult(int tx, int ty, List layers) { this.tx = tx; this.ty = ty; - this.task = task; + this.layers = layers; } } } \ No newline at end of file diff --git a/src/DotRecast.Detour.TileCache/DtTileCacheLayerBuilder.cs b/src/DotRecast.Detour.TileCache/DtTileCacheLayerBuilder.cs index 5f2a50b..5a1a517 100644 --- a/src/DotRecast.Detour.TileCache/DtTileCacheLayerBuilder.cs +++ b/src/DotRecast.Detour.TileCache/DtTileCacheLayerBuilder.cs @@ -39,7 +39,7 @@ namespace DotRecast.Detour.TileCache _compFactory = compFactory; } - public List Build(IInputGeomProvider geom, RcConfig cfg, RcByteOrder order, bool cCompatibility, int threads, int tw, int th) + public List Build(IInputGeomProvider geom, RcConfig cfg, RcByteOrder order, bool cCompatibility, int threads, int tw, int th) { if (threads == 1) { @@ -49,25 +49,25 @@ namespace DotRecast.Detour.TileCache return BuildMultiThread(geom, cfg, order, cCompatibility, tw, th, threads); } - private List BuildSingleThread(IInputGeomProvider geom, RcConfig cfg, RcByteOrder order, bool cCompatibility, int tw, int th) + private List BuildSingleThread(IInputGeomProvider geom, RcConfig cfg, RcByteOrder order, bool cCompatibility, int tw, int th) { - List layers = new List(); + var results = new List(); for (int y = 0; y < th; ++y) { for (int x = 0; x < tw; ++x) { - var list = BuildTileCacheLayer(geom, cfg, x, y, order, cCompatibility); - layers.AddRange(list); + var result = BuildTileCacheLayer(geom, cfg, x, y, order, cCompatibility); + results.Add(result); } } - return layers; + return results; } - private List BuildMultiThread(IInputGeomProvider geom, RcConfig cfg, RcByteOrder order, bool cCompatibility, int tw, int th, int threads) + private List BuildMultiThread(IInputGeomProvider geom, RcConfig cfg, RcByteOrder order, bool cCompatibility, int tw, int th, int threads) { - var results = new List(); + var results = new List>(); for (int y = 0; y < th; ++y) { for (int x = 0; x < tw; ++x) @@ -75,12 +75,12 @@ namespace DotRecast.Detour.TileCache int tx = x; int ty = y; var task = Task.Run(() => BuildTileCacheLayer(geom, cfg, tx, ty, order, cCompatibility)); - results.Add(new DtTileCacheBuildResult(tx, ty, task)); + results.Add(task); } } return results - .SelectMany(x => x.task.Result) + .Select(x => x.Result) .ToList(); } @@ -94,7 +94,7 @@ namespace DotRecast.Detour.TileCache return lset; } - protected virtual List BuildTileCacheLayer(IInputGeomProvider geom, RcConfig cfg, int tx, int ty, RcByteOrder order, bool cCompatibility) + protected virtual DtTileCacheLayerBuildResult BuildTileCacheLayer(IInputGeomProvider geom, RcConfig cfg, int tx, int ty, RcByteOrder order, bool cCompatibility) { RcHeightfieldLayerSet lset = BuildHeightfieldLayerSet(geom, cfg, tx, ty); List result = new List(); @@ -133,7 +133,7 @@ namespace DotRecast.Detour.TileCache } } - return result; + return new DtTileCacheLayerBuildResult(tx, ty, result); } } } \ No newline at end of file diff --git a/test/DotRecast.Detour.TileCache.Test/TestTileLayerBuilder.cs b/test/DotRecast.Detour.TileCache.Test/TestTileLayerBuilder.cs index 52e01f4..d5bb28e 100644 --- a/test/DotRecast.Detour.TileCache.Test/TestTileLayerBuilder.cs +++ b/test/DotRecast.Detour.TileCache.Test/TestTileLayerBuilder.cs @@ -19,6 +19,7 @@ freely, subject to the following restrictions: */ using System.Collections.Generic; +using System.Linq; using DotRecast.Core; using DotRecast.Detour.TileCache.Io.Compress; using DotRecast.Detour.TileCache.Test.Io; @@ -70,6 +71,9 @@ public class TestTileLayerBuilder : DtTileCacheLayerBuilder public List Build(RcByteOrder order, bool cCompatibility, int threads) { - return Build(_geom, _cfg, order, cCompatibility, threads, tw, th); + var results = Build(_geom, _cfg, order, cCompatibility, threads, tw, th); + return results + .SelectMany(x => x.layers) + .ToList(); } } \ No newline at end of file