Skip to content

Commit d1ac967

Browse files
committed
Add Basis::from_quat
1 parent b863148 commit d1ac967

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

gdnative-core/src/core_types/geom/basis.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::core_types::{IsEqualApprox, Quat, Vector3};
22
use core::ops::Mul;
3+
use glam::Mat3;
34

45
/// A 3x3 matrix.
56
#[repr(C)]
@@ -86,6 +87,18 @@ impl Basis {
8687
b
8788
}
8889

90+
/// Constructs a pure rotation basis matrix from the given quaternion.
91+
#[inline]
92+
pub fn from_quat(quat: Quat) -> Self {
93+
let basis = Mat3::from_quat(quat.glam()).to_cols_array_2d();
94+
let basis = [
95+
Vector3::new(basis[0][0], basis[1][0], basis[2][0]),
96+
Vector3::new(basis[0][1], basis[1][1], basis[2][1]),
97+
Vector3::new(basis[0][2], basis[1][2], basis[2][2]),
98+
];
99+
Basis::from_elements(basis)
100+
}
101+
89102
/// Rotation matrix from axis and angle.
90103
///
91104
/// See <https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_angle>

gdnative-core/src/core_types/quat.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{Basis, IsEqualApprox, Vector3};
2-
use glam::{Mat3, EulerRot};
2+
use glam::EulerRot;
33
use std::ops::Mul;
44

55
#[derive(Copy, Clone, Debug, PartialEq)]
@@ -68,14 +68,7 @@ impl Quat {
6868
/// the rotation angles in the format (X angle, Y angle, Z angle).
6969
#[inline]
7070
pub fn to_euler(self) -> Vector3 {
71-
let basis = Mat3::from_quat(self.glam());
72-
let basis = basis.to_cols_array_2d();
73-
let basis = [
74-
Vector3::new(basis[0][0], basis[1][0], basis[2][0]),
75-
Vector3::new(basis[0][1], basis[1][1], basis[2][1]),
76-
Vector3::new(basis[0][2], basis[1][2], basis[2][2]),
77-
];
78-
Basis::from_elements(basis).to_euler()
71+
Basis::from_quat(self).to_euler()
7972
}
8073

8174
/// Returns the inverse of the quaternion.
@@ -159,13 +152,12 @@ impl Quat {
159152
}
160153

161154
#[inline]
162-
fn gd(quat: glam::Quat) -> Self {
155+
pub(super) fn gd(quat: glam::Quat) -> Self {
163156
Self::new(quat.x, quat.y, quat.z, quat.w)
164157
}
165158

166159
#[inline]
167-
#[allow(unused)]
168-
fn glam(self) -> glam::Quat {
160+
pub(super) fn glam(self) -> glam::Quat {
169161
glam::Quat::from_xyzw(self.x, self.y, self.z, self.w)
170162
}
171163
}
@@ -199,13 +191,7 @@ mod test {
199191
Vector3::new(0.136732, -0.959216, -0.247404),
200192
Vector3::new(-0.837788, -0.245243, 0.487819),
201193
]);
202-
let basis = Mat3::from_quat(quat.glam()).to_cols_array_2d();
203-
let basis = [
204-
Vector3::new(basis[0][0], basis[1][0], basis[2][0]),
205-
Vector3::new(basis[0][1], basis[1][1], basis[2][1]),
206-
Vector3::new(basis[0][2], basis[1][2], basis[2][2]),
207-
];
208-
let basis = Basis::from_elements(basis);
194+
let basis = Basis::from_quat(quat);
209195
assert!(basis.is_equal_approx(&expect));
210196
}
211197

0 commit comments

Comments
 (0)