Skip to content

Commit 521b7cf

Browse files
committed
task: increase code coverage
1 parent df9356a commit 521b7cf

9 files changed

Lines changed: 612 additions & 41 deletions

File tree

src/GridForge/Grids/Managers/GridDirectionUtility.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ public static class GridDirectionUtility
1515
/// <returns>The corresponding <see cref="SpatialDirection"/>, or <see cref="SpatialDirection.None"/> if invalid.</returns>
1616
public static SpatialDirection GetNeighborDirectionFromOffset((int x, int y, int z) gridOffset)
1717
{
18-
Debug.Assert(gridOffset.x >= -1 && gridOffset.x <= 1, "Invalid x offset.");
19-
Debug.Assert(gridOffset.y >= -1 && gridOffset.y <= 1, "Invalid y offset.");
20-
Debug.Assert(gridOffset.z >= -1 && gridOffset.z <= 1, "Invalid z offset.");
18+
if(IsValidOffset(gridOffset) == false)
19+
{
20+
GridForgeLogger.DebugChannel.Info($"Invalid grid offset: {gridOffset}. Offsets must be in the range [-1, 1] for each axis.");
21+
return SpatialDirection.None;
22+
}
2123

22-
if (gridOffset == (0, 0, 0))
24+
if (gridOffset == (0, 0, 0)) // The center voxel does not correspond to any direction.
2325
return SpatialDirection.None;
2426

2527
for (int i = 0; i < SpatialAwareness.DirectionOffsets.Length; i++)
@@ -30,4 +32,11 @@ public static SpatialDirection GetNeighborDirectionFromOffset((int x, int y, int
3032

3133
return SpatialDirection.None;
3234
}
35+
36+
private static bool IsValidOffset((int x, int y, int z) offset)
37+
{
38+
return offset.x >= -1 && offset.x <= 1 &&
39+
offset.y >= -1 && offset.y <= 1 &&
40+
offset.z >= -1 && offset.z <= 1;
41+
}
3342
}

src/GridForge/Utility/GridTracer.cs

Lines changed: 120 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,41 @@ private static IEnumerable<GridVoxelSet> TraceLineIterator(
258258
SwiftDictionary<VoxelGrid, SwiftList<Voxel>> gridVoxelMapping = new();
259259
SwiftHashSet<Voxel> voxelRedundancyCheck = SwiftHashSetPool<Voxel>.Shared.Rent();
260260

261+
try
262+
{
263+
AddTraceLineVoxelsToMapping(
264+
world,
265+
start,
266+
end,
267+
padding,
268+
gridVoxelMapping,
269+
voxelRedundancyCheck);
270+
271+
AddTraceLineEndVoxel(
272+
world,
273+
end,
274+
includeEnd,
275+
gridVoxelMapping,
276+
voxelRedundancyCheck);
277+
278+
foreach (KeyValuePair<VoxelGrid, SwiftList<Voxel>> kvp in gridVoxelMapping)
279+
yield return new GridVoxelSet(kvp.Key, kvp.Value);
280+
}
281+
finally
282+
{
283+
ReleaseTraceLineMapping(gridVoxelMapping);
284+
SwiftHashSetPool<Voxel>.Shared.Release(voxelRedundancyCheck);
285+
}
286+
}
287+
288+
private static void AddTraceLineVoxelsToMapping(
289+
GridWorld world,
290+
Vector3d start,
291+
Vector3d end,
292+
Fixed64? padding,
293+
SwiftDictionary<VoxelGrid, SwiftList<Voxel>> gridVoxelMapping,
294+
SwiftHashSet<Voxel> voxelRedundancyCheck)
295+
{
261296
(Vector3d snappedMin, Vector3d snappedMax) =
262297
world.SnapBoundsToVoxelSize(start, end, padding);
263298

@@ -273,9 +308,7 @@ private static IEnumerable<GridVoxelSet> TraceLineIterator(
273308

274309
Vector3d diff = traceEnd - traceStart;
275310
Vector3d delta = Vector3d.Abs(diff);
276-
277311
Fixed64 steps = FixedMath.Ceiling(FixedMath.Max(FixedMath.Max(delta.x, delta.y), delta.z));
278-
279312
Fixed64 stepX = diff.x / (steps + Fixed64.One);
280313
Fixed64 stepY = diff.y / (steps + Fixed64.One);
281314
Fixed64 stepZ = diff.z / (steps + Fixed64.One);
@@ -285,49 +318,103 @@ private static IEnumerable<GridVoxelSet> TraceLineIterator(
285318
if (!world.SpatialGridHash.TryGetValue(cellIndex, out SwiftHashSet<ushort> gridList))
286319
continue;
287320

288-
foreach (ushort gridIndex in gridList)
289-
{
290-
if (!world.ActiveGrids.IsAllocated(gridIndex))
291-
continue;
292-
293-
VoxelGrid currentGrid = world.ActiveGrids[gridIndex];
294-
if (gridVoxelMapping.ContainsKey(currentGrid))
295-
continue;
321+
AddTraceLineVoxelsForCell(
322+
world,
323+
gridList,
324+
traceStart,
325+
steps,
326+
stepX,
327+
stepY,
328+
stepZ,
329+
gridVoxelMapping,
330+
voxelRedundancyCheck);
331+
}
332+
}
296333

297-
SwiftList<Voxel> voxelList = SwiftListPool<Voxel>.Shared.Rent();
298-
gridVoxelMapping.Add(currentGrid, voxelList);
334+
private static void AddTraceLineVoxelsForCell(
335+
GridWorld world,
336+
SwiftHashSet<ushort> gridList,
337+
Vector3d traceStart,
338+
Fixed64 steps,
339+
Fixed64 stepX,
340+
Fixed64 stepY,
341+
Fixed64 stepZ,
342+
SwiftDictionary<VoxelGrid, SwiftList<Voxel>> gridVoxelMapping,
343+
SwiftHashSet<Voxel> voxelRedundancyCheck)
344+
{
345+
foreach (ushort gridIndex in gridList)
346+
{
347+
if (!world.ActiveGrids.IsAllocated(gridIndex))
348+
continue;
299349

300-
for (Fixed64 i = Fixed64.Zero; i <= steps; i += Fixed64.One)
301-
{
302-
Vector3d tracePos = world.FloorToVoxelSize(
303-
new Vector3d(
304-
traceStart.x + stepX * i,
305-
traceStart.y + stepY * i,
306-
traceStart.z + stepZ * i));
350+
VoxelGrid currentGrid = world.ActiveGrids[gridIndex];
351+
if (gridVoxelMapping.ContainsKey(currentGrid))
352+
continue;
307353

308-
if (!currentGrid.TryGetVoxel(tracePos, out Voxel? voxel) || voxelRedundancyCheck.Add(voxel!) != true)
309-
continue;
354+
SwiftList<Voxel> voxelList = SwiftListPool<Voxel>.Shared.Rent();
355+
gridVoxelMapping.Add(currentGrid, voxelList);
310356

311-
voxelList.Add(voxel!);
312-
}
313-
}
357+
AddTraceLineGridVoxels(
358+
world,
359+
currentGrid,
360+
traceStart,
361+
steps,
362+
stepX,
363+
stepY,
364+
stepZ,
365+
voxelList,
366+
voxelRedundancyCheck);
314367
}
368+
}
315369

316-
if (includeEnd
317-
&& world.TryGetGridAndVoxel(end, out VoxelGrid? endGrid, out Voxel? endVoxel)
318-
&& gridVoxelMapping.TryGetValue(endGrid!, out SwiftList<Voxel> endVoxelList)
319-
&& voxelRedundancyCheck.Add(endVoxel!))
370+
private static void AddTraceLineGridVoxels(
371+
GridWorld world,
372+
VoxelGrid currentGrid,
373+
Vector3d traceStart,
374+
Fixed64 steps,
375+
Fixed64 stepX,
376+
Fixed64 stepY,
377+
Fixed64 stepZ,
378+
SwiftList<Voxel> voxelList,
379+
SwiftHashSet<Voxel> voxelRedundancyCheck)
380+
{
381+
for (Fixed64 i = Fixed64.Zero; i <= steps; i += Fixed64.One)
320382
{
321-
endVoxelList.Add(endVoxel!);
383+
Vector3d tracePos = world.FloorToVoxelSize(
384+
new Vector3d(
385+
traceStart.x + stepX * i,
386+
traceStart.y + stepY * i,
387+
traceStart.z + stepZ * i));
388+
389+
if (!currentGrid.TryGetVoxel(tracePos, out Voxel? voxel) || voxelRedundancyCheck.Add(voxel!) != true)
390+
continue;
391+
392+
voxelList.Add(voxel!);
322393
}
394+
}
323395

324-
foreach (KeyValuePair<VoxelGrid, SwiftList<Voxel>> kvp in gridVoxelMapping)
396+
private static void AddTraceLineEndVoxel(
397+
GridWorld world,
398+
Vector3d end,
399+
bool includeEnd,
400+
SwiftDictionary<VoxelGrid, SwiftList<Voxel>> gridVoxelMapping,
401+
SwiftHashSet<Voxel> voxelRedundancyCheck)
402+
{
403+
if (!includeEnd
404+
|| !world.TryGetGridAndVoxel(end, out VoxelGrid? endGrid, out Voxel? endVoxel)
405+
|| !gridVoxelMapping.TryGetValue(endGrid!, out SwiftList<Voxel> endVoxelList)
406+
|| !voxelRedundancyCheck.Add(endVoxel!))
325407
{
326-
yield return new GridVoxelSet(kvp.Key, kvp.Value);
327-
SwiftListPool<Voxel>.Shared.Release(kvp.Value);
408+
return;
328409
}
329410

330-
SwiftHashSetPool<Voxel>.Shared.Release(voxelRedundancyCheck);
411+
endVoxelList.Add(endVoxel!);
412+
}
413+
414+
private static void ReleaseTraceLineMapping(SwiftDictionary<VoxelGrid, SwiftList<Voxel>> gridVoxelMapping)
415+
{
416+
foreach (KeyValuePair<VoxelGrid, SwiftList<Voxel>> kvp in gridVoxelMapping)
417+
SwiftListPool<Voxel>.Shared.Release(kvp.Value);
331418
}
332419

333420
private static IEnumerable<GridVoxelSet> GetCoveredVoxelsIterator(

tests/GridForge.Benchmarks/GridForge.Benchmarks.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
<ProjectReference Include="..\..\src\GridForge\GridForge.csproj" />
1717
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
1818
<PackageReference Include="FixedMathSharp" Version="3.0.1" />
19-
<PackageReference Include="SwiftCollections" Version="4.0.3" />
19+
<PackageReference Include="SwiftCollections" Version="4.0.4" />
2020
<PackageReference Include="System.Text.Json" Version="10.0.7" />
2121
<!-- The Test SDK is required only for the VSTest Adapter to work -->
2222
</ItemGroup>
2323
<ItemGroup>
2424
<None Remove=".gitignore" />
2525
</ItemGroup>
26-
</Project>
26+
</Project>

tests/GridForge.Tests/GridForge.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</PackageReference>
2525
<PackageReference Include="FixedMathSharp" Version="3.0.1" />
2626
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.5.1" />
27-
<PackageReference Include="SwiftCollections" Version="4.0.3" />
27+
<PackageReference Include="SwiftCollections" Version="4.0.4" />
2828
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
2929
<PrivateAssets>all</PrivateAssets>
3030
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@@ -38,4 +38,4 @@
3838
<ItemGroup>
3939
<None Remove=".gitignore" />
4040
</ItemGroup>
41-
</Project>
41+
</Project>

0 commit comments

Comments
 (0)