172 lines
4.4 KiB
C#
172 lines
4.4 KiB
C#
/*
|
|
* Copyright 2016 The Cartographer Authors
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
using RobotNet10.Shared.Numbers;
|
|
|
|
namespace CartographerSharp.Common.Math;
|
|
|
|
/// <summary>
|
|
/// 2D integer array (equivalent to Eigen::Array2i).
|
|
/// Optimized for performance with value type semantics.
|
|
/// </summary>
|
|
public struct Array2i : IEquatable<Array2i>
|
|
{
|
|
public int X { get; set; }
|
|
public int Y { get; set; }
|
|
|
|
public Array2i(int x, int y)
|
|
{
|
|
X = x;
|
|
Y = y;
|
|
}
|
|
|
|
public Array2i(Vector2 vector)
|
|
{
|
|
X = (int)vector.X;
|
|
Y = (int)vector.Y;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Zero array (0, 0).
|
|
/// </summary>
|
|
public static Array2i Zero => new(0, 0);
|
|
|
|
/// <summary>
|
|
/// Creates from Vector2 by rounding.
|
|
/// </summary>
|
|
public static Array2i FromVector2(Vector2 vector)
|
|
{
|
|
return new Array2i((int)System.Math.Round(vector.X), (int)System.Math.Round(vector.Y));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts to Vector2.
|
|
/// </summary>
|
|
public readonly Vector2 ToVector2()
|
|
{
|
|
return new Vector2(X, Y);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Element-wise addition.
|
|
/// </summary>
|
|
public static Array2i operator +(Array2i left, Array2i right)
|
|
{
|
|
return new Array2i(left.X + right.X, left.Y + right.Y);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Element-wise subtraction.
|
|
/// </summary>
|
|
public static Array2i operator -(Array2i left, Array2i right)
|
|
{
|
|
return new Array2i(left.X - right.X, left.Y - right.Y);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Element-wise multiplication.
|
|
/// </summary>
|
|
public static Array2i operator *(Array2i left, int scalar)
|
|
{
|
|
return new Array2i(left.X * scalar, left.Y * scalar);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Element-wise division.
|
|
/// </summary>
|
|
public static Array2i operator /(Array2i left, int scalar)
|
|
{
|
|
return new Array2i(left.X / scalar, left.Y / scalar);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Element-wise comparison (all elements must be less than).
|
|
/// </summary>
|
|
public static bool operator <(Array2i left, Array2i right)
|
|
{
|
|
return left.X < right.X && left.Y < right.Y;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Element-wise comparison (all elements must be less than or equal).
|
|
/// </summary>
|
|
public static bool operator <=(Array2i left, Array2i right)
|
|
{
|
|
return left.X <= right.X && left.Y <= right.Y;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Element-wise comparison (all elements must be greater than).
|
|
/// </summary>
|
|
public static bool operator >(Array2i left, Array2i right)
|
|
{
|
|
return left.X > right.X && left.Y > right.Y;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Element-wise comparison (all elements must be greater than or equal).
|
|
/// </summary>
|
|
public static bool operator >=(Array2i left, Array2i right)
|
|
{
|
|
return left.X >= right.X && left.Y >= right.Y;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Equality comparison.
|
|
/// </summary>
|
|
public static bool operator ==(Array2i left, Array2i right)
|
|
{
|
|
return left.X == right.X && left.Y == right.Y;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Inequality comparison.
|
|
/// </summary>
|
|
public static bool operator !=(Array2i left, Array2i right)
|
|
{
|
|
return !(left == right);
|
|
}
|
|
|
|
public readonly bool Equals(Array2i other)
|
|
{
|
|
return X == other.X && Y == other.Y;
|
|
}
|
|
|
|
public override readonly bool Equals(object? obj)
|
|
{
|
|
return obj is Array2i other && Equals(other);
|
|
}
|
|
|
|
public override readonly int GetHashCode()
|
|
{
|
|
return HashCode.Combine(X, Y);
|
|
}
|
|
|
|
public override readonly string ToString()
|
|
{
|
|
return $"({X}, {Y})";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deconstructs into x and y.
|
|
/// </summary>
|
|
public readonly void Deconstruct(out int x, out int y)
|
|
{
|
|
x = X;
|
|
y = Y;
|
|
}
|
|
}
|