[FEAT] Add truncated cylinders

This commit is contained in:
NADAL Jean-Baptiste
2024-03-18 18:39:25 +01:00
parent e17ac42b4a
commit cff3d78450
7 changed files with 287 additions and 4 deletions

View File

@@ -44,6 +44,25 @@ public:
/* ------------------------------------------------------------------------- */
class CylinderTestNormal
{
public:
Tuple point;
Tuple normal;
};
/* ------------------------------------------------------------------------- */
class CylinderTestConstrained
{
public:
Tuple point;
Tuple direction;
uint16_t count;
};
/* ------------------------------------------------------------------------- */
SCENARIO("A Ray misses a cylinder", "[features/cylinders.feature]")
{
// | origin | direction |
@@ -122,3 +141,107 @@ SCENARIO("A Ray strikes a cylinder", "[features/cylinders.feature]")
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Normal vector on a cylinder", "[features/cylinders.feature]")
{
// | point | normal |
// | point(1, 0, 0) | vector(1, 0, 0) |
// | point(0, 5, -1) | vector(0, 0, -1) |
// | point(0, -2, 1) | vector(0, 0, 1) |
// | point(-1, 1, 0) | vector(-1, 0, 0) |
CylinderTestNormal the_test[] = {
{ Tuple::Point(1, 0, 0), Tuple::Vector(1, 0, 0)},
{ Tuple::Point(0, 5, -1), Tuple::Vector(0, 0, -1)},
{ Tuple::Point(0, -2, 1), Tuple::Vector(0, 0, 1)},
{Tuple::Point(-1, 1, 0), Tuple::Vector(-1, 0, 0)}
};
GIVEN("cyl <- cylinder()")
{
Cylinder cyl;
WHEN("n <- local_normal_at(cyl,<point>)")
{
for (int i = 0; i < 3; i++)
{
Tuple p = the_test[i].point;
Tuple normal = cyl.local_normal_at(p);
THEN("n = <normal>")
{
REQUIRE(normal == the_test[i].normal);
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("The default minimum and maximum for a cylinder", "[features/cylinders.feature]")
{
GIVEN("cyl <- cylinder()")
{
Cylinder cyl;
THEN("cym.minimum = -infinity")
{
REQUIRE(cyl.minimum() == std::numeric_limits<double>::infinity());
}
AND_THEN("cym.maximum = -infinity")
{
REQUIRE(cyl.maximum() == std::numeric_limits<double>::infinity());
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Intersecting a constrained cylinder", "[features/cylinders.feature]")
{
// | | point | direction | count |
// | 1 | point(0, 1.5, 0) | vector(0.1, 1, 0) | 0 |
// | 2 | point(0, 3, -5) | vector(0, 0, 1) | 0 |
// | 3 | point(0, 0, -5) | vector(0, 0, 1) | 0 |
// | 4 | point(0, 2, -5) | vector(0, 0, 1) | 0 |
// | 5 | point(0, 1, -5) | vector(0, 0, 1) | 0 |
// | 6 | point(0, 1.5, -2) | vector(0, 0, 1) | 2 |
CylinderTestConstrained the_test[] = {
{Tuple::Point(0, 1.5, 0), Tuple::Vector(0.1, 1, 0), 0},
{Tuple::Point(0, 3, -5), Tuple::Vector(0, 0, 1), 0},
{Tuple::Point(0, 0, -5), Tuple::Vector(0, 0, 1), 0},
{Tuple::Point(0, 2, -5), Tuple::Vector(0, 0, 1), 0},
{Tuple::Point(0, 1, -5), Tuple::Vector(0, 0, 1), 0},
{Tuple::Point(0, 1.5, -20), Tuple::Vector(0, 0, 1), 2}
};
GIVEN("cyl <- cylinder()")
{
Cylinder cyl;
AND_GIVEN("cyl.minimum <- 1")
{
cyl.set_minimum(1);
AND_GIVEN("cyl.maximum <- 2")
{
cyl.set_maximum(2);
AND_GIVEN("direction <- normalize(<direction>)")
{
AND_GIVEN("r <- ray(<point>, direction)")
{
WHEN("xs <- local_intersect(cyl,r)")
{
for (int i = 0; i < 6; i++)
{
Tuple direction = the_test[i].direction.normalize();
Ray r(the_test[i].point, direction);
Intersections xs = cyl.local_intersect(r);
THEN("xs.count = <count>")
{
REQUIRE(xs.count() == the_test[i].count);
}
}
}
}
}
}
}
}
}