Skip to content

Commit d910273

Browse files
committed
Refactor restLife/hittable.h
Bring it in sync with the version from nextWeek, and update book to match.
1 parent dc08cc4 commit d910273

File tree

2 files changed

+82
-41
lines changed

2 files changed

+82
-41
lines changed

books/RayTracingTheRestOfYourLife.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,9 +1567,15 @@
15671567
public:
15681568
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const = 0;
15691569
virtual bool bounding_box(double t0, double t1, aabb& output_box) const = 0;
1570+
15701571
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1571-
virtual double pdf_value(const vec3& o, const vec3& v) const { return 0.0; }
1572-
virtual vec3 random(const vec3& o) const {return vec3(1, 0, 0);}
1572+
virtual double pdf_value(const vec3& o, const vec3& v) const {
1573+
return 0.0;
1574+
}
1575+
1576+
virtual vec3 random(const vec3& o) const {
1577+
return vec3(1, 0, 0);
1578+
}
15731579
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
15741580
};
15751581
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/TheRestOfYourLife/hittable.h

Lines changed: 74 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -40,127 +40,162 @@ struct hit_record {
4040
}
4141
};
4242

43+
4344
class hittable {
4445
public:
4546
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const = 0;
4647
virtual bool bounding_box(double t0, double t1, aabb& output_box) const = 0;
47-
virtual double pdf_value(const vec3& o, const vec3& v) const { return 0.0; }
48-
virtual vec3 random(const vec3& o) const { return vec3(1,0,0); }
48+
49+
virtual double pdf_value(const vec3& o, const vec3& v) const {
50+
return 0.0;
51+
}
52+
53+
virtual vec3 random(const vec3& o) const {
54+
return vec3(1,0,0);
55+
}
4956
};
5057

58+
5159
class flip_face : public hittable {
5260
public:
5361
flip_face(shared_ptr<hittable> p) : ptr(p) {}
62+
5463
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
55-
if (ptr->hit(r, t_min, t_max, rec)) {
56-
rec.front_face = !rec.front_face;
57-
return true;
58-
}
59-
else
64+
if (!ptr->hit(r, t_min, t_max, rec))
6065
return false;
66+
67+
rec.front_face = !rec.front_face;
68+
return true;
6169
}
70+
6271
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
6372
return ptr->bounding_box(t0, t1, output_box);
6473
}
74+
75+
public:
6576
shared_ptr<hittable> ptr;
6677
};
6778

79+
6880
class translate : public hittable {
6981
public:
7082
translate(shared_ptr<hittable> p, const vec3& displacement)
7183
: ptr(p), offset(displacement) {}
84+
7285
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const;
7386
virtual bool bounding_box(double t0, double t1, aabb& output_box) const;
87+
88+
public:
7489
shared_ptr<hittable> ptr;
7590
vec3 offset;
7691
};
7792

93+
7894
bool translate::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
7995
ray moved_r(r.origin() - offset, r.direction(), r.time());
80-
if (ptr->hit(moved_r, t_min, t_max, rec)) {
81-
rec.p += offset;
82-
rec.set_face_normal(moved_r, rec.normal);
83-
return true;
84-
}
85-
else
96+
if (!ptr->hit(moved_r, t_min, t_max, rec))
8697
return false;
98+
99+
rec.p += offset;
100+
rec.set_face_normal(moved_r, rec.normal);
101+
102+
return true;
87103
}
88104

105+
89106
bool translate::bounding_box(double t0, double t1, aabb& output_box) const {
90-
if (ptr->bounding_box(t0, t1, output_box)) {
91-
output_box = aabb(
92-
output_box.min() + offset,
93-
output_box.max() + offset);
94-
return true;
95-
}
96-
else
107+
if (!ptr->bounding_box(t0, t1, output_box))
97108
return false;
109+
110+
output_box = aabb(
111+
output_box.min() + offset,
112+
output_box.max() + offset);
113+
114+
return true;
98115
}
99116

