/// <summary> /// Returns a <see cref="Quaternion"/> that represents the rotation of the given matrix /// </summary> public static Quaternion FromTransformationMatrix(R3DMatrix44 matrix) { Quaternion result = new Quaternion(0, 0, 0, 0); float sqrt; float half; float scale = matrix.M11 + matrix.M22 + matrix.M33; if (scale > 0.0f) { sqrt = (float)Math.Sqrt(scale + 1.0f); result.W = sqrt * 0.5f; sqrt = 0.5f / sqrt; result.X = (matrix.M23 - matrix.M32) * sqrt; result.Y = (matrix.M31 - matrix.M13) * sqrt; result.Z = (matrix.M12 - matrix.M21) * sqrt; } else if ((matrix.M11 >= matrix.M22) && (matrix.M11 >= matrix.M33)) { sqrt = (float)Math.Sqrt(1.0f + matrix.M11 - matrix.M22 - matrix.M33); half = 0.5f / sqrt; result.X = 0.5f * sqrt; result.Y = (matrix.M12 + matrix.M21) * half; result.Z = (matrix.M13 + matrix.M31) * half; result.W = (matrix.M23 - matrix.M32) * half; } else if (matrix.M22 > matrix.M33) { sqrt = (float)Math.Sqrt(1.0f + matrix.M22 - matrix.M11 - matrix.M33); half = 0.5f / sqrt; result.X = (matrix.M21 + matrix.M12) * half; result.Y = 0.5f * sqrt; result.Z = (matrix.M32 + matrix.M23) * half; result.W = (matrix.M31 - matrix.M13) * half; } else { sqrt = (float)Math.Sqrt(1.0f + matrix.M33 - matrix.M11 - matrix.M22); half = 0.5f / sqrt; result.X = (matrix.M31 + matrix.M13) * half; result.Y = (matrix.M32 + matrix.M23) * half; result.Z = 0.5f * sqrt; result.W = (matrix.M12 - matrix.M21) * half; } return(result); }
/// <summary> /// Creates a clone of a <see cref="R3DMatrix44"/> object /// </summary> /// <param name="r3dMatrix44">The <see cref="R3DMatrix44"/> to clone</param> public R3DMatrix44(R3DMatrix44 r3dMatrix44) { this.M11 = r3dMatrix44.M11; this.M12 = r3dMatrix44.M12; this.M13 = r3dMatrix44.M13; this.M14 = r3dMatrix44.M14; this.M21 = r3dMatrix44.M21; this.M22 = r3dMatrix44.M22; this.M23 = r3dMatrix44.M23; this.M24 = r3dMatrix44.M24; this.M31 = r3dMatrix44.M31; this.M32 = r3dMatrix44.M32; this.M33 = r3dMatrix44.M33; this.M34 = r3dMatrix44.M34; this.M41 = r3dMatrix44.M41; this.M42 = r3dMatrix44.M42; this.M43 = r3dMatrix44.M43; this.M44 = r3dMatrix44.M44; }