shape equality optimizations

generics
Evan Hemsley 2019-10-25 16:20:43 -07:00
parent 150f16916e
commit eefa52c4b3
8 changed files with 641 additions and 56 deletions

View File

@ -15,7 +15,7 @@
<PackageProjectUrl>https://github.com/MoonsideGames/MoonTools.Core.Bonk</PackageProjectUrl>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MoonTools.Core.Structs" Version="1.0.1"/>
<PackageReference Include="MoonTools.Core.Structs" Version="1.1.0"/>
<PackageReference Include="morelinq" Version="3.2.0"/>
<PackageReference Include="Collections.Pooled" Version="1.0.82"/>
</ItemGroup>

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using MoonTools.Core.Structs;
@ -22,18 +23,48 @@ namespace MoonTools.Core.Bonk
this.transformB = transformB;
}
public bool Equals(MinkowskiDifference other)
{
return
shapeA == other.shapeA &&
transformA.Equals(other.transformA) &&
shapeB == other.shapeB &&
transformB.Equals(other.transformB);
}
public Vector2 Support(Vector2 direction)
{
return shapeA.Support(direction, transformA) - shapeB.Support(-direction, transformB);
}
public override bool Equals(object other)
{
if (other is MinkowskiDifference otherMinkowskiDifference)
{
return Equals(otherMinkowskiDifference);
}
return false;
}
public bool Equals(MinkowskiDifference other)
{
return
shapeA == other.shapeA &&
transformA == other.transformA &&
shapeB == other.shapeB &&
transformB == other.transformB;
}
public override int GetHashCode()
{
var hashCode = 974363698;
hashCode = hashCode * -1521134295 + EqualityComparer<IShape2D>.Default.GetHashCode(shapeA);
hashCode = hashCode * -1521134295 + EqualityComparer<Transform2D>.Default.GetHashCode(transformA);
hashCode = hashCode * -1521134295 + EqualityComparer<IShape2D>.Default.GetHashCode(shapeB);
hashCode = hashCode * -1521134295 + EqualityComparer<Transform2D>.Default.GetHashCode(transformB);
return hashCode;
}
public static bool operator ==(MinkowskiDifference a, MinkowskiDifference b)
{
return a.Equals(b);
}
public static bool operator !=(MinkowskiDifference a, MinkowskiDifference b)
{
return !(a == b);
}
}
}

View File

@ -31,6 +31,16 @@ namespace MoonTools.Core.Bonk
);
}
public override bool Equals(object obj)
{
if (obj is Circle other)
{
return Equals(other);
}
return false;
}
public bool Equals(IShape2D other)
{
if (other is Circle circle)
@ -40,5 +50,20 @@ namespace MoonTools.Core.Bonk
return false;
}
public override int GetHashCode()
{
return 598075851 + Radius.GetHashCode();
}
public static bool operator ==(Circle a, Circle b)
{
return a.Equals(b);
}
public static bool operator !=(Circle a, Circle b)
{
return !(a == b);
}
}
}

View File

@ -42,15 +42,43 @@ namespace MoonTools.Core.Bonk
return Bonk.AABB.FromTransformedVertices(vertices, Transform2D);
}
public bool Equals(IShape2D other)
public override bool Equals(object obj)
{
if (other is Line)
if (obj is IShape2D other)
{
var otherLine = (Line)other;
return v0.ToVector2() == otherLine.v0.ToVector2() && v1.ToVector2() == otherLine.v1.ToVector2();
return Equals(other);
}
return false;
}
public bool Equals(IShape2D other)
{
if (other is Line otherLine)
{
return (v0 == otherLine.v0 && v1 == otherLine.v1) || (v1 == otherLine.v0 && v0 == otherLine.v1);
}
return false;
}
public override int GetHashCode()
{
var hashCode = -851829407;
hashCode = hashCode * -1521134295 + EqualityComparer<Position2D>.Default.GetHashCode(v0);
hashCode = hashCode * -1521134295 + EqualityComparer<Position2D>.Default.GetHashCode(v1);
hashCode = hashCode * -1521134295 + EqualityComparer<IEnumerable<Position2D>>.Default.GetHashCode(vertices);
return hashCode;
}
public static bool operator ==(Line a, Line b)
{
return a.Equals(b);
}
public static bool operator !=(Line a, Line b)
{
return !(a == b);
}
}
}

