Skip to content

Commit e0a3bd5

Browse files
authored
Merge pull request #483 from RayTracing/math-notation-2
Math notation 2
2 parents c080b2a + 70b1aa9 commit e0a3bd5

File tree

3 files changed

+144
-146
lines changed

3 files changed

+144
-146
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -391,19 +391,19 @@
391391
--------------
392392
<div class='together'>
393393
The one thing that all ray tracers have is a ray class, and a computation of what color is seen
394-
along a ray. Let’s think of a ray as a function $\mathbf{p}(t) = \mathbf{a} + t \vec{\mathbf{b}}$.
395-
Here $\mathbf{p}$ is a 3D position along a line in 3D. $\mathbf{a}$ is the ray origin and
396-
$\vec{\mathbf{b}}$ is the ray direction. The ray parameter $t$ is a real number (`double` in the
397-
code). Plug in a different $t$ and $p(t)$ moves the point along the ray. Add in negative $t$ and you
398-
can go anywhere on the 3D line. For positive $t$, you get only the parts in front of $\mathbf{a}$,
399-
and this is what is often called a half-line or ray.
394+
along a ray. Let’s think of a ray as a function $\mathbf{P}(t) = \mathbf{A} + t \mathbf{b}$. Here
395+
$\mathbf{P}$ is a 3D position along a line in 3D. $\mathbf{A}$ is the ray origin and $\mathbf{b}$ is
396+
the ray direction. The ray parameter $t$ is a real number (`double` in the code). Plug in a
397+
different $t$ and $\mathbf{P}(t)$ moves the point along the ray. Add in negative $t$ and you can go
398+
anywhere on the 3D line. For positive $t$, you get only the parts in front of $\mathbf{A}$, and this
399+
is what is often called a half-line or ray.
400400

401401
![Figure [lerp]: Linear interpolation](../images/fig.lerp.jpg)
402402

403403
</div>
404404

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

408408
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
409409
#ifndef RAY_H
@@ -529,66 +529,66 @@
529529
Adding a Sphere
530530
====================================================================================================
531531

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

536535

537536
Ray-Sphere Intersection
538537
------------------------
538+
<div class='together'>
539539
Recall that the equation for a sphere centered at the origin of radius $R$ is $x^2 + y^2 + z^2 =
540540
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
541541
the given point $(x,y,z)$ is _inside_ the sphere, then $x^2 + y^2 + z^2 < R^2$, and if a given point
542542
$(x,y,z)$ is _outside_ the sphere, then $x^2 + y^2 + z^2 > R^2$.
543543

544-
It gets uglier if the sphere center is at $(\mathbf{c}_x, \mathbf{c}_y, \mathbf{c}_z)$:
544+
It gets uglier if the sphere center is at $(C_x, C_y, C_z)$:
545545

546-
$$ (x-\mathbf{c}_x)^2 + (y-\mathbf{c}_y)^2 + (z-\mathbf{c}_z)^2 = R^2 $$
546+
$$ (x - C_x)^2 + (y - C_y)^2 + (z - C_z)^2 = r^2 $$
547547
</div>
548548

549549
<div class='together'>
550550
In graphics, you almost always want your formulas to be in terms of vectors so all the x/y/z stuff
551551
is under the hood in the `vec3` class. You might note that the vector from center
552-
$\mathbf{c} = (\mathbf{c}_x,\mathbf{c}_y,\mathbf{c}_z)$ to point $\mathbf{P} = (x,y,z)$ is
553-
$(\mathbf{p} - \mathbf{c})$, and therefore
552+
$\mathbf{C} = (C_x,C_y,C_z)$ to point $\mathbf{P} = (x,y,z)$ is $(\mathbf{P} - \mathbf{C})$, and
553+
therefore
554554

555-
$$ (\mathbf{p} - \mathbf{c}) \cdot (\mathbf{p} - \mathbf{c})
556-
= (x-\mathbf{c}_x)^2 + (y-\mathbf{c}_y)^2 + (z-\mathbf{c}_z)^2
555+
$$ (\mathbf{P} - \mathbf{C}) \cdot (\mathbf{P} - \mathbf{C})
556+
= (x - C_x)^2 + (y - C_y)^2 + (z - C_z)^2
557557
$$
558558
</div>
559559

560560
<div class='together'>
561561
So the equation of the sphere in vector form is:
562562

563-
$$ (\mathbf{p} - \mathbf{c}) \cdot (\mathbf{p} - \mathbf{c}) = R^2 $$
563+
$$ (\mathbf{P} - \mathbf{C}) \cdot (\mathbf{P} - \mathbf{C}) = r^2 $$
564564
</div>
565565

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

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

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

576-
$$ (\mathbf{a} + t \vec{\mathbf{b}} - \mathbf{c})
577-
\cdot (\mathbf{a} + t \vec{\mathbf{b}} - \mathbf{c}) = R^2 $$
576+
$$ (\mathbf{A} + t \mathbf{b} - \mathbf{C})
577+
\cdot (\mathbf{A} + t \mathbf{b} - \mathbf{C}) = r^2 $$
578578
</div>
579579

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

584-
$$ t^2 \vec{\mathbf{b}}\cdot\vec{\mathbf{b}}
585-
+ 2t \vec{\mathbf{b}} \cdot \vec{(\mathbf{a}-\mathbf{c})}
586-
+ \vec{(\mathbf{a}-\mathbf{c})} \cdot \vec{(\mathbf{a}-\mathbf{c})} - R^2 = 0
584+
$$ t^2 \mathbf{b} \cdot \mathbf{b}
585+
+ 2t \mathbf{b} \cdot (\mathbf{A}-\mathbf{C})
586+
+ (\mathbf{A}-\mathbf{C}) \cdot (\mathbf{A}-\mathbf{C}) - r^2 = 0
587587
$$
588588
</div>
589589

