alloy/math/
euler.rs

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
371
372
373
374
375
376
377
use std::borrow::{Borrow, BorrowMut};
use std::ops::{Add, AddAssign, Deref, DerefMut, Div, Mul, Sub, SubAssign};

use crate::math::vec::Vec3;
use crate::math::{Angle, Radian};

/// Euler angles are a representation of orientation in 3D space.
#[derive(Default, Debug, Clone, Copy, PartialEq)]
#[repr(transparent)]
pub struct EulerAngles([Radian; 3]);

// Constructors

impl EulerAngles {
  /// Create a new Euler angle with the given yaw, pitch, and roll.
  ///
  /// # Parameters
  ///
  /// * `yaw` - The yaw angle.
  /// * `pitch` - The pitch angle.
  /// * `roll` - The roll angle.
  #[must_use]
  #[inline(always)]
  pub fn new<A: Angle>(yaw: A, pitch: A, roll: A) -> Self {
    Self([yaw.to_angle(), pitch.to_angle(), roll.to_angle()])
  }

  /// Create a new Euler angle with the given yaw, pitch, and roll in radians.
  ///
  /// # Parameters
  ///
  /// * `yaw` - The yaw angle in radians.
  /// * `pitch` - The pitch angle in radians.
  /// * `roll` - The roll angle in radians.
  #[must_use]
  #[inline(always)]
  pub const fn from_radians(yaw: Radian, pitch: Radian, roll: Radian) -> Self {
    Self([yaw, pitch, roll])
  }

  /// Create a new Euler angle with the given yaw.
  ///
  /// # Parameters
  ///
  /// * `yaw` - The yaw angle.
  #[must_use]
  #[inline(always)]
  pub fn from_yaw<A: Angle>(yaw: A) -> Self {
    Self([yaw.to_angle(), Radian::new(0.0), Radian::new(0.0)])
  }

  /// Create a new Euler angle with the given pitch.
  ///
  /// # Parameters
  ///
  /// * `pitch` - The pitch angle.
  #[must_use]
  #[inline(always)]
  pub fn from_pitch<A: Angle>(pitch: A) -> Self {
    Self([Radian::new(0.0), pitch.to_angle(), Radian::new(0.0)])
  }

  /// Create a new Euler angle with the given roll.
  ///
  /// # Parameters
  ///
  /// * `roll` - The roll angle.
  #[must_use]
  #[inline(always)]
  pub fn from_roll<A: Angle>(roll: A) -> Self {
    Self([Radian::new(0.0), Radian::new(0.0), roll.to_angle()])
  }
}

// Conversions

impl EulerAngles {
  /// Convert the Euler angles to a 3D vector.
  #[must_use]
  #[inline(always)]
  pub const fn as_vec3(&self) -> &Vec3 {
    // Safety: [Angle] and [f32] are layout compatible because Angle is repr(transparent) over f32.
    let slice: &[f32] = unsafe { std::mem::transmute(self.0.as_slice()) };

    // Safety: The slice is of length 3.
    unsafe { Vec3::from_slice_unchecked(slice) }
  }

  /// Convert the Euler angles to a mutable 3D vector.
  #[must_use]
  #[inline(always)]
  pub fn as_vec3_mut(&mut self) -> &mut Vec3 {
    // Safety: [Angle] and [f32] are layout compatible because Angle is repr(transparent) over f32.
    let slice: &mut [f32] = unsafe { std::mem::transmute(self.0.as_mut_slice()) };

    // Safety: The slice is of length 3.
    unsafe { Vec3::from_mut_slice_unchecked(slice) }
  }

  /// Convert the Euler angles to a tuple of yaw, pitch, and roll.
  #[must_use]
  #[inline(always)]
  pub fn to_tuple<A: Angle>(&self) -> (A, A, A) {
    (
      self.0[0].to_angle::<A>(),
      self.0[1].to_angle::<A>(),
      self.0[2].to_angle::<A>(),
    )
  }
}

impl Borrow<Vec3> for EulerAngles {
  #[must_use]
  #[inline(always)]
  fn borrow(&self) -> &Vec3 {
    self.as_vec3()
  }
}

impl BorrowMut<Vec3> for EulerAngles {
  #[must_use]
  #[inline(always)]
  fn borrow_mut(&mut self) -> &mut Vec3 {
    self.as_vec3_mut()
  }
}

impl AsRef<Vec3> for EulerAngles {
  #[must_use]
  #[inline(always)]
  fn as_ref(&self) -> &Vec3 {
    self.as_vec3()
  }
}

impl AsMut<Vec3> for EulerAngles {
  #[must_use]
  #[inline(always)]
  fn as_mut(&mut self) -> &mut Vec3 {
    self.as_vec3_mut()
  }
}

impl Deref for EulerAngles {
  type Target = Vec3;

  #[must_use]
  #[inline(always)]
  fn deref(&self) -> &Self::Target {
    self.as_vec3()
  }
}

impl DerefMut for EulerAngles {
  #[must_use]
  #[inline(always)]
  fn deref_mut(&mut self) -> &mut Self::Target {
    self.as_vec3_mut()
  }
}

// Properties

impl EulerAngles {
  /// Return the yaw angle of the Euler angles.
  #[must_use]
  #[inline(always)]
  pub const fn yaw(&self) -> Radian {
    self.0[0]
  }

  /// Return the pitch angle of the Euler angles.
  #[must_use]
  #[inline(always)]
  pub const fn pitch(&self) -> Radian {
    self.0[1]
  }

