/*! * 09_planes.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: 27/02/2024 * */ /*---------------------------------------------------------------------------*/ #include #include "raytracing.h" using namespace Raytracer; /* ------------------------------------------------------------------------- */ class TestShape : public Shape { public: TestShape(void) = default; Intersections local_intersect(const Ray &a_ray) override { saved_ray = a_ray; return Intersections(); } Tuple local_normal_at(const Tuple &a_local_point) const override { return Tuple::Vector(a_local_point.x(), a_local_point.y(), a_local_point.z()); } Ray saved_ray; }; /* ------------------------------------------------------------------------- */ TestShape test_shape(void) { return TestShape(); } /* ------------------------------------------------------------------------- */ SCENARIO("The default transformation", "[features/shapes.feature]") { GIVEN("s <- test_shape()") { TestShape s; THEN("s.transform = identity_matrix") { REQUIRE(s.transform() == Matrix::identity()); } } } /* ------------------------------------------------------------------------- */ SCENARIO("Assigning a transformation", "[features/shapes.feature]") { GIVEN("s <- test_shape()") { TestShape s; WHEN("set_transform(s, translation(2, 3, 4))") { s.set_transform(Matrix::translation(2, 3, 4)); THEN("s.transform = translation(2, 3, 4)") { REQUIRE(s.transform() == Matrix::translation(2, 3, 4)); } } } } /* ------------------------------------------------------------------------- */ SCENARIO("The default material (shape)", "[features/shapes.feature]") { GIVEN("s <- test_shape()") { TestShape s; WHEN("m <- s.material()") { Material m = s.material(); THEN("m = material()") { REQUIRE(m == Material()); } } } } /* ------------------------------------------------------------------------- */ SCENARIO("Assigning a material", "[features/shapes.feature]") { GIVEN("s <- test_shape()") { TestShape s; AND_GIVEN("m = material()") { Material m; AND_GIVEN("m.ambient = 1") { m.set_ambient(1); WHEN("s.material <- m") { s.set_material(m); THEN("s.material = m") { REQUIRE(s.material() == m); } } } } } } /* ------------------------------------------------------------------------- */ SCENARIO("Intersecting a scaled shape with a ray", "[features/shapes.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 <- test_shape()") { TestShape *s = new TestShape; 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("s.saved_ray.origin = point(0, 0, -2.5)") { REQUIRE(s->saved_ray.origin() == Tuple::Point(0, 0, -2.5)); } AND_THEN("s.saved_ray.direction = vector(0, 0, 0.5)") { REQUIRE(s->saved_ray.direction() == Tuple::Vector(0, 0, 0.5)); } } } delete s; } } } /* ------------------------------------------------------------------------- */ SCENARIO("Intersecting a translated shape with a ray", "[features/shapes.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 <- test_shape()") { TestShape *s = new TestShape; WHEN("set_transform(s,translation(5, 0, 0))") { s->set_transform(Matrix::translation(5, 0, 0)); AND_WHEN("xs <- intersect(s,r)") { Intersections xs = s->intersect(r); THEN("s.saved_ray.origin = point(-5, 0, -5)") { REQUIRE(s->saved_ray.origin() == Tuple::Point(-5, 0, -5)); } AND_THEN("s.saved_ray.direction = vector(0, 0, 1)") { REQUIRE(s->saved_ray.direction() == Tuple::Vector(0, 0, 1)); } } } delete s; } } } /* ------------------------------------------------------------------------- */ SCENARIO("Computing the normal on a translated shape", "[features/shapes.feature]") { AND_GIVEN("s <- test_shape()") { TestShape s; WHEN("set_transform(s,translation(0, 1, 0))") { s.set_transform(Matrix::translation(0, 1, 0)); AND_WHEN("n <- normal_at(s, point(0, 1.70711, -0.70711))") { Tuple n = s.normal_at(Tuple::Point(0, 1.70711, -0.70711)); THEN("n = vector(0, 0.70711, -0.70711)") { REQUIRE(n == Tuple::Vector(0, 0.70711, -0.70711)); } } } } } /* ------------------------------------------------------------------------- */ SCENARIO("Computing the normal on a transformed shape", "[features/shapes.feature]") { AND_GIVEN("s <- test_shape()") { TestShape s; AND_GIVEN("m <- scaling(1, 0.5, 1) * rotation_z(pi/5)") { Matrix m = Matrix::scaling(1, 0.5, 1) * Matrix::rotation_z(std::numbers::pi / 5); WHEN("set_transform(m)") { s.set_transform(m); AND_WHEN("n <- normal_at(s, point(0, sqrt(2)/2, -sqrt(2)/2))") { Tuple n = s.normal_at(Tuple::Point(0, sqrt(2) / 2, -sqrt(2) / 2)); THEN("n = vector(0, 0.97014, -0.24254)") { REQUIRE(n == Tuple::Vector(0, 0.97014, -0.24254)); } } } } } } /* ------------------------------------------------------------------------- */ SCENARIO("The normal of a plane is constant everywhere", "[features/planes.feature]") { GIVEN("p <- plane()") { Plane p; WHEN("n1 <- local_normal_at(p, point(0, 0, 0)))") { Tuple n1 = p.local_normal_at(Tuple::Point(0, 0, 0)); AND_WHEN("n2 <- local_normal_at(p, point(10, 0, 0)))") { Tuple n2 = p.local_normal_at(Tuple::Point(10, 0, 0)); AND_WHEN("n3 <- local_normal_at(p, point(-5, 0, 150)))") { Tuple n3 = p.local_normal_at(Tuple::Point(-5, 0, 150)); THEN("n1 = vector(0, 1, 0)") { REQUIRE(n1 == Tuple::Vector(0, 1, 0)); } AND_THEN("n2 = vector(0, 1, 0)") { REQUIRE(n1 == Tuple::Vector(0, 1, 0)); } AND_THEN("n3 = vector(0, 1, 0)") { REQUIRE(n1 == Tuple::Vector(0, 1, 0)); } } } } } } /* ------------------------------------------------------------------------- */ SCENARIO("Intersect with a ray parallel to the plane", "[features/planes.feature]") { GIVEN("p <- plane()") { Plane p; AND_GIVEN("r <- ray(point(0, 10, 0), vector(0, 0, 1))") { Ray r(Tuple::Point(0, 10, 0), Tuple::Vector(0, 0, 1)); WHEN("xs <-local_intersect(p, r)") { Intersections xs = p.local_intersect(r); THEN("xs is empty") { REQUIRE(xs.count() == 0); } } } } } /* ------------------------------------------------------------------------- */ SCENARIO("Intersect with a coplanar ray", "[features/planes.feature]") { GIVEN("p <- plane()") { Plane p; AND_GIVEN("r <- ray(point(0, 0, 0), vector(0, 0, 1))") { Ray r(Tuple::Point(0, 0, 0), Tuple::Vector(0, 0, 1)); WHEN("xs <-local_intersect(p, r)") { Intersections xs = p.local_intersect(r); THEN("xs is empty") { REQUIRE(xs.count() == 0); } } } } } /* ------------------------------------------------------------------------- */ SCENARIO("A ray Intersecting a plane from above", "[features/planes.feature]") { GIVEN("p <- plane()") { Plane p; AND_GIVEN("r <- ray(point(0, 1, 0), vector(0, -1, 0))") { Ray r(Tuple::Point(0, 1, 0), Tuple::Vector(0, -1, 0)); WHEN("xs <-local_intersect(p, r)") { Intersections xs = p.local_intersect(r); THEN("xs.count = 1") { REQUIRE(xs.count() == 1); } AND_THEN("xs[0].t = 1") { REQUIRE(xs[0].distance_t() == 1); } AND_THEN("xs[0].object = p") { REQUIRE(*xs[0].object() == p); } } } } } /* ------------------------------------------------------------------------- */ SCENARIO("A ray Intersecting a plane from below", "[features/planes.feature]") { GIVEN("p <- plane()") { Plane p; AND_GIVEN("r <- ray(point(0, -1, 0), vector(0, 1, 0))") { Ray r(Tuple::Point(0, -1, 0), Tuple::Vector(0, 1, 0)); WHEN("xs <-local_intersect(p, r)") { Intersections xs = p.local_intersect(r); THEN("xs.count = 1") { REQUIRE(xs.count() == 1); } AND_THEN("xs[0].t = 1") { REQUIRE(xs[0].distance_t() == 1); } AND_THEN("xs[0].object = p") { REQUIRE(xs[0].object() == p); } } } } }