Initial commit
This commit is contained in:
commit
ef3b7d68fb
30 changed files with 1568 additions and 0 deletions
108
Domain/TileManager.cs
Normal file
108
Domain/TileManager.cs
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
using StitchATon2.Infra;
|
||||
using StitchATon2.Infra.Buffers;
|
||||
|
||||
namespace StitchATon2.Domain;
|
||||
|
||||
public sealed class TileManager : IDisposable
|
||||
{
|
||||
private readonly IMemoryOwner<Tile> _tiles;
|
||||
|
||||
public Configuration Configuration { get; }
|
||||
|
||||
public TileManager(Configuration config)
|
||||
{
|
||||
Configuration = config;
|
||||
_tiles = MemoryAllocator.AllocateManaged<Tile>(config.TileCount);
|
||||
var tilesSpan = _tiles.Memory.Span;
|
||||
for (var id = 0; id < config.TileCount; id++)
|
||||
tilesSpan[id] = CreateTile(id);
|
||||
|
||||
Console.WriteLine("Tile manager created");
|
||||
}
|
||||
|
||||
~TileManager() => Dispose();
|
||||
|
||||
private Tile CreateTile(int id)
|
||||
{
|
||||
var (row, column) = int.DivRem(id, Configuration.Columns);
|
||||
var coordinate = $"{Utils.GetSBSNotation(++row)}{++column}";
|
||||
return new Tile
|
||||
{
|
||||
Id = id,
|
||||
Row = row,
|
||||
Column = column,
|
||||
Coordinate = coordinate,
|
||||
Integral = new ImageIntegral(
|
||||
imagePath: Configuration.GetAssetPath($"{coordinate}.png"),
|
||||
outputDirectory: Configuration.CachePath,
|
||||
width: Configuration.Width,
|
||||
height: Configuration.Height
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private int GetId(int column, int row) => column - 1 + (row - 1) * Configuration.Columns;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Tile GetTile(int id) => _tiles.Memory.Span[id];
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Tile GetTile(int column, int row) => GetTile(GetId(column, row));
|
||||
|
||||
public Tile GetTile(string coordinate)
|
||||
{
|
||||
var (column, row) = Utils.GetSBSCoordinate(coordinate);
|
||||
return GetTile(column, row);
|
||||
}
|
||||
|
||||
public Tile? TryGetAdjacent(Tile tile, int columnOffset, int rowOffset)
|
||||
{
|
||||
var column = tile.Column + columnOffset;
|
||||
if(column <= 0 || column > Configuration.Columns)
|
||||
return null;
|
||||
|
||||
var row = tile.Row + rowOffset;
|
||||
if(row <= 0 || row > Configuration.Rows)
|
||||
return null;
|
||||
|
||||
return GetTile(column, row);
|
||||
}
|
||||
|
||||
public (Tile TopLeft, Tile BottomRight) GetTilePair(string coordinatePair)
|
||||
{
|
||||
var index = coordinatePair.IndexOf(':');
|
||||
var topLeft = GetTile(coordinatePair[..index++]);
|
||||
var bottomRight = GetTile(coordinatePair[index..]);
|
||||
|
||||
return (topLeft, bottomRight);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Tile GetAdjacent(Tile tile, int columnOffset, int rowOffset)
|
||||
{
|
||||
return GetTile(tile.Column + columnOffset, tile.Row + rowOffset);
|
||||
}
|
||||
|
||||
public GridSection CreateSection(
|
||||
string coordinatePair,
|
||||
float cropX,
|
||||
float cropY,
|
||||
float cropWidth,
|
||||
float cropHeight)
|
||||
=> new(
|
||||
this,
|
||||
coordinatePair,
|
||||
cropX,
|
||||
cropY,
|
||||
cropWidth,
|
||||
cropHeight);
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_tiles.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue