meshmeshmesh/
ray.rs

1use crate::point::Point;
2use crate::vector::Vector;
3
4/// Represents a Ray object in three-dimensional space.
5pub struct Ray {
6    /// The origin [Point] from which we shoot the [Vector].
7    pub origin: Point,
8
9    /// The direction [Vector] of the [Ray].
10    pub direction: Vector,
11}
12
13impl PartialEq for Ray {
14    fn eq(&self, other: &Self) -> bool {
15
16        if self.origin != other.origin {
17            return false;
18        }
19
20        if self.direction != other.direction {
21            return false;
22        }
23
24        true
25    }
26}
27
28
29impl Ray {
30    /// Creates a new [Ray].
31    ///
32    /// # Example
33    ///
34    /// ```
35    /// use meshmeshmesh::point::Point;
36    /// use meshmeshmesh::ray::Ray;
37    /// use meshmeshmesh::vector::Vector;
38    ///
39    /// let result = Ray::new(Point::new(0.0, 1.0, -2.5), Vector::new(1.0, 0.0, 0.0));
40    ///
41    /// assert_eq!(result.origin.eq(&Point::new(0.0, 1.0, -2.5)), true);
42    /// assert_eq!(result.direction.eq(&Vector::new(1.0, 0.0, 0.0)), true);
43    /// ```
44    pub fn new(origin: Point, direction: Vector) -> Ray {Ray {origin, direction}}
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50    #[test]
51    fn test_new() {
52        let result = Ray::new(Point::new(0.0, 1.0, -2.5), Vector::new(1.0, 0.0, 0.0));
53
54        assert_eq!(result.origin.eq(&Point::new(0.0, 1.0, -2.5)), true);
55        assert_eq!(result.direction.eq(&Vector::new(1.0, 0.0, 0.0)), true);
56    }
57
58    #[test]
59    fn test_partialeq_true() {
60        let a = Ray::new(Point::new(0.0, 1.0, -2.5), Vector::new(1.0, 0.0, 0.0));
61        let b = Ray::new(Point::new(0.0, 1.0, -2.5), Vector::new(1.0, 0.0, 0.0));
62        assert_eq!(a.eq(&b), true);
63        assert_eq!(b.eq(&a), true);
64    }
65
66    #[test]
67    fn test_partialeq_different_origin_false() {
68        let a = Ray::new(Point::new(0.0, 1.0, -2.5), Vector::new(1.0, 0.0, 0.0));
69        let b = Ray::new(Point::new(0.0, 1.1, -2.5), Vector::new(1.0, 0.0, 0.0));
70        assert_eq!(a.eq(&b), false);
71        assert_eq!(b.eq(&a), false);
72    }
73
74    #[test]
75    fn test_partialeq_different_direction_false() {
76        let a = Ray::new(Point::new(0.0, 1.0, -2.5), Vector::new(1.0, 0.0, 0.0));
77        let b = Ray::new(Point::new(0.0, 1.0, -2.5), Vector::new(1.0, 0.0, 5.0));
78        assert_eq!(a.eq(&b), false);
79        assert_eq!(b.eq(&a), false);
80    }
81
82    #[test]
83    fn test_partialeq_different_all_false() {
84        let a = Ray::new(Point::new(0.0, 1.0, -2.5), Vector::new(1.0, 0.0, 0.0));
85        let b = Ray::new(Point::new(0.0, 1.1, -2.5), Vector::new(1.0, 0.0, 5.0));
86        assert_eq!(a.eq(&b), false);
87        assert_eq!(b.eq(&a), false);
88    }
89}