Ejemplo n.º 1
0
        public void TestFunctions()
        {
            {
                Vector3 vec1     = new Vector3(2.2f, 6.3f, 12.36f);
                Vector3 vec2     = new Vector3(7.9f, 6.3f, 45.3f);
                float   dotPrdt1 = vec1.Dot(vec2);
                float   dotPrdt2 = vec2.Dot(vec1);

                Assert.IsTrue(dotPrdt1 == dotPrdt2, "Dot Product test fails for Vector3");

                Vec3 vec1Native = new Vec3(vec1.x, vec1.y, vec1.z);
                Vec3 vec2Native = new Vec3(vec2.x, vec2.y, vec2.z);
                dotPrdt1 = vec1Native.Dot(vec2Native);
                dotPrdt2 = vec2Native.Dot(vec1Native);
                Assert.IsTrue(dotPrdt1 == dotPrdt2, "Dot Product test fails for Vec3");
            }

            {
                Vector3 vec1       = new Vector3(2.2f, 6.3f, 2.36f);
                Vector3 vec2       = new Vector3(7.9f, 6.3f, 5.3f);
                Vector3 crossPrdt1 = vec1.Cross(vec2);
                Vector3 crossPrdt2 = vec2.Cross(vec1);

                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(crossPrdt1, -crossPrdt2), "Cross(Vector3) fails for Vector3 1");
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(-crossPrdt1, crossPrdt2), "Cross(Vector3) fails for Vector3 2");
            }
        }
Ejemplo n.º 2
0
        public void TestLerp()
        {
            {
                //native
                Vec4 vec4p = new Vec4(1f, 1f, 0f, 0f);
                vec4p.Normalize();
                Vec4 vec4q = new Vec4(0f, 1f, 0f, 0f);
                vec4q.Normalize();
                Vec4 vec4Mid = Vec4.CreateLerp(vec4p, vec4q, 0.5f);
                //managed
                Vector4 vector4p   = new Vector4(vec4p.x, vec4p.y, vec4p.z, vec4p.w);
                Vector4 vector4q   = new Vector4(vec4q.x, vec4q.y, vec4q.z, vec4q.w);
                Vector4 vector4Mid = Vector4.Lerp(vector4p, vector4q, 0.5f);

                Assert.IsTrue(vec4Mid == vector4Mid, " Lerp tests fails for native and managed vector4 1" + vec4Mid.PrintString() + "," + vector4Mid.ToString());
            }

            {
                //native
                Vec4 vec4p   = new Vec4(1f, 0f, 0f, 0f);
                Vec4 vec4q   = new Vec4(0f, 1f, 0f, 0f);
                Vec4 vec4Mid = Vec4.CreateLerp(vec4p, vec4q, 0.5f);
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(vec4Mid, new Vector4(0.5f, 0.5f, 0f, 0f)), "Lerp tests fails for native vec4 2" + vec4Mid.PrintString());

                //managed
                Vector4 vector4p   = new Vector4(vec4p.x, vec4p.y, vec4p.z, vec4p.w);
                Vector4 vector4q   = new Vector4(vec4q.x, vec4q.y, vec4q.z, vec4q.w);
                Vector4 vector4Mid = Vector4.Lerp(vector4p, vector4q, 0.5f);
                Assert.IsTrue(vec4Mid == vector4Mid, " Lerp tests fails for native and managed vector4 4" + vec4Mid.PrintString() + "," + vector4Mid.ToString());
            }

            {
                //native
                Vec4 vec4p          = new Vec4(0.5f, 0f, 0f, 0f);
                Vec4 vec4q          = new Vec4(0.5f, 0f, 0f, 0f);
                Vec4 lerpVec        = Vec4.CreateLerp(vec4p, vec4q, 0.5f);
                Vec4 expectedResult = new Vec4(0.5f, 0f, 0f, 0f);
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(lerpVec, expectedResult), "Lerp tests fail for native vec4 " + "Expected :" + expectedResult.PrintString() + ", Actual :" + lerpVec.PrintString());

                //managed
                Vector4 vector4p        = new Vector4(0.5f, 0f, 0f, 0f);
                Vector4 vector4q        = new Vector4(0.5f, 0f, 0f, 0f);
                Vector4 lerpVector4     = Vector4.Lerp(vector4p, vector4q, 0.5f);
                Vector4 expectedResult2 = new Vector4(0.5f, 0, 0, 0);
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(lerpVector4, expectedResult2), "Lerp tests fail for managed vector4 " + "Expected :" + expectedResult2.ToString() + ", Actual :" + lerpVector4.ToString());
            }

            {
                Vector4 p = new Vector4(1.0f, 0f, 0f, 0f);
                Vector4 q = new Vector4(-1f, 0f, 0f, 0f);

                //middle of interpolation will be (0,0,0,0)
                Vector4 expectedResult = new Vector4(0, 0, 0, 0);

                Vector4 actualResult = Vector4.Lerp(p, q, 0.5f);
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(expectedResult, actualResult), "Lerp fails for managed vector4 " + "Expected :" + expectedResult + ", Actual :" + actualResult);
            }
        }
