Skip to content

Commit b0ec330

Browse files
committed
quad: rename quad member variables
- corner -> Q - side_A -> u - side_B -> v Also moved hit_ab() method to follow the hit() method.
1 parent 16c7621 commit b0ec330

File tree

3 files changed

+69
-69
lines changed

3 files changed

+69
-69
lines changed

books/RayTracingTheNextWeek.html

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2488,35 +2488,23 @@
24882488

24892489
class quad : public hittable {
24902490
public:
2491-
quad(const point3& _corner, const vec3& _sideA, const vec3& _sideB, shared_ptr<material> m)
2492-
: corner(_corner), side_A(_sideA), side_B(_sideB), mat(m)
2491+
quad(const point3& _Q, const vec3& _u, const vec3& _v, shared_ptr<material> m)
2492+
: Q(_Q), u(_u), v(_v), mat(m)
24932493
{
2494-
auto n = cross(side_A, side_B);
2494+
auto n = cross(u, v);
24952495
normal = unit_vector(n);
2496-
D = -dot(normal, corner);
2496+
D = -dot(normal, Q);
24972497
w = n / dot(n,n);
24982498

24992499
set_bounding_box();
25002500
}
25012501

25022502
virtual void set_bounding_box() {
2503-
bbox = aabb(corner, corner + side_A + side_B).pad();
2503+
bbox = aabb(Q, Q + u + v).pad();
25042504
}
25052505

25062506
aabb bounding_box() const override { return bbox; }
25072507

2508-
virtual bool hit_ab(double a, double b, hit_record& rec) const {
2509-
// Given the hit point in plane coordinates, return false if it is outside the
2510-
// primitive, otherwise set the hit record UV coordinates and return true.
2511-
2512-
if ((a < 0) || (1 < a) || (b < 0) || (1 < b))
2513-
return false;
2514-
2515-
rec.u = a;
2516-
rec.v = b;
2517-
return true;
2518-
}
2519-
25202508
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
25212509
auto denom = dot(normal, r.direction());
25222510

@@ -2531,9 +2519,9 @@
25312519

25322520
// Determine the hit point lies within the planar shape using its plane coordinates.
25332521
auto intersection = r.at(t);
2534-
vec3 planar_hitpt_vector = intersection - corner;
2535-
auto a = dot(w, cross(planar_hitpt_vector, side_B));
2536-
auto b = dot(w, cross(side_A, planar_hitpt_vector));
2522+
vec3 planar_hitpt_vector = intersection - Q;
2523+
auto a = dot(w, cross(planar_hitpt_vector, v));
2524+
auto b = dot(w, cross(u, planar_hitpt_vector));
25372525

25382526
if (!hit_ab(a, b, rec))
25392527
return false;
@@ -2547,9 +2535,21 @@
25472535
return true;
25482536
}
25492537

2538+
virtual bool hit_ab(double a, double b, hit_record& rec) const {
2539+
// Given the hit point in plane coordinates, return false if it is outside the
2540+
// primitive, otherwise set the hit record UV coordinates and return true.
2541+
2542+
if ((a < 0) || (1 < a) || (b < 0) || (1 < b))
2543+
return false;
2544+
2545+
rec.u = a;
2546+
rec.v = b;
2547+
return true;
2548+
}
2549+
25502550
protected:
2551-
point3 corner;
2552-
vec3 side_A, side_B;
2551+
point3 Q;
2552+
vec3 u, v;
25532553
shared_ptr<material> mat;
25542554
vec3 normal;
25552555
double D;

src/TheNextWeek/quad.h

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,23 @@
1818

