Skip to content

Commit b3bcf6f

Browse files
committed
Refactor onb.h for readability
1 parent 2aea643 commit b3bcf6f

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

books/RayTracingTheRestOfYourLife.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,9 @@
12011201
class onb {
12021202
public:
12031203
onb() {}
1204+
12041205
inline vec3 operator[](int i) const { return axis[i]; }
1206+
12051207
vec3 u() const { return axis[0]; }
12061208
vec3 v() const { return axis[1]; }
12071209
vec3 w() const { return axis[2]; }
@@ -1215,17 +1217,15 @@
12151217
}
12161218

12171219
void build_from_w(const vec3&);
1220+
1221+
public:
12181222
vec3 axis[3];
12191223
};
12201224

12211225

12221226
void onb::build_from_w(const vec3& n) {
12231227
axis[2] = unit_vector(n);
1224-
vec3 a;
1225-
if (fabs(w().x()) > 0.9)
1226-
a = vec3(0, 1, 0);
1227-
else
1228-
a = vec3(1, 0, 0);
1228+
vec3 a = (fabs(w().x()) > 0.9) ? vec3(0,1,0) : vec3(1,0,0);
12291229
axis[1] = unit_vector(cross(w(), a));
12301230
axis[0] = cross(w(), v());
12311231
}

src/TheRestOfYourLife/onb.h

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,31 @@ class onb
1818
{
1919
public:
2020
onb() {}
21+
2122
inline vec3 operator[](int i) const { return axis[i]; }
22-
vec3 u() const { return axis[0]; }
23-
vec3 v() const { return axis[1]; }
24-
vec3 w() const { return axis[2]; }
25-
vec3 local(double a, double b, double c) const { return a*u() + b*v() + c*w(); }
26-
vec3 local(const vec3& a) const { return a.x()*u() + a.y()*v() + a.z()*w(); }
23+
24+
vec3 u() const { return axis[0]; }
25+
vec3 v() const { return axis[1]; }
26+
vec3 w() const { return axis[2]; }
27+
28+
vec3 local(double a, double b, double c) const {
29+
return a*u() + b*v() + c*w();
30+
}
31+
32+
vec3 local(const vec3& a) const {
33+
return a.x()*u() + a.y()*v() + a.z()*w();
34+
}
35+
2736
void build_from_w(const vec3&);
37+
38+
public:
2839
vec3 axis[3];
2940
};
3041

3142

3243
void onb::build_from_w(const vec3& n) {
3344
axis[2] = unit_vector(n);
34-
vec3 a;
35-
if (fabs(w().x()) > 0.9)
36-
a = vec3(0, 1, 0);
37-
else
38-
a = vec3(1, 0, 0);
45+
vec3 a = (fabs(w().x()) > 0.9) ? vec3(0,1,0) : vec3(1,0,0);
3946
axis[1] = unit_vector(cross(w(), a));
4047
axis[0] = cross(w(), v());
4148
}

0 commit comments

Comments
 (0)