Skip to content

Commit 6251287

Browse files
authored
Merge pull request RayTracing#822 from RayTracing/defs-in-classes
Defs in classes
2 parents 1184d57 + b2d6736 commit 6251287

21 files changed

+1208
-1401
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Change Log -- Ray Tracing in One Weekend
1515
some global utility functions as either private static, or in better locations.
1616
- Fix: Remove redundant `virtual` keyword for methods with `override` (#805)
1717
- Change: `aabb` class constructor treats two params as extreme points in any orientation (#733)
18+
- Change: Moved all class method definitions inside class definition (#802)
1819

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

books/RayTracingInOneWeekend.html

Lines changed: 94 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -894,38 +894,36 @@
894894
sphere() {}
895895
sphere(point3 ctr, double r) : center(ctr), radius(r) {};
896896

897-
bool hit(const ray& r, double ray_tmin, double ray_tmax, hit_record& rec) const override;
897+
bool hit(const ray& r, double ray_tmin, double ray_tmax, hit_record& rec) const override {
898+
vec3 oc = r.origin() - center;
899+
auto a = r.direction().length_squared();
900+
auto half_b = dot(oc, r.direction());
901+
auto c = oc.length_squared() - radius*radius;
902+
903+
auto discriminant = half_b*half_b - a*c;
904+
if (discriminant < 0) return false;
905+
auto sqrtd = sqrt(discriminant);
906+
907+
// Find the nearest root that lies in the acceptable range.
908+
auto root = (-half_b - sqrtd) / a;
909+
if (root < ray_tmin || ray_tmax < root) {
910+
root = (-half_b + sqrtd) / a;
911+
if (root < ray_tmin || ray_tmax < root)
912+
return false;
913+
}
914+
915+
rec.t = root;
916+
rec.p = r.at(rec.t);
917+
rec.normal = (rec.p - center) / radius;
918+
919+
return true;
920+
}
898921

899922
public:
900923
point3 center;
901924
double radius;
902925
};
903926

904-
bool sphere::hit(const ray& r, double ray_tmin, double ray_tmax, hit_record& rec) const {
905-
vec3 oc = r.origin() - center;
906-
auto a = r.direction().length_squared();
907-
auto half_b = dot(oc, r.direction());
908-
auto c = oc.length_squared() - radius*radius;
909-
910-
auto discriminant = half_b*half_b - a*c;
911-
if (discriminant < 0) return false;
912-
auto sqrtd = sqrt(discriminant);
913-
914-
// Find the nearest root that lies in the acceptable range.
915-
auto root = (-half_b - sqrtd) / a;
916-
if (root < ray_tmin || ray_tmax < root) {
917-
root = (-half_b + sqrtd) / a;
918-
if (root < ray_tmin || ray_tmax < root)
919-
return false;
920-
}
921-
922-
rec.t = root;
923-
rec.p = r.at(rec.t);
924-
rec.normal = (rec.p - center) / radius;
925-
926-
return true;
927-
}
928-
929927
#endif
930928
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
931929
[Listing [sphere-initial]: <kbd>[sphere.h]</kbd> The sphere class]
@@ -1024,18 +1022,23 @@
10241022
And then we add the surface side determination to the class:
10251023

10261024
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1027-
bool sphere::hit(const ray& r, double ray_tmin, double ray_tmax, hit_record& rec) const {
1025+
class sphere : public hittable {
1026+
public:
10281027
...
1028+
bool here::hit(const ray& r, double ray_tmin, double ray_tmax, hit_record& rec) const {
1029+
...
10291030

1030-
rec.t = root;
1031-
rec.p = r.at(rec.t);
1031+
rec.t = root;
1032+
rec.p = r.at(rec.t);
10321033
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1033-
vec3 outward_normal = (rec.p - center) / radius;
1034-
rec.set_face_normal(r, outward_normal);
1034+
vec3 outward_normal = (rec.p - center) / radius;
1035+
rec.set_face_normal(r, outward_normal);
10351036
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
10361037

1037-
return true;
1038-
}
1038+
return true;
1039+
}
1040+
...
1041+
};
10391042
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10401043
[Listing [sphere-final]: <kbd>[sphere.h]</kbd> The sphere class with normal determination]
10411044

@@ -1068,27 +1071,25 @@
10681071
void clear() { objects.clear(); }
10691072
void add(shared_ptr<hittable> object) { objects.push_back(object); }
10701073

1071-
bool hit(const ray& r, double ray_tmin, double ray_tmax, hit_record& rec) const override;
1072-
1073-
public:
1074-
std::vector<shared_ptr<hittable>> objects;
1075-
};
1074+
bool hit(const ray& r, double ray_tmin, double ray_tmax, hit_record& rec) const override {
1075+
hit_record temp_rec;
1076+
bool hit_anything = false;
1077+
auto closest_so_far = ray_tmax;
10761078

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;
1079+
for (const auto& object : objects) {
1080+
if (object->hit(r, ray_tmin, closest_so_far, temp_rec)) {
1081+
hit_anything = true;
1082+
closest_so_far = temp_rec.t;
1083+
rec = temp_rec;
1084+
}
10871085
}
1086+
1087+
return hit_anything;
10881088
}
10891089

1090-
return hit_anything;
1091-
}
1090+
public:
1091+
std::vector<shared_ptr<hittable>> objects;
1092+
};
10921093

