Files
raytracer_challenge/tests/14_groups.cpp
NADAL Jean-Baptiste 649ac3880d [ADD] add group
2024-03-22 19:09:55 +01:00

365 lines
13 KiB
C++

/*!
* 14_groups.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: 21/03/2024
*
*/
/*---------------------------------------------------------------------------*/
#include <catch2/catch_test_macros.hpp>
#include "raytracing.h"
#include "common_data.h"
using namespace Raytracer;
/* ------------------------------------------------------------------------- */
SCENARIO("Creating a new group", "[features/groups.feature]")
{
GIVEN("g <- group()")
{
Group g;
THEN("g.transform = identity_matrix")
{
REQUIRE(g.transform() == Matrix::identity());
}
AND_THEN("g is empty")
{
REQUIRE(g.is_empty() == true);
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("A shape has a parent attribute", "[features/shapes.feature]")
{
GIVEN("s <- test_shape()")
{
TestShape s;
THEN("s.parent is nothing")
{
REQUIRE(s.parent() == nullptr);
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Adding a child to a group", "[features/groups.feature]")
{
GIVEN("g <- group()")
{
Group g;
GIVEN("s <- test_shape()")
{
TestShape s;
WHEN("add_child(g, s)")
{
g.add_child(&s);
THEN("g is not empty")
{
REQUIRE(g.is_empty() == false);
}
AND_THEN("g includes s")
{
REQUIRE(g.contains(s) == true);
}
AND_THEN("s.parent = g")
{
REQUIRE(s.parent() == &g);
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Intersecting a ray with an empty group", "[features/groups.feature]")
{
GIVEN("g <- group()")
{
Group g;
AND_GIVEN("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(g, r)")
{
Intersections xs = g.local_intersect(r);
THEN("xs is empty")
{
REQUIRE(xs.count() == 0);
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Intersecting a ray with a nonempty group", "[features/groups.feature]")
{
GIVEN("g <- group()")
{
Group g;
AND_GIVEN("s1 <- sphere()")
{
Sphere s1;
AND_GIVEN("s2 <- sphere()")
{
Sphere s2;
AND_GIVEN("set_transform(s2, translation(0, 0, -3))")
{
s2.set_transform(Matrix::translation(0, 0, -3));
AND_GIVEN("s3 <- sphere()")
{
Sphere s3;
AND_GIVEN("set_transform(s3, translation(5, 0, 0))")
{
s3.set_transform(Matrix::translation(5, 0, 0));
AND_GIVEN("add_child(g, s1)")
{
g.add_child(&s1);
AND_GIVEN("add_child(g, s2)")
{
g.add_child(&s2);
AND_GIVEN("add_child(g, s3)")
{
g.add_child(&s3);
WHEN("ray(point(0, 0, -5), vector(0, 0, 1)")
{
Ray r(Tuple::Point(0, 0, -5), Tuple::Vector(0, 0, 1));
AND_WHEN("xs <-local_intersect(g, r)")
{
Intersections xs = g.local_intersect(r);
THEN("xs.count = 4")
{
REQUIRE(xs.count() == 4);
}
AND_THEN("xs[0].object = s2")
{
REQUIRE(*xs[0].object() == s2);
}
AND_THEN("xs[1].object = s2")
{
REQUIRE(*xs[1].object() == s2);
}
AND_THEN("xs[2].object = s1")
{
REQUIRE(*xs[2].object() == s1);
}
AND_THEN("xs[3].object = s1")
{
REQUIRE(*xs[3].object() == s1);
}
}
}
}
}
}
}
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Intersecting a transformed group", "[features/groups.feature]")
{
GIVEN("g <- group()")
{
Group g;
AND_GIVEN("set_transform(g, scaling(2, 2, 2))")
{
g.set_transform(Matrix::scaling(2, 2, 2));
AND_GIVEN("s <- sphere()")
{
Sphere s;
AND_GIVEN("set_transform(s, translation(5, 0, 0))")
{
s.set_transform(Matrix::translation(5, 0, 0));
AND_GIVEN("add_child(g, s)")
{
g.add_child(&s);
WHEN("ray(point(10, 0, -10), vector(0, 0, 1)")
{
Ray r(Tuple::Point(10, 0, -10), Tuple::Vector(0, 0, 1));
AND_WHEN("xs <-intersect(g, r)")
{
Intersections xs = g.intersect(r);
THEN("xs.count = 2")
{
REQUIRE(xs.count() == 2);
}
}
}
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Converting a point from world to object space", "[features/shapes.feature]")
{
GIVEN("g1 <- group()")
{
Group g1;
AND_GIVEN("set_transform(g1, rotation_y(pi/2))")
{
g1.set_transform(Matrix::rotation_y(std::numbers::pi / 2));
GIVEN("g2 <- group()")
{
Group g2;
AND_GIVEN("set_transform(g2, scaling(2, 2, 2))")
{
g2.set_transform(Matrix::scaling(2, 2, 2));
AND_GIVEN("add_child(g1, g2)")
{
g1.add_child(&g2);
AND_GIVEN("s <- sphere()")
{
Sphere s;
AND_GIVEN("set_transform(s, translation(5, 0, 0))")
{
s.set_transform(Matrix::translation(5, 0, 0));
AND_GIVEN("add_child(g2, s)")
{
g2.add_child(&s);
WHEN("p <- world_to_object(s, point(-2, 0, -10))")
{
Tuple p = s.world_to_object(Tuple::Point(-2, 0, -10));
THEN("p = point(0, 0, -1)")
{
REQUIRE(p == Tuple::Point(0, 0, -1));
}
}
}
}
}
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Converting a normal from object to world space", "[features/shapes.feature]")
{
GIVEN("g1 <- group()")
{
Group g1;
AND_GIVEN("set_transform(g1, rotation_y(pi/2))")
{
g1.set_transform(Matrix::rotation_y(std::numbers::pi / 2));
GIVEN("g2 <- group()")
{
Group g2;
AND_GIVEN("set_transform(g2, scaling(1, 2, 3))")
{
g2.set_transform(Matrix::scaling(1, 2, 3));
AND_GIVEN("add_child(g1, g2)")
{
g1.add_child(&g2);
AND_GIVEN("s <- sphere()")
{
Sphere s;
AND_GIVEN("set_transform(s, translation(5, 0, 0))")
{
s.set_transform(Matrix::translation(5, 0, 0));
AND_GIVEN("add_child(g2, s)")
{
g2.add_child(&s);
WHEN("n <- normal_to_world(s, vector(sqrt(3)/3, sqrt(3)/3, sqrt(3)/3))")
{
Tuple n = s.normal_to_world(Tuple::Vector(sqrt(3) / 3, sqrt(3) / 3, sqrt(3) / 3));
THEN("n = vector(0.2857, 0.4286, -0.8571)")
{
REQUIRE(n == Tuple::Vector(0.2857, 0.4286, -0.8571));
}
}
}
}
}
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Finding the normal on a child object", "[features/shapes.feature]")
{
GIVEN("g1 <- group()")
{
Group g1;
AND_GIVEN("set_transform(g1, rotation_y(pi/2))")
{
g1.set_transform(Matrix::rotation_y(std::numbers::pi / 2));
GIVEN("g2 <- group()")
{
Group g2;
AND_GIVEN("set_transform(g2, scaling(1, 2, 3))")
{
g2.set_transform(Matrix::scaling(1, 2, 3));
AND_GIVEN("add_child(g1, g2)")
{
g1.add_child(&g2);
AND_GIVEN("s <- sphere()")
{
Sphere s;
AND_GIVEN("set_transform(s, translation(5, 0, 0))")
{
s.set_transform(Matrix::translation(5, 0, 0));
AND_GIVEN("add_child(g2, s)")
{
g2.add_child(&s);
WHEN("n <- normal_at(s, point(1.7321, 1.1547, -5.5774))")
{
Tuple n = s.normal_at(Tuple::Point(1.7321, 1.1547, -5.5774));
THEN("n = vector(0.2857, 0.4286, -0.8571)")
{
REQUIRE(n == Tuple::Vector(0.2857, 0.4286, -0.8571));
}
}
}
}
}
}
}
}
}
}
}