View File

@ -1,6 +1,10 @@
using System;
using System.Linq;
using System;
using System.Collections.Generic;
using Collections.Pooled;
using Microsoft.Xna.Framework;
using MoonTools.Core.Structs;
using MoreLinq;
namespace MoonTools.Core.Bonk
{
@ -9,31 +13,19 @@ namespace MoonTools.Core.Bonk
/// </summary>
public struct Polygon : IShape2D, IEquatable<IShape2D>
{
public Position2D[] Vertices { get; private set; }
private PooledSet<Position2D> vertices;
public IEnumerable<Position2D> Vertices { get { return vertices == null ? Enumerable.Empty<Position2D>() : vertices; } }
// vertices are local to the origin
public Polygon(params Position2D[] vertices)
{
Vertices = vertices;
this.vertices = new PooledSet<Position2D>(vertices, ClearMode.Always);
}
public Vector2 Support(Vector2 direction, Transform2D transform)
{
var furthest = float.NegativeInfinity;
var furthestVertex = Vector2.Transform(Vertices[0], transform.TransformMatrix);
foreach (var vertex in Vertices)
{
var TransformedVertex = Vector2.Transform(vertex, transform.TransformMatrix);
var distance = Vector2.Dot(TransformedVertex, direction);
if (distance > furthest)
{
furthest = distance;
furthestVertex = TransformedVertex;
}
}
return furthestVertex;
return Vertices.Select(vertex => Vector2.Transform(vertex, transform.TransformMatrix)).MaxBy(transformed => Vector2.Dot(transformed, direction)).First();
}
public AABB AABB(Transform2D Transform2D)
@ -41,23 +33,42 @@ namespace MoonTools.Core.Bonk
return Bonk.AABB.FromTransformedVertices(Vertices, Transform2D);
}
public bool Equals(IShape2D other)
public override bool Equals(object obj)
{
if (other is Polygon)
if (obj is IShape2D other)
{
var otherPolygon = (Polygon)other;
if (Vertices.Length != otherPolygon.Vertices.Length) { return false; }
for (int i = 0; i < Vertices.Length; i++)
{
if (Vertices[i].ToVector2() != otherPolygon.Vertices[i].ToVector2()) { return false; }
}
return true;
return Equals(other);
}
return false;
}
public bool Equals(IShape2D other)
{
if (other is Polygon otherPolygon)
{
return vertices.SetEquals(otherPolygon.vertices);
}
return false;
}
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);
}
}
}

View File

@ -46,6 +46,16 @@ namespace MoonTools.Core.Bonk
return Bonk.AABB.FromTransformedVertices(vertices, Transform2D);
}
public override bool Equals(object obj)
{
if (obj is IShape2D other)
{
return Equals(other);
}
return false;
}
public bool Equals(IShape2D other)
{
if (other is Rectangle rectangle)
@ -58,5 +68,26 @@ namespace MoonTools.Core.Bonk
return false;
}
public override int GetHashCode()
{
var hashCode = -1260800952;
hashCode = hashCode * -1521134295 + MinX.GetHashCode();
hashCode = hashCode * -1521134295 + MinY.GetHashCode();
hashCode = hashCode * -1521134295 + MaxX.GetHashCode();
hashCode = hashCode * -1521134295 + MaxY.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<IEnumerable<Position2D>>.Default.GetHashCode(vertices);
return hashCode;
}
public static bool operator ==(Rectangle a, Rectangle b)
{
return a.Equals(b);
}
public static bool operator !=(Rectangle a, Rectangle b)
{
return !(a == b);
}
}
}

View File

