Skip to content

Commit b4ffe6f

Browse files
committed
Math notation update: book 1
Points are uppercase bold, vectors are lowercase bold, no barb.
1 parent e9f5d03 commit b4ffe6f

File tree

1 file changed

+49
-48
lines changed

1 file changed

+49
-48
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -386,19 +386,20 @@
386386
--------------
387387
<div class='together'>
388388
The one thing that all ray tracers have is a ray class, and a computation of what color is seen
389-
along a ray. Let’s think of a ray as a function $\mathbf{p}(t) = \mathbf{a} + t \vec{\mathbf{b}}$.
390-
Here $\mathbf{p}$ is a 3D position along a line in 3D. $\mathbf{a}$ is the ray origin and
391-
$\vec{\mathbf{b}}$ is the ray direction. The ray parameter $t$ is a real number (`double` in the
392-
code). Plug in a different $t$ and $p(t)$ moves the point along the ray. Add in negative $t$ and you
393-
can go anywhere on the 3D line. For positive $t$, you get only the parts in front of $\mathbf{a}$,
394-
and this is what is often called a half-line or ray.
389+
along a ray. Let’s think of a ray as a function $\mathbf{P}(t) = \mathbf{A} + t \mathbf{b}$. (Note:
390+
throughout these books, we'll use uppercase bold letters for points, and lowercase bold letters for
391+
vectors.) Here $\mathbf{P}$ is a 3D position along a line in 3D. $\mathbf{A}$ is the ray origin and
392+
$\mathbf{b}$ is the ray direction. The ray parameter $t$ is a real number (`double` in the
393+
code). Plug in a different $t$ and $\mathbf{P}(t)$ moves the point along the ray. Add in negative
394+
$t$ and you can go anywhere on the 3D line. For positive $t$, you get only the parts in front of
395+
$\mathbf{A}$, and this is what is often called a half-line or ray.
395396

396397
![Figure [lerp]: Linear interpolation](../images/fig.lerp.jpg)
397398

398399
</div>
399400

400401
<div class='together'>
401-
The function $p(t)$ in more verbose code form I call `ray::at(t)`:
402+
The function $\mathbf{P}(t)$ in more verbose code form I call `ray::at(t)`:
402403

403404
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
404405
#ifndef RAY_H
@@ -524,66 +525,66 @@
524525
Adding a Sphere
525526
====================================================================================================
526527

527-
<div class='together'>
528528
Let’s add a single object to our ray tracer. People often use spheres in ray tracers because
529529
calculating whether a ray hits a sphere is pretty straightforward.
530530

531531

532532
Ray-Sphere Intersection
533533
------------------------
534+
<div class='together'>
534535
Recall that the equation for a sphere centered at the origin of radius $R$ is $x^2 + y^2 + z^2 =
535536
R^2$. Put another way, if a given point $(x,y,z)$ is on the sphere, then $x^2 + y^2 + z^2 = R^2$. If
536537
the given point $(x,y,z)$ is _inside_ the sphere, then $x^2 + y^2 + z^2 < R^2$, and if a given point
537538
$(x,y,z)$ is _outside_ the sphere, then $x^2 + y^2 + z^2 > R^2$.
538539

539-
It gets uglier if the sphere center is at $(\mathbf{c}_x, \mathbf{c}_y, \mathbf{c}_z)$:
540+
It gets uglier if the sphere center is at $(\mathbf{C}_x, \mathbf{C}_y, \mathbf{C}_z)$:
540541

541-
$$ (x-\mathbf{c}_x)^2 + (y-\mathbf{c}_y)^2 + (z-\mathbf{c}_z)^2 = R^2 $$
542+
$$ (x-\mathbf{C}_x)^2 + (y-\mathbf{C}_y)^2 + (z-\mathbf{C}_z)^2 = r^2 $$
542543
</div>
543544

