Insert: naive implementation

This commit is contained in:
mischa 2023-07-24 17:57:36 +08:00
parent 639d80b59a
commit e75df9dfb3
2 changed files with 52 additions and 8 deletions

View File

@ -11,12 +11,25 @@ namespace Mirror
public static class HistoryBounds
{
// insert current bounds into history. returns new total bounds.
public static Bounds Insert(Queue<Bounds> history, Bounds bounds)
// Queue.Dequeue() always has the oldest bounds.
public static Bounds Insert(Queue<Bounds> history, int limit, Bounds bounds)
{
// TODO insert new
// TODO remove old based on history limit
// remove oldest if limit reached
if (history.Count >= limit)
history.Dequeue();
return bounds;
// insert the new bounds
history.Enqueue(bounds);
// summarize total bounds.
// starting at latest bounds, not at 'new Bounds' because that would
// encapsulate (0,0) too.
// TODO make this not be O(N)
Bounds total = bounds;
foreach (Bounds b in history)
total.Encapsulate(b);
return total;
}

View File

@ -14,13 +14,44 @@ public void SetUp()
history = new Queue<Bounds>();
}
// helper function to construct (min, max) bounds
public static Bounds MinMax(Vector3 min, Vector3 max)
{
Bounds bounds = new Bounds();
bounds.SetMinMax(min, max);
return bounds;
}
[Test]
public void Insert()
{
// insert initial. new bounds should be initial.
Bounds initial = new Bounds(Vector3.zero, Vector3.one);
Bounds total = HistoryBounds.Insert(history, initial);
Assert.That(total, Is.EqualTo(initial));
const int limit = 3;
// insert initial [-1, 1].
// should calculate new bounds == initial.
Bounds total = HistoryBounds.Insert(history, limit, MinMax(-Vector3.one, Vector3.one));
Assert.That(history.Count, Is.EqualTo(1));
Assert.That(total, Is.EqualTo(MinMax(-Vector3.one, Vector3.one)));
// insert [0, 2]
// should calculate new bounds == [-1, 2].
total = HistoryBounds.Insert(history, limit, MinMax(Vector3.zero, Vector3.one * 2));
Assert.That(history.Count, Is.EqualTo(2));
Assert.That(total, Is.EqualTo(MinMax(-Vector3.one, Vector3.one * 2)));
// insert one that's smaller than current bounds [-.5, 0]
// history needs to contain it even if smaller, because once the oldest
// largest one gets removed, this one matters too.
total = HistoryBounds.Insert(history, limit, MinMax(-Vector3.one / 2, Vector3.zero));
Assert.That(history.Count, Is.EqualTo(3));
Assert.That(total, Is.EqualTo(MinMax(-Vector3.one, Vector3.one * 2)));
// insert more than 'limit': [0, 0]
// the oldest one [-1, 1] should be discarded.
// new bounds should be [-0.5, 2]
total = HistoryBounds.Insert(history, limit, MinMax(Vector3.zero, Vector3.zero));
Assert.That(history.Count, Is.EqualTo(3));
Assert.That(total, Is.EqualTo(MinMax(-Vector3.one / 2, Vector3.one * 2)));
}
}
}