2019-10-26 05:00:34 +00:00
|
|
|
using System.Linq;
|
2019-10-25 19:42:39 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using MoonTools.Core.Structs;
|
|
|
|
using MoonTools.Core.Bonk.Extensions;
|
2019-10-26 05:00:34 +00:00
|
|
|
using MoreLinq;
|
2019-10-25 19:42:39 +00:00
|
|
|
|
|
|
|
namespace MoonTools.Core.Bonk
|
|
|
|
{
|
2019-10-25 21:01:36 +00:00
|
|
|
/// <summary>
|
2019-10-26 05:00:34 +00:00
|
|
|
/// A simplex is a shape with up to n - 2 vertices in the nth dimension.
|
2019-10-25 21:01:36 +00:00
|
|
|
/// </summary>
|
2019-10-26 05:00:34 +00:00
|
|
|
public struct Simplex2D : IShape2D
|
2019-10-25 19:42:39 +00:00
|
|
|
{
|
2019-10-26 05:00:34 +00:00
|
|
|
Vector2 a;
|
|
|
|
Vector2? b;
|
|
|
|
Vector2? c;
|
2019-10-25 19:42:39 +00:00
|
|
|
|
2019-10-26 05:00:34 +00:00
|
|
|
public Vector2 A => a;
|
|
|
|
public Vector2? B => b;
|
|
|
|
public Vector2? C => c;
|
2019-10-25 20:09:03 +00:00
|
|
|
|
2019-10-26 05:00:34 +00:00
|
|
|
public bool ZeroSimplex { get { return !b.HasValue && !c.HasValue; } }
|
|
|
|
public bool OneSimplex { get { return b.HasValue && !c.HasValue; } }
|
|
|
|
public bool TwoSimplex { get { return b.HasValue && c.HasValue; } }
|
|
|
|
|
|
|
|
public int Count => TwoSimplex ? 3 : (OneSimplex ? 2 : 1);
|
|
|
|
|
|
|
|
public Simplex2D(Vector2 a)
|
|
|
|
{
|
|
|
|
this.a = a;
|
|
|
|
this.b = null;
|
|
|
|
this.c = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Simplex2D(Vector2 a, Vector2 b)
|
2019-10-25 19:42:39 +00:00
|
|
|
{
|
2019-10-26 05:00:34 +00:00
|
|
|
this.a = a;
|
|
|
|
this.b = b;
|
|
|
|
this.c = null;
|
2019-10-25 19:42:39 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 05:00:34 +00:00
|
|
|
public Simplex2D(Vector2 a, Vector2 b, Vector2 c)
|
2019-10-25 20:09:03 +00:00
|
|
|
{
|
2019-10-26 05:00:34 +00:00
|
|
|
this.a = a;
|
|
|
|
this.b = b;
|
|
|
|
this.c = c;
|
2019-10-25 20:09:03 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 19:42:39 +00:00
|
|
|
public IEnumerable<Position2D> Vertices
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2019-10-26 05:00:34 +00:00
|
|
|
yield return (Position2D)a;
|
|
|
|
if (b.HasValue) { yield return (Position2D)b; }
|
|
|
|
if (c.HasValue) { yield return (Position2D)c; }
|
2019-10-25 19:42:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public AABB AABB(Transform2D transform)
|
|
|
|
{
|
|
|
|
return Bonk.AABB.FromTransformedVertices(Vertices, transform);
|
|
|
|
}
|
|
|
|
|
2019-10-25 23:20:43 +00:00
|
|
|
public Vector2 Support(Vector2 direction)
|
|
|
|
{
|
2019-10-26 05:00:34 +00:00
|
|
|
return Vertices.MaxBy(vertex => Vector2.Dot(vertex, direction)).First();
|
2019-10-25 23:20:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2 Support(Vector2 direction, Transform2D transform)
|
|
|
|
{
|
|
|
|
return Vector2.Transform(Support(direction), transform.TransformMatrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
{
|
|
|
|
if (obj is IShape2D other)
|
|
|
|
{
|
|
|
|
return Equals(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-25 19:42:39 +00:00
|
|
|
public bool Equals(IShape2D other)
|
|
|
|
{
|
2019-10-26 05:00:34 +00:00
|
|
|
if (other is Simplex2D otherSimplex)
|
2019-10-25 19:42:39 +00:00
|
|
|
{
|
2019-10-26 05:00:34 +00:00
|
|
|
if (Count != otherSimplex.Count) { return false; }
|
|
|
|
return Vertices.Intersect(otherSimplex.Vertices).Count() == Count;
|
2019-10-25 19:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-25 23:20:43 +00:00
|
|
|
public override int GetHashCode()
|
2019-10-25 19:42:39 +00:00
|
|
|
{
|
2019-10-26 05:00:34 +00:00
|
|
|
var hashCode = -495772172;
|
|
|
|
hashCode = hashCode * -1521134295 + EqualityComparer<Vector2>.Default.GetHashCode(a);
|
|
|
|
hashCode = hashCode * -1521134295 + EqualityComparer<Vector2?>.Default.GetHashCode(b);
|
|
|
|
hashCode = hashCode * -1521134295 + EqualityComparer<Vector2?>.Default.GetHashCode(c);
|
|
|
|
hashCode = hashCode * -1521134295 + ZeroSimplex.GetHashCode();
|
|
|
|
hashCode = hashCode * -1521134295 + OneSimplex.GetHashCode();
|
|
|
|
hashCode = hashCode * -1521134295 + TwoSimplex.GetHashCode();
|
2019-10-25 23:20:43 +00:00
|
|
|
hashCode = hashCode * -1521134295 + EqualityComparer<IEnumerable<Position2D>>.Default.GetHashCode(Vertices);
|
|
|
|
return hashCode;
|
2019-10-25 19:42:39 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 05:00:34 +00:00
|
|
|
public static bool operator ==(Simplex2D a, Simplex2D b)
|
2019-10-25 19:42:39 +00:00
|
|
|
{
|
2019-10-25 23:20:43 +00:00
|
|
|
return a.Equals(b);
|
|
|
|
}
|
|
|
|
|
2019-10-26 05:00:34 +00:00
|
|
|
public static bool operator !=(Simplex2D a, Simplex2D b)
|
2019-10-25 23:20:43 +00:00
|
|
|
{
|
|
|
|
return !(a == b);
|
2019-10-25 19:42:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|