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