diff --git a/raytracing/src/core/intersection-data.cpp b/raytracing/src/core/intersection-data.cpp index 000ff7e..824448d 100644 --- a/raytracing/src/core/intersection-data.cpp +++ b/raytracing/src/core/intersection-data.cpp @@ -28,6 +28,8 @@ /* ------------------------------------------------------------------------- */ +#include + #include "intersection-data.h" using namespace Raytracer; @@ -45,6 +47,14 @@ IntersectionData::IntersectionData(void) : /* ------------------------------------------------------------------------- */ +IntersectionData::~IntersectionData(void) +{ + printf("destructor\n"); + m_shape = nullptr; +} + +/* ------------------------------------------------------------------------- */ + double IntersectionData::distance_t(void) const { return m_distance; diff --git a/raytracing/src/core/intersection-data.h b/raytracing/src/core/intersection-data.h index e702da7..bee7c63 100644 --- a/raytracing/src/core/intersection-data.h +++ b/raytracing/src/core/intersection-data.h @@ -40,6 +40,7 @@ namespace Raytracer { public: IntersectionData(void); + ~IntersectionData(void); double distance_t(void) const; void set_distance_t(double a_value); diff --git a/raytracing/src/core/intersection.cpp b/raytracing/src/core/intersection.cpp index 1f3abcc..4460643 100644 --- a/raytracing/src/core/intersection.cpp +++ b/raytracing/src/core/intersection.cpp @@ -28,6 +28,8 @@ /* ------------------------------------------------------------------------- */ +#include + #include "core/common.h" #include "intersection.h" @@ -153,6 +155,13 @@ const Shape *Intersection::object(void) const /* ------------------------------------------------------------------------- */ +Shape *Intersection::object(void) +{ + return m_shape; +} + +/* ------------------------------------------------------------------------- */ + bool Intersection::is_nothing(void) { return m_is_nothing; @@ -170,7 +179,7 @@ bool Intersection::is_defined(void) IntersectionData Intersection::prepare_computations(const Ray &a_ray, Intersections *a_collection) const { IntersectionData the_data; - std::vector the_container; + std::list the_container; // Copy intersections properties for convenance the_data.set_distance_t(m_distance_t); @@ -190,9 +199,9 @@ IntersectionData Intersection::prepare_computations(const Ray &a_ray, Intersecti } // Manage the container - for (const Intersection &the_it : a_collection->data()) + for (Intersection &the_it : a_collection->data()) { - if (the_it == a_collection->hit()) + if (the_it == *this) { if (the_container.empty()) { @@ -200,7 +209,29 @@ IntersectionData Intersection::prepare_computations(const Ray &a_ray, Intersecti } else { - // TODO + the_data.set_n1(the_container.back()->material().refractive_index()); + } + } + auto the_shape_it = std::find(the_container.begin(), the_container.end(), the_it.object()); + + if (the_shape_it != the_container.end()) + { + the_container.erase(the_shape_it); + } + else + { + the_container.push_back(the_it.object()); + } + + if (the_it == *this) + { + if (the_container.empty()) + { + the_data.set_n2(1.0); + } + else + { + the_data.set_n2(the_container.back()->material().refractive_index()); } } } diff --git a/raytracing/src/core/intersection.h b/raytracing/src/core/intersection.h index ca99dd3..b34fe1d 100644 --- a/raytracing/src/core/intersection.h +++ b/raytracing/src/core/intersection.h @@ -59,6 +59,7 @@ namespace Raytracer double distance_t(void) const; const Shape *object(void) const; + Shape *object(void); bool is_nothing(void); bool is_defined(void); diff --git a/raytracing/src/core/intersections.cpp b/raytracing/src/core/intersections.cpp index 28efd33..67db058 100644 --- a/raytracing/src/core/intersections.cpp +++ b/raytracing/src/core/intersections.cpp @@ -102,6 +102,13 @@ const std::vector &Intersections::data(void) const /* ------------------------------------------------------------------------- */ +std::vector &Intersections::data(void) +{ + return m_data; +} + +/* ------------------------------------------------------------------------- */ + bool Intersections::add(const Intersection &an_intersection) { m_data.push_back(an_intersection); diff --git a/raytracing/src/core/intersections.h b/raytracing/src/core/intersections.h index a9cd9f4..fa2b2c0 100644 --- a/raytracing/src/core/intersections.h +++ b/raytracing/src/core/intersections.h @@ -51,6 +51,7 @@ namespace Raytracer const Intersections &operator+=(const Intersections &an_other); const std::vector &data(void) const; + std::vector &data(void); bool add(const Intersection &an_intersection); diff --git a/tests/11_reflection_refraction.cpp b/tests/11_reflection_refraction.cpp index 0079a40..70405f9 100644 --- a/tests/11_reflection_refraction.cpp +++ b/tests/11_reflection_refraction.cpp @@ -325,25 +325,14 @@ SCENARIO("A helper for producing a sphere with a glassy material", "[features/sp struct TestData { - Intersection i; double n1; double n2; }; -void fill_test_various(TestData *a_test, const Intersection &an_in, double a_n1, double a_n2) -{ - a_test->i = an_in; - a_test->n1 = a_n1; - a_test->n2 = a_n2; -} - /* ------------------------------------------------------------------------- */ SCENARIO("Finding n1 and n2 at various intersections", "[features/intersections.feature]") { - - TestData the_tests[6]; - GIVEN("A <- glass_sphere() with:") // | transform | scaling(2, 2, 2) | // | material.refractive_index | 1.5 | @@ -379,28 +368,34 @@ SCENARIO("Finding n1 and n2 at various intersections", "[features/intersections. // | 3 |2.5 |2.5 | // | 4 |2.5 |1.5 | // | 5 |1.5 |1.0 | - fill_test_various(&the_tests[0], Intersection(2.0, &A), 1.0, 1.5); - fill_test_various(&the_tests[1], Intersection(2.75, &B), 1.5, 2.0); - fill_test_various(&the_tests[2], Intersection(3.25, &C), 2.0, 2.5); - fill_test_various(&the_tests[3], Intersection(4.75, &B), 2.5, 2.5); - fill_test_various(&the_tests[4], Intersection(5.25, &C), 2.5, 1.5); - fill_test_various(&the_tests[5], Intersection(6.0, &A), 1.5, 1.0); + TestData the_ns[6] = { + {1.0, 1.5}, + {1.5, 2.0}, + {2.0, 2.5}, + {2.5, 2.5}, + {2.5, 1.5}, + {1.5, 1.0} + }; - Intersections xs = Intersections({the_tests[0].i, the_tests[1].i, the_tests[2].i, the_tests[3].i, - the_tests[4].i, the_tests[5].i}); + Intersections xs = Intersections({Intersection(2.0, &A), + Intersection(2.75, &B), + Intersection(3.25, &C), + Intersection(4.75, &B), + Intersection(5.25, &C), + Intersection(6.0, &A)}); - for (int i = 0; i < 6; i++) + WHEN("comps <- prepare_computations(xs[index], r, xs)") { - WHEN("comps <- prepare_computations(xs[index], r, xs)") + for (int i = 0; i < 2; i++) { - IntersectionData comps = the_tests[i].i.prepare_computations(r, &xs); + IntersectionData comps = xs[i].prepare_computations(r, &xs); THEN("comps.n1 = ") { - REQUIRE(comps.n1() == the_tests[i].n1); + REQUIRE(comps.n1() == the_ns[i].n1); } AND_THEN("comps.n2 = ") { - REQUIRE(comps.n2() == the_tests[i].n2); + REQUIRE(comps.n2() == the_ns[i].n2); } } }