#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using MoonWorks.Math;
using MoonWorks.Math.Float;
#endregion
namespace MoonWorks.Graphics.PackedVector
{
///
/// Packed vector type containing four 16-bit signed integer values.
///
public struct Short4 : IPackedVector, IEquatable
{
#region Public Properties
///
/// Directly gets or sets the packed representation of the value.
///
/// The packed representation of the value.
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 Short4 class.
///
///
/// A vector containing the initial values for the components of the Short4 structure.
///
public Short4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
///
/// Initializes a new instance of the Short4 class.
///
/// Initial value for the x component.
/// Initial value for the y component.
/// Initial value for the z component.
/// Initial value for the w component.
public Short4(float x, float y, float z, float w)
{
packedValue = Pack(x, y, z, w);
}
#endregion
#region Public Methods
///
/// Expands the packed representation into a Vector4.
///
/// The expanded vector.
public Vector4 ToVector4()
{
return new Vector4(
(short) (packedValue & 0xFFFF),
(short) ((packedValue >> 16) & 0xFFFF),
(short) ((packedValue >> 32) & 0xFFFF),
(short) (packedValue >> 48)
);
}
#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(vector.X, vector.Y, vector.Z, vector.W);
}
#endregion
#region Public Static Operators and Override Methods
///
/// 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 !=(Short4 a, Short4 b)
{
return a.PackedValue != b.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 ==(Short4 a, Short4 b)
{
return a.PackedValue == b.PackedValue;
}
///
/// 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 Short4) && Equals((Short4) 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(Short4 other)
{
return this == other;
}
///
/// Gets the hash code for the current instance.
///
/// Hash code for the instance.
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
///
/// Returns a string representation of the current instance.
///
/// String that represents the object.
public override string ToString()
{
return packedValue.ToString("X");
}
#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.
static ulong Pack(float x, float y, float z, float w)
{
return (ulong) (
((long) System.Math.Round(MathHelper.Clamp(x, -32768, 32767)) & 0xFFFF) |
(((long) System.Math.Round(MathHelper.Clamp(y, -32768, 32767)) << 16) & 0xFFFF0000) |
(((long) System.Math.Round(MathHelper.Clamp(z, -32768, 32767)) << 32) & 0xFFFF00000000) |
((long) System.Math.Round(MathHelper.Clamp(w, -32768, 32767)) << 48)
);
}
#endregion
}
}