Skip to content

Commit 56c2f8f

Browse files
committed
Move hittable_list method defs into class def
1 parent 58532aa commit 56c2f8f

File tree

6 files changed

+136
-167
lines changed

6 files changed

+136
-167
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,28 +1068,26 @@
10681068
void clear() { objects.clear(); }
10691069
void add(shared_ptr<hittable> object) { objects.push_back(object); }
10701070

1071-
bool hit(const ray& r, double ray_tmin, double ray_tmax, hit_record& rec) const override;
1071+
bool hit(const ray& r, double ray_tmin, double ray_tmax, hit_record& rec) const override {
1072+
hit_record temp_rec;
1073+
bool hit_anything = false;
1074+
auto closest_so_far = ray_tmax;
1075+
1076+
for (const auto& object : objects) {
1077+
if (object->hit(r, ray_tmin, closest_so_far, temp_rec)) {
1078+
hit_anything = true;
1079+
closest_so_far = temp_rec.t;
1080+
rec = temp_rec;
1081+
}
1082+
}
1083+
1084+
return hit_anything;
1085+
}
10721086

10731087
public:
10741088
std::vector<shared_ptr<hittable>> objects;
10751089
};
10761090

1077-
bool hittable_list::hit(const ray& r, double ray_tmin, double ray_tmax, hit_record& rec) const {
1078-
hit_record temp_rec;
1079-
bool hit_anything = false;
1080-
auto closest_so_far = ray_tmax;
1081-
1082-
for (const auto& object : objects) {
1083-
if (object->hit(r, ray_tmin, closest_so_far, temp_rec)) {
1084-
hit_anything = true;
1085-
closest_so_far = temp_rec.t;
1086-
rec = temp_rec;
1087-
}
1088-
}
1089-
1090-
return hit_anything;
1091-
}
1092-
10931091
#endif
10941092
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10951093
[Listing [hittable-list-initial]: <kbd>[hittable_list.h]</kbd> The hittable_list class]
@@ -1326,39 +1324,31 @@
13261324
class hittable_list : public hittable {
13271325
...
13281326
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1329-
bool hit(const ray& r, interval ray_t, hit_record& rec) const override;
1327+
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
13301328
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1331-
...
1332-
};
1333-
1334-
1335-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1336-
bool hittable_list::hit(const ray& r, interval ray_t, hit_record& rec) const {
1337-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1338-
hit_record temp_rec;
1339-
bool hit_anything = false;
1329+
hit_record temp_rec;
1330+
bool hit_anything = false;
13401331
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1341-
auto closest_so_far = ray_t.max;
1332+
auto closest_so_far = ray_t.max;
13421333
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
13431334

1344-
for (const auto& object : objects) {
1335+
for (const auto& object : objects) {
13451336
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1346-
if (object->hit(r, interval(ray_t.min, closest_so_far), temp_rec)) {
1337+
if (object->hit(r, interval(ray_t.min, closest_so_far), temp_rec)) {
13471338
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1348-
hit_anything = true;
1349-
closest_so_far = temp_rec.t;
1350-
rec = temp_rec;
1339+
hit_anything = true;
1340+
closest_so_far = temp_rec.t;
1341+
rec = temp_rec;
1342+
}
13511343
}
1352-
}
13531344

1354-
return hit_anything;
1355-
}
1345+
return hit_anything;
1346+
}
13561347
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13571348
[Listing [hittable-list-with-interval]: <kbd>[hittable.h]</kbd>
13581349
hittable_list::hit() using interval]
13591350

13601351

