Example #1
0
        public static Plane Normalize(Plane value)
        {
            if (Vector.IsHardwareAccelerated)
            {
                Single normalLengthSquared = value.Normal.LengthSquared();
                if (MathF.Abs(normalLengthSquared - 1.0f) < NormalizeEpsilon)
                {
                    // It already normalized, so we don't need to farther process.
                    return(value);
                }
                Single normalLength = MathF.Sqrt(normalLengthSquared);
                return(new Plane(
                           value.Normal / normalLength,
                           value.D / normalLength));
            }
            else
            {
                Single f = value.Normal.X * value.Normal.X + value.Normal.Y * value.Normal.Y + value.Normal.Z * value.Normal.Z;

                if (MathF.Abs(f - 1.0f) < NormalizeEpsilon)
                {
                    return(value); // It already normalized, so we don't need to further process.
                }

                Single fInv = 1.0f / MathF.Sqrt(f);

                return(new Plane(
                           value.Normal.X * fInv,
                           value.Normal.Y * fInv,
                           value.Normal.Z * fInv,
                           value.D * fInv));
            }
        }
Example #2
0
        public void Matrix3x2DeterminantTest1()
        {
            Matrix3x2 a = new Matrix3x2();

            a.M11 = 5.0f;
            a.M12 = 2.0f;
            a.M21 = 12.0f;
            a.M22 = 6.8f;
            a.M31 = 6.5f;
            a.M32 = 1.0f;
            Matrix3x2 i;

            Assert.True(Matrix3x2.Invert(a, out i));

            Single detA = a.GetDeterminant();
            Single detI = i.GetDeterminant();
            Single t    = 1.0f / detI;

            // only accurate to 3 precision
            Assert.True(Math.Abs(detA - t) < 1e-3, "Matrix3x2.Determinant was not set correctly.");

            // sanity check against 4x4 version
            Assert.Equal(new Matrix4x4(a).GetDeterminant(), detA);
            Assert.Equal(new Matrix4x4(i).GetDeterminant(), detI);
        }
Example #3
0
        public static T Abs <T>(T value) where T : struct
        {
            Type[] unsignedTypes = new[] { typeof(byte), typeof(ushort), typeof(uint), typeof(ulong) };
            if (unsignedTypes.Contains(typeof(T)))
            {
                return(value);
            }

            dynamic dyn = (dynamic)value;
            var     abs = Math.Abs(dyn);
            T       ret = (T)abs;

            return(ret);
        }
Example #4
0
 public static bool SingleAreEqual(Single expectedResult, Single actualResult)
 {
     if (Single.IsNaN(expectedResult))
     {
         return(Single.IsNaN(actualResult));
     }
     else if (Single.IsNaN(actualResult))
     {
         // expectedResult is finite
         return(false);
     }
     else
     {
         var diff = Math.Abs(expectedResult - actualResult);
         return(diff <= SingleEpsilon);
     }
 }
Example #5
0
        /// <summary>
        /// Attempts to invert the given matrix. If the operation succeeds, the inverted matrix is stored in the result parameter.
        /// </summary>
        /// <param name="matrix">The source matrix.</param>
        /// <param name="result">The output matrix.</param>
        /// <returns>True if the operation succeeded, False otherwise.</returns>
        public static bool Invert(Matrix3x2 matrix, out Matrix3x2 result)
        {
            Single det = (matrix.M11 * matrix.M22) - (matrix.M21 * matrix.M12);

            if (MathF.Abs(det) < Single.Epsilon)
            {
                result = new Matrix3x2(Single.NaN, Single.NaN, Single.NaN, Single.NaN, Single.NaN, Single.NaN);
                return(false);
            }

            Single invDet = 1.0f / det;

            result.M11 = matrix.M22 * invDet;
            result.M12 = -matrix.M12 * invDet;
            result.M21 = -matrix.M21 * invDet;
            result.M22 = matrix.M11 * invDet;
            result.M31 = (matrix.M21 * matrix.M32 - matrix.M31 * matrix.M22) * invDet;
            result.M32 = (matrix.M31 * matrix.M12 - matrix.M11 * matrix.M32) * invDet;

            return(true);
        }
Example #6
0
 // Comparison helpers with small tolerance to allow for floating point rounding during computations.
 public static bool Equal(Single a, Single b)
 {
     return(Math.Abs(a - b) < 1e-5);
 }
Example #7
0
 public static Vector3 Abs(Vector3 value)
 {
     return(new Vector3(MathF.Abs(value.X), MathF.Abs(value.Y), MathF.Abs(value.Z)));
 }
Example #8
0
 public static Vector4 Abs(Vector4 value)
 {
     return(new Vector4(MathF.Abs(value.X), MathF.Abs(value.Y), MathF.Abs(value.Z), MathF.Abs(value.W)));
 }
Example #9
0
 public static Vector2 Abs(Vector2 value)
 {
     return(new Vector2(MathF.Abs(value.X), MathF.Abs(value.Y)));
 }