544545
<div class='together'>
545546
In graphics, you almost always want your formulas to be in terms of vectors so all the x/y/z stuff
546547
is under the hood in the `vec3` class. You might note that the vector from center
547-
$\mathbf{c} = (\mathbf{c}_x,\mathbf{c}_y,\mathbf{c}_z)$ to point $\mathbf{P} = (x,y,z)$ is
548-
$(\mathbf{p} - \mathbf{c})$, and therefore
548+
$\mathbf{C} = (\mathbf{C}_x,\mathbf{C}_y,\mathbf{C}_z)$ to point $\mathbf{P} = (x,y,z)$ is
549+
$(\mathbf{P} - \mathbf{C})$, and therefore
549550

550-
$$ (\mathbf{p} - \mathbf{c}) \cdot (\mathbf{p} - \mathbf{c})
551-
= (x-\mathbf{c}_x)^2 + (y-\mathbf{c}_y)^2 + (z-\mathbf{c}_z)^2
551+
$$ (\mathbf{P} - \mathbf{C}) \cdot (\mathbf{P} - \mathbf{C})
552+
= (x-\mathbf{C}_x)^2 + (y-\mathbf{C}_y)^2 + (z-\mathbf{C}_z)^2
552553
$$
553554
</div>
554555

555556
<div class='together'>
556557
So the equation of the sphere in vector form is:
557558

558-
$$ (\mathbf{p} - \mathbf{c}) \cdot (\mathbf{p} - \mathbf{c}) = R^2 $$
559+
$$ (\mathbf{P} - \mathbf{C}) \cdot (\mathbf{P} - \mathbf{C}) = r^2 $$
559560
</div>
560561

561562
<div class='together'>
562-
We can read this as “any point $\mathbf{p}$ that satisfies this equation is on the sphere”. We want
563-
to know if our ray $p(t) = \mathbf{a} + t\vec{\mathbf{b}}$ ever hits the sphere anywhere. If it does
564-
hit the sphere, there is some $t$ for which $p(t)$ satisfies the sphere equation. So we are looking
565-
for any $t$ where this is true:
563+
We can read this as “any point $\mathbf{P}$ that satisfies this equation is on the sphere”. We want
564+
to know if our ray $\mathbf{P}(t) = \mathbf{A} + t\mathbf{b}$ ever hits the sphere anywhere. If it
565+
does hit the sphere, there is some $t$ for which $\mathbf{P}(t)$ satisfies the sphere equation. So
566+
we are looking for any $t$ where this is true:
566567

567-
$$ (p(t) - \mathbf{c})\cdot(p(t) - \mathbf{c}) = R^2 $$
568+
$$ (\mathbf{P}(t) - \mathbf{C})\cdot(\mathbf{P}(t) - \mathbf{C}) = r^2 $$
568569

569-
or expanding the full form of the ray $p(t)$:
570+
or expanding the full form of the ray $\mathbf{P}(t)$:
570571

571-
$$ (\mathbf{a} + t \vec{\mathbf{b}} - \mathbf{c})
572-
\cdot (\mathbf{a} + t \vec{\mathbf{b}} - \mathbf{c}) = R^2 $$
572+
$$ (\mathbf{A} + t \mathbf{b} - \mathbf{C})
573+
\cdot (\mathbf{A} + t \mathbf{b} - \mathbf{C}) = r^2 $$
573574
</div>
574575

575576
<div class='together'>
576577
The rules of vector algebra are all that we would want here, and if we expand that equation and
577578
move all the terms to the left hand side we get:
578579

579-
$$ t^2 \vec{\mathbf{b}}\cdot\vec{\mathbf{b}}
580-
+ 2t \vec{\mathbf{b}} \cdot \vec{(\mathbf{a}-\mathbf{c})}
581-
+ \vec{(\mathbf{a}-\mathbf{c})} \cdot \vec{(\mathbf{a}-\mathbf{c})} - R^2 = 0
580+
$$ t^2 \mathbf{b} \cdot \mathbf{b}
581+
+ 2t \mathbf{b} \cdot (\mathbf{A}-\mathbf{C})
582+
+ (\mathbf{A}-\mathbf{C}) \cdot (\mathbf{A}-\mathbf{C}) - r^2 = 0
582583
$$
583584
</div>
584585

