[FEAT] RAY: Add transform method
This commit is contained in:
@@ -34,13 +34,7 @@ using namespace Raytracer;
|
|||||||
|
|
||||||
/* ------------------------------------------------------------------------- */
|
/* ------------------------------------------------------------------------- */
|
||||||
|
|
||||||
Intersections::Intersections(void)
|
Intersections::Intersections(const std::initializer_list<Intersection> &a_list)
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ------------------------------------------------------------------------- */
|
|
||||||
|
|
||||||
Intersections::Intersections(const std::initializer_list<Intersection> a_list)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
m_data.reserve(a_list.size());
|
m_data.reserve(a_list.size());
|
||||||
|
|||||||
@@ -41,8 +41,8 @@ namespace Raytracer
|
|||||||
class Intersections
|
class Intersections
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Intersections(void);
|
Intersections(void) = default;
|
||||||
Intersections(const std::initializer_list<Intersection> a_list);
|
Intersections(const std::initializer_list<Intersection> &a_list);
|
||||||
Intersections(Intersections &an_intersections);
|
Intersections(Intersections &an_intersections);
|
||||||
|
|
||||||
Intersection &operator[](uint8_t an_index);
|
Intersection &operator[](uint8_t an_index);
|
||||||
|
|||||||
@@ -84,3 +84,15 @@ Intersections Ray::intersect(Sphere a_sphere)
|
|||||||
|
|
||||||
return the_intersections;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -31,6 +31,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "intersections.h"
|
#include "intersections.h"
|
||||||
|
#include "matrix.h"
|
||||||
#include "sphere.h"
|
#include "sphere.h"
|
||||||
#include "tuple.h"
|
#include "tuple.h"
|
||||||
|
|
||||||
@@ -41,6 +42,7 @@ namespace Raytracer
|
|||||||
class Ray
|
class Ray
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
Ray(void) = default;
|
||||||
Ray(Tuple an_origin, Tuple a_direction);
|
Ray(Tuple an_origin, Tuple a_direction);
|
||||||
|
|
||||||
const Tuple &origin(void) const;
|
const Tuple &origin(void) const;
|
||||||
@@ -49,6 +51,8 @@ namespace Raytracer
|
|||||||
Tuple position(double a_distance);
|
Tuple position(double a_distance);
|
||||||
Intersections intersect(Sphere a_sphere);
|
Intersections intersect(Sphere a_sphere);
|
||||||
|
|
||||||
|
Ray transform(const Matrix &a_matrix);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Tuple m_origin;
|
Tuple m_origin;
|
||||||
Tuple m_direction;
|
Tuple m_direction;
|
||||||
|
|||||||
@@ -227,3 +227,27 @@ TEST_CASE("[05][Rays] The hit is always the lowest nonnegative intersection", "[
|
|||||||
|
|
||||||
REQUIRE(i == i4);
|
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));
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user