Skip to content

Commit f2becce

Browse files
authored
Merge pull request #786 from RayTracing/interval
Introduce interval class for hit() methods
2 parents fd33dca + d2dc5b9 commit f2becce

27 files changed

+308
-203
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Change Log -- Ray Tracing in One Weekend
44
# v4.0.0 (pending, targeted for 2020-12-25)
55

66
### Common
7+
- Change: Introduce new `interval` class used throughout codebase (#777)
8+
- Change: `hittable:hit()` methods use new interval class for ray t parameter
79

810
### In One Weekend
911

books/RayTracingInOneWeekend.html

Lines changed: 130 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,128 @@
12781278

12791279
</div>
12801280

1281+
An Interval Class
1282+
------------------
1283+
Before we continue, we'll implement an interval class to manage real-valued intervals with a minimum
1284+
and a maximum. We'll end up using this class quite often as we proceed.
1285+
1286+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1287+
#ifndef INTERVAL_H
1288+
#define INTERVAL_H
1289+
1290+
class interval {
1291+
public:
1292+
double min, max;
1293+
1294+
interval(double _min, double _max) : min(_min), max(_max) {}
1295+
interval() : min(+infinity), max(-infinity) {} // Default interval is empty
1296+
1297+
bool contains(double x) const {
1298+
return min <= x && x <= max;
1299+
}
1300+
};
1301+
1302+
const static interval empty (+infinity, -infinity);
1303+
const static interval universe(-infinity, +infinity);
1304+
1305+
1306+
#endif
1307+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1308+
[Listing [interval-initial]: <kbd>[interval.h]</kbd> Introducing the new interval class]
1309+
1310+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1311+
class hittable {
1312+
public:
1313+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1314+
virtual bool hit(const ray& r, interval ray_t, hit_record& rec) const = 0;
1315+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1316+
};
1317+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1318+
[Listing [hittable-with-interval]: <kbd>[hittable.h]</kbd> hittable::hit() using interval]
1319+
1320+
1321+
1322+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1323+
class hittable_list : public hittable {
1324+
...
1325+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1326+
virtual bool hit(const ray& r, interval ray_t, hit_record& rec) const override;
1327+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1328+
...
1329+
};
1330+
1331+
1332+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1333+
bool hittable_list::hit(const ray& r, interval ray_t, hit_record& rec) const {
1334+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1335+
hit_record temp_rec;
1336+
bool hit_anything = false;
1337+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1338+
auto closest_so_far = ray_t.max;
1339+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1340+
1341+
for (const auto& object : objects) {
1342+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1343+
if (object->hit(r, interval(ray_t.min, closest_so_far), temp_rec)) {
1344+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1345+
hit_anything = true;
1346+
closest_so_far = temp_rec.t;
1347+
rec = temp_rec;
1348+
}
1349+
}
1350+
1351+
return hit_anything;
1352+
}
1353+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1354+
[Listing [hittable-list-with-interval]: <kbd>[hittable.h]</kbd>
1355+
hittable_list::hit() using interval]
1356+
1357+
1358+
1359+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1360+
class sphere : public hittable {
1361+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1362+
virtual bool hit(const ray& r, interval ray_t, hit_record& rec) const override;
1363+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1364+
...
1365+
};
1366+
1367+
1368+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1369+
bool sphere::hit(const ray& r, interval ray_t, hit_record& rec) const {
1370+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1371+
...
1372+
1373+
// Find the nearest root that lies in the acceptable range.
1374+
auto root = (-half_b - sqrtd) / a;
1375+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1376+
if (!ray_t.contains(root)) {
1377+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1378+
root = (-half_b + sqrtd) / a;
1379+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1380+
if (!ray_t.contains(root))
1381+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1382+
return false;
1383+
}
1384+
...
1385+
}
1386+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1387+
[Listing [sphere-with-interval]: <kbd>[sphere.h]</kbd> sphere using interval]
1388+
1389+
1390+
1391+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1392+
...
1393+
color ray_color(const ray& r, const hittable& world) {
1394+
hit_record rec;
1395+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1396+
if (world.hit(r, interval(0, infinity), rec)) {
1397+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1398+
return 0.5 * (rec.normal + color(1,1,1));
1399+
}
1400+
...
1401+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1402+
[Listing [main-with-interval]: <kbd>[main.cc]</kbd> The new main using interval]
12811403

12821404

12831405
Antialiasing
@@ -1570,7 +1692,7 @@
15701692
color ray_color(const ray& r, const hittable& world) {
15711693
hit_record rec;
15721694

1573-
if (world.hit(r, 0, infinity, rec)) {
1695+
if (world.hit(r, interval(0, infinity), rec)) {
15741696
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
15751697
point3 target = rec.p + rec.normal + random_in_unit_sphere();
15761698
return 0.5 * ray_color(ray(rec.p, target - rec.p), world);
@@ -1606,7 +1728,7 @@
16061728
return color(0,0,0);
16071729
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
16081730

1609-
if (world.hit(r, 0, infinity, rec)) {
1731+
if (world.hit(r, interval(0, infinity), rec)) {
16101732
point3 target = rec.p + rec.normal + random_in_unit_sphere();
16111733
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
16121734
return 0.5 * ray_color(ray(rec.p, target - rec.p), world, depth-1);
@@ -1721,7 +1843,7 @@
17211843
point approximation the sphere intersector gives us. So we need to ignore hits very near zero:
17221844

17231845
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1724-
if (world.hit(r, 0.001, infinity, rec)) {
1846+
if (world.hit(r, interval(0.001, infinity), rec)) {
17251847
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17261848
[Listing [reflect-tolerance]: <kbd>[main.cc]</kbd> Calculating reflected ray origins with tolerance]
17271849

@@ -1772,7 +1894,7 @@
17721894
if (depth <= 0)
17731895
return color(0,0,0);
17741896

1775-
if (world.hit(r, 0.001, infinity, rec)) {
1897+
if (world.hit(r, interval(0.001, infinity), rec)) {
17761898
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
17771899
point3 target = rec.p + rec.normal + random_unit_vector();
17781900
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
@@ -1854,7 +1976,7 @@
18541976
if (depth <= 0)
18551977
return color(0,0,0);
18561978

1857-
if (world.hit(r, 0.001, infinity, rec)) {
1979+
if (world.hit(r, interval(0.001, infinity), rec)) {
18581980
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
18591981
point3 target = rec.p + random_in_hemisphere(rec.normal);
18601982
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
@@ -1976,8 +2098,7 @@
19762098
: center(ctr), radius(r), mat_ptr(m) {};
19772099
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
19782100

1979-
virtual bool hit(const ray& r, double ray_tmin, double ray_tmax, hit_record& rec)
1980-
const override;
2101+
virtual bool hit(const ray& r, interval ray_t, hit_record& rec) const override;
19812102

19822103
public:
19832104
point3 center;
@@ -1987,7 +2108,7 @@
19872108
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
19882109
};
19892110

1990-
bool sphere::hit(const ray& r, double ray_tmin, double ray_tmax, hit_record& rec) const {
2111+
bool sphere::hit(const ray& r, interval ray_t, hit_record& rec) const {
19912112
...
19922113

19932114
rec.t = root;
@@ -2146,7 +2267,7 @@
21462267
if (depth <= 0)
21472268
return color(0,0,0);
21482269

2149-
if (world.hit(r, 0.001, infinity, rec)) {
2270+
if (world.hit(r, interval(0.001, infinity), rec)) {
21502271
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
21512272
ray scattered;
21522273
color attenuation;

0 commit comments

Comments
 (0)