Skip to main content

rapier2d/utils/
matrix_column.rs

1//! MatrixColumn trait for matrix column access.
2
3use crate::math::{Matrix, Vector};
4#[cfg(not(target_arch = "spirv"))]
5use na::Scalar;
6
7/// Extension trait for matrix column access (like nalgebra's `.column()`)
8pub trait MatrixColumn {
9    /// The column type returned by `column()`.
10    type Column;
11    /// Returns the i-th column of this matrix.
12    fn column(&self, i: usize) -> Self::Column;
13}
14
15impl<T: Copy> MatrixColumn for [T; 2] {
16    type Column = T;
17    fn column(&self, i: usize) -> Self::Column {
18        self[i]
19    }
20}
21
22impl MatrixColumn for Matrix {
23    type Column = Vector;
24    #[inline]
25    fn column(&self, i: usize) -> Self::Column {
26        self.col(i)
27    }
28}
29
30#[cfg(not(target_arch = "spirv"))]
31impl<T: Scalar> MatrixColumn for na::Matrix3<T> {
32    type Column = na::Vector3<T>;
33    #[inline]
34    fn column(&self, i: usize) -> Self::Column {
35        self.column(i).into_owned()
36    }
37}
38
39#[cfg(not(target_arch = "spirv"))]
40impl<T: Scalar> MatrixColumn for na::Matrix2<T> {
41    type Column = na::Vector2<T>;
42    #[inline]
43    fn column(&self, i: usize) -> Self::Column {
44        self.column(i).into_owned()
45    }
46}