10931094
#endif
10941095
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1310,82 +1311,73 @@
13101311
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13111312
[Listing [interval-initial]: <kbd>[interval.h]</kbd> Introducing the new interval class]
13121313

1314+
13131315
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
13141316
class hittable {
1315-
public:
1317+
public:
1318+
...
13161319
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1317-
virtual bool hit(const ray& r, interval ray_t, hit_record& rec) const = 0;
1320+
virtual bool hit(const ray& r, interval ray_t, hit_record& rec) const = 0;
13181321
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
13191322
};
13201323
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13211324
[Listing [hittable-with-interval]: <kbd>[hittable.h]</kbd> hittable::hit() using interval]
13221325

13231326

1324-
13251327
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
13261328
class hittable_list : public hittable {
1329+
public:
13271330
...
13281331
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1329-
bool hit(const ray& r, interval ray_t, hit_record& rec) const override;
1330-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1331-
...
1332-
};
1333-
1334-
1335-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1336-
bool hittable_list::hit(const ray& r, interval ray_t, hit_record& rec) const {
1332+
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
13371333
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1338-
hit_record temp_rec;
1339-
bool hit_anything = false;
1334+
hit_record temp_rec;
1335+
bool hit_anything = false;
13401336
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1341-
auto closest_so_far = ray_t.max;
1337+
auto closest_so_far = ray_t.max;
13421338
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
13431339

1344-
for (const auto& object : objects) {
1340+
for (const auto& object : objects) {
13451341
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1346-
if (object->hit(r, interval(ray_t.min, closest_so_far), temp_rec)) {
1342+
if (object->hit(r, interval(ray_t.min, closest_so_far), temp_rec)) {
13471343
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1348-
hit_anything = true;
1349-
closest_so_far = temp_rec.t;
1350-
rec = temp_rec;
1344+
hit_anything = true;
1345+
closest_so_far = temp_rec.t;
1346+
rec = temp_rec;
1347+
}
13511348
}
1352-
}
13531349

1354-
return hit_anything;
1355-
}
1350+
return hit_anything;
1351+
}
13561352
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13571353
[Listing [hittable-list-with-interval]: <kbd>[hittable.h]</kbd>
1358-
hittable_list::hit() using interval]
1359-
1354+
hittable_list::hit() using interval]
13601355

13611356

13621357
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
13631358
class sphere : public hittable {
1364-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1365-
bool hit(const ray& r, interval ray_t, hit_record& rec) const override;
1366-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1359+
public:
13671360
...
1368-
};
1369-
1370-
13711361
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1372-
bool sphere::hit(const ray& r, interval ray_t, hit_record& rec) const {
1362+
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
13731363
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1374-
...
1364+
...
13751365

1376-
// Find the nearest root that lies in the acceptable range.
1377-
auto root = (-half_b - sqrtd) / a;
1366+
// Find the nearest root that lies in the acceptable range.
1367+
auto root = (-half_b - sqrtd) / a;
13781368
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1379-
if (!ray_t.contains(root)) {
1369+
if (!ray_t.contains(root)) {
13801370
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1381-
root = (-half_b + sqrtd) / a;
1371+
root = (-half_b + sqrtd) / a;
13821372
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1383-
if (!ray_t.contains(root))
1373+
if (!ray_t.contains(root))
13841374
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1385-
return false;
1375+
return false;
1376+
}
1377+
...
13861378
}
13871379
...
1388-
}
1380+
};
13891381
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13901382
[Listing [sphere-with-interval]: <kbd>[sphere.h]</kbd> sphere using interval]
13911383

@@ -2107,7 +2099,19 @@
21072099
: center(ctr), radius(r), mat_ptr(m) {};
21082100
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
21092101

2110-
bool hit(const ray& r, interval ray_t, hit_record& rec) const override;
2102+
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
2103+
...
2104+
2105+
rec.t = root;
2106+
rec.p = r.at(rec.t);
2107+
vec3 outward_normal = (rec.p - center) / radius;
2108+
rec.set_face_normal(r, outward_normal);
2109+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
2110+
rec.mat_ptr = mat_ptr;
2111+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2112+
2113+
return true;
2114+
}
21112115

21122116
public:
21132117
point3 center;
@@ -2116,20 +2120,6 @@
21162120
shared_ptr<material> mat_ptr;
21172121
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
21182122
};
2119-
2120-
bool sphere::hit(const ray& r, interval ray_t, hit_record& rec) const {
2121-
...
2122-
2123-
rec.t = root;
2124-
rec.p = r.at(rec.t);
2125-
vec3 outward_normal = (rec.p - center) / radius;
2126-
rec.set_face_normal(r, outward_normal);
2127-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
2128-
rec.mat_ptr = mat_ptr;
2129-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2130-
2131-
return true;
2132-
}
21332123
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21342124
[Listing [sphere-material]: <kbd>[sphere.h]</kbd> Ray-sphere intersection with added material information]
21352125
</div>

0 commit comments

Comments
 (0)