MoonTools.Bonk/Bonk/BroadPhase/SpatialHash.cs

157 lines
5.4 KiB
C#
Raw Permalink Normal View History

2019-12-30 06:19:10 +00:00
using System;
2019-09-06 08:11:58 +00:00
using System.Collections.Generic;
2019-12-30 06:19:10 +00:00
using System.Numerics;
2020-02-21 02:07:59 +00:00
using MoonTools.Structs;
2019-09-06 08:11:58 +00:00
2020-02-21 02:07:59 +00:00
namespace MoonTools.Bonk
2019-09-06 08:11:58 +00:00
{
2019-10-25 21:01:36 +00:00
/// <summary>
/// Used to quickly check if two shapes are potentially overlapping.
/// </summary>
/// <typeparam name="T">The type that will be used to uniquely identify shape-transform pairs.</typeparam>
2019-09-06 08:11:58 +00:00
public class SpatialHash<T> where T : IEquatable<T>
{
private readonly int cellSize;
private readonly Dictionary<long, HashSet<T>> hashDictionary = new Dictionary<long, HashSet<T>>();
2020-01-02 09:40:03 +00:00
private readonly Dictionary<T, (IHasAABB2D, Transform2D)> IDLookup = new Dictionary<T, (IHasAABB2D, Transform2D)>();
2019-09-06 08:11:58 +00:00
2020-01-17 10:52:38 +00:00
public int MinX { get; private set; } = 0;
public int MaxX { get; private set; } = 0;
public int MinY { get; private set; } = 0;
public int MaxY { get; private set; } = 0;
2019-09-06 08:11:58 +00:00
public SpatialHash(int cellSize)
{
this.cellSize = cellSize;
}
2019-12-30 06:19:10 +00:00
private (int, int) Hash(Vector2 position)
2019-09-06 08:11:58 +00:00
{
2019-12-30 06:19:10 +00:00
return ((int)Math.Floor(position.X / cellSize), (int)Math.Floor(position.Y / cellSize));
2019-09-06 08:11:58 +00:00
}
2019-10-25 21:01:36 +00:00
/// <summary>
/// Inserts an element into the SpatialHash.
/// </summary>
/// <param name="id">A unique ID for the shape-transform pair.</param>
/// <param name="shape"></param>
/// <param name="transform2D"></param>
2020-01-02 09:40:03 +00:00
public void Insert(T id, IHasAABB2D shape, Transform2D transform2D)
2019-09-06 08:11:58 +00:00
{
2019-12-30 06:19:10 +00:00
var box = shape.TransformedAABB(transform2D);
var minHash = Hash(box.Min);
var maxHash = Hash(box.Max);
2019-09-06 08:11:58 +00:00
2019-12-30 06:19:10 +00:00
for (var i = minHash.Item1; i <= maxHash.Item1; i++)
2019-09-06 08:11:58 +00:00
{
2019-12-30 06:19:10 +00:00
for (var j = minHash.Item2; j <= maxHash.Item2; j++)
2019-09-06 08:11:58 +00:00
{
2020-01-01 01:57:38 +00:00
var key = MakeLong(i, j);
if (!hashDictionary.ContainsKey(key))
2019-09-06 08:11:58 +00:00
{
hashDictionary.Add(key, new HashSet<T>());
2019-09-06 08:11:58 +00:00
}
hashDictionary[key].Add(id);
IDLookup[id] = (shape, transform2D);
2019-09-06 08:11:58 +00:00
}
}
2020-01-17 10:52:38 +00:00
MinX = Math.Min(MinX, minHash.Item1);
MinY = Math.Min(MinY, minHash.Item2);
MaxX = Math.Max(MaxX, maxHash.Item1);
MaxY = Math.Max(MaxY, maxHash.Item2);
2019-09-06 08:11:58 +00:00
}
2019-10-25 21:01:36 +00:00
/// <summary>
/// Retrieves all the potential collisions of a shape-transform pair. Excludes any shape-transforms with the given ID.
/// </summary>
2020-01-02 09:40:03 +00:00
public IEnumerable<(T, IHasAABB2D, Transform2D)> Retrieve(T id, IHasAABB2D shape, Transform2D transform2D)
2019-09-06 08:11:58 +00:00
{
var box = shape.TransformedAABB(transform2D);
2020-01-17 10:52:38 +00:00
var (minX, minY) = Hash(box.Min);
var (maxX, maxY) = Hash(box.Max);
2019-09-06 08:11:58 +00:00
2020-01-17 10:52:38 +00:00
if (minX < MinX) { minX = MinX; }
if (maxX > MaxX) { maxX = MaxX; }
if (minY < MinY) { minY = MinY; }
if (maxY > MaxY) { maxY = MaxY; }
for (var i = minX; i <= maxX; i++)
2019-09-06 08:11:58 +00:00
{
2020-01-17 10:52:38 +00:00
for (var j = minY; j <= maxY; j++)
2019-09-06 08:11:58 +00:00
{
2020-01-01 01:57:38 +00:00
var key = MakeLong(i, j);
if (hashDictionary.ContainsKey(key))
2019-09-06 08:11:58 +00:00
{
foreach (var t in hashDictionary[key])
2019-09-06 08:11:58 +00:00
{
var (otherShape, otherTransform) = IDLookup[t];
2020-01-05 21:10:52 +00:00
if (!id.Equals(t) && AABB.TestOverlap(box, otherShape.TransformedAABB(otherTransform)))
{
yield return (t, otherShape, otherTransform);
}
}
}
}
}
}
/// <summary>
/// Retrieves objects based on a pre-transformed AABB.
/// </summary>
/// <param name="aabb">A transformed AABB.</param>
/// <returns></returns>
public IEnumerable<(T, IHasAABB2D, Transform2D)> Retrieve(AABB aabb)
{
2020-01-17 10:52:38 +00:00
var (minX, minY) = Hash(aabb.Min);
var (maxX, maxY) = Hash(aabb.Max);
2020-01-05 21:10:52 +00:00
2020-01-17 10:52:38 +00:00
if (minX < MinX) { minX = MinX; }
if (maxX > MaxX) { maxX = MaxX; }
if (minY < MinY) { minY = MinY; }
if (maxY > MaxY) { maxY = MaxY; }
for (var i = minX; i <= maxX; i++)
2020-01-05 21:10:52 +00:00
{
2020-01-17 10:52:38 +00:00
for (var j = minY; j <= maxY; j++)
2019-09-06 08:11:58 +00:00
{
2020-01-01 01:57:38 +00:00
var key = MakeLong(i, j);
if (hashDictionary.ContainsKey(key))
2019-09-06 08:11:58 +00:00
{
foreach (var t in hashDictionary[key])
2019-09-06 08:11:58 +00:00
{
var (otherShape, otherTransform) = IDLookup[t];
2020-01-05 21:10:52 +00:00
if (AABB.TestOverlap(aabb, otherShape.TransformedAABB(otherTransform)))
{
yield return (t, otherShape, otherTransform);
}
2019-09-06 08:11:58 +00:00
}
}
}
}
}
2019-10-25 21:01:36 +00:00
/// <summary>
/// Removes everything that has been inserted into the SpatialHash.
/// </summary>
2019-09-06 08:11:58 +00:00
public void Clear()
{
foreach (var hash in hashDictionary.Values)
2019-09-06 08:11:58 +00:00
{
hash.Clear();
2019-09-06 08:11:58 +00:00
}
IDLookup.Clear();
}
2020-01-01 01:57:38 +00:00
private static long MakeLong(int left, int right)
{
return ((long)left << 32) | ((uint)right);
}
2019-09-06 08:11:58 +00:00
}
2019-12-30 06:19:10 +00:00
}