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

/// A 3x3 column-major Matrix.
///
/// There's three columns, and each column is itself a `Vec3`.
#[derive(Clone, Copy, Default, PartialEq)]
#[repr(align(16), C)]
pub struct Mat3 {
  pub(crate) x_axis: Vec3,
  pub(crate) y_axis: Vec3,
  pub(crate) z_axis: Vec3,
}
unsafe impl Zeroable for Mat3 {}
unsafe impl Pod for Mat3 {}

impl core::fmt::Debug for Mat3 {
  /// Debug formats with each axis labeled and then shown as a 3-tuple in one
  /// long line.
  ///
  /// 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("Mat3 { x_axis: (")?;
    let [x, y, z, _]: [f32; 4] = cast(self.x_axis);
    core::fmt::Debug::fmt(&x, f)?;
    f.write_str(", ")?;
    core::fmt::Debug::fmt(&y, f)?;
    f.write_str(", ")?;
    core::fmt::Debug::fmt(&z, f)?;
    f.write_str("), y_axis: (")?;
    let [x, y, z, _]: [f32; 4] = cast(self.y_axis);
    core::fmt::Debug::fmt(&x, f)?;
    f.write_str(", ")?;
    core::fmt::Debug::fmt(&y, f)?;
    f.write_str(", ")?;
    core::fmt::Debug::fmt(&z, f)?;
    f.write_str("), z_axis: (")?;
    let [x, y, z, _]: [f32; 4] = cast(self.z_axis);
    core::fmt::Debug::fmt(&x, f)?;
    f.write_str(", ")?;
    core::fmt::Debug::fmt(&y, f)?;
    f.write_str(", ")?;
    core::fmt::Debug::fmt(&z, f)?;
    f.write_str(") }")
  }
}

impl core::fmt::Display for Mat3 {
  /// Display formats without labels in a 3 line style similar to standard math
  /// notation.
  ///
  /// 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 {
    let [x0, x1, x2, _]: [f32; 4] = cast(self.x_axis);
    let [y0, y1, y2, _]: [f32; 4] = cast(self.y_axis);
    let [z0, z1, z2, _]: [f32; 4] = cast(self.z_axis);
    //
    f.write_str("| ")?;
    core::fmt::Display::fmt(&x0, f)?;
    f.write_str(", ")?;
    core::fmt::Display::fmt(&y0, f)?;
    f.write_str(", ")?;
    core::fmt::Display::fmt(&z0, f)?;
    f.write_str(" |\n| ")?;
    core::fmt::Display::fmt(&x1, f)?;
    f.write_str(", ")?;
    core::fmt::Display::fmt(&y1, f)?;
    f.write_str(", ")?;
    core::fmt::Display::fmt(&z1, f)?;
    f.write_str(" |\n| ")?;
    core::fmt::Display::fmt(&x2, f)?;
    f.write_str(", ")?;
    core::fmt::Display::fmt(&y2, f)?;
    f.write_str(", ")?;
    core::fmt::Display::fmt(&z2, f)?;
    f.write_str(" |")
  }
}

impl core::fmt::LowerExp for Mat3 {
  /// 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 {
    let [x0, x1, x2, _]: [f32; 4] = cast(self.x_axis);
    let [y0, y1, y2, _]: [f32; 4] = cast(self.y_axis);
    let [z0, z1, z2, _]: [f32; 4] = cast(self.z_axis);
    //
    f.write_str("| ")?;
    core::fmt::LowerExp::fmt(&x0, f)?;
    f.write_str(", ")?;
    core::fmt::LowerExp::fmt(&y0, f)?;
    f.write_str(", ")?;
    core::fmt::LowerExp::fmt(&z0, f)?;
    f.write_str(" |\n| ")?;
    core::fmt::LowerExp::fmt(&x1, f)?;
    f.write_str(", ")?;
    core::fmt::LowerExp::fmt(&y1, f)?;
    f.write_str(", ")?;
    core::fmt::LowerExp::fmt(&z1, f)?;
    f.write_str(" |\n| ")?;
    core::fmt::LowerExp::fmt(&x2, f)?;
    f.write_str(", ")?;
    core::fmt::LowerExp::fmt(&y2, f)?;
    f.write_str(", ")?;
    core::fmt::LowerExp::fmt(&z2, f)?;
    f.write_str(" |")
  }
}

impl core::fmt::UpperExp for Mat3 {
  /// 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 {
    let [x0, x1, x2, _]: [f32; 4] = cast(self.x_axis);
    let [y0, y1, y2, _]: [f32; 4] = cast(self.y_axis);
    let [z0, z1, z2, _]: [f32; 4] = cast(self.z_axis);
    //
    f.write_str("| ")?;
    core::fmt::UpperExp::fmt(&x0, f)?;
    f.write_str(", ")?;
    core::fmt::UpperExp::fmt(&y0, f)?;
    f.write_str(", ")?;
    core::fmt::UpperExp::fmt(&z0, f)?;
    f.write_str(" |\n| ")?;
    core::fmt::UpperExp::fmt(&x1, f)?;
    f.write_str(", ")?;
    core::fmt::UpperExp::fmt(&y1, f)?;
    f.write_str(", ")?;
    core::fmt::UpperExp::fmt(&z1, f)?;
    f.write_str(" |\n| ")?;
    core::fmt::UpperExp::fmt(&x2, f)?;
    f.write_str(", ")?;
    core::fmt::UpperExp::fmt(&y2, f)?;
    f.write_str(", ")?;
    core::fmt::UpperExp::fmt(&z2, f)?;
    f.write_str(" |")
  }
}