117+
100118
class rotate_y : public hittable {
101119
public:
102120
rotate_y(shared_ptr<hittable> p, double angle);
121+
103122
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const;
104123
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
105124
output_box = bbox;
106125
return hasbox;
107126
}
127+
128+
public:
108129
shared_ptr<hittable> ptr;
109130
double sin_theta;
110131
double cos_theta;
111132
bool hasbox;
112133
aabb bbox;
113134
};
114135

136+
115137
rotate_y::rotate_y(shared_ptr<hittable> p, double angle) : ptr(p) {
116138
auto radians = degrees_to_radians(angle);
117139
sin_theta = sin(radians);
118140
cos_theta = cos(radians);
119141
hasbox = ptr->bounding_box(0, 1, bbox);
120-
vec3 min(infinity, infinity, infinity);
142+
143+
vec3 min( infinity, infinity, infinity);
121144
vec3 max(-infinity, -infinity, -infinity);
145+
122146
for (int i = 0; i < 2; i++) {
123147
for (int j = 0; j < 2; j++) {
124148
for (int k = 0; k < 2; k++) {
125149
auto x = i*bbox.max().x() + (1-i)*bbox.min().x();
126150
auto y = j*bbox.max().y() + (1-j)*bbox.min().y();
127151
auto z = k*bbox.max().z() + (1-k)*bbox.min().z();
128-
auto newx = cos_theta*x + sin_theta*z;
152+
153+
auto newx = cos_theta*x + sin_theta*z;
129154
auto newz = -sin_theta*x + cos_theta*z;
155+
130156
vec3 tester(newx, y, newz);
157+
131158
for (int c = 0; c < 3; c++) {
132-
if (tester[c] > max[c])
133-
max[c] = tester[c];
134-
if (tester[c] < min[c])
135-
min[c] = tester[c];
159+
min[c] = ffmin(min[c], tester[c]);
160+
max[c] = ffmax(max[c], tester[c]);
136161
}
137162
}
138163
}
139164
}
165+
140166
bbox = aabb(min, max);
141167
}
142168

169+
143170
bool rotate_y::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
144171
vec3 origin = r.origin();
145172
vec3 direction = r.direction();
173+
146174
origin[0] = cos_theta*r.origin()[0] - sin_theta*r.origin()[2];
147-
origin[2] = sin_theta*r.origin()[0] + cos_theta*r.origin()[2];
175+
origin[2] = sin_theta*r.origin()[0] + cos_theta*r.origin()[2];
176+
148177
direction[0] = cos_theta*r.direction()[0] - sin_theta*r.direction()[2];
149178
direction[2] = sin_theta*r.direction()[0] + cos_theta*r.direction()[2];
179+
150180
ray rotated_r(origin, direction, r.time());
151-
if (ptr->hit(rotated_r, t_min, t_max, rec)) {
152-
vec3 p = rec.p;
153-
vec3 normal = rec.normal;
154-
p[0] = cos_theta*rec.p[0] + sin_theta*rec.p[2];
155-
p[2] = -sin_theta*rec.p[0] + cos_theta*rec.p[2];
156-
normal[0] = cos_theta*rec.normal[0] + sin_theta*rec.normal[2];
157-
normal[2] = -sin_theta*rec.normal[0] + cos_theta*rec.normal[2];
158-
rec.p = p;
159-
rec.set_face_normal(rotated_r, normal);
160-
return true;
161-
}
162-
else
181+
182+
if (!ptr->hit(rotated_r, t_min, t_max, rec))
163183
return false;
184+
185+
vec3 p = rec.p;
186+
vec3 normal = rec.normal;
187+
188+
p[0] = cos_theta*rec.p[0] + sin_theta*rec.p[2];
189+
p[2] = -sin_theta*rec.p[0] + cos_theta*rec.p[2];
190+
191+
normal[0] = cos_theta*rec.normal[0] + sin_theta*rec.normal[2];
192+
normal[2] = -sin_theta*rec.normal[0] + cos_theta*rec.normal[2];
193+
194+
rec.p = p;
195+
rec.set_face_normal(rotated_r, normal);
196+
197+
return true;
164198
}
165199

200+
166201
#endif

0 commit comments

Comments
 (0)