[WIP] Add Intersection

This commit is contained in:
NADAL Jean-Baptiste
2024-02-08 19:54:50 +01:00
parent f7a450e736
commit af1b3ed2be
8 changed files with 280 additions and 10 deletions

View File

@@ -7,13 +7,15 @@ project(raytracing)
add_library(raytracing add_library(raytracing
src/common.cpp
src/tuple.cpp
src/color.cpp
src/canvas.cpp src/canvas.cpp
src/common.cpp
src/color.cpp
src/intersection.cpp
src/matrix.cpp src/matrix.cpp
src/object.cpp
src/ray.cpp src/ray.cpp
src/sphere.cpp src/sphere.cpp
src/tuple.cpp
) )
target_include_directories(${PROJECT_NAME} target_include_directories(${PROJECT_NAME}

View File

@@ -27,6 +27,7 @@
#include "color.h" #include "color.h"
#include "common.h" #include "common.h"
#include "intersection.h"
#include "matrix.h" #include "matrix.h"
#include "ray.h" #include "ray.h"
#include "sphere.h" #include "sphere.h"

View File

@@ -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;
}

View File

@@ -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

70
raytracing/src/object.cpp Normal file
View File

@@ -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;
}

52
raytracing/src/object.h Normal file
View File

@@ -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 <cstdint>
/* ------------------------------------------------------------------------- */
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

View File

@@ -28,11 +28,13 @@
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
#include "object.h"
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
namespace Raytracer namespace Raytracer
{ {
class Sphere class Sphere : public Object
{ {
public: public:
Sphere(void); Sphere(void);

View File

@@ -37,7 +37,7 @@ using namespace Raytracer;
TEST_CASE("[05][Rays] Creating and querying a ray", "[Rays]") 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); Tuple direction = Tuple::Vector(4, 5, 6);
Ray r(origin, direction); 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)); Ray r(Tuple::Point(0, 0, -5), Tuple::Vector(0, 0, 1));
Sphere s; 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)); Ray r(Tuple::Point(0, 1, -5), Tuple::Vector(0, 0, 1));
Sphere s; 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)); Ray r(Tuple::Point(0, 2, -5), Tuple::Vector(0, 0, 1));
Sphere s; 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)); Ray r(Tuple::Point(0, 0, 0), Tuple::Vector(0, 0, 1));
Sphere s; 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)); Ray r(Tuple::Point(0, 0, 5), Tuple::Vector(0, 0, 1));
Sphere s; Sphere s;
@@ -119,3 +119,42 @@ TEST_CASE("[05][Rays] a sphere is behind a ray", "[Rays]")
REQUIRE(xs[0] == -6.0); REQUIRE(xs[0] == -6.0);
REQUIRE(xs[1] == -4.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