Skip to content

Commit 2be723d

Browse files
authored
Merge pull request #511 from RayTracing/aabb-offset
Add explanation for aarect padding
2 parents b06194f + 9fe20ee commit 2be723d

File tree

5 files changed

+29
-23
lines changed

5 files changed

+29
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ Change Log -- Ray Tracing in One Weekend
66

77
### Common
88
- Fix: Scattered improvements to the text.
9-
- New: subchapters throughout all three books (#267)
9+
- New: Subchapters throughout all three books (#267)
10+
- New: Add explanation for padding `aarect` in the zero dimension (#488)
1011
- Change: Minor change to use new `point3` and `color` type aliases for `vec3` (#422)
1112
- Change: Renamed `constant_texture` to `solid_color`, add RGB constructor (#452)
1213

books/RayTracingTheNextWeek.html

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,6 +1999,11 @@
19991999

20002000
It is a hit if $x_0 < x < x_1$ and $y_0 < y < y_1$.
20012001

2002+
Because our rectangles are axis-aligned, their bounding boxes will have an infinitely-thin side.
2003+
This can be a problem when dividing them up with our axis-aligned bounding volume hierarchy. To
2004+
counter this, all hittable objects should get a bounding box that has finite width along every
2005+
dimension. For our rectangles, we'll just pad the box a bit on the infnitely-thin side.
2006+
20022007
<div class='together'>
20032008
The actual `xy_rect` class is thus:
20042009

@@ -2013,7 +2018,9 @@
20132018
virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const;
20142019

20152020
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
2016-
output_box = aabb(point3(x0,y0, k-0.0001), point3(x1, y1, k+0.0001));
2021+
// The bounding box must have non-zero width in each dimension, so pad the Z
2022+
// dimension a small amount.
2023+
output_box = aabb(point3(x0,y0, k-0.0001), point3(x1, y1, k+0.0001));
20172024
return true;
20182025
}
20192026

@@ -2117,7 +2124,9 @@
21172124
virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const;
21182125

21192126
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
2120-
output_box = aabb(point3(x0,k-0.0001,z0), point3(x1, k+0.0001, z1));
2127+
// The bounding box must have non-zero width in each dimension, so pad the Y
2128+
// dimension a small amount.
2129+
output_box = aabb(point3(x0,k-0.0001,z0), point3(x1, k+0.0001, z1));
21212130
return true;
21222131
}
21232132

@@ -2136,7 +2145,9 @@
21362145
virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const;
21372146

21382147
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
2139-
output_box = aabb(point3(k-0.0001, y0, z0), point3(k+0.0001, y1, z1));
2148+
// The bounding box must have non-zero width in each dimension, so pad the X
2149+
// dimension a small amount.
2150+
output_box = aabb(point3(k-0.0001, y0, z0), point3(k+0.0001, y1, z1));
21402151
return true;
21412152
}
21422153

books/RayTracingTheRestOfYourLife.html

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,19 +1679,7 @@
16791679
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
16801680
class xz_rect: public hittable {
16811681
public:
1682-
xz_rect() {}
1683-
1684-
xz_rect(
1685-
double _x0, double _x1, double _z0, double _z1, double _k, shared_ptr<material> mat)
1686-
: x0(_x0), x1(_x1), z0(_z0), z1(_z1), k(_k), mp(mat) {};
1687-
1688-
virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const;
1689-
1690-
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
1691-
output_box = aabb(point3(x0,k-0.0001,z0), point3(x1, k+0.0001, z1));
1692-
return true;
1693-
}
1694-
1682+
...
16951683

16961684
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
16971685
virtual double pdf_value(const point3& origin, const vec3& v) const {
@@ -1710,12 +1698,6 @@
17101698
auto random_point = point3(random_double(x0,x1), k, random_double(z0,z1));
17111699
return random_point - origin;
17121700
}
1713-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1714-
1715-
public:
1716-
shared_ptr<material> mp;
1717-
double x0, x1, z0, z1, k;
1718-
};
17191701
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17201702
[Listing [xz-rect-pdf]: <kbd>[aarect.h]</kbd> XZ rect with pdf]
17211703
</div>

src/TheNextWeek/aarect.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class xy_rect: public hittable {
2727
virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const;
2828

2929
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
30+
// The bounding box must have non-zero width in each dimension, so pad the Z
31+
// dimension a small amount.
3032
output_box = aabb(point3(x0,y0, k-0.0001), point3(x1, y1, k+0.0001));
3133
return true;
3234
}
@@ -47,6 +49,8 @@ class xz_rect: public hittable {
4749
virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const;
4850

4951
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
52+
// The bounding box must have non-zero width in each dimension, so pad the Y
53+
// dimension a small amount.
5054
output_box = aabb(point3(x0,k-0.0001,z0), point3(x1, k+0.0001, z1));
5155
return true;
5256
}
@@ -67,6 +71,8 @@ class yz_rect: public hittable {
6771
virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const;
6872

6973
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
74+
// The bounding box must have non-zero width in each dimension, so pad the X
75+
// dimension a small amount.
7076
output_box = aabb(point3(k-0.0001, y0, z0), point3(k+0.0001, y1, z1));
7177
return true;
7278
}

src/TheRestOfYourLife/aarect.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class xy_rect: public hittable {
2727
virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const;
2828

2929
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
30+
// The bounding box must have non-zero width in each dimension, so pad the Z
31+
// dimension a small amount.
3032
output_box = aabb(point3(x0,y0, k-0.0001), point3(x1, y1, k+0.0001));
3133
return true;
3234
}
@@ -47,6 +49,8 @@ class xz_rect: public hittable {
4749
virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const;
4850

4951
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
52+
// The bounding box must have non-zero width in each dimension, so pad the Y
53+
// dimension a small amount.
5054
output_box = aabb(point3(x0,k-0.0001,z0), point3(x1, k+0.0001, z1));
5155
return true;
5256
}
@@ -84,6 +88,8 @@ class yz_rect: public hittable {
8488
virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const;
8589

8690
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
91+
// The bounding box must have non-zero width in each dimension, so pad the X
92+
// dimension a small amount.
8793
output_box = aabb(point3(k-0.0001, y0, z0), point3(k+0.0001, y1, z1));
8894
return true;
8995
}

0 commit comments

Comments
 (0)