/* * 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; /// /// 2D integer array (equivalent to Eigen::Array2i). /// Optimized for performance with value type semantics. /// public struct Array2i : IEquatable { 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; } /// /// Zero array (0, 0). /// public static Array2i Zero => new(0, 0); /// /// Creates from Vector2 by rounding. /// public static Array2i FromVector2(Vector2 vector) { return new Array2i((int)System.Math.Round(vector.X), (int)System.Math.Round(vector.Y)); } /// /// Converts to Vector2. /// public readonly Vector2 ToVector2() { return new Vector2(X, Y); } /// /// Element-wise addition. /// public static Array2i operator +(Array2i left, Array2i right) { return new Array2i(left.X + right.X, left.Y + right.Y); } /// /// Element-wise subtraction. /// public static Array2i operator -(Array2i left, Array2i right) { return new Array2i(left.X - right.X, left.Y - right.Y); } /// /// Element-wise multiplication. /// public static Array2i operator *(Array2i left, int scalar) { return new Array2i(left.X * scalar, left.Y * scalar); } /// /// Element-wise division. /// public static Array2i operator /(Array2i left, int scalar) { return new Array2i(left.X / scalar, left.Y / scalar); } /// /// Element-wise comparison (all elements must be less than). /// public static bool operator <(Array2i left, Array2i right) { return left.X < right.X && left.Y < right.Y; } /// /// Element-wise comparison (all elements must be less than or equal). /// public static bool operator <=(Array2i left, Array2i right) { return left.X <= right.X && left.Y <= right.Y; } /// /// Element-wise comparison (all elements must be greater than). /// public static bool operator >(Array2i left, Array2i right) { return left.X > right.X && left.Y > right.Y; } /// /// Element-wise comparison (all elements must be greater than or equal). /// public static bool operator >=(Array2i left, Array2i right) { return left.X >= right.X && left.Y >= right.Y; } /// /// Equality comparison. /// public static bool operator ==(Array2i left, Array2i right) { return left.X == right.X && left.Y == right.Y; } /// /// Inequality comparison. /// 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})"; } /// /// Deconstructs into x and y. /// public readonly void Deconstruct(out int x, out int y) { x = X; y = Y; } }