intersections: moving another doctest to unit
This commit is contained in:
parent
3838efd134
commit
9006671a26
@ -20,47 +20,12 @@ impl<'i> PartialEq for Intersection<'i> {
|
|||||||
|
|
||||||
impl<'i> Intersection<'i> {
|
impl<'i> Intersection<'i> {
|
||||||
/// Create new `Intersection` at the given `t` that hits the given `object`.
|
/// Create new `Intersection` at the given `t` that hits the given `object`.
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
/// ```
|
|
||||||
/// use rtchallenge::{intersections::Intersection, shapes::Shape};
|
|
||||||
///
|
|
||||||
/// // An intersection ecapsulates t and object.
|
|
||||||
/// let s = Shape::sphere();
|
|
||||||
/// let i = Intersection::new(3.5, &s);
|
|
||||||
/// assert_eq!(i.t, 3.5);
|
|
||||||
/// assert_eq!(i.object, &s);
|
|
||||||
/// ```
|
|
||||||
pub fn new(t: Float, object: &Shape) -> Intersection {
|
pub fn new(t: Float, object: &Shape) -> Intersection {
|
||||||
Intersection { t, object }
|
Intersection { t, object }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Aggregates `Intersection`s.
|
/// Aggregates `Intersection`s.
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
/// ```
|
|
||||||
/// use rtchallenge::{
|
|
||||||
/// intersections::{Intersection, Intersections},
|
|
||||||
/// rays::Ray,
|
|
||||||
/// shapes::{intersect, Shape},
|
|
||||||
/// tuples::Tuple,
|
|
||||||
/// };
|
|
||||||
///
|
|
||||||
/// let s = Shape::sphere();
|
|
||||||
/// let i1 = Intersection::new(1., &s);
|
|
||||||
/// let i2 = Intersection::new(2., &s);
|
|
||||||
/// let xs = Intersections::new(vec![i1, i2]);
|
|
||||||
/// assert_eq!(xs.len(), 2);
|
|
||||||
/// assert_eq!(xs[0].t, 1.);
|
|
||||||
/// assert_eq!(xs[1].t, 2.);
|
|
||||||
///
|
|
||||||
/// let r = Ray::new(Tuple::point(0., 0., -5.), Tuple::vector(0., 0., 1.));
|
|
||||||
/// let xs = intersect(&s, &r);
|
|
||||||
/// assert_eq!(xs.len(), 2);
|
|
||||||
/// assert_eq!(xs[0].object, &s);
|
|
||||||
/// assert_eq!(xs[1].object, &s);
|
|
||||||
/// ```
|
|
||||||
#[derive(Debug, Default, PartialEq)]
|
#[derive(Debug, Default, PartialEq)]
|
||||||
pub struct Intersections<'i>(Vec<Intersection<'i>>);
|
pub struct Intersections<'i>(Vec<Intersection<'i>>);
|
||||||
|
|
||||||
@ -209,11 +174,36 @@ mod tests {
|
|||||||
materials::MaterialBuilder,
|
materials::MaterialBuilder,
|
||||||
matrices::{scaling, translation},
|
matrices::{scaling, translation},
|
||||||
rays::Ray,
|
rays::Ray,
|
||||||
shapes::{glass_sphere, Shape},
|
shapes::{glass_sphere, intersect, Shape},
|
||||||
tuples::{point, vector},
|
tuples::{point, vector},
|
||||||
Float, EPSILON,
|
Float, EPSILON,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn intersection() {
|
||||||
|
// An intersection ecapsulates t and object.
|
||||||
|
let s = Shape::sphere();
|
||||||
|
let i = Intersection::new(3.5, &s);
|
||||||
|
assert_eq!(i.t, 3.5);
|
||||||
|
assert_eq!(i.object, &s);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn intersections() {
|
||||||
|
let s = Shape::sphere();
|
||||||
|
let i1 = Intersection::new(1., &s);
|
||||||
|
let i2 = Intersection::new(2., &s);
|
||||||
|
let xs = Intersections::new(vec![i1, i2]);
|
||||||
|
assert_eq!(xs.len(), 2);
|
||||||
|
assert_eq!(xs[0].t, 1.);
|
||||||
|
assert_eq!(xs[1].t, 2.);
|
||||||
|
|
||||||
|
let r = Ray::new(point(0., 0., -5.), vector(0., 0., 1.));
|
||||||
|
let xs = intersect(&s, &r);
|
||||||
|
assert_eq!(xs.len(), 2);
|
||||||
|
assert_eq!(xs[0].object, &s);
|
||||||
|
assert_eq!(xs[1].object, &s);
|
||||||
|
}
|
||||||
|
|
||||||
mod hit {
|
mod hit {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user