Ejemplo n.º 3
0
        public void TestSlerp()
        {
            {
                //native
                Vec2 vec2p = new Vec2(1f, 1f);
                vec2p.Normalize();
                Vec2 vec2q = new Vec2(10.0f, 10.0f);
                vec2q.Normalize();
                Vec2 vec2Mid = Vec2.CreateSlerp(vec2p, vec2q, 0.5f);
                //managed
                Vector2 vector2p = new Vector2(vec2p.x, vec2p.y);
                vector2p = vector2p.Normalized;
                Vector2 vector2q = new Vector2(vec2q.x, vec2q.y);
                vector2q = vector2q.Normalized;
                Vector2 vector2Mid = Vector2.Slerp(vector2p, vector2q, 0.5f);

                Assert.IsTrue(MathHelpers.Approximately(vec2Mid.x, vector2Mid.x, 0.000001f) && MathHelpers.Approximately(vec2Mid.y, vector2Mid.y, 0.000001f), " Slerp tests fails for native and managed vector2 1");
            }
            {
                Vec2 vec2p = new Vec2(1f, 1f);
                vec2p.Normalize();
                Vec2 vec2q = new Vec2(10.0f, 10.0f);
                vec2q.Normalize();
                Vec2 vec2Start = Vec2.CreateSlerp(vec2p, vec2q, 0.0f);

                //managed
                Vector2 vector2p = new Vector2(vec2p.x, vec2p.y);
                vector2p = vector2p.Normalized;
                Vector2 vector2q = new Vector2(vec2q.x, vec2q.y);
                vector2q = vector2q.Normalized;
                Vector2 vector2Start = Vector2.Slerp(vector2p, vector2q, 0.0f);

                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(vec2Start, vector2Start), "Slerp tests fails for vector2 2a");
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(vec2Start, vec2p), "Slerp tests fails for vector2 2b " + vec2Start.PrintString() + "," + vec2p.PrintString());
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(vector2Start, vector2p), "Slerp tests fails for vector2 2c");
            }
            {
                Vec2 vec2p = new Vec2(1f, 1f);
                vec2p.Normalize();
                Vec2 vec2q = new Vec2(10.0f, 10.0f);
                vec2q.Normalize();
                Vec2 vec2Start = Vec2.CreateSlerp(vec2p, vec2q, 1.0f);

                //managed
                Vector2 vector2p = new Vector2(vec2p.x, vec2p.y);
                vector2p = vector2p.Normalized;
                Vector2 vector2q = new Vector2(vec2q.x, vec2q.y);
                vector2q = vector2q.Normalized;
                Vector2 vector2Start = Vector2.Slerp(vector2p, vector2q, 1.0f);

                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(vec2Start, vector2Start), "Slerp tests fails for vector2 3a");
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(vec2Start, vec2q), "Slerp tests fails for vector2 3b");
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(vector2Start, vector2q), "Slerp tests fails for vector2 3c");
            }
        }
