nphysics2d/utils/
generalized_cross.rs1use na::RealField;
2use na::{Matrix2, Matrix3, RowVector2, Vector1, Vector2, Vector3};
3
4pub trait GeneralizedCross {
7 type Rhs;
9 type CrossVector;
11 type CrossMatrix;
13 type CrossMatrixTr;
15
16 fn gcross(&self, rhs: &Self::Rhs) -> Self::CrossVector;
18
19 fn gcross_matrix(&self) -> Self::CrossMatrix;
21
22 fn gcross_matrix_tr(&self) -> Self::CrossMatrixTr;
24}
25
26impl<N: RealField + Copy> GeneralizedCross for Vector1<N> {
27 type Rhs = Vector2<N>;
28 type CrossVector = Vector2<N>;
29 type CrossMatrix = Matrix2<N>;
30 type CrossMatrixTr = Matrix2<N>;
31
32 #[inline]
33 fn gcross(&self, rhs: &Vector2<N>) -> Vector2<N> {
34 Vector2::new(-rhs.y * self.x, rhs.x * self.x)
35 }
36
37 #[inline]
38 fn gcross_matrix(&self) -> Matrix2<N> {
39 Matrix2::new(N::zero(), -self.x, self.x, N::zero())
40 }
41
42 #[inline]
43 fn gcross_matrix_tr(&self) -> Matrix2<N> {
44 Matrix2::new(N::zero(), self.x, -self.x, N::zero())
45 }
46}
47
48impl<N: RealField + Copy> GeneralizedCross for Vector2<N> {
49 type Rhs = Vector2<N>;
50 type CrossVector = Vector1<N>;
51 type CrossMatrix = RowVector2<N>;
52 type CrossMatrixTr = Vector2<N>;
53
54 #[inline]
55 fn gcross(&self, rhs: &Vector2<N>) -> Vector1<N> {
56 Vector1::new(self.x * rhs.y - self.y * rhs.x)
57 }
58
59 #[inline]
60 fn gcross_matrix(&self) -> RowVector2<N> {
61 RowVector2::new(-self.y, self.x)
62 }
63
64 #[inline]
65 fn gcross_matrix_tr(&self) -> Vector2<N> {
66 Vector2::new(-self.y, self.x)
67 }
68}
69
70impl<N: RealField + Copy> GeneralizedCross for Vector3<N> {
71 type Rhs = Vector3<N>;
72 type CrossVector = Vector3<N>;
73 type CrossMatrix = Matrix3<N>;
74 type CrossMatrixTr = Matrix3<N>;
75
76 #[inline]
77 fn gcross(&self, rhs: &Vector3<N>) -> Vector3<N> {
78 self.cross(rhs)
79 }
80
81 #[inline]
82 fn gcross_matrix(&self) -> Matrix3<N> {
83 self.cross_matrix()
84 }
85
86 #[inline]
87 fn gcross_matrix_tr(&self) -> Matrix3<N> {
88 Matrix3::new(
89 N::zero(),
90 self.z,
91 -self.y,
92 -self.z,
93 N::zero(),
94 self.x,
95 self.y,
96 -self.x,
97 N::zero(),
98 )
99 }
100}