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