#region License
/* FNA - XNA4 Reimplementation for Desktop Platforms
* Copyright 2009-2016 Ethan Lee and the MonoGame Team
*
* Released under the Microsoft Public License.
* See LICENSE for details.
*/
#endregion
#region Using Statements
using System;
#endregion
namespace Microsoft.Xna.Framework.Graphics.PackedVector
{
///
/// Packed vector type containing four 16-bit floating-point values.
///
public struct HalfVector4 : IPackedVector, IPackedVector, IEquatable
{
#region Public Properties
///
/// Directly gets or sets the packed representation of the value.
///
/// The packed representation of the value.
[CLSCompliant(false)]
public ulong PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private ulong packedValue;
#endregion
#region Public Constructors
///
/// Initializes a new instance of the HalfVector4 structure.
///
/// Initial value for the x component.
/// Initial value for the y component.
/// Initial value for the z component.
/// Initial value for the q component.
public HalfVector4(float x, float y, float z, float w)
{
Vector4 vector = new Vector4(x, y, z, w);
packedValue = Pack(ref vector);
}
///
/// Initializes a new instance of the HalfVector4 structure.
///
///
/// A vector containing the initial values for the components of the HalfVector4
/// structure.
///
public HalfVector4(Vector4 vector)
{
packedValue = Pack(ref vector);
}
#endregion
#region Public Methods
///
/// Expands the packed representation into a Vector4.
///
/// The expanded vector.
public Vector4 ToVector4()
{
return new Vector4(
HalfTypeHelper.Convert((ushort) packedValue),
HalfTypeHelper.Convert((ushort) (packedValue >> 0x10)),
HalfTypeHelper.Convert((ushort) (packedValue >> 0x20)),
HalfTypeHelper.Convert((ushort) (packedValue >> 0x30))
);
}
#endregion
#region IPackedVector Methods
///
/// Sets the packed representation from a Vector4.
///
/// The vector to create the packed representation from.
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = Pack(ref vector);
}
#endregion
#region Public Static Operators and Override Methods
///
/// Returns a string representation of the current instance.
///
/// String that represents the object.
public override string ToString()
{
return ToVector4().ToString();
}
///
/// Gets the hash code for the current instance.
///
/// Hash code for the instance.
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
///
/// Returns a value that indicates whether the current instance is equal to a
/// specified object.
///
/// The object with which to make the comparison.
///
/// True if the current instance is equal to the specified object; false otherwise.
///
public override bool Equals(object obj)
{
return ((obj is HalfVector4) && Equals((HalfVector4) obj));
}
///
/// Returns a value that indicates whether the current instance is equal to a
/// specified object.
///
/// The object with which to make the comparison.
///
/// True if the current instance is equal to the specified object; false otherwise.
///
public bool Equals(HalfVector4 other)
{
return packedValue.Equals(other.packedValue);
}
///
/// Compares the current instance of a class to another instance to determine
/// whether they are the same.
///
/// The object to the left of the equality operator.
/// The object to the right of the equality operator.
/// True if the objects are the same; false otherwise.
public static bool operator ==(HalfVector4 a, HalfVector4 b)
{
return a.Equals(b);
}
///
/// Compares the current instance of a class to another instance to determine
/// whether they are different.
///
/// The object to the left of the equality operator.
/// The object to the right of the equality operator.
/// True if the objects are different; false otherwise.
public static bool operator !=(HalfVector4 a, HalfVector4 b)
{
return !a.Equals(b);
}
#endregion
#region Private Static Pack Method
///
/// Packs a vector into a ulong.
///
/// The vector containing the values to pack.
/// The ulong containing the packed values.
private static ulong Pack(ref Vector4 vector)
{
return (
((ulong) HalfTypeHelper.Convert(vector.X)) |
(((ulong) HalfTypeHelper.Convert(vector.Y) << 0x10)) |
(((ulong) HalfTypeHelper.Convert(vector.Z) << 0x20)) |
(((ulong) HalfTypeHelper.Convert(vector.W) << 0x30))
);
}
#endregion
}
}