| | | 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 | | |
| | | 8 | | using FixedMathSharp; |
| | | 9 | | using System; |
| | | 10 | | |
| | | 11 | | namespace SwiftCollections.Query; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Represents a fixed-point spatial hash optimized for deterministic broad-phase spatial queries. |
| | | 15 | | /// </summary> |
| | | 16 | | public class SwiftFixedSpatialHash<T> : SwiftSpatialHash<T, FixedBoundVolume> |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initializes a new instance of the <see cref="SwiftFixedSpatialHash{T}"/> class with the specified capacity and c |
| | | 20 | | /// </summary> |
| | | 21 | | public SwiftFixedSpatialHash(int capacity, Fixed64 cellSize) |
| | 6 | 22 | | : this(capacity, cellSize, SwiftSpatialHashOptions.Default) { } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="SwiftFixedSpatialHash{T}"/> class with the specified capacity, cell |
| | | 26 | | /// </summary> |
| | | 27 | | public SwiftFixedSpatialHash(int capacity, Fixed64 cellSize, SwiftSpatialHashOptions options) |
| | 6 | 28 | | : base(capacity, new FixedBoundVolumeCellMapper(cellSize), options) { } |
| | | 29 | | |
| | | 30 | | private sealed class FixedBoundVolumeCellMapper : ISpatialHashCellMapper<FixedBoundVolume> |
| | | 31 | | { |
| | | 32 | | private readonly Fixed64 _cellSize; |
| | | 33 | | |
| | 4 | 34 | | public FixedBoundVolumeCellMapper(Fixed64 cellSize) |
| | | 35 | | { |
| | 4 | 36 | | if (cellSize <= Fixed64.Zero) |
| | 2 | 37 | | throw new ArgumentOutOfRangeException(nameof(cellSize), cellSize, "Cell size must be greater than zero." |
| | | 38 | | |
| | 2 | 39 | | _cellSize = cellSize; |
| | 2 | 40 | | } |
| | | 41 | | |
| | | 42 | | public void GetCellRange(FixedBoundVolume bounds, out SwiftSpatialHashCellIndex minCell, out SwiftSpatialHashCel |
| | | 43 | | { |
| | 4 | 44 | | minCell = new SwiftSpatialHashCellIndex( |
| | 4 | 45 | | ToCell(bounds.Min.X), |
| | 4 | 46 | | ToCell(bounds.Min.Y), |
| | 4 | 47 | | ToCell(bounds.Min.Z)); |
| | | 48 | | |
| | 4 | 49 | | maxCell = new SwiftSpatialHashCellIndex( |
| | 4 | 50 | | ToCell(bounds.Max.X), |
| | 4 | 51 | | ToCell(bounds.Max.Y), |
| | 4 | 52 | | ToCell(bounds.Max.Z)); |
| | 4 | 53 | | } |
| | | 54 | | |
| | | 55 | | private int ToCell(Fixed64 value) |
| | | 56 | | { |
| | 24 | 57 | | return (value / _cellSize).FloorToInt(); |
| | | 58 | | } |
| | | 59 | | } |
| | | 60 | | } |