radiant_utils/math/
vec3.rs

1use prelude::*;
2use super::Vector;
3
4/// A 3-dimensional vector.
5#[derive(Copy, Clone)]
6#[cfg_attr(feature = "serialize-serde", derive(Deserialize, Serialize))]
7pub struct Vec3<T = f32>(pub T, pub T, pub T);
8
9impl<T> Vec3<T> where T: Float {
10    /// Creates a new instances.
11    pub fn new() -> Vec3<T> {
12        Vec3::<T>(T::zero(), T::zero(), T::zero())
13    }
14    /// Returns the length of the vector
15    pub fn len(self: &Self) -> T {
16        (self.0*self.0 + self.1*self.1 + self.2*self.2).sqrt()
17    }
18    /// Returns the dot-product of the vectors.
19    pub fn dot(self: &Self, other: &Vec3<T>) -> T {
20        self.0 * other.0 + self.1 * other.1 + self.1 * other.2
21    }
22}
23
24impl<T> Vector<T> for Vec3<T> where T: Copy {
25    fn as_vec3(&self, _: T) -> Vec3<T> {
26        *self
27    }
28}
29
30// from/to array
31
32impl<T> From<[ T; 3 ]> for Vec3<T> where T: Copy {
33    fn from(source: [ T; 3 ]) -> Self {
34        Vec3(source[0], source[1], source[2])
35    }
36}
37
38impl From<Vec3<f32>> for [ f32; 3 ] {
39    fn from(source: Vec3<f32>) -> Self {
40        [ source.0, source.1, source.2 ]
41    }
42}
43
44impl From<Vec3<f64>> for [ f64; 3 ] {
45    fn from(source: Vec3<f64>) -> Self {
46        [ source.0, source.1, source.2 ]
47    }
48}
49
50// from/to tuple struct
51
52impl<T> From<(T, T, T)> for Vec3<T> {
53    fn from(source: (T, T, T)) -> Self {
54        Vec3(source.0, source.1, source.2)
55    }
56}
57
58impl From<Vec3<f32>> for (f32, f32, f32) {
59    fn from(source: Vec3<f32>) -> Self {
60        (source.0, source.1, source.2)
61    }
62}
63
64impl From<Vec3<f64>> for (f64, f64, f64) {
65    fn from(source: Vec3<f64>) -> Self {
66        (source.0, source.1, source.2)
67    }
68}
69
70// default
71
72impl<T> Default for Vec3<T> where T: Float {
73    fn default() -> Self {
74        Vec3(T::zero(), T::zero(), T::zero())
75    }
76}
77
78// operators
79
80impl<T> Neg for Vec3<T> where T: Float {
81    type Output = Vec3<T>;
82    fn neg(self) -> Vec3<T> {
83        Vec3::<T>(-self.0, -self.1, -self.2)
84    }
85}
86
87impl<T> Add for Vec3<T> where T: Float {
88    type Output = Vec3<T>;
89    fn add(self, other: Vec3<T>) -> Vec3<T> {
90        Vec3::<T>(self.0 + other.0, self.1 + other.1, self.2 + other.2)
91    }
92}
93
94impl<T> AddAssign for Vec3<T> where T: Float {
95    fn add_assign(self: &mut Self, other: Vec3<T>) {
96        *self = Vec3::<T> (
97            self.0 + other.0,
98            self.1 + other.1,
99            self.2 + other.2
100        )
101    }
102}
103
104impl<T> Sub for Vec3<T> where T: Float {
105    type Output = Vec3<T>;
106    fn sub(self, other: Vec3<T>) -> Vec3<T> {
107        Vec3::<T>(self.0 - other.0, self.1 - other.1, self.2 - other.2)
108    }
109}
110
111impl<T> SubAssign for Vec3<T> where T: Float {
112    fn sub_assign(self: &mut Self, other: Vec3<T>) {
113        *self = Vec3::<T> (
114            self.0 - other.0,
115            self.1 - other.1,
116            self.2 - other.2,
117        )
118    }
119}
120
121impl<T> Mul<Vec3<T>> for Vec3<T> where T: Float {
122    type Output = Vec3<T>;
123    /// Multiplies individual vector components with those of the given vector.
124    fn mul(self, other: Vec3<T>) -> Vec3<T> {
125        Vec3::<T>(self.0 * other.0, self.1 * other.1, self.2 * other.2)
126    }
127}
128
129impl<T> MulAssign<Vec3<T>> for Vec3<T> where T: Float {
130    /// Mutates the vector by multiplying its components with those of the given vector.
131    fn mul_assign(&mut self, other: Vec3<T>) {
132        *self = Vec3::<T>(self.0 * other.0, self.1 * other.1, self.2 * other.2)
133    }
134}
135
136impl<T> Mul<T> for Vec3<T> where T: Float {
137    type Output = Vec3<T>;
138    /// Multiplies the vector with given scalar operand.
139    fn mul(self, other: T) -> Vec3<T> {
140        Vec3::<T>(self.0 * other, self.1 * other, self.2 * other)
141    }
142}
143
144impl<T> MulAssign<T> for Vec3<T> where T: Float {
145    /// Mutates the vector by multiplying it with the scalar operand.
146    fn mul_assign(&mut self, other: T) {
147        *self = Vec3::<T>(self.0 * other, self.1 * other, self.2 * other)
148    }
149}
150
151impl<T> Div<Vec3<T>> for Vec3<T> where T: Float {
152    type Output = Vec3<T>;
153    /// Divides individual vector components with those of the given vector.
154    fn div(self, other: Vec3<T>) -> Vec3<T> {
155        Vec3::<T>(self.0 / other.0, self.1 / other.1, self.2 / other.2)
156    }
157}
158
159impl<T> DivAssign<Vec3<T>> for Vec3<T> where T: Float {
160    /// Mutates the vector by dividing its components with those of the given vector.
161    fn div_assign(&mut self, other: Vec3<T>) {
162        *self = Vec3::<T>(self.0 / other.0, self.1 / other.1, self.2 / other.2)
163    }
164}
165
166impl<T> Div<T> for Vec3<T> where T: Float {
167    type Output = Vec3<T>;
168    /// Divides the vector by given scalar operand.
169    fn div(self, other: T) -> Vec3<T> {
170        Vec3::<T>(self.0 / other, self.1 / other, self.2 / other)
171    }
172}
173
174impl<T> DivAssign<T> for Vec3<T> where T: Float {
175    /// Mutates the vector by dividing it by given scalar.
176    fn div_assign(&mut self, other: T) {
177        *self = Vec3::<T>(self.0 / other, self.1 / other, self.2 / other)
178    }
179}
180
181impl Mul<Vec3<f32>> for f32 {
182    type Output = Vec3<f32>;
183    fn mul(self, other: Vec3<f32>) -> Vec3<f32> {
184        Vec3::<f32>(self * other.0, self * other.1, self * other.2)
185    }
186}
187
188impl Mul<Vec3<f64>> for f64 {
189    type Output = Vec3<f64>;
190    fn mul(self, other: Vec3<f64>) -> Vec3<f64> {
191        Vec3::<f64>(self * other.0, self * other.1, self * other.2)
192    }
193}
194
195// as radiant uniform
196
197#[cfg(feature = "uniforms")]
198use radiant_rs::{Uniform, AsUniform};
199
200#[cfg(feature = "uniforms")]
201impl AsUniform for Vec3<f32> {
202    fn as_uniform(&self) -> Uniform {
203        Uniform::Vec3([ self.0, self.1, self.2 ])
204    }
205}
206
207#[cfg(feature = "uniforms")]
208impl AsUniform for Vec3<f64> {
209    fn as_uniform(&self) -> Uniform {
210        Uniform::DoubleVec3([ self.0, self.1, self.2 ])
211    }
212}
213
214// debug print
215
216impl<T> Debug for Vec3<T> where T: Debug {
217    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
218        write!(f, "Vec3({:?}, {:?}, {:?})", self.0, self.1, self.2)
219    }
220}