585586
<div class='together'>
586-
The vectors and $R$ in that equation are all constant and known. The unknown is $t$, and the
587+
The vectors and $r$ in that equation are all constant and known. The unknown is $t$, and the
587588
equation is a quadratic, like you probably saw in your high school math class. You can solve for $t$
588589
and there is a square root part that is either positive (meaning two real solutions), negative
589590
(meaning no real solutions), or zero (meaning one real solution). In graphics, the algebra almost
@@ -664,11 +665,11 @@
664665
On the earth, this implies that the vector from the earth’s center to you points straight up. Let’s
665666
throw that into the code now, and shade it. We don’t have any lights or anything yet, so let’s just
666667
visualize the normals with a color map. A common trick used for visualizing normals (because it’s
667-
easy and somewhat intuitive to assume $\vec{\mathbf{N}}$ is a unit length vector -- so each
668+
easy and somewhat intuitive to assume $\mathbf{n}$ is a unit length vector -- so each
668669
component is between -1 and 1) is to map each component to the interval from 0 to 1, and then map
669670
x/y/z to r/g/b. For the normal, we need the hit point, not just whether we hit or not. Let’s assume
670671
the closest hit point (smallest $t$). These changes in the code let us compute and visualize
671-
$\vec{\mathbf{N}}$:
672+
$\mathbf{n}$:
672673

