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
use crate::math::vec3::Vec3;
pub struct Ray {
pub origin: Vec3,
pub dir: Vec3,
}
impl Default for Ray {
fn default() -> Self {
Ray {
origin: Vec3::default(),
dir: Vec3::make(0.0, 0.0, 1.0),
}
}
}
impl Ray {
#[inline]
pub fn from_two_points(begin: Vec3, end: Vec3) -> Option<Ray> {
let dir = end - begin;
if dir.len() >= std::f32::EPSILON {
Some(Ray { origin: begin, dir })
} else {
None
}
}
#[inline]
pub fn sphere_intersection(&self, position: Vec3, radius: f32) -> Option<(Vec3, Vec3)> {
let d = self.origin - position;
let a = self.dir.dot(&self.dir);
let b = 2.0 * self.dir.dot(&d);
let c = d.dot(&d) - radius * radius;
let discriminant = b * b - 4.0 * a * c;
if discriminant < 0.0 {
return None;
}
let discr_root = discriminant.sqrt();
let r1 = (-b + discr_root) / 2.0;
let r2 = (-b - discr_root) / 2.0;
Some((self.origin + self.dir.scale(r1), self.origin + self.dir.scale(r2)))
}
#[inline]
pub fn is_intersect_sphere(&self, position: Vec3, radius: f32) -> bool {
let d = self.origin - position;
let a = self.dir.dot(&self.dir);
let b = 2.0 * self.dir.dot(&d);
let c = d.dot(&d) - radius * radius;
let discriminant = b * b - 4.0 * a * c;
discriminant >= 0.0
}
#[inline]
pub fn project_point(&self, point: Vec3) -> f32 {
(point - self.origin).dot(&self.dir) / self.dir.sqr_len()
}
#[inline]
pub fn get_point(&self, t: f32) -> Vec3 {
self.origin + self.dir.scale(t)
}
}