[FEAT] Make refactoring of shape object

This commit is contained in:
NADAL Jean-Baptiste
2024-02-27 17:49:05 +01:00
parent 91b9453267
commit 429fa3f743
6 changed files with 282 additions and 11 deletions

246
tests/09_planes.cpp Normal file
View File

@@ -0,0 +1,246 @@
/*!
* 09_planes.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: 27/02/2024
*
*/
/*---------------------------------------------------------------------------*/
#include <catch.hpp>
#include "raytracing.h"
using namespace Raytracer;
/* ------------------------------------------------------------------------- */
class TestShape : public Shape
{
public:
TestShape(void) = default;
Intersections local_intersect(const Ray &a_ray) override
{
saved_ray = a_ray;
return Intersections();
}
Tuple local_normal_at(const Tuple &a_local_point) const override
{
return Tuple::Vector(a_local_point.x(), a_local_point.y(), a_local_point.z());
}
Ray saved_ray;
};
/* ------------------------------------------------------------------------- */
TestShape test_shape(void)
{
return TestShape();
}
/* ------------------------------------------------------------------------- */
SCENARIO("The default transformation", "[features/shapes.feature]")
{
GIVEN("s <- test_shape()")
{
TestShape s;
THEN("s.transform = identity_matrix")
{
REQUIRE(s.transform() == Matrix::identity());
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Assigning a transformation", "[features/shapes.feature]")
{
GIVEN("s <- test_shape()")
{
TestShape s;
WHEN("set_transform(s, translation(2, 3, 4))")
{
s.set_transform(Matrix::translation(2, 3, 4));
THEN("s.transform = translation(2, 3, 4)")
{
REQUIRE(s.transform() == Matrix::translation(2, 3, 4));
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("The default material (shape)", "[features/shapes.feature]")
{
GIVEN("s <- test_shape()")
{
TestShape s;
WHEN("m <- s.material()")
{
Material m = s.material();
THEN("m = material()")
{
REQUIRE(m == Material());
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Assigning a material", "[features/shapes.feature]")
{
GIVEN("s <- test_shape()")
{
TestShape s;
AND_GIVEN("m = material()")
{
Material m;
AND_GIVEN("m.ambient = 1")
{
m.set_ambient(1);
WHEN("s.material <- m")
{
s.set_material(m);
THEN("s.material = m")
{
REQUIRE(s.material() == m);
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Intersecting a scaled shape with a ray", "[features/shapes.feature]")
{
GIVEN("r <- ray(point(0, 0, -5), vector(0, 0, 1))")
{
Ray r(Tuple::Point(0, 0, -5), Tuple::Vector(0, 0, 1));
AND_GIVEN("s <- test_shape()")
{
TestShape *s = new TestShape;
WHEN("set_transform(s,scaling(2, 2, 2))")
{
s->set_transform(Matrix::scaling(2, 2, 2));
AND_WHEN("xs <- intersect(s,r)")
{
Intersections xs = s->intersect(r);
THEN("s.saved_ray.origin = point(0, 0, -2.5)")
{
REQUIRE(s->saved_ray.origin() == Tuple::Point(0, 0, -2.5));
}
AND_THEN("s.saved_ray.direction = vector(0, 0, 0.5)")
{
REQUIRE(s->saved_ray.direction() == Tuple::Vector(0, 0, 0.5));
}
}
}
delete s;
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Intersecting a translated shape with a ray", "[features/shapes.feature]")
{
GIVEN("r <- ray(point(0, 0, -5), vector(0, 0, 1))")
{
Ray r(Tuple::Point(0, 0, -5), Tuple::Vector(0, 0, 1));
AND_GIVEN("s <- test_shape()")
{
TestShape *s = new TestShape;
WHEN("set_transform(s,translation(5, 0, 0))")
{
s->set_transform(Matrix::translation(5, 0, 0));
AND_WHEN("xs <- intersect(s,r)")
{
Intersections xs = s->intersect(r);
THEN("s.saved_ray.origin = point(-5, 0, -5)")
{
REQUIRE(s->saved_ray.origin() == Tuple::Point(-5, 0, -5));
}
AND_THEN("s.saved_ray.direction = vector(0, 0, 1)")
{
REQUIRE(s->saved_ray.direction() == Tuple::Vector(0, 0, 1));
}
}
}
delete s;
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Computing the normal on a translated shape", "[features/shapes.feature]")
{
AND_GIVEN("s <- test_shape()")
{
TestShape s;
WHEN("set_transform(s,translation(0, 1, 0))")
{
s.set_transform(Matrix::translation(0, 1, 0));
AND_WHEN("n <- normal_at(s, point(0, 1.70711, -0.70711))")
{
Tuple n = s.normal_at(Tuple::Point(0, 1.70711, -0.70711));
THEN("n = vector(0, 0.70711, -0.70711)")
{
REQUIRE(n == Tuple::Vector(0, 0.70711, -0.70711));
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Computing the normal on a transformed shape", "[features/shapes.feature]")
{
AND_GIVEN("s <- test_shape()")
{
TestShape s;
AND_GIVEN("m <- scaling(1, 0.5, 1) * rotation_z(pi/5)")
{
Matrix m = Matrix::scaling(1, 0.5, 1) * Matrix::rotation_z(std::numbers::pi / 5);
WHEN("set_transform(m)")
{
s.set_transform(m);
AND_WHEN("n <- normal_at(s, point(0, sqrt(2)/2, -sqrt(2)/2))")
{
Tuple n = s.normal_at(Tuple::Point(0, sqrt(2) / 2, -sqrt(2) / 2));
THEN("n = vector(0, 0.97014, -0.24254)")
{
REQUIRE(n == Tuple::Vector(0, 0.97014, -0.24254));
}
}
}
}
}
}