Skip to content

Commit

Permalink
Add Ray::new function
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Oom committed Jul 7, 2024
1 parent fc2f236 commit 1ad88be
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 28 deletions.
4 changes: 4 additions & 0 deletions geometry/src/ray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ pub struct Ray {
}

impl Ray {
pub fn new(origin: Vec3, direction: Vec3) -> Ray {
Ray { origin, direction }
}

pub fn between(a: Vec3, b: Vec3) -> Ray {
Ray {
origin: a,
Expand Down
8 changes: 4 additions & 4 deletions kdtree-reducer-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ struct Args {
fn main() {
let args = Args::parse();

let ray = Ray {
origin: [3.897963, 0.24242611, -4.203691].into(),
direction: [-13.897963, 9.757574, 14.2036915].into(),
};
let ray = Ray::new(
[3.897963, 0.24242611, -4.203691].into(),
[-13.897963, 9.757574, 14.2036915].into(),
);
let expected_intersection = (
7589,
RayIntersection {
Expand Down
8 changes: 4 additions & 4 deletions kdtree-tester-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,14 @@ impl RayBouncer {
}

let sample = material.sample(&IncomingRay { wi, n, uv }, rng);
let next_ray = Ray {
origin: if sample.wo.dot(n) >= 0.0 {
let next_ray = Ray::new(
if sample.wo.dot(n) >= 0.0 {
point_above
} else {
point_below
},
direction: sample.wo,
};
sample.wo,
);

self.bounce(rng, &next_ray, accumulated_bounces + 1)
}
Expand Down
16 changes: 8 additions & 8 deletions kdtree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,10 @@ mod tests {
geometries: vec![triangle.into()],
root,
};
let ray = Ray {
origin: Vec3::new(0.0, 0.0, 3.0),
direction: Vec3::new(0.06646079, 0.08247295, -0.9238795),
};
let ray = Ray::new(
Vec3::new(0.0, 0.0, 3.0),
Vec3::new(0.06646079, 0.08247295, -0.9238795),
);

let actual = tree.intersect(&ray, 0.0..=f32::MAX);

Expand Down Expand Up @@ -459,10 +459,10 @@ mod tests {
root: KdNode::new_node(Aap::new_x(-1.0), KdNode::empty(), KdNode::new_leaf(vec![0])),
geometries: vec![triangle.into()],
};
let ray = Ray {
origin: Vec3::new(-0.5170438, -0.4394186, -0.045965273),
direction: Vec3::new(-0.8491798, -0.1408107, -0.5089852),
};
let ray = Ray::new(
Vec3::new(-0.5170438, -0.4394186, -0.045965273),
Vec3::new(-0.8491798, -0.1408107, -0.5089852),
);

let actual = tree.intersect(&ray, 0.0..=f32::MAX);

Expand Down
5 changes: 1 addition & 4 deletions scene/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ impl Pinhole {

#[inline]
pub fn ray(&self, x: f32, y: f32) -> Ray {
Ray {
origin: self.camera.position,
direction: self.plane + x * self.dx + y * self.dy,
}
Ray::new(self.camera.position, self.plane + x * self.dx + y * self.dy)
}
}

Expand Down
8 changes: 4 additions & 4 deletions tracing/src/pathtracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ impl Pathtracer {
let accumulated_radiance = accumulated_radiance + accumulated_transport * incoming_radiance;

let sample = material.sample(&IncomingRay { wi, n, uv }, rng);
let next_ray = Ray {
origin: if sample.wo.dot(n) >= 0.0 {
let next_ray = Ray::new(
if sample.wo.dot(n) >= 0.0 {
point_above
} else {
point_below
},
direction: sample.wo,
};
sample.wo,
);

if sample.pdf <= 0.01 {
return accumulated_radiance;
Expand Down
5 changes: 1 addition & 4 deletions tracing/src/raytracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ impl Raytracer {
let this = &self;
let material: &MaterialModel = &triangle.material;
let direction = light.center - point;
let shadow_ray = Ray {
origin: offset_point,
direction,
};
let shadow_ray = Ray::new(offset_point, direction);
if this.intersect_any(&shadow_ray, 0.0..=1.0) {
return Vec3::ZERO;
}
Expand Down

0 comments on commit 1ad88be

Please sign in to comment.