Skip to content

Commit 871b466

Browse files
committed
Final round of review feedback for this chain.
- Document new interval::expand() method - Wording revisions - Fixed up some improper math styling - Added clarifying explanation about inability to divide by vectors, and solving for alpha and beta of planar coordinates. - Added missing aabb::pad() function. - Tweak to interval::pad() method.
1 parent 01727d3 commit 871b466

File tree

3 files changed

+73
-25
lines changed

3 files changed

+73
-25
lines changed

books/RayTracingTheNextWeek.html

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,25 @@
594594
<div class='together'>
595595
If there are any `NaN`s running around there, the compare will return false so we need to be sure
596596
our bounding boxes have a little padding if we care about grazing cases (and we probably should
597-
because in a ray tracer all cases come up eventually). With all three dimensions in a loop, and
598-
passing in the interval $[t_{min}$, $t_{max}]$, we get:
597+
because in a ray tracer all cases come up eventually). Here's the implementation:
598+
599+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
600+
class interval {
601+
public:
602+
...
603+
double size() const {
604+
return max - min;
605+
}
606+
607+
interval expand(double delta) const {
608+
const auto padding = delta/2;
609+
return interval(min - padding, max + padding);
610+
}
611+
...
612+
};
613+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
614+
[Listing [interval-contains]: <kbd>interval.h</kbd> interval::expand() method]
615+
599616

600617
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
601618
#ifndef AABB_H
@@ -618,6 +635,12 @@
618635
z = interval(fmin(a[2],b[2]), fmax(a[2],b[2]));
619636
}
620637

