Skip to content

Commit 9b156c5

Browse files
committed
Switch theRestOfYourLife code to std::shared_ptr
1 parent f5f64cc commit 9b156c5

File tree

10 files changed

+100
-79
lines changed

10 files changed

+100
-79
lines changed

src/TheRestOfYourLife/aarect.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class xy_rect: public hittable {
1919
public:
2020
xy_rect() {}
2121

22-
xy_rect(double _x0, double _x1, double _y0, double _y1, double _k, material *mat)
22+
xy_rect(
23+
double _x0, double _x1, double _y0, double _y1, double _k, shared_ptr<material> mat)
2324
: x0(_x0), x1(_x1), y0(_y0), y1(_y1), k(_k), mp(mat) {};
2425

2526
virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const;
@@ -29,15 +30,16 @@ class xy_rect: public hittable {
2930
return true;
3031
}
3132

32-
material *mp;
33+
shared_ptr<material> mp;
3334
double x0, x1, y0, y1, k;
3435
};
3536

3637
class xz_rect: public hittable {
3738
public:
3839
xz_rect() {}
3940

40-
xz_rect(double _x0, double _x1, double _z0, double _z1, double _k, material *mat)
41+
xz_rect(
42+
double _x0, double _x1, double _z0, double _z1, double _k, shared_ptr<material> mat)
4143
: x0(_x0), x1(_x1), z0(_z0), z1(_z1), k(_k), mp(mat) {};
4244

4345
virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const;
@@ -64,15 +66,16 @@ class xz_rect: public hittable {
6466
return random_point - origin;
6567
}
6668

67-
material *mp;
69+
shared_ptr<material> mp;
6870
double x0, x1, z0, z1, k;
6971
};
7072

7173
class yz_rect: public hittable {
7274
public:
7375
yz_rect() {}
7476

75-
yz_rect(double _y0, double _y1, double _z0, double _z1, double _k, material *mat)
77+
yz_rect(
78+
double _y0, double _y1, double _z0, double _z1, double _k, shared_ptr<material> mat)
7679
: y0(_y0), y1(_y1), z0(_z0), z1(_z1), k(_k), mp(mat) {};
7780

7881
virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const;
@@ -82,7 +85,7 @@ class yz_rect: public hittable {
8285
return true;
8386
}
8487

85-
material *mp;
88+
shared_ptr<material> mp;
8689
double y0, y1, z0, z1, k;
8790
};
8891

src/TheRestOfYourLife/box.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class box: public hittable {
2020
public:
2121
box() {}
22-
box(const vec3& p0, const vec3& p1, material *ptr);
22+
box(const vec3& p0, const vec3& p1, shared_ptr<material> ptr);
2323

2424
virtual bool hit(const ray& r, double t0, double t1, hit_record& rec) const;
2525

@@ -28,21 +28,27 @@ class box: public hittable {
2828
return true;
2929
}
3030

31+
public:
3132
vec3 box_min;
3233
vec3 box_max;
3334
hittable_list sides;
3435
};
3536

