Skip to content

Commit 317155f

Browse files
authored
Merge pull request #332 from RayTracing/refactoring
Refactoring
2 parents fe725ac + ac5409b commit 317155f

28 files changed

+337
-265
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ set ( SOURCE_REST_OF_YOUR_LIFE
6363
src/TheRestOfYourLife/hittable.h
6464
src/TheRestOfYourLife/hittable_list.h
6565
src/TheRestOfYourLife/material.h
66-
src/TheRestOfYourLife/moving_sphere.h
6766
src/TheRestOfYourLife/onb.h
6867
src/TheRestOfYourLife/pdf.h
6968
src/TheRestOfYourLife/sphere.h

books/RayTracingInOneWeekend.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@
251251
<< static_cast<int>(255.999 * e[2]) << '\n';
252252
}
253253

254+
public:
254255
double e[3];
255256
};
256257
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -377,6 +378,7 @@
377378
return orig + t*dir;
378379
}
379380

381+
public:
380382
vec3 orig;
381383
vec3 dir;
382384
};
@@ -1216,6 +1218,7 @@
12161218
return ray(origin, lower_left_corner + u*horizontal + v*vertical - origin);
12171219
}
12181220

1221+
public:
12191222
vec3 origin;
12201223
vec3 lower_left_corner;
12211224
vec3 horizontal;
@@ -2356,6 +2359,7 @@
23562359
return ray(origin, lower_left_corner + u*horizontal + v*vertical - origin);
23572360
}
23582361

2362+
public:
23592363
vec3 origin;
23602364
vec3 lower_left_corner;
23612365
vec3 horizontal;
@@ -2440,6 +2444,7 @@
24402444
return ray(origin, lower_left_corner + s*horizontal + t*vertical - origin);
24412445
}
24422446

2447+
public:
24432448
vec3 origin;
24442449
vec3 lower_left_corner;
24452450
vec3 horizontal;
@@ -2571,6 +2576,7 @@
25712576
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
25722577
}
25732578

2579+
public:
25742580
vec3 origin;
25752581
vec3 lower_left_corner;
25762582
vec3 horizontal;

books/RayTracingTheNextWeek.html

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
return orig + t*dir;
8282
}
8383

84+
public:
8485
vec3 orig;
8586
vec3 dir;
8687
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -143,6 +144,7 @@
143144
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
144145
}
145146

147+
public:
146148
vec3 origin;
147149
vec3 lower_left_corner;
148150
vec3 horizontal;
@@ -204,10 +206,12 @@
204206
auto a = r.direction().length_squared();
205207
auto half_b = dot(oc, r.direction());
206208
auto c = oc.length_squared() - radius*radius;
209+
207210
auto discriminant = half_b*half_b - a*c;
208211

209212
if (discriminant > 0) {
210213
auto root = sqrt(discriminant);
214+
211215
auto temp = (-half_b - root)/a;
212216
if (temp < t_max && temp > t_min) {
213217
rec.t = temp;
@@ -219,6 +223,7 @@
219223
rec.mat_ptr = mat_ptr;
220224
return true;
221225
}
226+
222227
temp = (-half_b + root) / a;
223228
if (temp < t_max && temp > t_min) {
224229
rec.t = temp;
@@ -884,9 +889,12 @@
884889
public:
885890
constant_texture() {}
886891
constant_texture(vec3 c) : color(c) {}
892+
887893
virtual vec3 value(double u, double v, const vec3& p) const {
888894
return color;
889895
}
896+
897+
public:
890898
vec3 color;
891899
};
892900
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -2038,12 +2046,11 @@
20382046
flip_face(shared_ptr<hittable> p) : ptr(p) {}
20392047

20402048
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
2041-
if (ptr->hit(r, t_min, t_max, rec)) {
2042-
rec.front_face = !rec.front_face;
2043-
return true;
2044-
}
2045-
else
2049+
if (!ptr->hit(r, t_min, t_max, rec))
20462050
return false;
2051+
2052+
rec.front_face = !rec.front_face;
2053+
return true;
20472054
}
20482055

20492056
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
@@ -2197,24 +2204,24 @@
21972204

21982205
bool translate::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
21992206
ray moved_r(r.origin() - offset, r.direction(), r.time());
2200-
if (ptr->hit(moved_r, t_min, t_max, rec)) {
2201-
rec.p += offset;
2202-
rec.set_face_normal(moved_r, rec.normal);
2203-
return true;
2204-
}
2205-
else
2207+
if (!ptr->hit(moved_r, t_min, t_max, rec))
22062208
return false;
2209+
2210+
rec.p += offset;
2211+
rec.set_face_normal(moved_r, rec.normal);
2212+
2213+
return true;
22072214
}
22082215