@ -44,18 +44,6 @@ namespace MoonTools.Core.Bonk
return Bonk.AABB.FromTransformedVertices(Vertices, transform);
}
public bool Equals(IShape2D other)
{
if (other is Simplex polytope)
{
return minkowskiDifference.Equals(polytope.minkowskiDifference) &&
directionA == polytope.directionA &&
directionB == polytope.directionB;
}
return false;
}
public Vector2 Support(Vector2 direction)
{
return minkowskiDifference.Support(direction);
@ -65,5 +53,49 @@ namespace MoonTools.Core.Bonk
{
return Vector2.Transform(Support(direction), transform.TransformMatrix);
}
public override bool Equals(object obj)
{
if (obj is IShape2D other)
{
return Equals(other);
}
return false;
}
public bool Equals(IShape2D other)
{
if (other is Simplex otherSimplex)
{
return minkowskiDifference == otherSimplex.minkowskiDifference &&
((directionA == otherSimplex.directionA && directionB == otherSimplex.directionB) ||
(directionA == otherSimplex.directionB && directionB == otherSimplex.directionA));
}
return false;
}
public override int GetHashCode()
{
var hashCode = 74270316;
hashCode = hashCode * -1521134295 + EqualityComparer<MinkowskiDifference>.Default.GetHashCode(minkowskiDifference);
hashCode = hashCode * -1521134295 + EqualityComparer<Vector2>.Default.GetHashCode(directionA);
hashCode = hashCode * -1521134295 + EqualityComparer<Vector2>.Default.GetHashCode(directionB);
hashCode = hashCode * -1521134295 + EqualityComparer<Vector2>.Default.GetHashCode(DirectionA);
hashCode = hashCode * -1521134295 + EqualityComparer<Vector2>.Default.GetHashCode(DirectionB);
hashCode = hashCode * -1521134295 + EqualityComparer<IEnumerable<Position2D>>.Default.GetHashCode(Vertices);
return hashCode;
}
public static bool operator ==(Simplex a, Simplex b)
{
return a.Equals(b);
}
public static bool operator !=(Simplex a, Simplex b)
{
return !(a == b);
}
}
}

427
Test/Equality.cs Normal file
View File

