From 5f296ba39af5db20d7f2495ff2e9fe5a17dbf403 Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Fri, 4 Oct 2024 21:07:10 +0200 Subject: [PATCH] feat: Iterate over objects to find first hit Closes #2 --- src/first_hit.cpp | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/first_hit.cpp b/src/first_hit.cpp index e7dd5ad..1858871 100644 --- a/src/first_hit.cpp +++ b/src/first_hit.cpp @@ -1,16 +1,30 @@ #include "first_hit.h" bool first_hit( - const Ray & ray, - const double min_t, - const std::vector< std::shared_ptr > & objects, - int & hit_id, - double & t, - Eigen::Vector3d & n) + const Ray & ray, + const double min_t, + const std::vector< std::shared_ptr > & objects, + int & hit_id, + double & t, + Eigen::Vector3d & n) { - //////////////////////////////////////////////////////////////////////////// - // Replace with your code here: - return false; - //////////////////////////////////////////////////////////////////////////// + bool hit = false; + t = -1; + + double t_temp; + Eigen::Vector3d n_temp; + + for (int i = 0; i < objects.size(); ++i) { + if (objects[i]->intersect(ray, min_t, t_temp, n_temp)) { + hit = true; + if (t == -1 || t_temp < t) { + hit_id = i; + t = t_temp; + n = n_temp; + } + } + } + + return hit; }