22092216
bool translate::bounding_box(double t0, double t1, aabb& output_box) const {
2210-
if (ptr->bounding_box(t0, t1, output_box)) {
2211-
output_box = aabb(
2212-
output_box.min() + offset,
2213-
output_box.max() + offset);
2214-
return true;
2215-
}
2216-
else
2217+
if (!ptr->bounding_box(t0, t1, output_box))
22172218
return false;
2219+
2220+
output_box = aabb(
2221+
output_box.min() + offset,
2222+
output_box.max() + offset);
2223+
2224+
return true;
22182225
}
22192226
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22202227
[Listing [translate-class]: <kbd>[hittable.h]</kbd> Hittable translation class]
@@ -2295,26 +2302,30 @@
22952302
sin_theta = sin(radians);
22962303
cos_theta = cos(radians);
22972304
hasbox = ptr->bounding_box(0, 1, bbox);
2298-
vec3 min(infinity, infinity, infinity);
2305+
2306+
vec3 min( infinity, infinity, infinity);
22992307
vec3 max(-infinity, -infinity, -infinity);
2308+
23002309
for (int i = 0; i < 2; i++) {
23012310
for (int j = 0; j < 2; j++) {
23022311
for (int k = 0; k < 2; k++) {
23032312
auto x = i*bbox.max().x() + (1-i)*bbox.min().x();
23042313
auto y = j*bbox.max().y() + (1-j)*bbox.min().y();
23052314
auto z = k*bbox.max().z() + (1-k)*bbox.min().z();
2306-
auto newx = cos_theta*x + sin_theta*z;
2315+
2316+
auto newx = cos_theta*x + sin_theta*z;
23072317
auto newz = -sin_theta*x + cos_theta*z;
2318+
23082319
vec3 tester(newx, y, newz);
2320+
23092321
for (int c = 0; c < 3; c++) {
2310-
if (tester[c] > max[c])
2311-
max[c] = tester[c];
2312-
if (tester[c] < min[c])
2313-
min[c] = tester[c];
2322+
min[c] = ffmin(min[c], tester[c]);
2323+
max[c] = ffmax(max[c], tester[c]);
23142324
}
23152325
}
23162326
}
23172327
}
2328+
23182329
bbox = aabb(min, max);
23192330
}
23202331
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -2328,24 +2339,31 @@
23282339
bool rotate_y::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
23292340
vec3 origin = r.origin();
23302341
vec3 direction = r.direction();
2342+
23312343
origin[0] = cos_theta*r.origin()[0] - sin_theta*r.origin()[2];
2332-
origin[2] = sin_theta*r.origin()[0] + cos_theta*r.origin()[2];
2344+
origin[2] = sin_theta*r.origin()[0] + cos_theta*r.origin()[2];
2345+
23332346
direction[0] = cos_theta*r.direction()[0] - sin_theta*r.direction()[2];
23342347
direction[2] = sin_theta*r.direction()[0] + cos_theta*r.direction()[2];
2348+
23352349
ray rotated_r(origin, direction, r.time());
2336-
if (ptr->hit(rotated_r, t_min, t_max, rec)) {
2337-
vec3 p = rec.p;
2338-
vec3 normal = rec.normal;
2339-
p[0] = cos_theta*rec.p[0] + sin_theta*rec.p[2];
2340-
p[2] = -sin_theta*rec.p[0] + cos_theta*rec.p[2];
2341-
normal[0] = cos_theta*rec.normal[0] + sin_theta*rec.normal[2];
2342-
normal[2] = -sin_theta*rec.normal[0] + cos_theta*rec.normal[2];
2343-
rec.p = p;
2344-
rec.set_face_normal(rotated_r, normal);
2345-
return true;
2346-
}
2347-
else
2350+
2351+
if (!ptr->hit(rotated_r, t_min, t_max, rec))
23482352
return false;
2353+
2354+
vec3 p = rec.p;
2355+
vec3 normal = rec.normal;
2356+
2357+
p[0] = cos_theta*rec.p[0] + sin_theta*rec.p[2];
2358+
p[2] = -sin_theta*rec.p[0] + cos_theta*rec.p[2];
2359+
2360+
normal[0] = cos_theta*rec.normal[0] + sin_theta*rec.normal[2];
2361+
normal[2] = -sin_theta*rec.normal[0] + cos_theta*rec.normal[2];
2362+
2363+
rec.p = p;
2364+
rec.set_face_normal(rotated_r, normal);
2365+
2366+
return true;
23492367
}
23502368
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23512369
[Listing [rot-y-hit]: <kbd>[hittable.h]</kbd> Hittable Y-rotate hit function]

0 commit comments

Comments
 (0)