aboutsummaryrefslogtreecommitdiff
path: root/src/hittable/instance
diff options
context:
space:
mode:
authorlamp2024-01-21 17:03:07 +0000
committerlamp2024-01-21 17:03:07 +0000
commit45fafbcd4b41a5388ece377c4e051b5846407288 (patch)
tree2192c471ca3b47671f0906e27f6f6088f40b3d8a /src/hittable/instance
parent89a5c9a8a0cdf627cda0e31da454f83ca21315ce (diff)
fmt and tidy
Diffstat (limited to 'src/hittable/instance')
-rw-r--r--src/hittable/instance/moving.rs29
-rw-r--r--src/hittable/instance/rotate_x.rs56
-rw-r--r--src/hittable/instance/rotate_y.rs54
-rw-r--r--src/hittable/instance/rotate_z.rs54
-rw-r--r--src/hittable/instance/translate.rs12
5 files changed, 159 insertions, 46 deletions
diff --git a/src/hittable/instance/moving.rs b/src/hittable/instance/moving.rs
index 418fdbb..062af86 100644
--- a/src/hittable/instance/moving.rs
+++ b/src/hittable/instance/moving.rs
@@ -1,7 +1,7 @@
use std::sync::Arc;
-use crate::{ray::Ray, vec3::Vec3};
use crate::hittable::{HitRecord, Hittable, AABB};
+use crate::{ray::Ray, vec3::Vec3};
pub struct Moving {
pub hittable: Arc<dyn Hittable>,
@@ -13,13 +13,19 @@ pub struct Moving {
impl Moving {
fn offset_at(&self, time: f64) -> Vec3 {
- &self.offset_start + ((time - self.time_start) / (self.time_end - self.time_start)) * (&self.offset_end - &self.offset_start)
+ &self.offset_start
+ + ((time - self.time_start) / (self.time_end - self.time_start))
+ * (&self.offset_end - &self.offset_start)
}
}
impl Hittable for Moving {
fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord> {
- let moved_ray = Ray { origin: &ray.origin - &self.offset_at(ray.time), direction: ray.direction.clone(), time: ray.time };
+ let moved_ray = Ray {
+ origin: &ray.origin - &self.offset_at(ray.time),
+ direction: ray.direction.clone(),
+ time: ray.time,
+ };
let mut hit_record = self.hittable.hit(&moved_ray, t_min, t_max)?;
hit_record.p += self.offset_at(ray.time).clone();
let normal = hit_record.normal.clone();
@@ -29,12 +35,15 @@ impl Hittable for Moving {
fn bounding_box(&self, time_start: f64, time_end: f64) -> Option<AABB> {
let output_box = self.hittable.bounding_box(time_start, time_end)?;
- Some(AABB {
- minimum: &output_box.minimum + &self.offset_at(time_start),
- maximum: &output_box.maximum + &self.offset_at(time_start),
- }.surrounding_box(&AABB {
- minimum: &output_box.minimum + &self.offset_at(time_end),
- maximum: &output_box.maximum + &self.offset_at(time_end),
- }))
+ Some(
+ AABB {
+ minimum: &output_box.minimum + &self.offset_at(time_start),
+ maximum: &output_box.maximum + &self.offset_at(time_start),
+ }
+ .surrounding_box(&AABB {
+ minimum: &output_box.minimum + &self.offset_at(time_end),
+ maximum: &output_box.maximum + &self.offset_at(time_end),
+ }),
+ )
}
}
diff --git a/src/hittable/instance/rotate_x.rs b/src/hittable/instance/rotate_x.rs
index 4ebc04d..0e09223 100644
--- a/src/hittable/instance/rotate_x.rs
+++ b/src/hittable/instance/rotate_x.rs
@@ -1,6 +1,11 @@
use std::sync::Arc;
-use crate::{hittable::{HitRecord, Hittable, AABB}, ray::Ray, util::degrees_to_radians, vec3::{Point3, Vec3}};
+use crate::{
+ hittable::{HitRecord, Hittable, AABB},
+ ray::Ray,
+ util::degrees_to_radians,
+ vec3::{Point3, Vec3},
+};
pub struct RotateX {
hittable: Arc<dyn Hittable>,
@@ -14,11 +19,25 @@ impl RotateX {
let radians = degrees_to_radians(angle);
let sin_theta = radians.sin();
let cos_theta = radians.cos();
- match hittable.bounding_box(0.0, 1.0) { // TODO: passing in 0.0 and 1.0 for time seems suspicious.
- None => Self { hittable, sin_theta, cos_theta, aabb: None },
+ match hittable.bounding_box(0.0, 1.0) {
+ // TODO: passing in 0.0 and 1.0 for time seems suspicious.
+ None => Self {
+ hittable,
+ sin_theta,
+ cos_theta,
+ aabb: None,
+ },
Some(aabb) => {
- let mut min = Point3 { x: f64::INFINITY, y: f64::INFINITY, z: f64::INFINITY };
- let mut max = Point3 { x: -f64::INFINITY, y: -f64::INFINITY, z: -f64::INFINITY };
+ let mut min = Point3 {
+ x: f64::INFINITY,
+ y: f64::INFINITY,
+ z: f64::INFINITY,
+ };
+ let mut max = Point3 {
+ x: -f64::INFINITY,
+ y: -f64::INFINITY,
+ z: -f64::INFINITY,
+ };
for i in 0..2 {
for j in 0..2 {
for k in 0..2 {
@@ -28,15 +47,24 @@ impl RotateX {
let new_y = cos_theta * y + sin_theta * y;
let new_z = -sin_theta * y + cos_theta * z;
- let tester = Vec3 { y: new_y, x, z: new_z };
+ let tester = Vec3 {
+ y: new_y,
+ x,
+ z: new_z,
+ };
for c in 0..3 {
- *min.get_mut(c).unwrap() = min.get(c).unwrap().min(*tester.get(c).unwrap());
- *max.get_mut(c).unwrap() = max.get(c).unwrap().max(*tester.get(c).unwrap());
+ *min.get_mut(c).unwrap() =
+ min.get(c).unwrap().min(*tester.get(c).unwrap());
+ *max.get_mut(c).unwrap() =
+ max.get(c).unwrap().max(*tester.get(c).unwrap());
}
}
}
}
- let aabb = AABB { minimum: min, maximum: max };
+ let aabb = AABB {
+ minimum: min,
+ maximum: max,
+ };
Self {
hittable,
@@ -56,17 +84,21 @@ impl Hittable for RotateX {
origin.y = self.cos_theta * ray.origin.y - self.sin_theta * ray.origin.z;
origin.z = self.sin_theta * ray.origin.y + self.cos_theta * ray.origin.z;
-
+
direction.y = self.cos_theta * ray.direction.y - self.sin_theta * ray.direction.z;
direction.z = self.sin_theta * ray.direction.y + self.cos_theta * ray.direction.z;
- let rotated_ray = Ray { origin, direction, time: ray.time };
+ let rotated_ray = Ray {
+ origin,
+ direction,
+ time: ray.time,
+ };
let mut hit_record = self.hittable.hit(&rotated_ray, t_min, t_max)?;
let mut p = hit_record.p.clone();
let mut normal = hit_record.normal.clone();
- p.y = self.cos_theta * hit_record.p.y+ self.sin_theta * hit_record.p.z;
+ p.y = self.cos_theta * hit_record.p.y + self.sin_theta * hit_record.p.z;
p.z = -self.sin_theta * hit_record.p.y + self.cos_theta * hit_record.p.z;
normal.y = self.cos_theta * hit_record.normal.y + self.sin_theta * hit_record.normal.z;
diff --git a/src/hittable/instance/rotate_y.rs b/src/hittable/instance/rotate_y.rs
index 8611616..8f34c09 100644
--- a/src/hittable/instance/rotate_y.rs
+++ b/src/hittable/instance/rotate_y.rs
@@ -1,6 +1,11 @@
use std::sync::Arc;
-use crate::{hittable::{HitRecord, Hittable, AABB}, ray::Ray, util::degrees_to_radians, vec3::{Point3, Vec3}};
+use crate::{
+ hittable::{HitRecord, Hittable, AABB},
+ ray::Ray,
+ util::degrees_to_radians,
+ vec3::{Point3, Vec3},
+};
pub struct RotateY {
hittable: Arc<dyn Hittable>,
@@ -14,11 +19,25 @@ impl RotateY {
let radians = degrees_to_radians(angle);
let sin_theta = radians.sin();
let cos_theta = radians.cos();
- match hittable.bounding_box(0.0, 1.0) { // TODO: passing in 0.0 and 1.0 for time seems suspicious.
- None => Self { hittable, sin_theta, cos_theta, aabb: None },
+ match hittable.bounding_box(0.0, 1.0) {
+ // TODO: passing in 0.0 and 1.0 for time seems suspicious.
+ None => Self {
+ hittable,
+ sin_theta,
+ cos_theta,
+ aabb: None,
+ },
Some(aabb) => {
- let mut min = Point3 { x: f64::INFINITY, y: f64::INFINITY, z: f64::INFINITY };
- let mut max = Point3 { x: -f64::INFINITY, y: -f64::INFINITY, z: -f64::INFINITY };
+ let mut min = Point3 {
+ x: f64::INFINITY,
+ y: f64::INFINITY,
+ z: f64::INFINITY,
+ };
+ let mut max = Point3 {
+ x: -f64::INFINITY,
+ y: -f64::INFINITY,
+ z: -f64::INFINITY,
+ };
for i in 0..2 {
for j in 0..2 {
for k in 0..2 {
@@ -28,15 +47,24 @@ impl RotateY {
let new_x = cos_theta * x + sin_theta * z;
let new_z = -sin_theta * x + cos_theta * z;
- let tester = Vec3 { x: new_x, y, z: new_z };
+ let tester = Vec3 {
+ x: new_x,
+ y,
+ z: new_z,
+ };
for c in 0..3 {
- *min.get_mut(c).unwrap() = min.get(c).unwrap().min(*tester.get(c).unwrap());
- *max.get_mut(c).unwrap() = max.get(c).unwrap().max(*tester.get(c).unwrap());
+ *min.get_mut(c).unwrap() =
+ min.get(c).unwrap().min(*tester.get(c).unwrap());
+ *max.get_mut(c).unwrap() =
+ max.get(c).unwrap().max(*tester.get(c).unwrap());
}
}
}
}
- let aabb = AABB { minimum: min, maximum: max };
+ let aabb = AABB {
+ minimum: min,
+ maximum: max,
+ };
Self {
hittable,
@@ -56,11 +84,15 @@ impl Hittable for RotateY {
origin.x = self.cos_theta * ray.origin.x - self.sin_theta * ray.origin.z;
origin.z = self.sin_theta * ray.origin.x + self.cos_theta * ray.origin.z;
-
+
direction.x = self.cos_theta * ray.direction.x - self.sin_theta * ray.direction.z;
direction.z = self.sin_theta * ray.direction.x + self.cos_theta * ray.direction.z;
- let rotated_ray = Ray { origin, direction, time: ray.time };
+ let rotated_ray = Ray {
+ origin,
+ direction,
+ time: ray.time,
+ };
let mut hit_record = self.hittable.hit(&rotated_ray, t_min, t_max)?;
let mut p = hit_record.p.clone();
diff --git a/src/hittable/instance/rotate_z.rs b/src/hittable/instance/rotate_z.rs
index 119baca..38601ce 100644
--- a/src/hittable/instance/rotate_z.rs
+++ b/src/hittable/instance/rotate_z.rs
@@ -1,6 +1,11 @@
use std::sync::Arc;
-use crate::{hittable::{HitRecord, Hittable, AABB}, ray::Ray, util::degrees_to_radians, vec3::{Point3, Vec3}};
+use crate::{
+ hittable::{HitRecord, Hittable, AABB},
+ ray::Ray,
+ util::degrees_to_radians,
+ vec3::{Point3, Vec3},
+};
pub struct RotateZ {
hittable: Arc<dyn Hittable>,
@@ -14,11 +19,25 @@ impl RotateZ {
let radians = degrees_to_radians(angle);
let sin_theta = radians.sin();
let cos_theta = radians.cos();
- match hittable.bounding_box(0.0, 1.0) { // TODO: passing in 0.0 and 1.0 for time seems suspicious.
- None => Self { hittable, sin_theta, cos_theta, aabb: None },
+ match hittable.bounding_box(0.0, 1.0) {
+ // TODO: passing in 0.0 and 1.0 for time seems suspicious.
+ None => Self {
+ hittable,
+ sin_theta,
+ cos_theta,
+ aabb: None,
+ },
Some(aabb) => {
- let mut min = Point3 { x: f64::INFINITY, y: f64::INFINITY, z: f64::INFINITY };
- let mut max = Point3 { x: -f64::INFINITY, y: -f64::INFINITY, z: -f64::INFINITY };
+ let mut min = Point3 {
+ x: f64::INFINITY,
+ y: f64::INFINITY,
+ z: f64::INFINITY,
+ };
+ let mut max = Point3 {
+ x: -f64::INFINITY,
+ y: -f64::INFINITY,
+ z: -f64::INFINITY,
+ };
for i in 0..2 {
for j in 0..2 {
for k in 0..2 {
@@ -28,15 +47,24 @@ impl RotateZ {
let new_x = cos_theta * x + sin_theta * y;
let new_y = -sin_theta * x + cos_theta * y;
- let tester = Vec3 { x: new_x, z, y: new_y };
+ let tester = Vec3 {
+ x: new_x,
+ z,
+ y: new_y,
+ };
for c in 0..3 {
- *min.get_mut(c).unwrap() = min.get(c).unwrap().min(*tester.get(c).unwrap());
- *max.get_mut(c).unwrap() = max.get(c).unwrap().max(*tester.get(c).unwrap());
+ *min.get_mut(c).unwrap() =
+ min.get(c).unwrap().min(*tester.get(c).unwrap());
+ *max.get_mut(c).unwrap() =
+ max.get(c).unwrap().max(*tester.get(c).unwrap());
}
}
}
}
- let aabb = AABB { minimum: min, maximum: max };
+ let aabb = AABB {
+ minimum: min,
+ maximum: max,
+ };
Self {
hittable,
@@ -56,11 +84,15 @@ impl Hittable for RotateZ {
origin.x = self.cos_theta * ray.origin.x - self.sin_theta * ray.origin.y;
origin.y = self.sin_theta * ray.origin.x + self.cos_theta * ray.origin.y;
-
+
direction.x = self.cos_theta * ray.direction.x - self.sin_theta * ray.direction.y;
direction.y = self.sin_theta * ray.direction.x + self.cos_theta * ray.direction.y;
- let rotated_ray = Ray { origin, direction, time: ray.time };
+ let rotated_ray = Ray {
+ origin,
+ direction,
+ time: ray.time,
+ };
let mut hit_record = self.hittable.hit(&rotated_ray, t_min, t_max)?;
let mut p = hit_record.p.clone();
diff --git a/src/hittable/instance/translate.rs b/src/hittable/instance/translate.rs
index a9c8162..8478bd4 100644
--- a/src/hittable/instance/translate.rs
+++ b/src/hittable/instance/translate.rs
@@ -1,6 +1,10 @@
use std::sync::Arc;
-use crate::{hittable::{HitRecord, Hittable, AABB}, ray::Ray, vec3::Vec3};
+use crate::{
+ hittable::{HitRecord, Hittable, AABB},
+ ray::Ray,
+ vec3::Vec3,
+};
pub struct Translate {
pub hittable: Arc<dyn Hittable>,
@@ -9,7 +13,11 @@ pub struct Translate {
impl Hittable for Translate {
fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord> {
- let moved_ray = Ray { origin: &ray.origin - &self.offset, direction: ray.direction.clone(), time: ray.time };
+ let moved_ray = Ray {
+ origin: &ray.origin - &self.offset,
+ direction: ray.direction.clone(),
+ time: ray.time,
+ };
let mut hit_record = self.hittable.hit(&moved_ray, t_min, t_max)?;
hit_record.p += self.offset.clone();
let normal = hit_record.normal.clone();