diff --git a/README.md b/README.md index eb03d70..fbdbe6a 100644 --- a/README.md +++ b/README.md @@ -33,4 +33,4 @@ The Web Site of the book: http://raytracerchallenge.com/ | Chapiter 12 | Chapiter 13 | Chapiter 14 | |:------------------------: | :---------------------------: | :----------------------------: | -|![12](data/chapter_12.png) | | | +|![12](data/chapter_12.png) | ![13](data/chapter_13.png) | | diff --git a/data/chapter_13.png b/data/chapter_13.png new file mode 100644 index 0000000..fafb5e4 Binary files /dev/null and b/data/chapter_13.png differ diff --git a/raytracing/CMakeLists.txt b/raytracing/CMakeLists.txt index 00e3d5b..8695024 100644 --- a/raytracing/CMakeLists.txt +++ b/raytracing/CMakeLists.txt @@ -34,6 +34,7 @@ add_library(raytracing src/renderer/ray.cpp src/renderer/world.cpp + src/shapes/cone.cpp src/shapes/cube.cpp src/shapes/cylinder.cpp src/shapes/plane.cpp diff --git a/raytracing/include/raytracing.h b/raytracing/include/raytracing.h index 957a28d..3aad06c 100644 --- a/raytracing/include/raytracing.h +++ b/raytracing/include/raytracing.h @@ -46,6 +46,7 @@ #include "renderer/ray.h" #include "renderer/world.h" +#include "shapes/cone.h" #include "shapes/cube.h" #include "shapes/cylinder.h" #include "shapes/plane.h" diff --git a/raytracing/src/shapes/cone.cpp b/raytracing/src/shapes/cone.cpp new file mode 100644 index 0000000..bec5de7 --- /dev/null +++ b/raytracing/src/shapes/cone.cpp @@ -0,0 +1,210 @@ +/*! + * cone.cpp + * + * Copyright (c) 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: 21/03/2024 + * + */ + +// This is an independent project of an individual developer. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com + +/* ------------------------------------------------------------------------- */ + +#include + +#include "core/common.h" +#include "core/intersections.h" + +#include "cone.h" + +using namespace Raytracer; + +/* ------------------------------------------------------------------------- */ + +Intersections Cone::local_intersect(const Ray &a_ray) +{ + double the_a, the_b, the_c, the_discriminant; + double the_t0, the_t1; + double the_y0, the_y1; + Intersections the_intersections; + const Tuple &the_ray_direction = a_ray.direction(); + const Tuple &the_ray_origin = a_ray.origin(); + + the_a = std::pow(the_ray_direction.x(), 2) - std::pow(the_ray_direction.y(), 2) + std::pow(the_ray_direction.z(), 2); + the_b = 2 * the_ray_origin.x() * the_ray_direction.x() - + 2 * the_ray_origin.y() * the_ray_direction.y() + + 2 * the_ray_origin.z() * the_ray_direction.z(); + the_c = std::pow(the_ray_origin.x(), 2) - std::pow(the_ray_origin.y(), 2) + std::pow(the_ray_origin.z(), 2); + + if (double_equal(the_a, 0) && !double_equal(the_b, 0)) + { + double the_t = -the_c / (2 * the_b); + the_intersections.add(Intersection(the_t, this)); + } + + // Ray is parallel to the y axis + if (double_equal(the_a, 0) == false) + { + the_discriminant = std::pow(the_b, 2) - 4 * the_a * the_c; + + if (the_discriminant < 0) + { + return the_intersections; + } + + the_t0 = (-the_b - std::sqrt(the_discriminant)) / (2 * the_a); + the_t1 = (-the_b + std::sqrt(the_discriminant)) / (2 * the_a); + if (the_t0 > the_t1) + { + std::swap(the_t0, the_t1); + } + + the_y0 = the_ray_origin.y() + the_t0 * the_ray_direction.y(); + if ((m_minimum < the_y0) && (the_y0 < m_maximum)) + { + the_intersections.add(Intersection(the_t0, this)); + } + + the_y1 = the_ray_origin.y() + the_t1 * the_ray_direction.y(); + if ((m_minimum < the_y1) && (the_y1 < m_maximum)) + { + the_intersections.add(Intersection(the_t1, this)); + } + } + + // Caps + intersect_caps(a_ray, the_intersections); + + return the_intersections; +} + +/* ------------------------------------------------------------------------- */ + +Tuple Cone::local_normal_at(const Tuple &a_local_point) const +{ + + double the_distance, the_y; + // Compute the square 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_minimum + kEpsilon)) + { + return Tuple::Vector(0, -1, 0); + } + + the_y = sqrt(the_distance); + if (a_local_point.y() > 0) + { + the_y = -the_y; + } + + return Tuple::Vector(a_local_point.x(), the_y, a_local_point.z()); +} + +/* ------------------------------------------------------------------------- */ + +double Cone::minimum(void) +{ + return m_minimum; +} + +/* ------------------------------------------------------------------------- */ + +void Cone::set_minimum(double a_value) +{ + m_minimum = a_value; +} + +/* ------------------------------------------------------------------------- */ + +double Cone::maximum(void) +{ + return m_maximum; +} + +/* ------------------------------------------------------------------------- */ + +void Cone::set_maximum(double a_value) +{ + m_maximum = a_value; +} + +/* ------------------------------------------------------------------------- */ + +bool Cone::closed(void) +{ + return m_closed; +} + +/* ------------------------------------------------------------------------- */ + +void Cone::set_closed(bool a_state) +{ + m_closed = a_state; +} + +/* ------------------------------------------------------------------------- */ + +bool Cone::check_cap(const Ray &a_ray, double a_distance_t, double an_y) +{ + double the_x, the_z; + + const Tuple &the_ray_direction = a_ray.direction(); + const Tuple &the_ray_origin = a_ray.origin(); + + the_x = the_ray_origin.x() + a_distance_t * the_ray_direction.x(); + the_z = the_ray_origin.z() + a_distance_t * the_ray_direction.z(); + + return (std::pow(the_x, 2) + std::pow(the_z, 2)) <= std::pow(an_y, 2); +} + +/* ------------------------------------------------------------------------- */ + +void Cone::intersect_caps(const Ray &a_ray, Intersections &an_xs) +{ + double the_distance_t; + const Tuple &the_ray_direction = a_ray.direction(); + + // Caps only matter if the cylinder is closed. and might possibility be intersected the ray. + if ((m_closed == false) or (double_equal(the_ray_direction.y(), 0))) + { + return; + } + + // Check for an intersection with the lower end cap by intersecting + // the ray with the plane at y = cyl.minimum + the_distance_t = (m_minimum - a_ray.origin().y()) / the_ray_direction.y(); + if (check_cap(a_ray, the_distance_t, m_minimum)) + { + an_xs.add(Intersection(the_distance_t, this)); + } + + // Check for an intersection with the upper end cap by intersecting + // the ray with the plane at y = cyl.maximum + the_distance_t = (m_maximum - a_ray.origin().y()) / the_ray_direction.y(); + if (check_cap(a_ray, the_distance_t, m_maximum)) + { + an_xs.add(Intersection(the_distance_t, this)); + } +} diff --git a/raytracing/src/shapes/cone.h b/raytracing/src/shapes/cone.h new file mode 100644 index 0000000..79df2b8 --- /dev/null +++ b/raytracing/src/shapes/cone.h @@ -0,0 +1,66 @@ +/*! + * cone.h + * + * Copyright (c) 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: 21/03/2024 + * + */ + +#ifndef _RAYTRACER_CONE_H +#define _RAYTRACER_CONE_H + +/* ------------------------------------------------------------------------- */ + +#include + +#include "shapes/shape.h" + +/* ------------------------------------------------------------------------- */ + +namespace Raytracer +{ + class Cone : public Shape + { + public: + Cone(void) = default; + Intersections local_intersect(const Ray &a_ray) override; + Tuple local_normal_at(const Tuple &a_local_point) const override; + + double minimum(void); + void set_minimum(double a_value); + + double maximum(void); + void set_maximum(double a_value); + + bool closed(void); + void set_closed(bool a_state); + + private: + bool check_cap(const Ray &a_ray, double a_distance_t, double an_y); + void intersect_caps(const Ray &a_ray, Intersections &an_xs); + + private: + double m_minimum = -std::numeric_limits::infinity(); + double m_maximum = std::numeric_limits::infinity(); + bool m_closed = false; + }; +}; // namespace Raytracer + +#endif // _RAYTRACER_CONE_H diff --git a/raytracing/src/shapes/cylinder.cpp b/raytracing/src/shapes/cylinder.cpp index f11dd5c..33df27f 100644 --- a/raytracing/src/shapes/cylinder.cpp +++ b/raytracing/src/shapes/cylinder.cpp @@ -173,16 +173,17 @@ bool Cylinder::check_cap(const Ray &a_ray, double a_distance_t) void Cylinder::intersect_caps(const Ray &a_ray, Intersections &an_xs) { double the_distance_t; + const Tuple &the_ray_direction = a_ray.direction(); // Caps only matter if the cylinder is closed. and might possibility be intersected the ray. - if ((m_closed == false) or (double_equal(a_ray.direction().y(), 0))) + if ((m_closed == false) or (double_equal(the_ray_direction.y(), 0))) { return; } // Check for an intersection with the lower end cap by intersecting // the ray with the plane at y = cyl.minimum - the_distance_t = (m_minimum - a_ray.origin().y()) / a_ray.direction().y(); + the_distance_t = (m_minimum - a_ray.origin().y()) / the_ray_direction.y(); if (check_cap(a_ray, the_distance_t)) { an_xs.add(Intersection(the_distance_t, this)); @@ -190,7 +191,7 @@ void Cylinder::intersect_caps(const Ray &a_ray, Intersections &an_xs) // Check for an intersection with the upper end cap by intersecting // the ray with the plane at y = cyl.maximum - the_distance_t = (m_maximum - a_ray.origin().y()) / a_ray.direction().y(); + the_distance_t = (m_maximum - a_ray.origin().y()) / the_ray_direction.y(); if (check_cap(a_ray, the_distance_t)) { an_xs.add(Intersection(the_distance_t, this)); diff --git a/tests/13_cylinders.cpp b/tests/13_cylinders.cpp index b53e09d..a82bfef 100644 --- a/tests/13_cylinders.cpp +++ b/tests/13_cylinders.cpp @@ -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,)") { - 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()") + { + AND_GIVEN("r <- ray(, 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 = ") + { + REQUIRE(xs[0].distance_t() == the_test[i].t0); + } + AND_THEN("xs[1].t = ") + { + 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()") + { + AND_GIVEN("r <- ray(, 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 = ") + { + 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, )") + { + for (int i = 0; i < 3; i++) + { + Tuple p = the_test[i].point; + Tuple normal = shape.local_normal_at(p); + THEN("n = ") + { + REQUIRE(normal == the_test[i].normal); + } + } + } + } +}