public Quaternion(ref Matrix3 matrix) { var scale = Math.Pow(matrix.Determinant, 1.0d / 3.0d); float x, y, z; w = (float)(Math.Sqrt(Math.Max(0, scale + matrix[0, 0] + matrix[1, 1] + matrix[2, 2])) / 2); x = (float)(Math.Sqrt(Math.Max(0, scale + matrix[0, 0] - matrix[1, 1] - matrix[2, 2])) / 2); y = (float)(Math.Sqrt(Math.Max(0, scale - matrix[0, 0] + matrix[1, 1] - matrix[2, 2])) / 2); z = (float)(Math.Sqrt(Math.Max(0, scale - matrix[0, 0] - matrix[1, 1] + matrix[2, 2])) / 2); xyz = new Vector3(x, y, z); if (matrix[2, 1] - matrix[1, 2] < 0) { X = -X; } if (matrix[0, 2] - matrix[2, 0] < 0) { Y = -Y; } if (matrix[1, 0] - matrix[0, 1] < 0) { Z = -Z; } }
public static double NextPowerOfTwo(double n) { if (double.IsNaN(n) || double.IsInfinity(n)) { throw new ArgumentOutOfRangeException(nameof(n), "Must be a number."); } if (n <= 0) { throw new ArgumentOutOfRangeException(nameof(n), "Must be positive."); } // Don't return negative powers of two, that's nonsense. if (n < 1) { return(1.0); } return(Math.Pow(2, Math.Floor(Math.Log(n, 2)) + 1)); }