Ejemplo n.º 4
0
        public void TestLerp()
        {
            {
                //native
                Vec3 vec3p = new Vec3(1f, 1f, 0f);
                vec3p.normalize();
                Vec3 vec3q = new Vec3(0f, 1f, 0f);
                vec3q.normalize();
                Vec3 vec3Mid = Vec3.CreateLerp(vec3p, vec3q, 0.5f);
                //managed
                Vector3 vector3p   = new Vector3(vec3p.x, vec3p.y, vec3p.z);
                Vector3 vector3q   = new Vector3(vec3q.x, vec3q.y, vec3q.z);
                Vector3 vector3Mid = Vector3.Lerp(vector3p, vector3q, 0.5f);

                Assert.IsTrue(vec3Mid == vector3Mid, " Lerp tests fails for native and managed vector3 1" + vec3Mid.PrintString() + "," + vector3Mid.ToString());
            }
            {
                //native
                Vec3 vec3p   = new Vec3(1f, 0f, 0f);
                Vec3 vec3q   = new Vec3(0f, 1f, 0f);
                Vec3 vec3Mid = Vec3.CreateLerp(vec3p, vec3q, 0.5f);
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(vec3Mid, new Vector3(0.5f, 0.5f, 0)), "Lerp tests fails for native vec3 2" + vec3Mid.PrintString());

                //managed
                Vector3 vector3p   = new Vector3(vec3p.x, vec3p.y, vec3p.z);
                Vector3 vector3q   = new Vector3(vec3q.x, vec3q.y, vec3q.z);
                Vector3 vector3Mid = Vector3.Lerp(vector3p, vector3q, 0.5f);
                Assert.IsTrue(vec3Mid == vector3Mid, " Lerp tests fails for native and managed vector3 3" + vec3Mid.PrintString() + "," + vector3Mid.ToString());
            }
            {
                //native
                Vec3 vec3p          = new Vec3(0.5f, 0f, 0f);
                Vec3 vec3q          = new Vec3(0.5f, 0f, 0f);
                Vec3 lerpVec        = Vec3.CreateLerp(vec3p, vec3q, 0.5f);
                Vec3 expectedResult = new Vec3(0.5f, 0f, 0f);
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(lerpVec, expectedResult), "Lerp tests fail for native vec3 " + "Expected :" + expectedResult.PrintString() + ", Actual :" + lerpVec.PrintString());

                //managed
                Vector3 vector3p        = new Vector3(0.5f, 0f, 0f);
                Vector3 vector3q        = new Vector3(0.5f, 0f, 0f);
                Vector3 lerpVector3     = Vector3.Lerp(vector3p, vector3q, 0.5f);
                Vector3 expectedResult2 = new Vector3(0.5f, 0, 0);
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(lerpVector3, expectedResult2), "Lerp tests fail for managed vector3 " + "Expected :" + expectedResult2.ToString() + ", Actual :" + lerpVector3.ToString());
            }
            {
                Vector3 p = new Vector3(1.0f, 0f, 0f);
                Vector3 q = new Vector3(-1f, 0f, 0f);

                //middle of interpolation will be (0,0,0)
                Vector3 expectedResult = new Vector3(0, 0, 0);

                Vector3 actualResult = Vector3.Lerp(p, q, 0.5f);
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(expectedResult, actualResult), "Lerp fails for managed vector3 " + "Expected :" + expectedResult + ", Actual :" + actualResult);
            }
        }
Ejemplo n.º 5
0
        public void TestOtherProperties()
        {
            {
                //get length
                Vector3 v1  = new Vector3(1.1f, 2.2f, 3.3f);
                float   len = v1.Length;

                float lenManual = (float)System.Math.Sqrt(v1.x * v1.x + v1.y * v1.y + v1.z * v1.z);
                Assert.IsTrue(len == lenManual, "Length get test fails for Vector3");

                Vec3  v2    = new Vec3(v1.x, v1.y, v1.z);
                float v2Len = v2.len();
                Assert.IsTrue(len == lenManual, "Length Comparison test fails for Vector3 and Vec3");

                //get magnitude
                float magnitude = v1.Magnitude;
                Assert.IsTrue(len == magnitude, "Length and magnitude are not equal");
            }

            {
                //LengthSquared
                Vector3 v1             = new Vector3(1.2f, 0.3f, 1.5f);
                float   lengthSquared  = v1.LengthSquared;
                float   lengthSquared2 = (v1.x * v1.x) + (v1.y * v1.y) + (v1.z * v1.z);
                Assert.IsTrue(MathHelpers.Approximately(lengthSquared, lengthSquared2, 0.0000001f), "LengthSquared fails for Vector3");
            }
            {
                //Length2D
                Vector2 v2       = new Vector2(1.2f, 0.3f);
                Vector3 v2_to_v3 = new Vector3(v2);

                float v2Length       = v2.Length;
                float v2_to_v3Length = v2_to_v3.Length2D;
                Assert.IsTrue(MathHelpers.Approximately(v2Length, v2_to_v3Length, 0.0000001f), "Length2D fails for Vector3");

                float v2LengthSquared       = v2.LengthSquared;
                float v2_to_v3LengthSquared = v2_to_v3.Length2DSquared;
                Assert.IsTrue(MathHelpers.Approximately(v2LengthSquared, v2_to_v3LengthSquared, 0.0000001f), "Length2DSquared fails for Vector3");
            }
            {
                //Normalized
                Vector3 v3     = new Vector3(0, 0, 0);
                Vector3 v3Unit = v3.Normalized;
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(v3, v3Unit), "Normalized fails for Vector3");
            }

            {
                //Volume
                Vector3 v3     = new Vector3(1f, 2f, 10f);
                float   volume = v3.Volume;

                float vol = v3.x * v3.y * v3.z;
                Assert.IsTrue(volume == vol, "Volume fails for Vector3");
            }
        }
