-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhittable_list.h
43 lines (40 loc) · 1.62 KB
/
hittable_list.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef HITTABLE_LIST_H
#define HITTABLE_LIST_H
#include <memory>
#include <vector>
#include "hittable.h"
struct hittable_list : public hittable {
hittable_list() {}
hittable_list(std::shared_ptr<hittable> object) {add(object);}
std::vector<std::shared_ptr<hittable>> objects;
void clear() {objects.clear();}
void add(std::shared_ptr<hittable> object) {objects.push_back(object);}
bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const override {
hit_record temp_rec; bool hit_anything = false; auto closest_so_far = t_max;
for (const auto& object : objects)
if (object->hit(r, t_min, closest_so_far, temp_rec)) {
hit_anything = true; closest_so_far = temp_rec.t; rec = temp_rec;
}
return hit_anything;
}
bool bounding_box(double time0, double time1, aabb& output_box) const {
if (objects.empty()) return false;
aabb temp_box; bool first_box = true;
for (const auto& object : objects) {
if (!object->bounding_box(time0, time1, temp_box)) return false;
output_box = first_box ? temp_box : surrounding_box(output_box, temp_box);
first_box = false;
}
return true;
}
double pdf_value(const point3& o, const vec3& v) const {
auto weight = 1.0/objects.size(), sum = 0.0;
for (const auto& object : objects) sum += weight * object->pdf_value(o, v);
return sum;
}
vec3 random(const vec3& o) const {
auto int_size = static_cast<int>(objects.size());
return objects[random_int(0, int_size - 1)]->random(o);
}
};
#endif