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