Skip to content

Commit d379f54

Browse files
committed
Refactor restLife/sphere.h
1 parent 168d1a8 commit d379f54

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

books/RayTracingTheRestOfYourLife.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,13 +2155,13 @@
21552155
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
21562156
double sphere::pdf_value(const vec3& o, const vec3& v) const {
21572157
hit_record rec;
2158-
if (this->hit(ray(o, v), 0.001, infinity, rec)) {
2159-
auto cos_theta_max = sqrt(1 - radius*radius/(center-o).length_squared());
2160-
auto solid_angle = 2*pi*(1-cos_theta_max);
2161-
return 1 / solid_angle;
2162-
}
2163-
else
2158+
if (!this->hit(ray(o, v), 0.001, infinity, rec))
21642159
return 0;
2160+
2161+
auto cos_theta_max = sqrt(1 - radius*radius/(center-o).length_squared());
2162+
auto solid_angle = 2*pi*(1-cos_theta_max);
2163+
2164+
return 1 / solid_angle;
21652165
}
21662166

21672167
vec3 sphere::random(const vec3& o) const {

src/TheRestOfYourLife/sphere.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,24 @@ class sphere: public hittable {
2323
: center(cen), radius(r), mat_ptr(m) {};
2424
virtual bool hit(const ray& r, double tmin, double tmax, hit_record& rec) const;
2525
virtual bool bounding_box(double t0, double t1, aabb& output_box) const;
26-
virtual double pdf_value(const vec3& o, const vec3& v) const;
26+
virtual double pdf_value(const vec3& o, const vec3& v) const;
2727
virtual vec3 random(const vec3& o) const;
2828

29+
public:
2930
vec3 center;
3031
double radius;
3132
shared_ptr<material> mat_ptr;
3233
};
3334

3435
double sphere::pdf_value(const vec3& o, const vec3& v) const {
3536
hit_record rec;
36-
if (this->hit(ray(o, v), 0.001, infinity, rec)) {
37-
auto cos_theta_max = sqrt(1 - radius*radius/(center-o).length_squared());
38-
auto solid_angle = 2*pi*(1-cos_theta_max);
39-
return 1 / solid_angle;
40-
}
41-
else
37+
if (!this->hit(ray(o, v), 0.001, infinity, rec))
4238
return 0;
39+
40+
auto cos_theta_max = sqrt(1 - radius*radius/(center-o).length_squared());
41+
auto solid_angle = 2*pi*(1-cos_theta_max);
42+
43+
return 1 / solid_angle;
4344
}
4445

4546
vec3 sphere::random(const vec3& o) const {
@@ -63,6 +64,7 @@ bool sphere::hit(const ray& r, double t_min, double t_max, hit_record& rec) cons
6364
auto a = r.direction().length_squared();
6465
auto half_b = dot(oc, r.direction());
6566
auto c = oc.length_squared() - radius*radius;
67+
6668
auto discriminant = half_b*half_b - a*c;
6769

6870
if (discriminant > 0) {
@@ -90,6 +92,7 @@ bool sphere::hit(const ray& r, double t_min, double t_max, hit_record& rec) cons
9092
return true;
9193
}
9294
}
95+
9396
return false;
9497
}
9598

0 commit comments

Comments
 (0)