673674
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
674675
double hit_sphere(const point3& center, double radius, const ray& r) {
@@ -1458,12 +1459,13 @@
14581459

14591460
<div class='together'>
14601461
There are two unit radius spheres tangent to the hit point $p$ of a surface. These two spheres have
1461-
a center of $(p + \vec{N})$ and $(p - \vec{N})$, where $\vec{N}$ is the normal of the surface. The
1462-
sphere with a center at $(p - \vec{N})$ is considered _inside_ the surface, whereas the sphere with
1463-
center $(p + \vec{N})$ is considered _outside_ the surface. Select the tangent unit radius sphere
1464-
that is on the same side of the surface as the ray origin. Pick a random point $s$ inside this unit
1465-
radius sphere and send a ray from the hit point $p$ to the random point $s$ (this is the vector
1466-
$(s-p)$):
1462+
a center of $(\mathbf{P} + \mathbf{n})$ and $(\mathbf{P} - \mathbf{n})$, where $\mathbf{n}$ is the
1463+
normal of the surface. The sphere with a center at $(\mathbf{P} - \mathbf{n})$ is considered
1464+
_inside_ the surface, whereas the sphere with center $(\mathbf{P} + \mathbf{n})$ is considered
1465+
_outside_ the surface. Select the tangent unit radius sphere that is on the same side of the surface
1466+
as the ray origin. Pick a random point $\mathbf{S}$ inside this unit radius sphere and send a ray
1467+
from the hit point $\mathbf{P}$ to the random point $\mathbf{S}$ (this is the vector
1468+
$(\mathbf{S}-\mathbf{P})$):
14671469

14681470
![Figure [rand-vector]: Generating a random diffuse bounce ray](../images/fig.rand-vector.jpg)
14691471

@@ -2002,10 +2004,9 @@
20022004
</div>
20032005

20042006
<div class='together'>
2005-
The reflected ray direction in red is just $\vec{\mathbf{V}} + 2\vec{\mathbf{B}}$. In our design,
2006-
$\vec{\mathbf{N}}$ is a unit vector, but $\vec{\mathbf{V}}$ may not be. The length of
2007-
$\vec{\mathbf{B}}$ should be $\vec{\mathbf{V}} \cdot \vec{\mathbf{N}}$. Because $\vec{\mathbf{V}}$
2008-
points in, we will need a minus sign, yielding:
2007+
The reflected ray direction in red is just $\mathbf{v} + 2\mathbf{b}$. In our design, $\mathbf{n}$
2008+
is a unit vector, but $\mathbf{v}$ may not be. The length of $\mathbf{b}$ should be $\mathbf{v}
2009+
\cdot \mathbf{n}$. Because $\mathbf{v}$ points in, we will need a minus sign, yielding:
20092010

20102011
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
20112012
vec3 reflect(const vec3& v, const vec3& n) {
@@ -2237,32 +2238,32 @@
22372238
$$ \sin\theta' = \frac{\eta}{\eta'} \cdot \sin\theta $$
22382239

22392240
On the refracted side of the surface there is a refracted ray $\mathbf{R'}$ and a normal
2240-
$\mathbf{N'}$, and there exists an angle, $\theta'$, between them. We can split $\mathbf{R'}$ into
2241-
the parts of the ray that are parallel to $\mathbf{N'}$ and perpendicular to $\mathbf{N'}$:
2241+
$\mathbf{n'}$, and there exists an angle, $\theta'$, between them. We can split $\mathbf{R'}$ into
2242+
the parts of the ray that are parallel to $\mathbf{n'}$ and perpendicular to $\mathbf{n'}$:
22422243

22432244
$$ \mathbf{R'} = \mathbf{R'}_{\parallel} + \mathbf{R'}_{\bot} $$
22442245

22452246
If we solve for $\mathbf{R'}_{\parallel}$ and $\mathbf{R'}_{\bot}$ we get:
22462247

2247-
$$ \mathbf{R'}_{\parallel} = \frac{\eta}{\eta'} (\mathbf{R} + \cos\theta \mathbf{N}) $$
2248-
$$ \mathbf{R'}_{\bot} = -\sqrt{1 - |\mathbf{R'}_{\parallel}|^2} \mathbf{N} $$
2248+
$$ \mathbf{R'}_{\parallel} = \frac{\eta}{\eta'} (\mathbf{R} + \cos\theta \mathbf{n}) $$
2249+
$$ \mathbf{R'}_{\bot} = -\sqrt{1 - |\mathbf{R'}_{\parallel}|^2} \mathbf{n} $$
22492250

22502251
You can go ahead and prove this for yourself if you want, but we will treat it as fact and move on.
22512252
The rest of the book will not require you to understand the proof.
22522253

22532254
We still need to solve for $\cos\theta$. It is well known that the dot product of two vectors can
22542255
be explained in terms of the cosine of the angle between them:
22552256

2256-
$$ \mathbf{A} \cdot \mathbf{B} = |\mathbf{A}| |\mathbf{B}| \cos\theta $$
2257+
$$ \mathbf{a} \cdot \mathbf{b} = |\mathbf{a}| |\mathbf{b}| \cos\theta $$
22572258

2258-
If we restrict $\mathbf{A}$ and $\mathbf{B}$ to be unit vectors:
2259+
If we restrict $\mathbf{a}$ and $\mathbf{b}$ to be unit vectors:
22592260

2260-
$$ \mathbf{A} \cdot \mathbf{B} = \cos\theta $$
2261+
$$ \mathbf{a} \cdot \mathbf{b} = \cos\theta $$
22612262

22622263
We can now rewrite $\mathbf{R'}_{\parallel}$ in terms of known quantities:
22632264

22642265
$$ \mathbf{R'}_{\parallel} =
2265-
\frac{\eta}{\eta'} (\mathbf{R} + (\mathbf{-R} \cdot \mathbf{N}) \mathbf{N}) $$
2266+
\frac{\eta}{\eta'} (\mathbf{R} + (\mathbf{-R} \cdot \mathbf{n}) \mathbf{n}) $$
22662267

22672268
When we combine them back together, we can write a function to calculate $\mathbf{R'}$:
22682269

@@ -2363,7 +2364,7 @@
23632364

23642365
and
23652366

2366-
$$ \cos\theta = \mathbf{R} \cdot \mathbf{N} $$
2367+
$$ \cos\theta = \mathbf{R} \cdot \mathbf{n} $$
23672368

23682369
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
23692370
double cos_theta = ffmin(dot(-unit_direction, rec.normal), 1.0);

0 commit comments

Comments
 (0)