From e17ac42b4ab035f23a19453300c2e0e4a9ec3c95 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Mon, 18 Mar 2024 16:35:12 +0100 Subject: [PATCH] [WIP] sart intergration of cylinders --- raytracing/CMakeLists.txt | 1 + raytracing/include/raytracing.h | 1 + raytracing/src/renderer/camera.cpp | 2 +- raytracing/src/shapes/cube.cpp | 10 ++-- raytracing/src/shapes/cylinder.cpp | 83 +++++++++++++++++++++++++++ raytracing/src/shapes/cylinder.h | 46 +++++++++++++++ tests/13_cylinders.cpp | 90 ++++++++++++++++++++++++++++++ 7 files changed, 228 insertions(+), 5 deletions(-) create mode 100644 raytracing/src/shapes/cylinder.cpp create mode 100644 raytracing/src/shapes/cylinder.h diff --git a/raytracing/CMakeLists.txt b/raytracing/CMakeLists.txt index 08a1c17..00e3d5b 100644 --- a/raytracing/CMakeLists.txt +++ b/raytracing/CMakeLists.txt @@ -35,6 +35,7 @@ add_library(raytracing src/renderer/world.cpp src/shapes/cube.cpp + src/shapes/cylinder.cpp src/shapes/plane.cpp src/shapes/shape.cpp src/shapes/sphere.cpp diff --git a/raytracing/include/raytracing.h b/raytracing/include/raytracing.h index 232ba82..957a28d 100644 --- a/raytracing/include/raytracing.h +++ b/raytracing/include/raytracing.h @@ -47,5 +47,6 @@ #include "renderer/world.h" #include "shapes/cube.h" +#include "shapes/cylinder.h" #include "shapes/plane.h" #include "shapes/sphere.h" \ No newline at end of file diff --git a/raytracing/src/renderer/camera.cpp b/raytracing/src/renderer/camera.cpp index 074dc85..b28a605 100644 --- a/raytracing/src/renderer/camera.cpp +++ b/raytracing/src/renderer/camera.cpp @@ -208,7 +208,7 @@ Canvas Camera::render(const World &a_world) { double the_temp_perc; the_cpt++; - the_temp_perc = the_cpt * 100 / the_loop_size; + the_temp_perc = (double)the_cpt * 100 / the_loop_size; if (the_percent != (int8_t)the_temp_perc) { the_percent = (int8_t)the_temp_perc; diff --git a/raytracing/src/shapes/cube.cpp b/raytracing/src/shapes/cube.cpp index 37b57a7..fde539a 100644 --- a/raytracing/src/shapes/cube.cpp +++ b/raytracing/src/shapes/cube.cpp @@ -1,5 +1,5 @@ /*! - * sphere.cpp + * cube.cpp * * Copyright (c) 2024, NADAL Jean-Baptiste. All rights reserved. * @@ -43,10 +43,12 @@ Intersections Cube::local_intersect(const Ray &a_ray) { double the_tmin, the_tmax; Intersections the_intersections; + const Tuple &the_ray_direction = a_ray.direction(); + const Tuple &the_ray_origin = a_ray.origin(); - 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()); + const auto [the_xtmin, the_xtmax] = check_axis(the_ray_origin.x(), the_ray_direction.x()); + const auto [the_ytmin, the_ytmax] = check_axis(the_ray_origin.y(), the_ray_direction.y()); + const auto [the_ztmin, the_ztmax] = check_axis(the_ray_origin.z(), the_ray_direction.z()); the_tmin = std::max({the_xtmin, the_ytmin, the_ztmin}); the_tmax = std::min({the_xtmax, the_ytmax, the_ztmax}); diff --git a/raytracing/src/shapes/cylinder.cpp b/raytracing/src/shapes/cylinder.cpp new file mode 100644 index 0000000..1c3db35 --- /dev/null +++ b/raytracing/src/shapes/cylinder.cpp @@ -0,0 +1,83 @@ +/*! + * cylinder.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: 18/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 "cylinder.h" + +using namespace Raytracer; + +/* ------------------------------------------------------------------------- */ + +Intersections Cylinder::local_intersect(const Ray &a_ray) +{ + double the_a, the_b, the_c, the_disc; + double the_t0, the_t1; + 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.z(), 2); + + // Ray is parallel to the y axis + if (double_equal(the_a, 0)) + { + return the_intersections; + } + + the_b = 2 * the_ray_origin.x() * the_ray_direction.x() + + 2 * the_ray_origin.z() * the_ray_direction.z(); + the_c = std::pow(the_ray_origin.x(), 2) + std::pow(the_ray_origin.z(), 2) - 1; + + the_disc = std::pow(the_b, 2) - 4 * the_a * the_c; + + if (the_disc < 0) + { + return the_intersections; + } + + the_t0 = (-the_b - std::sqrt(the_disc)) / (2 * the_a); + the_t1 = (-the_b + std::sqrt(the_disc)) / (2 * the_a); + + the_intersections.add(Intersection(the_t0, this)); + the_intersections.add(Intersection(the_t1, this)); + + return the_intersections; +} + +/* ------------------------------------------------------------------------- */ + +Tuple Cylinder::local_normal_at(const Tuple &a_local_point) const +{ + return Tuple::Vector(0, 0, 0); +} diff --git a/raytracing/src/shapes/cylinder.h b/raytracing/src/shapes/cylinder.h new file mode 100644 index 0000000..9efbce5 --- /dev/null +++ b/raytracing/src/shapes/cylinder.h @@ -0,0 +1,46 @@ +/*! + * cylinder.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: 18/03/2024 + * + */ + +#ifndef _RAYTRACER_CYLINDER_H +#define _RAYTRACER_CYLINDER_H + +/* ------------------------------------------------------------------------- */ + +#include "shapes/shape.h" + +/* ------------------------------------------------------------------------- */ + +namespace Raytracer +{ + class Cylinder : public Shape + { + public: + Cylinder(void) = default; + Intersections local_intersect(const Ray &a_ray) override; + Tuple local_normal_at(const Tuple &a_local_point) const override; + }; +}; // namespace Raytracer + +#endif // _RAYTRACER_CYLINDER_H diff --git a/tests/13_cylinders.cpp b/tests/13_cylinders.cpp index 610448b..9c52e87 100644 --- a/tests/13_cylinders.cpp +++ b/tests/13_cylinders.cpp @@ -32,3 +32,93 @@ using namespace Raytracer; /* ------------------------------------------------------------------------- */ + +class CylinderTestIntersect +{ +public: + Tuple origin; + Tuple direction; + double t0 = -1; + double t1 = -1; +}; + +/* ------------------------------------------------------------------------- */ + +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(, )") + { + 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()") + { + AND_GIVEN("r <- ray(, 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 = ") + { + REQUIRE(xs[0].distance_t() == the_test[i].t0); + } + AND_THEN("xs[1].t = ") + { + REQUIRE(xs[1].distance_t() == the_test[i].t1); + } + } + } + } + } + } +}