@ -0,0 +1,427 @@
using NUnit.Framework;
using FluentAssertions;
using MoonTools.Core.Bonk;
using MoonTools.Core.Structs;
using Microsoft.Xna.Framework;
namespace Tests
{
public class EqualityTests
{
public class CircleTests
{
[Test]
public void CircleEqual()
{
var a = new Circle(2);
var b = new Circle(2);
(a.Equals(b)).Should().BeTrue();
}
[Test]
public void CircleNotEqual()
{
var a = new Circle(2);
var b = new Circle(3);
(a.Equals(b)).Should().BeFalse();
}
[Test]
public void CircleEqualOperator()
{
var a = new Circle(2);
var b = new Circle(2);
(a == b).Should().BeTrue();
}
[Test]
public void CircleNotEqualOperator()
{
var a = new Circle(2);
var b = new Circle(3);
(a != b).Should().BeTrue();
}
}
public class LineTests
{
[Test]
public void LineEqual()
{
var a = new Line(new Position2D(0, 2), new Position2D(2, 4));
var b = new Line(new Position2D(0, 2), new Position2D(2, 4));
a.Equals(b).Should().BeTrue();
}
[Test]
public void LineEqualOperator()
{
var a = new Line(new Position2D(0, 2), new Position2D(2, 4));
var b = new Line(new Position2D(0, 2), new Position2D(2, 4));
(a == b).Should().BeTrue();
}
[Test]
public void LineNotEqual()
{
var a = new Line(new Position2D(-2, 4), new Position2D(2, 4));
var b = new Line(new Position2D(0, 3), new Position2D(5, 1));
a.Equals(b).Should().BeFalse();
}
[Test]
public void LineNotEqualOperator()
{
var a = new Line(new Position2D(-2, 4), new Position2D(2, 4));
var b = new Line(new Position2D(0, 3), new Position2D(5, 1));
(a != b).Should().BeTrue();
}
[Test]
public void LineReversedEqual()
{
var a = new Line(new Position2D(0, 2), new Position2D(2, 4));
var b = new Line(new Position2D(2, 4), new Position2D(0, 2));
a.Equals(b).Should().BeTrue();
}
[Test]
public void LineReversedEqualOperator()
{
var a = new Line(new Position2D(0, 2), new Position2D(2, 4));
var b = new Line(new Position2D(2, 4), new Position2D(0, 2));
(a == b).Should().BeTrue();
}
}
public class RectangleTests
{
[Test]
public void RectangleEqual()
{
var a = new MoonTools.Core.Bonk.Rectangle(0, 0, 3, 3);
var b = new MoonTools.Core.Bonk.Rectangle(0, 0, 3, 3);
a.Equals(b).Should().BeTrue();
}
[Test]
public void RectangleEqualOperator()
{
var a = new MoonTools.Core.Bonk.Rectangle(0, 0, 3, 3);
var b = new MoonTools.Core.Bonk.Rectangle(0, 0, 3, 3);
(a == b).Should().BeTrue();
}
[Test]
public void RectangleNotEqual()
{
var a = new MoonTools.Core.Bonk.Rectangle(0, 0, 3, 3);
var b = new MoonTools.Core.Bonk.Rectangle(-1, -1, 5, 5);
a.Equals(b).Should().BeFalse();
}
[Test]
public void RectangleNotEqualOperator()
{
var a = new MoonTools.Core.Bonk.Rectangle(0, 0, 3, 3);
var b = new MoonTools.Core.Bonk.Rectangle(-1, -1, 5, 5);
(a != b).Should().BeTrue();
}
}
public class PolygonTests
{
[Test]
public void PolygonEqual()
{
var a = new Polygon(
new Position2D(0, 1),
new Position2D(1, 2),
new Position2D(-1, -1)
);
var b = new Polygon(
new Position2D(0, 1),
new Position2D(1, 2),
new Position2D(-1, -1)
);
a.Equals(b).Should().BeTrue();
}
[Test]
public void PolygonEqualOperator()
{
var a = new Polygon(
new Position2D(0, 1),
new Position2D(1, 2),
new Position2D(-1, -1)
);
var b = new Polygon(
new Position2D(0, 1),
new Position2D(1, 2),
new Position2D(-1, -1)
);
(a == b).Should().BeTrue();
}
[Test]
public void PolygonDifferentOrderEqual()
{
var a = new Polygon(
new Position2D(0, 1),
new Position2D(1, 2),
new Position2D(-1, -1)
);
var b = new Polygon(
new Position2D(1, 2),
new Position2D(-1, -1),
new Position2D(0, 1)
);
a.Equals(b).Should().BeTrue();
}
[Test]
public void PolygonDifferentOrderEqualOperator()
{
var a = new Polygon(
new Position2D(0, 1),
new Position2D(1, 2),
new Position2D(-1, -1)
);
var b = new Polygon(
new Position2D(1, 2),
new Position2D(-1, -1),
new Position2D(0, 1)
);
(a == b).Should().BeTrue();
}
[Test]
public void PolygonNotEqual()
{
var a = new Polygon(
new Position2D(0, 1),
new Position2D(1, 2),
new Position2D(-1, -1)
);
var b = new Polygon(
new Position2D(1, 0),
new Position2D(2, 1),
new Position2D(-1, -1)
);
a.Equals(b).Should().BeFalse();
}
[Test]
public void PolygonNotEqualOperator()
{
var a = new Polygon(
new Position2D(0, 1),
new Position2D(1, 2),
new Position2D(-1, -1)
);
var b = new Polygon(
new Position2D(1, 0),
new Position2D(2, 1),
new Position2D(-1, -1)
);
(a != b).Should().BeTrue();
}
}
public class SimplexTests
{
[Test]
public void SimplexEquals()
{
var shapeA = new Circle(3);
var transformA = new Transform2D(new Position2D(1, 2));
var shapeB = new Circle(2);
var transformB = new Transform2D(new Position2D(4, 5));
var minkowskiDifference = new MinkowskiDifference(shapeA, transformA, shapeB, transformB);
var directionA = Vector2.UnitX;
var directionB = Vector2.UnitY;
var simplexA = new Simplex(minkowskiDifference, directionA, directionB);
var simplexB = new Simplex(minkowskiDifference, directionA, directionB);
simplexA.Equals(simplexB).Should().BeTrue();
}
[Test]
public void SimplexEqualsOperator()
{
var shapeA = new Circle(3);
var transformA = new Transform2D(new Position2D(1, 2));
var shapeB = new Circle(2);
var transformB = new Transform2D(new Position2D(4, 5));
var minkowskiDifference = new MinkowskiDifference(shapeA, transformA, shapeB, transformB);
var directionA = Vector2.UnitX;
var directionB = Vector2.UnitY;
var simplexA = new Simplex(minkowskiDifference, directionA, directionB);
var simplexB = new Simplex(minkowskiDifference, directionA, directionB);
(simplexA == simplexB).Should().BeTrue();
}
[Test]
public void SimplexDirectionOutOfOrderEqual()
{
var shapeA = new Circle(3);
var transformA = new Transform2D(new Position2D(1, 2));
var shapeB = new Circle(2);
var transformB = new Transform2D(new Position2D(4, 5));
var minkowskiDifference = new MinkowskiDifference(shapeA, transformA, shapeB, transformB);
var directionA = Vector2.UnitX;
var directionB = Vector2.UnitY;
var simplexA = new Simplex(minkowskiDifference, directionA, directionB);
var simplexB = new Simplex(minkowskiDifference, directionB, directionA);
simplexA.Equals(simplexB).Should().BeTrue();
}
[Test]
public void SimplexDirectionOutOfOrderEqualOperator()
{
var shapeA = new Circle(3);
var transformA = new Transform2D(new Position2D(1, 2));
var shapeB = new Circle(2);
var transformB = new Transform2D(new Position2D(4, 5));
var minkowskiDifference = new MinkowskiDifference(shapeA, transformA, shapeB, transformB);
var directionA = Vector2.UnitX;
var directionB = Vector2.UnitY;
var simplexA = new Simplex(minkowskiDifference, directionA, directionB);
var simplexB = new Simplex(minkowskiDifference, directionB, directionA);
(simplexA == simplexB).Should().BeTrue();
}
[Test]
public void SimplexMinkowskiNotEqual()
{
var shapeA = new Circle(3);
var transformA = new Transform2D(new Position2D(1, 2));
var shapeB = new Circle(2);
var transformB = new Transform2D(new Position2D(4, 5));
var minkowskiDifferenceA = new MinkowskiDifference(shapeA, transformA, shapeB, transformB);
var minkowskiDifferenceB = new MinkowskiDifference(shapeB, transformB, shapeA, transformA);
var directionA = Vector2.UnitX;
var directionB = Vector2.UnitY;
var simplexA = new Simplex(minkowskiDifferenceA, directionA, directionB);
var simplexB = new Simplex(minkowskiDifferenceB, directionA, directionB);
simplexA.Equals(simplexB).Should().BeFalse();
}
[Test]
public void SimplexMinkowskiNotEqualOperator()
{
var shapeA = new Circle(3);
var transformA = new Transform2D(new Position2D(1, 2));
var shapeB = new Circle(2);
var transformB = new Transform2D(new Position2D(4, 5));
var minkowskiDifferenceA = new MinkowskiDifference(shapeA, transformA, shapeB, transformB);
var minkowskiDifferenceB = new MinkowskiDifference(shapeB, transformB, shapeA, transformA);
var directionA = Vector2.UnitX;
var directionB = Vector2.UnitY;
var simplexA = new Simplex(minkowskiDifferenceA, directionA, directionB);
var simplexB = new Simplex(minkowskiDifferenceB, directionA, directionB);
(simplexA != simplexB).Should().BeTrue();
}
[Test]
public void SimplexDirectionsNotEqual()
{
var shapeA = new Circle(3);
var transformA = new Transform2D(new Position2D(1, 2));
var shapeB = new Circle(2);
var transformB = new Transform2D(new Position2D(4, 5));
var minkowskiDifference = new MinkowskiDifference(shapeA, transformA, shapeB, transformB);
var directionA = Vector2.UnitX;
var directionB = Vector2.UnitY;
var directionC = -Vector2.UnitX;
var directionD = -Vector2.UnitY;
var simplexA = new Simplex(minkowskiDifference, directionA, directionB);
var simplexB = new Simplex(minkowskiDifference, directionC, directionD);
simplexA.Equals(simplexB).Should().BeFalse();
}
[Test]
public void SimplexDirectionsNotEqualOperator()
{
var shapeA = new Circle(3);
var transformA = new Transform2D(new Position2D(1, 2));
var shapeB = new Circle(2);
var transformB = new Transform2D(new Position2D(4, 5));
var minkowskiDifference = new MinkowskiDifference(shapeA, transformA, shapeB, transformB);
var directionA = Vector2.UnitX;
var directionB = Vector2.UnitY;
var directionC = -Vector2.UnitX;
var directionD = -Vector2.UnitY;
var simplexA = new Simplex(minkowskiDifference, directionA, directionB);
var simplexB = new Simplex(minkowskiDifference, directionC, directionD);
(simplexA != simplexB).Should().BeTrue();
}
}
}
}