// Copyright (c) Xenko contributors (https://xenko.com) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
//
// Copyright (c) 2010-2011 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Globalization;
using System.Runtime.InteropServices;
using Xenko.Core.Serialization;
using System.Runtime.Serialization;
namespace math
{
///
/// Represents a four dimensional mathematical vector.
///
[DataContract]
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct UInt4 : IEquatable, IFormattable
{
///
/// The size of the type, in bytes.
///
public static readonly int SizeInBytes = Utilities.SizeOf();
///
/// A with all of its components set to zero.
///
public static readonly UInt4 Zero = new UInt4();
///
/// The X unit (1, 0, 0, 0).
///
public static readonly UInt4 UnitX = new UInt4(1, 0, 0, 0);
///
/// The Y unit (0, 1, 0, 0).
///
public static readonly UInt4 UnitY = new UInt4(0, 1, 0, 0);
///
/// The Z unit (0, 0, 1, 0).
///
public static readonly UInt4 UnitZ = new UInt4(0, 0, 1, 0);
///
/// The W unit (0, 0, 0, 1).
///
public static readonly UInt4 UnitW = new UInt4(0, 0, 0, 1);
///
/// A with all of its components set to one.
///
public static readonly UInt4 One = new UInt4(1, 1, 1, 1);
///
/// The X component of the vector.
///
public uint X;
///
/// The Y component of the vector.
///
public uint Y;
///
/// The Z component of the vector.
///
public uint Z;
///
/// The W component of the vector.
///
public uint W;
///
/// Initializes a new instance of the struct.
///
/// The value that will be assigned to all components.
public UInt4(uint 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 UInt4(uint x, uint y, uint z, uint w)
{
X = x;
Y = 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 UInt4(uint[] values)
{
if (values == null)
throw new ArgumentNullException("values");
if (values.Length != 4)
throw new ArgumentOutOfRangeException("values", "There must be four and only four input values for UInt4.");
X = values[0];
Y = values[1];
Z = values[2];
W = values[3];
}
///
/// 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 uint this[uint index]
{
get
{
switch (index)
{
case 0:
return X;
case 1:
return Y;
case 2:
return Z;
case 3:
return W;
}
throw new ArgumentOutOfRangeException("index", "Indices for UInt4 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("index", "Indices for UInt4 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 uint[] ToArray()
{
return new uint[] { 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 UInt4 left, ref UInt4 right, out UInt4 result)
{
result = new UInt4(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 UInt4 Add(UInt4 left, UInt4 right)
{
return new UInt4(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.
/// When the method completes, contains the difference of the two vectors.
public static void Subtract(ref UInt4 left, ref UInt4 right, out UInt4 result)
{
result = new UInt4(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 UInt4 Subtract(UInt4 left, UInt4 right)
{
return new UInt4(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 Multiply(ref UInt4 value, uint scale, out UInt4 result)
{
result = new UInt4(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 UInt4 Multiply(UInt4 value, uint scale)
{
return new UInt4(value.X * scale, value.Y * scale, value.Z * scale, value.W * scale);
}
///
/// Modulates a vector with another by performing component-wise multiplication.
///
/// The first vector to modulate.
/// The second vector to modulate.
/// When the method completes, contains the modulated vector.
public static void Modulate(ref UInt4 left, ref UInt4 right, out UInt4 result)
{
result = new UInt4(left.X * right.X, left.Y * right.Y, left.Z * right.Z, left.W * right.W);
}
///
/// Modulates a vector with another by performing component-wise multiplication.
///
/// The first vector to modulate.
/// The second vector to modulate.
/// The modulated vector.
public static UInt4 Modulate(UInt4 left, UInt4 right)
{
return new UInt4(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 UInt4 value, uint scale, out UInt4 result)
{
result = new UInt4(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 UInt4 Divide(UInt4 value, uint scale)
{
return new UInt4(value.X / scale, value.Y / scale, value.Z / scale, value.W / scale);
}
///
/// 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 UInt4 value, ref UInt4 min, ref UInt4 max, out UInt4 result)
{
uint x = value.X;
x = (x > max.X) ? max.X : x;
x = (x < min.X) ? min.X : x;
uint y = value.Y;
y = (y > max.Y) ? max.Y : y;
y = (y < min.Y) ? min.Y : y;
uint z = value.Z;
z = (z > max.Z) ? max.Z : z;
z = (z < min.Z) ? min.Z : z;
uint w = value.W;
w = (w > max.W) ? max.W : w;
w = (w < min.W) ? min.W : w;
result = new UInt4(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 UInt4 Clamp(UInt4 value, UInt4 min, UInt4 max)
{
UInt4 result;
Clamp(ref value, ref min, ref max, out 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 largest components of the source vectors.
public static void Max(ref UInt4 left, ref UInt4 right, out UInt4 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 UInt4 Max(UInt4 left, UInt4 right)
{
UInt4 result;
Max(ref left, ref right, out 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 UInt4 left, ref UInt4 right, out UInt4 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 UInt4 Min(UInt4 left, UInt4 right)
{
UInt4 result;
Min(ref left, ref right, out result);
return result;
}
///
/// Adds two vectors.
///
/// The first vector to add.
/// The second vector to add.
/// The sum of the two vectors.
public static UInt4 operator +(UInt4 left, UInt4 right)
{
return new UInt4(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 (unchange).
/// The asserted (unchanged) vector.
public static UInt4 operator +(UInt4 value)
{
return value;
}
///
/// Subtracts two vectors.
///
/// The first vector to subtract.
/// The second vector to subtract.
/// The difference of the two vectors.
public static UInt4 operator -(UInt4 left, UInt4 right)
{
return new UInt4(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.
/// The scaled vector.
public static UInt4 operator *(uint scale, UInt4 value)
{
return new UInt4(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 UInt4 operator *(UInt4 value, uint scale)
{
return new UInt4(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 UInt4 operator /(UInt4 value, uint scale)
{
return new UInt4(value.X / scale, value.Y / scale, value.Z / scale, value.W / scale);
}
///
/// 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.
public static bool operator ==(UInt4 left, UInt4 right)
{
return left.Equals(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.
public static bool operator !=(UInt4 left, UInt4 right)
{
return !left.Equals(right);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Vec2(UInt4 value)
{
return new Vec2(value.X, value.Y);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Vector3(UInt4 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(UInt4 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, "X:{0} Y:{1} Z:{2} W:{3}", 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, "X:{0} Y:{1} Z:{2} W:{3}",
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, "X:{0} Y:{1} Z:{2} W:{3}", 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)
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()
{
return X.GetHashCode() + Y.GetHashCode() + Z.GetHashCode() + W.GetHashCode();
}
///
/// 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(UInt4 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.
///
public override bool Equals(object value)
{
if (value == null)
return false;
if (value.GetType() != GetType())
return false;
return Equals((UInt4)value);
}
///
/// Performs an implicit conversion from array to .
///
/// The input.
/// The result of the conversion.
public static implicit operator UInt4(uint[] input)
{
return new UInt4(input);
}
///
/// Performs an implicit conversion from to array.
///
/// The input.
/// The result of the conversion.
public static implicit operator uint[](UInt4 input)
{
return input.ToArray();
}
}
}