Skip to content

Commit 4cb4ffe

Browse files
committed
Use explicit methods to convert between arrays and matrices.
Removed `From` trait implementations converting between 1D and 2D arrays and matrix types to avoid any potential confusion about data being loaded and stored in column major order. New function names hopefully make it clear that column major ordering is being used. Fixes #21.
1 parent 5b69980 commit 4cb4ffe

File tree

9 files changed

+230
-192
lines changed

9 files changed

+230
-192
lines changed

src/f32/glam_mint.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl From<Quat> for mint::Quaternion<f32> {
8484

8585
impl From<mint::RowMatrix2<f32>> for Mat2 {
8686
fn from(m: mint::RowMatrix2<f32>) -> Self {
87-
Self::new(m.x.into(), m.y.into()).transpose()
87+
Self::from_cols(m.x.into(), m.y.into()).transpose()
8888
}
8989
}
9090

@@ -100,7 +100,7 @@ impl From<Mat2> for mint::RowMatrix2<f32> {
100100

101101
impl From<mint::ColumnMatrix2<f32>> for Mat2 {
102102
fn from(m: mint::ColumnMatrix2<f32>) -> Self {
103-
Self::new(m.x.into(), m.y.into())
103+
Self::from_cols(m.x.into(), m.y.into())
104104
}
105105
}
106106

@@ -115,7 +115,7 @@ impl From<Mat2> for mint::ColumnMatrix2<f32> {
115115

116116
impl From<mint::RowMatrix3<f32>> for Mat3 {
117117
fn from(m: mint::RowMatrix3<f32>) -> Self {
118-
Self::new(m.x.into(), m.y.into(), m.z.into()).transpose()
118+
Self::from_cols(m.x.into(), m.y.into(), m.z.into()).transpose()
119119
}
120120
}
121121

@@ -132,7 +132,7 @@ impl From<Mat3> for mint::RowMatrix3<f32> {
132132

133133
impl From<mint::ColumnMatrix3<f32>> for Mat3 {
134134
fn from(m: mint::ColumnMatrix3<f32>) -> Self {
135-
Self::new(m.x.into(), m.y.into(), m.z.into())
135+
Self::from_cols(m.x.into(), m.y.into(), m.z.into())
136136
}
137137
}
138138

@@ -148,7 +148,7 @@ impl From<Mat3> for mint::ColumnMatrix3<f32> {
148148

149149
impl From<mint::RowMatrix4<f32>> for Mat4 {
150150
fn from(m: mint::RowMatrix4<f32>) -> Self {
151-
Self::new(m.x.into(), m.y.into(), m.z.into(), m.w.into()).transpose()
151+
Self::from_cols(m.x.into(), m.y.into(), m.z.into(), m.w.into()).transpose()
152152
}
153153
}
154154

@@ -166,7 +166,7 @@ impl From<Mat4> for mint::RowMatrix4<f32> {
166166

167167
impl From<mint::ColumnMatrix4<f32>> for Mat4 {
168168
fn from(m: mint::ColumnMatrix4<f32>) -> Self {
169-
Self::new(m.x.into(), m.y.into(), m.z.into(), m.w.into())
169+
Self::from_cols(m.x.into(), m.y.into(), m.z.into(), m.w.into())
170170
}
171171
}
172172

@@ -261,7 +261,7 @@ mod test {
261261
#[test]
262262
fn test_matrix2() {
263263
use crate::Mat2;
264-
let g = Mat2::from([[1.0, 2.0], [3.0, 4.0]]);
264+
let g = Mat2::from_cols_array_2d(&[[1.0, 2.0], [3.0, 4.0]]);
265265
let m = mint::ColumnMatrix2::from(g);
266266
assert_eq!(g, Mat2::from(m));
267267
let mt = mint::RowMatrix2::from(g);
@@ -272,7 +272,7 @@ mod test {
272272
#[test]
273273
fn test_matrix3() {
274274
use crate::Mat3;
275-
let g = Mat3::from([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]);
275+
let g = Mat3::from_cols_array_2d(&[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]);
276276
let m = mint::ColumnMatrix3::from(g);
277277
assert_eq!(g, Mat3::from(m));
278278
let mt = mint::RowMatrix3::from(g);
@@ -286,7 +286,7 @@ mod test {
286286
#[test]
287287
fn test_matrix4() {
288288
use crate::Mat4;
289-
let g = Mat4::from([
289+
let g = Mat4::from_cols_array_2d(&[
290290
[1.0, 2.0, 3.0, 4.0],
291291
[5.0, 6.0, 7.0, 8.0],
292292
[9.0, 10.0, 11.0, 12.0],

src/f32/glam_serde.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl<'de> Deserialize<'de> for Mat2 {
303303
}
304304
let x = Vec2::new(f[0], f[1]);
305305
let y = Vec2::new(f[2], f[3]);
306-
Ok(Mat2::new(x, y))
306+
Ok(Mat2::from_cols(x, y))
307307
}
308308
}
309309

@@ -340,7 +340,7 @@ impl<'de> Deserialize<'de> for Mat3 {
340340
let x = Vec3::new(f[0], f[1], f[2]);
341341
let y = Vec3::new(f[3], f[4], f[5]);
342342
let z = Vec3::new(f[6], f[7], f[8]);
343-
Ok(Mat3::new(x, y, z))
343+
Ok(Mat3::from_cols(x, y, z))
344344
}
345345
}
346346

@@ -378,7 +378,7 @@ impl<'de> Deserialize<'de> for Mat4 {
378378
let y = Vec4::new(f[4], f[5], f[6], f[7]);
379379
let z = Vec4::new(f[8], f[9], f[10], f[11]);
380380
let w = Vec4::new(f[12], f[13], f[14], f[15]);
381-
Ok(Mat4::new(x, y, z, w))
381+
Ok(Mat4::from_cols(x, y, z, w))
382382
}
383383
}
384384

src/f32/mat2.rs

+39-34
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::ops::{Add, Mul, Sub};
1010

1111
#[inline]
1212
pub fn mat2(x_axis: Vec2, y_axis: Vec2) -> Mat2 {
13-
Mat2::new(x_axis, y_axis)
13+
Mat2::from_cols(x_axis, y_axis)
1414
}
1515

1616
/// A 2x2 column major matrix.
@@ -35,11 +35,47 @@ impl Mat2 {
3535
Self(Vec4::new(1.0, 0.0, 0.0, 1.0))
3636
}
3737

38+
#[deprecated(since = "0.7.2", note = "please use `Mat4::from_cols` instead")]
3839
#[inline]
3940
pub fn new(x_axis: Vec2, y_axis: Vec2) -> Self {
41+
Self::from_cols(x_axis, y_axis)
42+
}
43+
44+
/// Creates a new `Mat2` from four column vectors.
45+
#[inline]
46+
pub fn from_cols(x_axis: Vec2, y_axis: Vec2) -> Self {
4047
Self(Vec4::new(x_axis.x(), x_axis.y(), y_axis.x(), y_axis.y()))
4148
}
4249

50+
/// Creates a new `Mat2` from a `[f32; 4]` stored in column major order.
51+
/// If your data is stored in row major you will need to `transpose` the resulting `Mat2`.
52+
#[inline]
53+
pub fn from_cols_array(m: &[f32; 4]) -> Self {
54+
Mat2(Vec4::new(m[0], m[1], m[2], m[3]))
55+
}
56+
57+
/// Creates a new `[f32; 4]` storing data in column major order.
58+
/// If you require data in row major order `transpose` the `Mat2` first.
59+
#[inline]
60+
pub fn to_cols_array(&self) -> [f32; 4] {
61+
self.0.into()
62+
}
63+
64+
/// Creates a new `Mat2` from a `[[f32; 2]; 2]` stored in column major order.
65+
/// If your data is in row major order you will need to `transpose` the resulting `Mat2`.
66+
#[inline]
67+
pub fn from_cols_array_2d(m: &[[f32; 2]; 2]) -> Self {
68+
Mat2(Vec4::new(m[0][0], m[0][1], m[1][0], m[1][1]))
69+
}
70+
71+
/// Creates a new `[[f32; 2]; 2]` storing data in column major order.
72+
/// If you require data in row major order `transpose` the `Mat2` first.
73+
#[inline]
74+
pub fn to_cols_array_2d(&self) -> [[f32; 2]; 2] {
75+
let (x0, y0, x1, y1) = self.0.into();
76+
[[x0, y0], [x1, y1]]
77+
}
78+
4379
/// Create a 2x2 matrix containing scale and rotation (in radians).
4480
#[inline]
4581
pub fn from_scale_angle(scale: Vec2, angle: f32) -> Self {
@@ -128,7 +164,7 @@ impl Mat2 {
128164
pub fn mul_mat2(&self, rhs: &Self) -> Self {
129165
// TODO: SSE2
130166
let (x0, y0, x1, y1) = rhs.0.into();
131-
Mat2::new(
167+
Mat2::from_cols(
132168
self.mul_vec2(Vec2::new(x0, y0)),
133169
self.mul_vec2(Vec2::new(x1, y1)),
134170
)
@@ -155,7 +191,7 @@ impl Mat2 {
155191
impl Distribution<Mat2> for Standard {
156192
#[inline]
157193
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Mat2 {
158-
rng.gen::<[[f32; 2]; 2]>().into()
194+
Mat2::from_cols_array(&rng.gen())
159195
}
160196
}
161197

@@ -220,34 +256,3 @@ impl Mul<f32> for Mat2 {
220256
self.mul_scalar(rhs)
221257
}
222258
}
223-
224-
impl From<[[f32; 2]; 2]> for Mat2 {
225-
#[inline]
226-
fn from(m: [[f32; 2]; 2]) -> Self {
227-
Mat2(Vec4::new(m[0][0], m[0][1], m[1][0], m[1][1]))
228-
}
229-
}
230-
231-
impl From<Mat2> for [[f32; 2]; 2] {
232-
#[inline]
233-
fn from(m: Mat2) -> Self {
234-
let (x0, y0, x1, y1) = m.0.into();
235-
[[x0, y0], [x1, y1]]
236-
}
237-
}
238-
239-
impl From<[f32; 4]> for Mat2 {
240-
#[inline]
241-
/// Load from array in column major order.
242-
fn from(m: [f32; 4]) -> Self {
243-
Mat2(m.into())
244-
}
245-
}
246-
247-
impl From<Mat2> for [f32; 4] {
248-
#[inline]
249-
/// Store to array in column major order.
250-
fn from(m: Mat2) -> Self {
251-
m.0.into()
252-
}
253-
}

src/f32/mat3.rs

+49-44
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,62 @@ impl Mat3 {
7676
}
7777
}
7878

79+
#[deprecated(since = "0.7.2", note = "please use `Mat3::from_cols` instead")]
7980
#[inline]
8081
pub fn new(x_axis: Vec3, y_axis: Vec3, z_axis: Vec3) -> Self {
82+
Self::from_cols(x_axis, y_axis, z_axis)
83+
}
84+
85+
/// Creates a new `Mat3` from three column vectors.
86+
#[inline]
87+
pub fn from_cols(x_axis: Vec3, y_axis: Vec3, z_axis: Vec3) -> Self {
8188
Self {
8289
x_axis,
8390
y_axis,
8491
z_axis,
8592
}
8693
}
8794

88-
/// Create a 3x3 matrix that can scale, rotate and translate a 2D vector.
95+
/// Creates a new `Mat3` from a `[f32; 9]` stored in column major order.
96+
/// If your data is stored in row major you will need to `transpose` the resulting `Mat3`.
97+
#[inline]
98+
pub fn from_cols_array(m: &[f32; 9]) -> Self {
99+
Mat3 {
100+
x_axis: Vec3::new(m[0], m[1], m[2]),
101+
y_axis: Vec3::new(m[3], m[4], m[5]),
102+
z_axis: Vec3::new(m[6], m[7], m[8]),
103+
}
104+
}
105+
106+
/// Creates a new `[f32; 9]` storing data in column major order.
107+
/// If you require data in row major order `transpose` the `Mat3` first.
108+
#[inline]
109+
pub fn to_cols_array(&self) -> [f32; 9] {
110+
let (m00, m01, m02) = self.x_axis.into();
111+
let (m10, m11, m12) = self.y_axis.into();
112+
let (m20, m21, m22) = self.z_axis.into();
113+
[m00, m01, m02, m10, m11, m12, m20, m21, m22]
114+
}
115+
116+
/// Creates a new `Mat3` from a `[[f32; 3]; 3]` stored in column major order.
117+
/// If your data is in row major order you will need to `transpose` the resulting `Mat3`.
118+
#[inline]
119+
pub fn from_cols_array_2d(m: &[[f32; 3]; 3]) -> Self {
120+
Mat3 {
121+
x_axis: m[0].into(),
122+
y_axis: m[1].into(),
123+
z_axis: m[2].into(),
124+
}
125+
}
126+
127+
/// Creates a new `[[f32; 3]; 3]` storing data in column major order.
128+
/// If you require data in row major order `transpose` the `Mat3` first.
129+
#[inline]
130+
pub fn to_cols_array_2d(&self) -> [[f32; 3]; 3] {
131+
[self.x_axis.into(), self.y_axis.into(), self.z_axis.into()]
132+
}
133+
134+
/// Creates a new `Mat3` that can scale, rotate and translate a 2D vector.
89135
/// `angle` is in radians.
90136
#[inline]
91137
pub fn from_scale_angle_translation(scale: Vec2, angle: f32, translation: Vec2) -> Self {
@@ -235,7 +281,7 @@ impl Mat3 {
235281
glam_assert!(det.cmpne(Vec3::zero()).all());
236282
let inv_det = det.reciprocal();
237283
// TODO: Work out if it's possible to get rid of the transpose
238-
Mat3::new(tmp0 * inv_det, tmp1 * inv_det, tmp2 * inv_det).transpose()
284+
Mat3::from_cols(tmp0 * inv_det, tmp1 * inv_det, tmp2 * inv_det).transpose()
239285
}
240286

241287
#[inline]
@@ -301,7 +347,7 @@ impl Mat3 {
301347
impl Distribution<Mat3> for Standard {
302348
#[inline]
303349
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Mat3 {
304-
rng.gen::<[[f32; 3]; 3]>().into()
350+
Mat3::from_cols_array(&rng.gen())
305351
}
306352
}
307353

@@ -352,44 +398,3 @@ impl Mul<f32> for Mat3 {
352398
self.mul_scalar(rhs)
353399
}
354400
}
355-
356-
impl From<[[f32; 3]; 3]> for Mat3 {
357-
#[inline]
358-
fn from(m: [[f32; 3]; 3]) -> Self {
359-
Mat3 {
360-
x_axis: m[0].into(),
361-
y_axis: m[1].into(),
362-
z_axis: m[2].into(),
363-
}
364-
}
365-
}
366-
367-
impl From<Mat3> for [[f32; 3]; 3] {
368-
#[inline]
369-
fn from(m: Mat3) -> Self {
370-
[m.x_axis.into(), m.y_axis.into(), m.z_axis.into()]
371-
}
372-
}
373-
374-
impl From<[f32; 9]> for Mat3 {
375-
#[inline]
376-
/// Load from array in column major order.
377-
fn from(m: [f32; 9]) -> Self {
378-
Mat3 {
379-
x_axis: Vec3::new(m[0], m[1], m[2]),
380-
y_axis: Vec3::new(m[3], m[4], m[5]),
381-
z_axis: Vec3::new(m[6], m[7], m[8]),
382-
}
383-
}
384-
}
385-
386-
impl From<Mat3> for [f32; 9] {
387-
#[inline]
388-
/// Store to array in column major order.
389-
fn from(m: Mat3) -> Self {
390-
let (m00, m01, m02) = m.x_axis.into();
391-
let (m10, m11, m12) = m.y_axis.into();
392-
let (m20, m21, m22) = m.z_axis.into();
393-
[m00, m01, m02, m10, m11, m12, m20, m21, m22]
394-
}
395-
}

0 commit comments

Comments
 (0)