Ejemplo n.º 6
0
        public void TestSlerpCollinearVectors()
        {
            {
                Vector2 p = new Vector2(1.0f, 0f);
                Vector2 q = new Vector2(-1.0f, 0f);

                //middle of interpolation will be (0,0)
                Vector2 yVector = new Vector2(0, 0);

                Vector2 result = Vector2.SlerpUnclamped(p, q, 0.5f);
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(result, yVector), "Slerp Collinear Vector2 fails, result is :" + result.ToString());
            }
        }
Ejemplo n.º 7
0
        public void TestZero()
        {
            Vector2 vec2 = new Vector2(0, 0);

            Assert.IsTrue(vec2 == Vector2.Zero, " Zero equality fails for Vector2.Zero");

            Vec2 vec2native = new Vec2(0, 0);

            Assert.IsTrue(vec2native == vec2, "Zero equality fails for native and managed");

            //this should not throw an error
            Vector2 normalized = vec2.Normalized;

            Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(vec2, normalized), "Zero equality fails for normalized 'zero' point");
        }
Ejemplo n.º 8
0
        public void TestOperators()
        {
            Vector3 a     = new Vector3(4.5f, 65.9f, 98.32f);
            float   scale = 15.6f;
            {
                Vector3 b = a * scale;
                Vector3 d = new Vector3(a.x * scale, a.y * scale, a.z * scale);
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(b, d), "Operator* (Vector3 v, float scale) test fails for Vector3");
            }
            {
                Vector3 b = a * scale;
                Vector3 d = scale * a;
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(b, d), "Operator * (float scale,Vector3 v) test fails for Vector3");
            }
            {
                Vector3 b          = a / scale;
                float   multiplier = 1 / scale;
                Vector3 c          = new Vector3(a.x * multiplier, a.y * multiplier, a.z * multiplier);
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(b, c), "Operator / (Vector3 v, float scale) test fails for Vector3");
            }
            {
                Vector3 b    = new Vector3(7.9f, 5.3f, 109.3f);
                Vector3 sum1 = a + b;

                Vector3 sum2 = new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(sum1, sum2), "operator +(Vector3 v0, Vector3 v1) test fails for Vector3");
            }
            {
                Vector3 b           = new Vector3(7.9f, 5.3f, 10.25f);
                Vector3 difference1 = a - b;

                Vector3 difference2 = new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(difference1, difference2), "operator -(Vector3 v0, Vector3 v1) fails for Vector3");
            }
            {
                Vector3 b = new Vector3(-a.x, -a.y, -a.z);
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(-a, b), "operator -(Vector3 v) fails for Vector3");
            }
            {
                Vector3 b = new Vector3((!a).x, (!a).y, (!a).z);
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(!a, b), "operator !(Vector3 v) fails for Vector3");
            }
        }
