Skip to content

Commit 683c5f9

Browse files
committed
Incorporate review feedback.
1 parent d1226ca commit 683c5f9

File tree

7 files changed

+34
-8
lines changed

7 files changed

+34
-8
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -915,8 +915,12 @@
915915
#define HITTABLE_LIST_H
916916

917917
#include "hittable.h"
918+
#include <memory>
918919
#include <vector>
919920

921+
using std::shared_ptr;
922+
using std::make_shared;
923+
920924
class hittable_list: public hittable {
921925
public:
922926
hittable_list() {}
@@ -954,8 +958,8 @@
954958

955959
## Some New C++ Features
956960

957-
This code uses two C++ features that may trip you up if you're not normally a C++ programmer:
958-
`vector` and `shared_ptr`.
961+
The `hittable_list` class code uses two C++ features that may trip you up if you're not normally a
962+
C++ programmer: `vector` and `shared_ptr`.
959963

960964
`shared_ptr<type>` is a pointer to some allocated type, with reference-counting semantics.
961965
Every time you assign its value to another shared pointer (usually with a simple assignment), the
@@ -975,17 +979,25 @@
975979
arguments `(1, true)`. It returns a `shared_ptr<thing>`. This can be simplified as in the second
976980
line with the `auto` type declaration, because the type is sufficiently defined by the return type
977981
of `make_shared<thing2>`.
982+
</div>
978983

979984
We'll use shared pointers in our code, because it allows multiple objects to use a common object
980985
(for example, a bunch of spheres that all use the same material), and because it makes memory
981986
management automatic and easier to reason about.
982-
</div>
987+
988+
`std::shared_ptr` is included with the `<memory>` header.
983989

984990
The second C++ feature you may be unfamiliar with is `std::vector`. This is a generic array-like
985991
collection of an arbitrary type. Above, we use a collection of pointers to `hittable`. `std::vector`
986992
automatically grows as more values are added: `objects.push_back(object)` adds a value to the end of
987993
the `std::vector` member variable `objects`.
988994

995+
`std::vector` is included with the `<vector>` header.
996+
997+
Finally, the `using` statements in listing [hittable-list-initial] tell the compiler that we'll be
998+
getting `shared_ptr` and `make_shared` from the `std` library, so we don't need to prefex these with
999+
`std::` every time we reference them.
1000+
9891001
## Common Constants and Utility Functions
9901002

9911003
<div class='together'></div>
@@ -999,8 +1011,16 @@
9991011
#ifndef RTWEEKEND_H
10001012
#define RTWEEKEND_H
10011013

1002-
#include <limits>
10031014
#include <cmath>
1015+
#include <cstdlib>
1016+
#include <limits>
1017+
#include <memory>
1018+
1019+
1020+
// Usings
1021+
1022+
using std::shared_ptr;
1023+
using std::make_shared;
10041024

10051025
// Constants
10061026

books/RayTracingTheNextWeek.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1078,9 +1078,11 @@
10781078
};
10791079

10801080
static double* perlin_generate() {
1081-
auto p = new double[perlin::point_count];
1081+
static double p[perlin::point_count];
1082+
10821083
for (int i = 0; i < perlin::point_count; ++i)
10831084
p[i] = random_double();
1085+
10841086
return p;
10851087
}
10861088

src/InOneWeekend/hittable_list.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
// along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
1212
//==============================================================================================
1313

14+
#include "common/rtweekend.h"
1415
#include "hittable.h"
16+
#include <memory>
1517
#include <vector>
1618

1719

src/TheNextWeek/hittable_list.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
1212
//==============================================================================================
1313

14+
#include "common/rtweekend.h"
1415
#include "hittable.h"
1516
#include <vector>
1617

src/TheNextWeek/material.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ class dielectric : public material {
5353
scattered = ray(rec.p, reflected, r_in.time());
5454
return true;
5555
}
56-
56+
5757
double reflect_prob = schlick(cos_theta, etai_over_etat);
5858
if (random_double() < reflect_prob)
5959
{
6060
vec3 reflected = reflect(unit_direction, rec.normal);
6161
scattered = ray(rec.p, reflected, r_in.time());
6262
return true;
6363
}
64-
64+
6565
vec3 refracted = refract(unit_direction, rec.normal, etai_over_etat);
6666
scattered = ray(rec.p, refracted, r_in.time());
6767
return true;

src/TheRestOfYourLife/hittable_list.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
1212
//==============================================================================================
1313

14+
#include "common/rtweekend.h"
1415
#include "hittable.h"
1516
#include <vector>
1617

src/common/rtweekend.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
// along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
1010
//==============================================================================================
1111

12+
#include <cmath>
1213
#include <cstdlib>
1314
#include <limits>
14-
#include <cmath>
1515
#include <memory>
1616

1717

0 commit comments

Comments
 (0)