Files
raytracer_challenge/tests/05_rays.cpp

697 lines
20 KiB
C++

/*!
* 05_rays.cpp
*
* Copyright (c) 2015-2024, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 05/02/2024
*
*/
/*---------------------------------------------------------------------------*/
#include <catch.hpp>
#include "raytracing.h"
using namespace Raytracer;
/* ------------------------------------------------------------------------- */
SCENARIO("Creating and querying a ray", "[features/rays.feature]")
{
GIVEN("origin <- point(1, 2, 3)")
{
Tuple origin = Tuple::Point(1, 2, 3);
AND_GIVEN("direction <- vector(4, 5, 6)")
{
Tuple direction = Tuple::Vector(4, 5, 6);
WHEN("r <- ray(origin, direction)")
{
Ray r(origin, direction);
THEN("r.origin = origin")
{
REQUIRE(r.origin() == origin);
}
AND_THEN("r.direction = direction")
{
REQUIRE(r.direction() == direction);
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Computing a point from a distance", "[features/rays.feature]")
{
GIVEN("r <- ray(point(2, 3, 4), vector(1, 0, 0))")
{
Ray r(Tuple::Point(2, 3, 4), Tuple::Vector(1, 0, 0));
THEN("position(r,0) = point(2, 3, 4)")
{
REQUIRE(r.position(0) == Tuple::Point(2, 3, 4));
}
AND_THEN("position(r,1) == point(3, 3, 4)")
{
REQUIRE(r.position(1) == Tuple::Point(3, 3, 4));
}
AND_THEN("position(r,-1) = point(1, 3, 4)")
{
REQUIRE(r.position(-1) == Tuple::Point(1, 3, 4));
}
AND_THEN("position(r,2.5) == point(4.5, 3, 4)")
{
REQUIRE(r.position(2.5) == Tuple::Point(4.5, 3, 4));
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("A ray intersects a sphere at two points", "[features/spheres.feature]")
{
GIVEN("r <- ray(point(0, 0, -5), vector(0, 0, 1))")
{
Ray r(Tuple::Point(0, 0, -5), Tuple::Vector(0, 0, 1));
AND_GIVEN("s <- sphere()")
{
Sphere s;
WHEN("xs <- intersect(s,r)")
{
Intersections xs = s.intersect(r);
THEN("xs.count = 2")
{
REQUIRE(xs.count() == 2);
}
AND_THEN("xs[0] = 4.0")
{
REQUIRE(xs[0].distance_t() == 4.0);
}
AND_THEN("xs[1 = 6.0")
{
REQUIRE(xs[1].distance_t() == 6.0);
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("A ray intersects a sphere at a tangent", "[features/spheres.feature]")
{
GIVEN("r <- ray(point(0, 0, -5), vector(0, 0, 1))")
{
Ray r(Tuple::Point(0, 1, -5), Tuple::Vector(0, 0, 1));
AND_GIVEN("s <- sphere()")
{
Sphere s;
WHEN("xs <- intersect(s,r)")
{
Intersections xs = s.intersect(r);
THEN("xs.count = 2")
{
REQUIRE(xs.count() == 2);
}
AND_THEN("xs[0] = 5.0")
{
REQUIRE(xs[0].distance_t() == 5.0);
}
AND_THEN("xs[1] = 5.0")
{
REQUIRE(xs[1].distance_t() == 5.0);
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("A ray misses a sphere", "[features/spheres.feature]")
{
GIVEN("r <- ray(point(0, 2, -5), vector(0, 0, 1))")
{
Ray r(Tuple::Point(0, 2, -5), Tuple::Vector(0, 0, 1));
AND_GIVEN("s <- sphere()")
{
Sphere s;
WHEN("xs <- intersect(s,r)")
{
Intersections xs = s.intersect(r);
THEN("xs.count = 0")
{
REQUIRE(xs.count() == 0);
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("A originates inside a sphere", "[features/spheres.feature]")
{
GIVEN("r <- ray(point(0, 0, 0), vector(0, 0, 1))")
{
Ray r(Tuple::Point(0, 0, 0), Tuple::Vector(0, 0, 1));
AND_GIVEN("s <- sphere()")
{
Sphere s;
WHEN("xs <- intersect(s,r)")
{
Intersections xs = s.intersect(r);
THEN("xs.count = 2")
{
REQUIRE(xs.count() == 2);
}
AND_THEN("xs[0] = -1.0")
{
REQUIRE(xs[0].distance_t() == -1.0);
}
AND_THEN("xs[1] = 1.0")
{
REQUIRE(xs[1].distance_t() == 1.0);
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("A sphere is behind a ray", "[features/spheres.feature]")
{
GIVEN("r <- ray(point(0, 0, 5), vector(0, 0, 1))")
{
Ray r(Tuple::Point(0, 0, 5), Tuple::Vector(0, 0, 1));
AND_GIVEN("s <- sphere()")
{
Sphere s;
WHEN("xs <- intersect(s,r)")
{
Intersections xs = s.intersect(r);
THEN("xs.count = 2")
{
REQUIRE(xs.count() == 2);
}
AND_THEN("xs[0] = -6.0")
{
REQUIRE(xs[0].distance_t() == -6.0);
}
AND_THEN("xs[1] = -4.0)")
{
REQUIRE(xs[1].distance_t() == -4.0);
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("An intersection encapsulates t and object", "[features/intersections.feature]")
{
GIVEN("s <- sphere()")
{
Sphere s;
WHEN("intersection(3.5,s)")
{
Intersection i(3.5, &s);
THEN("i.t = 3.5")
{
REQUIRE(i.distance_t() == 3.5);
}
AND_THEN("i.object = s")
{
REQUIRE(i.object() == s);
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("An intersection could be affected", "[features/intersections.feature]")
{
GIVEN("s <- sphere()")
{
Sphere s;
AND_GIVEN("i1 <- intersection(3.5,s) and i2 <- intersection()")
{
Intersection i1(3.5, &s);
Intersection i2;
WHEN("i2 <- i1")
{
i1 = i1;
i2 = i1;
THEN("i2.t = 3.5")
{
REQUIRE(i2.distance_t() == 3.5);
}
AND_THEN("i2.object = s")
{
REQUIRE(i2.object() == s);
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Intersection could be compared", "[features/intersections.feature]")
{
GIVEN("s <- sphere()")
{
Sphere s;
AND_GIVEN("i1 <- intersection(3,s) and i2 <- intersection(4,s)")
{
Intersection i1(3.0, &s);
Intersection i2(4.0, &s);
THEN("i2 > i1")
{
REQUIRE(i2 > i1);
}
AND_THEN("i1 < i2")
{
REQUIRE(i1 < i2);
}
AND_THEN("i2 > 2")
{
REQUIRE(i2 > 2.0);
}
AND_THEN("i2 >= 4")
{
REQUIRE(i2 >= 4.0);
}
AND_THEN("i2 < 6")
{
REQUIRE(i2 < 6.0);
}
AND_THEN("i2 <= 4")
{
REQUIRE(i2 <= 4.0);
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Aggregating intersections", "[features/intersections.feature]")
{
GIVEN("s <- sphere()")
{
Sphere s;
AND_GIVEN("i1 <- intersection(1,s)")
{
Intersection i1(1, &s);
AND_GIVEN("i2 <- intersection(2,s)")
{
Intersection i2(2, &s);
WHEN("xs <- intersections(i1,i2)")
{
Intersections xs = Intersections({i1, i2});
THEN("xs.count = 2")
{
REQUIRE(xs.count() == 2);
}
AND_THEN("xs[0].t = 1")
{
REQUIRE(xs[0].distance_t() == 1);
}
AND_THEN("xs[1].t = 2")
{
REQUIRE(xs[1].distance_t() == 2);
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Operations with intersections", "[features/intersections.feature]")
{
GIVEN("xs <- intersections({})")
{
Intersections xs1({});
AND_GIVEN("s <- sphere()")
{
Sphere s;
Intersection i1(1, &s);
AND_GIVEN("xs2 <- intersections({i1})")
{
Intersections xs2({i1});
WHEN("xs1 <- xs2")
{
// xs2 = xs2;
xs1 = xs2;
THEN("xs1.count = 1")
{
REQUIRE(xs1.count() == 1);
}
Intersections xs3 = xs1;
THEN("xs3.count = 1")
{
REQUIRE(xs3.count() == 1);
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Intersect sets the object on the intersection", "[features/spheres.feature]")
{
GIVEN("r <- ray(point(0, 0, 5), vector(0, 0, 1))")
{
Ray r(Tuple::Point(0, 0, 5), Tuple::Vector(0, 0, 1));
AND_GIVEN("s <- sphere()")
{
Sphere s;
WHEN("xs <- intersect(s,r)")
{
Intersections xs = s.intersect(r);
THEN("xs.count = 2")
{
REQUIRE(xs.count() == 2);
}
AND_THEN("xs[0].object = s")
{
REQUIRE(xs[0].object() == s);
}
AND_THEN("xs[1].object = s")
{
REQUIRE(xs[1].object() == s);
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("The hit, when all intersections have positive t", "[features/intersections.feature]")
{
GIVEN("s <- sphere()")
{
Sphere s;
AND_GIVEN("i1 <- intersection(1,s)")
{
Intersection i1(1, &s);
AND_GIVEN("i2 <- intersection(2,s)")
{
Intersection i2(2, &s);
AND_GIVEN("xs <- intersections(i1,i2)")
{
Intersections xs = Intersections({i2, i1});
WHEN("i <- hit(xs)")
{
Intersection i = xs.hit();
THEN("i = i1")
{
REQUIRE(i == i1);
}
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("The hit, when some intersections have negative t", "[features/intersections.feature]")
{
GIVEN("s <- sphere()")
{
Sphere s;
AND_GIVEN("i1 <- intersection(-1,s)")
{
Intersection i1(-1, &s);
AND_GIVEN("i2 <- intersection(2,s)")
{
Intersection i2(1, &s);
AND_GIVEN("xs <- intersections(i1,i2)")
{
Intersections xs = Intersections({i2, i1});
WHEN("i <- hit(xs)")
{
Intersection i = xs.hit();
THEN("i = i2")
{
REQUIRE(i == i2);
}
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("The hit, when all intersections have negative t", "[features/intersections.feature]")
{
GIVEN("s <- sphere()")
{
Sphere s;
AND_GIVEN("i1 <- intersection(-2,s)")
{
Intersection i1(-2, &s);
AND_GIVEN("i2 <- intersection(-1,s)")
{
Intersection i2(-1, &s);
AND_GIVEN("xs <- intersections(i1,i2)")
{
Intersections xs = Intersections({i1, i2});
WHEN("i <- hit(xs)")
{
auto i = xs.hit();
THEN("i is nothing")
{
REQUIRE(i.is_nothing());
REQUIRE(!i.is_defined());
}
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("The hit is always the lowest nonnegative intersection", "[features/intersections.feature]")
{
GIVEN("s <- sphere()")
{
Sphere s;
AND_GIVEN("i1 <- intersection(5,s)")
{
Intersection i1(5, &s);
AND_GIVEN("i2 <- intersection(7,s)")
{
Intersection i2(7, &s);
AND_GIVEN("i3 <- intersection(-3,s)")
{
Intersection i3(-3, &s);
AND_GIVEN("i4 <- intersection(2,s)")
{
Intersection i4(2, &s);
AND_GIVEN("xs <- intersections(i1, i2, i3, i4)")
{
Intersections xs = Intersections({i1, i2, i3, i4});
WHEN("i <- hit(xs)")
{
Intersection i = xs.hit();
THEN("i = i4")
{
REQUIRE(i == i4);
}
AND_THEN("i is defined")
{
REQUIRE(i.is_defined());
}
}
}
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Translating a ray", "[features/rays.feature]")
{
GIVEN("r <- ray(point(1, 2, 3), vector(0, 1, 0))")
{
Ray r(Tuple::Point(1, 2, 3), Tuple::Vector(0, 1, 0));
AND_GIVEN("m <- translation(3, 4, 5)")
{
Matrix m = Matrix::translation(3, 4, 5);
WHEN("r2 <- transform(r,m)")
{
Ray r2 = r.transform(m);
THEN("r2.origin = point(4, 6, 8)")
{
REQUIRE(r2.origin() == Tuple::Point(4, 6, 8));
}
AND_THEN("r2.direction = vector(0, 1, 0)")
{
REQUIRE(r2.direction() == Tuple::Vector(0, 1, 0));
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Scaling a ray", "[features/rays.feature]")
{
GIVEN("r <- ray(point(1, 2, 3), vector(0, 1, 0))")
{
Ray r(Tuple::Point(1, 2, 3), Tuple::Vector(0, 1, 0));
AND_GIVEN("m <- scaling(2, 3, 4)")
{
Matrix m = Matrix::scaling(2, 3, 4);
WHEN("r2 <- transform(r,m)")
{
Ray r2 = r.transform(m);
THEN("r2.origin = point(2, 6, 12)")
{
REQUIRE(r2.origin() == Tuple::Point(2, 6, 12));
}
AND_THEN("r2.direction = vector(0, 3, 0)")
{
REQUIRE(r2.direction() == Tuple::Vector(0, 3, 0));
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("A sphere's default transformation", "[features/spheres.feature]")
{
GIVEN("s <- Sphere()")
{
Sphere s;
THEN("s.transform = identity_matrix")
{
REQUIRE(s.transform() == Matrix::identity());
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Changing a sphere's transformation", "[features/spheres.feature]")
{
GIVEN("s <- Sphere()")
{
Sphere s;
AND_GIVEN("t <- translation(2, 3, 4)")
{
Matrix t = Matrix::translation(2, 3, 4);
WHEN("set_transform(s,t)")
{
s.set_transform(t);
THEN("s.transform = t")
{
REQUIRE(s.transform() == t);
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Intersecting a scaled sphere with a ray", "[features/spheres.feature]")
{
GIVEN("r <- ray(point(0, 0, -5), vector(0, 0, 1))")
{
Ray r(Tuple::Point(0, 0, -5), Tuple::Vector(0, 0, 1));
AND_GIVEN("s <- Sphere()")
{
Sphere s;
WHEN("set_transform(s,scaling(2,2,2))")
{
s.set_transform(Matrix::scaling(2, 2, 2));
AND_WHEN("xs <- intersect(s,r)")
{
Intersections xs = s.intersect(r);
THEN("xs.count = 2")
{
REQUIRE(xs.count() == 2);
}
AND_THEN("xs[0].t = 3")
{
REQUIRE(xs[0].distance_t() == 3);
}
AND_THEN("xs[1].t = 7")
{
REQUIRE(xs[1].distance_t() == 7);
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Intersecting a translated sphere with a ray", "[features/spheres.feature]")
{
GIVEN("r <- ray(point(0, 0, -5), vector(0, 0, 1))")
{
Ray r(Tuple::Point(0, 0, -5), Tuple::Vector(0, 0, 1));
AND_GIVEN("s <- Sphere()")
{
Sphere s;
WHEN("set_transform(s,translation(5, 0, 0))")
{
s.set_transform(Matrix::translation(5, 0, 0));
AND_WHEN("xs <- intersect(s,t)")
{
Intersections xs = s.intersect(r);
THEN("xs.count = 0")
{
REQUIRE(xs.count() == 0);
}
}
}
}
}
}