alloy/math/dual_quaternion.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
use std::ops;
use crate::math::vec::Vec3;
use crate::math::{Angle, Quaternion};
use super::mat::Matrix4;
use super::vec::Vector3;
/// A dual-quaternion represents both translations and rotations in a combined
/// format.
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct DualQuaternion {
real: Quaternion,
dual: Quaternion,
}
// Construction
impl DualQuaternion {
/// Creates a new dual quaternion from a real and dual quaternion.
///
/// # Parameters
///
/// * `real` - The real part of the dual quaternion.
/// * `dual` - The dual part of the dual quaternion.
#[inline(always)]
#[must_use]
pub const fn new(real: Quaternion, dual: Quaternion) -> Self {
Self { real, dual }
}
/// Constructs a new dual quaternion with the identity rotation and no translation.
#[inline(always)]
#[must_use]
pub const fn identity() -> Self {
Self::new(Quaternion::identity(), Quaternion::ZERO)
}
/// Constructs this dual quaternion from a rotation quaternion.
///
/// # Parameters
///
/// * `rotation` - The rotation quaternion to construct the dual quaternion from.
#[must_use]
pub const fn from_rotation(rotation: Quaternion) -> Self {
Self::new(rotation, Quaternion::ZERO)
}
/// Constructs this dual quaternion from the yaw angle
///
/// # Parameters
///
/// * `angle` - The yaw angle.
#[inline(always)]
#[must_use]
pub fn from_yaw<A: Angle>(angle: A) -> Self {
Self::from_rotation(Quaternion::from_yaw(angle))
}
/// Constructs this dual quaternion from the pitch angle
///
/// # Parameters
///
/// * `angle` - The pitch angle.
#[inline(always)]
#[must_use]
pub fn from_pitch<A: Angle>(angle: A) -> Self {
Self::from_rotation(Quaternion::from_pitch(angle))
}
/// Constructs this dual quaternion from the roll angle
///
/// # Parameters
///
/// * `angle` - The roll angle.
#[inline(always)]
#[must_use]
pub fn from_roll<A: Angle>(angle: A) -> Self {
Self::from_rotation(Quaternion::from_roll(angle))
}
/// Constructs this dual quaternion from a translation vector.
///
/// # Parameters
///
/// * `translation` - The translation vector to construct the dual quaternion from.
pub fn from_translation(translation: &Vec3) -> Self {
Self::new(
Quaternion::identity(),
Quaternion::new(
0.0,
0.5 * translation.x(),
0.5 * translation.y(),
0.5 * translation.z(),
),
)
}
}
// Conversions
impl DualQuaternion {
/// Returns the transform matrix representation of the quaternion.
#[must_use]
pub fn to_matrix4(&self) -> Matrix4 {
let translation = self.translation();
let mut matrix4 = self.real.to_matrix4();
matrix4[0][3] = translation.x();
matrix4[1][3] = translation.y();
matrix4[2][3] = translation.z();
matrix4
}
}
// Properties
impl DualQuaternion {
/// Returns the real part of the dual quaternion.
#[must_use]
#[inline(always)]
pub const fn real(&self) -> &Quaternion {
&self.real
}
/// Returns the dual part of the dual quaternion.
#[must_use]
#[inline(always)]
pub const fn dual(&self) -> &Quaternion {
&self.dual
}
/// Returns the translation component in local-space.
#[must_use]
pub fn translation(&self) -> Vector3 {
let dual = Quaternion::new(
self.dual.w() * 2.0,
self.dual.i() * 2.0,
self.dual.j() * 2.0,
self.dual.k() * 2.0,
);
let translation_quat = dual * self.real.conjugate();
Vector3::new(
translation_quat.i(),
translation_quat.j(),
translation_quat.k(),
)
}
/// Returns the translation of the dual quaternion in world-space.
#[must_use]
pub fn world_translation(&self) -> Vector3 {
Vector3::new(
self.dual.i() * 2.0,
self.dual.j() * 2.0,
self.dual.k() * 2.0,
)
}
/// Returns the rotation component of the dual quaternion.
#[must_use]
#[inline(always)]
pub const fn rotation(&self) -> &Quaternion {
&self.real
}
/// Returns a normalized DualQuaternion.
#[must_use]
#[inline(always)]
pub fn normalized(&self) -> DualQuaternion {
let norm = self.real.norm();
Self {
real: &self.real / norm,
dual: &self.dual / norm,
}
}
/// Returns the conjugate of the dual quaternion.
#[must_use]
pub fn conjugate(&self) -> DualQuaternion {
Self {
real: self.real.conjugate(),
dual: self.dual.conjugate(),
}
}
}
// Modifiers
impl DualQuaternion {
/// Normalizes the dual quaternion.
pub fn normalize(&mut self) {
let norm = self.real.norm();
self.real /= norm;
self.dual /= norm;
}
/// Translates the dual quaternion by the given vector in world space.
///
/// # Parameters
///
/// * `translation` - The translation vector to apply to the dual quaternion.
pub fn translate_world(&mut self, translation: &Vec3) {
self.dual += &Quaternion::new(
0.0,
0.5 * translation.x(),
0.5 * translation.y(),
0.5 * translation.z(),
);
}
/// Translates the dual quaternion by the given vector in local space.
///
/// This will translate relative to the orientation of the dual quaternion.
///
/// # Parameters
///
/// * `translation` - The translation vector to apply to the dual quaternion.
pub fn translate(&mut self, translation: &Vec3) {
self.dual += &(Quaternion::new(
0.0,
0.5 * translation.x(),
0.5 * translation.y(),
0.5 * translation.z(),
) * self.real);
}
/// Rotates the dual quaternion by the given angle.
pub fn rotate(&mut self, rotation: &Quaternion) {
self.real = rotation * self.real;
self.dual = rotation * self.dual;
}
/// Rotates the dual quaternion by the given angle around the x-axis.
#[inline(always)]
pub fn rotate_yaw<A: Angle>(&mut self, angle: A) {
self.rotate(&Quaternion::from_yaw(angle));
}
/// Rotates the dual quaternion by the given angle around the y-axis.
#[inline(always)]
pub fn rotate_pitch<A: Angle>(&mut self, angle: A) {
self.rotate(&Quaternion::from_pitch(angle));
}
/// Rotates the dual quaternion by the given angle around the z-axis.
#[inline(always)]
pub fn rotate_roll<A: Angle>(&mut self, angle: A) {
self.rotate(&Quaternion::from_roll(angle));
}
}
impl ops::Mul for DualQuaternion {
type Output = Self;
fn mul(self, rhs: Self) -> Self {
Self {
real: self.real * rhs.real,
dual: (self.real * rhs.dual) + (self.dual * rhs.real),
}
}
}
impl ops::Add for DualQuaternion {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Self {
real: self.real + rhs.real,
dual: self.dual + rhs.dual,
}
}
}
#[cfg(test)]
#[path = "dual_quaternion.test.rs"]
mod test;