1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* // Copyright (c) 2021 Feng Yang
* //
* // I am making my contributions/submissions to this project solely in my
* // personal capacity and am not conveying any rights to any intellectual
* // property of any third parties.
*/
use num::Float;
use crate::vector2::Vector2;
///
/// # Class for 2-D ray.
///
/// - tparam T The value type.
///
pub struct Ray2<T: Float> {
/// The origin of the ray.
pub origin: Vector2<T>,
/// The direction of the ray.
pub direction: Vector2<T>,
}
/// Float-type 2-D ray.
pub type Ray2F = Ray2<f32>;
/// Double-type 2-D ray.
pub type Ray2D = Ray2<f64>;
impl<T: Float> Ray2<T> {
/// Constructs an empty ray that points (1, 0) from (0, 0).
/// ```
/// use vox_geometry_rust::ray2::Ray2D;
/// use vox_geometry_rust::vector2::Vector2D;
/// let ray = Ray2D::new_default();
/// assert_eq!(Vector2D::new_default(), ray.origin);
/// assert_eq!(Vector2D::new(1.0, 0.0), ray.direction);
/// ```
pub fn new_default() -> Ray2<T> {
return Ray2 {
origin: Vector2::new_default(),
direction: Vector2::new(T::one(), T::zero()),
};
}
/// Constructs a ray with given origin and direction.
/// ```
/// use vox_geometry_rust::ray2::Ray2D;
/// use vox_geometry_rust::vector2::Vector2D;
/// let ray2 = Ray2D::new(Vector2D::new_lst([1.0, 2.0]), Vector2D::new_lst([3.0, 4.0]));
/// assert_eq!(Vector2D::new(1.0, 2.0), ray2.origin);
/// assert_eq!(Vector2D::new(3.0, 4.0).normalized(), ray2.direction);
/// ```
pub fn new(new_origin: Vector2<T>, new_direction: Vector2<T>) -> Ray2<T> {
return Ray2 {
origin: new_origin,
direction: new_direction.normalized(),
};
}
/// Returns a point on the ray at distance **t**.
/// ```
/// use vox_geometry_rust::ray2::Ray2D;
/// use vox_geometry_rust::vector2::Vector2D;
/// let ray = Ray2D::new_default();
/// assert_eq!(Vector2D::new(4.5, 0.0), ray.point_at(4.5));
/// ```
pub fn point_at(&self, t: T) -> Vector2<T> {
return self.origin + self.direction * t;
}
}