  /// Return the roll angle of the Euler angles.
  #[must_use]
  #[inline(always)]
  pub const fn roll(&self) -> Radian {
    self.0[2]
  }

  /// Return a mutable reference to the yaw angle of the Euler angles.
  #[must_use]
  #[inline(always)]
  pub fn yaw_mut(&mut self) -> &mut Radian {
    &mut self.0[0]
  }

  /// Return a mutable reference to the pitch angle of the Euler angles.
  #[must_use]
  #[inline(always)]
  pub fn pitch_mut(&mut self) -> &mut Radian {
    &mut self.0[1]
  }

  /// Return a mutable reference to the roll angle of the Euler angles.
  #[must_use]
  #[inline(always)]
  pub fn roll_mut(&mut self) -> &mut Radian {
    &mut self.0[2]
  }
}

// Modifiers

impl EulerAngles {
  /// Set the yaw angle of the Euler angles.
  ///
  /// # Parameters
  ///
  /// * `yaw` - The new yaw angle.
  pub fn set_yaw<A: Angle>(&mut self, yaw: A) {
    self.0[0] = yaw.to_angle();
  }

  /// Set the pitch angle of the Euler angles.
  ///
  /// # Parameters
  ///
  /// * `pitch` - The new pitch angle.
  pub fn set_pitch<A: Angle>(&mut self, pitch: A) {
    self.0[1] = pitch.to_angle();
  }

  /// Set the roll angle of the Euler angles.
  ///
  /// # Parameters
  ///
  /// * `roll` - The new roll angle.
  pub fn set_roll<A: Angle>(&mut self, roll: A) {
    self.0[2] = roll.to_angle();
  }
}

// Arithmetic

impl Add for EulerAngles {
  type Output = Self;

  #[inline(always)]
  fn add(self, rhs: Self) -> Self::Output {
    Self([
      self.0[0] + rhs.0[0],
      self.0[1] + rhs.0[1],
      self.0[2] + rhs.0[2],
    ])
  }
}

impl Add for &EulerAngles {
  type Output = EulerAngles;

  #[inline(always)]
  fn add(self, rhs: Self) -> Self::Output {
    EulerAngles([
      self.0[0] + rhs.0[0],
      self.0[1] + rhs.0[1],
      self.0[2] + rhs.0[2],
    ])
  }
}

impl AddAssign for EulerAngles {
  fn add_assign(&mut self, rhs: Self) {
    self.0[0] += rhs.0[0];
    self.0[1] += rhs.0[1];
    self.0[2] += rhs.0[2];
  }
}

impl AddAssign<&EulerAngles> for EulerAngles {
  fn add_assign(&mut self, rhs: &EulerAngles) {
    self.0[0] += rhs.0[0];
    self.0[1] += rhs.0[1];
    self.0[2] += rhs.0[2];
  }
}

impl Sub for EulerAngles {
  type Output = Self;

  #[inline(always)]
  fn sub(self, rhs: Self) -> Self::Output {
    EulerAngles([
      self.0[0] - rhs.0[0],
      self.0[1] - rhs.0[1],
      self.0[2] - rhs.0[2],
    ])
  }
}

impl Sub for &EulerAngles {
  type Output = EulerAngles;

  #[inline(always)]
  fn sub(self, rhs: Self) -> Self::Output {
    EulerAngles([
      self.0[0] - rhs.0[0],
      self.0[1] - rhs.0[1],
      self.0[2] - rhs.0[2],
    ])
  }
}

impl SubAssign for EulerAngles {
  fn sub_assign(&mut self, rhs: Self) {
    self.0[0] -= rhs.0[0];
    self.0[1] -= rhs.0[1];
    self.0[2] -= rhs.0[2];
  }
}

impl SubAssign<&EulerAngles> for EulerAngles {
  fn sub_assign(&mut self, rhs: &EulerAngles) {
    self.0[0] -= rhs.0[0];
    self.0[1] -= rhs.0[1];
    self.0[2] -= rhs.0[2];
  }
}

impl Mul<f32> for EulerAngles {
  type Output = Self;

  #[inline(always)]
  fn mul(self, rhs: f32) -> Self::Output {
    EulerAngles([self.0[0] * rhs, self.0[1] * rhs, self.0[2] * rhs])
  }
}

impl Mul<f32> for &EulerAngles {
  type Output = EulerAngles;

  #[inline(always)]
  fn mul(self, rhs: f32) -> Self::Output {
    EulerAngles([self.0[0] * rhs, self.0[1] * rhs, self.0[2] * rhs])
  }
}

impl Mul<EulerAngles> for f32 {
  type Output = EulerAngles;

  #[inline(always)]
  fn mul(self, rhs: EulerAngles) -> Self::Output {
    EulerAngles([rhs.0[0] * self, rhs.0[1] * self, rhs.0[2] * self])
  }
}

impl Mul<&EulerAngles> for f32 {
  type Output = EulerAngles;

  #[inline(always)]
  fn mul(self, rhs: &EulerAngles) -> Self::Output {
    EulerAngles([rhs.0[0] * self, rhs.0[1] * self, rhs.0[2] * self])
  }
}

impl Div<f32> for EulerAngles {
  type Output = Self;

  #[inline(always)]
  fn div(self, rhs: f32) -> Self::Output {
    self * (1.0 / rhs)
  }
}

impl Div<f32> for &EulerAngles {
  type Output = EulerAngles;

  #[inline(always)]
  fn div(self, rhs: f32) -> Self::Output {
    self * (1.0 / rhs)
  }
}