Esempio n. 1
0
        /// <summary>
        /// Converts the bases for the specified vector from the basis definition vectors.
        /// </summary>
        /// <param name="b1">The first basis definition vector.</param>
        /// <param name="b2">The second basis definition vector.</param>
        /// <param name="w">The vector to convert bases on.</param>
        /// <returns>The new basis of the vector.</returns>
        public static Vector2 ConvertBasis( Vector2 b1, Vector2 b2, Vector2 w )
        {
            //
            // [w] = B^(-1) * w = [ B.M11 * w.X + B.M12 * w.Y ]
            //					  [ B.M21 * w.X + B.M22 * w.Y ]
            //
            Vector2 basis = Vector2.Empty;
            Matrix B = new Matrix();

            // Build inverse basis matrix
            B.M11 = b1.X;
            B.M21 = b1.Y;
            B.M12 = b2.X;
            B.M22 = b2.Y;
            B.M33 = 1;
            B.M44 = 1;

            B.Invert();

            // Determine basis
            basis.X = B.M11 * w.X + B.M12 * w.Y;
            basis.Y = B.M21 * w.X + B.M22 * w.Y;

            return basis;
        }
Esempio n. 2
0
		public void TestInverse4()
		{
			// test identity inverse
			var m = Matrix.GetIdentity(4);

			Assert.IsTrue(Matrix.AreEqual(m.Invert(), new Matrix(new float[,] {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}})));

			// test a known inverse
			m = new Matrix(new[,]
			               	{
			               		{0.100f, 0.200f, 0.300f, 1.300f},
			               		{-0.400f, 0.500f, 0.600f, 1.400f},
			               		{0.700f, 0.800f, 0.900f, 1.500f},
			               		{1.000f, 1.100f, 1.200f, -1.600f}
			               	});

			var r = new Matrix(new[,]
			                   	{
			                   		{0.62500f, -1.25000f, 0.62500f, 0.00000f},
			                   		{-18.12500f, 2.50000f, 9.37500f, -3.75000f},
			                   		{15.8854167f, -1.25000f, -8.4895833f, 3.854167f},
			                   		{-0.15625f, 0.00000f, 0.46875f, -0.31250f}
			                   	});

			Assert.IsTrue(Matrix.AreEqual(m.Invert(), r, _tolerance));

			// test inverse multiplied against original is the identity
			m.SetRow(0, -1.1F, 2.6F, -7.1F, 2.2F);
			m.SetRow(1, 4.6F, -3.7F, 9.1F, 6.9F);
			m.SetRow(2, 4.1F, -3.1F, 7.7F, 7.1F);
			m.SetRow(3, -9.9F, 0.2F, 4.3F, 5.5F);

			Assert.IsTrue(Matrix.AreEqual(m*m.Invert(), Matrix.GetIdentity(4), _tolerance));
			Assert.IsTrue(Matrix.AreEqual(m.Invert()*m, Matrix.GetIdentity(4), _tolerance));

			// test non-invertible
			m.SetRow(0, 1, 0, 0, 0);
			m.SetRow(1, 0, 1, 0, 0);
			m.SetRow(2, 0, 0, 1, 0);
			m.SetRow(3, 0, 0, 5, 0);

			try
			{
				m.Invert();
				Assert.Fail("Expected an exception");
			}
			catch (ArgumentException) {}
		}
Esempio n. 3
0
		public void TestInverse3()
		{
			// test identity inverse
			var m = Matrix.GetIdentity(3);

			Assert.IsTrue(Matrix.AreEqual(m.Invert(), new Matrix(new float[,] {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}})));

			// test a known inverse
			m = new Matrix(new[,]
			               	{
			               		{0.100f, 0.200f, 0.300f},
			               		{-0.400f, 0.500f, 0.600f},
			               		{0.700f, 0.800f, 0.900f}
			               	});

			var r = new Matrix(new[,]
			                   	{
			                   		{0.62500f, -1.25000f, 0.62500f},
			                   		{-16.25000f, 2.50000f, 3.75000f},
			                   		{13.95833f, -1.25000f, -2.70833f}
			                   	});

			Assert.IsTrue(Matrix.AreEqual(m.Invert(), r, _tolerance));

			// test inverse multiplied against original is the identity
			m.SetRow(0, -1.1F, 2.6F, -7.1F);
			m.SetRow(1, 4.6F, -3.7F, 9.1F);
			m.SetRow(2, 4.1F, -3.1F, 7.7F);

			Assert.IsTrue(Matrix.AreEqual(m*m.Invert(), Matrix.GetIdentity(3), _tolerance));
			Assert.IsTrue(Matrix.AreEqual(m.Invert()*m, Matrix.GetIdentity(3), _tolerance));

			// test non-invertible
			m.SetRow(0, 1, 0, 0);
			m.SetRow(1, 0, 1, 0);
			m.SetRow(2, 0, 5, 0);

			try
			{
				m.Invert();
				Assert.Fail("Expected an exception");
			}
			catch (ArgumentException) {}
		}