590590
<div class='together'>
591-
The vectors and $R$ in that equation are all constant and known. The unknown is $t$, and the
591+
The vectors and $r$ in that equation are all constant and known. The unknown is $t$, and the
592592
equation is a quadratic, like you probably saw in your high school math class. You can solve for $t$
593593
and there is a square root part that is either positive (meaning two real solutions), negative
594594
(meaning no real solutions), or zero (meaning one real solution). In graphics, the algebra almost
@@ -669,11 +669,11 @@
669669
On the earth, this implies that the vector from the earth’s center to you points straight up. Let’s
670670
throw that into the code now, and shade it. We don’t have any lights or anything yet, so let’s just
671671
visualize the normals with a color map. A common trick used for visualizing normals (because it’s
672-
easy and somewhat intuitive to assume $\vec{\mathbf{N}}$ is a unit length vector -- so each
672+
easy and somewhat intuitive to assume $\mathbf{n}$ is a unit length vector -- so each
673673
component is between -1 and 1) is to map each component to the interval from 0 to 1, and then map
674674
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
675675
the closest hit point (smallest $t$). These changes in the code let us compute and visualize
676-
$\vec{\mathbf{N}}$:
676+
$\mathbf{n}$:
677677

678678
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
679679
double hit_sphere(const point3& center, double radius, const ray& r) {
@@ -1466,12 +1466,13 @@
14661466

14671467
<div class='together'>
14681468
There are two unit radius spheres tangent to the hit point $p$ of a surface. These two spheres have
1469-
a center of $(p + \vec{N})$ and $(p - \vec{N})$, where $\vec{N}$ is the normal of the surface. The
1470-
sphere with a center at $(p - \vec{N})$ is considered _inside_ the surface, whereas the sphere with
1471-
center $(p + \vec{N})$ is considered _outside_ the surface. Select the tangent unit radius sphere
1472-
that is on the same side of the surface as the ray origin. Pick a random point $s$ inside this unit
1473-
radius sphere and send a ray from the hit point $p$ to the random point $s$ (this is the vector
1474-
$(s-p)$):
1469+
a center of $(\mathbf{P} + \mathbf{n})$ and $(\mathbf{P} - \mathbf{n})$, where $\mathbf{n}$ is the
1470+
normal of the surface. The sphere with a center at $(\mathbf{P} - \mathbf{n})$ is considered
1471+
_inside_ the surface, whereas the sphere with center $(\mathbf{P} + \mathbf{n})$ is considered
1472+
_outside_ the surface. Select the tangent unit radius sphere that is on the same side of the surface
1473+
as the ray origin. Pick a random point $\mathbf{S}$ inside this unit radius sphere and send a ray
1474+
from the hit point $\mathbf{P}$ to the random point $\mathbf{S}$ (this is the vector
1475+
$(\mathbf{S}-\mathbf{P})$):
14751476

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

@@ -2020,10 +2021,9 @@
20202021
</div>
20212022

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

20282028
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
20292029
vec3 reflect(const vec3& v, const vec3& n) {
@@ -2256,32 +2256,32 @@
22562256
$$ \sin\theta' = \frac{\eta}{\eta'} \cdot \sin\theta $$
22572257

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

22622262
$$ \mathbf{R'} = \mathbf{R'}_{\parallel} + \mathbf{R'}_{\bot} $$
22632263

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

2266-
$$ \mathbf{R'}_{\parallel} = \frac{\eta}{\eta'} (\mathbf{R} + \cos\theta \mathbf{N}) $$
2267-
$$ \mathbf{R'}_{\bot} = -\sqrt{1 - |\mathbf{R'}_{\parallel}|^2} \mathbf{N} $$
2266+
$$ \mathbf{R'}_{\parallel} = \frac{\eta}{\eta'} (\mathbf{R} + \cos\theta \mathbf{n}) $$
2267+
$$ \mathbf{R'}_{\bot} = -\sqrt{1 - |\mathbf{R'}_{\parallel}|^2} \mathbf{n} $$
22682268

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

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

2275-
$$ \mathbf{A} \cdot \mathbf{B} = |\mathbf{A}| |\mathbf{B}| \cos\theta $$
2275+
$$ \mathbf{a} \cdot \mathbf{b} = |\mathbf{a}| |\mathbf{b}| \cos\theta $$
22762276

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

2279-
$$ \mathbf{A} \cdot \mathbf{B} = \cos\theta $$
2279+
$$ \mathbf{a} \cdot \mathbf{b} = \cos\theta $$
22802280

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

22832283
$$ \mathbf{R'}_{\parallel} =
2284-
\frac{\eta}{\eta'} (\mathbf{R} + (\mathbf{-R} \cdot \mathbf{N}) \mathbf{N}) $$
2284+
\frac{\eta}{\eta'} (\mathbf{R} + (\mathbf{-R} \cdot \mathbf{n}) \mathbf{n}) $$
22852285

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

@@ -2382,7 +2382,7 @@
23822382

23832383
and
23842384

2385-
$$ \cos\theta = \mathbf{R} \cdot \mathbf{N} $$
2385+
$$ \cos\theta = \mathbf{R} \cdot \mathbf{n} $$
23862386

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

0 commit comments

Comments
 (0)