2019-10-25 23:20:43 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Collections.Pooled;
|
2019-09-06 08:11:58 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using MoonTools.Core.Structs;
|
2019-10-25 23:20:43 +00:00
|
|
|
|
using MoreLinq;
|
2019-09-06 08:11:58 +00:00
|
|
|
|
|
|
|
|
|
namespace MoonTools.Core.Bonk
|
|
|
|
|
{
|
2019-10-25 21:01:36 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// A Shape defined by an arbitrary collection of vertices. WARNING: Polygon must use an Array internally and therefore will create GC pressure.
|
|
|
|
|
/// </summary>
|
2019-09-06 08:11:58 +00:00
|
|
|
|
public struct Polygon : IShape2D, IEquatable<IShape2D>
|
|
|
|
|
{
|
2019-10-25 23:20:43 +00:00
|
|
|
|
private PooledSet<Position2D> vertices;
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Position2D> Vertices { get { return vertices == null ? Enumerable.Empty<Position2D>() : vertices; } }
|
2019-09-06 08:11:58 +00:00
|
|
|
|
|
|
|
|
|
// vertices are local to the origin
|
|
|
|
|
public Polygon(params Position2D[] vertices)
|
|
|
|
|
{
|
2019-10-25 23:20:43 +00:00
|
|
|
|
this.vertices = new PooledSet<Position2D>(vertices, ClearMode.Always);
|
2019-09-06 08:11:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Vector2 Support(Vector2 direction, Transform2D transform)
|
|
|
|
|
{
|
2019-10-25 23:20:43 +00:00
|
|
|
|
return Vertices.Select(vertex => Vector2.Transform(vertex, transform.TransformMatrix)).MaxBy(transformed => Vector2.Dot(transformed, direction)).First();
|
2019-09-06 08:11:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AABB AABB(Transform2D Transform2D)
|
|
|
|
|
{
|
2019-09-06 20:00:35 +00:00
|
|
|
|
return Bonk.AABB.FromTransformedVertices(Vertices, Transform2D);
|
2019-09-06 08:11:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-25 23:20:43 +00:00
|
|
|
|
public override bool Equals(object obj)
|
2019-09-06 08:11:58 +00:00
|
|
|
|
{
|
2019-10-25 23:20:43 +00:00
|
|
|
|
if (obj is IShape2D other)
|
2019-09-06 08:11:58 +00:00
|
|
|
|
{
|
2019-10-25 23:20:43 +00:00
|
|
|
|
return Equals(other);
|
|
|
|
|
}
|
2019-09-06 08:11:58 +00:00
|
|
|
|
|
2019-10-25 23:20:43 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-09-06 08:11:58 +00:00
|
|
|
|
|
2019-10-25 23:20:43 +00:00
|
|
|
|
public bool Equals(IShape2D other)
|
|
|
|
|
{
|
|
|
|
|
if (other is Polygon otherPolygon)
|
|
|
|
|
{
|
|
|
|
|
return vertices.SetEquals(otherPolygon.vertices);
|
2019-09-06 08:11:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-10-25 23:20:43 +00:00
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
var hashCode = -1404792980;
|
|
|
|
|
hashCode = hashCode * -1521134295 + EqualityComparer<PooledSet<Position2D>>.Default.GetHashCode(vertices);
|
|
|
|
|
hashCode = hashCode * -1521134295 + EqualityComparer<IEnumerable<Position2D>>.Default.GetHashCode(Vertices);
|
|
|
|
|
return hashCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator ==(Polygon a, Polygon b)
|
|
|
|
|
{
|
|
|
|
|
return a.Equals(b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator !=(Polygon a, Polygon b)
|
|
|
|
|
{
|
|
|
|
|
return !(a == b);
|
|
|
|
|
}
|
2019-09-06 08:11:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|