Created
May 26, 2011 09:24
-
-
Save Konctantin/992829 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <summary> | |
| /// Structure that represents a rotation in three dimensions. | |
| /// </summary> | |
| public struct Quaternion | |
| { | |
| const int PACK_COEFF_YZ = 1 << 20; | |
| const int PACK_COEFF_X = 1 << 21; | |
| /// <summary> | |
| /// The X component of the quaternion. | |
| /// </summary> | |
| public readonly double X; | |
| /// <summary> | |
| /// The Y component of the quaternion. | |
| /// </summary> | |
| public readonly double Y; | |
| /// <summary> | |
| /// The Z component of the quaternion. | |
| /// </summary> | |
| public readonly double Z; | |
| /// <summary> | |
| /// The W component of the quaternion. | |
| /// </summary> | |
| public readonly double W; | |
| /// <summary> | |
| /// Initializes a new instance of the Quaternion structure. | |
| /// </summary> | |
| /// <param name="x"> | |
| /// Type: System.Single | |
| /// Value of the new Quaternion's X coordinate. | |
| /// </param> | |
| /// <param name="y"> | |
| /// Type: System.Single | |
| /// Value of the new Quaternion's Y coordinate. | |
| /// </param> | |
| /// <param name="z"> | |
| /// Type: System.Single | |
| /// Value of the new Quaternion's Z coordinate. | |
| /// </param> | |
| /// <param name="w"> | |
| /// Type: System.Single | |
| /// Value of the new Quaternion's W coordinate. | |
| /// </param> | |
| public Quaternion(float x, float y, float z, float w) | |
| { | |
| X = x; | |
| Y = y; | |
| Z = z; | |
| W = w; | |
| } | |
| /// <summary> | |
| /// Initializes a new instance of the Quaternion structure. | |
| /// </summary> | |
| /// <param name="packedQuaternion"> | |
| /// Type: System.UInt64 | |
| /// Value of the packed quarterion | |
| /// </param> | |
| public Quaternion(ulong packedQuaternion) | |
| { | |
| X = (double)(packedQuaternion >> 42) / (double)PACK_COEFF_X; | |
| Y = (double)(packedQuaternion << 22 >> 43) / (double)PACK_COEFF_YZ; | |
| Z = (double)(packedQuaternion << 43 >> 43) / (double)PACK_COEFF_YZ; | |
| W = 1 - (X * X + Y * Y + Z * Z); | |
| if (W < 0) W = Math.Sqrt(W); | |
| } | |
| /// <summary> | |
| /// Converts the quaternion values of this instance to its equivalent ulong value. | |
| /// </summary> | |
| public ulong ToUInt64() | |
| { | |
| byte w_sign = (byte)(this.W >= 0 ? 1 : -1); | |
| ulong x = (ulong)((this.X * PACK_COEFF_X ) * w_sign) & ((1 << 22) - 1); | |
| ulong y = (ulong)((this.Y * PACK_COEFF_YZ) * w_sign) & ((1 << 21) - 1); | |
| ulong z = (ulong)((this.Z * PACK_COEFF_YZ) * w_sign) & ((1 << 21) - 1); | |
| return (z | (y << 21) | (x << 42)); | |
| } | |
| /// <summary> | |
| /// Converts the numeric values of this instance to its equivalent string representations, separator is space. | |
| /// </summary> | |
| public override string ToString() | |
| { | |
| return string.Format(CultureInfo.InvariantCulture, "(X:{0} Y:{1} Z:{2} W:{3})", this.X, this.Y, this.Z, this.W); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment