| 1 |
use super::glam::{ |
| 2 |
BVec2, BVec3, BVec4, DMat2, DMat3, DMat4, DVec2, DVec3, DVec4, IVec2, IVec3, IVec4, Mat2, Mat3, |
| 3 |
Mat4, UVec2, UVec3, UVec4, Vec2, Vec3, Vec3A, Vec4, |
| 4 |
}; |
| 5 |
use crate::storage::RawStorage; |
| 6 |
use crate::{ |
| 7 |
Matrix, Matrix2, Matrix3, Matrix4, U2, U3, U4, Unit, UnitVector2, UnitVector3, UnitVector4, |
| 8 |
Vector, Vector2, Vector3, Vector4, |
| 9 |
}; |
| 10 |
use std::convert::TryFrom; |
| 11 |
|
| 12 |
macro_rules! impl_vec_conversion( |
| 13 |
($N: ty, $Vec2: ty, $Vec3: ty, $Vec4: ty) => { |
| 14 |
impl From<$Vec2> for Vector2<$N> { |
| 15 |
#[inline] |
| 16 |
fn from(e: $Vec2) -> Vector2<$N> { |
| 17 |
<[$N;2]>::from(e).into() |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
impl<S> From<Vector<$N, U2, S>> for $Vec2 |
| 22 |
where |
| 23 |
S: RawStorage<$N, U2>, |
| 24 |
{ |
| 25 |
#[inline] |
| 26 |
fn from(e: Vector<$N, U2, S>) -> $Vec2 { |
| 27 |
<$Vec2>::new(e[0], e[1]) |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
impl From<$Vec3> for Vector3<$N> { |
| 32 |
#[inline] |
| 33 |
fn from(e: $Vec3) -> Vector3<$N> { |
| 34 |
<[$N;3]>::from(e).into() |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
impl<S> From<Vector<$N, U3, S>> for $Vec3 |
| 39 |
where |
| 40 |
S: RawStorage<$N, U3>, |
| 41 |
{ |
| 42 |
#[inline] |
| 43 |
fn from(e: Vector<$N, U3, S>) -> $Vec3 { |
| 44 |
<$Vec3>::new(e[0], e[1], e[2]) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
impl From<$Vec4> for Vector4<$N> { |
| 49 |
#[inline] |
| 50 |
fn from(e: $Vec4) -> Vector4<$N> { |
| 51 |
<[$N;4]>::from(e).into() |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
impl<S> From<Vector<$N, U4, S>> for $Vec4 |
| 56 |
where |
| 57 |
S: RawStorage<$N, U4>, |
| 58 |
{ |
| 59 |
#[inline] |
| 60 |
fn from(e: Vector<$N, U4, S>) -> $Vec4 { |
| 61 |
<$Vec4>::new(e[0], e[1], e[2], e[3]) |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
); |
| 66 |
|
| 67 |
impl_vec_conversion!(f32, Vec2, Vec3, Vec4); |
| 68 |
impl_vec_conversion!(f64, DVec2, DVec3, DVec4); |
| 69 |
impl_vec_conversion!(i32, IVec2, IVec3, IVec4); |
| 70 |
impl_vec_conversion!(u32, UVec2, UVec3, UVec4); |
| 71 |
impl_vec_conversion!(bool, BVec2, BVec3, BVec4); |
| 72 |
|
| 73 |
const ERR: &'static str = "Normalization failed."; |
| 74 |
|
| 75 |
macro_rules! impl_unit_vec_conversion( |
| 76 |
($N: ty, $Vec2: ty, $Vec3: ty, $Vec4: ty) => { |
| 77 |
impl TryFrom<$Vec2> for UnitVector2<$N> { |
| 78 |
type Error = &'static str; |
| 79 |
#[inline] |
| 80 |
fn try_from(e: $Vec2) -> Result<Self, Self::Error> { |
| 81 |
Unit::try_new(e.into(), 0.0).ok_or(ERR) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
impl From<UnitVector2<$N>> for $Vec2 |
| 86 |
{ |
| 87 |
#[inline] |
| 88 |
fn from(e: UnitVector2<$N>) -> $Vec2 { |
| 89 |
e.into_inner().into() |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
impl TryFrom<$Vec3> for UnitVector3<$N> { |
| 94 |
type Error = &'static str; |
| 95 |
#[inline] |
| 96 |
fn try_from(e: $Vec3) -> Result<Self, Self::Error> { |
| 97 |
Unit::try_new(e.into(), 0.0).ok_or(ERR) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
impl From<UnitVector3<$N>> for $Vec3 |
| 102 |
{ |
| 103 |
#[inline] |
| 104 |
fn from(e: UnitVector3<$N>) -> $Vec3 { |
| 105 |
e.into_inner().into() |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
impl TryFrom<$Vec4> for UnitVector4<$N> { |
| 110 |
type Error = &'static str; |
| 111 |
#[inline] |
| 112 |
fn try_from(e: $Vec4) -> Result<Self, Self::Error> { |
| 113 |
Unit::try_new(e.into(), 0.0).ok_or(ERR) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
impl From<UnitVector4<$N>> for $Vec4 |
| 118 |
{ |
| 119 |
#[inline] |
| 120 |
fn from(e: UnitVector4<$N>) -> $Vec4 { |
| 121 |
e.into_inner().into() |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
); |
| 126 |
|
| 127 |
impl_unit_vec_conversion!(f32, Vec2, Vec3, Vec4); |
| 128 |
impl_unit_vec_conversion!(f64, DVec2, DVec3, DVec4); |
| 129 |
|
| 130 |
impl From<Vec3A> for Vector3<f32> { |
| 131 |
#[inline] |
| 132 |
fn from(e: Vec3A) -> Vector3<f32> { |
| 133 |
(*e.as_ref()).into() |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
impl<S> From<Vector<f32, U3, S>> for Vec3A |
| 138 |
where |
| 139 |
S: RawStorage<f32, U3>, |
| 140 |
{ |
| 141 |
#[inline] |
| 142 |
fn from(e: Vector<f32, U3, S>) -> Vec3A { |
| 143 |
Vec3A::new(e[0], e[1], e[2]) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
impl TryFrom<Vec3A> for UnitVector3<f32> { |
| 148 |
type Error = &'static str; |
| 149 |
#[inline] |
| 150 |
fn try_from(e: Vec3A) -> Result<Self, Self::Error> { |
| 151 |
Unit::try_new(e.into(), 0.0).ok_or(ERR) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
impl From<UnitVector3<f32>> for Vec3A { |
| 156 |
#[inline] |
| 157 |
fn from(e: UnitVector3<f32>) -> Vec3A { |
| 158 |
e.into_inner().into() |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
impl From<Mat2> for Matrix2<f32> { |
| 163 |
#[inline] |
| 164 |
fn from(e: Mat2) -> Matrix2<f32> { |
| 165 |
e.to_cols_array_2d().into() |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
impl<S> From<Matrix<f32, U2, U2, S>> for Mat2 |
| 170 |
where |
| 171 |
S: RawStorage<f32, U2, U2>, |
| 172 |
{ |
| 173 |
#[inline] |
| 174 |
fn from(e: Matrix<f32, U2, U2, S>) -> Mat2 { |
| 175 |
Mat2::from_cols( |
| 176 |
Vec2::new(e[(0, 0)], e[(1, 0)]), |
| 177 |
Vec2::new(e[(0, 1)], e[(1, 1)]), |
| 178 |
) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
impl From<Mat3> for Matrix3<f32> { |
| 183 |
#[inline] |
| 184 |
fn from(e: Mat3) -> Matrix3<f32> { |
| 185 |
e.to_cols_array_2d().into() |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
impl<S> From<Matrix<f32, U3, U3, S>> for Mat3 |
| 190 |
where |
| 191 |
S: RawStorage<f32, U3, U3>, |
| 192 |
{ |
| 193 |
#[inline] |
| 194 |
fn from(e: Matrix<f32, U3, U3, S>) -> Mat3 { |
| 195 |
Mat3::from_cols( |
| 196 |
Vec3::new(e[(0, 0)], e[(1, 0)], e[(2, 0)]), |
| 197 |
Vec3::new(e[(0, 1)], e[(1, 1)], e[(2, 1)]), |
| 198 |
Vec3::new(e[(0, 2)], e[(1, 2)], e[(2, 2)]), |
| 199 |
) |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
impl From<Mat4> for Matrix4<f32> { |
| 204 |
#[inline] |
| 205 |
fn from(e: Mat4) -> Matrix4<f32> { |
| 206 |
e.to_cols_array_2d().into() |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
impl<S> From<Matrix<f32, U4, U4, S>> for Mat4 |
| 211 |
where |
| 212 |
S: RawStorage<f32, U4, U4>, |
| 213 |
{ |
| 214 |
#[inline] |
| 215 |
fn from(e: Matrix<f32, U4, U4, S>) -> Mat4 { |
| 216 |
Mat4::from_cols( |
| 217 |
Vec4::new(e[(0, 0)], e[(1, 0)], e[(2, 0)], e[(3, 0)]), |
| 218 |
Vec4::new(e[(0, 1)], e[(1, 1)], e[(2, 1)], e[(3, 1)]), |
| 219 |
Vec4::new(e[(0, 2)], e[(1, 2)], e[(2, 2)], e[(3, 2)]), |
| 220 |
Vec4::new(e[(0, 3)], e[(1, 3)], e[(2, 3)], e[(3, 3)]), |
| 221 |
) |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
impl From<DMat2> for Matrix2<f64> { |
| 226 |
#[inline] |
| 227 |
fn from(e: DMat2) -> Matrix2<f64> { |
| 228 |
e.to_cols_array_2d().into() |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
impl<S> From<Matrix<f64, U2, U2, S>> for DMat2 |
| 233 |
where |
| 234 |
S: RawStorage<f64, U2, U2>, |
| 235 |
{ |
| 236 |
#[inline] |
| 237 |
fn from(e: Matrix<f64, U2, U2, S>) -> DMat2 { |
| 238 |
DMat2::from_cols( |
| 239 |
DVec2::new(e[(0, 0)], e[(1, 0)]), |
| 240 |
DVec2::new(e[(0, 1)], e[(1, 1)]), |
| 241 |
) |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
impl From<DMat3> for Matrix3<f64> { |
| 246 |
#[inline] |
| 247 |
fn from(e: DMat3) -> Matrix3<f64> { |
| 248 |
e.to_cols_array_2d().into() |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
impl<S> From<Matrix<f64, U3, U3, S>> for DMat3 |
| 253 |
where |
| 254 |
S: RawStorage<f64, U3, U3>, |
| 255 |
{ |
| 256 |
#[inline] |
| 257 |
fn from(e: Matrix<f64, U3, U3, S>) -> DMat3 { |
| 258 |
DMat3::from_cols( |
| 259 |
DVec3::new(e[(0, 0)], e[(1, 0)], e[(2, 0)]), |
| 260 |
DVec3::new(e[(0, 1)], e[(1, 1)], e[(2, 1)]), |
| 261 |
DVec3::new(e[(0, 2)], e[(1, 2)], e[(2, 2)]), |
| 262 |
) |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
impl From<DMat4> for Matrix4<f64> { |
| 267 |
#[inline] |
| 268 |
fn from(e: DMat4) -> Matrix4<f64> { |
| 269 |
e.to_cols_array_2d().into() |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
impl<S> From<Matrix<f64, U4, U4, S>> for DMat4 |
| 274 |
where |
| 275 |
S: RawStorage<f64, U4, U4>, |
| 276 |
{ |
| 277 |
#[inline] |
| 278 |
fn from(e: Matrix<f64, U4, U4, S>) -> DMat4 { |
| 279 |
DMat4::from_cols( |
| 280 |
DVec4::new(e[(0, 0)], e[(1, 0)], e[(2, 0)], e[(3, 0)]), |
| 281 |
DVec4::new(e[(0, 1)], e[(1, 1)], e[(2, 1)], e[(3, 1)]), |
| 282 |
DVec4::new(e[(0, 2)], e[(1, 2)], e[(2, 2)], e[(3, 2)]), |
| 283 |
DVec4::new(e[(0, 3)], e[(1, 3)], e[(2, 3)], e[(3, 3)]), |
| 284 |
) |
| 285 |
} |
| 286 |
} |