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
use crate::vector::*;
use crate::vector3::Vector3;
use serde::{Deserialize, Serialize};
use std::ops;

#[derive(Copy, Clone, PartialEq, Serialize, Deserialize, Debug)]
pub struct Quaternion {
    pub x: f32,
    pub y: f32,
    pub z: f32,
    pub w: f32,
}

impl Quaternion {
    pub fn get_vector_part(&self) -> Vector3 {
        Vector3 {
            x: self.x,
            y: self.y,
            z: self.z,
        }
    }
    pub fn get_scalar_part(&self) -> f32 {
        self.w
    }
    pub fn set_identity(&mut self) {
        let one = 1.0f32;
        let zero = 0.0f32;

        self.x = zero;
        self.y = zero;
        self.z = zero;
        self.w = one;
    }
}

pub fn make_identity() -> Quaternion {
    let one = 1.0f32;
    let zero = 0.0f32;

    Quaternion {
        x: zero,
        y: zero,
        z: zero,
        w: one,
    }
}

impl Scale for Quaternion {
    fn scale(&self, t: f32) -> Self {
        Quaternion {
            x: self.x * t,
            y: self.y * t,
            z: self.z * t,
            w: self.w * t,
        }
    }
}

pub fn dot(a: Quaternion, b: Quaternion) -> f32 {
    a.get_scalar_part() * b.get_scalar_part()
        + a.get_vector_part().inner_product(&b.get_vector_part())
}

pub fn inverse(q: Quaternion) -> Quaternion {
    q.scale(q.get_length().recip())
}

pub fn conjugate(a: Quaternion) -> Quaternion {
    let vector_neg = a.get_vector_part().negate();
    Quaternion {
        x: vector_neg.x,
        y: vector_neg.y,
        z: vector_neg.z,
        w: a.w,
    }
}

impl Quaternion {
    fn get_squared_length(&self) -> f32 {
        self.w * self.w + self.get_vector_part().get_squared_length()
    }
    fn get_length(&self) -> f32 {
        self.get_squared_length().sqrt()
    }
}

pub fn rotate(q: Quaternion, angle_in_radius: f32) -> Quaternion {
    let (axis, angle) = get_axis_angle(q);
    from_axis_angle(axis, angle + angle_in_radius)
}

pub fn from_euler_angles(x: f32, y: f32, z: f32) -> Quaternion {
    let half_x = x / 2.0f32;
    let half_y = y / 2.0f32;
    let half_z = z / 2.0f32;

    let cos_x_2 = half_x.cos();
    let cos_y_2 = half_y.cos();
    let cos_z_2 = half_z.cos();

    let sin_x_2 = half_x.sin();
    let sin_y_2 = half_y.sin();
    let sin_z_2 = half_z.sin();

    // Z -> Y -> X
    Quaternion {
        x: sin_x_2 * cos_y_2 * cos_z_2 - cos_x_2 * sin_y_2 * sin_z_2,
        y: cos_x_2 * sin_y_2 * cos_z_2 + sin_x_2 * cos_y_2 * sin_z_2,
        z: cos_x_2 * cos_y_2 * sin_z_2 - sin_x_2 * sin_y_2 * cos_z_2,
        w: cos_x_2 * cos_y_2 * cos_z_2 + sin_x_2 * sin_y_2 * sin_z_2,
    }
}

pub fn from_axis_angle(axis: Vector3, angle: f32) -> Quaternion {
    let half_angle = angle / 2.0f32;
    let vector_part = axis.scale(half_angle.sin());

    Quaternion {
        x: vector_part.x,
        y: vector_part.y,
        z: vector_part.z,
        w: half_angle.cos(),
    }
}

pub fn get_axis_angle(q: Quaternion) -> (Vector3, f32) {
    let axis = q.get_vector_part().normalize();
    let angle = 2.0 * q.get_scalar_part().acos();

    if angle > ::std::f32::consts::PI {
        (axis.negate(), 2.0 * ::std::f32::consts::PI - angle)
    } else {
        (axis, angle)
    }
}

impl ops::Add<Quaternion> for Quaternion {
    type Output = Quaternion;

    fn add(self, _rhs: Quaternion) -> Self {
        Quaternion {
            x: self.x + _rhs.x,
            y: self.y + _rhs.y,
            z: self.z + _rhs.z,
            w: self.w + _rhs.w,
        }
    }
}

impl ops::Mul<Quaternion> for Quaternion {
    type Output = Quaternion;

    fn mul(self, _rhs: Quaternion) -> Self {
        let vector_a = self.get_vector_part();
        let vector_b = _rhs.get_vector_part();
        let inner_product_result = vector_a.inner_product(&vector_b);
        let vector_part =
            vector_b.scale(self.w) + vector_a.scale(_rhs.w) + vector_a.outer_product(&vector_b);

        Quaternion {
            x: vector_part.x,
            y: vector_part.y,
            z: vector_part.z,
            w: self.w * _rhs.w - inner_product_result,
        }
    }
}