/*! * 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 #include "raytracing.h" using namespace Raytracer; /* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */ TEST_CASE("[05][Rays] Creating and querying a ray", "[Rays]") { Tuple origin = Tuple::Point(1, 2, 3); Tuple direction = Tuple::Vector(4, 5, 6); Ray r(origin, direction); REQUIRE(r.origin() == origin); REQUIRE(r.direction() == direction); } /* ------------------------------------------------------------------------- */ TEST_CASE("[05][Rays] Computing a point from a distance", "[Rays]") { Ray r(Tuple::Point(2, 3, 4), Tuple::Vector(1, 0, 0)); REQUIRE(r.position(0) == Tuple::Point(2, 3, 4)); REQUIRE(r.position(1) == Tuple::Point(3, 3, 4)); REQUIRE(r.position(-1) == Tuple::Point(1, 3, 4)); REQUIRE(r.position(2.5) == Tuple::Point(4.5, 3, 4)); } /* ------------------------------------------------------------------------- */ TEST_CASE("[05][Rays] a ray intersects a sphere at two points", "[Sphere]") { Ray r(Tuple::Point(0, 0, -5), Tuple::Vector(0, 0, 1)); Sphere s; Intersections xs = r.intersect(s); REQUIRE(xs.count() == 2); REQUIRE(xs[0].distance_t() == 4.0); REQUIRE(xs[1].distance_t() == 6.0); } /* ------------------------------------------------------------------------- */ TEST_CASE("[05][Rays] a ray intersects a sphere at a tangent", "[Sphere]") { Ray r(Tuple::Point(0, 1, -5), Tuple::Vector(0, 0, 1)); Sphere s; Intersections xs = r.intersect(s); REQUIRE(xs.count() == 2); REQUIRE(xs[0].distance_t() == 5.0); REQUIRE(xs[1].distance_t() == 5.0); } /* ------------------------------------------------------------------------- */ TEST_CASE("[05][Rays] a ray misses a sphere", "[Sphere]") { Ray r(Tuple::Point(0, 2, -5), Tuple::Vector(0, 0, 1)); Sphere s; Intersections xs = r.intersect(s); REQUIRE(xs.count() == 0); } /* ------------------------------------------------------------------------- */ TEST_CASE("[05][Rays] a originates inside a sphere", "[Sphere]") { Ray r(Tuple::Point(0, 0, 0), Tuple::Vector(0, 0, 1)); Sphere s; Intersections xs = r.intersect(s); REQUIRE(xs.count() == 2); REQUIRE(xs[0].distance_t() == -1.0); REQUIRE(xs[1].distance_t() == 1.0); } /* ------------------------------------------------------------------------- */ TEST_CASE("[05][Rays] a sphere is behind a ray", "[Sphere]") { Ray r(Tuple::Point(0, 0, 5), Tuple::Vector(0, 0, 1)); Sphere s; Intersections xs = r.intersect(s); REQUIRE(xs.count() == 2); REQUIRE(xs[0].distance_t() == -6.0); REQUIRE(xs[1].distance_t() == -4.0); } /* ------------------------------------------------------------------------- */ TEST_CASE("[05][Rays] Test Sphere Object", "[Sphere]") { Sphere s1; Sphere s2 = s1; Sphere s3; REQUIRE(s1 == s2); REQUIRE(s1 != s3); } /* ------------------------------------------------------------------------- */ TEST_CASE("[05][Rays] An intersection encapsulates t and object", "[Intersections]") { Sphere s; Intersection i(3.5, s); REQUIRE(i.distance_t() == 3.5); REQUIRE(i.object() == s); } /* ------------------------------------------------------------------------- */ TEST_CASE("[05][Rays] Aggregating intersections", "[Intersections]") { Sphere s; Intersection i1(1, s); Intersection i2(2, s); Intersections xs = Intersections({i1, i2}); REQUIRE(xs.count() == 2); REQUIRE(xs[0].distance_t() == 1); REQUIRE(xs[1].distance_t() == 2); } /* ------------------------------------------------------------------------- */ TEST_CASE("[05][Rays] Intersect set the object on the intersection", "[Intersections]") { Ray r(Tuple::Point(0, 0, 5), Tuple::Vector(0, 0, 1)); Sphere s; Intersections xs = r.intersect(s); REQUIRE(xs.count() == 2); REQUIRE(xs[0].object() == s); REQUIRE(xs[1].object() == s); } /* ------------------------------------------------------------------------- */ TEST_CASE("[05][Rays] The hit, when all intersections have positive t", "[Intersections]") { Sphere s; Intersection i1(1, s); Intersection i2(2, s); Intersections xs = Intersections({i2, i1}); Intersection i = xs.hit(); REQUIRE(i == i1); } /* ------------------------------------------------------------------------- */ TEST_CASE("[05][Rays] The hit, when some intersections have negative t", "[Intersections]") { Sphere s; Intersection i1(-1, s); Intersection i2(1, s); Intersections xs = Intersections({i2, i1}); Intersection i = xs.hit(); REQUIRE(i == i2); } /* ------------------------------------------------------------------------- */ TEST_CASE("[05][Rays] The hit, when all intersections have negative t", "[Intersections]") { Sphere s; Intersection i1(-2, s); Intersection i2(-1, s); Intersections xs = Intersections({i1, i2}); Intersection i = xs.hit(); REQUIRE(i.object().is_nothing()); } /* ------------------------------------------------------------------------- */ TEST_CASE("[05][Rays] The hit is always the lowest nonnegative intersection", "[Intersections]") { Sphere s; Intersection i1(5, s); Intersection i2(7, s); Intersection i3(-3, s); Intersection i4(2, s); Intersections xs = Intersections({i1, i2, i3, i4}); Intersection i = xs.hit(); 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)); }