diff --git a/raytracing/CMakeLists.txt b/raytracing/CMakeLists.txt index 5862210..9abf8dd 100644 --- a/raytracing/CMakeLists.txt +++ b/raytracing/CMakeLists.txt @@ -7,13 +7,15 @@ project(raytracing) add_library(raytracing - src/common.cpp - src/tuple.cpp - src/color.cpp src/canvas.cpp + src/common.cpp + src/color.cpp + src/intersection.cpp src/matrix.cpp + src/object.cpp src/ray.cpp src/sphere.cpp + src/tuple.cpp ) target_include_directories(${PROJECT_NAME} diff --git a/raytracing/include/raytracing.h b/raytracing/include/raytracing.h index 03ee123..5054042 100644 --- a/raytracing/include/raytracing.h +++ b/raytracing/include/raytracing.h @@ -27,6 +27,7 @@ #include "color.h" #include "common.h" +#include "intersection.h" #include "matrix.h" #include "ray.h" #include "sphere.h" diff --git a/raytracing/src/intersection.cpp b/raytracing/src/intersection.cpp new file mode 100644 index 0000000..650cf0b --- /dev/null +++ b/raytracing/src/intersection.cpp @@ -0,0 +1,53 @@ +/*! + * intersection.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: 08/02/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 "intersection.h" + +using namespace Raytracer; + +/* ------------------------------------------------------------------------- */ + +Intersection::Intersection(double a_distance_t, Object an_object) : m_distance_t(a_distance_t), m_object(an_object) +{ +} + +/* ------------------------------------------------------------------------- */ + +double Intersection::distance_t(void) +{ + return m_distance_t; +} + +/* ------------------------------------------------------------------------- */ + +Object &Intersection::object(void) +{ + return m_object; +} diff --git a/raytracing/src/intersection.h b/raytracing/src/intersection.h new file mode 100644 index 0000000..435f39a --- /dev/null +++ b/raytracing/src/intersection.h @@ -0,0 +1,51 @@ +/*! + * intersection.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: 08/02/2024 + * + */ + +#ifndef _RAYTRACER_INTERSECTION_H +#define _RAYTRACER_INTERSECTION_H + +/* ------------------------------------------------------------------------- */ + +#include "object.h" + +/* ------------------------------------------------------------------------- */ + +namespace Raytracer +{ + class Intersection + { + public: + Intersection(double a_distance_t, Object an_object); + + double distance_t(void); + Object &object(void); + + private: + double m_distance_t; + Object m_object; + }; +}; // namespace Raytracer + +#endif // _RAYTRACER_INTERSECTION_H diff --git a/raytracing/src/object.cpp b/raytracing/src/object.cpp new file mode 100644 index 0000000..8ba8852 --- /dev/null +++ b/raytracing/src/object.cpp @@ -0,0 +1,70 @@ +/*! + * object.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: 08/02/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 "object.h" + +using namespace Raytracer; + +/* ------------------------------------------------------------------------- */ + +uint32_t Object::s_current_index = 0; + +/* ------------------------------------------------------------------------- */ + +Object::Object(void) : m_id(s_current_index++) +{ +} + +/* ------------------------------------------------------------------------- */ + +Object::Object(Object &a_copy) : m_id(a_copy.m_id) +{ +} + +/* ------------------------------------------------------------------------- */ + +const Object &Object::operator=(const Object &an_other) +{ + if (this == &an_other) + { + return *this; + } + + m_id = an_other.m_id; + + return *this; +} + +/* ------------------------------------------------------------------------- */ + +bool Object::operator==(const Object &an_object) const +{ + return m_id == an_object.m_id; +} diff --git a/raytracing/src/object.h b/raytracing/src/object.h new file mode 100644 index 0000000..4977fd1 --- /dev/null +++ b/raytracing/src/object.h @@ -0,0 +1,52 @@ +/*! + * intersection.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: 08/02/2024 + * + */ + +#ifndef _RAYTRACER_OBJECT_H +#define _RAYTRACER_OBJECT_H + +/* ------------------------------------------------------------------------- */ + +#include + +/* ------------------------------------------------------------------------- */ + +namespace Raytracer +{ + class Object + { + public: + Object(void); + Object(Object &a_copy); + + const Object &operator=(const Object &an_other); + bool operator==(const Object &an_object) const; + + private: + uint32_t m_id; + static uint32_t s_current_index; + }; +}; // namespace Raytracer + +#endif // _RAYTRACER_OBJECT_H diff --git a/raytracing/src/sphere.h b/raytracing/src/sphere.h index 27135c9..21eda1c 100644 --- a/raytracing/src/sphere.h +++ b/raytracing/src/sphere.h @@ -28,11 +28,13 @@ /* ------------------------------------------------------------------------- */ +#include "object.h" + /* ------------------------------------------------------------------------- */ namespace Raytracer { - class Sphere + class Sphere : public Object { public: Sphere(void); diff --git a/tests/05_rays.cpp b/tests/05_rays.cpp index 1f18c92..78f3790 100644 --- a/tests/05_rays.cpp +++ b/tests/05_rays.cpp @@ -37,7 +37,7 @@ using namespace Raytracer; TEST_CASE("[05][Rays] Creating and querying a ray", "[Rays]") { - Tuple origin = Tuple::Point(1, 2, 3); + Tuple origin = Tuple::Point(1, 2, 3); Tuple direction = Tuple::Vector(4, 5, 6); Ray r(origin, direction); @@ -59,7 +59,7 @@ TEST_CASE("[05][Rays] Computing a point from a distance", "[Rays]") /* ------------------------------------------------------------------------- */ -TEST_CASE("[05][Rays] a ray intersects a sphere at two points", "[Rays]") +TEST_CASE("[05][Rays] a ray intersects a sphere at two points", "[Sphere]") { Ray r(Tuple::Point(0, 0, -5), Tuple::Vector(0, 0, 1)); Sphere s; @@ -72,7 +72,7 @@ TEST_CASE("[05][Rays] a ray intersects a sphere at two points", "[Rays]") /* ------------------------------------------------------------------------- */ -TEST_CASE("[05][Rays] a ray intersects a sphere at a tangent", "[Rays]") +TEST_CASE("[05][Rays] a ray intersects a sphere at a tangent", "[Sphere]") { Ray r(Tuple::Point(0, 1, -5), Tuple::Vector(0, 0, 1)); Sphere s; @@ -85,7 +85,7 @@ TEST_CASE("[05][Rays] a ray intersects a sphere at a tangent", "[Rays]") /* ------------------------------------------------------------------------- */ -TEST_CASE("[05][Rays] a ray misses a sphere", "[Rays]") +TEST_CASE("[05][Rays] a ray misses a sphere", "[Sphere]") { Ray r(Tuple::Point(0, 2, -5), Tuple::Vector(0, 0, 1)); Sphere s; @@ -96,7 +96,7 @@ TEST_CASE("[05][Rays] a ray misses a sphere", "[Rays]") /* ------------------------------------------------------------------------- */ -TEST_CASE("[05][Rays] a originates inside a sphere", "[Rays]") +TEST_CASE("[05][Rays] a originates inside a sphere", "[Sphere]") { Ray r(Tuple::Point(0, 0, 0), Tuple::Vector(0, 0, 1)); Sphere s; @@ -109,7 +109,7 @@ TEST_CASE("[05][Rays] a originates inside a sphere", "[Rays]") /* ------------------------------------------------------------------------- */ -TEST_CASE("[05][Rays] a sphere is behind a ray", "[Rays]") +TEST_CASE("[05][Rays] a sphere is behind a ray", "[Sphere]") { Ray r(Tuple::Point(0, 0, 5), Tuple::Vector(0, 0, 1)); Sphere s; @@ -119,3 +119,42 @@ TEST_CASE("[05][Rays] a sphere is behind a ray", "[Rays]") REQUIRE(xs[0] == -6.0); REQUIRE(xs[1] == -4.0); } + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[05][Rays] Test Sphere Object", "[Sphere]") +{ + Sphere s1; + Sphere s2 = s1; + Sphere s3; + + REQUIRE(s1 == s2); + REQUIRE(s1 != s3); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[05][Rays] An intersection encapsulates t and object", "[Intersections]") +{ + Sphere s; + Intersection i(3.5, s); + + REQUIRE(i.distance_t() == 3.5); + REQUIRE(i.object() == s); +} + +#if 0 +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[05][Rays] Aggregating intersections", "[Intersections]") +{ + Sphere s; + Intersection i1(1, s); + Intersection i2(2, s); + Intersections xs = Intersections(i1, i2); + + REQUIRE(xs.count() == 2); + REQUIRE(xs[0].t() == 1); + REQUIRE(xs[1].t() == 2); +} +#endif \ No newline at end of file