static void Analize()
        {
            // Test multiplication with sizes from 100 to 1000 with step 100

            Array methods = Enum.GetValues(typeof(MultiplicationMethod));

            //for (int i = methods.Length - 1; i >= 0; i--)
            for (int i = 0; i < methods.Length; i++)
            {
                MultiplicationMethod method = (MultiplicationMethod)methods.GetValue(i);

                Console.WriteLine("\nMethod: " + method);

                MatrixMultiplier multiplier = MatrixMultiplier.GetMatrixMultiplier(method);
                //MatrixMultiplier multiplier = new WinogradMultiplier();

                for (int size = 100; size <= 400; size += 100)
                {
                    Matrix left  = new Matrix(size, size);
                    Matrix right = new Matrix(size, size);
                    Matrix.GenerateRandom(left, 0, size);
                    Matrix.GenerateRandom(right, 0, size);

                    long ticksAvg = TestNTimes(multiplier, left, right, 1);

                    Console.WriteLine("Size: " + size + ". Ticks: " + ticksAvg);
                }
            }
        }
        //------------------SCALING-------------------------------
        public void Scale(MCommonPrimitive shape, float sx, float sy, float sz)
        {
            float[,] scaleMatrix =
            {
                { sx,  0,  0, 0 },
                {  0, sy,  0, 0 },
                {  0,  0, sz, 0 },
                {  0,  0,  0, 1 }
            };

            shape.ModelMatrix = MatrixMultiplier.MultiplyMatrix(shape.ModelMatrix, scaleMatrix);
        }
        public void BigMatricesMultiplicationTest()
        {
            const int size    = 100;
            var       matrix1 = new Matrix(size, size, 1);
            var       matrix2 = new Matrix(size, size, 2);
            var       matrix3 = new Matrix(size, size, size * 2);
            var       matrix4 = MatrixMultiplier.MultiplyConcurrently(matrix1, matrix2);
            var       matrix5 = MatrixMultiplier.MultiplySequentally(matrix1, matrix2);

            Assert.IsTrue(MatrixComparer.Compare(matrix4, matrix3));
            Assert.IsTrue(MatrixComparer.Compare(matrix5, matrix3));
        }
        //------------------TRANSLATING-------------------------------
        public void Translate(MCommonPrimitive shape, float sx, float sy, float sz)
        {
            float[,] movementMatrix =
            {
                { 1, 0, 0, sx },
                { 0, 1, 0, sy },
                { 0, 0, 1, sz },
                { 0, 0, 0,  1 }
            };

            shape.ModelMatrix = MatrixMultiplier.MultiplyMatrix(movementMatrix, shape.ModelMatrix);
        }
        public void MatrixMultiplication_MatrixesAColumnsAndBRowsDoesntEqual_ExpectedArithmeticException()
        {
            // arrange
            float[,] matrixA = { { 1, 2, 7 }, { 3, 2, 2 } };

            float[,] matrixB = { { 8, 8 }, { 4, 0 } };

            // act
            float[,] ABMultiplicationResult = MatrixMultiplier.MultiplyMatrix(matrixA, matrixB);

            // assert
        }
        private void GetClippedCoordinates(MCommonPrimitive shape, float[,] projectionMatrix)
        {
            List <MPoint> vertices = shape.GetVertices();

            for (int i = 0; i < vertices.Count; ++i)
            {
                float[,] vertexCoords = { { vertices[i].X }, { vertices[i].Y }, { vertices[i].Z }, { 1 } };

                float[,] newCoords = MatrixMultiplier.MultiplyMatrix(projectionMatrix, vertexCoords);

                SetNewCoordinatesToPoint(vertices[i], newCoords);
            }
        }
        public void EmptyMatricesTest()
        {
            var matrix1 = new Matrix(new int[0, 0] {
            });
            var matrix2 = new Matrix(new int[0, 0] {
            });
            var matrix3 = MatrixMultiplier.MultiplyConcurrently(matrix1, matrix2);

            Assert.IsTrue(MatrixComparer.Compare(matrix3, matrix1));
            var matrix4 = MatrixMultiplier.MultiplySequentally(matrix1, matrix2);

            Assert.IsTrue(MatrixComparer.Compare(matrix4, matrix1));
        }
        public void IncorrectSizedMatricesTest()
        {
            var matrix1 = new Matrix(new int[, ] {
                { 1, 2, 3 },
                { 4, 5, 6 }
            });
            var matrix2 = new Matrix(new int[, ] {
                { 1, 2, 3 },
                { 4, 5, 6 }
            });

            Assert.Throws <ArgumentException>(() => MatrixMultiplier.MultiplyConcurrently(matrix1, matrix2));
            Assert.Throws <ArgumentException>(() => MatrixMultiplier.MultiplySequentally(matrix1, matrix2));
        }
        public void RotateY(MCommonPrimitive shape, double angle)
        {
            double rads = angle * Math.PI / 180.0;

            float[,] rotateY =
            {
                { (float)Math.Cos(rads),  0, (float)Math.Sin(rads), 0 },
                {                      0, 1,                     0, 0 },
                { -(float)Math.Sin(rads), 0, (float)Math.Cos(rads), 0 },
                {                      0, 0,                     0, 1 }
            };

            shape.ModelMatrix = MatrixMultiplier.MultiplyMatrix(shape.ModelMatrix, rotateY);
        }