1919
class quad : public hittable {
2020
public:
21-
quad(const point3& _corner, const vec3& _sideA, const vec3& _sideB, shared_ptr<material> m)
22-
: corner(_corner), side_A(_sideA), side_B(_sideB), mat(m)
21+
quad(const point3& _Q, const vec3& _u, const vec3& _v, shared_ptr<material> m)
22+
: Q(_Q), u(_u), v(_v), mat(m)
2323
{
24-
auto n = cross(side_A, side_B);
24+
auto n = cross(u, v);
2525
normal = unit_vector(n);
26-
D = -dot(normal, corner);
26+
D = -dot(normal, Q);
2727
w = n / dot(n,n);
2828

2929
set_bounding_box();
3030
}
3131

3232
virtual void set_bounding_box() {
33-
bbox = aabb(corner, corner + side_A + side_B).pad();
33+
bbox = aabb(Q, Q + u + v).pad();
3434
}
3535

3636
aabb bounding_box() const override { return bbox; }
3737

38-
virtual bool hit_ab(double a, double b, hit_record& rec) const {
39-
// Given the hit point in plane coordinates, return false if it is outside the
40-
// primitive, otherwise set the hit record UV coordinates and return true.
41-
42-
if ((a < 0) || (1 < a) || (b < 0) || (1 < b))
43-
return false;
44-
45-
rec.u = a;
46-
rec.v = b;
47-
return true;
48-
}
49-
5038
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
5139
auto denom = dot(normal, r.direction());
5240

@@ -61,11 +49,11 @@ class quad : public hittable {
6149

6250
// Determine the hit point lies within the planar shape using its plane coordinates.
6351
auto intersection = r.at(t);
64-
vec3 planar_hitpt_vector = intersection - corner;
65-
auto a = dot(w, cross(planar_hitpt_vector, side_B));
66-
auto b = dot(w, cross(side_A, planar_hitpt_vector));
52+
vec3 planar_hitpt_vector = intersection - Q;
53+
auto alpha = dot(w, cross(planar_hitpt_vector, v));
54+
auto beta = dot(w, cross(u, planar_hitpt_vector));
6755

68-
if (!hit_ab(a, b, rec))
56+
if (!hit_ab(alpha, beta, rec))
6957
return false;
7058

7159
// Ray hits the 2D shape; set the rest of the hit record and return true.
@@ -77,9 +65,21 @@ class quad : public hittable {
7765
return true;
7866
}
7967

68+
virtual bool hit_ab(double a, double b, hit_record& rec) const {
69+
// Given the hit point in plane coordinates, return false if it is outside the
70+
// primitive, otherwise set the hit record UV coordinates and return true.
71+
72+
if ((a < 0) || (1 < a) || (b < 0) || (1 < b))
73+
return false;
74+
75+
rec.u = a;
76+
rec.v = b;
77+
return true;
78+
}
79+
8080
protected:
81-
point3 corner;
82-
vec3 side_A, side_B;
81+
point3 Q;
82+
vec3 u, v;
8383
shared_ptr<material> mat;
8484
vec3 normal;
8585
double D;

src/TheRestOfYourLife/quad.h

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
class quad : public hittable {
2020
public:
21-
quad(const point3& _corner, const vec3& _sideA, const vec3& _sideB, shared_ptr<material> m)
22-
: corner(_corner), side_A(_sideA), side_B(_sideB), mat(m)
21+
quad(const point3& _Q, const vec3& _u, const vec3& _v, shared_ptr<material> m)
22+
: Q(_Q), u(_u), v(_v), mat(m)
2323
{
24-
auto n = cross(side_A, side_B);
24+
auto n = cross(u, v);
2525
normal = unit_vector(n);
26-
D = -dot(normal, corner);
26+
D = -dot(normal, Q);
2727
w = n / dot(n,n);
2828

2929
area = n.length();
@@ -32,23 +32,11 @@ class quad : public hittable {
3232
}
3333

3434
virtual void set_bounding_box() {
35-
bbox = aabb(corner, corner + side_A + side_B).pad();
35+
bbox = aabb(Q, Q + u + v).pad();
3636
}
3737

3838
aabb bounding_box() const override { return bbox; }
3939

40-
virtual bool hit_ab(double a, double b, hit_record& rec) const {
41-
// Given the hit point in plane coordinates, return false if it is outside the
42-
// primitive, otherwise set the hit record UV coordinates and return true.
43-
44-
if ((a < 0) || (1 < a) || (b < 0) || (1 < b))
45-
return false;
46-
47-
rec.u = a;
48-
rec.v = b;
49-
return true;
50-
}
51-
5240
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
5341
auto denom = dot(normal, r.direction());
5442

@@ -63,11 +51,11 @@ class quad : public hittable {
6351

6452
// Determine the hit point lies within the planar shape using its plane coordinates.
6553
auto intersection = r.at(t);
66-
vec3 planar_hitpt_vector = intersection - corner;
67-
auto a = dot(w, cross(planar_hitpt_vector, side_B));
68-
auto b = dot(w, cross(side_A, planar_hitpt_vector));
54+
vec3 planar_hitpt_vector = intersection - Q;
55+
auto alpha = dot(w, cross(planar_hitpt_vector, v));
56+
auto beta = dot(w, cross(u, planar_hitpt_vector));
6957

70-
if (!hit_ab(a, b, rec))
58+
if (!hit_ab(alpha, beta, rec))
7159
return false;
7260

7361
// Ray hits the 2D shape; set the rest of the hit record and return true.
@@ -79,6 +67,18 @@ class quad : public hittable {
7967
return true;
8068
}
8169

70+
virtual bool hit_ab(double a, double b, hit_record& rec) const {
71+
// Given the hit point in plane coordinates, return false if it is outside the
72+
// primitive, otherwise set the hit record UV coordinates and return true.
73+
74+
if ((a < 0) || (1 < a) || (b < 0) || (1 < b))
75+
return false;
76+
77+
rec.u = a;
78+
rec.v = b;
79+
return true;
80+
}
81+
8282
double pdf_value(const point3& origin, const vec3& v) const override {
8383
hit_record rec;
8484
if (!this->hit(ray(origin, v), interval(0.001, infinity), rec))
@@ -91,13 +91,13 @@ class quad : public hittable {
9191
}
9292

9393
vec3 random(const point3& origin) const override {
94-
auto p = plane_origin + (random_double() * axis_A) + (random_double() * axis_B);
94+
auto p = Q + (random_double() * u) + (random_double() * v);
9595
return p - origin;
9696
}
9797

9898
protected:
99-
point3 corner;
100-
vec3 side_A, side_B;
99+
point3 Q;
100+
vec3 u, v;
101101
shared_ptr<material> mat;
102102
vec3 normal;
103103
double D;

0 commit comments

Comments
 (0)