rays: move tests from doctest to unit.
This commit is contained in:
parent
4352c03d20
commit
e4846de25b
@ -10,17 +10,6 @@ pub struct Ray {
|
|||||||
impl Ray {
|
impl Ray {
|
||||||
/// Create a ray with the given origin point and direction vector.
|
/// Create a ray with the given origin point and direction vector.
|
||||||
/// Will panic if origin not a point or direction not a vector.
|
/// Will panic if origin not a point or direction not a vector.
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
/// ```
|
|
||||||
/// use rtchallenge::{rays::Ray, tuples::Tuple};
|
|
||||||
///
|
|
||||||
/// let origin = Tuple::point(1., 2., 3.);
|
|
||||||
/// let direction = Tuple::vector(4., 5., 6.);
|
|
||||||
/// let r = Ray::new(origin, direction);
|
|
||||||
/// assert_eq!(r.origin, origin);
|
|
||||||
/// assert_eq!(r.direction, direction);
|
|
||||||
/// ```
|
|
||||||
pub fn new(origin: Tuple, direction: Tuple) -> Ray {
|
pub fn new(origin: Tuple, direction: Tuple) -> Ray {
|
||||||
assert!(origin.is_point(), "Ray origin must be a point");
|
assert!(origin.is_point(), "Ray origin must be a point");
|
||||||
assert!(direction.is_vector(), "Ray direction must be a vector");
|
assert!(direction.is_vector(), "Ray direction must be a vector");
|
||||||
@ -28,41 +17,11 @@ impl Ray {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Compute a point from the given distance along the `Ray`.
|
/// Compute a point from the given distance along the `Ray`.
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
/// ```
|
|
||||||
/// use rtchallenge::{rays::Ray, tuples::Tuple};
|
|
||||||
///
|
|
||||||
/// let r = Ray::new(Tuple::point(2., 3., 4.), Tuple::vector(1., 0., 0.));
|
|
||||||
/// assert_eq!(r.position(0.), Tuple::point(2., 3., 4.));
|
|
||||||
/// assert_eq!(r.position(1.), Tuple::point(3., 3., 4.));
|
|
||||||
/// assert_eq!(r.position(-1.), Tuple::point(1., 3., 4.));
|
|
||||||
/// assert_eq!(r.position(2.5), Tuple::point(4.5, 3., 4.));
|
|
||||||
/// ```
|
|
||||||
pub fn position(&self, t: Float) -> Tuple {
|
pub fn position(&self, t: Float) -> Tuple {
|
||||||
self.origin + self.direction * t
|
self.origin + self.direction * t
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Apply Matrix4x4 transforms to Ray.
|
/// Apply Matrix4x4 transforms to Ray.
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
/// ```
|
|
||||||
/// use rtchallenge::{matrices::Matrix4x4, rays::Ray, tuples::Tuple};
|
|
||||||
///
|
|
||||||
/// // Translating a ray
|
|
||||||
/// let r = Ray::new(Tuple::point(1., 2., 3.), Tuple::vector(0., 1., 0.));
|
|
||||||
/// let m = Matrix4x4::translation(3., 4., 5.);
|
|
||||||
/// let r2 = r.transform(m);
|
|
||||||
/// assert_eq!(r2.origin, Tuple::point(4., 6., 8.));
|
|
||||||
/// assert_eq!(r2.direction, Tuple::vector(0., 1., 0.));
|
|
||||||
///
|
|
||||||
/// // Scaling a ray
|
|
||||||
/// let r = Ray::new(Tuple::point(1., 2., 3.), Tuple::vector(0., 1., 0.));
|
|
||||||
/// let m = Matrix4x4::scaling(2., 3., 4.);
|
|
||||||
/// let r2 = r.transform(m);
|
|
||||||
/// assert_eq!(r2.origin, Tuple::point(2., 6., 12.));
|
|
||||||
/// assert_eq!(r2.direction, Tuple::vector(0., 3., 0.));
|
|
||||||
/// ```
|
|
||||||
pub fn transform(&self, m: Matrix4x4) -> Ray {
|
pub fn transform(&self, m: Matrix4x4) -> Ray {
|
||||||
Ray {
|
Ray {
|
||||||
origin: m * self.origin,
|
origin: m * self.origin,
|
||||||
@ -70,3 +29,46 @@ impl Ray {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::{
|
||||||
|
matrices::Matrix4x4,
|
||||||
|
rays::Ray,
|
||||||
|
tuples::{point, vector},
|
||||||
|
};
|
||||||
|
#[test]
|
||||||
|
fn create() {
|
||||||
|
let origin = point(1., 2., 3.);
|
||||||
|
let direction = vector(4., 5., 6.);
|
||||||
|
let r = Ray::new(origin, direction);
|
||||||
|
assert_eq!(r.origin, origin);
|
||||||
|
assert_eq!(r.direction, direction);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn position() {
|
||||||
|
let r = Ray::new(point(2., 3., 4.), vector(1., 0., 0.));
|
||||||
|
assert_eq!(r.position(0.), point(2., 3., 4.));
|
||||||
|
assert_eq!(r.position(1.), point(3., 3., 4.));
|
||||||
|
assert_eq!(r.position(-1.), point(1., 3., 4.));
|
||||||
|
assert_eq!(r.position(2.5), point(4.5, 3., 4.));
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn transform_translating_ray() {
|
||||||
|
// Translating a ray
|
||||||
|
let r = Ray::new(point(1., 2., 3.), vector(0., 1., 0.));
|
||||||
|
let m = Matrix4x4::translation(3., 4., 5.);
|
||||||
|
let r2 = r.transform(m);
|
||||||
|
assert_eq!(r2.origin, point(4., 6., 8.));
|
||||||
|
assert_eq!(r2.direction, vector(0., 1., 0.));
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn transform_scaling_ray() {
|
||||||
|
// Scaling a ray
|
||||||
|
let r = Ray::new(point(1., 2., 3.), vector(0., 1., 0.));
|
||||||
|
let m = Matrix4x4::scaling(2., 3., 4.);
|
||||||
|
let r2 = r.transform(m);
|
||||||
|
assert_eq!(r2.origin, point(2., 6., 12.));
|
||||||
|
assert_eq!(r2.direction, vector(0., 3., 0.));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user