Ver código fonte

:sparkles: can create rotation matrix without the angular feature now

Felix Bytow 2 anos atrás
pai
commit
42fa05b20f
1 arquivos alterados com 28 adições e 17 exclusões
  1. 28 17
      src/types/matrix/generic.rs

+ 28 - 17
src/types/matrix/generic.rs

@@ -429,25 +429,34 @@ impl<T: Numeric + Float + Primitive> SquareMatrix<T, 4> {
         SquareMatrix::diagonal(&[x, y, z, one])
     }
 
-    /// Create a 3D rotation matrix.
-    #[cfg(feature = "angular")]
-    pub fn rotation<A: Angular<T>>(angle: A, x: T, y: T, z: T) -> Self {
+    fn rotation_impl(angle: T, x: T, y: T, z: T) -> Self {
         let zero = unsafe { T::whole(0) };
         let one = unsafe { T::whole(1) };
 
-        let rad_angle = angle.radiant();
-        let c = rad_angle.cos();
-        let s = rad_angle.sin();
+        let c = angle.cos();
+        let s = angle.sin();
         let omc = one - c;
 
-        Self {
-            data: [
-                [x * x * omc + c, x * y * omc - z * s, x * z * omc + y * s, zero],
-                [y * x * omc + z * s, y * y * omc + c, y * z * omc - x * s, zero],
-                [x * z * omc - y * s, y * z * omc + x * s, z * z * omc + c, zero],
-                [zero, zero, zero, one],
-            ],
-        }
+        Self::from([
+            [x * x * omc + c, x * y * omc - z * s, x * z * omc + y * s, zero],
+            [y * x * omc + z * s, y * y * omc + c, y * z * omc - x * s, zero],
+            [x * z * omc - y * s, y * z * omc + x * s, z * z * omc + c, zero],
+            [zero, zero, zero, one],
+        ])
+    }
+
+    /// Create a 3D rotation matrix.
+    ///
+    /// `angle` is expected to be in radians.
+    #[cfg(not(feature = "angular"))]
+    pub fn rotation(angle: T, x: T, y: T, z: T) -> Self {
+        Self::rotation_impl(angle, x, y, z)
+    }
+
+    /// Create a 3D rotation matrix.
+    #[cfg(feature = "angular")]
+    pub fn rotation<A: Angular<T>>(angle: A, x: T, y: T, z: T) -> Self {
+        Self::rotation_impl(angle.radiant(), x, y, z)
     }
 }
 
@@ -473,9 +482,11 @@ mod tests {
     #[test]
     fn identity_matrix() {
         let mat: SquareMatrix<f64, 3> = SquareMatrix::identity();
-        let expected = SquareMatrix {
-            data: [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
-        };
+        let expected = SquareMatrix::from([
+            [1.0, 0.0, 0.0],
+            [0.0, 1.0, 0.0],
+            [0.0, 0.0, 1.0],
+        ]);
 
         assert_eq!(mat, expected);
     }