< Summary

Information
Class: SwiftCollections.Query.SwiftFixedSpatialHash<T>
Assembly: SwiftCollections.FixedMathSharp
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections.FixedMathSharp/Query/SpatialHash/SwiftFixedSpatialHash.cs
Line coverage
100%
Covered lines: 39
Uncovered lines: 0
Coverable lines: 39
Total lines: 111
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
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%11100%
.ctor(...)100%11100%
CollectPointCandidates(...)100%11100%
GetCellIndex(...)100%11100%
ToCell(...)100%1010100%
.ctor(...)100%22100%
GetCellRange(...)100%11100%
ToCell(...)100%11100%

File(s)

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections.FixedMathSharp/Query/SpatialHash/SwiftFixedSpatialHash.cs

#LineLine coverage
 1//=======================================================================
 2// SwiftFixedSpatialHash.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 System.Runtime.CompilerServices;
 10using FixedMathSharp;
 11
 12namespace SwiftCollections.Query;
 13
 14/// <summary>
 15/// Represents a fixed-point spatial hash optimized for deterministic broad-phase spatial queries.
 16/// </summary>
 17public class SwiftFixedSpatialHash<T> : SwiftSpatialHash<T, FixedBoundVolume>
 18{
 19    private readonly Fixed64 _cellSize;
 20
 21    /// <summary>
 22    /// Initializes a new instance of the <see cref="SwiftFixedSpatialHash{T}"/> class with the specified capacity and c
 23    /// </summary>
 24    public SwiftFixedSpatialHash(int capacity, Fixed64 cellSize)
 2625        : this(capacity, cellSize, SwiftSpatialHashOptions.Default) { }
 26
 27    /// <summary>
 28    /// Initializes a new instance of the <see cref="SwiftFixedSpatialHash{T}"/> class with the specified capacity, cell
 29    /// </summary>
 30    public SwiftFixedSpatialHash(int capacity, Fixed64 cellSize, SwiftSpatialHashOptions options)
 1431        : base(capacity, new FixedBoundVolumeCellMapper(cellSize), options)
 32    {
 1233        _cellSize = cellSize;
 1234    }
 35
 36    /// <summary>
 37    /// Collects broad-phase candidates registered in the spatial-hash cell
 38    /// containing a point. Callers remain responsible for exact filtering.
 39    /// </summary>
 40    /// <param name="point">The fixed-point position whose cell should be queried.</param>
 41    /// <param name="results">The caller-owned result collection.</param>
 42    public void CollectPointCandidates(Vector3d point, System.Collections.Generic.ICollection<T> results) =>
 843        CollectCellCandidates(GetCellIndex(point), results);
 44
 45    /// <summary>
 46    /// Maps a fixed-point position to its exact spatial-hash cell.
 47    /// </summary>
 48    /// <param name="point">The fixed-point position to map.</param>
 49    /// <returns>The containing spatial-hash cell.</returns>
 50    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 51    public SwiftSpatialHashCellIndex GetCellIndex(Vector3d point) =>
 952        new(
 953            ToCell(point.X, _cellSize),
 954            ToCell(point.Y, _cellSize),
 955            ToCell(point.Z, _cellSize));
 56
 57    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 58    private static int ToCell(Fixed64 value, Fixed64 cellSize)
 59    {
 14160        if ((uint)cellSize.m_rawValue == 0U)
 61        {
 10562            int wholeValue = (int)(value.m_rawValue >> 32);
 10563            int wholeCellSize = (int)(cellSize.m_rawValue >> 32);
 10564            int wholeQuotient = wholeValue / wholeCellSize;
 10565            return wholeValue % wholeCellSize < 0
 10566                ? wholeQuotient - 1
 10567                : wholeQuotient;
 68        }
 69
 3670        long quotient = value.m_rawValue / cellSize.m_rawValue;
 3671        if (value.m_rawValue % cellSize.m_rawValue < 0L)
 972            quotient--;
 73
 3674        if (quotient < int.MinValue)
 975            return int.MinValue;
 2776        if (quotient > int.MaxValue)
 977            return int.MaxValue;
 1878        return (int)quotient;
 79    }
 80
 81    private sealed class FixedBoundVolumeCellMapper : ISpatialHashCellMapper<FixedBoundVolume>
 82    {
 83        private readonly Fixed64 _cellSize;
 84
 1485        public FixedBoundVolumeCellMapper(Fixed64 cellSize)
 86        {
 1487            if (cellSize <= Fixed64.Zero)
 288                throw new ArgumentOutOfRangeException(nameof(cellSize), cellSize, "Cell size must be greater than zero."
 89
 1290            _cellSize = cellSize;
 1291        }
 92
 93        public void GetCellRange(FixedBoundVolume bounds, out SwiftSpatialHashCellIndex minCell, out SwiftSpatialHashCel
 94        {
 1995            minCell = new SwiftSpatialHashCellIndex(
 1996                ToCell(bounds.Min.X),
 1997                ToCell(bounds.Min.Y),
 1998                ToCell(bounds.Min.Z));
 99
 19100            maxCell = new SwiftSpatialHashCellIndex(
 19101                ToCell(bounds.Max.X),
 19102                ToCell(bounds.Max.Y),
 19103                ToCell(bounds.Max.Z));
 19104        }
 105
 106        private int ToCell(Fixed64 value)
 107        {
 114108            return SwiftFixedSpatialHash<T>.ToCell(value, _cellSize);
 109        }
 110    }
 111}