2019-12-30 06:19:10 +00:00
|
|
|
|
using System;
|
2019-09-06 08:11:58 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-10-31 23:19:30 +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>
|
|
|
|
|
/// Axis-aligned bounding box.
|
|
|
|
|
/// </summary>
|
2019-12-09 03:46:08 +00:00
|
|
|
|
public struct AABB : IEquatable<AABB>
|
2019-09-06 08:11:58 +00:00
|
|
|
|
{
|
2020-01-02 03:24:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The top-left position of the AABB.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value></value>
|
2019-12-30 06:19:10 +00:00
|
|
|
|
public Vector2 Min { get; private set; }
|
2020-01-02 03:24:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The bottom-right position of the AABB.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value></value>
|
2019-12-30 06:19:10 +00:00
|
|
|
|
public Vector2 Max { get; private set; }
|
2019-09-06 08:11:58 +00:00
|
|
|
|
|
2019-12-30 06:19:10 +00:00
|
|
|
|
public float Width { get { return Max.X - Min.X; } }
|
|
|
|
|
public float Height { get { return Max.Y - Min.Y; } }
|
2019-09-06 08:11:58 +00:00
|
|
|
|
|
2020-01-01 01:45:42 +00:00
|
|
|
|
public float Right { get { return Max.X; } }
|
|
|
|
|
public float Left { get { return Min.X; } }
|
2020-01-02 03:24:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The top of the AABB. Assumes a downward-aligned Y axis, so this value will be smaller than Bottom.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value></value>
|
|
|
|
|
public float Top { get { return Min.Y; } }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The bottom of the AABB. Assumes a downward-aligned Y axis, so this value will be larger than Top.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value></value>
|
2020-01-01 01:45:42 +00:00
|
|
|
|
public float Bottom { get { return Max.Y; } }
|
|
|
|
|
|
2019-12-30 06:19:10 +00:00
|
|
|
|
public AABB(float minX, float minY, float maxX, float maxY)
|
|
|
|
|
{
|
|
|
|
|
Min = new Vector2(minX, minY);
|
|
|
|
|
Max = new Vector2(maxX, maxY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AABB(Vector2 min, Vector2 max)
|
2019-09-06 08:11:58 +00:00
|
|
|
|
{
|
2019-12-30 06:19:10 +00:00
|
|
|
|
Min = min;
|
|
|
|
|
Max = max;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-02 07:08:14 +00:00
|
|
|
|
private static Matrix3x2 AbsoluteMatrix(Matrix3x2 matrix)
|
2019-12-30 06:19:10 +00:00
|
|
|
|
{
|
2020-01-02 07:08:14 +00:00
|
|
|
|
return new Matrix3x2
|
2019-12-30 06:19:10 +00:00
|
|
|
|
(
|
2020-01-02 07:08:14 +00:00
|
|
|
|
Math.Abs(matrix.M11), Math.Abs(matrix.M12),
|
|
|
|
|
Math.Abs(matrix.M21), Math.Abs(matrix.M22),
|
|
|
|
|
Math.Abs(matrix.M31), Math.Abs(matrix.M32)
|
2019-12-30 06:19:10 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-02 03:24:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Efficiently transforms the AABB by a Transform2D.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="aabb"></param>
|
|
|
|
|
/// <param name="transform"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-12-30 06:19:10 +00:00
|
|
|
|
public static AABB Transformed(AABB aabb, Transform2D transform)
|
|
|
|
|
{
|
|
|
|
|
var center = (aabb.Min + aabb.Max) / 2f;
|
|
|
|
|
var extent = (aabb.Max - aabb.Min) / 2f;
|
|
|
|
|
|
|
|
|
|
var newCenter = Vector2.Transform(center, transform.TransformMatrix);
|
|
|
|
|
var newExtent = Vector2.TransformNormal(extent, AbsoluteMatrix(transform.TransformMatrix));
|
|
|
|
|
|
|
|
|
|
return new AABB(newCenter - newExtent, newCenter + newExtent);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-02 03:24:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates an AABB for an arbitrary collection of positions.
|
|
|
|
|
/// This is less efficient than defining a custom AABB method for most shapes, so avoid using this if possible.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="vertices"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-12-30 06:19:10 +00:00
|
|
|
|
public static AABB FromVertices(IEnumerable<Position2D> vertices)
|
|
|
|
|
{
|
|
|
|
|
var minX = float.MaxValue;
|
|
|
|
|
var minY = float.MaxValue;
|
|
|
|
|
var maxX = float.MinValue;
|
|
|
|
|
var maxY = float.MinValue;
|
2019-12-30 05:09:28 +00:00
|
|
|
|
|
|
|
|
|
foreach (var vertex in vertices)
|
|
|
|
|
{
|
2019-12-30 06:19:10 +00:00
|
|
|
|
if (vertex.X < minX)
|
2019-12-30 05:09:28 +00:00
|
|
|
|
{
|
2019-12-30 06:19:10 +00:00
|
|
|
|
minX = vertex.X;
|
2019-12-30 05:09:28 +00:00
|
|
|
|
}
|
2019-12-30 06:19:10 +00:00
|
|
|
|
if (vertex.Y < minY)
|
2019-12-30 05:09:28 +00:00
|
|
|
|
{
|
2019-12-30 06:19:10 +00:00
|
|
|
|
minY = vertex.Y;
|
2019-12-30 05:09:28 +00:00
|
|
|
|
}
|
2019-12-30 06:19:10 +00:00
|
|
|
|
if (vertex.X > maxX)
|
2019-12-30 05:09:28 +00:00
|
|
|
|
{
|
2019-12-30 06:19:10 +00:00
|
|
|
|
maxX = vertex.X;
|
2019-12-30 05:09:28 +00:00
|
|
|
|
}
|
2019-12-30 06:19:10 +00:00
|
|
|
|
if (vertex.Y > maxY)
|
2019-12-30 05:09:28 +00:00
|
|
|
|
{
|
2019-12-30 06:19:10 +00:00
|
|
|
|
maxY = vertex.Y;
|
2019-12-30 05:09:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-06 08:11:58 +00:00
|
|
|
|
|
2019-12-30 06:19:10 +00:00
|
|
|
|
return new AABB(minX, minY, maxX, maxY);
|
2019-09-06 08:11:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-02 03:24:42 +00:00
|
|
|
|
public static bool TestOverlap(AABB a, AABB b)
|
|
|
|
|
{
|
|
|
|
|
return a.Left <= b.Right && a.Right >= b.Left && a.Top <= b.Bottom && a.Bottom >= b.Top;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-09 03:46:08 +00:00
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
{
|
|
|
|
|
return obj is AABB aabb && Equals(aabb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Equals(AABB other)
|
|
|
|
|
{
|
2019-12-30 06:19:10 +00:00
|
|
|
|
return Min == other.Min &&
|
|
|
|
|
Max == other.Max;
|
2019-12-09 03:46:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
2019-12-30 06:19:10 +00:00
|
|
|
|
return HashCode.Combine(Min, Max);
|
2019-09-06 08:11:58 +00:00
|
|
|
|
}
|
2019-12-09 03:46:08 +00:00
|
|
|
|
|
|
|
|
|
public static bool operator ==(AABB left, AABB right)
|
|
|
|
|
{
|
|
|
|
|
return left.Equals(right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator !=(AABB left, AABB right)
|
|
|
|
|
{
|
|
|
|
|
return !(left == right);
|
|
|
|
|
}
|
2019-09-06 08:11:58 +00:00
|
|
|
|
}
|
2019-12-16 18:51:27 +00:00
|
|
|
|
}
|