| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace SwiftCollections.Query; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents a numerics-backed spatial hash optimized for high-churn broad-phase spatial queries. |
| | | 7 | | /// </summary> |
| | | 8 | | public class SwiftSpatialHash<TKey> : SwiftSpatialHash<TKey, BoundVolume> |
| | | 9 | | where TKey : notnull |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Initializes a new instance of the <see cref="SwiftSpatialHash{TKey}"/> class with the specified capacity and cel |
| | | 13 | | /// </summary> |
| | | 14 | | public SwiftSpatialHash(int capacity, float cellSize) |
| | 1 | 15 | | : this(capacity, cellSize, SwiftSpatialHashOptions.Default) |
| | | 16 | | { |
| | 1 | 17 | | } |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Initializes a new instance of the <see cref="SwiftSpatialHash{TKey}"/> class with the specified capacity, cell s |
| | | 21 | | /// </summary> |
| | | 22 | | public SwiftSpatialHash(int capacity, float cellSize, SwiftSpatialHashOptions options) |
| | 2 | 23 | | : base(capacity, new BoundVolumeCellMapper(cellSize), options) |
| | | 24 | | { |
| | 2 | 25 | | } |
| | | 26 | | |
| | | 27 | | private sealed class BoundVolumeCellMapper : ISpatialHashCellMapper<BoundVolume> |
| | | 28 | | { |
| | | 29 | | private readonly float _cellSize; |
| | | 30 | | |
| | 2 | 31 | | public BoundVolumeCellMapper(float cellSize) |
| | | 32 | | { |
| | 2 | 33 | | if (float.IsNaN(cellSize) || float.IsInfinity(cellSize) || cellSize <= 0f) |
| | 0 | 34 | | throw new ArgumentOutOfRangeException(nameof(cellSize), cellSize, "Cell size must be a finite positive v |
| | | 35 | | |
| | 2 | 36 | | _cellSize = cellSize; |
| | 2 | 37 | | } |
| | | 38 | | |
| | | 39 | | public void GetCellRange(BoundVolume bounds, out SwiftSpatialHashCellIndex minCell, out SwiftSpatialHashCellInde |
| | | 40 | | { |
| | 5 | 41 | | minCell = new SwiftSpatialHashCellIndex( |
| | 5 | 42 | | ToCell(bounds.Min.X), |
| | 5 | 43 | | ToCell(bounds.Min.Y), |
| | 5 | 44 | | ToCell(bounds.Min.Z)); |
| | | 45 | | |
| | 5 | 46 | | maxCell = new SwiftSpatialHashCellIndex( |
| | 5 | 47 | | ToCell(bounds.Max.X), |
| | 5 | 48 | | ToCell(bounds.Max.Y), |
| | 5 | 49 | | ToCell(bounds.Max.Z)); |
| | 5 | 50 | | } |
| | | 51 | | |
| | | 52 | | private int ToCell(float value) |
| | | 53 | | { |
| | 30 | 54 | | return (int)MathF.Floor(value / _cellSize); |
| | | 55 | | } |
| | | 56 | | } |
| | | 57 | | } |