spatial_math/
alias.rs

1// Copyright (C) 2020-2025 spatial-math authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::Real;
16
17pub type SMatrix<const M: usize, const N: usize> = na::SMatrix<Real, M, N>;
18/// Static alloc vector type alias.
19pub type SVector<const N: usize> = na::SVector<Real, N>;
20/// Dynamic matrix that has 6 rows and N columns.
21pub type Matrix6xN = na::Matrix6xX<Real>;
22
23pub type Mat3 = SMatrix<3, 3>;
24pub type Vec3 = SVector<3>;
25pub type Vec4 = SVector<4>;
26pub type Vec6 = SVector<6>;
27pub type UnitQuat = na::UnitQuaternion<Real>;
28pub type Quat = na::Quaternion<Real>;
29pub type UnitVec3 = na::Unit<Vec3>;
30pub type DVector = na::DVector<Real>;
31
32pub type MatrixViewMut<'a, const R: usize, const C: usize> =
33    na::MatrixViewMut<'a, Real, na::Const<R>, na::Const<C>>;
34
35pub type MatrixView<'a, const R: usize, const C: usize> =
36    na::MatrixView<'a, Real, na::Const<R>, na::Const<C>>;
37
38pub type DMatrix = na::DMatrix<Real>;
39pub type DMatrixView<'a> = na::DMatrixView<'a, Real>;
40pub type DMatrixViewMut<'a> = na::DMatrixViewMut<'a, Real>;
41pub type DVectorView<'a> = na::DVectorView<'a, Real>;
42pub type DVectorViewMut<'a> = na::DVectorViewMut<'a, Real>;
43
44pub const VEC3_ZERO: Vec3 = na::vector![0.0, 0.0, 0.0];
45
46#[inline]
47pub const fn vec3(x: Real, y: Real, z: Real) -> Vec3 {
48    na::vector![x, y, z]
49}
50
51#[inline]
52pub const fn vec4(x: Real, y: Real, z: Real, w: Real) -> Vec4 {
53    na::vector![x, y, z, w]
54}
55
56#[inline]
57pub const fn vec6(array: [Real; 6]) -> Vec6 {
58    na::vector![array[0], array[1], array[2], array[3], array[4], array[5]]
59}
60
61#[inline]
62pub fn unit_vec3(x: Real, y: Real, z: Real) -> UnitVec3 {
63    na::Unit::new_normalize(vec3(x, y, z))
64}
65
66#[inline]
67pub fn unit_vec3_unchecked(x: Real, y: Real, z: Real) -> UnitVec3 {
68    na::Unit::new_unchecked(vec3(x, y, z))
69}
70
71#[inline]
72pub fn unit_quat(x: Real, y: Real, z: Real, w: Real) -> UnitQuat {
73    #[cfg(debug_assertions)]
74    {
75        let norm = x * x + y * y + z * z + w * w;
76        debug_assert!(norm.abs() > 1e-6, "norm is zero");
77    }
78    na::UnitQuaternion::new_normalize(Quat::new(w, x, y, z))
79}
80
81#[inline]
82pub fn unit_quat_unchecked(x: Real, y: Real, z: Real, w: Real) -> UnitQuat {
83    #[cfg(debug_assertions)]
84    {
85        let norm = (x * x + y * y + z * z + w * w).sqrt();
86        debug_assert!(
87            (norm - 1.0).abs() < 1e-6,
88            "Quaternion is not normalized: {norm}",
89        );
90    }
91    na::UnitQuaternion::new_unchecked(Quat::new(w, x, y, z))
92}
93
94#[cfg(test)]
95mod tests {
96    use approx::assert_relative_eq;
97    use na::Quaternion;
98
99    use crate::{UnitQuat, unit_quat, vec4};
100
101    #[test]
102    fn test_unit_quat() {
103        let q = unit_quat(1.0, 2.0, 3.0, 4.0);
104        let q_expected = UnitQuat::new_normalize(Quaternion::from_vector(vec4(1.0, 2.0, 3.0, 4.0)));
105        assert_relative_eq!(q, q_expected);
106    }
107}