638+
const interval& axis(int n) const {
639+
if (n == 1) return y;
640+
if (n == 2) return z;
641+
return x;
642+
}
643+
621644
bool hit(const ray& r, interval ray_t) const {
622645
for (int a = 0; a < 3; a++) {
623646
auto t0 = fmin((axis(a).min - r.origin()[a]) / r.direction()[a],
@@ -632,12 +655,6 @@
632655
return true;
633656
}
634657

635-
const interval& axis(int n) const {
636-
if (n == 1) return y;
637-
if (n == 2) return z;
638-
return x;
639-
}
640-
641658
public:
642659
interval x, y, z;
643660
};
@@ -2273,7 +2290,7 @@
22732290

22742291
$$ Ax + By + Cz + D = 0 $$
22752292

2276-
where $A,B,C,D$ are just numbers, and $x,y,z$ are the values of a point $\mathbf{P} = (x,y,z)$.
2293+
where $A,B,C,D$ are just constants, and $x,y,z$ are the values of a point $\mathbf{P} = (x,y,z)$.
22772294
That is, a plane is the set of all points $\mathbf{P} = (x,y,z)$ that satisfy the formula above. It
22782295
makes things slightly easier to use the alternate formulation:
22792296

@@ -2312,7 +2329,7 @@
23122329
All right, we can find the point of intersection between a ray and a plane. This will help us
23132330
determine whether a ray hits a given quadrilateral, using the plane that contains the (planar)
23142331
quadrilateral. In fact, we can use this approach to define any planar primitive, like triangles and
2315-
disks (more later).
2332+
disks (more on that later).
23162333

23172334
We now have three questions we must answer:
23182335

@@ -2335,7 +2352,8 @@
23352352

23362353
These values are three-dimensional, even though a quad itself is a two-dimensional object. For
23372354
example, a quad with corner at the origin and extending two units in the Z direction and one unit in
2338-
the Y direction would have $Q = (0,0,0), u = (0,0,2), \text{and } v = (0,1,0)$.
2355+
the Y direction would have $\mathbf{Q} = (0,0,0), \mathbf{u} = (0,0,2), \text{and } \mathbf{v} =
2356+
(0,1,0)$.
23392357

23402358
The following figure illustrates the quadrilateral components.
23412359

@@ -2348,20 +2366,21 @@
23482366
three of these values (and thus the quad itself).
23492367

23502368
Fortunately, this is very simple. Recall that in $Ax + By + Cz = D$, $(A,B,C)$ represents the
2351-
normal vector. To get this, we just use the cross product of the two side vectors $u$ and $v$:
2369+
normal vector. To get this, we just use the cross product of the two side vectors $\mathbf{u}$ and
2370+
$\mathbf{v}$:
23522371

23532372
$$ \mathbf{n} = \text{unit_vector}(\mathbf{u} \times \mathbf{v}) $$
23542373

23552374
The plane is defined as all points $(x,y,z)$ that satisfy the equation $Ax + By + Cz = D$. Well, we
23562375
know that $\mathbf{Q}$ lies on the plane, so we can use that to find $D$:
23572376

23582377
$$ \begin{align*}
2359-
D &= \mathbf{n}_x\mathbf{Q}_x + \mathbf{n}_y\mathbf{Q}_y + \mathbf{n}_z\mathbf{Q}_z \\
2360-
&= n \cdot \mathbf{Q} \\
2378+
D &= n_x Q_x + n_y Q_y + n_z Q_z \\
2379+
&= \mathbf{n} \cdot \mathbf{Q} \\
23612380
\end{align*}
23622381
$$
23632382

2364-
We can now use these two values $\mathbf{n}$ and $D$ to find the point of intersection with a given
2383+
We can now use the two values $\mathbf{n}$ and $D$ to find the point of intersection between a given
23652384
ray and the plane containing the quadrilateral.
23662385

23672386

@@ -2427,9 +2446,11 @@
24272446
$$ \mathbf{u} \times \mathbf{p} = \beta(\mathbf{u} \times \mathbf{v}) $$
24282447
$$ \mathbf{v} \times \mathbf{p} = \alpha(\mathbf{v} \times \mathbf{u}) $$
24292448

2430-
To solve for the coefficients $\alpha$ and $\beta$, we can take the dot product of both sides of the
2431-
above equations with the plane normal $\mathbf{n} = \mathbf{u} \times \mathbf{v}$, reducing both
2432-
sides to scalars.
2449+
Now to solve for the coefficients $\alpha$ and $\beta$. If you're new to vector math, you might try
2450+
to divide by $\mathbf{u} \times \mathbf{v}$ and $\mathbf{v} \times \mathbf{u}$, but you can't divide
2451+
by vectors. Instead, we can take the dot product of both sides of the above equations with the plane
2452+
normal $\mathbf{n} = \mathbf{u} \times \mathbf{v}$, reducing both sides to scalars, which we _can_
2453+
divide by.
24332454

24342455
$$ \mathbf{n} \cdot (\mathbf{u} \times \mathbf{p})
24352456
= \mathbf{n} \cdot \beta(\mathbf{u} \times \mathbf{v}) $$
@@ -2484,6 +2505,32 @@
24842505

24852506
That's the last piece needed to implement quadrilateral primitives.
24862507

2508+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2509+
...
2510+
class aabb {
2511+
public:
2512+
...
2513+
aabb(const aabb& box0, const aabb& box1) {
2514+
x = interval(box0.x, box1.x);
2515+
y = interval(box0.y, box1.y);
2516+
z = interval(box0.z, box1.z);
2517+
}
2518+
2519+
2520+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
2521+
aabb pad() {
2522+
// Return an AABB that has no side narrower than some delta, padding if necessary.
2523+
const double delta = 0.0001;
2524+
interval new_x = (x.size() >= delta) ? x : x.expand(delta);
2525+
interval new_y = (y.size() >= delta) ? y : y.expand(delta);
2526+
interval new_z = (z.size() >= delta) ? z : z.expand(delta);
2527+
2528+
return aabb(new_x, new_y, new_z);
2529+
}
2530+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2531+
[Listing [aabb]: <kbd>[aabb.h]</kbd> New aabb::pad() method]
2532+
2533+
24872534
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
24882535
#ifndef QUAD_H
24892536
#define QUAD_H

src/common/aabb.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ class aabb {
3535
z = interval(box0.z, box1.z);
3636
}
3737

38-
const interval& axis(int n) const {
39-
if (n == 1) return y;
40-
if (n == 2) return z;
41-
return x;
42-
}
43-
4438
aabb pad() {
4539
// Return an AABB that has no side narrower than some delta, padding if necessary.
4640
const double delta = 0.0001;
@@ -51,6 +45,12 @@ class aabb {
5145
return aabb(new_x, new_y, new_z);
5246
}
5347

48+
const interval& axis(int n) const {
49+
if (n == 1) return y;
50+
if (n == 2) return z;
51+
return x;
52+
}
53+
5454
#if 1
5555
// GitHub Issue #817
5656
// For some reason I haven't figured out yet, this version is 10x faster than the

src/common/interval.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class interval {
2323
}
2424

2525
interval expand(double delta) const {
26-
return interval(min - delta/2, max + delta/2);
26+
const auto padding = delta/2;
27+
return interval(min - padding, max + padding);
2728
}
2829

2930
bool contains(double x) const {

0 commit comments

Comments
 (0)