Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #43

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 11 additions & 32 deletions include/box.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#ifndef BOX_HPP
#define BOX_HPP

#include <array>

#include "primitives.hpp"
#include "rectangle.hpp"
#include "rtweekend.hpp"
#include "visit.hpp"

namespace raytracer::scene {
/// This class implements a axis aligned cuboid using 6 rectangles
class box {
public:
Expand All @@ -17,42 +19,19 @@ class box {
, box_max { p1 }
, material_type { mat_type } {
/// Add six sides of the box based on box_min and box_max to sides
sides[0] = xy_rect(p0.x(), p1.x(), p0.y(), p1.y(), p1.z(), mat_type);
sides[1] = xy_rect(p0.x(), p1.x(), p0.y(), p1.y(), p0.z(), mat_type);
sides[2] = xz_rect(p0.x(), p1.x(), p0.z(), p1.z(), p1.y(), mat_type);
sides[3] = xz_rect(p0.x(), p1.x(), p0.z(), p1.z(), p0.y(), mat_type);
sides[4] = yz_rect(p0.y(), p1.y(), p0.z(), p1.z(), p1.x(), mat_type);
sides[5] = yz_rect(p0.y(), p1.y(), p0.z(), p1.z(), p0.x(), mat_type);
}

/// Compute ray interaction with the box
bool hit(auto& ctx, const ray& r, real_t min, real_t max, hit_record& rec,
material_t& hit_material_type) const {
hit_record temp_rec;
material_t temp_material_type;
auto hit_anything = false;
auto closest_so_far = max;
// Checking if the ray hits any of the sides
for (const auto& side : sides) {
if (dev_visit(
[&](auto&& arg) {
return arg.hit(ctx, r, min, closest_so_far, temp_rec,
temp_material_type);
},
side)) {
hit_anything = true;
closest_so_far = temp_rec.t;
rec = temp_rec;
hit_material_type = temp_material_type;
}
}
return hit_anything;
sides[0] = xy_rect(p0.x(), p1.x(), p0.y(), p1.y(), p1.z(), material_type);
sides[1] = xy_rect(p0.x(), p1.x(), p0.y(), p1.y(), p0.z(), material_type);
sides[2] = xz_rect(p0.x(), p1.x(), p0.z(), p1.z(), p1.y(), material_type);
sides[3] = xz_rect(p0.x(), p1.x(), p0.z(), p1.z(), p0.y(), material_type);
sides[4] = yz_rect(p0.y(), p1.y(), p0.z(), p1.z(), p1.x(), material_type);
sides[5] = yz_rect(p0.y(), p1.y(), p0.z(), p1.z(), p0.x(), material_type);
}

point box_min;
point box_max;
material_t material_type;
std::array<rectangle_t, 6> sides;
};
} // namespace raytracer::scene

#endif
2 changes: 1 addition & 1 deletion include/build_parameters.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef BUILD_PARAMETERS_HPP
#define BUILD_PARAMETERS_HPP
namespace buildparams {
namespace raytracer::buildparams {

#ifdef USE_SINGLE_TASK
constexpr bool use_single_task = true;
Expand Down
10 changes: 6 additions & 4 deletions include/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

#include <cmath>

#include "localrandom.hpp"
#include "primitives.hpp"
#include "ray.hpp"
#include "rtweekend.hpp"

namespace raytracer {
/** Camera model

This implements:
Expand Down Expand Up @@ -90,14 +92,14 @@ class camera {
viewport local coordinates (s,t) based on viewport
width, height and focus distance
*/
ray get_ray(real_t s, real_t t, LocalPseudoRNG& rng) const {
ray get_ray(real_t s, real_t t, random::LocalPseudoRNG& rng) const {
vec rd = lens_radius * rng.in_unit_disk();
vec offset = u * rd.x() + v * rd.y();
return { origin + offset,
lower_left_corner + s * horizontal + t * vertical - origin -
offset,
rng.float_t(time0, time1) };
rng.real(time0, time1) };
}
};

} // namespace raytracer
#endif
58 changes: 4 additions & 54 deletions include/constant_medium.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
#include "material.hpp"
#include "sphere.hpp"
#include "texture.hpp"
#include "visit.hpp"

using hittableVolume_t = std::variant<sphere, box>;
namespace raytracer::scene {

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

/**
* A ray going through the volume can either make it all the way through
Expand All @@ -25,60 +26,9 @@ class constant_medium {
, neg_inv_density { -1 / d }
, phase_function { isotropic_material { a } } {}

bool hit(auto& ctx, const ray& r, real_t min, real_t max, hit_record& rec,
material_t& hit_material_type) const {
auto& rng = ctx.rng;
hit_material_type = phase_function;
material_t temp_material_type;
hit_record rec1, rec2;
if (!dev_visit(
[&](auto&& arg) {
return arg.hit(ctx, r, -infinity, infinity, rec1,
temp_material_type);
},
boundary)) {
return false;
}

if (!dev_visit(
[&](auto&& arg) {
return arg.hit(ctx, r, rec1.t + 0.0001f, infinity, rec2,
temp_material_type);
},
boundary)) {
return false;
}

if (rec1.t < min)
rec1.t = min;
if (rec2.t > max)
rec2.t = max;
if (rec1.t >= rec2.t)
return false;
if (rec1.t < 0)
rec1.t = 0;

const auto ray_length = sycl::length(r.direction());
/// Distance between the two hitpoints affect of probability
/// of the ray hitting a smoke particle
const auto distance_inside_boundary = (rec2.t - rec1.t) * ray_length;
const auto hit_distance = neg_inv_density * sycl::log(rng.float_t());

/// With lower density, hit_distance has higher probabilty
/// of being greater than distance_inside_boundary
if (hit_distance > distance_inside_boundary)
return false;

rec.t = rec1.t + hit_distance / ray_length;
rec.p = r.at(rec.t);

rec.normal = vec { 1, 0, 0 }; // arbitrary
rec.front_face = true; // also arbitrary
return true;
}

hittableVolume_t boundary;
real_t neg_inv_density;
material_t phase_function;
};
} // namespace raytracer::scene
#endif
25 changes: 25 additions & 0 deletions include/hit_record.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef HIT_RECORD_HPP
#define HIT_RECORD_HPP

#include "primitives.hpp"

namespace raytracer::visitor {
class hit_record {
public:
real_t t;
point p; // hit point
vec normal; // normal at hit point
bool front_face; // to check if hit point is on the outer surface
/*local coordinates for rectangles
and mercator coordinates for spheres */
real_t u;
real_t v;

// To set if the hit point is on the front face
void set_face_normal(const ray& r, const vec& outward_normal) {
front_face = dot(r.direction(), outward_normal) < 0;
normal = front_face ? outward_normal : vec {} - outward_normal;
}
};
} // namespace raytracer::visitor
#endif
32 changes: 13 additions & 19 deletions include/hitable.hpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
#ifndef HITTABLE_H
#define HITTABLE_H

#include "ray.hpp"
#include "rtweekend.hpp"
#include "vec.hpp"
#include <variant>

class hit_record {
public:
float t; //
point p; // hit point
vec normal; // normal at hit point
bool front_face; // to check if hit point is on the outer surface
/*local coordinates for rectangles
and mercator coordintes for spheres */
float u;
float v;
#include "hit_record.hpp"
#include "material.hpp"

// To set if the hit point is on the front face
void set_face_normal(const ray& r, const vec& outward_normal) {
front_face = dot(r.direction(), outward_normal) < 0;
normal = front_face ? outward_normal : vec {} - outward_normal;
}
};
#include "box.hpp"
#include "constant_medium.hpp"
#include "ray.hpp"
#include "rectangle.hpp"
#include "sphere.hpp"
#include "triangle.hpp"

namespace raytracer::scene {
using hittable_t = std::variant<std::monostate, sphere, xy_rect, triangle, box,
constant_medium>;
}
#endif
Loading