fix: Adds ISpanFormattable to Geometry structs (#1231)

* Adds ISpanFormattable to geometry structs and makes ToString() near-zero-allocation.
* Adds IEquatable, and Parsable to Rectangle3D to get it in-line with the other structs.

Closes #1067
This commit is contained in:
Kamron Batman 2022-11-06 17:37:30 -08:00 committed by GitHub
parent c75bde55da
commit d7d914df6c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 721 additions and 195 deletions

View file

@ -13,129 +13,164 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Buffers;
namespace Server;
[NoSort]
[Parsable]
[PropertyObject]
public struct Rectangle3D
public struct Rectangle3D : IEquatable<Rectangle3D>, ISpanFormattable
{
private Point3D m_Start;
private Point3D m_End;
private Point3D _start;
private Point3D _end;
public Rectangle3D(Point3D start, Point3D end)
{
m_Start = start;
m_End = end;
_start = start;
_end = end;
}
public Rectangle3D(int x, int y, int z, int width, int height, int depth)
{
m_Start = new Point3D(x, y, z);
m_End = new Point3D(x + width, y + height, z + depth);
_start = new Point3D(x, y, z);
_end = new Point3D(x + width, y + height, z + depth);
}
[CommandProperty(AccessLevel.Counselor)]
public Point3D Start
{
get => m_Start;
set => m_Start = value;
get => _start;
set => _start = value;
}
[CommandProperty(AccessLevel.Counselor)]
public Point3D End
{
get => m_End;
set => m_End = value;
get => _end;
set => _end = value;
}
[CommandProperty(AccessLevel.Counselor)]
public int X
{
get => m_Start.m_X;
set => m_Start.m_X = value;
get => _start.m_X;
set => _start.m_X = value;
}
[CommandProperty(AccessLevel.Counselor)]
public int Y
{
get => m_Start.m_Y;
set => m_Start.m_Y = value;
get => _start.m_Y;
set => _start.m_Y = value;
}
[CommandProperty(AccessLevel.Counselor)]
public int Z
{
get => m_Start.m_Z;
set => m_Start.m_Z = value;
get => _start.m_Z;
set => _start.m_Z = value;
}
[CommandProperty(AccessLevel.Counselor)]
public int Width => m_End.X - m_Start.X;
public int Width => _end.X - _start.X;
[CommandProperty(AccessLevel.Counselor)]
public int Height => m_End.Y - m_Start.Y;
public int Height => _end.Y - _start.Y;
[CommandProperty(AccessLevel.Counselor)]
public int Depth => m_End.Z - m_Start.Z;
public int Depth => _end.Z - _start.Z;
public bool Equals(Rectangle3D other) => _start == other._start && _end == other._end;
public override bool Equals(object obj) => obj is Rectangle3D other && Equals(other);
public static bool operator ==(Rectangle3D l, Rectangle3D r) => l._start == r._start && l._end == r._end;
public static bool operator !=(Rectangle3D l, Rectangle3D r) => l._start != r._start || l._end != r._end;
public override int GetHashCode() => HashCode.Combine(_start, _end);
public void MakeHold(Rectangle3D r)
{
if (r.m_Start.m_X < m_Start.m_X)
if (r._start.m_X < _start.m_X)
{
m_Start.m_X = r.m_Start.m_X;
_start.m_X = r._start.m_X;
}
if (r.m_Start.m_Y < m_Start.m_Y)
if (r._start.m_Y < _start.m_Y)
{
m_Start.m_Y = r.m_Start.m_Y;
_start.m_Y = r._start.m_Y;
}
if (r.m_Start.m_Z < m_Start.m_Z)
if (r._start.m_Z < _start.m_Z)
{
m_Start.m_Z = r.m_Start.m_Z;
_start.m_Z = r._start.m_Z;
}
if (r.m_End.m_X > m_End.m_X)
if (r._end.m_X > _end.m_X)
{
m_End.m_X = r.m_End.m_X;
_end.m_X = r._end.m_X;
}
if (r.m_End.m_Y > m_End.m_Y)
if (r._end.m_Y > _end.m_Y)
{
m_End.m_Y = r.m_End.m_Y;
_end.m_Y = r._end.m_Y;
}
if (r.m_End.m_Z < m_End.m_Z)
if (r._end.m_Z < _end.m_Z)
{
m_End.m_Z = r.m_End.m_Z;
_end.m_Z = r._end.m_Z;
}
}
public bool Contains(Point3D p) =>
p.m_X >= m_Start.m_X
&& p.m_X < m_End.m_X
&& p.m_Y >= m_Start.m_Y
&& p.m_Y < m_End.m_Y
&& p.m_Z >= m_Start.m_Z
&& p.m_Z < m_End.m_Z;
p.m_X >= _start.m_X
&& p.m_X < _end.m_X
&& p.m_Y >= _start.m_Y
&& p.m_Y < _end.m_Y
&& p.m_Z >= _start.m_Z
&& p.m_Z < _end.m_Z;
public bool Contains(Point2D p) =>
p.m_X >= m_Start.m_X
&& p.m_X < m_End.m_X
&& p.m_Y >= m_Start.m_Y
&& p.m_Y < m_End.m_Y;
p.m_X >= _start.m_X
&& p.m_X < _end.m_X
&& p.m_Y >= _start.m_Y
&& p.m_Y < _end.m_Y;
public bool Contains(IPoint2D p) =>
p.X >= m_Start.m_X
&& p.X < m_End.m_X
&& p.Y >= m_Start.m_Y
&& p.Y < m_End.m_Y;
p.X >= _start.m_X
&& p.X < _end.m_X
&& p.Y >= _start.m_Y
&& p.Y < _end.m_Y;
public bool Contains(IPoint3D p) =>
p.X >= m_Start.m_X
&& p.X < m_End.m_X
&& p.Y >= m_Start.m_Y
&& p.Y < m_End.m_Y
&& p.Z >= m_Start.m_Z
&& p.Z < m_End.m_Z;
p.X >= _start.m_X
&& p.X < _end.m_X
&& p.Y >= _start.m_Y
&& p.Y < _end.m_Y
&& p.Z >= _start.m_Z
&& p.Z < _end.m_Z;
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider)
=> destination.TryWrite(provider, $"({X}, {Y}, {Z})+({Width}, {Height}, {Depth})", out charsWritten);
public override string ToString()
{
// Maximum number of characters that are needed to represent this:
// 13 characters for (, , )+(, , )
// Up to 11 characters to represent each integer
const int maxLength = 13 + 11 * 6;
Span<char> span = stackalloc char[maxLength];
TryFormat(span, out var charsWritten, null, null);
return span[..charsWritten].ToString();
}
public string ToString(string format, IFormatProvider formatProvider)
{
// format and formatProvider are not doing anything right now, so use the
// default ToString implementation.
return ToString();
}
}