Skip to content

Commit f5f64cc

Browse files
committed
Switch theNextWeek to using std::shared_ptr
1 parent fc908ec commit f5f64cc

File tree

11 files changed

+248
-187
lines changed

11 files changed

+248
-187
lines changed

src/TheNextWeek/aarect.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ 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)
23-
: x0(_x0), x1(_x1), y0(_y0), y1(_y1), k(_k), mp(mat) {};
22+
xy_rect(
23+
double _x0, double _x1, double _y0, double _y1, double _k, shared_ptr<material> mat
24+
) : 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;
2627

@@ -29,16 +30,17 @@ 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-
: x0(_x0), x1(_x1), z0(_z0), z1(_z1), k(_k), mp(mat) {};
41+
xz_rect(
42+
double _x0, double _x1, double _z0, double _z1, double _k, shared_ptr<material> mat
43+
) : 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;
4446

@@ -47,16 +49,17 @@ class xz_rect: public hittable {
4749
return true;
4850
}
4951

50-
material *mp;
52+
shared_ptr<material> mp;
5153
double x0, x1, z0, z1, k;
5254
};
5355

5456
class yz_rect: public hittable {
5557
public:
5658
yz_rect() {}
5759

58-
yz_rect(double _y0, double _y1, double _z0, double _z1, double _k, material *mat)
59-
: y0(_y0), y1(_y1), z0(_z0), z1(_z1), k(_k), mp(mat) {};
60+
yz_rect(
61+
double _y0, double _y1, double _z0, double _z1, double _k, shared_ptr<material> mat
62+
) : y0(_y0), y1(_y1), z0(_z0), z1(_z1), k(_k), mp(mat) {};
6063

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

@@ -65,7 +68,7 @@ class yz_rect: public hittable {
6568
return true;
6669
}
6770

68-
material *mp;
71+
shared_ptr<material> mp;
6972
double y0, y1, z0, z1, k;
7073
};
7174

src/TheNextWeek/box.h

Lines changed: 15 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,28 @@ 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+
38+
box::box(const vec3& p0, const vec3& p1, shared_ptr<material> ptr) {
3739
box_min = p0;
3840
box_max = p1;
3941

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)));
42+
sides.add(make_shared<xy_rect>(p0.x(), p1.x(), p0.y(), p1.y(), p1.z(), ptr));
43+
sides.add(make_shared<flip_face>(
44+
make_shared<xy_rect>(p0.x(), p1.x(), p0.y(), p1.y(), p0.z(), ptr)));
45+
46+
sides.add(make_shared<xz_rect>(p0.x(), p1.x(), p0.z(), p1.z(), p1.y(), ptr));
47+
sides.add(make_shared<flip_face>(
48+
make_shared<xz_rect>(p0.x(), p1.x(), p0.z(), p1.z(), p0.y(), ptr)));
49+
50+
sides.add(make_shared<yz_rect>(p0.y(), p1.y(), p0.z(), p1.z(), p1.x(), ptr));
51+
sides.add(make_shared<flip_face>(
52+
make_shared<yz_rect>(p0.y(), p1.y(), p0.z(), p1.z(), p0.x(), ptr)));
4653
}
4754

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

src/TheNextWeek/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/TheNextWeek/constant_medium.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
class constant_medium : public hittable {
2121
public:
22-
constant_medium(hittable *b, double d, texture *a) : boundary(b), density(d) {
23-
phase_function = new isotropic(a);
22+
constant_medium(shared_ptr<hittable> b, double d, shared_ptr<texture> a)
23+
: boundary(b), density(d)
24+
{
25+
phase_function = make_shared<isotropic>(a);
2426
}
2527

2628
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const;
@@ -29,9 +31,9 @@ class constant_medium : public hittable {
2931
return boundary->bounding_box(t0, t1, output_box);
3032
}
3133

32-
hittable *boundary;
34+
shared_ptr<hittable> boundary;
3335
double density;
34-
material *phase_function;
36+
shared_ptr<material> phase_function;
3537
};
3638

3739

src/TheNextWeek/hittable.h

Lines changed: 16 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;
@@ -48,7 +48,8 @@ class hittable {
4848

4949
class flip_face : public hittable {
5050
public:
51-
flip_face(hittable *p) : ptr(p) {}
51+
flip_face(shared_ptr<hittable> p) : ptr(p) {}
52+
5253
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
5354
if (ptr->hit(r, t_min, t_max, rec)) {
5455
rec.front_face = !rec.front_face;
@@ -57,18 +58,24 @@ class flip_face : public hittable {
5758
else
5859
return false;
5960
}
61+
6062
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
6163
return ptr->bounding_box(t0, t1, output_box);
6264
}
63-
hittable *ptr;
65+
66+
public:
67+
shared_ptr<hittable> ptr;
6468
};
6569

6670
class translate : public hittable {
6771
public:
68-
translate(hittable *p, const vec3& displacement) : ptr(p), offset(displacement) {}
72+
translate(shared_ptr<hittable> p, const vec3& displacement)
73+
: ptr(p), offset(displacement) {}
6974
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const;
7075
virtual bool bounding_box(double t0, double t1, aabb& output_box) const;
71-
hittable *ptr;
76+
77+
public:
78+
shared_ptr<hittable> ptr;
7279
vec3 offset;
7380
};
7481

@@ -96,20 +103,21 @@ bool translate::bounding_box(double t0, double t1, aabb& output_box) const {
96103

97104
class rotate_y : public hittable {
98105
public:
99-
rotate_y(hittable *p, double angle);
106+
rotate_y(shared_ptr<hittable> p, double angle);
100107
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const;
101108
virtual bool bounding_box(double t0, double t1, aabb& output_box) const {
102109
output_box = bbox;
103110
return hasbox;
104111
}
105-
hittable *ptr;
112+
113+
shared_ptr<hittable> ptr;
106114
double sin_theta;
107115
double cos_theta;
108116
bool hasbox;
109117
aabb bbox;
110118
};
111119

112-
rotate_y::rotate_y(hittable *p, double angle) : ptr(p) {
120+
rotate_y::rotate_y(shared_ptr<hittable> p, double angle) : ptr(p) {
113121
auto radians = degrees_to_radians(angle);
114122
sin_theta = sin(radians);
115123
cos_theta = cos(radians);

src/TheNextWeek/hittable_list.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
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

2929
public:
30-
std::vector<hittable*> objects;
30+
std::vector<shared_ptr<hittable>> objects;
3131
};
3232

3333

0 commit comments

Comments
 (0)