Skip to content

Commit b23bb9e

Browse files
committed
Refactor hittable.h for clarity
1 parent c9ec073 commit b23bb9e

File tree

2 files changed

+95
-74
lines changed

2 files changed

+95
-74
lines changed

books/RayTracingTheNextWeek.html

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,12 +2043,11 @@
20432043
flip_face(shared_ptr<hittable> p) : ptr(p) {}
20442044

20452045
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
2046-
if (ptr->hit(r, t_min, t_max, rec)) {
2047-
rec.front_face = !rec.front_face;
2048-
return true;
2049-
}
2050-
else
2046+
if (!ptr->hit(r, t_min, t_max, rec))
20512047
return false;
2048+
2049+
rec.front_face = !rec.front_face;
2050+
return true;
20522051
}
20532052

20542053
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
@@ -2202,24 +2201,24 @@
22022201

22032202
bool translate::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
22042203
ray moved_r(r.origin() - offset, r.direction(), r.time());
2205-
if (ptr->hit(moved_r, t_min, t_max, rec)) {
2206-
rec.p += offset;
2207-
rec.set_face_normal(moved_r, rec.normal);
2208-
return true;
2209-
}
2210-
else
2204+
if (!ptr->hit(moved_r, t_min, t_max, rec))
22112205
return false;
2206+
2207+
rec.p += offset;
2208+
rec.set_face_normal(moved_r, rec.normal);
2209+
2210+
return true;
22122211
}
22132212

22142213
bool translate::bounding_box(double t0, double t1, aabb& output_box) const {
2215-
if (ptr->bounding_box(t0, t1, output_box)) {
2216-
output_box = aabb(
2217-
output_box.min() + offset,
2218-
output_box.max() + offset);
2219-
return true;
2220-
}
2221-
else
2214+
if (!ptr->bounding_box(t0, t1, output_box))
22222215
return false;
2216+
2217+
output_box = aabb(
2218+
output_box.min() + offset,
2219+
output_box.max() + offset);
2220+
2221+
return true;
22232222
}
22242223
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22252224
[Listing [translate-class]: <kbd>[hittable.h]</kbd> Hittable translation class]
@@ -2300,26 +2299,30 @@
23002299
sin_theta = sin(radians);
23012300
cos_theta = cos(radians);
23022301
hasbox = ptr->bounding_box(0, 1, bbox);
2303-
vec3 min(infinity, infinity, infinity);
2302+
2303+
vec3 min( infinity, infinity, infinity);
23042304
vec3 max(-infinity, -infinity, -infinity);
2305+
23052306
for (int i = 0; i < 2; i++) {
23062307
for (int j = 0; j < 2; j++) {
23072308
for (int k = 0; k < 2; k++) {
23082309
auto x = i*bbox.max().x() + (1-i)*bbox.min().x();
23092310
auto y = j*bbox.max().y() + (1-j)*bbox.min().y();
23102311
auto z = k*bbox.max().z() + (1-k)*bbox.min().z();
2311-
auto newx = cos_theta*x + sin_theta*z;
2312+
2313+
auto newx = cos_theta*x + sin_theta*z;
23122314
auto newz = -sin_theta*x + cos_theta*z;
2315+
23132316
vec3 tester(newx, y, newz);
2317+
23142318
for (int c = 0; c < 3; c++) {
2315-
if (tester[c] > max[c])
2316-
max[c] = tester[c];
2317-
if (tester[c] < min[c])
2318-
min[c] = tester[c];
2319+
min[c] = ffmin(min[c], tester[c]);
2320+
max[c] = ffmax(max[c], tester[c]);
23192321
}
23202322
}
23212323
}
23222324
}
2325+
23232326
bbox = aabb(min, max);
23242327
}
23252328
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -2333,24 +2336,31 @@
23332336
bool rotate_y::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
23342337
vec3 origin = r.origin();
23352338
vec3 direction = r.direction();
2339+
23362340
origin[0] = cos_theta*r.origin()[0] - sin_theta*r.origin()[2];
2337-
origin[2] = sin_theta*r.origin()[0] + cos_theta*r.origin()[2];
2341+
origin[2] = sin_theta*r.origin()[0] + cos_theta*r.origin()[2];
2342+
23382343
direction[0] = cos_theta*r.direction()[0] - sin_theta*r.direction()[2];
23392344
direction[2] = sin_theta*r.direction()[0] + cos_theta*r.direction()[2];
2345+
23402346
ray rotated_r(origin, direction, r.time());
2341-
if (ptr->hit(rotated_r, t_min, t_max, rec)) {
2342-
vec3 p = rec.p;
2343-
vec3 normal = rec.normal;
2344-
p[0] = cos_theta*rec.p[0] + sin_theta*rec.p[2];
2345-
p[2] = -sin_theta*rec.p[0] + cos_theta*rec.p[2];
2346-
normal[0] = cos_theta*rec.normal[0] + sin_theta*rec.normal[2];
2347-
normal[2] = -sin_theta*rec.normal[0] + cos_theta*rec.normal[2];
2348-
rec.p = p;
2349-
rec.set_face_normal(rotated_r, normal);
2350-
return true;
2351-
}
2352-
else
2347+
2348+
if (!ptr->hit(rotated_r, t_min, t_max, rec))
23532349
return false;
2350+
2351+
vec3 p = rec.p;
2352+
vec3 normal = rec.normal;
2353+
2354+
p[0] = cos_theta*rec.p[0] + sin_theta*rec.p[2];
2355+
p[2] = -sin_theta*rec.p[0] + cos_theta*rec.p[2];
2356+
2357+
normal[0] = cos_theta*rec.normal[0] + sin_theta*rec.normal[2];
2358+
normal[2] = -sin_theta*rec.normal[0] + cos_theta*rec.normal[2];
2359+
2360+
rec.p = p;
2361+
rec.set_face_normal(rotated_r, normal);
2362+
2363+
return true;
23542364
}
23552365
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23562366
[Listing [rot-y-hit]: <kbd>[hittable.h]</kbd> Hittable Y-rotate hit function]

