Fix warnings

This commit is contained in:
Savanni D'Gerinel 2024-06-23 15:53:41 -04:00
parent af75bc20c8
commit f15fa9dd48
4 changed files with 31 additions and 51 deletions

View File

@ -10,6 +10,10 @@ pub struct Intersection<'a> {
pub struct Intersections<'a>(Vec<Intersection<'a>>); pub struct Intersections<'a>(Vec<Intersection<'a>>);
impl<'a> Intersections<'a> { impl<'a> Intersections<'a> {
pub fn is_empty(&'a self) -> bool {
self.0.is_empty()
}
pub fn len(&'a self) -> usize { pub fn len(&'a self) -> usize {
self.0.len() self.0.len()
} }

View File

@ -30,8 +30,8 @@ impl Ray {
let t2 = (-b + discriminant.sqrt()) / (2. * a); let t2 = (-b + discriminant.sqrt()) / (2. * a);
vec![ vec![
Intersection { t: t1, object: &s }, Intersection { t: t1, object: s },
Intersection { t: t2, object: &s }, Intersection { t: t2, object: s },
] ]
.into() .into()
} }
@ -61,7 +61,7 @@ mod tests {
#[test] #[test]
fn ray_intersects_sphere_at_two_points() { fn ray_intersects_sphere_at_two_points() {
let r = Ray::new(Point::new(0., 0., -5.), Vector::new(0., 0., 1.)); let r = Ray::new(Point::new(0., 0., -5.), Vector::new(0., 0., 1.));
let s = Sphere::new(); let s = Sphere::default();
let xs = r.intersect(&s); let xs = r.intersect(&s);
assert_eq!(xs.len(), 2); assert_eq!(xs.len(), 2);
assert_eq!(xs[0].t, 4.); assert_eq!(xs[0].t, 4.);
@ -73,7 +73,7 @@ mod tests {
#[test] #[test]
fn ray_tangents_sphere_at_one_point() { fn ray_tangents_sphere_at_one_point() {
let r = Ray::new(Point::new(0., 1., -5.), Vector::new(0., 0., 1.)); let r = Ray::new(Point::new(0., 1., -5.), Vector::new(0., 0., 1.));
let s = Sphere::new(); let s = Sphere::default();
let xs = r.intersect(&s); let xs = r.intersect(&s);
assert_eq!(xs.len(), 2); assert_eq!(xs.len(), 2);
assert_eq!(xs[0].t, 5.); assert_eq!(xs[0].t, 5.);
@ -83,7 +83,7 @@ mod tests {
#[test] #[test]
fn ray_misses_the_sphere() { fn ray_misses_the_sphere() {
let r = Ray::new(Point::new(0., 2., -5.), Vector::new(0., 0., 1.)); let r = Ray::new(Point::new(0., 2., -5.), Vector::new(0., 0., 1.));
let s = Sphere::new(); let s = Sphere::default();
let xs = r.intersect(&s); let xs = r.intersect(&s);
assert_eq!(xs.len(), 0); assert_eq!(xs.len(), 0);
} }
@ -91,7 +91,7 @@ mod tests {
#[test] #[test]
fn ray_originates_inside_the_sphere() { fn ray_originates_inside_the_sphere() {
let r = Ray::new(Point::new(0., 0., 0.), Vector::new(0., 0., 1.)); let r = Ray::new(Point::new(0., 0., 0.), Vector::new(0., 0., 1.));
let s = Sphere::new(); let s = Sphere::default();
let xs = r.intersect(&s); let xs = r.intersect(&s);
assert_eq!(xs.len(), 2); assert_eq!(xs.len(), 2);
assert_eq!(xs[0].t, -1.); assert_eq!(xs[0].t, -1.);
@ -101,7 +101,7 @@ mod tests {
#[test] #[test]
fn sphere_is_behind_the_ray() { fn sphere_is_behind_the_ray() {
let r = Ray::new(Point::new(0., 0., 5.), Vector::new(0., 0., 1.)); let r = Ray::new(Point::new(0., 0., 5.), Vector::new(0., 0., 1.));
let s = Sphere::new(); let s = Sphere::default();
let xs = r.intersect(&s); let xs = r.intersect(&s);
assert_eq!(xs.len(), 2); assert_eq!(xs.len(), 2);
assert_eq!(xs[0].t, -6.); assert_eq!(xs[0].t, -6.);
@ -110,7 +110,7 @@ mod tests {
#[test] #[test]
fn hit_all_intersections_are_positive() { fn hit_all_intersections_are_positive() {
let s = Sphere::new(); let s = Sphere::default();
let i1 = Intersection { t: 1., object: &s }; let i1 = Intersection { t: 1., object: &s };
let i2 = Intersection { t: 2., object: &s }; let i2 = Intersection { t: 2., object: &s };
let xs = Intersections::from(vec![i1.clone(), i2.clone()]); let xs = Intersections::from(vec![i1.clone(), i2.clone()]);
@ -120,7 +120,7 @@ mod tests {
#[test] #[test]
fn hit_some_intersections_are_negative() { fn hit_some_intersections_are_negative() {
let s = Sphere::new(); let s = Sphere::default();
let i1 = Intersection { t: -1., object: &s }; let i1 = Intersection { t: -1., object: &s };
let i2 = Intersection { t: 1., object: &s }; let i2 = Intersection { t: 1., object: &s };
let xs = Intersections::from(vec![i1, i2.clone()]); let xs = Intersections::from(vec![i1, i2.clone()]);
@ -130,7 +130,7 @@ mod tests {
#[test] #[test]
fn hit_all_intersections_are_negative() { fn hit_all_intersections_are_negative() {
let s = Sphere::new(); let s = Sphere::default();
let i1 = Intersection { t: -2., object: &s }; let i1 = Intersection { t: -2., object: &s };
let i2 = Intersection { t: -1., object: &s }; let i2 = Intersection { t: -1., object: &s };
let xs = Intersections::from(vec![i1, i2]); let xs = Intersections::from(vec![i1, i2]);
@ -140,7 +140,7 @@ mod tests {
#[test] #[test]
fn hit_is_always_lowest_nonnegative() { fn hit_is_always_lowest_nonnegative() {
let s = Sphere::new(); let s = Sphere::default();
let i1 = Intersection { t: 5., object: &s }; let i1 = Intersection { t: 5., object: &s };
let i2 = Intersection { t: 7., object: &s }; let i2 = Intersection { t: 7., object: &s };
let i3 = Intersection { t: -3., object: &s }; let i3 = Intersection { t: -3., object: &s };
@ -171,7 +171,7 @@ mod tests {
#[test] #[test]
fn intersect_scaled_sphere_with_ray() { fn intersect_scaled_sphere_with_ray() {
let r = Ray::new(Point::new(0., 0., -5.), Vector::new(0., 0., 1.)); let r = Ray::new(Point::new(0., 0., -5.), Vector::new(0., 0., 1.));
let mut s = Sphere::new(); let mut s = Sphere::default();
s.set_transformation(scaling(2., 2., 2.)); s.set_transformation(scaling(2., 2., 2.));
let xs = r.intersect(&s); let xs = r.intersect(&s);
assert_eq!(xs.len(), 2); assert_eq!(xs.len(), 2);
@ -182,7 +182,7 @@ mod tests {
#[test] #[test]
fn intersect_translated_sphere_with_ray() { fn intersect_translated_sphere_with_ray() {
let r = Ray::new(Point::new(0., 0., -5.), Vector::new(0., 0., 1.)); let r = Ray::new(Point::new(0., 0., -5.), Vector::new(0., 0., 1.));
let mut s = Sphere::new(); let mut s = Sphere::default();
s.set_transformation(translation(5., 0., 0.)); s.set_transformation(translation(5., 0., 0.));
let xs = r.intersect(&s); let xs = r.intersect(&s);
assert_eq!(xs.len(), 0); assert_eq!(xs.len(), 0);

View File

@ -7,13 +7,6 @@ pub struct Sphere {
} }
impl Sphere { impl Sphere {
pub fn new() -> Self {
Self {
origin: Point::new(0., 0., 0.),
transformation: Matrix::identity(),
}
}
pub fn transformation(&self) -> &Matrix { pub fn transformation(&self) -> &Matrix {
&self.transformation &self.transformation
} }
@ -23,20 +16,29 @@ impl Sphere {
} }
} }
impl Default for Sphere {
fn default() -> Self {
Self {
origin: Point::new(0., 0., 0.),
transformation: Matrix::identity(),
}
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::transforms::translation;
use super::*; use super::*;
use crate::transforms::translation;
#[test] #[test]
fn sphere_has_default_transformation() { fn sphere_has_default_transformation() {
let s = Sphere::new(); let s = Sphere::default();
assert_eq!(s.transformation(), &Matrix::identity()); assert_eq!(s.transformation(), &Matrix::identity());
} }
#[test] #[test]
fn change_a_spheres_transformation() { fn change_a_spheres_transformation() {
let mut s = Sphere::new(); let mut s = Sphere::default();
let t = translation(2., 3., 4.); let t = translation(2., 3., 4.);
s.set_transformation(t.clone()); s.set_transformation(t.clone());
assert_eq!(*s.transformation(), t); assert_eq!(*s.transformation(), t);

View File

@ -34,20 +34,6 @@ impl std::ops::Add<&Tuple> for &Tuple {
} }
} }
impl std::ops::Add<Tuple> for &Tuple {
type Output = Tuple;
fn add(self, r: Tuple) -> Self::Output {
self + &r
}
}
impl std::ops::Add<&Tuple> for Tuple {
type Output = Tuple;
fn add(self, r: &Tuple) -> Self::Output {
&self + r
}
}
impl std::ops::Add<Tuple> for Tuple { impl std::ops::Add<Tuple> for Tuple {
type Output = Tuple; type Output = Tuple;
fn add(self, r: Tuple) -> Self::Output { fn add(self, r: Tuple) -> Self::Output {
@ -62,20 +48,6 @@ impl std::ops::Sub<&Tuple> for &Tuple {
} }
} }
impl std::ops::Sub<Tuple> for &Tuple {
type Output = Tuple;
fn sub(self, r: Tuple) -> Self::Output {
self - &r
}
}
impl std::ops::Sub<&Tuple> for Tuple {
type Output = Tuple;
fn sub(self, r: &Tuple) -> Self::Output {
&self - r
}
}
impl std::ops::Sub<Tuple> for Tuple { impl std::ops::Sub<Tuple> for Tuple {
type Output = Tuple; type Output = Tuple;
fn sub(self, r: Tuple) -> Self::Output { fn sub(self, r: Tuple) -> Self::Output {
@ -111,6 +83,7 @@ impl std::ops::Mul<f64> for &Tuple {
impl std::ops::Mul<f64> for Tuple { impl std::ops::Mul<f64> for Tuple {
type Output = Tuple; type Output = Tuple;
#[allow(clippy::op_ref)]
fn mul(self, scalar: f64) -> Self::Output { fn mul(self, scalar: f64) -> Self::Output {
&self * scalar &self * scalar
} }
@ -130,6 +103,7 @@ impl std::ops::Div<f64> for &Tuple {
impl std::ops::Div<f64> for Tuple { impl std::ops::Div<f64> for Tuple {
type Output = Tuple; type Output = Tuple;
#[allow(clippy::op_ref)]
fn div(self, scalar: f64) -> Self::Output { fn div(self, scalar: f64) -> Self::Output {
&self / scalar &self / scalar
} }