nalgebra/base/
coordinates.rs

1#![allow(missing_docs)]
2
3//! Structures to which matrices and vector can be auto-dereferenced (through `Deref`) to access
4//! components using their names. For example, if `v` is a 3D vector, one can write `v.z` instead
5//! of `v[2]`.
6
7use std::ops::{Deref, DerefMut};
8
9use crate::base::dimension::{U1, U2, U3, U4, U5, U6};
10use crate::base::storage::{IsContiguous, RawStorage, RawStorageMut};
11use crate::base::{Matrix, Scalar};
12
13/*
14 *
15 * Give coordinates to owned Vector{1 .. 6} and Matrix{1 .. 6}
16 *
17 */
18
19macro_rules! coords_impl(
20    ($T: ident; $($comps: ident),*) => {
21        /// Data structure used to provide access to matrix and vector coordinates with the dot
22        /// notation, e.g., `v.x` is the same as `v[0]` for a vector.
23        #[repr(C)]
24        #[derive(Eq, PartialEq, Clone, Hash, Debug, Copy)]
25        #[cfg_attr(feature = "serde-serialize-no-std", derive(Serialize, Deserialize))]
26        pub struct $T<T: Scalar> {
27            $(pub $comps: T),*
28        }
29    }
30);
31
32macro_rules! deref_impl(
33    ($R: ty, $C: ty; $Target: ident) => {
34        impl<T: Scalar, S> Deref for Matrix<T, $R, $C, S>
35            where S: RawStorage<T, $R, $C> + IsContiguous {
36            type Target = $Target<T>;
37
38            #[inline]
39            fn deref(&self) -> &Self::Target {
40                // Safety: this is OK because of the IsContiguous trait.
41                unsafe { &*(self.data.ptr() as *const Self::Target) }
42            }
43        }
44
45        impl<T: Scalar, S> DerefMut for Matrix<T, $R, $C, S>
46            where S: RawStorageMut<T, $R, $C> + IsContiguous {
47            #[inline]
48            fn deref_mut(&mut self) -> &mut Self::Target {
49                // Safety: this is OK because of the IsContiguous trait.
50                unsafe { &mut *(self.data.ptr_mut() as *mut Self::Target) }
51            }
52        }
53    }
54);
55
56/*
57 *
58 * Vector coordinates.
59 *
60 */
61coords_impl!(X; x);
62coords_impl!(XY; x, y);
63coords_impl!(XYZ; x, y, z);
64coords_impl!(XYZW; x, y, z, w);
65coords_impl!(XYZWA; x, y, z, w, a);
66coords_impl!(XYZWAB; x, y, z, w, a, b);
67coords_impl!(IJKW; i, j, k, w);
68
69/*
70 * Rectangular matrices with 2 rows.
71 */
72coords_impl!(M2x2; m11, m21,
73                   m12, m22);
74coords_impl!(M2x3; m11, m21,
75                   m12, m22,
76                   m13, m23);
77coords_impl!(M2x4; m11, m21,
78                   m12, m22,
79                   m13, m23,
80                   m14, m24);
81coords_impl!(M2x5; m11, m21,
82                   m12, m22,
83                   m13, m23,
84                   m14, m24,
85                   m15, m25);
86coords_impl!(M2x6; m11, m21,
87                   m12, m22,
88                   m13, m23,
89                   m14, m24,
90                   m15, m25,
91                   m16, m26);
92
93/*
94 * Rectangular matrices with 3 rows.
95 */
96coords_impl!(M3x2; m11, m21, m31,
97                   m12, m22, m32);
98coords_impl!(M3x3; m11, m21, m31,
99                   m12, m22, m32,
100                   m13, m23, m33);
101coords_impl!(M3x4; m11, m21, m31,
102                   m12, m22, m32,
103                   m13, m23, m33,
104                   m14, m24, m34);
105coords_impl!(M3x5; m11, m21, m31,
106                   m12, m22, m32,
107                   m13, m23, m33,
108                   m14, m24, m34,
109                   m15, m25, m35);
110coords_impl!(M3x6; m11, m21, m31,
111                   m12, m22, m32,
112                   m13, m23, m33,
113                   m14, m24, m34,
114                   m15, m25, m35,
115                   m16, m26, m36);
116
117/*
118 * Rectangular matrices with 4 rows.
119 */
120coords_impl!(M4x2; m11, m21, m31, m41,
121                   m12, m22, m32, m42);
122coords_impl!(M4x3; m11, m21, m31, m41,
123                   m12, m22, m32, m42,
124                   m13, m23, m33, m43);
125coords_impl!(M4x4; m11, m21, m31, m41,
126                   m12, m22, m32, m42,
127                   m13, m23, m33, m43,
128                   m14, m24, m34, m44);
129coords_impl!(M4x5; m11, m21, m31, m41,
130                   m12, m22, m32, m42,
131                   m13, m23, m33, m43,
132                   m14, m24, m34, m44,
133                   m15, m25, m35, m45);
134coords_impl!(M4x6; m11, m21, m31, m41,
135                   m12, m22, m32, m42,
136                   m13, m23, m33, m43,
137                   m14, m24, m34, m44,
138                   m15, m25, m35, m45,
139                   m16, m26, m36, m46);
140
141/*
142 * Rectangular matrices with 5 rows.
143 */
144coords_impl!(M5x2; m11, m21, m31, m41, m51,
145                   m12, m22, m32, m42, m52);
146coords_impl!(M5x3; m11, m21, m31, m41, m51,
147                   m12, m22, m32, m42, m52,
148                   m13, m23, m33, m43, m53);
149coords_impl!(M5x4; m11, m21, m31, m41, m51,
150                   m12, m22, m32, m42, m52,
151                   m13, m23, m33, m43, m53,
152                   m14, m24, m34, m44, m54);
153coords_impl!(M5x5; m11, m21, m31, m41, m51,
154                   m12, m22, m32, m42, m52,
155                   m13, m23, m33, m43, m53,
156                   m14, m24, m34, m44, m54,
157                   m15, m25, m35, m45, m55);
158coords_impl!(M5x6; m11, m21, m31, m41, m51,
159                   m12, m22, m32, m42, m52,
160                   m13, m23, m33, m43, m53,
161                   m14, m24, m34, m44, m54,
162                   m15, m25, m35, m45, m55,
163                   m16, m26, m36, m46, m56);
164
165/*
166 * Rectangular matrices with 6 rows.
167 */
168
169coords_impl!(M6x2; m11, m21, m31, m41, m51, m61,
170                   m12, m22, m32, m42, m52, m62);
171coords_impl!(M6x3; m11, m21, m31, m41, m51, m61,
172                   m12, m22, m32, m42, m52, m62,
173                   m13, m23, m33, m43, m53, m63);
174coords_impl!(M6x4; m11, m21, m31, m41, m51, m61,
175                   m12, m22, m32, m42, m52, m62,
176                   m13, m23, m33, m43, m53, m63,
177                   m14, m24, m34, m44, m54, m64);
178coords_impl!(M6x5; m11, m21, m31, m41, m51, m61,
179                   m12, m22, m32, m42, m52, m62,
180                   m13, m23, m33, m43, m53, m63,
181                   m14, m24, m34, m44, m54, m64,
182                   m15, m25, m35, m45, m55, m65);
183coords_impl!(M6x6; m11, m21, m31, m41, m51, m61,
184                   m12, m22, m32, m42, m52, m62,
185                   m13, m23, m33, m43, m53, m63,
186                   m14, m24, m34, m44, m54, m64,
187                   m15, m25, m35, m45, m55, m65,
188                   m16, m26, m36, m46, m56, m66);
189
190/*
191 *
192 * Attach coordinates to matrices.
193 *
194 */
195deref_impl!(U1, U1; X);
196deref_impl!(U2, U1; XY);
197deref_impl!(U3, U1; XYZ);
198deref_impl!(U4, U1; XYZW);
199deref_impl!(U5, U1; XYZWA);
200deref_impl!(U6, U1; XYZWAB);
201
202deref_impl!(U1, U2; XY);
203deref_impl!(U1, U3; XYZ);
204deref_impl!(U1, U4; XYZW);
205deref_impl!(U1, U5; XYZWA);
206deref_impl!(U1, U6; XYZWAB);
207
208deref_impl!(U2, U2; M2x2);
209deref_impl!(U2, U3; M2x3);
210deref_impl!(U2, U4; M2x4);
211deref_impl!(U2, U5; M2x5);
212deref_impl!(U2, U6; M2x6);
213
214deref_impl!(U3, U2; M3x2);
215deref_impl!(U3, U3; M3x3);
216deref_impl!(U3, U4; M3x4);
217deref_impl!(U3, U5; M3x5);
218deref_impl!(U3, U6; M3x6);
219
220deref_impl!(U4, U2; M4x2);
221deref_impl!(U4, U3; M4x3);
222deref_impl!(U4, U4; M4x4);
223deref_impl!(U4, U5; M4x5);
224deref_impl!(U4, U6; M4x6);
225
226deref_impl!(U5, U2; M5x2);
227deref_impl!(U5, U3; M5x3);
228deref_impl!(U5, U4; M5x4);
229deref_impl!(U5, U5; M5x5);
230deref_impl!(U5, U6; M5x6);
231
232deref_impl!(U6, U2; M6x2);
233deref_impl!(U6, U3; M6x3);
234deref_impl!(U6, U4; M6x4);
235deref_impl!(U6, U5; M6x5);
236deref_impl!(U6, U6; M6x6);