Ejemplo n.º 9
0
        public void TestMethods()
        {
            //we need 3 orthonormal vectors for equivalence test to pass
            Vector3 right = new Vec3(1f, 0f, -1f);

            right = right.Normalized;

            Vector3 forward = new Vec3(1f, 1.4142136f, 1f);

            forward = forward.Normalized;

            Vector3 up = new Vec3(1f, -1.4142136f, 1f);

            up = up.Normalized;

            //native
            Matrix34 nativematrix = new Matrix34(0f, 3f, 6f, 9f, 1f, 4f, 7f, 10f, 2f, 5f, 8f, 11f);
            Quat     nativeQuat   = Quat.CreateQuatFromMatrix(nativematrix);

            //managed
            Matrix3x4  managedmatrix = new Matrix3x4(0f, 3f, 6f, 9f, 1f, 4f, 7f, 10f, 2f, 5f, 8f, 11f);
            Quaternion managedQuat   = Quat.CreateQuatFromMatrix(managedmatrix);

            float radians = 1.57f;

            //1 normalize
            {
                Quaternion test1a = managedQuat;
                test1a.Normalize();

                Quat test1b = nativeQuat;
                test1b.Normalize();

                Assert.IsTrue(Quat.IsEquivalent(test1a, test1b), "Normalize Equivalence test failed");
            }
            //2 Dot

            //3 Difference

            //4 CreateFromVectors
            {
                Matrix3x3 matrix33 = Matrix33.CreateFromVectors(right, forward, up);
                Quat      quat2a   = Quat.CreateQuatFromMatrix(matrix33);

                Vector3    right2   = new Vector3(right.x, right.y, right.z);
                Vector3    forward2 = new Vector3(forward.x, forward.y, forward.z);
                Vector3    up2      = new Vector3(up.x, up.y, up.z);
                Quaternion quat2b   = Quaternion.CreateFromVectors(right2, forward2, up2);
                Assert.IsTrue(quat2a == quat2b, "CreateFromVectors equality failed");
                Assert.IsTrue(Quat.IsEquivalent(quat2a, quat2b), "CreateFromVectors equivalence failed");
            }

            //5 SetLookOrientation
            //5a
            {
                //compare to SetRotationVDir, up vector = (0,0,1)

                //get 2 orthogonal vectors
                Vector3 up5      = new Vector3(0f, 0f, 1f);
                Vector3 forward5 = new Vector3(1f, 1.4142136f, 0f);
                forward5 = forward5.Normalized;
                Vector3 right5 = forward5.Cross(up5);

                Quat nativeQuaternion2 = Quat.CreateIdentity();
                nativeQuaternion2.SetRotationVDir(forward5);

                Matrix33 nativeMatrix      = Matrix33.CreateMatrix33(right5, forward5, up5);
                Quat     nativeQuaternion3 = Quat.CreateQuatFromMatrix(nativeMatrix);

                Assert.IsTrue(Quat.IsEquivalent(nativeQuaternion2, nativeQuaternion3), "Native Quaternion constructor failed comparison with SetRotationVDir. Expected :" + ((Quaternion)nativeQuaternion2).ToString() + ", Actual :" + ((Quaternion)nativeQuaternion3).ToString());
            }

            //5b
            {
                // test new C# SetLookOrientation, DEV-3691
                // rotate forward around x-y plane, z=0
                // i) 20 values for x-y plane
                // ii) 20 values for y-z plane

                Vec3[] testVectors = new Vec3[40];
                for (uint i = 0; i < 20; ++i)
                {
                    radians        = ((float)System.Math.PI) / 20.0f * i;
                    testVectors[i] = new Vec3((float)System.Math.Cos(radians), (float)System.Math.Sin(radians), 0f);
                }
                for (uint i = 20; i < 40; ++i)
                {
                    radians        = ((float)System.Math.PI) / 20.0f * i;
                    testVectors[i] = new Vec3(0f, (float)System.Math.Cos(radians), (float)System.Math.Sin(radians));
                }

                const float test_margin_error = 0.05f;
                for (uint i = 0; i < testVectors.Length; ++i)
                {
                    Vec3 forward5c = testVectors[i];
                    forward5c = forward5c.normalized();
                    Quat nativeQuat5c = Quat.CreateIdentity();
                    nativeQuat5c.SetRotationVDir(forward5c);
                    float dtPrdt1 = MathHelpers.Clamp(forward5c.Dot(nativeQuat5c.GetColumn1()), -1f, 1f);

                    Quaternion managedQuat5c = Quaternion.Identity;
                    Vector3    upManaged     = Vector3.Up;
                    managedQuat5c.SetLookOrientation(forward5c, upManaged);
                    float dtPrdt2 = MathHelpers.Clamp(forward5c.Dot(managedQuat5c.Forward), -1f, 1f);

                    float diffFromNative  = (float)(System.Math.Acos(dtPrdt1) * (180f / System.Math.PI));
                    float diffFromManaged = (float)(System.Math.Acos(dtPrdt2) * (180f / System.Math.PI));
                    float absoluteDiff    = System.Math.Abs(diffFromManaged - diffFromNative);
                    Assert.IsTrue(absoluteDiff <= test_margin_error, "SetLookOrientation failed at loop index " + i + ".Absolute Difference:" + absoluteDiff + " Expected (Native) :" + diffFromNative + ", Actual (Managed) :" + diffFromManaged + ", Forward : " + NativeExtensions.PrintString(forward5c) + ", Up :" + upManaged.ToString());
                }
                {
                    //boundary case where axis are flipped when comparing native to managed
                    Quaternion quatManaged    = Quaternion.Identity;
                    Vector3    upManaged      = Vector3.Up;
                    Vector3    forwardManaged = new Vector3(-8.126793f, 3.401123f, -1.644333f);
                    forwardManaged = forwardManaged.Normalized;
                    quatManaged.SetLookOrientation(forwardManaged, upManaged);

                    Quat quatNative    = Quat.CreateIdentity();
                    Vec3 forwardNative = new Vec3(-8.126793f, 3.401123f, -1.644333f);
                    forwardNative = forwardNative.normalized();
                    quatNative.SetRotationVDir(forwardNative);
                    bool isEqui1 = Quat.IsEquivalent(quatManaged, quatNative, 0.00999999776f);
                    Assert.IsTrue(isEqui1, String.Format("Native Quaternion {0} and Managed Quaternion {1} are not equivalent", ((Quaternion)quatNative).ToString(), quatManaged));
                }
            }

            //6 SetFromTORotation(Vector3 , Vector3)
            {
                Vec3 fromVec = new Vec3(0.5f, 0.5f, 0.5f);
                Vec3 toVec   = new Vec3(0.5f, -0.5f, -0.5f);
                Quat quat6a  = Quat.CreateIdentity();
                quat6a.SetRotationV0V1(fromVec, toVec);

                Vector3    fromVec2 = new Vector3(fromVec.x, fromVec.y, fromVec.z);
                Vector3    toVec2   = new Vector3(toVec.x, toVec.y, toVec.z);
                Quaternion quat6b   = Quaternion.Identity;
                quat6b.SetFromToRotation(fromVec2, toVec2);
                Assert.IsTrue(Quat.IsEquivalent(quat6a, quat6b), "SetFromToRotation failed");
            }
            //7 CreateRotationX(float)
            {
                Quat       quat7a = Quat.CreateRotationX(radians);
                Quaternion quat7b = Quaternion.CreateRotationX(radians);
                Assert.IsTrue(Quat.IsEquivalent(quat7a, quat7b), "CreateRotationX failed");
            }


            //8 CreateRotationY(float)
            {
                Quat       quat8a = Quat.CreateRotationY(radians + 0.1f);
                Quaternion quat8b = Quaternion.CreateRotationY(radians + 0.1f);
                Assert.IsTrue(Quat.IsEquivalent(quat8a, quat8b), "CreateRotationY failed");
            }


            //9 CreateRotationZ(float)
            {
                Quat       quat9a = Quat.CreateRotationZ(radians + 0.1f);
                Quaternion quat9b = Quaternion.CreateRotationZ(radians + 0.1f);
                Assert.IsTrue(Quat.IsEquivalent(quat9a, quat9b), "CreateRotationZ failed");
            }


            //10 CreateRotationXYZ(Angles3)
            {
                Angles3 angles  = new Vec3(radians, radians, radians);
                Quat    quat10a = Quat.CreateRotationXYZ(angles);

                Quaternion quat10b = Quaternion.CreateRotationXYZ(angles);
                Assert.IsTrue(Quat.IsEquivalent(quat10a, quat10b), "CreateRotationXYZ equivalence failed");
            }


            //11 Slerp(Quaternion, Quaternion, float)
            {
                float timeRatio = 0.5f;
                Quat  quat11a   = Quat.CreateRotationX(radians);
                Quat  quat11b   = Quat.CreateRotationY(radians);
                Quat  quat11c   = Quat.CreateIdentity();
                quat11c.SetSlerp(quat11a, quat11b, timeRatio);

                Quaternion quat11d = Quaternion.CreateRotationX(radians);
                Quaternion quat11e = Quaternion.CreateRotationY(radians);
                Quaternion quat11f = Quaternion.Slerp(quat11d, quat11e, timeRatio);
                Assert.IsTrue(Quat.IsEquivalent(quat11c, quat11f), "Slerp equivalence failed");
            }


            //12 Lerp(Quaternion, Quaternion, float)
            {
                float timeRatio = 0.5f;
                Quat  quat12a   = Quat.CreateRotationX(radians);
                Quat  quat12b   = Quat.CreateRotationY(radians);
                Quat  quat12c   = Quat.CreateIdentity();
                quat12c.SetNlerp(quat12a, quat12b, timeRatio);

                Quaternion quat12d = Quaternion.CreateRotationX(radians);
                Quaternion quat12e = Quaternion.CreateRotationY(radians);
                Quaternion quat12f = Quaternion.Lerp(quat12d, quat12e, timeRatio);
                Assert.IsTrue(Quat.IsEquivalent(quat12c, quat12f), "Lerp equivalence failed");
            }

            //13 Properties
            {
                Matrix3x3 matrix33 = Matrix33.CreateFromVectors(right, forward, up);
                Quat      quat13a  = Quat.CreateQuatFromMatrix(matrix33);

                Vector3    right2   = new Vector3(right.x, right.y, right.z);
                Vector3    forward2 = new Vector3(forward.x, forward.y, forward.z);
                Vector3    up2      = new Vector3(up.x, up.y, up.z);
                Quaternion quat13b  = Quaternion.CreateFromVectors(right2, forward2, up2);
                Assert.IsTrue(quat13a == quat13b, "Quaternions equality test failed");
                Assert.IsTrue(Quat.IsEquivalent(quat13a, quat13b), "Quaternions equivalence test failed");

                Assert.IsTrue(Vec3.IsEquivalent(right2, quat13b.Right), "Right equivalence test failed");
                Assert.IsTrue(Vec3.IsEquivalent(forward2, quat13b.Forward), "Forward equivalence test failed");
                Assert.IsTrue(Vec3.IsEquivalent(up2, quat13b.Up), "Up equivalence test failed");

                //inversion
                Quat       invertNative  = quat13a.GetInverted();
                Quaternion invertManaged = quat13b.Inverted;
                Assert.IsTrue(Quat.IsEquivalent(invertNative, invertManaged), "Inversion equivalence test failed");

                //normalization
                Quat       normalNative  = quat13a.GetNormalized();
                Quaternion normalManaged = quat13b.Normalized;
                Assert.IsTrue(Quat.IsEquivalent(normalNative, normalManaged), "Normalization test failed");

                //length
                float lengthNative  = quat13a.GetLength();
                float lengthManaged = quat13b.Length;
                Assert.IsTrue(System.Math.Abs(lengthNative - lengthManaged) <= Single.Epsilon, "Length test failed");

                //IsIdentity
                Quaternion managedIdentity = Quaternion.Identity;
                Assert.IsTrue(managedIdentity.IsIdentity, "Managed Identity test failed");

                Quat nativeIdentity = new Quat();
                nativeIdentity.SetIdentity();
                Assert.IsTrue(nativeIdentity.IsIdentity(), "Native Identity test failed");
                Assert.IsTrue(managedIdentity == nativeIdentity, "Identity comparison failed");

                // yaw pitch roll
            }
        }
