From fd5753efebe26cae7bce5c9414425c1b134d755d Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Tue, 19 Mar 2024 23:06:30 +0100 Subject: [PATCH] [FEAT] cylinder is now ok --- apps/chapter_13.cpp | 7 +++++ raytracing/src/shapes/cylinder.cpp | 12 ++++++++ tests/13_cylinders.cpp | 49 ++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/apps/chapter_13.cpp b/apps/chapter_13.cpp index a2f8435..34ed29a 100644 --- a/apps/chapter_13.cpp +++ b/apps/chapter_13.cpp @@ -69,6 +69,7 @@ int main(void) the_cylinder = new Cylinder(); the_cylinder->set_minimum(0); the_cylinder->set_maximum(0.75); + the_cylinder->set_closed(true); the_cylinder->set_transform(Matrix::translation(-1, 0, 1) * Matrix::scaling(0.5, 1, 0.5)); Material &the_cylinder_material = the_cylinder->material(); @@ -125,6 +126,7 @@ int main(void) the_concentric_cylinder4 = new Cylinder(); the_concentric_cylinder4->set_minimum(0); the_concentric_cylinder4->set_maximum(0.5); + the_concentric_cylinder4->set_closed(true); the_concentric_cylinder4->set_transform(Matrix::translation(1, 0, 0) * Matrix::scaling(0.2, 1, 0.2)); Material &the_concentric_cylinder4_material = the_concentric_cylinder4->material(); @@ -139,6 +141,7 @@ int main(void) the_decorative_cylinder1 = new Cylinder(); the_decorative_cylinder1->set_minimum(0); the_decorative_cylinder1->set_maximum(0.3); + the_decorative_cylinder1->set_closed(true); the_decorative_cylinder1->set_transform(Matrix::translation(0, 0, -0.75) * Matrix::scaling(0.05, 1, 0.05)); Material &the_decorative_cylinder1_material = the_decorative_cylinder1->material(); @@ -153,6 +156,7 @@ int main(void) the_decorative_cylinder2 = new Cylinder(); the_decorative_cylinder2->set_minimum(0); the_decorative_cylinder2->set_maximum(0.3); + the_decorative_cylinder2->set_closed(true); the_decorative_cylinder2->set_transform(Matrix::translation(0, 0, -2.25) * Matrix::rotation_y(-0.15) * Matrix::translation(0, 0, 1.5) * Matrix::scaling(0.05, 1, 0.05)); Material &the_decorative_cylinder2_material = the_decorative_cylinder2->material(); @@ -167,6 +171,7 @@ int main(void) the_decorative_cylinder3 = new Cylinder(); the_decorative_cylinder3->set_minimum(0); the_decorative_cylinder3->set_maximum(0.3); + the_decorative_cylinder3->set_closed(true); the_decorative_cylinder3->set_transform(Matrix::translation(0, 0, -2.25) * Matrix::rotation_y(-0.3) * Matrix::translation(0, 0, 1.5) * Matrix::scaling(0.05, 1, 0.05)); Material &the_decorative_cylinder3_material = the_decorative_cylinder3->material(); @@ -181,6 +186,7 @@ int main(void) the_decorative_cylinder4 = new Cylinder(); the_decorative_cylinder4->set_minimum(0); the_decorative_cylinder4->set_maximum(0.3); + the_decorative_cylinder4->set_closed(true); the_decorative_cylinder4->set_transform(Matrix::translation(0, 0, -2.25) * Matrix::rotation_y(-0.45) * Matrix::translation(0, 0, 1.5) * Matrix::scaling(0.05, 1, 0.05)); Material &the_decorative_cylinder4_material = the_decorative_cylinder4->material(); @@ -195,6 +201,7 @@ int main(void) the_glass_cylinder = new Cylinder(); the_glass_cylinder->set_minimum(0.0001); the_glass_cylinder->set_maximum(0.5); + the_glass_cylinder->set_closed(true); the_glass_cylinder->set_transform(Matrix::translation(0, 0, -1.5) * Matrix::scaling(0.33, 1, 0.33)); Material &the_glass_cylinder_material = the_glass_cylinder->material(); diff --git a/raytracing/src/shapes/cylinder.cpp b/raytracing/src/shapes/cylinder.cpp index 5511bc1..aa3f540 100644 --- a/raytracing/src/shapes/cylinder.cpp +++ b/raytracing/src/shapes/cylinder.cpp @@ -95,6 +95,18 @@ Intersections Cylinder::local_intersect(const Ray &a_ray) Tuple Cylinder::local_normal_at(const Tuple &a_local_point) const { + double the_distance; + // Compute the sqare of the distance from the y axis + the_distance = std::pow(a_local_point.x(), 2) + std::pow(a_local_point.z(), 2); + if ((the_distance < 1) && (a_local_point.y() >= m_maximum - kEpsilon)) + { + return Tuple::Vector(0, 1, 0); + } + else if ((the_distance < 1) && (a_local_point.y() <= m_maximum + kEpsilon)) + { + return Tuple::Vector(0, -1, 0); + } + return Tuple::Vector(a_local_point.x(), 0, a_local_point.z()); } diff --git a/tests/13_cylinders.cpp b/tests/13_cylinders.cpp index 6d36627..b53e09d 100644 --- a/tests/13_cylinders.cpp +++ b/tests/13_cylinders.cpp @@ -313,3 +313,52 @@ SCENARIO("Intersecting the caps of a closed cylinder", "[features/cylinders.feat } } } + +/* ------------------------------------------------------------------------- */ + +SCENARIO("The normal vector on the cylinder's end caps", "[features/cylinders.feature]") +{ + // | point | normal | + // | point(0, 1, 0) | vector(0, -1, 0) | + // | point(0.5, 1, 0) | vector(0, -1, 0) | + // | point(0, 1, 0.5) | vector(0, -1, 0) | + // | point(0, 2, 0) | vector(0, 1, 0) | + // | point(0.5, 2, 0) | vector(0, 1, 0) | + // | point(0, 2, 0.5) | vector(0, 1, 0) | + CylinderTestNormal the_test[] = { + { Tuple::Point(0, 1, 0), Tuple::Vector(0, -1, 0)}, + {Tuple::Point(0.5, 1, 0), Tuple::Vector(0, -1, 0)}, + { Tuple::Point(0, 1, 0.5), Tuple::Vector(0, -1, 0)}, + { Tuple::Point(0, 2, 0), Tuple::Vector(0, 1, 0)}, + {Tuple::Point(0.5, 2, 0), Tuple::Vector(0, 1, 0)}, + { Tuple::Point(0, 2, 0.5), Tuple::Vector(0, 1, 0)} + }; + 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("cyl.closed <- true") + { + cyl.set_closed(true); + WHEN("n <- local_normal_at(cyl,)") + { + for (int i = 0; i < 6; i++) + { + Tuple p = the_test[i].point; + Tuple normal = cyl.local_normal_at(p); + THEN("n = ") + { + REQUIRE(normal == the_test[i].normal); + } + } + } + } + } + } + } +}