1361-
13621352
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
13631353
class sphere : public hittable {
13641354
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight

books/RayTracingTheNextWeek.html

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -781,32 +781,24 @@
781781
class hittable_list : public hittable {
782782
public:
783783
...
784-
bool hit(const ray& r, interval ray_t, hit_record& rec) const override;
785-
786-
787784
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
788-
bool bounding_box(double time_start, double time_end, aabb& output_box) const override;
789-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
790-
...
791-
};
785+
bool bounding_box(double time_start, double time_end, aabb& output_box) const override {
786+
if (objects.empty()) return false;
792787

793-
...
788+
aabb temp_box;
789+
bool first_box = true;
794790

795-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
796-
bool hittable_list::bounding_box(double time_start, double time_end, aabb& output_box) const {
797-
if (objects.empty()) return false;
798-
799-
aabb temp_box;
800-
bool first_box = true;
791+
for (const auto& object : objects) {
792+
if (!object->bounding_box(time_start, time_end, temp_box)) return false;
793+
output_box = first_box ? temp_box : surrounding_box(output_box, temp_box);
794+
first_box = false;
795+
}
801796

802-
for (const auto& object : objects) {
803-
if (!object->bounding_box(time_start, time_end, temp_box)) return false;
804-
output_box = first_box ? temp_box : surrounding_box(output_box, temp_box);
805-
first_box = false;
797+
return true;
806798
}
807-
808-
return true;
809-
}
799+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
800+
...
801+
};
810802
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
811803
[Listing [hit-list-bbox]: <kbd>[hittable_list.h]</kbd> Hittable list with bounding box]
812804
</div>

books/RayTracingTheRestOfYourLife.html

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2489,20 +2489,26 @@
24892489
think both tactics would work fine, but I will go with instrumenting `hittable_list`.
24902490

24912491
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2492-
double hittable_list::pdf_value(const point3& o, const vec3& v) const {
2493-
auto weight = 1.0/objects.size();
2494-
auto sum = 0.0;
2492+
class hittable_list : public hittable {
2493+
public:
2494+
...
2495+
double pdf_value(const point3& o, const vec3& v) const override {
2496+
auto weight = 1.0/objects.size();
2497+
auto sum = 0.0;
24952498

2496-
for (const auto& object : objects)
2497-
sum += weight * object->pdf_value(o, v);
2499+
for (const auto& object : objects)
2500+
sum += weight * object->pdf_value(o, v);
24982501

2499-
return sum;
2500-
}
2502+
return sum;
2503+
}
25012504

2502-
vec3 hittable_list::random(const vec3& o) const {
2503-
auto int_size = static_cast<int>(objects.size());
2504-
return objects[random_int(0, int_size-1)]->random(o);
2505-
}
2505+
vec3 random(const vec3& o) const override {
2506+
auto int_size = static_cast<int>(objects.size());
2507+
return objects[random_int(0, int_size-1)]->random(o);
2508+
}
2509+
2510+
...
2511+
};
25062512
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25072513
[Listing [density-mixture]: <kbd>[hittable_list.h]</kbd> Creating a mixture of densities]
25082514
</div>

src/InOneWeekend/hittable_list.h

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,25 @@ class hittable_list : public hittable {
2727
void clear() { objects.clear(); }
2828
void add(shared_ptr<hittable> object) { objects.push_back(object); }
2929

30-
bool hit(const ray& r, interval ray_t, hit_record& rec) const override;
30+
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
31+
hit_record temp_rec;
32+
auto hit_anything = false;
33+
auto closest_so_far = ray_t.max;
34+
35+
for (const auto& object : objects) {
36+
if (object->hit(r, interval(ray_t.min, closest_so_far), temp_rec)) {
37+
hit_anything = true;
38+
closest_so_far = temp_rec.t;
39+
rec = temp_rec;
40+
}
41+
}
42+
43+
return hit_anything;
44+
}
3145

3246
public:
3347
std::vector<shared_ptr<hittable>> objects;
3448
};
3549

3650

37-
bool hittable_list::hit(const ray& r, interval ray_t, hit_record& rec) const {
38-
hit_record temp_rec;
39-
auto hit_anything = false;
40-
auto closest_so_far = ray_t.max;
41-
42-
for (const auto& object : objects) {
43-
if (object->hit(r, interval(ray_t.min, closest_so_far), temp_rec)) {
44-
hit_anything = true;
45-
closest_so_far = temp_rec.t;
46-
rec = temp_rec;
47-
}
48-
}
49-
50-
return hit_anything;
51-
}
52-
53-
5451
#endif

src/TheNextWeek/hittable_list.h

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "rtweekend.h"
1515

16+
#include "aabb.h"
1617
#include "hittable.h"
1718

1819
#include <memory>
@@ -27,46 +28,40 @@ class hittable_list : public hittable {
2728
void clear() { objects.clear(); }
2829
void add(shared_ptr<hittable> object) { objects.push_back(object); }
2930

30-
bool hit(const ray& r, interval ray_t, hit_record& rec) const override;
31-
32-
bool bounding_box(double time_start, double time_end, aabb& output_box) const override;
33-
34-
public:
35-
std::vector<shared_ptr<hittable>> objects;
36-
};
37-
38-
39-
bool hittable_list::hit(const ray& r, interval ray_t, hit_record& rec) const {
40-
hit_record temp_rec;
41-
auto hit_anything = false;
42-
auto closest_so_far = ray_t.max;
43-
44-
for (const auto& object : objects) {
45-
if (object->hit(r, interval(ray_t.min, closest_so_far), temp_rec)) {
46-
hit_anything = true;
47-
closest_so_far = temp_rec.t;
48-
rec = temp_rec;
31+
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
32+
hit_record temp_rec;
33+
auto hit_anything = false;
34+
auto closest_so_far = ray_t.max;
35+
36+
for (const auto& object : objects) {
37+
if (object->hit(r, interval(ray_t.min, closest_so_far), temp_rec)) {
38+
hit_anything = true;
39+
closest_so_far = temp_rec.t;
40+
rec = temp_rec;
41+
}
4942
}
50-
}
5143

52-
return hit_anything;
53-
}
44+
return hit_anything;
45+
}
5446

47+
bool bounding_box(double time_start, double time_end, aabb& output_box) const override {
48+
if (objects.empty()) return false;
5549

56-
bool hittable_list::bounding_box(double time_start, double time_end, aabb& output_box) const {
57-
if (objects.empty()) return false;
50+
aabb temp_box;
51+
bool first_box = true;
5852

59-
aabb temp_box;
60-
bool first_box = true;
53+
for (const auto& object : objects) {
54+
if (!object->bounding_box(time_start, time_end, temp_box)) return false;
55+
output_box = first_box ? temp_box : surrounding_box(output_box, temp_box);
56+
first_box = false;
57+
}
6158

62-
for (const auto& object : objects) {
63-
if (!object->bounding_box(time_start, time_end, temp_box)) return false;
64-
output_box = first_box ? temp_box : surrounding_box(output_box, temp_box);
65-
first_box = false;
59+
return true;
6660
}
6761

68-
return true;
69-
}
62+
public:
63+
std::vector<shared_ptr<hittable>> objects;
64+
};
7065

7166

7267
#endif

0 commit comments

Comments
 (0)