Skip to content

Commit 94fa208

Browse files
authored
Merge pull request #860 from RayTracing/bounding-box
Bounding box
2 parents 74904c4 + 8bbfd77 commit 94fa208

21 files changed

+287
-381
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Change Log -- Ray Tracing in One Weekend
2020
- Fix: CSS fix for cases where code listing overflow; change to fit content (#826)
2121
- Change: `hittable` member variable `ptr` renamed to `object`
2222
- Change: general rename of `mat_ptr` to `mat` (material)
23+
- Change: hittable::bounding_box() signature has changed to always return a value (#859)
2324

2425
### In One Weekend
2526
- Added: More commentary about the choice between `double` and `float` (#752)

books/RayTracingInOneWeekend.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,8 @@
12971297
return min <= x && x <= max;
12981298
}
12991299

1300+
static const interval empty, universe;
1301+
13001302
public:
13011303
double min, max;
13021304
};
@@ -1347,7 +1349,7 @@
13471349
return hit_anything;
13481350
}
13491351
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1350-
[Listing [hittable-list-with-interval]: <kbd>[hittable.h]</kbd>
1352+
[Listing [hittable-list-with-interval]: <kbd>[hittable_list.h]</kbd>
13511353
hittable_list::hit() using interval]
13521354

13531355
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++

books/RayTracingTheNextWeek.html

Lines changed: 147 additions & 154 deletions
Large diffs are not rendered by default.

books/RayTracingTheRestOfYourLife.html

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -809,15 +809,10 @@
809809
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
810810
double f(double d)
811811
{
812-
if(d <= 0.5)
813-
{
812+
if (d <= 0.5)
814813
return sqrt(2.0) * random_double();
815-
}
816814
else
817-
{
818815
return sqrt(2.0) + (2 - sqrt(2.0)) * random_double();
819-
}
820-
821816
}
822817
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
823818
[Listing [crude-approx]: A crude, first-order approximation to nonuniform PDF]
@@ -1513,6 +1508,7 @@
15131508
return color(0,0,0);
15141509
}
15151510

1511+
15161512
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
15171513
virtual bool scatter(
15181514
const ray& r_in, const hit_record& rec, color& albedo, ray& scattered, double& pdf
@@ -2352,16 +2348,11 @@
23522348
if (!object->hit(r, ray_t, rec))
23532349
return false;
23542350

2355-
if (!object->hit(r, ray_tmin, ray_tmax, rec))
2356-
return false;
2357-
23582351
rec.front_face = !rec.front_face;
23592352
return true;
23602353
}
23612354

2362-
bool bounding_box(aabb& output_box) const override {
2363-
return object->bounding_box(output_box);
2364-
}
2355+
aabb bounding_box() const override { return object->bounding_box(); }
23652356

23662357
public:
23672358
shared_ptr<hittable> object;
@@ -2566,7 +2557,7 @@
25662557
public:
25672558
virtual bool hit(const ray& r, interval ray_t, hit_record& rec) const = 0;
25682559

2569-
virtual bool bounding_box(aabb& output_box) const = 0;
2560+
virtual aabb bounding_box() const = 0;
25702561

25712562

25722563
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight

