From 5bcc33383dc1793efc797471c9c3098e3e08f688 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Mon, 5 Feb 2024 22:03:06 +0100 Subject: [PATCH] [FEAT] Add shearing matrix transform --- raytracing/src/matrix.cpp | 14 ++++++++++ raytracing/src/matrix.h | 1 + tests/04_transformations.cpp | 50 ++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/raytracing/src/matrix.cpp b/raytracing/src/matrix.cpp index dfd2b3d..f8cbf81 100644 --- a/raytracing/src/matrix.cpp +++ b/raytracing/src/matrix.cpp @@ -382,6 +382,20 @@ Matrix Matrix::rotation_z(double a_radians) /* ------------------------------------------------------------------------- */ +Matrix Matrix::shearing(double a_xy, double a_xz, double a_yx, double a_yz, double a_zx, double a_zy) +{ + Matrix the_shearing = { + { 1, a_xy, a_xz, 0}, + {a_yx, 1, a_yz, 0}, + {a_zx, a_zy, 1, 0}, + { 0, 0, 0, 1} + }; + + return the_shearing; +} + +/* ------------------------------------------------------------------------- */ + 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 3afa4ce..149bd90 100644 --- a/raytracing/src/matrix.h +++ b/raytracing/src/matrix.h @@ -76,6 +76,7 @@ namespace Raytracer static Matrix rotation_x(double a_radians); static Matrix rotation_y(double a_radians); static Matrix rotation_z(double a_radians); + static Matrix shearing(double a_xy, double a_xz, double a_yx, double a_yz, double a_zx, double a_zy); private: bool validate_dimensions(const std::initializer_list> &a_values) const; diff --git a/tests/04_transformations.cpp b/tests/04_transformations.cpp index ce0258f..cdadc4d 100644 --- a/tests/04_transformations.cpp +++ b/tests/04_transformations.cpp @@ -151,3 +151,53 @@ TEST_CASE("[04][Trans] Rotating a point around the z axis", "[Matrix]") REQUIRE(half_quarter * p == Tuple::Point(-sqrt(2) / 2, sqrt(2) / 2, 0)); REQUIRE(full_quarter * p == Tuple::Point(-1, 0, 0)); } + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[04][Trans] A shearing transformation moves x in proportion to y", "[Matrix]") +{ + Matrix transform = Matrix::shearing(1, 0, 0, 0, 0, 0); + Tuple p = Tuple::Point(2, 3, 4); + + REQUIRE(transform * p == Tuple::Point(5, 3, 4)); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[04][Trans] A shearing transformation moves y in proportion to x", "[Matrix]") +{ + Matrix transform = Matrix::shearing(0, 0, 1, 0, 0, 0); + Tuple p = Tuple::Point(2, 3, 4); + + REQUIRE(transform * p == Tuple::Point(2, 5, 4)); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[04][Trans] A shearing transformation moves y in proportion to z", "[Matrix]") +{ + Matrix transform = Matrix::shearing(0, 0, 0, 1, 0, 0); + Tuple p = Tuple::Point(2, 3, 4); + + REQUIRE(transform * p == Tuple::Point(2, 7, 4)); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[04][Trans] A shearing transformation moves z in proportion to x", "[Matrix]") +{ + Matrix transform = Matrix::shearing(0, 0, 0, 0, 1, 0); + Tuple p = Tuple::Point(2, 3, 4); + + REQUIRE(transform * p == Tuple::Point(2, 3, 6)); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[04][Trans] A shearing transformation moves z in proportion to y", "[Matrix]") +{ + Matrix transform = Matrix::shearing(0, 0, 0, 0, 0, 1); + Tuple p = Tuple::Point(2, 3, 4); + + REQUIRE(transform * p == Tuple::Point(2, 3, 7)); +}