From dbac8a37c4253031c3c12c73894f2129da9c90eb Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Mon, 5 Feb 2024 17:32:22 +0100 Subject: [PATCH] [FEAT] Add scaling --- raytracing/src/matrix.cpp | 14 ++++++++++++ raytracing/src/matrix.h | 1 + tests/04_transformations.cpp | 41 ++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/raytracing/src/matrix.cpp b/raytracing/src/matrix.cpp index 3e8b968..d7c56aa 100644 --- a/raytracing/src/matrix.cpp +++ b/raytracing/src/matrix.cpp @@ -326,6 +326,20 @@ Matrix Matrix::translation(double an_x, double an_y, double an_z) /* ------------------------------------------------------------------------- */ +Matrix Matrix::scaling(double an_x, double an_y, double an_z) +{ + Matrix the_scaling = { + {an_x, 0, 0, 0}, + { 0, an_y, 0, 0}, + { 0, 0, an_z, 0}, + { 0, 0, 0, 1} + }; + + return the_scaling; +} + +/* ------------------------------------------------------------------------- */ + 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 bfaf2a3..da797e4 100644 --- a/raytracing/src/matrix.h +++ b/raytracing/src/matrix.h @@ -72,6 +72,7 @@ namespace Raytracer static Matrix identity(void); static Matrix translation(double an_x, double an_y, double an_z); + static Matrix scaling(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 index 418e6ac..77d0de1 100644 --- a/tests/04_transformations.cpp +++ b/tests/04_transformations.cpp @@ -61,3 +61,44 @@ TEST_CASE("[04][TRANSFORMATION] Translation does not affect vectors", "[Matrix]" REQUIRE(transform * v == v); } + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[04][TRANSFORMATION] A scaling matrix applied to a point", "[Matrix]") +{ + Matrix transform = Matrix::scaling(2, 3, 4); + Tuple p = Tuple::Point(-4, 6, 8); + + REQUIRE(transform * p == Tuple::Point(-8, 18, 32)); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[04][TRANSFORMATION] A scaling matrix applied to a vector", "[Matrix]") +{ + Matrix transform = Matrix::scaling(2, 3, 4); + Tuple v = Tuple::Vector(-4, 6, 8); + + REQUIRE(transform * v == Tuple::Vector(-8, 18, 32)); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[04][TRANSFORMATION] Multiplying by the inverse of a scaling matrix", "[Matrix]") +{ + Matrix transform = Matrix::scaling(2, 3, 4); + Matrix inv = transform.inverse(); + Tuple v = Tuple::Vector(-4, 6, 8); + + REQUIRE(inv * v == Tuple::Vector(-2, 2, 2)); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[04][TRANSFORMATION] Reflection is scaling by a negative value", "[Matrix]") +{ + Matrix transform = Matrix::scaling(-1, 1, 1); + Tuple p = Tuple::Point(2, 3, 4); + + REQUIRE(transform * p == Tuple::Point(-2, 3, 4)); +}