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
use super::*;

/// A Quaternion: one scalar part (`a`) and three imaginary parts (`b`, `c`, and `d`).
///
/// This is a _borderline magical_ mathematical construct. You can read about it
/// on [wikipedia](https://en.wikipedia.org/wiki/Quaternion), and
/// [3blue1brown](https://www.youtube.com/watch?v=d4EgbgTm0Bg) also did a video
/// on them.
///
/// The basic idea is that, in similar to how you describe a standard complex
/// number with one real part and one imaginary part, this thing is made up of a
/// real part and _three_ imaginary parts. Because there's three imaginary
/// dimensions as well as a real one, it's kinda a four dimensional thing. This
/// can be difficult to envision, because if you're reading this you're probably
/// only three dimensional.
///
/// The practical application of it all is that you can store rotation and/or
/// orientation information very well by using a quaternion.
#[derive(Clone, Copy, Default, PartialEq)]
#[repr(align(16), C)]
pub struct Quat {
  pub(crate) a: f32,
  pub(crate) b: f32,
  pub(crate) c: f32,
  pub(crate) d: f32,
}
unsafe impl Zeroable for Quat {}
unsafe impl Pod for Quat {}

impl core::fmt::Debug for Quat {
  /// Shows the fields with labels `a`, `b`, `c`, and `d`.
  ///
  /// Passes the formatter along to the fields, so you can use any normal `f32`
  /// Debug format arguments that you like.
  fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
    f.write_str("Quat { a: ")?;
    core::fmt::Debug::fmt(&self.a, f)?;
    f.write_str(", b: ")?;
    core::fmt::Debug::fmt(&self.b, f)?;
    f.write_str(", c: ")?;
    core::fmt::Debug::fmt(&self.c, f)?;
    f.write_str(", d: ")?;
    core::fmt::Debug::fmt(&self.d, f)?;
    f.write_str(" }")
  }
}

impl core::fmt::Display for Quat {
  /// Shows the quaternion in a standard `a + bi + cj+ dk` style.
  ///
  /// Passes the formatter along to the fields, so you can use any normal `f32`
  /// Display format arguments that you like.
  fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
    core::fmt::Display::fmt(&self.a, f)?;
    f.write_str(" + ")?;
    core::fmt::Display::fmt(&self.b, f)?;
    f.write_str("i + ")?;
    core::fmt::Display::fmt(&self.c, f)?;
    f.write_str("j + ")?;
    core::fmt::Display::fmt(&self.d, f)?;
    f.write_str("k")
  }
}

impl core::fmt::LowerExp for Quat {
  /// LowerExp formats like Display, but with the lower exponent.
  ///
  /// Passes the formatter along to the fields, so you can use any normal `f32`
  /// LowerExp format arguments that you like.
  fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
    core::fmt::LowerExp::fmt(&self.a, f)?;
    f.write_str(" + ")?;
    core::fmt::LowerExp::fmt(&self.b, f)?;
    f.write_str("i + ")?;
    core::fmt::LowerExp::fmt(&self.c, f)?;
    f.write_str("j + ")?;
    core::fmt::LowerExp::fmt(&self.d, f)?;
    f.write_str("k")
  }
}

impl core::fmt::UpperExp for Quat {
  /// UpperExp formats like Display, but with the upper exponent.
  ///
  /// Passes the formatter along to the fields, so you can use any normal `f32`
  /// UpperExp format arguments that you like.
  fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
    core::fmt::UpperExp::fmt(&self.a, f)?;
    f.write_str(" + ")?;
    core::fmt::UpperExp::fmt(&self.b, f)?;
    f.write_str("i + ")?;
    core::fmt::UpperExp::fmt(&self.c, f)?;
    f.write_str("j + ")?;
    core::fmt::UpperExp::fmt(&self.d, f)?;
    f.write_str("k")
  }
}

impl Index<usize> for Quat {
  type Output = f32;
  #[inline(always)]
  fn index(&self, index: usize) -> &f32 {
    match index {
      0 => &self.a,
      1 => &self.b,
      2 => &self.c,
      3 => &self.d,
      otherwise => panic!("Quat index out of bounds: {}", otherwise),
    }
  }
}
impl IndexMut<usize> for Quat {
  #[inline(always)]
  fn index_mut(&mut self, index: usize) -> &mut f32 {
    match index {
      0 => &mut self.a,
      1 => &mut self.b,
      2 => &mut self.c,
      3 => &mut self.d,
      otherwise => panic!("Quat index out of bounds: {}", otherwise),
    }
  }
}

