[FEAT] Add shearing matrix transform

This commit is contained in:
2024-02-05 22:03:06 +01:00
parent ac1c702f78
commit 5bcc33383d
3 changed files with 65 additions and 0 deletions

View File

@@ -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<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

@@ -76,6 +76,7 @@ namespace Raytracer
static Matrix rotation_x(double a_radians); static Matrix rotation_x(double a_radians);
static Matrix rotation_y(double a_radians); static Matrix rotation_y(double a_radians);
static Matrix rotation_z(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: 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

@@ -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(half_quarter * p == Tuple::Point(-sqrt(2) / 2, sqrt(2) / 2, 0));
REQUIRE(full_quarter * p == Tuple::Point(-1, 0, 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));
}