/
quaternion.ts
228 lines (201 loc) · 5.53 KB
/
quaternion.ts
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
import { Float32Vector3 } from './float32vector';
import { Matrix4x4 } from './matrix';
/**
* Quaternion which is 4-dimensional complex number.
* See [Wikipedia](https://en.wikipedia.org/wiki/Quaternion).
*/
export class Quaternion {
protected _values: Float32Array;
constructor(x: number, y: number, z: number, w: number) {
this._values = new Float32Array([x, y, z, w]);
}
/**
* Create a rotation quaternion around `normalizedAxis`.
* `normalizedAxis` must be normalized.
* @param {Float32Vector3} normalizedAxis
* @param {number} radian
* @returns {Quaternion}
*/
static rotationAround(normalizedAxis: Float32Vector3, radian: number): Quaternion {
const sin = Math.sin(radian / 2.0);
const cos = Math.cos(radian / 2.0);
return new Quaternion(normalizedAxis.x * sin, normalizedAxis.y * sin, normalizedAxis.z * sin, cos);
}
/**
* Returns a normalized quaternion.
* @returns {Quaternion}
*/
normalize() : Quaternion {
const mag = this.magnitude;
if(mag === 0) { return this; }
const r = 1 / mag;
return new Quaternion(this.x * r, this.y * r, this.z * r, this.w * r);
}
/**
* Adds the `other` to the quaternion and returns the sum.
*
* This method does not mutate the quaternion.
* @param {Quaternion} other
* @returns {Quaternion}
*/
add(other: Quaternion): Quaternion {
return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
}
/**
* Multiplies the quaternion by `scalar` and returns the product.
*
* This method does not mutate the quaternion.
* @param {number} scalar
* @returns {Quaternion}
*/
mulByScalar(scalar: number): Quaternion {
return new Quaternion(this.x * scalar, this.y * scalar, this.z * scalar, this.w * scalar);
}
/**
* Calculates dot product.
* @param {Quaternion} other
* @returns {number}
*/
dot(other: Quaternion): number {
return this.x * other.x + this.y * other.y + this.z * other.z + this.w * other.w;
}
/**
* Calculates spherical linear interpolation(also known as Slerp) and returns new `Quaternion` between the quaternion and the other.
* @param {Quaternion} other
* @param {number} t 0.0 <= t <= 1.0
* @param {{chooseShorterAngle: boolean}} options Does not work currently. slerp chooses shorter angle regardless of this value.
* @returns {Quaternion}
*/
slerp(other: Quaternion , t: number, options: { chooseShorterAngle: boolean } = { chooseShorterAngle: true }): Quaternion {
let dotProd: number = this.dot(other);
let otherQuaternion: Quaternion = other;
// When the dot product is negative, slerp chooses the longer way.
// So we should negate the `other` quaternion.
if(dotProd < 0) {
dotProd = -dotProd;
otherQuaternion = other.mulByScalar(-1);
}
const omega: number = Math.acos(dotProd);
const sinOmega: number = Math.sin(omega);
const q1: Quaternion = this.mulByScalar(Math.sin((1 - t) * omega) / sinOmega);
const q2: Quaternion = otherQuaternion.mulByScalar(Math.sin(t * omega) / sinOmega);
return q1.add(q2);
}
/**
* Calc magnitude of the quaternion.
* @returns {number}
*/
get magnitude(): number {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
}
/**
* Calc norm of the quaternion.
* An alias for `magnitude`.
* @returns {number}
*/
get norm(): number {
return this.magnitude;
}
/**
* Returns x value of the vector.
* @returns {number}
*/
get x(): number {
return this._values[0];
}
/**
* Returns y value of the vector.
* @returns {number}
*/
get y(): number {
return this._values[1];
}
/**
* Returns z value of the vector.
* @returns {number}
*/
get z(): number {
return this._values[2];
}
/**
* Returns w value of the vector.
* @returns {number}
*/
get w(): number {
return this._values[3];
}
/**
* Set the `value` as new x.
* @param {number} value
*/
set x(value: number) {
this._values[0] = value;
}
/**
* Set the `value` as new y.
* @param {number} value
*/
set y(value: number) {
this._values[1] = value;
}
/**
* Set the `value` as new z.
* @param {number} value
*/
set z(value: number) {
this._values[2] = value;
}
/**
* Set the `value` as new w.
* @param {number} value
*/
set w(value: number) {
this._values[3] = value;
}
/**
* Returns values of the quaternion.
* @returns {Float32Array}
*/
get values(): Float32Array {
return this._values;
}
/**
* Convert the quaternion to a rotation matrix.
* @returns {Matrix4x4}
*/
toRotationMatrix4(): Matrix4x4 {
const x = this.x;
const y = this.y;
const z = this.z;
const w = this.w;
const m11 = 1 - 2 * y * y - 2 * z * z;
const m12 = 2 * x * y - 2 * w * z;
const m13 = 2 * x * z + 2 * w * y;
const m14 = 0;
const m21 = 2 * x * y + 2 * w * z;
const m22 = 1 - 2 * x * x - 2 * z * z;
const m23 = 2 * y * z - 2 * w * x;
const m24 = 0;
const m31 = 2 * x * z - 2 * w * y;
const m32 = 2 * y * z + 2 * w * x;
const m33 = 1 - 2 * x * x - 2 * y * y;
const m34 = 0;
const m41 = 0;
const m42 = 0;
const m43 = 0;
const m44 = 1;
return new Matrix4x4(
m11, m21, m31, m41,
m12, m22, m32, m42,
m13, m23, m33, m43,
m14, m24, m34, m44
);
}
/**
* Returns values as `String`.
* @returns {string}
*/
toString(): string {
return `Quaternion(${this.x}, ${this.y}, ${this.z}, ${this.w})`;
}
}