/*! * 13_cylinders.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: 18/03/2024 * */ /*---------------------------------------------------------------------------*/ #include #include "raytracing.h" using namespace Raytracer; /* ------------------------------------------------------------------------- */ class CylinderTestIntersect { public: Tuple origin; Tuple direction; double t0 = -1; double t1 = -1; }; /* ------------------------------------------------------------------------- */ SCENARIO("A Ray misses a cylinder", "[features/cylinders.feature]") { // | origin | direction | // | point(1, 0, 0) | vector(0, 1, 0) | // | point(0, 0, 0) | vector(0, 1, 0) | // | point(0, 0, -5) | vector(1, 1, 1) | CylinderTestIntersect the_test[] = { {Tuple::Point(1, 0, 0), Tuple::Vector(0, 1, 0)}, {Tuple::Point(0, 0, 0), Tuple::Vector(0, 1, 0)}, {Tuple::Point(0, 0, -5), Tuple::Vector(1, 1, 1)} }; GIVEN("cyl <- cylinder()") { Cylinder cyl; AND_GIVEN("r <- ray(, )") { WHEN("xs <- local_intersect(cyl,r)") { for (int i = 0; i < 3; i++) { Ray r(the_test[i].origin, the_test[i].direction); Intersections xs = cyl.local_intersect(r); THEN("xs.count = 0") { REQUIRE(xs.count() == 0); } } } } } } /* ------------------------------------------------------------------------- */ SCENARIO("A Ray strikes a cylinder", "[features/cylinders.feature]") { // | origin | direction | t0 | t1 | // | point(1, 0, -5) | vector(0, 0, 1) | 5 | 5 | // | point(0, 0, -5) | vector(0, 0, 1) | 4 | 6 | // | point(0.5, 0, -5) | vector(0.1, 1, 1) | 6.80798 | 7.08872 | CylinderTestIntersect the_test[] = { { Tuple::Point(1, 0, -5), Tuple::Vector(0, 0, 1), 5, 5}, { Tuple::Point(0, 0, -5), Tuple::Vector(0, 0, 1), 4, 6}, {Tuple::Point(0.5, 0, -5), Tuple::Vector(0.1, 1, 1), 6.80798, 7.08872} }; GIVEN("cyl <- cylinder()") { Cylinder cyl; AND_GIVEN("direction <- normalize()") { AND_GIVEN("r <- ray(, direction)") { WHEN("xs <- local_intersect(cyl,r)") { for (int i = 0; i < 3; i++) { Tuple direction = the_test[i].direction.normalize(); Ray r(the_test[i].origin, direction); Intersections xs = cyl.local_intersect(r); THEN("xs.count = 2") { REQUIRE(xs.count() == 2); } AND_THEN("xs[0].t = ") { REQUIRE(xs[0].distance_t() == the_test[i].t0); } AND_THEN("xs[1].t = ") { REQUIRE(xs[1].distance_t() == the_test[i].t1); } } } } } } }