mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-25 12:38:24 +01:00
106 lines
3.3 KiB
C#
106 lines
3.3 KiB
C#
// <auto-generated /> - edit Vector.tt to modify
|
|
#nullable enable
|
|
|
|
namespace Brutal.Numerics;
|
|
|
|
/// <summary>
|
|
/// Implements a 2D vector with byte components.
|
|
/// </summary>
|
|
[DebuggerTypeProxy(typeof(VectorDebugView2<byte2, byte, float>))]
|
|
public partial struct byte2 : IVector2<byte2, byte>
|
|
, IIntegerVector<byte2, byte, int, float>
|
|
{
|
|
/// <inheritdoc />
|
|
public static int ComponentSize => sizeof(byte);
|
|
|
|
/// <summary>Gets the number of components.</summary>
|
|
public static int ComponentCount => 2;
|
|
|
|
/// <inheritdoc cref="Vector2.X" />
|
|
[XmlAttribute]
|
|
public byte X;
|
|
|
|
/// <inheritdoc cref="Vector2.Y" />
|
|
[XmlAttribute]
|
|
public byte Y;
|
|
|
|
byte IVector2<byte2, byte>.X { readonly get => X; set => X = value; }
|
|
|
|
byte IVector2<byte2, byte>.Y { readonly get => Y; set => Y = value; }
|
|
|
|
/// <inheritdoc cref="Vector2(float, float)" />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public byte2(byte x, byte y)
|
|
{
|
|
X = x;
|
|
Y = y;
|
|
}
|
|
|
|
/// <inheritdoc cref="Vector2(float)" />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public byte2(byte xy)
|
|
: this(xy, xy) { }
|
|
|
|
/// <summary>Creates a vector from the leading 2 elements of a span.</summary>
|
|
/// <param name="span">The elements to read; must contain at least 2, and any beyond that are ignored.</param>
|
|
/// <exception cref="ArgumentOutOfRangeException"><paramref name="span" /> holds fewer than 2 elements.</exception>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public byte2(ReadOnlySpan<byte> span)
|
|
{
|
|
if (span.Length < 2)
|
|
throw new ArgumentOutOfRangeException(nameof(span), "Length must be at least 2.");
|
|
|
|
this = Unsafe.ReadUnaligned<byte2>(ref Unsafe.As<byte, byte>(ref MemoryMarshal.GetReference(span)));
|
|
}
|
|
|
|
/// <inheritdoc cref="byte2(ReadOnlySpan{byte})" />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
static byte2 IComponentTuple<byte2, byte>.Create(ReadOnlySpan<byte> span) => new(span);
|
|
|
|
/// <inheritdoc />
|
|
public static byte2 UnitX => new(1, 0);
|
|
|
|
/// <inheritdoc />
|
|
public static byte2 UnitY => new(0, 1);
|
|
|
|
/// <inheritdoc />
|
|
public byte this[int index]
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
readonly get
|
|
{
|
|
if ((uint)index >= ComponentCount)
|
|
throw new ArgumentOutOfRangeException(nameof(index));
|
|
|
|
return Unsafe.Add(ref Unsafe.AsRef(in X), index);
|
|
}
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
set
|
|
{
|
|
if ((uint)index >= ComponentCount)
|
|
throw new ArgumentOutOfRangeException(nameof(index));
|
|
|
|
Unsafe.Add(ref X, index) = value;
|
|
}
|
|
}
|
|
|
|
public readonly void Deconstruct(out byte x, out byte y)
|
|
{
|
|
x = X;
|
|
y = Y;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public Span<byte> AsSpan()
|
|
{
|
|
return MemoryMarshal.CreateSpan(ref Unsafe.As<byte2, byte>(ref this), 2);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public readonly ReadOnlySpan<byte> AsReadOnlySpan()
|
|
{
|
|
return MemoryMarshal.CreateReadOnlySpan(in Unsafe.As<byte2, byte>(ref Unsafe.AsRef(in this)), 2);
|
|
}
|
|
}
|