Fix warnings
This commit is contained in:
parent
af75bc20c8
commit
f15fa9dd48
|
@ -10,6 +10,10 @@ pub struct Intersection<'a> {
|
|||
pub struct Intersections<'a>(Vec<Intersection<'a>>);
|
||||
|
||||
impl<'a> Intersections<'a> {
|
||||
pub fn is_empty(&'a self) -> bool {
|
||||
self.0.is_empty()
|
||||
}
|
||||
|
||||
pub fn len(&'a self) -> usize {
|
||||
self.0.len()
|
||||
}
|
||||
|
|
|
@ -30,8 +30,8 @@ impl Ray {
|
|||
let t2 = (-b + discriminant.sqrt()) / (2. * a);
|
||||
|
||||
vec![
|
||||
Intersection { t: t1, object: &s },
|
||||
Intersection { t: t2, object: &s },
|
||||
Intersection { t: t1, object: s },
|
||||
Intersection { t: t2, object: s },
|
||||
]
|
||||
.into()
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ mod tests {
|
|||
#[test]
|
||||
fn ray_intersects_sphere_at_two_points() {
|
||||
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);
|
||||
assert_eq!(xs.len(), 2);
|
||||
assert_eq!(xs[0].t, 4.);
|
||||
|
@ -73,7 +73,7 @@ mod tests {
|
|||
#[test]
|
||||
fn ray_tangents_sphere_at_one_point() {
|
||||
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);
|
||||
assert_eq!(xs.len(), 2);
|
||||
assert_eq!(xs[0].t, 5.);
|
||||
|
@ -83,7 +83,7 @@ mod tests {
|
|||
#[test]
|
||||
fn ray_misses_the_sphere() {
|
||||
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);
|
||||
assert_eq!(xs.len(), 0);
|
||||
}
|
||||
|
@ -91,7 +91,7 @@ mod tests {
|
|||
#[test]
|
||||
fn ray_originates_inside_the_sphere() {
|
||||
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);
|
||||
assert_eq!(xs.len(), 2);
|
||||
assert_eq!(xs[0].t, -1.);
|
||||
|
@ -101,7 +101,7 @@ mod tests {
|
|||
#[test]
|
||||
fn sphere_is_behind_the_ray() {
|
||||
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);
|
||||
assert_eq!(xs.len(), 2);
|
||||
assert_eq!(xs[0].t, -6.);
|
||||
|
@ -110,7 +110,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn hit_all_intersections_are_positive() {
|
||||
let s = Sphere::new();
|
||||
let s = Sphere::default();
|
||||
let i1 = Intersection { t: 1., object: &s };
|
||||
let i2 = Intersection { t: 2., object: &s };
|
||||
let xs = Intersections::from(vec![i1.clone(), i2.clone()]);
|
||||
|
@ -120,7 +120,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn hit_some_intersections_are_negative() {
|
||||
let s = Sphere::new();
|
||||
let s = Sphere::default();
|
||||
let i1 = Intersection { t: -1., object: &s };
|
||||
let i2 = Intersection { t: 1., object: &s };
|
||||
let xs = Intersections::from(vec![i1, i2.clone()]);
|
||||
|
@ -130,7 +130,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn hit_all_intersections_are_negative() {
|
||||
let s = Sphere::new();
|
||||
let s = Sphere::default();
|
||||
let i1 = Intersection { t: -2., object: &s };
|
||||
let i2 = Intersection { t: -1., object: &s };
|
||||
let xs = Intersections::from(vec![i1, i2]);
|
||||
|
@ -140,7 +140,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn hit_is_always_lowest_nonnegative() {
|
||||
let s = Sphere::new();
|
||||
let s = Sphere::default();
|
||||
let i1 = Intersection { t: 5., object: &s };
|
||||
let i2 = Intersection { t: 7., object: &s };
|
||||
let i3 = Intersection { t: -3., object: &s };
|
||||
|
@ -171,7 +171,7 @@ mod tests {
|
|||
#[test]
|
||||
fn intersect_scaled_sphere_with_ray() {
|
||||
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.));
|
||||
let xs = r.intersect(&s);
|
||||
assert_eq!(xs.len(), 2);
|
||||
|
@ -182,7 +182,7 @@ mod tests {
|
|||
#[test]
|
||||
fn intersect_translated_sphere_with_ray() {
|
||||
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.));
|
||||
let xs = r.intersect(&s);
|
||||
assert_eq!(xs.len(), 0);
|
||||
|
|
|
@ -7,13 +7,6 @@ pub struct Sphere {
|
|||
}
|
||||
|
||||
impl Sphere {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
origin: Point::new(0., 0., 0.),
|
||||
transformation: Matrix::identity(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn transformation(&self) -> &Matrix {
|
||||
&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)]
|
||||
mod test {
|
||||
use crate::transforms::translation;
|
||||
use super::*;
|
||||
use crate::transforms::translation;
|
||||
|
||||
#[test]
|
||||
fn sphere_has_default_transformation() {
|
||||
let s = Sphere::new();
|
||||
let s = Sphere::default();
|
||||
assert_eq!(s.transformation(), &Matrix::identity());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn change_a_spheres_transformation() {
|
||||
let mut s = Sphere::new();
|
||||
let mut s = Sphere::default();
|
||||
let t = translation(2., 3., 4.);
|
||||
s.set_transformation(t.clone());
|
||||
assert_eq!(*s.transformation(), t);
|
||||
|
|
|
@ -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 {
|
||||
type Output = Tuple;
|
||||
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 {
|
||||
type Output = Tuple;
|
||||
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 {
|
||||
type Output = Tuple;
|
||||
#[allow(clippy::op_ref)]
|
||||
fn mul(self, scalar: f64) -> Self::Output {
|
||||
&self * scalar
|
||||
}
|
||||
|
@ -130,6 +103,7 @@ impl std::ops::Div<f64> for &Tuple {
|
|||
|
||||
impl std::ops::Div<f64> for Tuple {
|
||||
type Output = Tuple;
|
||||
#[allow(clippy::op_ref)]
|
||||
fn div(self, scalar: f64) -> Self::Output {
|
||||
&self / scalar
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue