-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
176 additions
and
168 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace BnbnavNetClient.Models; | ||
|
||
public readonly record struct IntRect(int Left, int Top, int Right, int Bottom) | ||
{ | ||
public bool Contains(int x, int y) => | ||
Left <= x && x <= Right && Top <= y && y <= Bottom; | ||
|
||
public IntRect Expand(int amt) => | ||
new(Left - amt, Top - amt, Right + amt, Bottom + amt); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using Avalonia; | ||
using DynamicData; | ||
|
||
namespace BnbnavNetClient.Models; | ||
|
||
public sealed class MapBins | ||
{ | ||
public class Bin | ||
{ | ||
public List<Node> Nodes { get; } = []; | ||
public List<Edge> Edges { get; } = []; | ||
} | ||
public const int BinSideLength = 256; | ||
|
||
private readonly Bin?[,] _bins; | ||
|
||
private int BinsXLength => _bins.GetLength(0); | ||
private int BinsYLength => _bins.GetLength(1); | ||
|
||
public IntRect Bounds { get; private set; } | ||
|
||
public MapBins(IntRect bounds, IEnumerable<Node> nodes, IEnumerable<Edge> edges) | ||
{ | ||
Bounds = bounds.Expand(5); | ||
var xLength = Bounds.Right - Bounds.Left; | ||
var yLength = Bounds.Bottom - Bounds.Top; | ||
var xNumBins = xLength / BinSideLength; | ||
var yNumBins = yLength / BinSideLength; | ||
_bins = new Bin?[xNumBins + 1, yNumBins + 1]; | ||
|
||
foreach (var node in nodes) | ||
{ | ||
InsertNode(node); | ||
} | ||
|
||
foreach (var edge in edges) | ||
{ | ||
Insert(edge); | ||
} | ||
} | ||
|
||
public void InsertNode(Node node) | ||
{ | ||
if (!Bounds.Contains(node.X, node.Z)) | ||
throw new NotImplementedException(); | ||
|
||
var x = node.X - Bounds.Left; | ||
var y = node.Z - Bounds.Top; | ||
var binX = x / BinSideLength; | ||
var binY = y / BinSideLength; | ||
|
||
ref var bin = ref _bins[binX, binY]; | ||
bin ??= new Bin(); | ||
bin.Nodes.Add(node); | ||
} | ||
|
||
public void Insert(Edge edge) | ||
{ | ||
var minX = int.Min(edge.From.X, edge.To.X); | ||
var minY = int.Min(edge.From.Z, edge.To.Z); | ||
var maxX = int.Max(edge.From.X, edge.To.X); | ||
var maxY = int.Max(edge.From.Z, edge.To.Z); | ||
var edgeBounds = new IntRect(minX, minY, maxX, maxY); | ||
var expandedBounds = edgeBounds.Expand(5); | ||
var startX = (expandedBounds.Left - Bounds.Left) / BinSideLength; | ||
var startY = (expandedBounds.Top - Bounds.Top) / BinSideLength; | ||
var endX = (expandedBounds.Right - Bounds.Left) / BinSideLength; | ||
var endY = (expandedBounds.Top - Bounds.Top) / BinSideLength; | ||
|
||
for (var i = startX; i <= endX; i++) | ||
{ | ||
for (var j = startY; j <= endY; j++) | ||
{ | ||
ref var bin = ref _bins[i, j]; | ||
bin ??= new Bin(); | ||
bin.Edges.Add(edge); | ||
} | ||
} | ||
} | ||
|
||
public void Query(IntRect rect, ref List<Node> nodes, ref List<Edge> edges) | ||
{ | ||
var startX = (rect.Left - Bounds.Left - BinSideLength / 2) / BinSideLength; | ||
var startY = (rect.Top - Bounds.Top - BinSideLength / 2) / BinSideLength; | ||
var endX = (rect.Right - Bounds.Left + BinSideLength / 2) / BinSideLength; | ||
var endY = (rect.Bottom - Bounds.Top + BinSideLength / 2) / BinSideLength; | ||
|
||
for (var i = startX; i <= endX; i++) | ||
{ | ||
for (var j = startY; j <= endY; j++) | ||
{ | ||
ref var bin = ref _bins[i, j]; | ||
if (bin is null) | ||
continue; | ||
foreach (var node in bin.Nodes) | ||
nodes.Add(node); | ||
foreach (var edge in bin.Edges) | ||
edges.Add(edge); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.