Exemplo n.º 1
0
        public void Basic()
        {
            var rng = new Random(1);
            var M   = m4x4.Random4x4(-5, +5, rng);

            // Compare with m4x4
            var m = new Matrix(4, 4);

            for (int v = 0; v != 4; ++v)
            {
                for (int c = 0; c != 4; ++c)
                {
                    m[v, c] = M[v][c];
                }
            }

            Assert.True(Matrix.FEql(m, M));

            Assert.True(Math_.FEql(M.w.x, m[3, 0]));
            Assert.True(Math_.FEql(M.x.w, m[0, 3]));
            Assert.True(Math_.FEql(M.z.z, m[2, 2]));

            Assert.True(Math_.IsInvertible(M));
            Assert.True(Matrix.IsInvertible(m));

            var M1 = Math_.Invert(M);
            var m1 = Matrix.Invert(m);

            Assert.True(Matrix.FEql(m1, M1));

            var M2 = Math_.Transpose(M);
            var m2 = Matrix.Transpose(m);

            Assert.True(Matrix.FEql(m2, M2));
        }
Exemplo n.º 2
0
        [Test] public void CreateFrom3()
        {
            var rng = new Random(123456789);
            var a2b = m4x4.Transform(v4.Random3N(0.0f, rng), rng.FloatC(0.0f, 1.0f), v4.Random3(0.0f, 10.0f, 1.0f, rng));

            var b2a = Math_.Invert(a2b);
            var a2a = b2a * a2b;

            Assert.True(Math_.FEql(m4x4.Identity, a2a));

            var b2a_fast = Math_.InvertFast(a2b);

            Assert.True(Math_.FEql(b2a_fast, b2a));
        }
Exemplo n.º 3
0
        public void TestInversion()
        {
            var rng = new Random();
            {
                var m      = m3x4.Random(v4.Random3N(0, rng), -Math_.TauF, +Math_.TauF, rng);
                var inv_m0 = Math_.InvertFast(m);
                var inv_m1 = Math_.Invert(m);
                Assert.True(Math_.FEqlRelative(inv_m0, inv_m1, 0.001f));
            } {
                for (; ;)
                {
                    var m = m3x4.Random(-5.0f, +5.0f, 0, rng);
                    if (!Math_.IsInvertible(m))
                    {
                        continue;
                    }
                    var inv_m = Math_.Invert(m);
                    var I0    = inv_m * m;
                    var I1    = m * inv_m;

                    Assert.True(Math_.FEqlRelative(I0, m3x4.Identity, 0.001f));
                    Assert.True(Math_.FEqlRelative(I1, m3x4.Identity, 0.001f));
                    break;
                }
            } {
                var m = new m3x4(
                    new v4(0.25f, 0.5f, 1.0f, 0.0f),
                    new v4(0.49f, 0.7f, 1.0f, 0.0f),
                    new v4(1.0f, 1.0f, 1.0f, 0.0f));
                var INV_M = new m3x4(
                    new v4(10.0f, -16.666667f, 6.66667f, 0.0f),
                    new v4(-17.0f, 25.0f, -8.0f, 0.0f),
                    new v4(7.0f, -8.333333f, 2.333333f, 0.0f));

                var inv_m = Math_.Invert(m);
                Assert.True(Math_.FEqlRelative(inv_m, INV_M, 0.001f));
            }
        }
Exemplo n.º 4
0
        [Test] public void TestInversion()
        {
            var rng = new Random();
            //{
            //	var m = m2x2.Random(rng, v4.Random3N(0, rng), -(float)Math_.Tau, +(float)Math_.Tau);
            //	var inv_m0 = m3x4.InvertFast(m);
            //	var inv_m1 = m3x4.Invert(m);
            //	Assert.True(m3x4.FEql(inv_m0, inv_m1, 0.001f));
            //}
            {
                //m2x2.Random(rng, -5.0f, +5.0f);
                var m = new m2x2(
                    new v2(-3.0f, +4.2f),
                    new v2(1.2f, -0.3f));
                var inv_m = Math_.Invert(m);
                var I0    = inv_m * m;
                var I1    = m * inv_m;

                Assert.True(Math_.FEql(I1, m2x2.Identity));
                Assert.True(Math_.FEql(I0, m2x2.Identity));
            }
            {
                var m = new m2x2(
                    new v2(4f, 7f),
                    new v2(2f, 6f));
                var inv_m = Math_.Invert(m);

                var det   = 4f * 6f - 7f * 2f;
                var INV_M = new m2x2(
                    new v2(6f, -7f) / det,
                    new v2(-2f, 4f) / det);

                Assert.True(Math_.FEql(m * INV_M, m2x2.Identity));
                Assert.True(Math_.FEql(inv_m, INV_M));
            }
        }