From b531e0b7146689b9ffa62deb2d4f3f2f71dec397 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Mon, 5 Feb 2024 17:02:42 +0100 Subject: [PATCH] [FEAT]Add maxtrix translation --- raytracing/src/matrix.cpp | 14 ++++++++ raytracing/src/matrix.h | 1 + tests/04_transformations.cpp | 63 ++++++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 1 + 4 files changed, 79 insertions(+) create mode 100644 tests/04_transformations.cpp diff --git a/raytracing/src/matrix.cpp b/raytracing/src/matrix.cpp index 3f24caf..3e8b968 100644 --- a/raytracing/src/matrix.cpp +++ b/raytracing/src/matrix.cpp @@ -312,6 +312,20 @@ Matrix Matrix::identity(void) /* ------------------------------------------------------------------------- */ +Matrix Matrix::translation(double an_x, double an_y, double an_z) +{ + Matrix the_translation = { + {1, 0, 0, an_x}, + {0, 1, 0, an_y}, + {0, 0, 1, an_z}, + {0, 0, 0, 1} + }; + + return the_translation; +} + +/* ------------------------------------------------------------------------- */ + bool Matrix::validate_dimensions(const std::initializer_list> &a_values) const { for (const auto &the_row : a_values) diff --git a/raytracing/src/matrix.h b/raytracing/src/matrix.h index 138a181..bfaf2a3 100644 --- a/raytracing/src/matrix.h +++ b/raytracing/src/matrix.h @@ -71,6 +71,7 @@ namespace Raytracer Matrix inverse(void) const; static Matrix identity(void); + static Matrix translation(double an_x, double an_y, double an_z); private: bool validate_dimensions(const std::initializer_list> &a_values) const; diff --git a/tests/04_transformations.cpp b/tests/04_transformations.cpp new file mode 100644 index 0000000..418e6ac --- /dev/null +++ b/tests/04_transformations.cpp @@ -0,0 +1,63 @@ +/*! + * 04_transformations.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: 05/02/2024 + * + */ + +/*---------------------------------------------------------------------------*/ + +#include + +#include "raytracing.h" + +using namespace Raytracer; + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[04][TRANSFORMATION] Multiplying by a translation matrix", "[Matrix]") +{ + Matrix transform = Matrix::translation(5, -3, 2); + Tuple p = Tuple::Point(-3, 4, 5); + + REQUIRE(transform * p == Tuple::Point(2, 1, 7)); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[04][TRANSFORMATION] Multiplying by the inverse of a translation matrix", "[Matrix]") +{ + Matrix transform = Matrix::translation(5, -3, 2); + Matrix inv = transform.inverse(); + Tuple p = Tuple::Point(-3, 4, 5); + + REQUIRE(inv * p == Tuple::Point(-8, 7, 3)); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[04][TRANSFORMATION] Translation does not affect vectors", "[Matrix]") +{ + Matrix transform = Matrix::translation(5, -3, 2); + Tuple v = Tuple::Vector(-3, 4, 5); + + REQUIRE(transform * v == v); +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3042c83..a35c2fa 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -14,6 +14,7 @@ add_executable(main_test 02_1_colors.cpp 02_2_canvas.cpp 03_matrix.cpp + 04_transformations.cpp ) include_directories("${CMAKE_SOURCE_DIR}/tests")