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
use cgmath;
use std::{fmt, mem, ops};
use approx::ApproxEq;
use cgmath::{InnerSpace, Rotation3};
use {DVec3, Vec3};
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C)]
pub struct Quat {
pub x: f32,
pub y: f32,
pub z: f32,
pub s: f32,
}
impl fmt::Display for Quat {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({}, {}, {}; {})", self.x, self.y, self.z, self.s)
}
}
impl Quat {
pub fn new(x: f32, y: f32, z: f32, s: f32) -> Self {
Quat { x, y, z, s }
}
pub fn identity() -> Self {
Quat::new(0.0, 0.0, 0.0, 1.0)
}
pub fn euler(angles: Vec3) -> Self {
let roll = quat!(0.0, 0.0, 1.0; angles.z);
let pitch = quat!(1.0, 0.0, 0.0; angles.x);
let yaw = quat!(0.0, 1.0, 0.0; angles.y);
roll * pitch * yaw
}
pub fn axis_angle(axis: Vec3, angle: f32) -> Self {
let q = cgmath::Quaternion::from_axis_angle(
cgmath::Vector3::new(axis.x, axis.y, axis.z).normalize(),
cgmath::Rad(angle),
);
Quat::new(q.v.x, q.v.y, q.v.z, q.s)
}
pub fn rotate(&self, vector: Vec3) -> Vec3 {
use cgmath::Rotation;
let rotation = cgmath::Quaternion::new(self.s, self.x, self.y, self.z);
let point = cgmath::Point3::new(vector.x, vector.y, vector.z);
let result = rotation.rotate_point(point);
vec3!(result.x, result.y, result.z)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C)]
pub struct DQuat {
pub x: f64,
pub y: f64,
pub z: f64,
pub s: f64,
}
impl fmt::Display for DQuat {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({}, {}, {}; {})", self.x, self.y, self.z, self.s)
}
}
impl DQuat {
pub fn new(x: f64, y: f64, z: f64, s: f64) -> Self {
DQuat { x, y, z, s }
}
pub fn identity() -> Self {
DQuat::new(0.0, 0.0, 0.0, 1.0)
}
pub fn euler(angles: DVec3) -> Self {
let roll = dquat!(0.0, 0.0, 1.0; angles.z);
let pitch = dquat!(1.0, 0.0, 0.0; angles.x);
let yaw = dquat!(0.0, 1.0, 0.0; angles.y);
roll * pitch * yaw
}
pub fn axis_angle(axis: DVec3, angle: f64) -> Self {
let q = cgmath::Quaternion::from_axis_angle(
cgmath::Vector3::new(axis.x, axis.y, axis.z).normalize(),
cgmath::Rad(angle),
);
DQuat::new(q.v.x, q.v.y, q.v.z, q.s)
}
pub fn rotate(&self, vector: DVec3) -> DVec3 {
use cgmath::Rotation;
let rotation = cgmath::Quaternion::new(self.s, self.x, self.y, self.z);
let point = cgmath::Point3::new(vector.x, vector.y, vector.z);
let result = rotation.rotate_point(point);
dvec3!(result.x, result.y, result.z)
}
}
macro_rules! impl_quaternion {
($self:ty, $base:ty, $inner:ty, $array:ty) => {
impl ops::Mul<$self> for $self {
type Output = $self;
fn mul(self, rhs: $self) -> $self {
let a: &$inner = self.as_ref().into();
let b: &$inner = rhs.as_ref().into();
let q = a * b;
Self::new(q.v.x, q.v.y, q.v.z, q.s)
}
}
impl ops::MulAssign<$self> for $self {
fn mul_assign(&mut self, rhs: $self) {
*self = *self * rhs;
}
}
impl Default for $self {
fn default() -> Self {
Self::identity()
}
}
impl AsRef<$array> for $self {
fn as_ref(&self) -> &$array {
unsafe {
mem::transmute(self)
}
}
}
impl From<$array> for $self {
fn from(q: $array) -> Self {
Self::new(q[0], q[1], q[2], q[3])
}
}
impl Into<$array> for $self {
fn into(self) -> $array {
[self.x, self.y, self.z, self.s]
}
}
impl ApproxEq for $self {
type Epsilon = <$inner as ApproxEq>::Epsilon;
fn default_epsilon() -> Self::Epsilon {
<$inner as ApproxEq>::default_epsilon()
}
fn default_max_relative() -> Self::Epsilon {
<$inner as ApproxEq>::default_max_relative()
}
fn default_max_ulps() -> u32 {
<$inner as ApproxEq>::default_max_ulps()
}
fn relative_eq(
&self,
other: &Self,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool {
let a: &$inner = self.as_ref().into();
let b: &$inner = other.as_ref().into();
a.relative_eq(&b, epsilon, max_relative)
}
fn ulps_eq(
&self,
other: &Self,
epsilon: Self::Epsilon,
max_ulps: u32,
) -> bool {
let a: &$inner = self.as_ref().into();
let b: &$inner = other.as_ref().into();
a.ulps_eq(b, epsilon, max_ulps)
}
}
};
}
impl_quaternion!(DQuat, f64, cgmath::Quaternion<f64>, [f64; 4]);
impl_quaternion!(Quat, f32, cgmath::Quaternion<f32>, [f32; 4]);
#[cfg(feature = "mint")]
mod mint_support {
use mint;
use super::*;
impl From<mint::Quaternion<f32>> for Quat {
fn from(m: mint::Quaternion<f32>) -> Self {
Quat::new(m.v.x, m.v.y, m.v.z, m.s)
}
}
impl Into<mint::Quaternion<f32>> for Quat {
fn into(self) -> mint::Quaternion<f32> {
mint::Quaternion {
v: mint::Vector3 { x: self.x, y: self.y, z: self.z },
s: self.s,
}
}
}
impl From<mint::Quaternion<f64>> for DQuat {
fn from(m: mint::Quaternion<f64>) -> Self {
DQuat::new(m.v.x, m.v.y, m.v.z, m.s)
}
}
impl Into<mint::Quaternion<f64>> for DQuat {
fn into(self) -> mint::Quaternion<f64> {
mint::Quaternion {
v: mint::Vector3 { x: self.x, y: self.y, z: self.z },
s: self.s,
}
}
}
}