248 lines
8.2 KiB
C++
248 lines
8.2 KiB
C++
/*!
|
|
* 13_cylinders.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: 18/03/2024
|
|
*
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#include <catch2/catch_test_macros.hpp>
|
|
|
|
#include "raytracing.h"
|
|
|
|
using namespace Raytracer;
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
class CylinderTestIntersect
|
|
{
|
|
public:
|
|
Tuple origin;
|
|
Tuple direction;
|
|
double t0 = -1;
|
|
double t1 = -1;
|
|
};
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
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 |
|
|
// | point(1, 0, 0) | vector(0, 1, 0) |
|
|
// | point(0, 0, 0) | vector(0, 1, 0) |
|
|
// | point(0, 0, -5) | vector(1, 1, 1) |
|
|
CylinderTestIntersect the_test[] = {
|
|
{Tuple::Point(1, 0, 0), Tuple::Vector(0, 1, 0)},
|
|
{Tuple::Point(0, 0, 0), Tuple::Vector(0, 1, 0)},
|
|
{Tuple::Point(0, 0, -5), Tuple::Vector(1, 1, 1)}
|
|
};
|
|
|
|
GIVEN("cyl <- cylinder()")
|
|
{
|
|
Cylinder cyl;
|
|
AND_GIVEN("r <- ray(<origin>, <direction>)")
|
|
{
|
|
WHEN("xs <- local_intersect(cyl,r)")
|
|
{
|
|
for (int i = 0; i < 3; i++)
|
|
{
|
|
Ray r(the_test[i].origin, the_test[i].direction);
|
|
Intersections xs = cyl.local_intersect(r);
|
|
THEN("xs.count = 0")
|
|
{
|
|
REQUIRE(xs.count() == 0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
SCENARIO("A Ray strikes a cylinder", "[features/cylinders.feature]")
|
|
{
|
|
// | origin | direction | t0 | t1 |
|
|
// | point(1, 0, -5) | vector(0, 0, 1) | 5 | 5 |
|
|
// | point(0, 0, -5) | vector(0, 0, 1) | 4 | 6 |
|
|
// | point(0.5, 0, -5) | vector(0.1, 1, 1) | 6.80798 | 7.08872 |
|
|
CylinderTestIntersect the_test[] = {
|
|
{ Tuple::Point(1, 0, -5), Tuple::Vector(0, 0, 1), 5, 5},
|
|
{ Tuple::Point(0, 0, -5), Tuple::Vector(0, 0, 1), 4, 6},
|
|
{Tuple::Point(0.5, 0, -5), Tuple::Vector(0.1, 1, 1), 6.80798, 7.08872}
|
|
};
|
|
GIVEN("cyl <- cylinder()")
|
|
{
|
|
Cylinder cyl;
|
|
AND_GIVEN("direction <- normalize(<direction>)")
|
|
{
|
|
AND_GIVEN("r <- ray(<origin>, direction)")
|
|
{
|
|
WHEN("xs <- local_intersect(cyl,r)")
|
|
{
|
|
for (int i = 0; i < 3; i++)
|
|
{
|
|
Tuple direction = the_test[i].direction.normalize();
|
|
Ray r(the_test[i].origin, direction);
|
|
Intersections xs = cyl.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("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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|