| 1 |
#![allow(deprecated)] |
| 2 |
|
| 3 |
use crate::{Affine3A, Mat4, Quat, Vec3, Vec3A, Vec3Swizzles}; |
| 4 |
use core::ops::Mul; |
| 5 |
|
| 6 |
#[cfg(feature = "rand")] |
| 7 |
use rand::{ |
| 8 |
distributions::{Distribution, Standard}, |
| 9 |
Rng, |
| 10 |
}; |
| 11 |
|
| 12 |
/** |
| 13 |
* A transform containing non-uniform scale, rotation and translation. |
| 14 |
* |
| 15 |
* Scale and translation are stored as `Vec3A` for better performance. |
| 16 |
*/ |
| 17 |
#[derive(Clone, Copy, PartialEq, Debug)] |
| 18 |
#[repr(C)] |
| 19 |
#[deprecated( |
| 20 |
since = "0.15.0", |
| 21 |
note = "Moving to a separate crate, see https://github.com/bitshifter/glam-rs/issues/175" |
| 22 |
)] |
| 23 |
pub struct TransformSRT { |
| 24 |
pub rotation: Quat, |
| 25 |
pub translation: Vec3, |
| 26 |
pub scale: Vec3, |
| 27 |
} |
| 28 |
|
| 29 |
impl Default for TransformSRT { |
| 30 |
#[inline] |
| 31 |
fn default() -> Self { |
| 32 |
Self::IDENTITY |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* A transform containing rotation and translation. |
| 38 |
* |
| 39 |
* Translation is stored as a `Vec3A` for better performance. |
| 40 |
*/ |
| 41 |
#[derive(Clone, Copy, PartialEq, Debug)] |
| 42 |
#[repr(C)] |
| 43 |
#[deprecated( |
| 44 |
since = "0.15.0", |
| 45 |
note = "Moving to a separate crate, see https://github.com/bitshifter/glam-rs/issues/175" |
| 46 |
)] |
| 47 |
pub struct TransformRT { |
| 48 |
pub rotation: Quat, |
| 49 |
pub translation: Vec3, |
| 50 |
} |
| 51 |
|
| 52 |
impl Default for TransformRT { |
| 53 |
#[inline] |
| 54 |
fn default() -> Self { |
| 55 |
Self::IDENTITY |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
impl TransformSRT { |
| 60 |
/// The identity transforms that does nothing. |
| 61 |
pub const IDENTITY: Self = Self { |
| 62 |
scale: Vec3::ONE, |
| 63 |
rotation: Quat::IDENTITY, |
| 64 |
translation: Vec3::ZERO, |
| 65 |
}; |
| 66 |
|
| 67 |
/// All NaN:s. |
| 68 |
pub const NAN: Self = Self { |
| 69 |
scale: Vec3::NAN, |
| 70 |
rotation: Quat::NAN, |
| 71 |
translation: Vec3::NAN, |
| 72 |
}; |
| 73 |
|
| 74 |
#[inline] |
| 75 |
pub fn from_scale_rotation_translation(scale: Vec3, rotation: Quat, translation: Vec3) -> Self { |
| 76 |
Self { |
| 77 |
rotation, |
| 78 |
translation, |
| 79 |
scale, |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/// Returns `true` if, and only if, all elements are finite. |
| 84 |
/// If any element is either `NaN`, positive or negative infinity, this will return `false`. |
| 85 |
#[inline] |
| 86 |
pub fn is_finite(&self) -> bool { |
| 87 |
self.rotation.is_finite() && self.translation.is_finite() |
| 88 |
} |
| 89 |
|
| 90 |
/// Returns `true` if, and only if, any element is `NaN`. |
| 91 |
#[inline] |
| 92 |
pub fn is_nan(&self) -> bool { |
| 93 |
self.rotation.is_nan() || self.translation.is_nan() |
| 94 |
} |
| 95 |
|
| 96 |
#[inline] |
| 97 |
pub fn inverse(&self) -> Self { |
| 98 |
let scale = self.scale.recip(); |
| 99 |
let rotation = self.rotation.conjugate(); |
| 100 |
let translation = -(rotation * (self.translation * scale)); |
| 101 |
Self { |
| 102 |
rotation, |
| 103 |
translation, |
| 104 |
scale, |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
#[inline] |
| 109 |
pub fn normalize(&self) -> Self { |
| 110 |
let rotation = self.rotation.normalize(); |
| 111 |
Self { |
| 112 |
scale: self.scale, |
| 113 |
rotation, |
| 114 |
translation: self.translation, |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
#[inline] |
| 119 |
pub fn mul_transform(&self, other: &Self) -> Self { |
| 120 |
mul_srt_srt(self, other) |
| 121 |
} |
| 122 |
|
| 123 |
#[deprecated( |
| 124 |
since = "0.15.0", |
| 125 |
note = "Please use `transform_point3(other)` instead" |
| 126 |
)] |
| 127 |
#[inline] |
| 128 |
pub fn transform_vec3(&self, other: Vec3) -> Vec3 { |
| 129 |
self.transform_point3(other) |
| 130 |
} |
| 131 |
|
| 132 |
#[inline] |
| 133 |
pub fn transform_point3(&self, other: Vec3) -> Vec3 { |
| 134 |
self.transform_point3a(other.into()).into() |
| 135 |
} |
| 136 |
|
| 137 |
#[inline] |
| 138 |
pub fn transform_vector3(&self, other: Vec3) -> Vec3 { |
| 139 |
self.transform_vector3a(other.into()).into() |
| 140 |
} |
| 141 |
|
| 142 |
#[inline] |
| 143 |
pub fn transform_point3a(&self, other: Vec3A) -> Vec3A { |
| 144 |
(self.rotation * (other * Vec3A::from(self.scale))) + Vec3A::from(self.translation) |
| 145 |
} |
| 146 |
|
| 147 |
#[inline] |
| 148 |
pub fn transform_vector3a(&self, other: Vec3A) -> Vec3A { |
| 149 |
self.rotation * (other * Vec3A::from(self.scale)) |
| 150 |
} |
| 151 |
|
| 152 |
/// Returns true if the absolute difference of all elements between `self` |
| 153 |
/// and `other` is less than or equal to `max_abs_diff`. |
| 154 |
/// |
| 155 |
/// This can be used to compare if two `Mat4`'s contain similar elements. It |
| 156 |
/// works best when comparing with a known value. The `max_abs_diff` that |
| 157 |
/// should be used used depends on the values being compared against. |
| 158 |
/// |
| 159 |
/// For more on floating point comparisons see |
| 160 |
/// https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ |
| 161 |
#[inline] |
| 162 |
pub fn abs_diff_eq(&self, other: Self, max_abs_diff: f32) -> bool { |
| 163 |
self.scale.abs_diff_eq(other.scale, max_abs_diff) |
| 164 |
&& self.rotation.abs_diff_eq(other.rotation, max_abs_diff) |
| 165 |
&& self |
| 166 |
.translation |
| 167 |
.abs_diff_eq(other.translation, max_abs_diff) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
#[inline] |
| 172 |
fn mul_srt_srt(lhs: &TransformSRT, rhs: &TransformSRT) -> TransformSRT { |
| 173 |
// Based on https://github.com/nfrechette/rtm `rtm::qvv_mul` |
| 174 |
let lhs_scale = Vec3A::from(lhs.scale); |
| 175 |
let rhs_scale = Vec3A::from(rhs.scale); |
| 176 |
let min_scale = lhs_scale.min(rhs_scale); |
| 177 |
let scale = lhs_scale * rhs_scale; |
| 178 |
|
| 179 |
if min_scale.cmplt(Vec3A::ZERO).any() { |
| 180 |
// If negative scale, we go through a matrix |
| 181 |
let lhs_mtx = |
| 182 |
Affine3A::from_scale_rotation_translation(lhs.scale, lhs.rotation, lhs.translation); |
| 183 |
let rhs_mtx = |
| 184 |
Affine3A::from_scale_rotation_translation(rhs.scale, rhs.rotation, rhs.translation); |
| 185 |
let mut result_mtx = lhs_mtx * rhs_mtx; |
| 186 |
|
| 187 |
let sign = scale.signum(); |
| 188 |
result_mtx.x_axis = result_mtx.x_axis.normalize() * sign.xxx(); |
| 189 |
result_mtx.y_axis = result_mtx.y_axis.normalize() * sign.yyy(); |
| 190 |
result_mtx.z_axis = result_mtx.z_axis.normalize() * sign.zzz(); |
| 191 |
|
| 192 |
let scale = Vec3::from(scale); |
| 193 |
let rotation = Quat::from_affine3(&result_mtx); |
| 194 |
let translation = Vec3::from(result_mtx.translation); |
| 195 |
TransformSRT { |
| 196 |
rotation, |
| 197 |
translation, |
| 198 |
scale, |
| 199 |
} |
| 200 |
} else { |
| 201 |
let rotation = lhs.rotation * rhs.rotation; |
| 202 |
let translation = (rhs.rotation * (lhs.translation * rhs.scale)) + rhs.translation; |
| 203 |
TransformSRT { |
| 204 |
rotation, |
| 205 |
translation, |
| 206 |
scale: scale.into(), |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
#[inline] |
| 212 |
fn mul_rt_rt(lhs: &TransformRT, rhs: &TransformRT) -> TransformRT { |
| 213 |
let rotation = lhs.rotation * rhs.rotation; |
| 214 |
let translation = (rhs.rotation * lhs.translation) + rhs.translation; |
| 215 |
TransformRT { |
| 216 |
rotation, |
| 217 |
translation, |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
impl TransformRT { |
| 222 |
/// The identity transforms that does nothing. |
| 223 |
pub const IDENTITY: Self = Self { |
| 224 |
rotation: Quat::IDENTITY, |
| 225 |
translation: Vec3::ZERO, |
| 226 |
}; |
| 227 |
|
| 228 |
/// All NaN:s. |
| 229 |
pub const NAN: Self = Self { |
| 230 |
rotation: Quat::NAN, |
| 231 |
translation: Vec3::NAN, |
| 232 |
}; |
| 233 |
|
| 234 |
#[inline] |
| 235 |
pub fn from_rotation_translation(rotation: Quat, translation: Vec3) -> Self { |
| 236 |
Self { |
| 237 |
rotation, |
| 238 |
translation, |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
/// Returns `true` if, and only if, all elements are finite. |
| 243 |
/// If any element is either `NaN`, positive or negative infinity, this will return `false`. |
| 244 |
#[inline] |
| 245 |
pub fn is_finite(&self) -> bool { |
| 246 |
self.rotation.is_finite() && self.translation.is_finite() |
| 247 |
} |
| 248 |
|
| 249 |
/// Returns `true` if, and only if, any element is `NaN`. |
| 250 |
#[inline] |
| 251 |
pub fn is_nan(&self) -> bool { |
| 252 |
self.rotation.is_nan() || self.translation.is_nan() |
| 253 |
} |
| 254 |
|
| 255 |
#[inline] |
| 256 |
pub fn inverse(&self) -> Self { |
| 257 |
let rotation = self.rotation.conjugate(); |
| 258 |
let translation = -(rotation * self.translation); |
| 259 |
Self { |
| 260 |
rotation, |
| 261 |
translation, |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
#[inline] |
| 266 |
pub fn normalize(&self) -> Self { |
| 267 |
let rotation = self.rotation.normalize(); |
| 268 |
Self { |
| 269 |
rotation, |
| 270 |
translation: self.translation, |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
#[inline] |
| 275 |
pub fn mul_transform(&self, other: &Self) -> Self { |
| 276 |
mul_rt_rt(self, other) |
| 277 |
} |
| 278 |
|
| 279 |
#[deprecated( |
| 280 |
since = "0.15.0", |
| 281 |
note = "Please use `transform_point3(other)` instead" |
| 282 |
)] |
| 283 |
#[inline] |
| 284 |
pub fn transform_vec3(self, other: Vec3) -> Vec3 { |
| 285 |
self.transform_point3(other) |
| 286 |
} |
| 287 |
|
| 288 |
#[inline] |
| 289 |
pub fn transform_point3(&self, other: Vec3) -> Vec3 { |
| 290 |
self.transform_point3a(other.into()).into() |
| 291 |
} |
| 292 |
|
| 293 |
#[inline] |
| 294 |
pub fn transform_vector3(&self, other: Vec3) -> Vec3 { |
| 295 |
self.transform_vector3a(other.into()).into() |
| 296 |
} |
| 297 |
|
| 298 |
#[inline] |
| 299 |
pub fn transform_point3a(&self, other: Vec3A) -> Vec3A { |
| 300 |
(self.rotation * other) + Vec3A::from(self.translation) |
| 301 |
} |
| 302 |
|
| 303 |
#[inline] |
| 304 |
pub fn transform_vector3a(&self, other: Vec3A) -> Vec3A { |
| 305 |
self.rotation * other |
| 306 |
} |
| 307 |
|
| 308 |
/// Returns true if the absolute difference of all elements between `self` |
| 309 |
/// and `other` is less than or equal to `max_abs_diff`. |
| 310 |
/// |
| 311 |
/// This can be used to compare if two `Mat4`'s contain similar elements. It |
| 312 |
/// works best when comparing with a known value. The `max_abs_diff` that |
| 313 |
/// should be used used depends on the values being compared against. |
| 314 |
/// |
| 315 |
/// For more on floating point comparisons see |
| 316 |
/// https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ |
| 317 |
#[inline] |
| 318 |
pub fn abs_diff_eq(&self, other: Self, max_abs_diff: f32) -> bool { |
| 319 |
self.rotation.abs_diff_eq(other.rotation, max_abs_diff) |
| 320 |
&& self |
| 321 |
.translation |
| 322 |
.abs_diff_eq(other.translation, max_abs_diff) |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
impl Mul<TransformRT> for TransformRT { |
| 327 |
type Output = TransformRT; |
| 328 |
#[inline] |
| 329 |
fn mul(self, other: TransformRT) -> TransformRT { |
| 330 |
mul_rt_rt(&self, &other) |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
impl Mul<TransformSRT> for TransformSRT { |
| 335 |
type Output = Self; |
| 336 |
#[inline] |
| 337 |
fn mul(self, other: Self) -> Self::Output { |
| 338 |
mul_srt_srt(&self, &other) |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
impl Mul<TransformRT> for TransformSRT { |
| 343 |
type Output = TransformSRT; |
| 344 |
#[inline] |
| 345 |
fn mul(self, other: TransformRT) -> Self::Output { |
| 346 |
mul_srt_srt(&self, &other.into()) |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
impl Mul<TransformSRT> for TransformRT { |
| 351 |
type Output = TransformSRT; |
| 352 |
#[inline] |
| 353 |
fn mul(self, other: TransformSRT) -> Self::Output { |
| 354 |
mul_srt_srt(&self.into(), &other) |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
impl From<TransformRT> for TransformSRT { |
| 359 |
#[inline] |
| 360 |
fn from(tr: TransformRT) -> Self { |
| 361 |
Self { |
| 362 |
translation: tr.translation, |
| 363 |
rotation: tr.rotation, |
| 364 |
scale: Vec3::ONE, |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
#[cfg(feature = "rand")] |
| 370 |
impl Distribution<TransformRT> for Standard { |
| 371 |
#[inline] |
| 372 |
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> TransformRT { |
| 373 |
TransformRT::from_rotation_translation( |
| 374 |
rng.gen::<Quat>(), |
| 375 |
Vec3::new( |
| 376 |
rng.gen_range(core::f32::MIN..=core::f32::MAX), |
| 377 |
rng.gen_range(core::f32::MIN..=core::f32::MAX), |
| 378 |
rng.gen_range(core::f32::MIN..=core::f32::MAX), |
| 379 |
), |
| 380 |
) |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
#[cfg(feature = "rand")] |
| 385 |
impl Distribution<TransformSRT> for Standard { |
| 386 |
#[inline] |
| 387 |
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> TransformSRT { |
| 388 |
let mut gen_non_zero = || loop { |
| 389 |
let f: f32 = rng.gen_range(core::f32::MIN..=core::f32::MAX); |
| 390 |
if f.abs() > core::f32::MIN_POSITIVE { |
| 391 |
return f; |
| 392 |
} |
| 393 |
}; |
| 394 |
TransformSRT::from_scale_rotation_translation( |
| 395 |
Vec3::new(gen_non_zero(), gen_non_zero(), gen_non_zero()), |
| 396 |
rng.gen::<Quat>(), |
| 397 |
Vec3::new( |
| 398 |
rng.gen_range(core::f32::MIN..=core::f32::MAX), |
| 399 |
rng.gen_range(core::f32::MIN..=core::f32::MAX), |
| 400 |
rng.gen_range(core::f32::MIN..=core::f32::MAX), |
| 401 |
), |
| 402 |
) |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
impl From<TransformSRT> for Mat4 { |
| 407 |
#[inline] |
| 408 |
fn from(srt: TransformSRT) -> Self { |
| 409 |
Self::from_scale_rotation_translation(srt.scale, srt.rotation, srt.translation) |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
impl From<TransformRT> for Mat4 { |
| 414 |
#[inline] |
| 415 |
fn from(rt: TransformRT) -> Self { |
| 416 |
Self::from_rotation_translation(rt.rotation, rt.translation) |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
impl From<TransformSRT> for Affine3A { |
| 421 |
#[inline] |
| 422 |
fn from(srt: TransformSRT) -> Self { |
| 423 |
Self::from_scale_rotation_translation(srt.scale, srt.rotation, srt.translation) |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
impl From<TransformRT> for Affine3A { |
| 428 |
#[inline] |
| 429 |
fn from(rt: TransformRT) -> Self { |
| 430 |
Self::from_rotation_translation(rt.rotation, rt.translation) |
| 431 |
} |
| 432 |
} |