[FEAT]Add maxtrix translation

This commit is contained in:
NADAL Jean-Baptiste
2024-02-05 17:02:42 +01:00
parent 51e07ea1a5
commit b531e0b714
4 changed files with 79 additions and 0 deletions

View File

@@ -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<std::initializer_list<double>> &a_values) const bool Matrix::validate_dimensions(const std::initializer_list<std::initializer_list<double>> &a_values) const
{ {
for (const auto &the_row : a_values) for (const auto &the_row : a_values)

View File

@@ -71,6 +71,7 @@ namespace Raytracer
Matrix inverse(void) const; Matrix inverse(void) const;
static Matrix identity(void); static Matrix identity(void);
static Matrix translation(double an_x, double an_y, double an_z);
private: private:
bool validate_dimensions(const std::initializer_list<std::initializer_list<double>> &a_values) const; bool validate_dimensions(const std::initializer_list<std::initializer_list<double>> &a_values) const;

View File

@@ -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 <catch.hpp>
#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);
}

View File

@@ -14,6 +14,7 @@ add_executable(main_test
02_1_colors.cpp 02_1_colors.cpp
02_2_canvas.cpp 02_2_canvas.cpp
03_matrix.cpp 03_matrix.cpp
04_transformations.cpp
) )
include_directories("${CMAKE_SOURCE_DIR}/tests") include_directories("${CMAKE_SOURCE_DIR}/tests")