Exemple #10
0
        static long TestNTimes(MatrixMultiplier multiplier, Matrix a, Matrix b, int repeats)
        {
            long ticks = 0;


            for (int i = 0; i < repeats; i++)

            {
                multiplier.Multiply(a, b);
                ticks += multiplier.Ticks;
            }


            return(ticks / repeats);
        }
Exemple #11
0
        private void TransformShape(MCommonPrimitive shape)
        {
            List <MPoint> vertices = shape.GetVertices();

            for (int i = 0; i < vertices.Count; ++i)
            {
                float[,] vertexCoords = { { vertices[i].SX }, { vertices[i].SY }, { vertices[i].SZ }, { 1 } };

                float[,] newCoords = MatrixMultiplier.MultiplyMatrix(shape.ModelMatrix, vertexCoords);

                SetNewCoordinatesToPoint(vertices[i], newCoords);

                ////TEST
                //TransformPointCoordsToDecart(vertices[i]);
            }
        }
        public void MatrixMultiplication_AMultipliedByBCorrect_ExpectedResultReturned()
        {
            // arrange
            float[,] matrixA = { { 1, 2, 7 }, { 3, 2, 2 }, { 5, 0, 4 } };

            float[,] matrixB = { { 8, 8, 6 }, { 4, 0, 0 }, { 1, 3, 2 } };

            float[,] expectedResult = { { 23, 29, 20 }, { 34, 30, 22 }, { 44, 52, 38 } };

            // act
            float[,] ABMultiplicationResult = MatrixMultiplier.MultiplyMatrix(matrixA, matrixB);

            bool isMatrixesEqual = MatrixComparator.IsMatrixesEqual(expectedResult, ABMultiplicationResult);

            // assert
            Assert.IsTrue(isMatrixesEqual);
        }
Exemple #13
0
        public void Sum()
        {
            var matrix1    = new Matrix(@"..\..\..\data\test_1.txt");
            var matrix2    = new Matrix(@"..\..\..\data\test_2.txt");
            var matrix_sum = new Matrix(@"..\..\..\data\test_sum.txt");

            var temp1   = matrix1.Get();
            var temp2   = matrix2.Get();
            var tempSum = matrix_sum.Get();

            var sum = MatrixMultiplier.Sum(temp1, temp2);

            var sumString     = JsonConvert.SerializeObject(sum);
            var tempSumString = JsonConvert.SerializeObject(tempSum);

            Assert.Equal(sumString, tempSumString);
        }
Exemple #14
0
        public static MatrixMultiplier GetMatrixMultiplier(MultiplicationMethod method)
        {
            MatrixMultiplier multiplier = null;

            switch (method)
            {
            case MultiplicationMethod.Simple:
                multiplier = new SimpleMultiplier();
                break;

            case MultiplicationMethod.Winograd:
                multiplier = new WinogradMultiplier();
                break;
            }

            return(multiplier);
        }
