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
// Oliver Berzs
// https://github.com/oberzs/duku

use super::Mat4;
use super::Vec3;

use std::ops::Mul;
use std::ops::MulAssign;

/// Compact 3D rotation representation.
///
/// Used for rotating vectors
///
/// # Examples
///
/// ```
/// # use duku::Vec3;
/// # use duku::Quat;
/// let vector = Vec3::up();
/// let quat = Quat::euler_rotation(0.0, 0.0, 90.0);
/// let rotated = quat * vector;
/// ```
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Quat {
    /// the X component
    pub x: f32,
    /// the Y component
    pub y: f32,
    /// the Z component
    pub z: f32,
    /// the W component
    pub w: f32,
}

impl Quat {
    /// Create quaternion
    pub const fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
        Self { x, y, z, w }
    }

    /// Create quaternion with euler angles
    ///
    /// This rotation's yaw, pitch and roll are z, y and x
    pub fn euler_rotation(x: f32, y: f32, z: f32) -> Self {
        let m = Mat4::euler_rotation(x, y, z);
        Self::from(m)
    }

    /// Create quaternion around axis
    ///
    /// This rotates vectors around axis by the angle
    pub fn axis_rotation(axis: impl Into<Vec3>, angle: f32) -> Self {
        let m = Mat4::axis_rotation(axis, angle);
        Self::from(m)
    }

    /// Create quaternion to rotate towards direction
    ///
    /// `global_up` is used as a guide to try aligning to
    pub fn look_rotation(dir: impl Into<Vec3>, global_up: impl Into<Vec3>) -> Self {
        let m = Mat4::look_rotation(dir, global_up);
        Self::from(m)
    }

    /// local up direction for transformation
    pub fn local_up(self) -> Vec3 {
        self.inverse() * Vec3::up()
    }

    /// local forward direction for transformation
    pub fn local_forward(self) -> Vec3 {
        self.inverse() * Vec3::forward()
    }

    /// local right direction for transformation
    pub fn local_right(self) -> Vec3 {
        self.inverse() * Vec3::right()
    }

    /// Calculate the inverse rotation
    pub fn inverse(self) -> Quat {
        let mut result = self;
        result.w = -result.w;
        result
    }
}

impl Default for Quat {
    fn default() -> Self {
        Self::new(0.0, 0.0, 0.0, 1.0)
    }
}

impl Mul<Vec3> for Quat {
    type Output = Vec3;

    fn mul(self, rhs: Vec3) -> Vec3 {
        let u = Vec3::new(self.x, self.y, self.z);
        let s = self.w;

        u * 2.0 * u.dot(rhs) + rhs * (s * s - u.dot(u)) + u.cross(rhs) * 2.0 * s
    }
}

impl Mul<Self> for Quat {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self {
        let w = self.w * rhs.w - self.x * rhs.x - self.y * rhs.y - self.z * rhs.z;
        let x = self.w * rhs.x + self.x * rhs.w + self.y * rhs.z - self.z * rhs.y;
        let y = self.w * rhs.y - self.x * rhs.z + self.y * rhs.w + self.z * rhs.x;
        let z = self.w * rhs.z + self.x * rhs.y - self.y * rhs.x + self.z * rhs.w;

        Self::new(x, y, z, w)
    }
}

impl MulAssign<Self> for Quat {
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}

impl From<[f32; 4]> for Quat {
    fn from(a: [f32; 4]) -> Self {
        Self::new(a[0], a[1], a[2], a[3])
    }
}

impl From<Mat4> for Quat {
    fn from(m: Mat4) -> Self {
        let mut q = Self::default();
        let trace = m.x.x + m.y.y + m.z.z;
        if trace > 0.0 {
            let s = 0.5 / (trace + 1.0).sqrt();
            q.w = 0.25 / s;
            q.x = (m.y.z - m.z.y) * s;
            q.y = (m.z.x - m.x.z) * s;
            q.z = (m.x.y - m.y.x) * s;
        } else if m.x.x > m.y.y && m.x.x > m.z.z {
            let s = 2.0 * (1.0 + m.x.x - m.y.y - m.z.z).sqrt();
            q.w = (m.y.z - m.z.y) / s;
            q.x = 0.25 * s;
            q.y = (m.y.x + m.x.y) / s;
            q.z = (m.z.x + m.x.z) / s;
        } else if m.y.y > m.z.z {
            let s = 2.0 * (1.0 + m.y.y - m.x.x - m.z.z).sqrt();
            q.w = (m.z.x - m.x.z) / s;
            q.x = (m.y.x + m.x.y) / s;
            q.y = 0.25 * s;
            q.z = (m.z.y + m.y.z) / s;
        } else {
            let s = 2.0 * (1.0 + m.z.z - m.x.x - m.y.y).sqrt();
            q.w = (m.x.y - m.y.x) / s;
            q.x = (m.z.x + m.x.z) / s;
            q.y = (m.z.y + m.y.z) / s;
            q.z = 0.25 * s;
        }
        q
    }
}