src/TheNextWeek/hittable.h

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,11 @@ class flip_face : public hittable {
5353
flip_face(shared_ptr<hittable> p) : ptr(p) {}
5454

5555
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
56-
if (ptr->hit(r, t_min, t_max, rec)) {
57-
rec.front_face = !rec.front_face;
58-
return true;
59-
}
60-
else
56+
if (!ptr->hit(r, t_min, t_max, rec))
6157
return false;
58+
59+
rec.front_face = !rec.front_face;
60+
return true;
6261
}
6362

6463
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
@@ -86,25 +85,25 @@ class translate : public hittable {
8685

8786
bool translate::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
8887
ray moved_r(r.origin() - offset, r.direction(), r.time());
89-
if (ptr->hit(moved_r, t_min, t_max, rec)) {
90-
rec.p += offset;
91-
rec.set_face_normal(moved_r, rec.normal);
92-
return true;
93-
}
94-
else
88+
if (!ptr->hit(moved_r, t_min, t_max, rec))
9589
return false;
90+
91+
rec.p += offset;
92+
rec.set_face_normal(moved_r, rec.normal);
93+
94+
return true;
9695
}
9796

9897

9998
bool translate::bounding_box(double t0, double t1, aabb& output_box) const {
100-
if (ptr->bounding_box(t0, t1, output_box)) {
101-
output_box = aabb(
102-
output_box.min() + offset,
103-
output_box.max() + offset);
104-
return true;
105-
}
106-
else
99+
if (!ptr->bounding_box(t0, t1, output_box))
107100
return false;
101+
102+
output_box = aabb(
103+
output_box.min() + offset,
104+
output_box.max() + offset);
105+
106+
return true;
108107
}
109108

110109

@@ -132,51 +131,63 @@ rotate_y::rotate_y(shared_ptr<hittable> p, double angle) : ptr(p) {
132131
sin_theta = sin(radians);
133132
cos_theta = cos(radians);
134133
hasbox = ptr->bounding_box(0, 1, bbox);
135-
vec3 min(infinity, infinity, infinity);
134+
135+
vec3 min( infinity, infinity, infinity);
136136
vec3 max(-infinity, -infinity, -infinity);
137+
137138
for (int i = 0; i < 2; i++) {
138139
for (int j = 0; j < 2; j++) {
139140
for (int k = 0; k < 2; k++) {
140141
auto x = i*bbox.max().x() + (1-i)*bbox.min().x();
141142
auto y = j*bbox.max().y() + (1-j)*bbox.min().y();
142143
auto z = k*bbox.max().z() + (1-k)*bbox.min().z();
143-
auto newx = cos_theta*x + sin_theta*z;
144+
145+
auto newx = cos_theta*x + sin_theta*z;
144146
auto newz = -sin_theta*x + cos_theta*z;
147+
145148
vec3 tester(newx, y, newz);
149+
146150
for (int c = 0; c < 3; c++) {
147-
if (tester[c] > max[c])
148-
max[c] = tester[c];
149-
if (tester[c] < min[c])
150-
min[c] = tester[c];
151+
min[c] = ffmin(min[c], tester[c]);
152+
max[c] = ffmax(max[c], tester[c]);
151153
}
152154
}
153155
}
154156
}
157+
155158
bbox = aabb(min, max);
156159
}
157160

158161

159162
bool rotate_y::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
160163
vec3 origin = r.origin();
161164
vec3 direction = r.direction();
165+
162166
origin[0] = cos_theta*r.origin()[0] - sin_theta*r.origin()[2];
163-
origin[2] = sin_theta*r.origin()[0] + cos_theta*r.origin()[2];
167+
origin[2] = sin_theta*r.origin()[0] + cos_theta*r.origin()[2];
168+
164169
direction[0] = cos_theta*r.direction()[0] - sin_theta*r.direction()[2];
165170
direction[2] = sin_theta*r.direction()[0] + cos_theta*r.direction()[2];
171+
166172
ray rotated_r(origin, direction, r.time());
167-
if (ptr->hit(rotated_r, t_min, t_max, rec)) {
168-
vec3 p = rec.p;
169-
vec3 normal = rec.normal;
170-
p[0] = cos_theta*rec.p[0] + sin_theta*rec.p[2];
171-
p[2] = -sin_theta*rec.p[0] + cos_theta*rec.p[2];
172-
normal[0] = cos_theta*rec.normal[0] + sin_theta*rec.normal[2];
173-
normal[2] = -sin_theta*rec.normal[0] + cos_theta*rec.normal[2];
174-
rec.p = p;
175-
rec.set_face_normal(rotated_r, normal);
176-
return true;
177-
}
178-
else
173+
174+
if (!ptr->hit(rotated_r, t_min, t_max, rec))
179175
return false;
176+
177+
vec3 p = rec.p;
178+
vec3 normal = rec.normal;
179+
180+
p[0] = cos_theta*rec.p[0] + sin_theta*rec.p[2];
181+
p[2] = -sin_theta*rec.p[0] + cos_theta*rec.p[2];
182+
183+
normal[0] = cos_theta*rec.normal[0] + sin_theta*rec.normal[2];
184+
normal[2] = -sin_theta*rec.normal[0] + cos_theta*rec.normal[2];
185+
186+
rec.p = p;
187+
rec.set_face_normal(rotated_r, normal);
188+
189+
return true;
180190
}
181191

192+
182193
#endif

0 commit comments

Comments
 (0)