// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace FlaxEngine
{
///
/// Represents a four dimensional mathematical vector (signed integers).
///
[Serializable]
#if FLAX_EDITOR
[System.ComponentModel.TypeConverter(typeof(TypeConverters.Int4Converter))]
#endif
partial struct Int4 : IEquatable, IFormattable
{
private static readonly string _formatString = "X:{0} Y:{1} Z:{2} W:{3}";
///
/// The size of the type, in bytes.
///
public static readonly int SizeInBytes = Marshal.SizeOf(typeof(Int4));
///
/// A with all of its components set to zero.
///
public static readonly Int4 Zero;
///
/// The X unit (1, 0, 0, 0).
///
public static readonly Int4 UnitX = new Int4(1, 0, 0, 0);
///
/// The Y unit (0, 1, 0, 0).
///
public static readonly Int4 UnitY = new Int4(0, 1, 0, 0);
///
/// The Z unit (0, 0, 1, 0).
///
public static readonly Int4 UnitZ = new Int4(0, 0, 1, 0);
///
/// The W unit (0, 0, 0, 1).
///
public static readonly Int4 UnitW = new Int4(0, 0, 0, 1);
///
/// A with all of its components set to one.
///
public static readonly Int4 One = new Int4(1, 1, 1, 1);
///
/// A with all components equal to .
///
public static readonly Int4 Minimum = new Int4(int.MinValue);
///
/// A with all components equal to .
///
public static readonly Int4 Maximum = new Int4(int.MaxValue);
///
/// Initializes a new instance of the struct.
///
/// The value that will be assigned to all components.
public Int4(int value)
{
X = value;
Y = value;
Z = value;
W = value;
}
///
/// Initializes a new instance of the struct.
///
/// Initial value for the X component of the vector.
/// Initial value for the Y component of the vector.
/// Initial value for the Z component of the vector.
/// Initial value for the W component of the vector.
public Int4(int x, int y, int z, int w)
{
X = x;
Y = y;
Z = z;
W = w;
}
///
/// Initializes a new instance of the struct.
///
/// A vector containing the values with which to initialize the X, Y, and Z components.
/// Initial value for the W component of the vector.
public Int4(Int3 value, int w)
{
X = value.X;
Y = value.Y;
Z = value.Z;
W = w;
}
///
/// Initializes a new instance of the struct.
///
/// A vector containing the values with which to initialize the X and Y components.
/// Initial value for the Z component of the vector.
/// Initial value for the W component of the vector.
public Int4(Int2 value, int z, int w)
{
X = value.X;
Y = value.Y;
Z = z;
W = w;
}
///
/// Initializes a new instance of the struct.
///
/// The values to assign to the X, Y, Z, and W components of the vector. This must be an array with four elements.
/// Thrown when is null.
/// Thrown when contains more or less than four elements.
public Int4(int[] values)
{
if (values == null)
throw new ArgumentNullException(nameof(values));
if (values.Length != 4)
throw new ArgumentOutOfRangeException(nameof(values), "There must be four and only four input values for Int4.");
X = values[0];
Y = values[1];
Z = values[2];
W = values[3];
}
///
/// Gets a value indicting whether this vector is zero
///
public bool IsZero => Mathf.IsZero(X) && Mathf.IsZero(Y) && Mathf.IsZero(Z) && Mathf.IsZero(W);
///
/// Gets a value indicting whether this vector is one
///
public bool IsOne => Mathf.IsOne(X) && Mathf.IsOne(Y) && Mathf.IsOne(Z) && Mathf.IsOne(W);
///
/// Gets a minimum component value
///
public int MinValue => Mathf.Min(X, Mathf.Min(Y, Mathf.Min(Z, W)));
///
/// Gets a maximum component value
///
public int MaxValue => Mathf.Max(X, Mathf.Max(Y, Mathf.Max(Z, W)));
///
/// Gets a sum of the component values.
///
public int ValuesSum => X + Y + Z + W;
///
/// Gets or sets the component at the specified index.
///
/// The value of the X, Y, Z, or W component, depending on the index.
/// The index of the component to access. Use 0 for the X component, 1 for the Y component, 2 for the Z component, and 3 for the W component.
/// The value of the component at the specified index.
/// Thrown when the is out of the range [0,3].
public int this[int index]
{
get
{
switch (index)
{
case 0: return X;
case 1: return Y;
case 2: return Z;
case 3: return W;
}
throw new ArgumentOutOfRangeException(nameof(index), "Indices for Int4 run from 0 to 3, inclusive.");
}
set
{
switch (index)
{
case 0:
X = value;
break;
case 1:
Y = value;
break;
case 2:
Z = value;
break;
case 3:
W = value;
break;
default: throw new ArgumentOutOfRangeException(nameof(index), "Indices for Int4 run from 0 to 3, inclusive.");
}
}
}
///
/// Creates an array containing the elements of the vector.
///
/// A four-element array containing the components of the vector.
public int[] ToArray()
{
return new[] { X, Y, Z, W };
}
///
/// Adds two vectors.
///
/// The first vector to add.
/// The second vector to add.
/// When the method completes, contains the sum of the two vectors.
public static void Add(ref Int4 left, ref Int4 right, out Int4 result)
{
result = new Int4(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W);
}
///
/// Adds two vectors.
///
/// The first vector to add.
/// The second vector to add.
/// The sum of the two vectors.
public static Int4 Add(Int4 left, Int4 right)
{
return new Int4(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W);
}
///
/// Perform a component-wise addition
///
/// The input vector
/// The scalar value to be added to elements
/// The vector with added scalar for each element.
public static void Add(ref Int4 left, ref int right, out Int4 result)
{
result = new Int4(left.X + right, left.Y + right, left.Z + right, left.W + right);
}
///
/// Perform a component-wise addition
///
/// The input vector
/// The scalar value to be added to elements
/// The vector with added scalar for each element.
public static Int4 Add(Int4 left, int right)
{
return new Int4(left.X + right, left.Y + right, left.Z + right, left.W + right);
}
///
/// Subtracts two vectors.
///
/// The first vector to subtract.
/// The second vector to subtract.
/// When the method completes, contains the difference of the two vectors.
public static void Subtract(ref Int4 left, ref Int4 right, out Int4 result)
{
result = new Int4(left.X - right.X, left.Y - right.Y, left.Z - right.Z, left.W - right.W);
}
///
/// Subtracts two vectors.
///
/// The first vector to subtract.
/// The second vector to subtract.
/// The difference of the two vectors.
public static Int4 Subtract(Int4 left, Int4 right)
{
return new Int4(left.X - right.X, left.Y - right.Y, left.Z - right.Z, left.W - right.W);
}
///
/// Perform a component-wise subtraction
///
/// The input vector
/// The scalar value to be subtracted from elements
/// The vector with subtracted scalar for each element.
public static void Subtract(ref Int4 left, ref int right, out Int4 result)
{
result = new Int4(left.X - right, left.Y - right, left.Z - right, left.W - right);
}
///
/// Perform a component-wise subtraction
///
/// The input vector
/// The scalar value to be subtracted from elements
/// The vector with subtracted scalar for each element.
public static Int4 Subtract(Int4 left, int right)
{
return new Int4(left.X - right, left.Y - right, left.Z - right, left.W - right);
}
///
/// Perform a component-wise subtraction
///
/// The scalar value to be subtracted from elements
/// The input vector.
/// The vector with subtracted scalar for each element.
public static void Subtract(ref int left, ref Int4 right, out Int4 result)
{
result = new Int4(left - right.X, left - right.Y, left - right.Z, left - right.W);
}
///
/// Perform a component-wise subtraction
///
/// The scalar value to be subtracted from elements
/// The input vector.
/// The vector with subtracted scalar for each element.
public static Int4 Subtract(int left, Int4 right)
{
return new Int4(left - right.X, left - right.Y, left - right.Z, left - right.W);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// When the method completes, contains the scaled vector.
public static void Multiply(ref Int4 value, int scale, out Int4 result)
{
result = new Int4(value.X * scale, value.Y * scale, value.Z * scale, value.W * scale);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The scaled vector.
public static Int4 Multiply(Int4 value, int scale)
{
return new Int4(value.X * scale, value.Y * scale, value.Z * scale, value.W * scale);
}
///
/// Multiplies a vector with another by performing component-wise multiplication.
///
/// The first vector to multiply.
/// The second vector to multiply.
/// When the method completes, contains the multiplied vector.
public static void Multiply(ref Int4 left, ref Int4 right, out Int4 result)
{
result = new Int4(left.X * right.X, left.Y * right.Y, left.Z * right.Z, left.W * right.W);
}
///
/// Multiplies a vector with another by performing component-wise multiplication.
///
/// The first vector to multiply.
/// The second vector to multiply.
/// The multiplied vector.
public static Int4 Multiply(Int4 left, Int4 right)
{
return new Int4(left.X * right.X, left.Y * right.Y, left.Z * right.Z, left.W * right.W);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// When the method completes, contains the scaled vector.
public static void Divide(ref Int4 value, int scale, out Int4 result)
{
result = new Int4(value.X / scale, value.Y / scale, value.Z / scale, value.W / scale);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The scaled vector.
public static Int4 Divide(Int4 value, int scale)
{
return new Int4(value.X / scale, value.Y / scale, value.Z / scale, value.W / scale);
}
///
/// Scales a vector by the given value.
///
/// The amount by which to scale the vector.
/// The vector to scale.
/// When the method completes, contains the scaled vector.
public static void Divide(int scale, ref Int4 value, out Int4 result)
{
result = new Int4(scale / value.X, scale / value.Y, scale / value.Z, scale / value.W);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The scaled vector.
public static Int4 Divide(int scale, Int4 value)
{
return new Int4(scale / value.X, scale / value.Y, scale / value.Z, scale / value.W);
}
///
/// Reverses the direction of a given vector.
///
/// The vector to negate.
/// When the method completes, contains a vector facing in the opposite direction.
public static void Negate(ref Int4 value, out Int4 result)
{
result = new Int4(-value.X, -value.Y, -value.Z, -value.W);
}
///
/// Reverses the direction of a given vector.
///
/// The vector to negate.
/// A vector facing in the opposite direction.
public static Int4 Negate(Int4 value)
{
return new Int4(-value.X, -value.Y, -value.Z, -value.W);
}
///
/// Restricts a value to be within a specified range.
///
/// The value to clamp.
/// The minimum value.
/// The maximum value.
/// When the method completes, contains the clamped value.
public static void Clamp(ref Int4 value, ref Int4 min, ref Int4 max, out Int4 result)
{
int x = value.X;
x = x > max.X ? max.X : x;
x = x < min.X ? min.X : x;
int y = value.Y;
y = y > max.Y ? max.Y : y;
y = y < min.Y ? min.Y : y;
int z = value.Z;
z = z > max.Z ? max.Z : z;
z = z < min.Z ? min.Z : z;
int w = value.W;
w = w > max.W ? max.W : w;
w = w < min.W ? min.W : w;
result = new Int4(x, y, z, w);
}
///
/// Restricts a value to be within a specified range.
///
/// The value to clamp.
/// The minimum value.
/// The maximum value.
/// The clamped value.
public static Int4 Clamp(Int4 value, Int4 min, Int4 max)
{
Clamp(ref value, ref min, ref max, out Int4 result);
return result;
}
///
/// Returns a vector containing the largest components of the specified vectors.
///
/// The first source vector.
/// The second source vector.
/// When the method completes, contains an new vector composed of the largest components of the source vectors.
public static void Max(ref Int4 left, ref Int4 right, out Int4 result)
{
result.X = left.X > right.X ? left.X : right.X;
result.Y = left.Y > right.Y ? left.Y : right.Y;
result.Z = left.Z > right.Z ? left.Z : right.Z;
result.W = left.W > right.W ? left.W : right.W;
}
///
/// Returns a vector containing the largest components of the specified vectors.
///
/// The first source vector.
/// The second source vector.
/// A vector containing the largest components of the source vectors.
public static Int4 Max(Int4 left, Int4 right)
{
Max(ref left, ref right, out Int4 result);
return result;
}
///
/// Returns a vector containing the smallest components of the specified vectors.
///
/// The first source vector.
/// The second source vector.
/// When the method completes, contains an new vector composed of the smallest components of the source vectors.
public static void Min(ref Int4 left, ref Int4 right, out Int4 result)
{
result.X = left.X < right.X ? left.X : right.X;
result.Y = left.Y < right.Y ? left.Y : right.Y;
result.Z = left.Z < right.Z ? left.Z : right.Z;
result.W = left.W < right.W ? left.W : right.W;
}
///
/// Returns a vector containing the smallest components of the specified vectors.
///
/// The first source vector.
/// The second source vector.
/// A vector containing the smallest components of the source vectors.
public static Int4 Min(Int4 left, Int4 right)
{
Min(ref left, ref right, out Int4 result);
return result;
}
///
/// Returns the absolute value of a vector.
///
/// The value.
/// A vector which components are less or equal to 0.
public static Int4 Abs(Int4 v)
{
return new Int4(Math.Abs(v.X), Math.Abs(v.Y), Math.Abs(v.Z), Math.Abs(v.W));
}
///
/// Adds two vectors.
///
/// The first vector to add.
/// The second vector to add.
/// The sum of the two vectors.
public static Int4 operator +(Int4 left, Int4 right)
{
return new Int4(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W);
}
///
/// Multiplies a vector with another by performing component-wise multiplication equivalent to
/// .
///
/// The first vector to multiply.
/// The second vector to multiply.
/// The multiplication of the two vectors.
public static Int4 operator *(Int4 left, Int4 right)
{
return new Int4(left.X * right.X, left.Y * right.Y, left.Z * right.Z, left.W * right.W);
}
///
/// Assert a vector (return it unchanged).
///
/// The vector to assert (unchanged).
/// The asserted (unchanged) vector.
public static Int4 operator +(Int4 value)
{
return value;
}
///
/// Subtracts two vectors.
///
/// The first vector to subtract.
/// The second vector to subtract.
/// The difference of the two vectors.
public static Int4 operator -(Int4 left, Int4 right)
{
return new Int4(left.X - right.X, left.Y - right.Y, left.Z - right.Z, left.W - right.W);
}
///
/// Reverses the direction of a given vector.
///
/// The vector to negate.
/// A vector facing in the opposite direction.
public static Int4 operator -(Int4 value)
{
return new Int4(-value.X, -value.Y, -value.Z, -value.W);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The scaled vector.
public static Int4 operator *(int scale, Int4 value)
{
return new Int4(value.X * scale, value.Y * scale, value.Z * scale, value.W * scale);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The scaled vector.
public static Int4 operator *(Int4 value, int scale)
{
return new Int4(value.X * scale, value.Y * scale, value.Z * scale, value.W * scale);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The scaled vector.
public static Int4 operator /(Int4 value, int scale)
{
return new Int4(value.X / scale, value.Y / scale, value.Z / scale, value.W / scale);
}
///
/// Scales a vector by the given value.
///
/// The amount by which to scale the vector.
/// The vector to scale.
/// The scaled vector.
public static Int4 operator /(int scale, Int4 value)
{
return new Int4(scale / value.X, scale / value.Y, scale / value.Z, scale / value.W);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The scaled vector.
public static Int4 operator /(Int4 value, Int4 scale)
{
return new Int4(value.X / scale.X, value.Y / scale.Y, value.Z / scale.Z, value.W / scale.W);
}
///
/// Remainder of value divided by scale.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The remained vector.
public static Int4 operator %(Int4 value, float scale)
{
return new Int4((int)(value.X % scale), (int)(value.Y % scale), (int)(value.Z % scale), (int)(value.W % scale));
}
///
/// Remainder of value divided by scale.
///
/// The amount by which to scale the vector.
/// The vector to scale.
/// The remained vector.
public static Int4 operator %(float value, Int4 scale)
{
return new Int4((int)(value % scale.X), (int)(value % scale.Y), (int)(value % scale.Z), (int)(value % scale.W));
}
///
/// Remainder of value divided by scale.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The remained vector.
public static Int4 operator %(Int4 value, Int4 scale)
{
return new Int4(value.X % scale.X, value.Y % scale.Y, value.Z % scale.Z, value.W % scale.W);
}
///
/// Perform a component-wise addition
///
/// The input vector.
/// The scalar value to be added on elements
/// The vector with added scalar for each element.
public static Int4 operator +(Int4 value, int scalar)
{
return new Int4(value.X + scalar, value.Y + scalar, value.Z + scalar, value.W + scalar);
}
///
/// Perform a component-wise addition
///
/// The input vector.
/// The scalar value to be added on elements
/// The vector with added scalar for each element.
public static Int4 operator +(int scalar, Int4 value)
{
return new Int4(scalar + value.X, scalar + value.Y, scalar + value.Z, scalar + value.W);
}
///
/// Perform a component-wise subtraction
///
/// The input vector.
/// The scalar value to be subtracted from elements
/// The vector with subtracted scalar from each element.
public static Int4 operator -(Int4 value, int scalar)
{
return new Int4(value.X - scalar, value.Y - scalar, value.Z - scalar, value.W - scalar);
}
///
/// Perform a component-wise subtraction
///
/// The input vector.
/// The scalar value to be subtracted from elements
/// The vector with subtracted scalar from each element.
public static Int4 operator -(int scalar, Int4 value)
{
return new Int4(scalar - value.X, scalar - value.Y, scalar - value.Z, scalar - value.W);
}
///
/// Tests for equality between two objects.
///
/// The first value to compare.
/// The second value to compare.
/// true if has the same value as ; otherwise, false.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Int4 left, Int4 right)
{
return left.Equals(ref right);
}
///
/// Tests for inequality between two objects.
///
/// The first value to compare.
/// The second value to compare.
/// true if has a different value than ; otherwise, false.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Int4 left, Int4 right)
{
return !left.Equals(ref right);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Int2(Int4 value)
{
return new Int2(value.X, value.Y);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Int3(Int4 value)
{
return new Int3(value.X, value.Y, value.Z);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Float2(Int4 value)
{
return new Float2(value.X, value.Y);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Float3(Int4 value)
{
return new Float3(value.X, value.Y, value.Z);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Float4(Int4 value)
{
return new Float4(value.X, value.Y, value.Z, value.W);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Vector2(Int4 value)
{
return new Vector2(value.X, value.Y);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Vector3(Int4 value)
{
return new Vector3(value.X, value.Y, value.Z);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Vector4(Int4 value)
{
return new Vector4(value.X, value.Y, value.Z, value.W);
}
///
/// Returns a that represents this instance.
///
/// A that represents this instance.
public override string ToString()
{
return string.Format(CultureInfo.CurrentCulture, _formatString, X, Y, Z, W);
}
///
/// Returns a that represents this instance.
///
/// The format.
/// A that represents this instance.
public string ToString(string format)
{
if (format == null)
return ToString();
return string.Format(CultureInfo.CurrentCulture, _formatString, X.ToString(format, CultureInfo.CurrentCulture), Y.ToString(format, CultureInfo.CurrentCulture), Z.ToString(format, CultureInfo.CurrentCulture), W.ToString(format, CultureInfo.CurrentCulture));
}
///
/// Returns a that represents this instance.
///
/// The format provider.
/// A that represents this instance.
public string ToString(IFormatProvider formatProvider)
{
return string.Format(formatProvider, _formatString, X, Y, Z, W);
}
///
/// Returns a that represents this instance.
///
/// The format.
/// The format provider.
/// A that represents this instance.
public string ToString(string format, IFormatProvider formatProvider)
{
if (format == null)
return ToString(formatProvider);
return string.Format(formatProvider, "X:{0} Y:{1} Z:{2} W:{3}", X.ToString(format, formatProvider), Y.ToString(format, formatProvider), Z.ToString(format, formatProvider), W.ToString(format, formatProvider));
}
///
/// Returns a hash code for this instance.
///
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
public override int GetHashCode()
{
unchecked
{
int hashCode = X.GetHashCode();
hashCode = (hashCode * 397) ^ Y.GetHashCode();
hashCode = (hashCode * 397) ^ Z.GetHashCode();
hashCode = (hashCode * 397) ^ W.GetHashCode();
return hashCode;
}
}
///
/// Determines whether the specified is equal to this instance.
///
/// The to compare with this instance.
/// true if the specified is equal to this instance; otherwise, false.
public bool Equals(ref Int4 other)
{
return other.X == X && other.Y == Y && other.Z == Z && other.W == W;
}
///
/// Determines whether the specified is equal to this instance.
///
/// The to compare with this instance.
/// true if the specified is equal to this instance; otherwise, false.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Int4 other)
{
return Equals(ref other);
}
///
/// Determines whether the specified is equal to this instance.
///
/// The to compare with this instance.
/// true if the specified is equal to this instance; otherwise, false.
public override bool Equals(object value)
{
return value is Int4 other && Equals(ref other);
}
}
}