Files
Syntriax.Engine/Engine.Core/Primitives/Vector3DInt.cs

315 lines
17 KiB
C#

using System;
namespace Engine.Core;
/// <summary>
/// Represents a three-dimensional integer vector.
/// </summary>
/// <param name="x">X position of the <see cref="Vector3DInt"/>.</param>
/// <param name="y">Y position of the <see cref="Vector3DInt"/>.</param>
/// <param name="z">Z position of the <see cref="Vector3DInt"/>.</param>
/// <remarks>
/// Initializes a new instance of the <see cref="Vector3DInt"/> struct with the specified positions.
/// </remarks>
[System.Diagnostics.DebuggerDisplay("{ToString(),nq}")]
public readonly struct Vector3DInt(int x, int y, int z) : IEquatable<Vector3DInt>
{
/// <summary>
/// The X coordinate of the <see cref="Vector3DInt"/>.
/// </summary>
public readonly int X = x;
/// <summary>
/// The Y coordinate of the <see cref="Vector3DInt"/>.
/// </summary>
public readonly int Y = y;
/// <summary>
/// The Z coordinate of the <see cref="Vector3DInt"/>.
/// </summary>
public readonly int Z = z;
/// <summary>
/// The magnitude (length) of the <see cref="Vector3DInt"/>.
/// </summary>
public float Magnitude => Length(this);
/// <summary>
/// The squared magnitude (length) of the <see cref="Vector3DInt"/>.
/// </summary>
public float MagnitudeSquared => LengthSquared(this);
/// <summary>
/// Represents the unit <see cref="Vector3DInt"/> pointing upwards.
/// </summary>
public readonly static Vector3DInt Up = new(0, 1, 0);
/// <summary>
/// Represents the unit <see cref="Vector3DInt"/> pointing downwards.
/// </summary>
public readonly static Vector3DInt Down = new(0, -1, 0);
/// <summary>
/// Represents the unit <see cref="Vector3DInt"/> pointing leftwards.
/// </summary>
public readonly static Vector3DInt Left = new(-1, 0, 0);
/// <summary>
/// Represents the unit <see cref="Vector3DInt"/> pointing rightwards.
/// </summary>
public readonly static Vector3DInt Right = new(1, 0, 0);
/// <summary>
/// Represents the unit <see cref="Vector3DInt"/> pointing forwards.
/// </summary>
public readonly static Vector3DInt Forward = new(0, 0, 1);
/// <summary>
/// Represents the unit <see cref="Vector3DInt"/> pointing backwards.
public readonly static Vector3DInt Backward = new(0, 0, -1);
/// <summary>
/// Represents the zero <see cref="Vector3DInt"/>.
/// </summary>
public readonly static Vector3DInt Zero = new(0, 0, 0);
/// <summary>
/// Represents the <see cref="Vector3DInt"/> with both components equal to 1.
/// </summary>
public readonly static Vector3DInt One = new(1, 1, 1);
public static Vector3DInt operator -(Vector3DInt vector) => new(0 - vector.X, 0 - vector.Y, 0 - vector.Z);
public static Vector3DInt operator +(Vector3DInt left, Vector3DInt right) => new(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
public static Vector3DInt operator -(Vector3DInt left, Vector3DInt right) => new(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
public static Vector3DInt operator *(Vector3DInt vector, int value) => new(vector.X * value, vector.Y * value, vector.Z * value);
public static Vector3DInt operator *(int value, Vector3DInt vector) => new(vector.X * value, vector.Y * value, vector.Z * value);
public static Vector3DInt operator /(Vector3DInt vector, int value) => new(vector.X / value, vector.Y / value, vector.Z / value);
public static bool operator ==(Vector3DInt left, Vector3DInt right) => left.X == right.X && left.Y == right.Y && left.Z == right.Z;
public static bool operator !=(Vector3DInt left, Vector3DInt right) => left.X != right.X || left.Y != right.Y || left.Z != right.Z;
public static implicit operator Vector3DInt(Vector3D vector) => new((int)vector.X, (int)vector.Y, (int)vector.Z);
public static implicit operator Vector3DInt(Vector2DInt vector) => new(vector.X, vector.Y, 0);
/// <summary>
/// Calculates the length of the <see cref="Vector3DInt"/>.
/// </summary>
/// <param name="vector">The <see cref="Vector3DInt"/>.</param>
/// <returns>The length of the <see cref="Vector3DInt"/>.</returns>
public static float Length(Vector3DInt vector) => Math.Sqrt(LengthSquared(vector));
/// <summary>
/// Calculates the squared length of the <see cref="Vector3DInt"/>.
/// </summary>
/// <param name="vector">The <see cref="Vector3DInt"/>.</param>
/// <returns>The squared length of the <see cref="Vector3DInt"/>.</returns>
public static float LengthSquared(Vector3DInt vector) => vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z;
/// <summary>
/// Calculates the distance between two <see cref="Vector3DInt"/>s.
/// </summary>
/// <param name="from">The start <see cref="Vector3DInt"/>.</param>
/// <param name="to">The end <see cref="Vector3DInt"/>.</param>
/// <returns>The distance between the two <see cref="Vector3DInt"/>s.</returns>
public static float Distance(Vector3DInt from, Vector3DInt to) => Length(FromTo(from, to));
/// <summary>
/// Inverts the direction of the <see cref="Vector3DInt"/>.
/// </summary>
/// <param name="vector">The <see cref="Vector3DInt"/>.</param>
/// <returns>The inverted <see cref="Vector3DInt"/>.</returns>
public static Vector3DInt Invert(Vector3DInt vector) => -vector;
/// <summary>
/// Adds two <see cref="Vector3DInt"/>s.
/// </summary>
/// <param name="left">The first <see cref="Vector3DInt"/>.</param>
/// <param name="right">The second <see cref="Vector3DInt"/>.</param>
/// <returns>The sum of the two <see cref="Vector3DInt"/>s.</returns>
public static Vector3DInt Add(Vector3DInt left, Vector3DInt right) => left + right;
/// <summary>
/// Subtracts one <see cref="Vector3DInt"/> from another.
/// </summary>
/// <param name="left">The <see cref="Vector3DInt"/> to subtract from.</param>
/// <param name="right">The <see cref="Vector3DInt"/> to subtract.</param>
/// <returns>The result of subtracting the second <see cref="Vector3DInt"/> from the first.</returns>
public static Vector3DInt Subtract(Vector3DInt left, Vector3DInt right) => left - right;
/// <summary>
/// Multiplies a <see cref="Vector3DInt"/> by a scalar value.
/// </summary>
/// <param name="vector">The <see cref="Vector3DInt"/>.</param>
/// <param name="value">The scalar value.</param>
/// <returns>The result of multiplying the <see cref="Vector3DInt"/> by the scalar value.</returns>
public static Vector3DInt Multiply(Vector3DInt vector, int value) => vector * value;
/// <summary>
/// Divides a <see cref="Vector3DInt"/> by a scalar value.
/// </summary>
/// <param name="vector">The <see cref="Vector3DInt"/>.</param>
/// <param name="value">The scalar value.</param>
/// <returns>The result of dividing the <see cref="Vector3DInt"/> by the scalar value.</returns>
public static Vector3DInt Divide(Vector3DInt vector, int value) => vector / value;
/// <summary>
/// Calculates the absolute value of each component of the vector.
/// </summary>
/// <param name="vector">The <see cref="Vector3DInt"/>.</param>
/// <returns>The <see cref="Vector3DInt"/> with each component's absolute value.</returns>
public static Vector3DInt Abs(Vector3DInt vector) => new(Math.Abs(vector.X), Math.Abs(vector.Y), Math.Abs(vector.Z));
/// <summary>
/// Calculates the <see cref="Vector3DInt"/> from one point to another.
/// </summary>
/// <param name="from">The starting point.</param>
/// <param name="to">The ending point.</param>
/// <returns>The <see cref="Vector3DInt"/> from the starting point to the ending point.</returns>
public static Vector3DInt FromTo(Vector3DInt from, Vector3DInt to) => to - from;
/// <summary>
/// Scales a <see cref="Vector3DInt"/> by another <see cref="Vector3DInt"/> component-wise.
/// </summary>
/// <param name="vector">The <see cref="Vector3DInt"/> to scale.</param>
/// <param name="scale">The <see cref="Vector3DInt"/> containing the scaling factors for each component.</param>
/// <returns>The scaled <see cref="Vector3DInt"/>.</returns>
public static Vector3DInt Scale(Vector3DInt vector, Vector3DInt scale) => new(vector.X * scale.X, vector.Y * scale.Y, vector.Z * scale.Z);
/// <summary>
/// Returns the component-wise minimum of two <see cref="Vector3DInt"/>s.
/// </summary>
/// <param name="left">The first <see cref="Vector3DInt"/>.</param>
/// <param name="right">The second <see cref="Vector3DInt"/>.</param>
/// <returns>The <see cref="Vector3DInt"/> containing the minimum components from both input <see cref="Vector3DInt"/>s.</returns>
public static Vector3DInt Min(Vector3DInt left, Vector3DInt right) => new((left.X < right.X) ? left.X : right.X, (left.Y < right.Y) ? left.Y : right.Y, (left.Z < right.Z) ? left.Z : right.Z);
/// <summary>
/// Returns the component-wise maximum of two <see cref="Vector3DInt"/>s.
/// </summary>
/// <param name="left">The first <see cref="Vector3DInt"/>.</param>
/// <param name="right">The second <see cref="Vector3DInt"/>.</param>
/// <returns>The <see cref="Vector3DInt"/> containing the maximum components from both input <see cref="Vector3DInt"/>s.</returns>
public static Vector3DInt Max(Vector3DInt left, Vector3DInt right) => new((left.X > right.X) ? left.X : right.X, (left.Y > right.Y) ? left.Y : right.Y, (left.Z > right.Z) ? left.Z : right.Z);
/// <summary>
/// Clamps each component of a <see cref="Vector3DInt"/> between the corresponding component of two other <see cref="Vector3DInt"/>s.
/// </summary>
/// <param name="vector">The <see cref="Vector3DInt"/> to clamp.</param>
/// <param name="min">The <see cref="Vector3DInt"/> representing the minimum values for each component.</param>
/// <param name="max">The <see cref="Vector3DInt"/> representing the maximum values for each component.</param>
/// <returns>A <see cref="Vector3DInt"/> with each component clamped between the corresponding components of the min and max <see cref="Vector3DInt"/>s.</returns>
public static Vector3DInt Clamp(Vector3DInt vector, Vector3DInt min, Vector3DInt max) => new(Math.Clamp(vector.X, min.X, max.X), Math.Clamp(vector.Y, min.Y, max.Y), Math.Clamp(vector.Z, min.Z, max.Z));
/// <summary>
/// Performs linear interpolation between two <see cref="Vector3DInt"/>s.
/// </summary>
/// <param name="from">The starting <see cref="Vector3DInt"/> (t = 0).</param>
/// <param name="to">The ending <see cref="Vector3DInt"/> (t = 1).</param>
/// <param name="t">The interpolation parameter.</param>
/// <returns>The interpolated <see cref="Vector3DInt"/>.</returns>
public static Vector3DInt Lerp(Vector3DInt from, Vector3DInt to, int t) => from + FromTo(from, to) * t;
/// <summary>
/// Calculates the cross product of two <see cref="Vector3DInt"/>s.
/// </summary>
/// <param name="left">The first <see cref="Vector3DInt"/>.</param>
/// <param name="right">The second <see cref="Vector3DInt"/>.</param>
/// <returns>The cross product of the two <see cref="Vector3DInt"/>s.</returns>
public static Vector3DInt Cross(Vector3DInt left, Vector3DInt right) => new(left.Y * right.Z - left.Z * right.Y, left.Z * right.X - left.X * right.Z, left.X * right.Y - left.Y * right.X);
/// <summary>
/// Calculates the angle between two <see cref="Vector3DInt"/>s.
/// </summary>
/// <param name="left">The first <see cref="Vector3DInt"/>.</param>
/// <param name="right">The second <see cref="Vector3DInt"/>.</param>
/// <returns>The angle between the two <see cref="Vector3DInt"/>s in radians.</returns>
public static float Angle(Vector3DInt left, Vector3DInt right) => Math.Acos(Dot(left, right) / (Length(left) * Length(right)));
/// <summary>
/// Calculates the dot product of two <see cref="Vector3DInt"/>s.
/// </summary>
/// <param name="left">The first <see cref="Vector3DInt"/>.</param>
/// <param name="right">The second <see cref="Vector3DInt"/>.</param>
/// <returns>The dot product of the two <see cref="Vector3DInt"/>s.</returns>
public static int Dot(Vector3DInt left, Vector3DInt right) => left.X * right.X + left.Y * right.Y + left.Z * right.Z;
/// <summary>
/// Determines whether the specified object is equal to the current <see cref="Vector3DInt"/>.
/// </summary>
/// <param name="obj">The object to compare with the current <see cref="Vector3DInt"/>.</param>
/// <returns><see cref="true"/> if the specified object is equal to the current <see cref="Vector3DInt"/>; otherwise, <see cref="false"/>.</returns>
public override bool Equals(object? obj) => obj is Vector3DInt vector3D && this == vector3D;
public bool Equals(Vector3DInt other) => this == other;
/// <summary>
/// Generates a hash code for the <see cref="Vector3DInt"/>.
/// </summary>
/// <returns>A hash code for the <see cref="Vector3DInt"/>.</returns>
public override int GetHashCode() => System.HashCode.Combine(X, Y, Z);
/// <summary>
/// Converts the <see cref="Vector3DInt"/> to its string representation.
/// </summary>
/// <returns>A string representation of the <see cref="Vector3DInt"/>.</returns>
public override string ToString() => $"{nameof(Vector3DInt)}({X}, {Y}, {Z})";
}
/// <summary>
/// Provides extension methods for <see cref="Vector3DInt"/> type.
/// </summary>
public static class Vector3DIntExtensions
{
/// <inheritdoc cref="Vector3DInt.Length(Vector3DInt)" />
public static float Length(this Vector3DInt vector) => Vector3DInt.Length(vector);
/// <inheritdoc cref="Vector3DInt.LengthSquared(Vector3DInt)" />
public static float LengthSquared(this Vector3DInt vector) => Vector3DInt.LengthSquared(vector);
/// <inheritdoc cref="Vector3DInt.Distance(Vector3DInt, Vector3DInt)" />
public static float Distance(this Vector3DInt from, Vector3DInt to) => Vector3DInt.Distance(from, to);
/// <inheritdoc cref="Vector3DInt.Invert(Vector3DInt)" />
public static Vector3DInt Invert(this Vector3DInt vector) => Vector3DInt.Invert(vector);
/// <inheritdoc cref="Vector3DInt.Add(Vector3DInt, Vector3DInt)" />
public static Vector3DInt Add(this Vector3DInt vector, Vector3DInt vectorToAdd) => Vector3DInt.Add(vector, vectorToAdd);
/// <inheritdoc cref="Vector3DInt.Subtract(Vector3DInt, Vector3DInt)" />
public static Vector3DInt Subtract(this Vector3DInt vector, Vector3DInt vectorToSubtract) => Vector3DInt.Subtract(vector, vectorToSubtract);
/// <inheritdoc cref="Vector3DInt.Multiply(Vector3DInt, int)" />
public static Vector3DInt Multiply(this Vector3DInt vector, int value) => Vector3DInt.Multiply(vector, value);
/// <inheritdoc cref="Vector3DInt.Divide(Vector3DInt, int)" />
public static Vector3DInt Divide(this Vector3DInt vector, int value) => Vector3DInt.Divide(vector, value);
/// <inheritdoc cref="Vector3DInt.Abs(Vector3DInt)" />
public static Vector3DInt Abs(this Vector3DInt vector) => Vector3DInt.Abs(vector);
/// <inheritdoc cref="Vector3DInt.FromTo(Vector3DInt, Vector3DInt)" />
public static Vector3DInt FromTo(this Vector3DInt from, Vector3DInt to) => Vector3DInt.FromTo(from, to);
/// <inheritdoc cref="Vector3DInt.Scale(Vector3DInt, Vector3DInt)" />
public static Vector3DInt Scale(this Vector3DInt vector, Vector3DInt scale) => Vector3DInt.Scale(vector, scale);
/// <inheritdoc cref="Vector3DInt.Min(Vector3DInt, Vector3DInt)" />
public static Vector3DInt Min(this Vector3DInt left, Vector3DInt right) => Vector3DInt.Min(left, right);
/// <inheritdoc cref="Vector3DInt.Max(Vector3DInt, Vector3DInt)" />
public static Vector3DInt Max(this Vector3DInt left, Vector3DInt right) => Vector3DInt.Max(left, right);
/// <inheritdoc cref="Vector3DInt.Clamp(Vector3DInt, Vector3DInt, Vector3DInt)" />
public static Vector3DInt Clamp(this Vector3DInt vector, Vector3DInt min, Vector3DInt max) => Vector3DInt.Clamp(vector, min, max);
/// <inheritdoc cref="Vector3DInt.Lerp(Vector3DInt, Vector3DInt, int)" />
public static Vector3DInt Lerp(this Vector3DInt from, Vector3DInt to, int t) => Vector3DInt.Lerp(from, to, t);
/// <inheritdoc cref="Vector3DInt.Cross(Vector3DInt, Vector3DInt)" />
public static Vector3DInt Cross(this Vector3DInt left, Vector3DInt right) => Vector3DInt.Cross(left, right);
/// <inheritdoc cref="Vector3DInt.Angle(Vector3DInt, Vector3DInt)" />
public static float AngleBetween(this Vector3DInt left, Vector3DInt right) => Vector3DInt.Angle(left, right);
/// <inheritdoc cref="Vector3DInt.Dot(Vector3DInt, Vector3DInt)" />
public static int Dot(this Vector3DInt left, Vector3DInt right) => Vector3DInt.Dot(left, right);
}