Skip to content

Commit

Permalink
Fix up rtweekend.h introduction and use
Browse files Browse the repository at this point in the history
Resolves #1473
  • Loading branch information
hollasch committed Mar 29, 2024
1 parent ea3754c commit aa45681
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 36 deletions.
69 changes: 65 additions & 4 deletions books/RayTracingInOneWeekend.html
Original file line number Diff line number Diff line change
Expand Up @@ -1391,19 +1391,20 @@
We need some math constants that we conveniently define in their own header file. For now we only
need infinity, but we will also throw our own definition of pi in there, which we will need later.
There is no standard portable definition of pi, so we just define our own constant for it. We'll
throw common useful constants and future utility functions in `rtweekend.h`, our general main header
file.
also throw common useful constants and future utility functions in `rtweekend.h`, our general main
header file.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
#ifndef RTWEEKEND_H
#define RTWEEKEND_H

#include <cmath>
#include <iostream>
#include <limits>
#include <memory>


// Usings
// C++ Std Usings

using std::shared_ptr;
using std::make_shared;
Expand All @@ -1429,8 +1430,56 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [rtweekend-initial]: <kbd>[rtweekend.h]</kbd> The rtweekend.h common header]

For all main program files, we will include `rtweekend.h` first, so most other header files (where
the bulk of our code will reside) can assume that they are free to assume that these definitions are
already available. (Headers included inside `rtweekend.h` still need to include any of their
dependencies.) Because of this, we'll make some updates with this assumption in mind.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ delete
#include "vec3.h"

#include <iostream>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [assume-rtw-color]: <kbd>[color.h]</kbd> Assume rtweekend.h in color.h]


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ delete
#include "ray.h"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [assume-rtw-hittable]: <kbd>[hittable.h]</kbd> Assume rtweekend.h in hittable.h]


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ delete
#include <memory>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
#include <vector>


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ delete
using std::shared_ptr;
using std::make_shared;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [assume-rtw-hittable-list]: <kbd>[hittable_list.h]</kbd>
Assume rtweekend.h in hittable_list.h
]


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ delete
#include "vec3.h"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [assume-rtw-sphere]: <kbd>[sphere.h]</kbd> Assume rtweekend.h in sphere.h]


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ delete
#include <cmath>
#include <iostream>

using std::sqrt;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [assume-rtw-vec3]: <kbd>[vec3.h]</kbd> Assume rtweekend.h in vec3.h]

<div class='together'>
And the new main:
And now the new main:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
#include "rtweekend.h"
Expand All @@ -1443,7 +1492,10 @@
#include "sphere.h"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ delete
#include <iostream>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ delete
Expand Down Expand Up @@ -2884,6 +2936,15 @@
In service of this, we'll create a new vector method -- `vec3::near_zero()` -- that returns true if
the vector is very close to zero in all dimensions.

We'll need to use the C++ standard library function `std::fabs`, which returns the absolute value of
its input. We'll add this to `rtweekend.h` since we'll need this in several locations as we proceed.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
XXXX
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [declare-fabs]: <kbd>rtweekend.h</kbd> Declaring std::fabs()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
class vec3 {
...
Expand Down
2 changes: 0 additions & 2 deletions books/RayTracingTheNextWeek.html
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,6 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
#include "hittable.h"

#include <memory>
#include <vector>

class hittable_list : public hittable {
Expand Down Expand Up @@ -2484,7 +2483,6 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [perlin-turb]: <kbd>[perlin.h]</kbd> Turbulence function]

Here `fabs()` is the absolute value function defined in `<cmath>`.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
class noise_texture : public texture {
Expand Down
4 changes: 0 additions & 4 deletions src/InOneWeekend/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
// along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//==============================================================================================

#include "vec3.h"

#include <iostream>

using color = vec3;

inline double linear_to_gamma(double linear_component)
Expand Down
1 change: 0 additions & 1 deletion src/InOneWeekend/hittable_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#include "hittable.h"

#include <memory>
#include <vector>


Expand Down
3 changes: 2 additions & 1 deletion src/InOneWeekend/rtweekend.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <memory>


// Usings
// C++ Std Usings

using std::shared_ptr;
using std::make_shared;
Expand Down
4 changes: 0 additions & 4 deletions src/InOneWeekend/vec3.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
// along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//==============================================================================================

#include <cmath>
#include <iostream>

using std::sqrt;
using std::fabs;

class vec3 {
Expand Down
4 changes: 0 additions & 4 deletions src/TheNextWeek/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
// along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//==============================================================================================

#include "vec3.h"

#include <iostream>

using color = vec3;

inline double linear_to_gamma(double linear_component)
Expand Down
1 change: 0 additions & 1 deletion src/TheNextWeek/hittable_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "aabb.h"
#include "hittable.h"

#include <memory>
#include <vector>


Expand Down
3 changes: 2 additions & 1 deletion src/TheNextWeek/rtweekend.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <memory>


// Usings
// C++ Std Usings

using std::shared_ptr;
using std::make_shared;
Expand Down
4 changes: 0 additions & 4 deletions src/TheNextWeek/vec3.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
// along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//==============================================================================================

#include <cmath>
#include <iostream>

using std::sqrt;
using std::fabs;

class vec3 {
Expand Down
4 changes: 0 additions & 4 deletions src/TheRestOfYourLife/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
// along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//==============================================================================================

#include "vec3.h"

#include <iostream>

using color = vec3;

inline double linear_to_gamma(double linear_component)
Expand Down
1 change: 0 additions & 1 deletion src/TheRestOfYourLife/hittable_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "aabb.h"
#include "hittable.h"

#include <memory>
#include <vector>


Expand Down
3 changes: 2 additions & 1 deletion src/TheRestOfYourLife/rtweekend.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <memory>


// Usings
// C++ Std Usings

using std::shared_ptr;
using std::make_shared;
Expand Down
4 changes: 0 additions & 4 deletions src/TheRestOfYourLife/vec3.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
// along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//==============================================================================================

#include <cmath>
#include <iostream>

using std::sqrt;
using std::fabs;

class vec3 {
Expand Down

0 comments on commit aa45681

Please sign in to comment.