[FEAT] Add a test for Cube

This commit is contained in:
NADAL Jean-Baptiste
2024-03-14 17:32:14 +01:00
parent 26282306a3
commit 03c825bd3a
3 changed files with 80 additions and 11 deletions

View File

@@ -41,8 +41,19 @@ using namespace Raytracer;
Intersections Cube::local_intersect(const Ray &a_ray) Intersections Cube::local_intersect(const Ray &a_ray)
{ {
double the_tmin, the_max;
Intersections the_intersections; Intersections the_intersections;
const auto [the_xtmin, the_xtmax] = check_axis(a_ray.origin().x(), a_ray.direction().x());
const auto [the_ytmin, the_ytmax] = check_axis(a_ray.origin().y(), a_ray.direction().y());
const auto [the_ztmin, the_ztmax] = check_axis(a_ray.origin().z(), a_ray.direction().z());
the_tmin = std::max({the_xtmin, the_ytmin, the_ztmin});
the_max = std::min({the_xtmax, the_ytmax, the_ztmax});
the_intersections.add(Intersection(the_tmin, this));
the_intersections.add(Intersection(the_max, this));
return the_intersections; return the_intersections;
} }
@@ -52,3 +63,32 @@ Tuple Cube::local_normal_at(const Tuple &a_local_point) const
{ {
return Tuple::Vector(0, 1, 0); return Tuple::Vector(0, 1, 0);
} }
/* ------------------------------------------------------------------------- */
std::pair<double, double> Cube::check_axis(double an_origin, double a_direction) const
{
double the_tmin, the_tmax;
double the_tmin_numerator, the_tmax_numerator;
the_tmin_numerator = (-1 - an_origin);
the_tmax_numerator = (1 - an_origin);
if (std::abs(a_direction) >= kEpsilon)
{
the_tmin = the_tmin_numerator / a_direction;
the_tmax = the_tmax_numerator / a_direction;
}
else
{
the_tmin = the_tmin_numerator * std::numeric_limits<double>::infinity();
the_tmax = the_tmax_numerator * std::numeric_limits<double>::infinity();
}
if (the_tmin > the_tmax)
{
std::swap(the_tmin, the_tmax);
}
return {the_tmin, the_tmax};
}

View File

@@ -40,6 +40,9 @@ namespace Raytracer
Cube(void) = default; Cube(void) = default;
Intersections local_intersect(const Ray &a_ray) override; Intersections local_intersect(const Ray &a_ray) override;
Tuple local_normal_at(const Tuple &a_local_point) const override; Tuple local_normal_at(const Tuple &a_local_point) const override;
private:
std::pair<double, double> check_axis(double an_origin, double a_direction) const;
}; };
}; // namespace Raytracer }; // namespace Raytracer

View File

@@ -33,7 +33,18 @@ using namespace Raytracer;
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
SCENARIO("Reflectivity for the default material", "[features/cubes.feature]") class CubeTest
{
public:
Tuple origin;
Tuple direction;
double t1;
double t2;
};
/* ------------------------------------------------------------------------- */
SCENARIO("A Ray intersects a cube", "[features/cubes.feature]")
{ {
// | | origin | direction | t1 | t2 | // | | origin | direction | t1 | t2 |
// | +x | point(5, 0.5, 0) | vector(-1, 0, 0) | 4 | 6 | // | +x | point(5, 0.5, 0) | vector(-1, 0, 0) | 4 | 6 |
@@ -43,6 +54,16 @@ SCENARIO("Reflectivity for the default material", "[features/cubes.feature]")
// | +z | point(0.5, 0, 5) | vector( 0, 0,-1) | 4 | 6 | // | +z | point(0.5, 0, 5) | vector( 0, 0,-1) | 4 | 6 |
// | -z | point(0.5, 0, -5) | vector( 0, 0, 1) | 4 | 6 | // | -z | point(0.5, 0, -5) | vector( 0, 0, 1) | 4 | 6 |
// | inside | point(0, 0.5, 0) | vector( 0, 0, 1) | -1 | -1 | // | inside | point(0, 0.5, 0) | vector( 0, 0, 1) | -1 | -1 |
CubeTest the_test[7] = {
{ Tuple::Point(5.0, 0.5, 0.0), Tuple::Vector(-1, 0, 0), 4, 6},
{Tuple::Point(-5.0, 0.5, 0.0), Tuple::Vector(1, 0, 0), 4, 6},
{ Tuple::Point(0.5, 5.0, 0.0), Tuple::Vector(0, -1, 0), 4, 6},
{ Tuple::Point(0.5, -5.0, 0.0), Tuple::Vector(0, 1, 0), 4, 6},
{ Tuple::Point(0.5, 0.0, 5.0), Tuple::Vector(0, 0, -1), 4, 6},
{ Tuple::Point(0.5, 0.0, -5.0), Tuple::Vector(0, 0, 1), 4, 6},
{ Tuple::Point(0.0, 0.5, 0.0), Tuple::Vector(0, 0, 1), -1, -1}
};
GIVEN("c <- cube()") GIVEN("c <- cube()")
{ {
Cube c; Cube c;
@@ -50,17 +71,22 @@ SCENARIO("Reflectivity for the default material", "[features/cubes.feature]")
{ {
WHEN("xs <- local_intersect(c,r)") WHEN("xs <- local_intersect(c,r)")
{ {
THEN("xs.count = 2") for (int i = 0; i < 7; i++)
{ {
// REQUIRE(); Ray r(the_test[i].origin, the_test[i].direction);
} Intersections xs = c.local_intersect(r);
AND_THEN("xs[0].t = <t1>") THEN("xs.count = 2")
{ {
// REQUIRE(); REQUIRE(xs.count() == 2);
} }
AND_THEN("xs[1].t = <t2>") AND_THEN("xs[0].t = <t1>")
{ {
// REQUIRE(); REQUIRE(xs[0].distance_t() == the_test[i].t1);
}
AND_THEN("xs[1].t = <t2>")
{
REQUIRE(xs[1].distance_t() == the_test[i].t2);
}
} }
} }
} }