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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
//! A 3D tuple which can represent points and vectors. //! The coordinates are floating point numbers. Support for generics may be added in the future. //! //! # Examples //! //! You can create points and vectors with [Tuple::point] and [Tuple::vector] respectively: //! ``` //! # use truster::tuple::Tuple; //! let p = Tuple::point(1.0, 4.2, -3.7); //! assert!(p.is_point()); //! assert!(!p.is_vector()); //! let v = Tuple::vector(1.0, 4.2, -3.7); //! assert!(!v.is_point()); //! assert!(v.is_vector()); //! ``` //! //! Points and vectors are not the same: //! ``` //! # use truster::tuple::Tuple; //! let p = Tuple::point(1.0, 4.2, -3.7); //! let v = Tuple::vector(1.0, 4.2, -3.7); //! assert_ne!(p, v); //! ``` //! //! Points represent a position in 3D space. Vectors represent a displacement or movement. //! //! //! Individual coordinates can be accessed with their respective methods: //! ``` //! # use truster::tuple::Tuple; //! let p = Tuple::point(1.0, 4.2, -3.7); //! assert_eq!(p.x(), 1.0); //! assert_eq!(p.y(), 4.2); //! assert_eq!(p.z(), -3.7); //! ``` //! //! ... or with indexing: //! ``` //! # use truster::tuple::Tuple; //! let p = Tuple::point(1.0, 4.2, -3.7); //! assert_eq!(p[0], 1.0); //! assert_eq!(p[1], 4.2); //! assert_eq!(p[2], -3.7); //! ``` //! //! ## Arithmetic //! //! Tuples support all common arithmetic operations. However, be careful, as for example points //! can't be added to points. You have to add vectors to points to get another point. This library //! won't check this for you, because of simplicity and for performance reasons. You should make //! sure you handle everything correctly to avoid bugs. All operations which support operator //! overloading support mutable assignment. The available operations are: //! //! - Addition (p+v -> v, v+p -> p, v+v -> v) //! ``` //! # use truster::tuple::Tuple; //! let mut p = Tuple::point(3.0, -2.0, 5.0); //! let v = Tuple::vector(-2.0, 3.0, 1.0); //! assert_eq!(p + v, Tuple::point(1.0, 1.0, 6.0)); //! p += v; //! assert_eq!(p, Tuple::point(1.0, 1.0, 6.0)); //! ``` //! //! - Subtraction (p-p -> v, p-v -> p, v-v -> v) //! ``` //! # use truster::tuple::Tuple; //! let p1 = Tuple::point(3.0, 2.0, 1.0); //! let p2 = Tuple::point(5.0, 6.0, 7.0); //! assert_eq!(p1 - p2, Tuple::vector(-2.0, -4.0, -6.0)); //! //! let v1 = Tuple::vector(5.0, 6.0, 7.0); //! assert_eq!(p1 - v1, Tuple::point(-2.0, -4.0, -6.0)); //! //! let v2 = Tuple::vector(3.0, 2.0, 1.0); //! assert_eq!(v2 - v1, Tuple::vector(-2.0, -4.0, -6.0)); //! ``` //! //! - Negation (-v -> v) //! ``` //! # use truster::tuple::Tuple; //! let v = Tuple::vector(1.0, -2.0, 3.0); //! assert_eq!(-v, Tuple::vector(-1.0, 2.0, -3.0)); //! ``` //! //! - Scalar multiplication (v*f -> v) //! ``` //! # use truster::tuple::Tuple; //! let v = Tuple::vector(1.0, -2.0, 3.0); //! assert_eq!(v * 3.5, Tuple::vector(3.5, -7.0, 10.5)); //! assert_eq!(v * 0.5, Tuple::vector(0.5, -1.0, 1.5)); //! ``` //! //! - Scalar division (v/f -> v) //! ``` //! # use truster::tuple::Tuple; //! let v = Tuple::vector(1.0, -2.0, 3.0); //! assert_eq!(v / 2.0, Tuple::vector(0.5, -1.0, 1.5)); //! ``` //! //! - Dot product (v⋅v -> f) //! ``` //! # use truster::tuple::Tuple; //! let v1 = Tuple::vector(1.0, 2.0, 3.0); //! let v2 = Tuple::vector(2.0, 3.0, 4.0); //! assert_eq!(v1.dot(v2), 20.0); //! ``` //! //! - Cross product (v×v -> v) //! ``` //! # use truster::tuple::Tuple; //! let v1 = Tuple::vector(1.0, 2.0, 3.0); //! let v2 = Tuple::vector(2.0, 3.0, 4.0); //! assert_eq!(v1.cross(v2), Tuple::vector(-1.0, 2.0, -1.0)); //! assert_eq!(v2.cross(v1), Tuple::vector(1.0, -2.0, 1.0)); //! ``` //! //! - Reflection (v.reflect(v) -> v) //! ``` //! # use truster::tuple::Tuple; //! let v = Tuple::vector(1.0, -1.0, 0.0); //! let n = Tuple::vector(0.0, 1.0, 0.0); //! let r = v.reflect(n); //! assert_eq!(r, Tuple::vector(1.0, 1.0, 0.0)); //! ``` //! //! ## Normalization //! //! When working with vectors (so not points), you can take the norm of vectors and normalize them. //! You can also ask the square of the norm. This is faster than the norm itself, because no square //! root has to be taken. //! //! ``` //! # use truster::tuple::Tuple; //! //! let sqrt14 = (14.0 as f64).sqrt(); //! let mut v = Tuple::vector(1.0, 2.0, 3.0); //! assert_eq!(v.norm_squared(), 14.0); //! assert_eq!(v.norm(), sqrt14); //! assert_eq!(v.normalized(), Tuple::vector(1.0 / sqrt14, 2.0 / sqrt14, 3.0 / sqrt14)); //! //! v.normalize(); //! assert_eq!(v, Tuple::vector(1.0 / sqrt14, 2.0 / sqrt14, 3.0 / sqrt14)); //! ``` use std::fmt::Display; use std::ops::{ Add, AddAssign, Div, DivAssign, Index, IndexMut, Mul, MulAssign, Neg, Sub, SubAssign, }; /// Tuple represents a 3D tuple. See the module's documentation for more information. #[derive(Debug, PartialEq, Clone, Copy, Default)] pub struct Tuple { x: f64, y: f64, z: f64, w: f64, } impl Tuple { /// Returns a new tuple with the given components. You should use [Tuple::point] and /// [Tuple::vector] instead. pub fn new(x: f64, y: f64, z: f64, w: f64) -> Self { Self { x, y, z, w } } /// Returns a new point with the given coordinates. pub fn point(x: f64, y: f64, z: f64) -> Self { Self::new(x, y, z, 1.0) } /// Returns a new vector with the given coordinates. pub fn vector(x: f64, y: f64, z: f64) -> Self { Self::new(x, y, z, 0.0) } /// Returns `self`s x coordinate. pub fn x(&self) -> f64 { self.x } /// Returns `self`s y coordinate. pub fn y(&self) -> f64 { self.y } /// Returns `self`s z coordinate. pub fn z(&self) -> f64 { self.z } /// Returns `self`s w coordinate. pub fn w(&self) -> f64 { self.w } /// Returns true if `self` represents a point, false otherwise. pub fn is_point(&self) -> bool { self.w == 1.0 } /// Returns true if `self` represents a vector, false otherwise. pub fn is_vector(&self) -> bool { self.w == 0.0 } /// Returns the dot product between `self` and `other`. See the module's documentation for /// examples. Only works for vectors, not points. pub fn dot(self, other: Self) -> f64 { self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w } /// Returns the cross product between `self` and `other`. See the module's documentation for /// examples. Only works for vectors, not points. pub fn cross(self, other: Self) -> Self { Self::vector( self.y * other.z - self.z * other.y, self.z * other.x - self.x * other.z, self.x * other.y - self.y * other.x, ) } /// Returns the square of the euclidean norm of `self`. See the module's documentation for /// examples. Only works for vectors, not points. pub fn norm_squared(self) -> f64 { self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w } /// Returns the norm of `self`. See the module's documentation for examples. Only works for /// vectors, not points. pub fn norm(self) -> f64 { self.norm_squared().sqrt() } /// Returns a vector in the same direction as `self`, but with euclidean norm of one. See the /// module's documentation for examples. Only works for vectors, not points. pub fn normalized(self) -> Self { self / self.norm() } /// Changes `self` to have a euclidean norm of one, while keeping its direction. See the /// module's documentation for examples. Only works for vectors, not points. pub fn normalize(&mut self) { *self /= self.norm(); } /// Reflects `self` along `normal` pub fn reflect(self, normal: Self) -> Self { self - normal * 2.0 * self.dot(normal) } } impl Display for Tuple { fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { if self.is_point() { write!(f, "P({}, {}, {})", self.x, self.y, self.z) } else if self.is_vector() { write!(f, "V[{} {} {}]", self.x, self.y, self.z) } else { write!(f, "[{} {} {} ({})]", self.x, self.y, self.z, self.w) } } } impl Add for Tuple { type Output = Self; fn add(self, rhs: Self) -> Self::Output { Self::Output::new( self.x + rhs.x, self.y + rhs.y, self.z + rhs.z, self.w + rhs.w, ) } } impl AddAssign for Tuple { fn add_assign(&mut self, rhs: Self) { self.x += rhs.x; self.y += rhs.y; self.z += rhs.z; self.w += rhs.w; } } impl Sub for Tuple { type Output = Self; fn sub(self, rhs: Self) -> Self::Output { Self::Output::new( self.x - rhs.x, self.y - rhs.y, self.z - rhs.z, self.w - rhs.w, ) } } impl SubAssign for Tuple { fn sub_assign(&mut self, rhs: Self) { self.x -= rhs.x; self.y -= rhs.y; self.z -= rhs.z; self.w -= rhs.w; } } impl Neg for Tuple { type Output = Self; fn neg(self) -> Self::Output { Self::Output::new(-self.x, -self.y, -self.z, -self.w) } } impl Mul<f64> for Tuple { type Output = Self; fn mul(self, rhs: f64) -> Self::Output { Self::Output::new(self.x * rhs, self.y * rhs, self.z * rhs, self.w * rhs) } } impl MulAssign<f64> for Tuple { fn mul_assign(&mut self, rhs: f64) { self.x *= rhs; self.y *= rhs; self.z *= rhs; self.w *= rhs; } } impl Div<f64> for Tuple { type Output = Self; fn div(self, rhs: f64) -> Self::Output { Self::Output::new(self.x / rhs, self.y / rhs, self.z / rhs, self.w / rhs) } } impl DivAssign<f64> for Tuple { fn div_assign(&mut self, rhs: f64) { self.x /= rhs; self.y /= rhs; self.z /= rhs; self.w /= rhs; } } impl Index<usize> for Tuple { type Output = f64; fn index(&self, index: usize) -> &Self::Output { match index { 0 => &self.x, 1 => &self.y, 2 => &self.z, _ => panic!("Index out of bounds for tuple, got {}", index), } } } impl IndexMut<usize> for Tuple { fn index_mut(&mut self, index: usize) -> &mut Self::Output { match index { 0 => &mut self.x, 1 => &mut self.y, 2 => &mut self.z, _ => panic!("Index out of bounds for tuple, got {}", index), } } }