diff --git a/raytracing/src/intersections.cpp b/raytracing/src/intersections.cpp index 8c485a1..27c274e 100644 --- a/raytracing/src/intersections.cpp +++ b/raytracing/src/intersections.cpp @@ -34,13 +34,7 @@ using namespace Raytracer; /* ------------------------------------------------------------------------- */ -Intersections::Intersections(void) -{ -} - -/* ------------------------------------------------------------------------- */ - -Intersections::Intersections(const std::initializer_list a_list) +Intersections::Intersections(const std::initializer_list &a_list) { m_data.reserve(a_list.size()); diff --git a/raytracing/src/intersections.h b/raytracing/src/intersections.h index 1ed89f7..5087615 100644 --- a/raytracing/src/intersections.h +++ b/raytracing/src/intersections.h @@ -41,8 +41,8 @@ namespace Raytracer class Intersections { public: - Intersections(void); - Intersections(const std::initializer_list a_list); + Intersections(void) = default; + Intersections(const std::initializer_list &a_list); Intersections(Intersections &an_intersections); Intersection &operator[](uint8_t an_index); diff --git a/raytracing/src/ray.cpp b/raytracing/src/ray.cpp index 8582904..583d861 100644 --- a/raytracing/src/ray.cpp +++ b/raytracing/src/ray.cpp @@ -84,3 +84,15 @@ Intersections Ray::intersect(Sphere a_sphere) return the_intersections; } + +/* ------------------------------------------------------------------------- */ + +Ray Ray::transform(const Matrix &a_matrix) +{ + Ray the_output_ray; + + the_output_ray.m_origin = a_matrix * m_origin; + the_output_ray.m_direction = a_matrix * m_direction; + + return the_output_ray; +} diff --git a/raytracing/src/ray.h b/raytracing/src/ray.h index f3f930e..bd83290 100644 --- a/raytracing/src/ray.h +++ b/raytracing/src/ray.h @@ -31,6 +31,7 @@ #include #include "intersections.h" +#include "matrix.h" #include "sphere.h" #include "tuple.h" @@ -41,6 +42,7 @@ namespace Raytracer class Ray { public: + Ray(void) = default; Ray(Tuple an_origin, Tuple a_direction); const Tuple &origin(void) const; @@ -49,6 +51,8 @@ namespace Raytracer Tuple position(double a_distance); Intersections intersect(Sphere a_sphere); + Ray transform(const Matrix &a_matrix); + private: Tuple m_origin; Tuple m_direction; diff --git a/tests/05_rays.cpp b/tests/05_rays.cpp index e0fcd34..7a34b97 100644 --- a/tests/05_rays.cpp +++ b/tests/05_rays.cpp @@ -227,3 +227,27 @@ TEST_CASE("[05][Rays] The hit is always the lowest nonnegative intersection", "[ REQUIRE(i == i4); } + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[05][Rays] Translating a ray", "[Rays]") +{ + Ray r(Tuple::Point(1, 2, 3), Tuple::Vector(0, 1, 0)); + Matrix m = Matrix::translation(3, 4, 5); + Ray r2 = r.transform(m); + + REQUIRE(r2.origin() == Tuple::Point(4, 6, 8)); + REQUIRE(r2.direction() == Tuple::Vector(0, 1, 0)); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[05][Rays] Scaling a ray", "[Rays]") +{ + Ray r(Tuple::Point(1, 2, 3), Tuple::Vector(0, 1, 0)); + Matrix m = Matrix::scaling(2, 3, 4); + Ray r2 = r.transform(m); + + REQUIRE(r2.origin() == Tuple::Point(2, 6, 12)); + REQUIRE(r2.direction() == Tuple::Vector(0, 3, 0)); +}