rgla/
vec3.rs

1use core::{f32, ops::*};
2
3const fn vec3(i: f32, j: f32, k: f32) -> Vec3 {
4    Vec3::new(i, j, k)
5}
6
7#[derive(Debug, Copy, Clone)]
8pub struct Vec3 {
9    pub i: f32,
10    pub j: f32,
11    pub k: f32,
12}
13
14impl Vec3 {
15    pub const fn new(i: f32, j: f32, k: f32) -> Self{
16        Self {i, j, k}
17    }
18
19    pub const ZERO: Self = Self::new(0.0, 0.0, 0.0);
20
21    pub const ONE: Self = Self::new(1.0, 1.0, 1.0);
22
23    pub const ONE_NEG: Self = Self::new(-1.0, -1.0, -1.0);
24
25    pub const I: Self = Self::new(1.0, 0.0, 0.0);
26
27    pub const J: Self = Self::new(0.0, 1.0, 0.0);
28
29    pub const K: Self = Self::new(0.0, 0.0, 1.0);
30
31    pub const I_NEG: Self = Self::new(-1.0, 0.0, 0.0);
32
33    pub const J_NEG: Self = Self::new(0.0, -1.0, 0.0);
34
35    pub const K_NEG: Self = Self::new(0.0, 0.0, -1.0);
36
37    pub fn norm(self) -> f32 {
38        f32::sqrt(f32::powi(self.i, 2) 
39                + f32::powi(self.j, 2) 
40                + f32::powi(self.k, 2))
41    }
42
43    pub fn norm_squared(self) -> f32 {
44        f32::powi(self.i, 2) 
45      + f32::powi(self.j, 2) 
46      + f32::powi(self.k, 2) 
47    }
48
49    pub fn abs(self) -> Self {
50        Self {
51            i: f32::abs(self.i),
52            j: f32::abs(self.j),
53            k: f32::abs(self.k),
54        }
55    }
56
57    pub fn cross(self, other: Vec3) -> Vec3 {
58        Self {
59            i: self.j * other.k - self.k * other.j,
60            j: self.i * other.k - self.k * other.i,
61            k: self.i * other.j - self.j * other.i,
62        }
63    }
64
65    pub fn dist(self, other: Vec3) -> f32 {
66        (self - other).norm()
67    }
68
69    pub fn dist_squared(self, other: Vec3) -> f32{
70        (self - other).norm_squared()
71    }
72
73    // be careful, divide by zero error!
74    pub fn normalize(self) -> Self {
75        Self {
76            i: self.i / self.norm(),
77            j: self.j / self.norm(),
78            k: self.k / self.norm(),
79        }
80    }
81
82    pub fn normalize_or_nan(self) -> Self {
83        if self.norm() > 0.0 {
84            Self {
85                i: self.i / self.norm(),
86                j: self.j / self.norm(),
87                k: self.k / self.norm(),
88            }
89        } else {
90            Self{
91                i: f32::NAN,
92                j: f32::NAN,
93                k: f32::NAN,
94            }
95        }
96    }
97
98    pub fn midpoint(self, other: Vec3) -> Self {
99        (self + other) * 0.5
100    }
101}
102
103// Operation Traits
104impl Add<Vec3> for Vec3 {
105    type Output = Self;
106    fn add(self, val: Self) -> Self{
107        Self {
108            i: self.i.add(val.i),
109            j: self.j.add(val.j),
110            k: self.k.add(val.k),
111        }
112    }
113}
114
115impl Add<f32> for Vec3 {
116    type Output = Self;
117    fn add(self, val: f32) -> Self{
118        Self {
119            i: self.i.add(val),
120            j: self.j.add(val),
121            k: self.k.add(val),
122        }
123    }
124}
125
126impl Sub<Vec3> for Vec3 {
127    type Output = Self;
128    fn sub(self, val: Self) -> Self{
129        Self {
130            i: self.i.sub(val.i),
131            j: self.j.sub(val.j),
132            k: self.k.sub(val.k),
133        }
134    }
135}
136
137impl Sub<f32> for Vec3 {
138    type Output = Self;
139    fn sub(self, val: f32) -> Self{
140        Self {
141            i: self.i.sub(val),
142            j: self.j.sub(val),
143            k: self.k.sub(val),
144        }
145    }
146}
147
148// Vector dot product
149impl Mul<Vec3> for Vec3 {
150    type Output = f32;
151    fn mul(self, val: Self) -> f32 {
152        self.i * val.i + self.j * val.j + self.k * val.k
153    }
154}
155
156impl Mul<f32> for Vec3 {
157    type Output = Self;
158    fn mul(self, val: f32) -> Self {
159        Self {
160            i: self.i.mul(val),
161            j: self.j.mul(val),
162            k: self.k.mul(val),
163        }
164    }
165}
166
167impl Div<f32> for Vec3 {
168    type Output = Self;
169    fn div(self, val: f32) -> Self {
170        Self {
171            i: self.i.div(val),
172            j: self.j.div(val),
173            k: self.k.div(val),
174        }
175    }
176}
177