mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-14 19:29:36 +01:00
Optimise ChunkEnumerator (#4899)
* Optimise ChunkEnumerator It never unioned the AABB passed in with the grid's AABB so it might inadvertantly iterate a lot more dummy chunks than it needs to. This helps speedup FindGridsIntersecting. * weh * oop wrong method * Update RELEASE-NOTES.md
This commit is contained in:
@@ -51,7 +51,7 @@ END TEMPLATE-->
|
||||
|
||||
### Internal
|
||||
|
||||
*None yet*
|
||||
* Significantly optimise ChunkEnumerator / FindGridsIntersecting in certain use cases by intersecting the grid's AABB with the local AABB to avoid iterating dummy chunks.
|
||||
|
||||
|
||||
## 210.1.1
|
||||
|
||||
@@ -186,10 +186,10 @@ namespace Robust.Shared.Maths
|
||||
[Pure]
|
||||
public readonly Box2 Intersect(in Box2 other)
|
||||
{
|
||||
var ourLeftBottom = new Vector2(Left, Bottom);
|
||||
var ourRightTop = new Vector2(Right, Top);
|
||||
var otherLeftBottom = new Vector2(other.Left, other.Bottom);
|
||||
var otherRightTop = new Vector2(other.Right, other.Top);
|
||||
var ourLeftBottom = BottomLeft;
|
||||
var ourRightTop = TopRight;
|
||||
var otherLeftBottom = other.BottomLeft;
|
||||
var otherRightTop = other.TopRight;
|
||||
|
||||
var max = Vector2.Max(ourLeftBottom, otherLeftBottom);
|
||||
var min = Vector2.Min(ourRightTop, otherRightTop);
|
||||
@@ -219,10 +219,10 @@ namespace Robust.Shared.Maths
|
||||
[Pure]
|
||||
public readonly Box2 Union(in Box2 other)
|
||||
{
|
||||
var ourLeftBottom = new Vector2(Left, Bottom);
|
||||
var otherLeftBottom = new Vector2(other.Left, other.Bottom);
|
||||
var ourRightTop = new Vector2(Right, Top);
|
||||
var otherRightTop = new Vector2(other.Right, other.Top);
|
||||
var ourLeftBottom = BottomLeft;
|
||||
var otherLeftBottom = other.BottomLeft;
|
||||
var ourRightTop = TopRight;
|
||||
var otherRightTop = other.TopRight;
|
||||
|
||||
var leftBottom = Vector2.Min(ourLeftBottom, otherLeftBottom);
|
||||
var rightTop = Vector2.Max(ourRightTop, otherRightTop);
|
||||
|
||||
@@ -974,19 +974,34 @@ public abstract partial class SharedMapSystem
|
||||
internal ChunkEnumerator GetMapChunks(EntityUid uid, MapGridComponent grid, Box2 worldAABB)
|
||||
{
|
||||
var localAABB = _transform.GetInvWorldMatrix(uid).TransformBox(worldAABB);
|
||||
return new ChunkEnumerator(grid.Chunks, localAABB, grid.ChunkSize);
|
||||
var compAABB = grid.LocalAABB.Intersect(localAABB);
|
||||
|
||||
if (compAABB.IsEmpty())
|
||||
return ChunkEnumerator.Empty;
|
||||
|
||||
return new ChunkEnumerator(grid.Chunks, compAABB, grid.ChunkSize);
|
||||
}
|
||||
|
||||
internal ChunkEnumerator GetMapChunks(EntityUid uid, MapGridComponent grid, Box2Rotated worldArea)
|
||||
{
|
||||
var matrix = _transform.GetInvWorldMatrix(uid);
|
||||
var localArea = matrix.TransformBox(worldArea);
|
||||
return new ChunkEnumerator(grid.Chunks, localArea, grid.ChunkSize);
|
||||
var compAABB = grid.LocalAABB.Intersect(localArea);
|
||||
|
||||
if (compAABB.IsEmpty())
|
||||
return ChunkEnumerator.Empty;
|
||||
|
||||
return new ChunkEnumerator(grid.Chunks, compAABB, grid.ChunkSize);
|
||||
}
|
||||
|
||||
internal ChunkEnumerator GetLocalMapChunks(EntityUid uid, MapGridComponent grid, Box2 localAABB)
|
||||
{
|
||||
return new ChunkEnumerator(grid.Chunks, localAABB, grid.ChunkSize);
|
||||
var compAABB = grid.LocalAABB.Intersect(localAABB);
|
||||
|
||||
if (compAABB.IsEmpty())
|
||||
return ChunkEnumerator.Empty;
|
||||
|
||||
return new ChunkEnumerator(grid.Chunks, compAABB, grid.ChunkSize);
|
||||
}
|
||||
|
||||
#endregion ChunkAccess
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Map.Components;
|
||||
using Robust.Shared.Maths;
|
||||
|
||||
namespace Robust.Shared.Map.Enumerators;
|
||||
|
||||
internal struct ChunkEnumerator
|
||||
{
|
||||
public static ChunkEnumerator Empty => new(new Dictionary<Vector2i, MapChunk>(), Box2.Empty, 16);
|
||||
|
||||
private Dictionary<Vector2i, MapChunk> _chunks;
|
||||
private Vector2i _chunkLB;
|
||||
private Vector2i _chunkRT;
|
||||
|
||||
Reference in New Issue
Block a user