impl From<[f32; 9]> for Mat3 {
  fn from([xx, xy, xz, yx, yy, yz, zx, zy, zz]: [f32; 9]) -> Self {
    Self {
      x_axis: Vec3::from([xx, xy, xz]),
      y_axis: Vec3::from([yx, yy, yz]),
      z_axis: Vec3::from([zx, zy, zz]),
    }
  }
}
impl From<Mat3> for [f32; 9] {
  fn from(
    Mat3 {
      x_axis,
      y_axis,
      z_axis,
    }: Mat3,
  ) -> Self {
    let [e0, e1, e2]: [f32; 3] = x_axis.into();
    let [e3, e4, e5]: [f32; 3] = y_axis.into();
    let [e6, e7, e8]: [f32; 3] = z_axis.into();
    [e0, e1, e2, e3, e4, e5, e6, e7, e8]
  }
}

#[cfg(feature = "mint")]
impl From<mint::ColumnMatrix3<f32>> for Mat3 {
  fn from(mint::ColumnMatrix3 { x, y, z }: mint::ColumnMatrix3<f32>) -> Self {
    let x_axis: Vec3 = x.into();
    let y_axis: Vec3 = y.into();
    let z_axis: Vec3 = z.into();
    Self {
      x_axis,
      y_axis,
      z_axis,
    }
  }
}
#[cfg(feature = "mint")]
impl From<Mat3> for mint::ColumnMatrix3<f32> {
  fn from(
    Mat3 {
      x_axis,
      y_axis,
      z_axis,
    }: Mat3,
  ) -> Self {
    let x: mint::Vector3<f32> = x_axis.into();
    let y: mint::Vector3<f32> = y_axis.into();
    let z: mint::Vector3<f32> = z_axis.into();
    Self { x, y, z }
  }
}

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

/// ## Accessors
impl Mat3 {
  /// Obtains the `x_axis` column of this matrix
  pub fn x_axis(self) -> Vec3 {
    self.x_axis
  }

  /// Obtains the `y_axis` column of this matrix
  pub fn y_axis(self) -> Vec3 {
    self.y_axis
  }

  /// Obtains the `z_axis` column of this matrix
  pub fn z_axis(self) -> Vec3 {
    self.z_axis
  }

  /// `&mut` to the `x_axis` column of this matrix
  pub fn x_axis_mut(&mut self) -> &mut Vec3 {
    &mut self.x_axis
  }

  /// `&mut` to the `y_axis` column of this matrix
  pub fn y_axis_mut(&mut self) -> &mut Vec3 {
    &mut self.y_axis
  }

  /// `&mut` to the `z_axis` column of this matrix
  pub fn z_axis_mut(&mut self) -> &mut Vec3 {
    &mut self.z_axis
  }
}

/// ## Constructors
impl Mat3 {
  /// Combines the columns given into a new `Mat3`
  pub fn new(x_axis: Vec3, y_axis: Vec3, z_axis: Vec3) -> Self {
    Self {
      x_axis,
      y_axis,
      z_axis,
    }
  }

  /// Splats the given value across all columns.
  pub fn splat(v: f32) -> Self {
    Self {
      x_axis: Vec3::splat(v),
      y_axis: Vec3::splat(v),
      z_axis: Vec3::splat(v),
    }
  }

  /// Forms a diagonal matrix from the values given.
  pub fn diagonal(x: f32, y: f32, z: f32) -> Self {
    let x_axis = Vec3::new(x, 0.0, 0.0);
    let y_axis = Vec3::new(0.0, y, 0.0);
    let z_axis = Vec3::new(0.0, 0.0, z);
    Self {
      x_axis,
      y_axis,
      z_axis,
    }
  }

  /// Down-converts to a [`Mat2`]
  pub fn to_mat2(self) -> Mat2 {
    let x_axis = self.x_axis.to_vec2();
    let y_axis = self.y_axis.to_vec2();
    Mat2 { x_axis, y_axis }
  }

  /// Up-converts to a [`Mat4`] by adding the given `w` as the `w` of the
  /// `w_axis`. The `x_axis`, `y_axis`, and `z_axis` are extended with `0.0`
  pub fn to_mat4(self, w: f32) -> Mat4 {
    let x_axis = self.x_axis.to_vec4(0.0);
    let y_axis = self.y_axis.to_vec4(0.0);
    let z_axis = self.z_axis.to_vec4(0.0);
    let w_axis = Vec4::new(0.0, 0.0, 0.0, w);
    Mat4 {
      x_axis,
      y_axis,
      z_axis,
      w_axis,
    }
  }
}