alloy/model/transform.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
//! The transform module defines model transformation types
use crate::{
core::hint,
math::{
mat::{Mat4, Matrix4},
vec::{Vec3, Vec4, Vector3},
Angle, Quaternion,
},
};
/// A transform represents the transformation matrix of a model
///
/// This type only implements [`Clone`] and not [`Copy`] because it is a large
/// type that is more efficient to pass by reference. If you need to create a
/// new transform that is a copy of an existing transform, you can use the
/// [`Clone`] trait to do so.
#[derive(Debug, Clone)]
pub struct Transform {
rotation: Quaternion,
translation: Vector3,
scale: Vector3,
dirty: bool,
transform: Matrix4,
}
// Transform currently takes ~2 cache entries (128 bytes), where the raw uncomputed
// content is 64 bytes, and the computed content is 64 bytes.
const _: () = assert!(std::mem::size_of::<Transform>() == 128);
// Constructors
impl Transform {
const DEFAULT_TRANSLATION: Vector3 = Vector3::ZERO;
const DEFAULT_SCALE: Vector3 = Vector3::new(1.0, 1.0, 1.0);
const DEFAULT_ROTATION: Quaternion = Quaternion::UNIT;
/// Represents the Identity transformation.
pub const IDENTITY: Self = Self::new(
Self::DEFAULT_TRANSLATION,
Self::DEFAULT_ROTATION,
Self::DEFAULT_SCALE,
);
/// Creates a new transform with the given position, rotation, and scale
///
/// # Parameters
///
/// * `translation` - The translation of the transform
/// * `rotation` - The rotation of the transform
/// * `scale` - The scale of the transform
#[must_use]
pub const fn new(translation: Vector3, rotation: Quaternion, scale: Vector3) -> Self {
Self {
translation,
rotation,
scale,
transform: Matrix4::IDENTITY,
dirty: true,
}
}
/// Creates a new transform representing just the translation
///
/// # Parameters
///
/// * `translation` - The translation of the transform
#[inline(always)]
#[must_use]
pub const fn from_translation(translation: Vector3) -> Self {
Self::new(translation, Quaternion::UNIT, Self::DEFAULT_SCALE)
}
/// Creates a new transform representing a translation only in the x direction.
///
/// # Parameters
///
/// * `x` - The translation along the x-axis
#[inline(always)]
#[must_use]
pub const fn from_x_translation(x: f32) -> Self {
Self::from_translation(Vector3::new(x, 0.0, 0.0))
}
/// Creates a new transform representing a translation only in the y direction.
///
/// # Parameters
///
/// * `y` - The translation along the y-axis
#[inline(always)]
#[must_use]
pub const fn from_y_translation(y: f32) -> Self {
Self::from_translation(Vector3::new(0.0, y, 0.0))
}
/// Creates a new transform representing a translation only in the z direction.
///
/// # Parameters
///
/// * `z` - The translation along the z-axis
#[inline(always)]
#[must_use]
pub const fn from_z_translation(z: f32) -> Self {
Self::from_translation(Vector3::new(0.0, 0.0, z))
}
/// Creates a new transform representing just the translation
///
/// # Parameters
///
/// * `translation` - The translation of the transform
#[inline(always)]
#[must_use]
pub const fn from_translation_vec3(translation: &Vec3) -> Self {
let vec3 = Vector3::new(translation.x(), translation.y(), translation.z());
Self::from_translation(vec3)
}
/// Creates a new transform representing just the rotation
///
/// # Parameters
///
/// * `rotation` - The rotation of the transform
#[inline(always)]
#[must_use]
pub const fn from_rotation(rotation: Quaternion) -> Self {
Self::new(Self::DEFAULT_TRANSLATION, rotation, Self::DEFAULT_SCALE)
}
/// Creates a new transform representing just the rotation identified in a
/// [`Vec4`] encoded as a [`Quaternion`].
///
/// # Parameters
///
/// * `rotation` - The rotation of the transform
pub const fn from_rotation_vec4(rotation: &Vec4) -> Self {
let vec4 = Quaternion::new(rotation.x(), rotation.y(), rotation.z(), rotation.w());
Self::from_rotation(vec4)
}
/// Creates a new transform representing just a yaw rotation
///
/// # Parameters
///
/// * `angle` - The yaw angle of the transform
#[inline(always)]
#[must_use]
pub fn from_yaw_rotation<A: Angle>(angle: A) -> Self {
Self::from_rotation(Quaternion::from_yaw(angle))
}
/// Creates a new transform representing just a pitch rotation
///
/// # Parameters
///
/// * `angle` - The pitch angle of the transform
#[inline(always)]
#[must_use]
pub fn from_pitch_rotation<A: Angle>(angle: A) -> Self {
Self::from_rotation(Quaternion::from_pitch(angle))
}
/// Creates a new transform representing just a roll rotation
///
/// # Parameters
///
/// * `angle` - The roll angle of the transform
#[inline(always)]
#[must_use]
pub fn from_roll_rotation<A: Angle>(angle: A) -> Self {
Self::from_rotation(Quaternion::from_roll(angle))
}
/// Creates a new transform representing just the scale
///
/// # Parameters
///
/// * `scale` - The scale of the transform
#[inline(always)]
pub const fn from_scale(scale: Vector3) -> Self {
Self::new(Self::DEFAULT_TRANSLATION, Self::DEFAULT_ROTATION, scale)
}
/// Creates a new transform representing just the scale
///
/// # Parameters
///
/// * `scale` - The scale of the transform
#[inline(always)]
#[must_use]
pub const fn from_scale_vec3(scale: &Vec3) -> Self {
// Note: the ToOwned is not const in stable, so this is done to preserve
// constness.
let vec3 = Vector3::new(scale.x(), scale.y(), scale.z());
Self::from_scale(vec3)
}
/// Creates a new transform representing a uniform scale
///
/// # Parameters
///
/// * `scale` - The scale factor
#[inline(always)]
#[must_use]
pub const fn from_uniform_scale(scale: f32) -> Self {
Self::from_scale(Vector3::new(scale, scale, scale))
}
/// Creates a new transform representing a non-uniform scale
///
/// # Parameters
///
/// * `x` - The scale factor along the x-axis
/// * `y` - The scale factor along the y-axis
/// * `z` - The scale factor along the z-axis
#[inline(always)]
#[must_use]
pub const fn from_nonuniform_scale(x: f32, y: f32, z: f32) -> Self {
Self::from_scale(Vector3::new(x, y, z))
}
/// Creates a new transform representing an x-scaled transform
///
/// # Parameters
///
/// * `x` - The scale factor along the x-axis
#[inline(always)]
#[must_use]
pub const fn from_x_scale(x: f32) -> Self {
Self::from_nonuniform_scale(x, 1.0, 1.0)
}
/// Creates a new transform representing a y-scaled transform
///
/// # Parameters
///
/// * `y` - The scale factor along the y-axis
#[inline(always)]
#[must_use]
pub const fn from_y_scale(y: f32) -> Self {
Self::from_nonuniform_scale(1.0, y, 1.0)
}
/// Creates a new transform representing a z-scaled transform
///
/// # Parameters
///
/// * `z` - The scale factor along the z-axis
#[inline(always)]
#[must_use]
pub const fn from_z_scale(z: f32) -> Self {
Self::from_nonuniform_scale(1.0, 1.0, z)
}
}
impl Default for Transform {
fn default() -> Self {
Self::IDENTITY
}
}
// Properties
impl Transform {
/// Returns the position of the transform
pub fn translation(&self) -> &Vec3 {
&self.translation
}
/// Returns the rotation of the transform
pub fn rotation(&self) -> &Quaternion {
&self.rotation
}
/// Returns the scale direction of the transform
pub fn scale_direction(&self) -> &Vec3 {
&self.scale
}
}
// Translation
impl Transform {
/// Sets the translation of the transform
///
/// # Parameters
///
/// * `translation` - The translation of the transform
pub fn set_translation(&mut self, translation: Vector3) {
self.translation = translation;
self.dirty = true;
}
/// Sets the translation of the transform
///
/// # Parameters
///
/// * `translation` - The translation of the transform
pub fn set_translation_vec3(&mut self, translation: &Vec3) {
let vec3 = Vector3::new(translation.x(), translation.y(), translation.z());
self.set_translation(vec3);
}
/// Sets the translation of the transform in the x direction
///
/// # Parameters
///
/// * `x` - The translation along the x-axis
pub fn translate_x(&mut self, x: f32) {
self.translation.x += x;
self.dirty = true;
}
/// Sets the translation of the transform in the y direction
///
/// # Parameters
///
/// * `y` - The translation along the y-axis
pub fn translate_y(&mut self, y: f32) {
self.translation.y += y;
self.dirty = true;
}
/// Sets the translation of the transform in the z direction
///
/// # Parameters
///
/// * `z` - The translation along the z-axis
pub fn translate_z(&mut self, z: f32) {
self.translation.z += z;
self.dirty = true;
}
/// Translates the transform by the given vector
///
/// # Parameters
///
/// * `translation` - The translation to apply
pub fn translate(&mut self, translation: &Vec3) {
self.translation += translation;
self.dirty = true;
}
}
impl Transform {
/// Sets the rotation of the transform
pub fn set_rotation(&mut self, rotation: Quaternion) {
self.rotation = rotation;
self.dirty = true;
}
/// Rotates the transform in the yaw direction by the given angle.
///
/// # Parameters
///
/// * `angle` - The angle to rotate the transform by.
pub fn rotate_yaw<A: Angle>(&mut self, angle: A) {
self.rotate(&Quaternion::from_yaw(angle))
}
/// Rotates the transform in the pitch direction by the given angle.
///
/// # Parameters
///
/// * `angle` - The angle to rotate the transform by.
#[inline(always)]
pub fn rotate_pitch<A: Angle>(&mut self, angle: A) {
self.rotate(&Quaternion::from_pitch(angle))
}
/// Rotates the transform in the roll direction by the given angle.
///
/// # Parameters
///
/// * `angle` - The angle to rotate the transform by.
#[inline(always)]
pub fn rotate_roll<A: Angle>(&mut self, angle: A) {
self.rotate(&Quaternion::from_roll(angle))
}
/// Rotates the transform by the given euler angles.
///
/// # Parameters
///
/// * `yaw` - The yaw angle to rotate the transform by.
/// * `pitch` - The pitch angle to rotate the transform by.
/// * `roll` - The roll angle to rotate the transform by.
pub fn rotate_euler_angles<A: Angle>(&mut self, yaw: A, pitch: A, roll: A) {
self.rotate(&Quaternion::from_euler_angles(yaw, pitch, roll))
}
/// Rotates the transform by the given quaternion.
///
/// # Parameters
///
/// * `rotation` - The rotation to apply.
pub fn rotate(&mut self, rotation: &Quaternion) {
self.rotation *= rotation;
self.dirty = true;
}
}
impl Transform {
/// Sets the scale of the transform
///
/// # Parameters
///
/// * `scale` - The scale to apply to the transform
pub fn set_scale(&mut self, scale: Vector3) {
self.scale = scale;
self.dirty = true;
}
/// Scales the transformation by the vector `scale`
///
/// # Parameters
///
/// * `scale` - The scale to apply to the transform
pub fn scale(&mut self, scale: &Vec3) {
self.scale.x *= scale.x();
self.scale.y *= scale.y();
self.scale.z *= scale.z();
self.dirty = true;
}
/// Scales the width of the transform
///
/// # Parameters
///
/// * `width` - The width to scale the transform by
pub fn scale_width(&mut self, width: f32) {
self.scale.x *= width;
self.dirty = true;
}
/// Scales the height of the transform
///
/// # Parameters
///
/// * `height` - The height to scale the transform by
pub fn scale_height(&mut self, height: f32) {
self.scale.y *= height;
self.dirty = true;
}
/// Scales the depth of the transform
///
/// # Parameters
///
/// * `depth` - The depth to scale the transform by
pub fn scale_depth(&mut self, depth: f32) {
self.scale.z *= depth;
self.dirty = true;
}
/// Scales the transform uniformly by the given scale factor
///
/// # Parameters
///
/// * `scale` - The scale factor to apply to the transform
pub fn scale_uniform(&mut self, scale: f32) {
self.scale *= scale;
self.dirty = true;
}
/// Scales the transform non-uniformly by the given scale factors
///
/// # Parameters
///
/// * `x` - The scale factor along the x-axis
/// * `y` - The scale factor along the y-axis
/// * `z` - The scale factor along the z-axis
pub fn scale_nonuniform(&mut self, x: f32, y: f32, z: f32) {
self.scale.x *= x;
self.scale.y *= y;
self.scale.z *= z;
self.dirty = true;
}
}
// Operations
impl Transform {
/// Computes and reutrns the transformation matrix of the transform
///
/// This function will only recompute the transformation matrix if the
/// transform has been modified since the last time the transformation matrix
/// was computed.
pub fn transform(&mut self) -> &Mat4 {
if hint::unlikely(self.dirty) {
let translation = self.translation_matrix();
let rotation = self.rotation_matrix();
let scale = self.scale_matrix();
self.transform = &translation * &rotation * &scale;
self.dirty = false;
}
&self.transform
}
}
// Implementation
impl Transform {
const fn scale_matrix(&self) -> Matrix4 {
Matrix4::from_arrays([
[self.scale.x, 0.0, 0.0, 0.0],
[0.0, self.scale.y, 0.0, 0.0],
[0.0, 0.0, self.scale.z, 0.0],
[0.0, 0.0, 0.0, 1.0],
])
}
const fn translation_matrix(&self) -> Matrix4 {
Matrix4::from_arrays([
[1.0, 0.0, 0.0, self.translation.x],
[0.0, 1.0, 0.0, self.translation.y],
[0.0, 0.0, 1.0, self.translation.z],
[0.0, 0.0, 0.0, 1.0],
])
}
fn rotation_matrix(&self) -> Matrix4 {
self.rotation.to_matrix4()
}
}
#[cfg(test)]
#[path = "transform.test.rs"]
mod test;