Ejemplo n.º 10
0
        public void TestSlerp()
        {
            {             // t =0.5
                          //native
                Vec3 vec3p = new Vec3(1f, 1f, 1f);
                vec3p.normalize();
                Vec3 vec3q = new Vec3(0f, 1f, 1f);
                vec3q.normalize();
                Vec3 vec3Mid = Vec3.CreateSlerp(vec3p, vec3q, 0.5f);
                //managed
                Vector3 vector3p   = new Vector3(vec3p.x, vec3p.y, vec3p.z);
                Vector3 vector3q   = new Vector3(vec3q.x, vec3q.y, vec3q.z);
                Vector3 vector3Mid = Vector3.Slerp(vector3p, vector3q, 0.5f);

                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(vec3Mid, vector3Mid), " Slerp tests fails for native and managed vector3 1 " + vec3Mid.PrintString() + "," + vector3Mid.ToString());
            }
            {
                //t =0.0
                //native
                Vec3 vec3p = new Vec3(1f, 1f, 1f);
                vec3p.normalize();
                Vec3 vec3q = new Vec3(0f, 1f, 1f);
                vec3q.normalize();
                Vec3 vec3Mid = Vec3.CreateSlerp(vec3p, vec3q, 0.0f);
                //managed
                Vector3 vector3p   = new Vector3(vec3p.x, vec3p.y, vec3p.z);
                Vector3 vector3q   = new Vector3(vec3q.x, vec3q.y, vec3q.z);
                Vector3 vector3Mid = Vector3.Slerp(vector3p, vector3q, 0.0f);

                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(vec3Mid, vector3Mid), " Slerp tests fails for native and managed vector3 2 " + vec3Mid.PrintString() + "," + vector3Mid.ToString());
            }
            {
                //t =1.0
                //native
                Vec3 vec3p = new Vec3(1f, 1f, 1f);
                vec3p.normalize();
                Vec3 vec3q = new Vec3(0f, 1f, 1f);
                vec3q.normalize();
                Vec3 vec3Mid = Vec3.CreateSlerp(vec3p, vec3q, 1.0f);
                //managed
                Vector3 vector3p   = new Vector3(vec3p.x, vec3p.y, vec3p.z);
                Vector3 vector3q   = new Vector3(vec3q.x, vec3q.y, vec3q.z);
                Vector3 vector3Mid = Vector3.Slerp(vector3p, vector3q, 1.0f);

                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(vec3Mid, vector3Mid), " Slerp tests fails for native and managed vector3 3 " + vec3Mid.PrintString() + "," + vector3Mid.ToString());
            }

            //same vector but different t
            {
                Vec3 vec3p = new Vec3(1f, 1f, 1f);
                vec3p.normalize();
                Vec3 vec3q = new Vec3(1f, 1f, 1f);
                vec3q.normalize();
                Vec3 vec3Mid = Vec3.CreateSlerp(vec3p, vec3q, 0.5f);
                //managed
                Vector3 vector3p = new Vector3(1f, 1f, 1f);
                vector3p = vector3p.Normalized;
                Vector3 vector3q = new Vector3(1f, 1f, 1f);
                vector3q = vector3q.Normalized;
                Vector3 vector3Mid = Vector3.Slerp(vector3p, vector3q, 0.5f);

                //TODO
                //The following will be tested again as it passes in Launcher but fails in Nunit console runner
                //Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(vec3Mid, vector3Mid), " Slerp tests fails for native and managed vector3 4 " + vec3Mid.PrintString() + "," + vector3Mid.ToString());
                float length = (float)System.Math.Sqrt(3);
                length = 1.0f / length;
                Vector3 expectedResult = new Vector3(length, length, length);
            }
            {
                Vec3 vec3p = new Vec3(1f, 0f, 0f);
                vec3p.normalize();
                Vec3 vec3q = new Vec3(1f, 0f, 0f);
                vec3q.normalize();
                Vec3 vec3Mid = Vec3.CreateSlerp(vec3p, vec3q, 0.5f);
                //managed
                Vector3 vector3p   = new Vector3(vec3p.x, vec3p.y, vec3p.z);
                Vector3 vector3q   = new Vector3(vec3q.x, vec3q.y, vec3q.z);
                Vector3 vector3Mid = Vector3.Slerp(vector3p, vector3q, 0.5f);

                float differenceX = System.Math.Abs(vec3Mid.x - vector3Mid.x);
                float differenceY = System.Math.Abs(vec3Mid.y - vector3Mid.y);
                float differenceZ = System.Math.Abs(vec3Mid.z - vector3Mid.z);
                bool  isSmall     = differenceX < 0.0001f && differenceY < 0.0001f && differenceZ < 0.0001f;

                float   length         = 1f;
                Vector3 expectedResult = new Vector3(length, 0, 0);
            }
            {
                Vector3 p = new Vector3(1.0f, 0f, 0f);
                Vector3 q = new Vector3(-1.0f, 0f, 0f);

                //middle of interpolation will be (0,0)
                Vector3 expectedResult = new Vector3(0, 0, 0);

                Vector3 result = Vector3.SlerpUnclamped(p, q, 0.5f);
                Assert.IsTrue(NativeExtensions.TestVectorsAreEqual(result, expectedResult), "Slerp fails for managed vector3 8, expected result is :" + expectedResult.ToString() + ", actual result:" + result.ToString());
            }
            {
                Vector3 p2 = new Vector3(0.5f, 0f, 0f);
                Vector3 q2 = new Vector3(0.5f, 0f, 0f);

                //middle of interpolation will be (0,0)
                Vector3 expectedResult = new Vector3(1f, 0, 0);                 //after normalized

                Vector3 actualResult = Vector3.SlerpUnclamped(p2, q2, 0.5f);
            }
        }