MoonTools.Bonk/Bonk/AABB2D.cs

157 lines
4.6 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-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>
2020-09-07 02:38:51 +00:00
public struct AABB2D : IEquatable<AABB2D>
2019-09-06 08:11:58 +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; }
/// <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
public float Right { get { return Max.X; } }
public float Left { get { return Min.X; } }
2020-07-18 20:19:36 +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; } }
2020-07-18 20:19:36 +00:00
/// <summary>
/// The bottom of the AABB. Assumes a downward-aligned Y axis, so this value will be larger than Top.
/// </summary>
/// <value></value>
public float Bottom { get { return Max.Y; } }
2020-09-07 02:38:51 +00:00
public AABB2D(float minX, float minY, float maxX, float maxY)
2019-12-30 06:19:10 +00:00
{
Min = new Vector2(minX, minY);
Max = new Vector2(maxX, maxY);
}
2020-09-07 02:38:51 +00:00
public AABB2D(Vector2 min, Vector2 max)
2019-09-06 08:11:58 +00:00
{
2019-12-30 06:19:10 +00:00
Min = min;
Max = max;
}
private static Matrix3x2 AbsoluteMatrix(Matrix3x2 matrix)
2019-12-30 06:19:10 +00:00
{
return new Matrix3x2
2019-12-30 06:19:10 +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
);
}
/// <summary>
/// Efficiently transforms the AABB by a Transform2D.
/// </summary>
/// <param name="aabb"></param>
/// <param name="transform"></param>
/// <returns></returns>
2020-09-07 02:38:51 +00:00
public static AABB2D Transformed(AABB2D aabb, Transform2D transform)
2019-12-30 06:19:10 +00:00
{
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));
2020-09-07 02:38:51 +00:00
return new AABB2D(newCenter - newExtent, newCenter + newExtent);
2019-12-30 06:19:10 +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>
2020-09-07 02:38:51 +00:00
public static AABB2D FromVertices(IEnumerable<Position2D> vertices)
2019-12-30 06:19:10 +00:00
{
var minX = float.MaxValue;
var minY = float.MaxValue;
var maxX = float.MinValue;
var maxY = float.MinValue;
foreach (var vertex in vertices)
{
2019-12-30 06:19:10 +00:00
if (vertex.X < minX)
{
2019-12-30 06:19:10 +00:00
minX = vertex.X;
}
2019-12-30 06:19:10 +00:00
if (vertex.Y < minY)
{
2019-12-30 06:19:10 +00:00
minY = vertex.Y;
}
2019-12-30 06:19:10 +00:00
if (vertex.X > maxX)
{
2019-12-30 06:19:10 +00:00
maxX = vertex.X;
}
2019-12-30 06:19:10 +00:00
if (vertex.Y > maxY)
{
2019-12-30 06:19:10 +00:00
maxY = vertex.Y;
}
}
2019-09-06 08:11:58 +00:00
2020-09-07 02:38:51 +00:00
return new AABB2D(minX, minY, maxX, maxY);
2019-09-06 08:11:58 +00:00
}
2020-09-07 02:38:51 +00:00
public static bool TestOverlap(AABB2D a, AABB2D b)
{
return a.Left <= b.Right && a.Right >= b.Left && a.Top <= b.Bottom && a.Bottom >= b.Top;
}
2020-09-07 02:38:51 +00:00
public AABB2D Merge(AABB2D other)
{
return new AABB2D(
Vector2.Min(Min, other.Min),
Vector2.Max(Max, other.Max)
);
}
public override bool Equals(object obj)
{
2020-09-07 02:38:51 +00:00
return obj is AABB2D aabb && Equals(aabb);
}
2020-09-07 02:38:51 +00:00
public bool Equals(AABB2D other)
{
2019-12-30 06:19:10 +00:00
return Min == other.Min &&
Max == other.Max;
}
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
}
2020-09-07 02:38:51 +00:00
public static bool operator ==(AABB2D left, AABB2D right)
{
return left.Equals(right);
}
2020-09-07 02:38:51 +00:00
public static bool operator !=(AABB2D left, AABB2D right)
{
return !(left == right);
}
2019-09-06 08:11:58 +00:00
}
2019-12-16 18:51:27 +00:00
}