src/InOneWeekend/sphere.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
class sphere : public hittable {
2020
public:
2121
sphere() {}
22-
sphere(point3 ctr, double r, shared_ptr<material> m)
23-
: center(ctr), radius(r), mat(m) {};
22+
sphere(point3 ctr, double r, shared_ptr<material> m) : center(ctr), radius(r), mat(m) {};
2423

2524
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
2625
vec3 oc = r.origin() - center;

src/TheNextWeek/aarect.h

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ class xy_rect : public hittable {
2121
xy_rect() {}
2222

2323
xy_rect(double _x0, double _x1, double _y0, double _y1, double _k, shared_ptr<material> _m)
24-
: x0(_x0), x1(_x1), y0(_y0), y1(_y1), k(_k), mat(_m) {};
24+
: x0(_x0), x1(_x1), y0(_y0), y1(_y1), k(_k), mat(_m),
25+
bbox(point3(x0, y0, k-0.0001), point3(x1, y1, k+0.0001))
26+
{}
2527

2628
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
2729
auto t = (k-r.origin().z()) / r.direction().z();
@@ -44,16 +46,12 @@ class xy_rect : public hittable {
4446
return true;
4547
}
4648

47-
bool bounding_box(aabb& output_box) const override {
48-
// The bounding box must have non-zero width in each dimension, so pad the Z
49-
// dimension a small amount.
50-
output_box = aabb(point3(x0, y0, k-0.0001), point3(x1, y1, k+0.0001));
51-
return true;
52-
}
49+
aabb bounding_box() const override { return bbox; }
5350

5451
public:
5552
shared_ptr<material> mat;
5653
double x0, x1, y0, y1, k;
54+
aabb bbox;
5755
};
5856

5957

@@ -62,7 +60,9 @@ class xz_rect : public hittable {
6260
xz_rect() {}
6361

6462
xz_rect(double _x0, double _x1, double _z0, double _z1, double _k, shared_ptr<material> _m)
65-
: x0(_x0), x1(_x1), z0(_z0), z1(_z1), k(_k), mat(_m) {};
63+
: x0(_x0), x1(_x1), z0(_z0), z1(_z1), k(_k), mat(_m),
64+
bbox(point3(x0, k-0.0001, z0), point3(x1, k+0.0001, z1))
65+
{}
6666

6767
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
6868
auto t = (k-r.origin().y()) / r.direction().y();
@@ -85,16 +85,12 @@ class xz_rect : public hittable {
8585
return true;
8686
}
8787

88-
bool bounding_box(aabb& output_box) const override {
89-
// The bounding box must have non-zero width in each dimension, so pad the Y
90-
// dimension a small amount.
91-
output_box = aabb(point3(x0, k-0.0001, z0), point3(x1, k+0.0001, z1));
92-
return true;
93-
}
88+
aabb bounding_box() const override { return bbox; }
9489

9590
public:
9691
shared_ptr<material> mat;
9792
double x0, x1, z0, z1, k;
93+
aabb bbox;
9894
};
9995

10096

@@ -103,7 +99,9 @@ class yz_rect : public hittable {
10399
yz_rect() {}
104100

105101
yz_rect(double _y0, double _y1, double _z0, double _z1, double _k, shared_ptr<material> _m)
106-
: y0(_y0), y1(_y1), z0(_z0), z1(_z1), k(_k), mat(_m) {};
102+
: y0(_y0), y1(_y1), z0(_z0), z1(_z1), k(_k), mat(_m),
103+
bbox(point3(k-0.0001, y0, z0), point3(k+0.0001, y1, z1))
104+
{}
107105

108106
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
109107
auto t = (k-r.origin().x()) / r.direction().x();
@@ -126,16 +124,12 @@ class yz_rect : public hittable {
126124
return true;
127125
}
128126

129-
bool bounding_box(aabb& output_box) const override {
130-
// The bounding box must have non-zero width in each dimension, so pad the X
131-
// dimension a small amount.
132-
output_box = aabb(point3(k-0.0001, y0, z0), point3(k+0.0001, y1, z1));
133-
return true;
134-
}
127+
aabb bounding_box() const override { return bbox; }
135128

136129
public:
137130
shared_ptr<material> mat;
138131
double y0, y1, z0, z1, k;
132+
aabb bbox;
139133
};
140134

141135

src/TheNextWeek/box.h

Lines changed: 6 additions & 8 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 "aarect.h"
1718
#include "hittable_list.h"
1819

@@ -21,10 +22,9 @@ class box : public hittable {
2122
public:
2223
box() {}
2324

24-
box(const point3& p0, const point3& p1, shared_ptr<material> mat) {
25-
box_min = p0;
26-
box_max = p1;
27-
25+
box(const point3& p0, const point3& p1, shared_ptr<material> mat)
26+
: box_min(p0), box_max(p1), bbox(p0, p1)
27+
{
2828
sides.add(make_shared<xy_rect>(p0.x(), p1.x(), p0.y(), p1.y(), p1.z(), mat));
2929
sides.add(make_shared<xy_rect>(p0.x(), p1.x(), p0.y(), p1.y(), p0.z(), mat));
3030

@@ -39,15 +39,13 @@ class box : public hittable {
3939
return sides.hit(r, ray_t, rec);
4040
}
4141

42-
bool bounding_box(aabb& output_box) const override {
43-
output_box = aabb(box_min, box_max);
44-
return true;
45-
}
42+
aabb bounding_box() const override { return bbox; }
4643

4744
public:
4845
point3 box_min;
4946
point3 box_max;
5047
hittable_list sides;
48+
aabb bbox;
5149
};
5250

5351

src/TheNextWeek/bvh.h

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,11 @@ class bvh_node : public hittable {
5353
right = make_shared<bvh_node>(objects, mid, end);
5454
}
5555

56-
aabb box_left, box_right;
57-
58-
if (!left->bounding_box(box_left) || !right->bounding_box(box_right))
59-
std::cerr << "No bounding box in bvh_node constructor.\n";
60-
61-
box = aabb(box_left, box_right);
56+
bbox = aabb(left->bounding_box(), right->bounding_box());
6257
}
6358

6459
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
65-
if (!box.hit(r, ray_t))
60+
if (!bbox.hit(r, ray_t))
6661
return false;
6762

6863
bool hit_left = left->hit(r, ray_t, rec);
@@ -71,27 +66,18 @@ class bvh_node : public hittable {
7166
return hit_left || hit_right;
7267
}
7368

74-
bool bounding_box(aabb& output_box) const override {
75-
output_box = box;
76-
return true;
77-
}
69+
aabb bounding_box() const override { return bbox; }
7870

7971
public:
8072
shared_ptr<hittable> left;
8173
shared_ptr<hittable> right;
82-
aabb box;
74+
aabb bbox;
8375

8476
private:
8577
static bool box_compare(
8678
const shared_ptr<hittable> a, const shared_ptr<hittable> b, int axis_index
8779
) {
88-
aabb box_a;
89-
aabb box_b;
90-
91-
if (!a->bounding_box(box_a) || !b->bounding_box(box_b))
92-
std::cerr << "No bounding box in bvh_node constructor.\n";
93-
94-
return box_a.axis(axis_index).min < box_b.axis(axis_index).min;
80+
return a->bounding_box().axis(axis_index).min < b->bounding_box().axis(axis_index).min;
9581
}
9682

9783
static bool box_x_compare (const shared_ptr<hittable> a, const shared_ptr<hittable> b) {

src/TheNextWeek/constant_medium.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class constant_medium : public hittable {
3535

3636
hit_record rec1, rec2;
3737

38-
if (!boundary->hit(r, universe, rec1))
38+
if (!boundary->hit(r, interval::universe, rec1))
3939
return false;
4040

4141
if (!boundary->hit(r, interval(rec1.t+0.0001, infinity), rec2))
@@ -75,9 +75,7 @@ class constant_medium : public hittable {
7575
return true;
7676
}
7777

78-
bool bounding_box(aabb& output_box) const override {
79-
return boundary->bounding_box(output_box);
80-
}
78+
aabb bounding_box() const override { return boundary->bounding_box(); }
8179

8280
public:
8381
shared_ptr<hittable> boundary;

src/TheNextWeek/hittable.h

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,17 @@ class hittable {
4040
public:
4141
virtual bool hit(const ray& r, interval ray_t, hit_record& rec) const = 0;
4242

43-
virtual bool bounding_box(aabb& output_box) const = 0;
43+
virtual aabb bounding_box() const = 0;
4444
};
4545

4646

4747
class translate : public hittable {
4848
public:
4949
translate(shared_ptr<hittable> p, const vec3& displacement)
50-
: object(p), offset(displacement) {}
50+
: object(p), offset(displacement)
51+
{
52+
bbox = object->bounding_box() + offset;
53+
}
5154

5255
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
5356
ray moved_r(r.origin() - offset, r.direction(), r.time());
@@ -60,17 +63,12 @@ class translate : public hittable {
6063
return true;
6164
}
6265

63-
bool bounding_box(aabb& output_box) const override {
64-
if (!object->bounding_box(output_box))
65-
return false;
66-
67-
output_box += offset;
68-
return true;
69-
}
66+
aabb bounding_box() const override { return bbox; }
7067

7168
public:
7269
shared_ptr<hittable> object;
7370
vec3 offset;
71+
aabb bbox;
7472
};
7573

7674

@@ -80,7 +78,7 @@ class rotate_y : public hittable {
8078
auto radians = degrees_to_radians(angle);
8179
sin_theta = sin(radians);
8280
cos_theta = cos(radians);
83-
hasbox = object->bounding_box(bbox);
81+
bbox = object->bounding_box();
8482

8583
point3 min( infinity, infinity, infinity);
8684
point3 max(-infinity, -infinity, -infinity);
@@ -138,16 +136,12 @@ class rotate_y : public hittable {
138136
return true;
139137
}
140138

141-
bool bounding_box(aabb& output_box) const override {
142-
output_box = bbox;
143-
return hasbox;
144-
}
139+
aabb bounding_box() const override { return bbox; }
145140

146141
public:
147142
shared_ptr<hittable> object;
148143
double sin_theta;
149144
double cos_theta;
150-
bool hasbox;
151145
aabb bbox;
152146
};
153147

0 commit comments

Comments
 (0)