36-
box::box(const vec3& p0, const vec3& p1, material *ptr) {
37+
box::box(const vec3& p0, const vec3& p1, shared_ptr<material> ptr) {
3738
box_min = p0;
3839
box_max = p1;
3940

40-
sides.add(new xy_rect(p0.x(), p1.x(), p0.y(), p1.y(), p1.z(), ptr));
41-
sides.add(new flip_face(new xy_rect(p0.x(), p1.x(), p0.y(), p1.y(), p0.z(), ptr)));
42-
sides.add(new xz_rect(p0.x(), p1.x(), p0.z(), p1.z(), p1.y(), ptr));
43-
sides.add(new flip_face(new xz_rect(p0.x(), p1.x(), p0.z(), p1.z(), p0.y(), ptr)));
44-
sides.add(new yz_rect(p0.y(), p1.y(), p0.z(), p1.z(), p1.x(), ptr));
45-
sides.add(new flip_face(new yz_rect(p0.y(), p1.y(), p0.z(), p1.z(), p0.x(), ptr)));
41+
sides.add(make_shared<xy_rect>(p0.x(), p1.x(), p0.y(), p1.y(), p1.z(), ptr));
42+
sides.add(make_shared<flip_face>(
43+
make_shared<xy_rect>(p0.x(), p1.x(), p0.y(), p1.y(), p0.z(), ptr)));
44+
45+
sides.add(make_shared<xz_rect>(p0.x(), p1.x(), p0.z(), p1.z(), p1.y(), ptr));
46+
sides.add(make_shared<flip_face>(
47+
make_shared<xz_rect>(p0.x(), p1.x(), p0.z(), p1.z(), p0.y(), ptr)));
48+
49+
sides.add(make_shared<yz_rect>(p0.y(), p1.y(), p0.z(), p1.z(), p1.x(), ptr));
50+
sides.add(make_shared<flip_face>(
51+
make_shared<yz_rect>(p0.y(), p1.y(), p0.z(), p1.z(), p0.x(), ptr)));
4652
}
4753

4854
bool box::hit(const ray& r, double t0, double t1, hit_record& rec) const {

src/TheRestOfYourLife/bvh.h

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ class bvh_node : public hittable {
2525
{}
2626

2727
bvh_node(
28-
std::vector<hittable*>& objects,
28+
std::vector<shared_ptr<hittable>>& objects,
2929
size_t start, size_t end, double time0, double time1);
3030

3131
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const;
3232
virtual bool bounding_box(double t0, double t1, aabb& output_box) const;
3333

3434
public:
35-
hittable *left;
36-
hittable *right;
35+
shared_ptr<hittable> left;
36+
shared_ptr<hittable> right;
3737
aabb box;
3838
};
3939

4040

41-
inline bool box_compare(const hittable* a, const hittable* b, int axis) {
41+
inline bool box_compare(const shared_ptr<hittable> a, const shared_ptr<hittable> b, int axis) {
4242
aabb box_a;
4343
aabb box_b;
4444

@@ -49,13 +49,22 @@ inline bool box_compare(const hittable* a, const hittable* b, int axis) {
4949
}
5050

5151

52-
bool box_x_compare (const hittable* a, const hittable* b) { return box_compare(a, b, 0); }
53-
bool box_y_compare (const hittable* a, const hittable* b) { return box_compare(a, b, 1); }
54-
bool box_z_compare (const hittable* a, const hittable* b) { return box_compare(a, b, 2); }
52+
bool box_x_compare (const shared_ptr<hittable> a, const shared_ptr<hittable> b) {
53+
return box_compare(a, b, 0);
54+
}
55+
56+
bool box_y_compare (const shared_ptr<hittable> a, const shared_ptr<hittable> b) {
57+
return box_compare(a, b, 1);
58+
}
59+
60+
bool box_z_compare (const shared_ptr<hittable> a, const shared_ptr<hittable> b) {
61+
return box_compare(a, b, 2);
62+
}
5563

5664

5765
bvh_node::bvh_node(
58-
std::vector<hittable*>& objects, size_t start, size_t end, double time0, double time1
66+
std::vector<shared_ptr<hittable>>& objects,
67+
size_t start, size_t end, double time0, double time1
5968
) {
6069
int axis = random_int(0,2);
6170
auto comparator = (axis == 0) ? box_x_compare
@@ -78,8 +87,8 @@ bvh_node::bvh_node(
7887
std::sort(objects.begin() + start, objects.begin() + end, comparator);
7988

8089
auto mid = start + object_span/2;
81-
left = new bvh_node(objects, start, mid, time0, time1);
82-
right = new bvh_node(objects, mid, end, time0, time1);
90+
left = make_shared<bvh_node>(objects, start, mid, time0, time1);
91+
right = make_shared<bvh_node>(objects, mid, end, time0, time1);
8392
}
8493

8594
aabb box_left, box_right;

src/TheRestOfYourLife/hittable.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void get_sphere_uv(const vec3& p, double& u, double& v) {
2828
struct hit_record {
2929
vec3 p;
3030
vec3 normal;
31-
material *mat_ptr;
31+
shared_ptr<material> mat_ptr;
3232
double t;
3333
double u;
3434
double v;
@@ -50,7 +50,7 @@ class hittable {
5050

5151
class flip_face : public hittable {
5252
public:
53-
flip_face(hittable *p) : ptr(p) {}
53+
flip_face(shared_ptr<hittable> p) : ptr(p) {}
5454
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
5555
if (ptr->hit(r, t_min, t_max, rec)) {
5656
rec.front_face = !rec.front_face;
@@ -62,15 +62,16 @@ class flip_face : public hittable {
6262
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
6363
return ptr->bounding_box(t0, t1, output_box);
6464
}
65-
hittable *ptr;
65+
shared_ptr<hittable> ptr;
6666
};
6767

6868
class translate : public hittable {
6969
public:
70-
translate(hittable *p, const vec3& displacement) : ptr(p), offset(displacement) {}
70+
translate(shared_ptr<hittable> p, const vec3& displacement)
71+
: ptr(p), offset(displacement) {}
7172
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const;
7273
virtual bool bounding_box(double t0, double t1, aabb& output_box) const;
73-
hittable *ptr;
74+
shared_ptr<hittable> ptr;
7475
vec3 offset;
7576
};
7677

@@ -98,20 +99,20 @@ bool translate::bounding_box(double t0, double t1, aabb& output_box) const {
9899

99100
class rotate_y : public hittable {
100101
public:
101-
rotate_y(hittable *p, double angle);
102+
rotate_y(shared_ptr<hittable> p, double angle);
102103
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const;
103104
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
104105
output_box = bbox;
105106
return hasbox;
106107
}
107-
hittable *ptr;
108+
shared_ptr<hittable> ptr;
108109
double sin_theta;
109110
double cos_theta;
110111
bool hasbox;
111112
aabb bbox;
112113
};
113114

114-
rotate_y::rotate_y(hittable *p, double angle) : ptr(p) {
115+
rotate_y::rotate_y(shared_ptr<hittable> p, double angle) : ptr(p) {
115116
auto radians = degrees_to_radians(angle);
116117
sin_theta = sin(radians);
117118
cos_theta = cos(radians);

src/TheRestOfYourLife/hittable_list.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@
1818
class hittable_list: public hittable {
1919
public:
2020
hittable_list() {}
21-
hittable_list(hittable* object) { add(object); }
21+
hittable_list(shared_ptr<hittable> object) { add(object); }
2222

23-
void clear() { objects.clear(); }
24-
void add(hittable* object) { objects.push_back(object); }
23+
void clear() { objects.clear(); }
24+
void add(shared_ptr<hittable> object) { objects.push_back(object); }
2525

2626
virtual bool hit(const ray& r, double tmin, double tmax, hit_record& rec) const;
2727
virtual bool bounding_box(double t0, double t1, aabb& output_box) const;
2828
virtual double pdf_value(const vec3 &o, const vec3 &v) const;
2929
virtual vec3 random(const vec3 &o) const;
3030

3131
public:
32-
std::vector<hittable*> objects;
32+
std::vector<shared_ptr<hittable>> objects;
3333
};
3434

3535

src/TheRestOfYourLife/main.cc

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <iostream>
2020

2121

22-
vec3 ray_color(const ray& r, hittable& world, hittable& light_shape, int depth) {
22+
vec3 ray_color(const ray& r, hittable& world, shared_ptr<hittable> light_shape, int depth) {
2323
hit_record rec;
2424

2525
// If we've exceeded the ray bounce limit, no more light is gathered.
@@ -40,11 +40,10 @@ vec3 ray_color(const ray& r, hittable& world, hittable& light_shape, int depth)
4040
return srec.attenuation * ray_color(srec.specular_ray, world, light_shape, depth-1);
4141
}
4242

43-
hittable_pdf plight(&light_shape, rec.p);
44-
mixture_pdf p(&plight, srec.pdf_ptr);
43+
auto plight = make_shared<hittable_pdf>(light_shape, rec.p);
44+
mixture_pdf p(plight, srec.pdf_ptr);
4545
ray scattered = ray(rec.p, p.generate(), r.time());
4646
auto pdf_val = p.value(scattered.direction());
47-
delete srec.pdf_ptr;
4847

4948
return emitted
5049
+ srec.attenuation * rec.mat_ptr->scattering_pdf(r, rec, scattered)
@@ -56,25 +55,25 @@ vec3 ray_color(const ray& r, hittable& world, hittable& light_shape, int depth)
5655
hittable_list cornell_box(camera& cam, double aspect) {
5756
hittable_list world;
5857

59-
auto red = new lambertian(new constant_texture(vec3(0.65, 0.05, 0.05)));
60-
auto white = new lambertian(new constant_texture(vec3(0.73, 0.73, 0.73)));
61-
auto green = new lambertian(new constant_texture(vec3(0.12, 0.45, 0.15)));
62-
auto light = new diffuse_light(new constant_texture(vec3(15, 15, 15)));
63-
64-
world.add(new flip_face(new yz_rect(0, 555, 0, 555, 555, green)));
65-
world.add(new yz_rect(0, 555, 0, 555, 0, red));
66-
world.add(new flip_face(new xz_rect(213, 343, 227, 332, 554, light)));
67-
world.add(new flip_face(new xz_rect(0, 555, 0, 555, 555, white)));
68-
world.add(new xz_rect(0, 555, 0, 555, 0, white));
69-
world.add(new flip_face(new xy_rect(0, 555, 0, 555, 555, white)));
70-
71-
hittable* box1 = new box(vec3(0,0,0), vec3(165,330,165), white);
72-
box1 = new rotate_y(box1, 15);
73-
box1 = new translate(box1, vec3(265,0,295));
58+
auto red = make_shared<lambertian>(make_shared<constant_texture>(vec3(0.65, 0.05, 0.05)));
59+
auto white = make_shared<lambertian>(make_shared<constant_texture>(vec3(0.73, 0.73, 0.73)));
60+
auto green = make_shared<lambertian>(make_shared<constant_texture>(vec3(0.12, 0.45, 0.15)));
61+
auto light = make_shared<diffuse_light>(make_shared<constant_texture>(vec3(15, 15, 15)));
62+
63+
world.add(make_shared<flip_face>(make_shared<yz_rect>(0, 555, 0, 555, 555, green)));
64+
world.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
65+
world.add(make_shared<flip_face>(make_shared<xz_rect>(213, 343, 227, 332, 554, light)));
66+
world.add(make_shared<flip_face>(make_shared<xz_rect>(0, 555, 0, 555, 555, white)));
67+
world.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white));
68+
world.add(make_shared<flip_face>(make_shared<xy_rect>(0, 555, 0, 555, 555, white)));
69+
70+
shared_ptr<hittable> box1 = make_shared<box>(vec3(0,0,0), vec3(165,330,165), white);
71+
box1 = make_shared<rotate_y>(box1, 15);
72+
box1 = make_shared<translate>(box1, vec3(265,0,295));
7473
world.add(box1);
7574

76-
material *glass = new dielectric(1.5);
77-
world.add(new sphere(vec3(190,90,190), 90 , glass));
75+
auto glass = make_shared<dielectric>(1.5);
76+
world.add(make_shared<sphere>(vec3(190,90,190), 90 , glass));
7877

7978
vec3 lookfrom(278, 278, -800);
8079
vec3 lookat(278, 278, 0);
@@ -104,9 +103,9 @@ int main() {
104103

105104
auto world = cornell_box(cam, aspect);
106105

107-
hittable_list lights;
108-
lights.add(new xz_rect(213, 343, 227, 332, 554, 0));
109-
lights.add(new sphere(vec3(190, 90, 190), 90, 0));
106+
auto lights = make_shared<hittable_list>();
107+
lights->add(make_shared<xz_rect>(213, 343, 227, 332, 554, shared_ptr<material>()));
108+
lights->add(make_shared<sphere>(vec3(190, 90, 190), 90, shared_ptr<material>()));
110109

111110
for (int j = ny-1; j >= 0; --j) {
112111
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;

0 commit comments

Comments
 (0)