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