Esempio n. 4
0
		public void TestInverse2()
		{
			// test identity inverse
			var m = Matrix.GetIdentity(2);

			Assert.IsTrue(Matrix.AreEqual(m.Invert(), new Matrix(new float[,] {{1, 0}, {0, 1}})));

			// test a known inverse
			m = new Matrix(new[,]
			               	{
			               		{0.100f, 0.200f},
			               		{-0.400f, 0.500f}
			               	});

			var r = new Matrix(new[,]
			                   	{
			                   		{3.84615f, -1.53846f},
			                   		{3.07692f, 0.76923f}
			                   	});

			Assert.IsTrue(Matrix.AreEqual(m.Invert(), r, _tolerance));

			// test inverse multiplied against original is the identity
			m.SetRow(0, -1.1F, 2.6F);
			m.SetRow(1, 4.6F, -3.7F);

			Assert.IsTrue(Matrix.AreEqual(m*m.Invert(), Matrix.GetIdentity(2), _tolerance));
			Assert.IsTrue(Matrix.AreEqual(m.Invert()*m, Matrix.GetIdentity(2), _tolerance));

			// test non-invertible
			m.SetRow(0, 1, 0);
			m.SetRow(1, 5, 0);

			try
			{
				m.Invert();
				Assert.Fail("Expected an exception");
			}
			catch (ArgumentException) {}
		}
Esempio n. 5
0
		public void TestInverse1()
		{
			// test identity inverse
			var m = Matrix.GetIdentity(1);

			Assert.IsTrue(Matrix.AreEqual(m.Invert(), new Matrix(new float[,] {{1}})));

			// test a known inverse
			m = new Matrix(new[,]
			               	{
			               		{0.100f}
			               	});

			var r = new Matrix(new[,]
			                   	{
			                   		{10.00f}
			                   	});

			Assert.IsTrue(Matrix.AreEqual(m.Invert(), r, _tolerance));

			// test inverse multiplied against original is the identity
			m.SetRow(0, -1.1F);

			Assert.IsTrue(Matrix.AreEqual(m*m.Invert(), Matrix.GetIdentity(1), _tolerance));
			Assert.IsTrue(Matrix.AreEqual(m.Invert()*m, Matrix.GetIdentity(1), _tolerance));

			// test non-invertible
			m.SetRow(0, 0);

			try
			{
				m.Invert();
				Assert.Fail("Expected an exception");
			}
			catch (ArgumentException) {}
		}
Esempio n. 6
0
		public void TestInverse()
		{
			try
			{
				var m = new Matrix(3, 2);
				m.Invert();
				Assert.Fail("Expected an exception");
			}
			catch (ArgumentException) {}

			try
			{
				var m = Matrix.GetIdentity(5);
				m.Invert();
				Assert.Fail("Expected an exception");
			}
			catch (NotImplementedException) {}
		}
Esempio n. 7
0
        public Vector2 IntersectPlane(Plane plane, Vector2 relativeMousePos)
        {
            // Relative screen position.
            float xRayView = relativeMousePos.X / Projection.M11;
            float yRayView = relativeMousePos.Y / Projection.M22;
            viewDirection.Normalize();
            Vector3 ray = viewDirection
                + rightVec * xRayView
                - upVec * yRayView;
            Vector4 pointVec = new Vector4(position - plane.Origin, 0);

            Matrix dirMat = new Matrix();
            Matrix test = Matrix.Translation(1, 1, 1);
            //var x = test.get_Rows(3);
            dirMat.set_Rows(0, new Vector4(plane.XAxis, 0));
            dirMat.set_Rows(1, new Vector4(plane.YAxis, 0));
            dirMat.set_Rows(2, new Vector4(-ray, 0));
            dirMat.set_Rows(3, Vector4.UnitW);
            dirMat.Invert();

            Vector4 res = Vector4.Transform(pointVec, dirMat);
            Vector2 xy = new Vector2(Vector4.Dot(dirMat.get_Rows(0), pointVec), Vector4.Dot(dirMat.get_Rows(1), pointVec));
            return new Vector2(res.X, res.Y);
        }
Esempio n. 8
0
        /// <summary>
        /// Pushes a matrix transformation.
        /// </summary>
        /// <param name="matrix">The matrix</param>
        /// <returns>A disposable used to undo the transformation.</returns>
        public IDisposable PushTransform(Matrix matrix)
        {
            _context.Transform(matrix.ToCairo());

            return Disposable.Create(() =>
            {
                _context.Transform(matrix.Invert().ToCairo());
            });
        }