Exemple #15
0
        static void TestSimpleMethod()
        {
            Matrix first  = new Matrix(2, 2);
            Matrix second = new Matrix(2, 2);

            Matrix correctResult = new Matrix(2, 2);

            correctResult[0, 0] = 1;
            correctResult[0, 1] = 0;
            correctResult[1, 0] = 9;
            correctResult[1, 1] = 4;

            first[0, 0] = 0;
            first[0, 1] = 1;
            first[1, 0] = 2;
            first[1, 1] = 3;

            second[0, 0] = 3;
            second[0, 1] = 2;
            second[1, 0] = 1;
            second[1, 1] = 0;

            Console.WriteLine("First matrix: ");
            Console.WriteLine(first.ToString());

            Console.WriteLine("\nSecond matrix: ");
            Console.WriteLine(second.ToString());


            MatrixMultiplier multiplier = MatrixMultiplier.GetMatrixMultiplier(MultiplicationMethod.Simple);

            if (multiplier != null)
            {
                Matrix result = multiplier.Multiply(first, second);
                Console.WriteLine("Result: ");
                Console.WriteLine(result);

                Console.WriteLine(correctResult.Equals(result) ? "CORRECT" : "WRONG RESULT!");
            }



            Console.WriteLine();
        }
        public void NegativeNumbersMultiplicationTest()
        {
            var matrix1 = new Matrix(new int[, ] {
                { -1, -2, -3 },
                { -4, -5, -6 }
            });
            var matrix2 = new Matrix(new int[, ] {
                { 9, 8, 7 },
                { 6, 5, 4 },
                { 3, 2, 1 }
            });
            var matrix3 = MatrixMultiplier.MultiplyConcurrently(matrix1, matrix2);
            var matrix4 = new Matrix(new int[, ] {
                { -30, -24, -18 },
                { -84, -69, -54 }
            });

            Assert.IsTrue(MatrixComparer.Compare(matrix3, matrix4));
            var matrix5 = MatrixMultiplier.MultiplyConcurrently(matrix1, matrix2);

            Assert.IsTrue(MatrixComparer.Compare(matrix5, matrix4));
        }
        public void BigNumbersMultiplicationTest()
        {
            var matrix1 = new Matrix(new int[, ] {
                { 100, 200, 300 },
                { 400, 500, 600 }
            });
            var matrix2 = new Matrix(new int[, ] {
                { 900, 800, 700 },
                { 600, 500, 400 },
                { 300, 200, 100 }
            });
            var matrix3 = MatrixMultiplier.MultiplyConcurrently(matrix1, matrix2);
            var matrix4 = new Matrix(new int[, ] {
                { 300000, 240000, 180000 },
                { 840000, 690000, 540000 }
            });

            Assert.IsTrue(MatrixComparer.Compare(matrix3, matrix4));
            var matrix5 = MatrixMultiplier.MultiplySequentally(matrix1, matrix2);

            Assert.IsTrue(MatrixComparer.Compare(matrix5, matrix4));
        }
        public void SimpleMultiplicationTest()
        {
            var matrix1 = new Matrix(new int[, ] {
                { 1, 2, 3 },
                { 4, 5, 6 }
            });
            var matrix2 = new Matrix(new int[, ] {
                { 9, 8, 7 },
                { 6, 5, 4 },
                { 3, 2, 1 }
            });
            var matrix3 = MatrixMultiplier.MultiplyConcurrently(matrix1, matrix2);
            var matrix4 = new Matrix(new int[, ] {
                { 30, 24, 18 },
                { 84, 69, 54 }
            });

            Assert.IsTrue(MatrixComparer.Compare(matrix3, matrix4));
            var matrix5 = MatrixMultiplier.MultiplySequentally(matrix1, matrix2);

            Assert.IsTrue(MatrixComparer.Compare(matrix5, matrix4));
        }
        public void SourceMatricesTest()
        {
            var values1 = new int[, ] {
                { -1, -2, -3 },
                { -4, -5, -6 }
            };
            var values2 = new int[, ] {
                { 9, 8, 7 },
                { 6, 5, 4 },
                { 3, 2, 1 }
            };
            var matrix1 = new Matrix(values1);
            var matrix2 = new Matrix(values2);
            var matrix3 = new Matrix(values1);
            var matrix4 = new Matrix(values2);

            MatrixMultiplier.MultiplyConcurrently(matrix1, matrix2);
            Assert.IsTrue(MatrixComparer.Compare(matrix1, matrix3));
            Assert.IsTrue(MatrixComparer.Compare(matrix2, matrix4));
            MatrixMultiplier.MultiplySequentally(matrix1, matrix2);
            Assert.IsTrue(MatrixComparer.Compare(matrix1, matrix3));
            Assert.IsTrue(MatrixComparer.Compare(matrix2, matrix4));
        }
