< Summary

Information
Class: Trailblazer.Pathing.NavigationBakedCellLookup
Assembly: Trailblazer
File(s): /home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/Graph/NavigationBakedCellLookup.cs
Line coverage
100%
Covered lines: 23
Uncovered lines: 0
Coverable lines: 23
Total lines: 68
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%22100%
get_RetainedBytes()100%22100%
Create(...)100%66100%
Find(...)100%44100%
GetLinearAddress(...)100%11100%

File(s)

/home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/Graph/NavigationBakedCellLookup.cs

#LineLine coverage
 1//=======================================================================
 2// NavigationBakedCellLookup.cs
 3//=======================================================================
 4// MIT License, Copyright (c) 2024-present David Oravsky (mrdav30)
 5// See LICENSE file in the project root for full license information.
 6//=======================================================================
 7
 8using System;
 9using GridForge.Spatial;
 10
 11namespace Trailblazer.Pathing;
 12
 13/// <summary>Identifies the measured baked-cell lookup representation.</summary>
 14public enum NavigationCellLookupKind
 15{
 16    /// <summary>Canonical baked entries are searched without address-volume storage.</summary>
 17    Sorted = 0,
 18    /// <summary>A direct address table is cheaper at the authored navigation density.</summary>
 19    Direct = 1
 20}
 21
 22/// <summary>Selects lookup representation from authored density, independent of GridForge storage kind.</summary>
 23internal sealed class NavigationBakedCellLookup
 24{
 25    // Keep the direct table below 4 MiB per map and require at least 50% authored density.
 26    internal const long MaximumDirectTableBytes = 4_194_304;
 27    internal const int MinimumDirectDensityDivisor = 2;
 28    private readonly NavigationMap _map;
 29    private readonly int[]? _directSlots;
 30
 202731    private NavigationBakedCellLookup(NavigationMap map, int[]? directSlots)
 32    {
 202733        _map = map;
 202734        _directSlots = directSlots;
 202735        Kind = directSlots == null ? NavigationCellLookupKind.Sorted : NavigationCellLookupKind.Direct;
 202736    }
 37
 38    internal NavigationCellLookupKind Kind { get; }
 39
 3808840    internal long RetainedBytes => _directSlots == null ? 16L : 24L + ((long)_directSlots.Length * sizeof(int));
 41
 42    internal static NavigationBakedCellLookup Create(NavigationMap map)
 43    {
 202744        int addressCount = map.GridBinding.AddressCount;
 202745        long directBytes = (long)addressCount * sizeof(int);
 202746        if (addressCount > map.CellSpan.Length * MinimumDirectDensityDivisor
 202747            || directBytes > MaximumDirectTableBytes)
 116148            return new NavigationBakedCellLookup(map, null);
 49
 86650        var slots = new int[addressCount];
 86651        Array.Fill(slots, -1);
 3871052        for (int i = 0; i < map.CellSpan.Length; i++)
 1848953            slots[GetLinearAddress(map, map.CellSpan[i].Index)] = i;
 86654        return new NavigationBakedCellLookup(map, slots);
 55    }
 56
 57    internal int Find(VoxelIndex index)
 58    {
 469160959        if (_directSlots == null)
 372165360            return _map.FindCellIndex(index);
 96995661        if (!_map.GridBinding.IsValidIndex(index))
 262            return -1;
 96995463        return _directSlots[GetLinearAddress(_map, index)];
 64    }
 65
 98844366    private static int GetLinearAddress(NavigationMap map, VoxelIndex index) => checked(
 98844367        ((index.x * map.GridBinding.Height) + index.y) * map.GridBinding.Length + index.z);
 68}