Skip to content

Commit 125e43e

Browse files
author
Luc Forget
committed
Refactoring
1 parent 3979043 commit 125e43e

21 files changed

Lines changed: 667 additions & 675 deletions

include/box.hpp

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#ifndef BOX_HPP
22
#define BOX_HPP
33

4+
#include <array>
5+
6+
#include "primitives.hpp"
47
#include "rectangle.hpp"
5-
#include "rtweekend.hpp"
6-
#include "visit.hpp"
78

9+
namespace raytracer::scene {
810
/// This class implements a axis aligned cuboid using 6 rectangles
911
class box {
1012
public:
@@ -17,39 +19,19 @@ class box {
1719
, box_max { p1 }
1820
, material_type { mat_type } {
1921
/// Add six sides of the box based on box_min and box_max to sides
20-
sides[0] = xy_rect(p0.x(), p1.x(), p0.y(), p1.y(), p1.z(), mat_type);
21-
sides[1] = xy_rect(p0.x(), p1.x(), p0.y(), p1.y(), p0.z(), mat_type);
22-
sides[2] = xz_rect(p0.x(), p1.x(), p0.z(), p1.z(), p1.y(), mat_type);
23-
sides[3] = xz_rect(p0.x(), p1.x(), p0.z(), p1.z(), p0.y(), mat_type);
24-
sides[4] = yz_rect(p0.y(), p1.y(), p0.z(), p1.z(), p1.x(), mat_type);
25-
sides[5] = yz_rect(p0.y(), p1.y(), p0.z(), p1.z(), p0.x(), mat_type);
26-
}
27-
28-
/// Compute ray interaction with the box
29-
bool hit(auto& ctx, const ray& r, real_t min, real_t max, hit_record& rec,
30-
material_t& hit_material_type) const {
31-
hit_record temp_rec;
32-
material_t temp_material_type;
33-
auto hit_anything = false;
34-
auto closest_so_far = max;
35-
// Checking if the ray hits any of the sides
36-
for (const auto& side : sides) {
37-
if (dev_visit(hittable_hit_visitor(ctx, r, min, closest_so_far, temp_rec,
38-
temp_material_type),
39-
side)) {
40-
hit_anything = true;
41-
closest_so_far = temp_rec.t;
42-
rec = temp_rec;
43-
hit_material_type = temp_material_type;
44-
}
45-
}
46-
return hit_anything;
22+
sides[0] = xy_rect(p0.x(), p1.x(), p0.y(), p1.y(), p1.z(), material_type);
23+
sides[1] = xy_rect(p0.x(), p1.x(), p0.y(), p1.y(), p0.z(), material_type);
24+
sides[2] = xz_rect(p0.x(), p1.x(), p0.z(), p1.z(), p1.y(), material_type);
25+
sides[3] = xz_rect(p0.x(), p1.x(), p0.z(), p1.z(), p0.y(), material_type);
26+
sides[4] = yz_rect(p0.y(), p1.y(), p0.z(), p1.z(), p1.x(), material_type);
27+
sides[5] = yz_rect(p0.y(), p1.y(), p0.z(), p1.z(), p0.x(), material_type);
4728
}
4829

4930
point box_min;
5031
point box_max;
5132
material_t material_type;
5233
std::array<rectangle_t, 6> sides;
5334
};
35+
} // namespace raytracer::scene
5436

5537
#endif

include/build_parameters.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifndef BUILD_PARAMETERS_HPP
22
#define BUILD_PARAMETERS_HPP
3-
namespace buildparams {
3+
namespace raytracer::buildparams {
44

55
#ifdef USE_SINGLE_TASK
66
constexpr bool use_single_task = true;

include/camera.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
#include <cmath>
55

6+
#include "localrandom.hpp"
7+
#include "primitives.hpp"
68
#include "ray.hpp"
7-
#include "rtweekend.hpp"
89

10+
namespace raytracer {
911
/** Camera model
1012
1113
This implements:
@@ -90,7 +92,7 @@ class camera {
9092
viewport local coordinates (s,t) based on viewport
9193
width, height and focus distance
9294
*/
93-
ray get_ray(real_t s, real_t t, LocalPseudoRNG& rng) const {
95+
ray get_ray(real_t s, real_t t, random::LocalPseudoRNG& rng) const {
9496
vec rd = lens_radius * rng.in_unit_disk();
9597
vec offset = u * rd.x() + v * rd.y();
9698
return { origin + offset,
@@ -99,5 +101,5 @@ class camera {
99101
rng.real(time0, time1) };
100102
}
101103
};
102-
104+
} // namespace raytracer
103105
#endif

include/constant_medium.hpp

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
#include "material.hpp"
66
#include "sphere.hpp"
77
#include "texture.hpp"
8-
#include "visit.hpp"
8+
9+
namespace raytracer::scene {
910

1011
using hittableVolume_t = std::variant<std::monostate, sphere, box>;
1112

@@ -25,54 +26,9 @@ class constant_medium {
2526
, neg_inv_density { -1 / d }
2627
, phase_function { isotropic_material { a } } {}
2728

28-
bool hit(auto& ctx, const ray& r, real_t min, real_t max, hit_record& rec,
29-
material_t& hit_material_type) const {
30-
auto& rng = ctx.rng;
31-
hit_material_type = phase_function;
32-
material_t temp_material_type;
33-
hit_record rec1, rec2;
34-
if (!dev_visit(hittable_hit_visitor(ctx, r, -infinity, infinity, rec1,
35-
temp_material_type),
36-
boundary)) {
37-
return false;
38-
}
39-
40-
if (!dev_visit(hittable_hit_visitor(ctx, r, rec1.t + 0.0001f, infinity, rec2,
41-
temp_material_type),
42-
boundary)) {
43-
return false;
44-
}
45-
46-
if (rec1.t < min)
47-
rec1.t = min;
48-
if (rec2.t > max)
49-
rec2.t = max;
50-
if (rec1.t >= rec2.t)
51-
return false;
52-
if (rec1.t < 0)
53-
rec1.t = 0;
54-
55-
const auto ray_length = sycl::length(r.direction());
56-
/// Distance between the two hitpoints affect of probability
57-
/// of the ray hitting a smoke particle
58-
const auto distance_inside_boundary = (rec2.t - rec1.t) * ray_length;
59-
const auto hit_distance = neg_inv_density * sycl::log(rng.real());
60-
61-
/// With lower density, hit_distance has higher probabilty
62-
/// of being greater than distance_inside_boundary
63-
if (hit_distance > distance_inside_boundary)
64-
return false;
65-
66-
rec.t = rec1.t + hit_distance / ray_length;
67-
rec.p = r.at(rec.t);
68-
69-
rec.normal = vec { 1, 0, 0 }; // arbitrary
70-
rec.front_face = true; // also arbitrary
71-
return true;
72-
}
73-
7429
hittableVolume_t boundary;
7530
real_t neg_inv_density;
7631
material_t phase_function;
7732
};
33+
} // namespace raytracer::scene
7834
#endif

include/hit_record.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
#ifndef HIT_RECORD_HPP
22
#define HIT_RECORD_HPP
33

4-
#include "rtweekend.hpp"
4+
#include "primitives.hpp"
55

6+
namespace raytracer::visitor {
67
class hit_record {
78
public:
8-
real_t t; //
9+
real_t t;
910
point p; // hit point
1011
vec normal; // normal at hit point
1112
bool front_face; // to check if hit point is on the outer surface
1213
/*local coordinates for rectangles
13-
and mercator coordintes for spheres */
14+
and mercator coordinates for spheres */
1415
real_t u;
1516
real_t v;
1617

@@ -20,4 +21,5 @@ class hit_record {
2021
normal = front_face ? outward_normal : vec {} - outward_normal;
2122
}
2223
};
24+
} // namespace raytracer::visitor
2325
#endif

include/hitable.hpp

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,6 @@
55

66
#include "hit_record.hpp"
77
#include "material.hpp"
8-
#include "rtweekend.hpp"
9-
#include "vec.hpp"
10-
11-
struct hittable_hit_visitor {
12-
private:
13-
task_context& ctx;
14-
const ray& r;
15-
real_t min;
16-
real_t max;
17-
hit_record& rec;
18-
material_t& hit_material_type;
19-
20-
public:
21-
hittable_hit_visitor(task_context& ctx, const ray& r, real_t min, real_t max,
22-
hit_record& rec, material_t& hit_material_type)
23-
: ctx { ctx }
24-
, r { r }
25-
, min { min }
26-
, max { max }
27-
, rec { rec }
28-
, hit_material_type { hit_material_type } {}
29-
30-
template <typename H> bool operator()(H&& hittable) {
31-
return hittable.hit(ctx, r, min, max, rec, hit_material_type);
32-
}
33-
34-
bool operator()(std::monostate) {
35-
assert(fase && "unreachable");
36-
return false;
37-
}
38-
};
398

409
#include "box.hpp"
4110
#include "constant_medium.hpp"
@@ -44,6 +13,8 @@ struct hittable_hit_visitor {
4413
#include "sphere.hpp"
4514
#include "triangle.hpp"
4615

16+
namespace raytracer::scene {
4717
using hittable_t = std::variant<std::monostate, sphere, xy_rect, triangle, box,
4818
constant_medium>;
19+
}
4920
#endif

0 commit comments

Comments
 (0)