[FEAT] Add cone

This commit is contained in:
NADAL Jean-Baptiste
2024-03-21 19:10:01 +01:00
parent 9a8e764b56
commit b3490b146e
8 changed files with 453 additions and 5 deletions

View File

@@ -63,6 +63,16 @@ public:
/* ------------------------------------------------------------------------- */
class ConeTestConstrained
{
public:
Tuple origin;
Tuple direction;
uint16_t count;
};
/* ------------------------------------------------------------------------- */
SCENARIO("A Ray misses a cylinder", "[features/cylinders.feature]")
{
// | origin | direction |
@@ -163,7 +173,7 @@ SCENARIO("Normal vector on a cylinder", "[features/cylinders.feature]")
Cylinder cyl;
WHEN("n <- local_normal_at(cyl,<point>)")
{
for (int i = 0; i < 3; i++)
for (int i = 0; i < 4; i++)
{
Tuple p = the_test[i].point;
Tuple normal = cyl.local_normal_at(p);
@@ -362,3 +372,162 @@ SCENARIO("The normal vector on the cylinder's end caps", "[features/cylinders.fe
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Intersecting a cone with a ray", "[features/cones.feature]")
{
// | origin | direction | t0 | t1 |
// | point(0, 0, -5) | vector(0, 0, 1) | 5 | 5 |
// | point(0, 0, -5) | vector(1, 1, 1) | 8.66025 | 8.66025 |
// | point(1, 1, -5) | vector(-0.5, -1, 1) | 4.55006 | 49.44994 |
CylinderTestIntersect the_test[] = {
{Tuple::Point(0, 0, -5), Tuple::Vector(0, 0, 1), 5, 5},
{Tuple::Point(0, 0, -5), Tuple::Vector(1, 1, 1), 8.66025, 8.66025},
{Tuple::Point(1, 1, -5), Tuple::Vector(-0.5, -1, 1), 4.55006, 49.44994}
};
GIVEN("shape <- cone()")
{
Cone shape;
AND_GIVEN("direction <- normalize(<direction>)")
{
AND_GIVEN("r <- ray(<origin>, direction)")
{
WHEN("xs <- local_intersect(shape, r)")
{
for (int i = 0; i < 5; i++)
{
Tuple direction = the_test[i].direction.normalize();
Ray r(the_test[i].origin, direction);
Intersections xs = shape.local_intersect(r);
THEN("xs.count = 2")
{
REQUIRE(xs.count() == 2);
}
AND_THEN("xs[0].t = <t1>")
{
REQUIRE(xs[0].distance_t() == the_test[i].t0);
}
AND_THEN("xs[1].t = <t2>")
{
REQUIRE(xs[1].distance_t() == the_test[i].t1);
}
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Intersecting a cone with a ray parallel ton one of its halves", "[features/cones.feature]")
{
GIVEN("shape <- cone()")
{
Cone shape;
AND_GIVEN("direction <- normalize(vector(0, 1, 1))")
{
Tuple direction = Tuple::Vector(0, 1, 1).normalize();
AND_GIVEN("r <- ray(point(0, 0, -1), direction)")
{
Ray r(Tuple::Point(0, 0, -1), direction);
WHEN("xs <- local_intersect(shape, r)")
{
Intersections xs = shape.local_intersect(r);
THEN("xs.count = 1")
{
REQUIRE(xs.count() == 1);
}
AND_THEN("xs[0].t = 0.35355")
{
REQUIRE(double_equal(xs[0].distance_t(), 0.35355));
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Intersecting a cone's end caps", "[features/cones.feature]")
{
// | origin | direction | count |
// | point(0, 0, -5) | vector(0, 1, 0) | 0 |
// | point(0, 0, -0.25) | vector(0, 1, 1) | 2 |
// | point(0, 0, -0.25) | vector(0, 1, 0) | 4 |
ConeTestConstrained the_test[] = {
{Tuple::Point(0, 0, -5), Tuple::Vector(0, 1, 0), 0},
{Tuple::Point(0, 0, -0.25), Tuple::Vector(0, 1, 1), 2},
{Tuple::Point(0, 0, -0.25), Tuple::Vector(0, 1, 0), 4}
};
GIVEN("shape <- cone()")
{
Cone shape;
AND_GIVEN("shape.minimum <- 0.5")
{
shape.set_minimum(0.5);
AND_GIVEN("shape.maximum <- 0.5")
{
shape.set_maximum(0.5);
AND_GIVEN("shape.closed <- true")
{
shape.set_closed(true);
AND_GIVEN("direction <- normalize(<direction>)")
{
AND_GIVEN("r <- ray(<origin>, direction)")
{
WHEN("xs <- local_intersect(shape,r)")
{
for (int i = 0; i < 5; i++)
{
Tuple direction = the_test[i].direction.normalize();
Ray r(the_test[i].origin, direction);
Intersections xs = shape.local_intersect(r);
THEN("xs.count = <count>")
{
REQUIRE(xs.count() == the_test[i].count);
}
}
}
}
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Computing the normal vector on a cone", "[features/cones.feature]")
{
// | point | normal |
// | point(0, 0, 0) | vector(0, 0, 0) |
// | point(1, 1, 1) | vector(1, -sqrt(2), 1) |
// | point(-1, -1, 0) | vector(-1, 1, 0) |
CylinderTestNormal the_test[] = {
{ Tuple::Point(0, 0, 0), Tuple::Vector(0, 0, 0)},
{ Tuple::Point(1, 1, 1), Tuple::Vector(1, -sqrt(2), 1)},
{Tuple::Point(-1, -1, 0), Tuple::Vector(-1, 1, 0)}
};
GIVEN("shape <- cone()")
{
Cone shape;
WHEN("n <- local_normal_at(shape, <point>)")
{
for (int i = 0; i < 3; i++)
{
Tuple p = the_test[i].point;
Tuple normal = shape.local_normal_at(p);
THEN("n = <normal>")
{
REQUIRE(normal == the_test[i].normal);
}
}
}
}
}