[ADD] add group

This commit is contained in:
NADAL Jean-Baptiste
2024-03-22 19:09:55 +01:00
parent f05603cb29
commit 649ac3880d
6 changed files with 381 additions and 10 deletions

View File

@@ -64,3 +64,301 @@ SCENARIO("A shape has a parent attribute", "[features/shapes.feature]")
}
}
}
/* ------------------------------------------------------------------------- */
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));
}
}
}
}
}
}
}
}
}
}
}