#[cfg(test)]
mod test {
    use super::Mat4;
    use super::Quat;
    use super::Vec3;

    #[test]
    fn default() {
        assert_eq!(Quat::default(), Quat::new(0.0, 0.0, 0.0, 1.0));
    }

    #[test]
    fn axis_rotation() {
        let q = Quat::axis_rotation([1.0, 0.0, 0.0], 180.0);
        let v = Vec3::new(1.0, 1.0, 1.0);
        let r = q * v;
        assert_eq_delta!(r.x, 1.0);
        assert_eq_delta!(r.y, -1.0);
        assert_eq_delta!(r.z, -1.0);
    }

    #[test]
    fn look_rotation_x() {
        let q = Quat::look_rotation([1.0, 0.0, 0.0], Vec3::up());
        let r = q * Vec3::forward();
        assert_eq_delta!(r.x, -1.0);
        assert_eq_delta!(r.y, 0.0);
        assert_eq_delta!(r.z, 0.0);
    }

    #[test]
    fn look_rotation_y() {
        let q = Quat::look_rotation([0.0, 1.0, 0.0], Vec3::forward());
        let r = q * Vec3::forward();
        assert_eq_delta!(r.x, 0.0);
        assert_eq_delta!(r.y, 1.0);
        assert_eq_delta!(r.z, 0.0);
    }

    #[test]
    fn look_rotation_z() {
        let q = Quat::look_rotation([0.0, 0.0, -1.0], Vec3::up());
        let r = q * Vec3::forward();
        assert_eq_delta!(r.x, 0.0);
        assert_eq_delta!(r.y, 0.0);
        assert_eq_delta!(r.z, -1.0);
    }

    #[test]
    fn euler_rotation_x() {
        let q = Quat::euler_rotation(90.0, 0.0, 0.0);
        let v = Vec3::new(0.0, 0.0, 1.0);
        let r = q * v;
        assert_eq_delta!(r.x, 0.0);
        assert_eq_delta!(r.y, -1.0);
        assert_eq_delta!(r.z, 0.0);
    }

    #[test]
    fn euler_rotation_y() {
        let q = Quat::euler_rotation(0.0, 90.0, 0.0);
        let v = Vec3::new(0.0, 0.0, 1.0);
        let r = q * v;
        assert_eq_delta!(r.x, 1.0);
        assert_eq_delta!(r.y, 0.0);
        assert_eq_delta!(r.z, 0.0);
    }

    #[test]
    fn euler_rotation_z() {
        let q = Quat::euler_rotation(0.0, 0.0, 90.0);
        let v = Vec3::new(1.0, 0.0, 0.0);
        let r = q * v;
        assert_eq_delta!(r.x, 0.0);
        assert_eq_delta!(r.y, 1.0);
        assert_eq_delta!(r.z, 0.0);
    }

    #[test]
    fn quat_to_mat_to_quat() {
        let q1 = Quat::euler_rotation(0.0, 45.0, 45.0);
        let q2 = Quat::from(Mat4::from(q1));

        assert_eq_delta!(q1.x, q2.x);
        assert_eq_delta!(q1.y, q2.y);
        assert_eq_delta!(q1.z, q2.z);
        assert_eq_delta!(q1.w, q2.w);
    }

    #[test]
    fn mul_vector() {
        let q = Quat::euler_rotation(0.0, 0.0, 90.0);
        let v = Vec3::right();
        let r = q * v;

        assert_eq_delta!(r.x, 0.0);
        assert_eq_delta!(r.y, 1.0);
        assert_eq_delta!(r.z, 0.0);
    }
    #[test]
    fn mul_self() {
        let q1 = Quat::euler_rotation(0.0, 0.0, 180.0);
        let q2 = Quat::euler_rotation(0.0, 0.0, 90.0) * Quat::euler_rotation(0.0, 0.0, 90.0);

        assert_eq_delta!(q1.x, q2.x);
        assert_eq_delta!(q1.y, q2.y);
        assert_eq_delta!(q1.z, q2.z);
        assert_eq_delta!(q1.w, q2.w);
    }
}