/*! * 03_matrix.cpp * * Copyright (c) 2015-2024, NADAL Jean-Baptiste. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301 USA * * @Author: NADAL Jean-Baptiste * @Date: 01/02/2024 * */ /*---------------------------------------------------------------------------*/ #include #include "matrix.h" #include "tuple.h" using namespace Raytracer; /* ------------------------------------------------------------------------- */ TEST_CASE("[Matrix] Constructing and inspecting a 4x4 matrix", "[Matrix]") { Matrix m = { { 1, 2, 3, 4}, { 5.5, 6.5, 7.5, 8.5}, { 9, 10, 11, 12}, {13.5, 14.5, 15.5, 16.5} }; REQUIRE(m.rows() == 4); REQUIRE(m.cols() == 4); REQUIRE(m[0][0] == 1); REQUIRE(m[0][3] == 4); REQUIRE(m[1][0] == 5.5); REQUIRE(m[1][2] == 7.5); REQUIRE(m[2][2] == 11); REQUIRE(m[3][0] == 13.5); REQUIRE(m[3][2] == 15.5); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Matrix] A 2x2 matrix ought to be representable", "[Matrix]") { Matrix m = { {-3, 5}, { 1, -2} }; REQUIRE(m.rows() == 2); REQUIRE(m.cols() == 2); REQUIRE(m[0][0] == -3); REQUIRE(m[0][1] == 5); REQUIRE(m[1][0] == 1); REQUIRE(m[1][1] == -2); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Matrix] A 3x3 matrix ought to be representable", "[Matrix]") { Matrix m = { {-3, 5, 0}, { 1, -2, -7}, { 0, 1, 1} }; REQUIRE(m.rows() == 3); REQUIRE(m.cols() == 3); REQUIRE(m[0][0] == -3); REQUIRE(m[1][1] == -2); REQUIRE(m[2][2] == 1); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Matrix] Matrix equality with identical matrices", "[Matrix]") { Matrix a = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 8, 7, 6}, {5, 4, 3, 2} }; Matrix b = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 8, 7, 6}, {5, 4, 3, 2} }; REQUIRE(a == b); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Matrix] Matrix equality with different matrices", "[Matrix]") { Matrix a = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 8, 7, 6}, {5, 4, 3, 2} }; Matrix b = { {2, 3, 4, 5}, {6, 7, 8, 9}, {8, 7, 6, 5}, {4, 3, 2, 1} }; REQUIRE(a != b); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Matrix] Multiplying two matrices", "[Matrix]") { Matrix a = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 8, 7, 6}, {5, 4, 3, 2} }; Matrix b = { {-2, 1, 2, 3}, { 3, 2, 1, -1}, { 4, 3, 6, 5}, { 1, 2, 7, 8} }; Matrix c = { {20, 22, 50, 48}, {44, 54, 114, 108}, {40, 58, 110, 102}, {16, 26, 46, 42} }; REQUIRE((a * b) == c); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Matrix] a matrix multiplied by a tuple", "[Matrix]") { Matrix a = { {1, 2, 3, 4}, {2, 4, 4, 2}, {8, 6, 4, 1}, {0, 0, 0, 1} }; Tuple b(1, 2, 3, 1); REQUIRE((a * b) == Tuple(18, 24, 33, 1)); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Matrix] Multiplying a matrix by the identity matrix", "[Matrix]") { Matrix a = { {0, 1, 2, 4}, {1, 2, 4, 8}, {2, 4, 8, 16}, {4, 8, 16, 32} }; REQUIRE((a * Matrix::identity()) == a); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Matrix] Multiplying the identity matrix by a tuple", "[Matrix]") { Tuple a(1, 2, 3, 4); REQUIRE((Matrix::identity() * a) == a); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Matrix] Transposing a matrix", "[Matrix]") { Matrix a = { {0, 9, 3, 0}, {9, 8, 0, 8}, {1, 8, 5, 3}, {0, 0, 5, 8} }; Matrix transposed = { {0, 9, 1, 0}, {9, 8, 8, 0}, {3, 0, 5, 5}, {0, 8, 3, 8} }; a.transpose(); REQUIRE(a == transposed); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Matrix] Transposing the identity matrix", "[Matrix]") { Matrix a = Matrix::identity(); a.transpose(); REQUIRE(a == Matrix::identity()); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Matrix] Calculating the determinant of a 2x2 matrix", "[Matrix]") { Matrix a = { { 1, 5}, {-3, 2} }; REQUIRE(a.determinant() == 17); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Matrix] A submatrix of a 3x3 matrix is a 2x2 matrix", "[Matrix]") { Matrix a = { { 1, 5, 0}, {-3, 2, 7}, { 0, 6, -3} }; Matrix b = { {-3, 2}, { 0, 6} }; REQUIRE(a.sub_matrix(0, 2) == b); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Matrix] A submatrix of a 4x4 matrix is a 3x3 matrix", "[Matrix]") { Matrix a = { {-6, 1, 1, 6}, {-8, 5, 8, 6}, {-1, 0, 8, 2}, {-7, 1, -1, 1} }; Matrix b = { {-6, 1, 6}, {-8, 8, 6}, {-7, -1, 1} }; REQUIRE(a.sub_matrix(2, 1) == b); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Matrix] Calculating a minor of a 3x3 matrix", "[Matrix]") { Matrix a = { {3, 5, 0}, {2, -1, -7}, {6, -1, 5} }; Matrix b = a.sub_matrix(1, 0); REQUIRE(b.determinant() == 25); REQUIRE(a.minor(1, 0) == 25); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Matrix] Calculating a cofactor of a 3x3 matrix", "[Matrix]") { Matrix a = { {3, 5, 0}, {2, -1, -7}, {6, -1, 5} }; REQUIRE(a.minor(0, 0) == -12); REQUIRE(a.cofactor(0, 0) == -12); REQUIRE(a.minor(1, 0) == 25); REQUIRE(a.cofactor(1, 0) == -25); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Matrix] Calculating the determinant of a 3x3 matrix", "[Matrix]") { Matrix a = { { 1, 2, 6}, {-5, 8, -4}, { 2, 6, 4} }; REQUIRE(a.cofactor(0, 0) == 56); REQUIRE(a.cofactor(0, 1) == 12); REQUIRE(a.cofactor(0, 2) == -46); REQUIRE(a.determinant() == -196); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Matrix] Calculating the determinant of a 4x4 matrix", "[Matrix]") { Matrix a = { {-2, -8, 3, 5}, {-3, 1, 7, 3}, {-6, 7, 7, -9} }; REQUIRE(a.cofactor(0, 0) == 690); REQUIRE(a.cofactor(0, 1) == 447); REQUIRE(a.cofactor(0, 2) == 210); REQUIRE(a.cofactor(0, 3) == 51); REQUIRE(a.determinant() == -4071); }