Exemple #20
0
        public void GetTransformedShape(MCommonPrimitive shape, Camera camera)
        {
            var vertices = shape.GetVertices();

            for (int i = 0; i < vertices.Count; ++i)
            {
                float[,] vertexCoords = { { vertices[i].SX }, { vertices[i].SY }, { vertices[i].SZ }, { vertices[i].SW } };

                var modelViewMatrix = MatrixMultiplier.MultiplyMatrix(camera.ViewMatrix, shape.ModelMatrix);
                var eyeCoordinates  = MatrixMultiplier.MultiplyMatrix(modelViewMatrix, vertexCoords);
                var clipCoordinates = MatrixMultiplier.MultiplyMatrix(camera.ProjectionMatrix, eyeCoordinates);

                //if (clipCoordinates[0, 0] < -1 || clipCoordinates[0, 0] > 1 ||
                //    clipCoordinates[1, 0] < -1 || clipCoordinates[1, 0] > 1)
                //{
                //    vertices.RemoveAt(i);
                //    continue;
                //}

                var ndc = new float[, ] {
                    { clipCoordinates[0, 0] / clipCoordinates[3, 0] },
                    { clipCoordinates[1, 0] / clipCoordinates[3, 0] },
                    { clipCoordinates[2, 0] / clipCoordinates[3, 0] },
                    { clipCoordinates[3, 0] }
                };

                var windowCoordinates = new float[, ] {
                    { 640 / 2 * ndc[0, 0] + (640 / 2) },
                    { 360 / 2 * ndc[1, 0] + (360 / 2) },
                    { (50 - (-50)) / 2 * ndc[2, 0] + (50 + (-50)) / 2 },
                    { ndc[3, 0] }
                };

                SetNewCoordinatesToPoint(vertices[i], windowCoordinates);
            }

            //float[,]
            //    ,
            //    projectionModelViewMatrix = GetProjectionModelViewMatrix(camera.ProjectionMatrix, modelViewMatrix);

            //GetClippedCoordinates(shape, projectionModelViewMatrix);
            //GetNormalizedCoordinates(shape);
            //GetWindowCoordinates(shape);

            //float[,] modelView = MatrixMultiplier.MultiplyMatrix(camera.ViewMatrix, shape.ModelMatrix);
            //float[,] projModelView = MatrixMultiplier.MultiplyMatrix(camera.ProjectionMatrix, modelView);

            //for (int i = 0; i < projModelView.GetLength(0); ++i)
            //{
            //    for (int j = 0; j < projModelView.GetLength(1); ++j)
            //    {
            //        if (i != 3 && j != 3)
            //            projModelView[i, j] /= projModelView[3, 3];
            //    }
            //}

            //List<MPoint> vertices = shape.GetVertices();

            //for (int i = 0; i < vertices.Count; ++i)
            //{
            //    float[,] vertexCoords = { { vertices[i].SX }, { vertices[i].SY }, { vertices[i].SZ }, { vertices[i].SW } };

            //    float[,] newCoords = MatrixMultiplier.MultiplyMatrix(projModelView, vertexCoords);

            //    SetNewCoordinatesToPoint(vertices[i], newCoords);

            //    TransformPointCoordsToDecart(vertices[i]);
            //}
        }
Exemple #21
0
 private float[,] GetModelViewMatrix(float[,] modelMatrix, float[,] viewMatrix)
 {
     return(MatrixMultiplier.MultiplyMatrix(viewMatrix, modelMatrix));
 }
Exemple #22
0
 private float[,] GetProjectionModelViewMatrix(float[,] projectionMatrix, float[,] modelViewMatrix)
 {
     return(MatrixMultiplier.MultiplyMatrix(projectionMatrix, modelViewMatrix));
 }