| 1 | use super::*; |
| 2 | |
| 3 | impl Vector3 { |
| 4 | pub fn new(X: f32, Y: f32, Z: f32) -> Self { |
| 5 | Self { X, Y, Z } |
| 6 | } |
| 7 | pub fn zero() -> Self { |
| 8 | Self { |
| 9 | X: 0f32, |
| 10 | Y: 0f32, |
| 11 | Z: 0f32, |
| 12 | } |
| 13 | } |
| 14 | pub fn one() -> Self { |
| 15 | Self { |
| 16 | X: 1f32, |
| 17 | Y: 1f32, |
| 18 | Z: 1f32, |
| 19 | } |
| 20 | } |
| 21 | pub fn unit_x() -> Self { |
| 22 | Self { |
| 23 | X: 1.0, |
| 24 | Y: 0.0, |
| 25 | Z: 0.0, |
| 26 | } |
| 27 | } |
| 28 | pub fn unit_y() -> Self { |
| 29 | Self { |
| 30 | X: 0.0, |
| 31 | Y: 1.0, |
| 32 | Z: 0.0, |
| 33 | } |
| 34 | } |
| 35 | pub fn unit_z() -> Self { |
| 36 | Self { |
| 37 | X: 0.0, |
| 38 | Y: 0.0, |
| 39 | Z: 1.0, |
| 40 | } |
| 41 | } |
| 42 | pub fn dot(&self, rhs: &Self) -> f32 { |
| 43 | self.X * rhs.X + self.Y * rhs.Y + self.Z * rhs.Z |
| 44 | } |
| 45 | pub fn length_squared(&self) -> f32 { |
| 46 | self.dot(self) |
| 47 | } |
| 48 | #[cfg (feature = "std" )] |
| 49 | pub fn length(&self) -> f32 { |
| 50 | self.length_squared().sqrt() |
| 51 | } |
| 52 | #[cfg (feature = "std" )] |
| 53 | pub fn distance(&self, value: &Self) -> f32 { |
| 54 | (self - value).length() |
| 55 | } |
| 56 | pub fn distance_squared(&self, value: &Self) -> f32 { |
| 57 | (self - value).length_squared() |
| 58 | } |
| 59 | #[cfg (feature = "std" )] |
| 60 | pub fn normalize(&self) -> Self { |
| 61 | self / self.length() |
| 62 | } |
| 63 | |
| 64 | fn impl_add(&self, rhs: &Self) -> Self { |
| 65 | Self { |
| 66 | X: self.X + rhs.X, |
| 67 | Y: self.Y + rhs.Y, |
| 68 | Z: self.Z + rhs.Z, |
| 69 | } |
| 70 | } |
| 71 | fn impl_sub(&self, rhs: &Self) -> Self { |
| 72 | Self { |
| 73 | X: self.X - rhs.X, |
| 74 | Y: self.Y - rhs.Y, |
| 75 | Z: self.Z - rhs.Z, |
| 76 | } |
| 77 | } |
| 78 | fn impl_div(&self, rhs: &Self) -> Self { |
| 79 | Self { |
| 80 | X: self.X / rhs.X, |
| 81 | Y: self.Y / rhs.Y, |
| 82 | Z: self.Z / rhs.Z, |
| 83 | } |
| 84 | } |
| 85 | fn impl_div_f32(&self, rhs: f32) -> Self { |
| 86 | Self { |
| 87 | X: self.X / rhs, |
| 88 | Y: self.Y / rhs, |
| 89 | Z: self.Z / rhs, |
| 90 | } |
| 91 | } |
| 92 | fn impl_mul(&self, rhs: &Self) -> Self { |
| 93 | Self { |
| 94 | X: self.X * rhs.X, |
| 95 | Y: self.Y * rhs.Y, |
| 96 | Z: self.Z * rhs.Z, |
| 97 | } |
| 98 | } |
| 99 | fn impl_mul_f32(&self, rhs: f32) -> Self { |
| 100 | Self { |
| 101 | X: self.X * rhs, |
| 102 | Y: self.Y * rhs, |
| 103 | Z: self.Z * rhs, |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | impl core::ops::Add<Vector3> for Vector3 { |
| 109 | type Output = Vector3; |
| 110 | fn add(self, rhs: Vector3) -> Vector3 { |
| 111 | self.impl_add(&rhs) |
| 112 | } |
| 113 | } |
| 114 | impl core::ops::Add<&Vector3> for Vector3 { |
| 115 | type Output = Vector3; |
| 116 | fn add(self, rhs: &Vector3) -> Vector3 { |
| 117 | self.impl_add(rhs) |
| 118 | } |
| 119 | } |
| 120 | impl core::ops::Add<Vector3> for &Vector3 { |
| 121 | type Output = Vector3; |
| 122 | fn add(self, rhs: Vector3) -> Vector3 { |
| 123 | self.impl_add(&rhs) |
| 124 | } |
| 125 | } |
| 126 | impl core::ops::Add<&Vector3> for &Vector3 { |
| 127 | type Output = Vector3; |
| 128 | fn add(self, rhs: &Vector3) -> Vector3 { |
| 129 | self.impl_add(rhs) |
| 130 | } |
| 131 | } |
| 132 | impl core::ops::Sub<Vector3> for Vector3 { |
| 133 | type Output = Vector3; |
| 134 | fn sub(self, rhs: Vector3) -> Vector3 { |
| 135 | self.impl_sub(&rhs) |
| 136 | } |
| 137 | } |
| 138 | impl core::ops::Sub<&Vector3> for Vector3 { |
| 139 | type Output = Vector3; |
| 140 | fn sub(self, rhs: &Vector3) -> Vector3 { |
| 141 | self.impl_sub(rhs) |
| 142 | } |
| 143 | } |
| 144 | impl core::ops::Sub<Vector3> for &Vector3 { |
| 145 | type Output = Vector3; |
| 146 | fn sub(self, rhs: Vector3) -> Vector3 { |
| 147 | self.impl_sub(&rhs) |
| 148 | } |
| 149 | } |
| 150 | impl core::ops::Sub<&Vector3> for &Vector3 { |
| 151 | type Output = Vector3; |
| 152 | fn sub(self, rhs: &Vector3) -> Vector3 { |
| 153 | self.impl_sub(rhs) |
| 154 | } |
| 155 | } |
| 156 | impl core::ops::Div<Vector3> for Vector3 { |
| 157 | type Output = Vector3; |
| 158 | fn div(self, rhs: Vector3) -> Vector3 { |
| 159 | self.impl_div(&rhs) |
| 160 | } |
| 161 | } |
| 162 | impl core::ops::Div<&Vector3> for Vector3 { |
| 163 | type Output = Vector3; |
| 164 | fn div(self, rhs: &Vector3) -> Vector3 { |
| 165 | self.impl_div(rhs) |
| 166 | } |
| 167 | } |
| 168 | impl core::ops::Div<Vector3> for &Vector3 { |
| 169 | type Output = Vector3; |
| 170 | fn div(self, rhs: Vector3) -> Vector3 { |
| 171 | self.impl_div(&rhs) |
| 172 | } |
| 173 | } |
| 174 | impl core::ops::Div<&Vector3> for &Vector3 { |
| 175 | type Output = Vector3; |
| 176 | fn div(self, rhs: &Vector3) -> Vector3 { |
| 177 | self.impl_div(rhs) |
| 178 | } |
| 179 | } |
| 180 | impl core::ops::Div<f32> for Vector3 { |
| 181 | type Output = Vector3; |
| 182 | fn div(self, rhs: f32) -> Vector3 { |
| 183 | self.impl_div_f32(rhs) |
| 184 | } |
| 185 | } |
| 186 | impl core::ops::Div<f32> for &Vector3 { |
| 187 | type Output = Vector3; |
| 188 | fn div(self, rhs: f32) -> Vector3 { |
| 189 | self.impl_div_f32(rhs) |
| 190 | } |
| 191 | } |
| 192 | impl core::ops::Mul<Vector3> for Vector3 { |
| 193 | type Output = Vector3; |
| 194 | fn mul(self, rhs: Vector3) -> Vector3 { |
| 195 | self.impl_mul(&rhs) |
| 196 | } |
| 197 | } |
| 198 | impl core::ops::Mul<&Vector3> for Vector3 { |
| 199 | type Output = Vector3; |
| 200 | fn mul(self, rhs: &Vector3) -> Vector3 { |
| 201 | self.impl_mul(rhs) |
| 202 | } |
| 203 | } |
| 204 | impl core::ops::Mul<Vector3> for &Vector3 { |
| 205 | type Output = Vector3; |
| 206 | fn mul(self, rhs: Vector3) -> Vector3 { |
| 207 | self.impl_mul(&rhs) |
| 208 | } |
| 209 | } |
| 210 | impl core::ops::Mul<&Vector3> for &Vector3 { |
| 211 | type Output = Vector3; |
| 212 | fn mul(self, rhs: &Vector3) -> Vector3 { |
| 213 | self.impl_mul(rhs) |
| 214 | } |
| 215 | } |
| 216 | impl core::ops::Mul<f32> for Vector3 { |
| 217 | type Output = Vector3; |
| 218 | fn mul(self, rhs: f32) -> Vector3 { |
| 219 | self.impl_mul_f32(rhs) |
| 220 | } |
| 221 | } |
| 222 | impl core::ops::Mul<f32> for &Vector3 { |
| 223 | type Output = Vector3; |
| 224 | fn mul(self, rhs: f32) -> Vector3 { |
| 225 | self.impl_mul_f32(rhs) |
| 226 | } |
| 227 | } |
| 228 | |