impl AsRef<[f32; 4]> for Quat {
  #[inline(always)]
  fn as_ref(&self) -> &[f32; 4] {
    cast_ref(self)
  }
}
impl AsMut<[f32; 4]> for Quat {
  #[inline(always)]
  fn as_mut(&mut self) -> &mut [f32; 4] {
    cast_mut(self)
  }
}

impl From<[f32; 4]> for Quat {
  #[inline]
  fn from([a, b, c, d]: [f32; 4]) -> Self {
    Self { a, b, c, d }
  }
}
impl From<Quat> for [f32; 4] {
  #[inline]
  fn from(Quat { a, b, c, d }: Quat) -> Self {
    [a, b, c, d]
  }
}

impl Mul<f32> for Quat {
  type Output = Self;
  #[inline]
  fn mul(self, rhs: f32) -> Self {
    Self {
      a: self.a * rhs,
      b: self.b * rhs,
      c: self.c * rhs,
      d: self.d * rhs,
    }
  }
}
impl Mul<Quat> for f32 {
  type Output = Quat;
  #[inline]
  fn mul(self, rhs: Quat) -> Quat {
    rhs * self
  }
}
impl MulAssign<f32> for Quat {
  #[inline]
  fn mul_assign(&mut self, rhs: f32) {
    *self = *self * rhs;
  }
}

#[cfg(feature = "mint")]
impl From<mint::Quaternion<f32>> for Quat {
  #[inline]
  fn from(mint::Quaternion { s, v }: mint::Quaternion<f32>) -> Self {
    let [x, y, z]: [f32; 3] = v.into();
    Self {
      a: s,
      b: x,
      c: y,
      d: z,
    }
  }
}
#[cfg(feature = "mint")]
impl From<Quat> for mint::Quaternion<f32> {
  #[inline]
  fn from(Quat { a, b, c, d }: Quat) -> Self {
    mint::Quaternion {
      s: a,
      v: mint::Vector3 { x: b, y: c, z: d },
    }
  }
}

#[cfg(feature = "serde")]
impl serde::Serialize for Quat {
  #[inline]
  fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
  where
    S: serde::Serializer,
  {
    <[f32; 4]>::from(*self).serialize(serializer)
  }
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Quat {
  #[inline]
  fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
  where
    D: serde::Deserializer<'de>,
  {
    Ok(Self::from(<[f32; 4]>::deserialize(deserializer)?))
  }
}

/// ## Accessors
impl Quat {
  /// Gets the real coefficient of this quaternion.
  #[inline(always)]
  pub fn a(self) -> f32 {
    self.a
  }
  /// Gets the `i` coefficient of this quaternion.
  #[inline(always)]
  pub fn b(self) -> f32 {
    self.b
  }
  /// Gets the `j` coefficient of this quaternion.
  #[inline(always)]
  pub fn c(self) -> f32 {
    self.c
  }
  /// Gets the `k` coefficient of this quaternion.
  #[inline(always)]
  pub fn d(self) -> f32 {
    self.d
  }
  /// `&mut` to the real coefficient of this quaternion.
  #[inline(always)]
  pub fn a_mut(&mut self) -> &mut f32 {
    &mut self.a
  }
  /// `&mut` to the `i` coefficient of this quaternion.
  #[inline(always)]
  pub fn b_mut(&mut self) -> &mut f32 {
    &mut self.b
  }
  /// `&mut` to the `j` coefficient of this quaternion.
  #[inline(always)]
  pub fn c_mut(&mut self) -> &mut f32 {
    &mut self.c
  }
  /// `&mut` to the `k` coefficient of this quaternion.
  #[inline(always)]
  pub fn d_mut(&mut self) -> &mut f32 {
    &mut self.d
  }
}

/// ## Constructors
impl Quat {
  /// Makes a new quaternion using the coefficients given.
  #[inline(always)]
  pub fn new(a: f32, b: f32, c: f32, d: f32) -> Self {
    Self { a, b, c, d }
  }

  /// Splats the given value across all coefficients.
  #[inline]
  pub fn splat(v: f32) -> Self {
    Self {
      a: v,
      b: v,
      c: v,
      d: v,
    }
  }
}