| 1 | use crate::core::{ |
| 2 | storage::{Columns2, Columns3, Columns4, XY, XYZ, XYZF32A16, XYZW}, |
| 3 | traits::{ |
| 4 | matrix::{ |
| 5 | FloatMatrix2x2, FloatMatrix3x3, FloatMatrix4x4, Matrix, Matrix2x2, Matrix3x3, |
| 6 | Matrix4x4, MatrixConst, |
| 7 | }, |
| 8 | projection::ProjectionMatrix, |
| 9 | scalar::{FloatEx, NanConstEx, NumEx}, |
| 10 | vector::*, |
| 11 | }, |
| 12 | }; |
| 13 | |
| 14 | impl<T: NumEx> MatrixConst for Columns2<XY<T>> { |
| 15 | const ZERO: Self = Self { |
| 16 | x_axis: XY::ZERO, |
| 17 | y_axis: XY::ZERO, |
| 18 | }; |
| 19 | const IDENTITY: Self = Self { |
| 20 | x_axis: XY::X, |
| 21 | y_axis: XY::Y, |
| 22 | }; |
| 23 | } |
| 24 | |
| 25 | impl<T: NanConstEx> NanConstEx for Columns2<XY<T>> { |
| 26 | const NAN: Self = Self { |
| 27 | x_axis: XY::NAN, |
| 28 | y_axis: XY::NAN, |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | impl<T: NumEx> Matrix<T> for Columns2<XY<T>> {} |
| 33 | |
| 34 | impl<T: NumEx> Matrix2x2<T, XY<T>> for Columns2<XY<T>> { |
| 35 | #[inline(always)] |
| 36 | fn from_cols(x_axis: XY<T>, y_axis: XY<T>) -> Self { |
| 37 | Self { x_axis, y_axis } |
| 38 | } |
| 39 | |
| 40 | #[inline(always)] |
| 41 | fn x_axis(&self) -> &XY<T> { |
| 42 | &self.x_axis |
| 43 | } |
| 44 | |
| 45 | #[inline(always)] |
| 46 | fn y_axis(&self) -> &XY<T> { |
| 47 | &self.y_axis |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | impl<T: FloatEx> FloatMatrix2x2<T, XY<T>> for Columns2<XY<T>> {} |
| 52 | |
| 53 | impl<T: NumEx> MatrixConst for Columns3<XYZ<T>> { |
| 54 | const ZERO: Self = Self { |
| 55 | x_axis: XYZ::ZERO, |
| 56 | y_axis: XYZ::ZERO, |
| 57 | z_axis: XYZ::ZERO, |
| 58 | }; |
| 59 | const IDENTITY: Self = Self { |
| 60 | x_axis: XYZ::X, |
| 61 | y_axis: XYZ::Y, |
| 62 | z_axis: XYZ::Z, |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | impl<T: NanConstEx> NanConstEx for Columns3<XYZ<T>> { |
| 67 | const NAN: Self = Self { |
| 68 | x_axis: XYZ::NAN, |
| 69 | y_axis: XYZ::NAN, |
| 70 | z_axis: XYZ::NAN, |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | impl<T: NumEx> Matrix<T> for Columns3<XYZ<T>> {} |
| 75 | |
| 76 | impl<T: NumEx> Matrix3x3<T, XYZ<T>> for Columns3<XYZ<T>> { |
| 77 | #[inline(always)] |
| 78 | fn from_cols(x_axis: XYZ<T>, y_axis: XYZ<T>, z_axis: XYZ<T>) -> Self { |
| 79 | Self { |
| 80 | x_axis, |
| 81 | y_axis, |
| 82 | z_axis, |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | #[inline(always)] |
| 87 | fn x_axis(&self) -> &XYZ<T> { |
| 88 | &self.x_axis |
| 89 | } |
| 90 | |
| 91 | #[inline(always)] |
| 92 | fn y_axis(&self) -> &XYZ<T> { |
| 93 | &self.y_axis |
| 94 | } |
| 95 | |
| 96 | #[inline(always)] |
| 97 | fn z_axis(&self) -> &XYZ<T> { |
| 98 | &self.z_axis |
| 99 | } |
| 100 | |
| 101 | #[inline] |
| 102 | fn mul_vector(&self, other: XYZ<T>) -> XYZ<T> { |
| 103 | // default implementation uses splat_x etc, which might not be optimal. Need to check. |
| 104 | let mut res = self.x_axis.mul_scalar(other.x); |
| 105 | res = self.y_axis.mul_scalar(other.y).add(res); |
| 106 | res = self.z_axis.mul_scalar(other.z).add(res); |
| 107 | res |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | impl<T: FloatEx> FloatMatrix3x3<T, XYZ<T>> for Columns3<XYZ<T>> { |
| 112 | #[inline] |
| 113 | fn transform_point2(&self, other: XY<T>) -> XY<T> { |
| 114 | // TODO: This is untested, probably slower than the high level code that uses a SIMD mat2 |
| 115 | Columns2::from_cols(self.x_axis.into(), self.y_axis.into()) |
| 116 | .mul_vector(other) |
| 117 | .add(self.z_axis.into()) |
| 118 | } |
| 119 | |
| 120 | #[inline] |
| 121 | fn transform_vector2(&self, other: XY<T>) -> XY<T> { |
| 122 | // TODO: This is untested, probably slower than the high level code that uses a SIMD mat2 |
| 123 | Columns2::from_cols(self.x_axis.into(), self.y_axis.into()).mul_vector(other) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | impl MatrixConst for Columns3<XYZF32A16> { |
| 128 | const ZERO: Self = Self { |
| 129 | x_axis: XYZF32A16::ZERO, |
| 130 | y_axis: XYZF32A16::ZERO, |
| 131 | z_axis: XYZF32A16::ZERO, |
| 132 | }; |
| 133 | const IDENTITY: Self = Self { |
| 134 | x_axis: XYZF32A16::X, |
| 135 | y_axis: XYZF32A16::Y, |
| 136 | z_axis: XYZF32A16::Z, |
| 137 | }; |
| 138 | } |
| 139 | |
| 140 | impl NanConstEx for Columns3<XYZF32A16> { |
| 141 | const NAN: Self = Self { |
| 142 | x_axis: XYZF32A16::NAN, |
| 143 | y_axis: XYZF32A16::NAN, |
| 144 | z_axis: XYZF32A16::NAN, |
| 145 | }; |
| 146 | } |
| 147 | |
| 148 | impl Matrix<f32> for Columns3<XYZF32A16> {} |
| 149 | |
| 150 | impl Matrix3x3<f32, XYZF32A16> for Columns3<XYZF32A16> { |
| 151 | #[inline(always)] |
| 152 | fn from_cols(x_axis: XYZF32A16, y_axis: XYZF32A16, z_axis: XYZF32A16) -> Self { |
| 153 | Self { |
| 154 | x_axis, |
| 155 | y_axis, |
| 156 | z_axis, |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | #[inline(always)] |
| 161 | fn x_axis(&self) -> &XYZF32A16 { |
| 162 | &self.x_axis |
| 163 | } |
| 164 | |
| 165 | #[inline(always)] |
| 166 | fn y_axis(&self) -> &XYZF32A16 { |
| 167 | &self.y_axis |
| 168 | } |
| 169 | |
| 170 | #[inline(always)] |
| 171 | fn z_axis(&self) -> &XYZF32A16 { |
| 172 | &self.z_axis |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | impl FloatMatrix3x3<f32, XYZF32A16> for Columns3<XYZF32A16> { |
| 177 | #[inline] |
| 178 | fn transform_point2(&self, other: XY<f32>) -> XY<f32> { |
| 179 | // TODO: This is untested, probably slower than the high level code that uses a SIMD mat2 |
| 180 | Columns2::from_cols(self.x_axis.into_xy(), self.y_axis.into_xy()) |
| 181 | .mul_vector(other) |
| 182 | .add(self.z_axis.into_xy()) |
| 183 | } |
| 184 | |
| 185 | #[inline] |
| 186 | fn transform_vector2(&self, other: XY<f32>) -> XY<f32> { |
| 187 | // TODO: This is untested, probably slower than the high level code that uses a SIMD mat2 |
| 188 | Columns2::from_cols(self.x_axis.into_xy(), self.y_axis.into_xy()).mul_vector(other) |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | impl<T: NumEx> MatrixConst for Columns4<XYZW<T>> { |
| 193 | const ZERO: Self = Self { |
| 194 | x_axis: XYZW::ZERO, |
| 195 | y_axis: XYZW::ZERO, |
| 196 | z_axis: XYZW::ZERO, |
| 197 | w_axis: XYZW::ZERO, |
| 198 | }; |
| 199 | const IDENTITY: Self = Self { |
| 200 | x_axis: XYZW::X, |
| 201 | y_axis: XYZW::Y, |
| 202 | z_axis: XYZW::Z, |
| 203 | w_axis: XYZW::W, |
| 204 | }; |
| 205 | } |
| 206 | |
| 207 | impl<T: NanConstEx> NanConstEx for Columns4<XYZW<T>> { |
| 208 | const NAN: Self = Self { |
| 209 | x_axis: XYZW::NAN, |
| 210 | y_axis: XYZW::NAN, |
| 211 | z_axis: XYZW::NAN, |
| 212 | w_axis: XYZW::NAN, |
| 213 | }; |
| 214 | } |
| 215 | |
| 216 | impl<T: NumEx> Matrix<T> for Columns4<XYZW<T>> {} |
| 217 | |
| 218 | impl<T: NumEx> Matrix4x4<T, XYZW<T>> for Columns4<XYZW<T>> { |
| 219 | #[rustfmt::skip] |
| 220 | #[inline(always)] |
| 221 | fn from_cols(x_axis: XYZW<T>, y_axis: XYZW<T>, z_axis: XYZW<T>, w_axis: XYZW<T>) -> Self { |
| 222 | Self { x_axis, y_axis, z_axis, w_axis } |
| 223 | } |
| 224 | |
| 225 | #[inline(always)] |
| 226 | fn x_axis(&self) -> &XYZW<T> { |
| 227 | &self.x_axis |
| 228 | } |
| 229 | |
| 230 | #[inline(always)] |
| 231 | fn y_axis(&self) -> &XYZW<T> { |
| 232 | &self.y_axis |
| 233 | } |
| 234 | |
| 235 | #[inline(always)] |
| 236 | fn z_axis(&self) -> &XYZW<T> { |
| 237 | &self.z_axis |
| 238 | } |
| 239 | |
| 240 | #[inline(always)] |
| 241 | fn w_axis(&self) -> &XYZW<T> { |
| 242 | &self.w_axis |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | impl<T: FloatEx> FloatMatrix4x4<T, XYZW<T>> for Columns4<XYZW<T>> { |
| 247 | type SIMDVector3 = XYZ<T>; |
| 248 | |
| 249 | #[inline(always)] |
| 250 | fn transform_float4_as_point3(&self, other: XYZ<T>) -> XYZ<T> { |
| 251 | self.transform_point3(other) |
| 252 | } |
| 253 | |
| 254 | #[inline(always)] |
| 255 | fn transform_float4_as_vector3(&self, other: XYZ<T>) -> XYZ<T> { |
| 256 | self.transform_vector3(other) |
| 257 | } |
| 258 | |
| 259 | #[inline(always)] |
| 260 | fn project_float4_as_point3(&self, other: XYZ<T>) -> XYZ<T> { |
| 261 | self.project_point3(other) |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | impl<T: FloatEx> ProjectionMatrix<T, XYZW<T>> for Columns4<XYZW<T>> {} |
| 266 | |
| 267 | impl From<Columns3<XYZ<f32>>> for Columns3<XYZF32A16> { |
| 268 | fn from(v: Columns3<XYZ<f32>>) -> Columns3<XYZF32A16> { |
| 269 | Self { |
| 270 | x_axis: v.x_axis.into(), |
| 271 | y_axis: v.y_axis.into(), |
| 272 | z_axis: v.z_axis.into(), |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | impl From<Columns3<XYZF32A16>> for Columns3<XYZ<f32>> { |
| 278 | fn from(v: Columns3<XYZF32A16>) -> Columns3<XYZ<f32>> { |
| 279 | Self { |
| 280 | x_axis: v.x_axis.into(), |
| 281 | y_axis: v.y_axis.into(), |
| 282 | z_axis: v.z_axis.into(), |
| 283 | } |
| 284 | } |
| 285 | } |