-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathQuat.ts
234 lines (206 loc) · 7.43 KB
/
Quat.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
229
230
231
232
233
234
import { Vec3 } from './Vec3'
import { Mat4 } from './Mat4'
/** Defines all possible rotations axis orders */
export type AxisOrder = 'XYZ' | 'XZY' | 'YXZ' | 'YZX' | 'ZXY' | 'ZYX'
/**
* Really basic quaternion class used for 3D rotation calculations
* @see https://github.com/mrdoosb/three.js/blob/dev/src/math/Quaternion.js
*/
export class Quat {
/** The type of the {@link Quat} */
type: string
/** Our quaternion array */
elements: Float32Array
/** Rotation axis order */
axisOrder: AxisOrder
/**
* Quat constructor
* @param [elements] - initial array to use
* @param [axisOrder='XYZ'] - axis order to use
*/
constructor(elements: Float32Array = new Float32Array([0, 0, 0, 1]), axisOrder: AxisOrder = 'XYZ') {
this.type = 'Quat'
this.elements = elements
// rotation axis order
this.axisOrder = axisOrder
}
/**
* Sets the {@link Quat} values from an array
* @param array - an array of at least 4 elements
* @returns - this {@link Quat} after being set
*/
setFromArray(array: Float32Array | number[] = new Float32Array([0, 0, 0, 1])): Quat {
this.elements[0] = array[0]
this.elements[1] = array[1]
this.elements[2] = array[2]
this.elements[3] = array[3]
return this
}
/**
* Sets the {@link Quat} axis order
* @param axisOrder - axis order to use
* @returns - this {@link Quat} after axis order has been set
*/
setAxisOrder(axisOrder: AxisOrder | string = 'XYZ'): Quat {
// force uppercase for strict equality tests
axisOrder = axisOrder.toUpperCase()
switch (axisOrder) {
case 'XYZ':
case 'YXZ':
case 'ZXY':
case 'ZYX':
case 'YZX':
case 'XZY':
this.axisOrder = axisOrder
break
default:
// apply a default axis order
this.axisOrder = 'XYZ'
}
return this
}
/**
* Copy a {@link Quat} into this {@link Quat}
* @param quaternion - {@link Quat} to copy
* @returns - this {@link Quat} after copy
*/
copy(quaternion: Quat = new Quat()): Quat {
this.elements = quaternion.elements
this.axisOrder = quaternion.axisOrder
return this
}
/**
* Clone a {@link Quat}
* @returns - cloned {@link Quat}
*/
clone(): Quat {
return new Quat().copy(this)
}
/**
* Check if 2 {@link Quat} are equal
* @param quaternion - {@link Quat} to check against
* @returns - whether the {@link Quat} are equal or not
*/
equals(quaternion: Quat = new Quat()): boolean {
return (
this.elements[0] === quaternion.elements[0] &&
this.elements[1] === quaternion.elements[1] &&
this.elements[2] === quaternion.elements[2] &&
this.elements[3] === quaternion.elements[3] &&
this.axisOrder === quaternion.axisOrder
)
}
/**
* Sets a rotation {@link Quat} using Euler angles {@link Vec3 | vector} and its axis order
* @param vector - rotation {@link Vec3 | vector} to set our {@link Quat} from
* @returns - {@link Quat} after having applied the rotation
*/
setFromVec3(vector: Vec3): Quat {
const ax = vector.x * 0.5
const ay = vector.y * 0.5
const az = vector.z * 0.5
const cosx = Math.cos(ax)
const cosy = Math.cos(ay)
const cosz = Math.cos(az)
const sinx = Math.sin(ax)
const siny = Math.sin(ay)
const sinz = Math.sin(az)
// XYZ order
if (this.axisOrder === 'XYZ') {
this.elements[0] = sinx * cosy * cosz + cosx * siny * sinz
this.elements[1] = cosx * siny * cosz - sinx * cosy * sinz
this.elements[2] = cosx * cosy * sinz + sinx * siny * cosz
this.elements[3] = cosx * cosy * cosz - sinx * siny * sinz
} else if (this.axisOrder === 'YXZ') {
this.elements[0] = sinx * cosy * cosz + cosx * siny * sinz
this.elements[1] = cosx * siny * cosz - sinx * cosy * sinz
this.elements[2] = cosx * cosy * sinz - sinx * siny * cosz
this.elements[3] = cosx * cosy * cosz + sinx * siny * sinz
} else if (this.axisOrder === 'ZXY') {
this.elements[0] = sinx * cosy * cosz - cosx * siny * sinz
this.elements[1] = cosx * siny * cosz + sinx * cosy * sinz
this.elements[2] = cosx * cosy * sinz + sinx * siny * cosz
this.elements[3] = cosx * cosy * cosz - sinx * siny * sinz
} else if (this.axisOrder === 'ZYX') {
this.elements[0] = sinx * cosy * cosz - cosx * siny * sinz
this.elements[1] = cosx * siny * cosz + sinx * cosy * sinz
this.elements[2] = cosx * cosy * sinz - sinx * siny * cosz
this.elements[3] = cosx * cosy * cosz + sinx * siny * sinz
} else if (this.axisOrder === 'YZX') {
this.elements[0] = sinx * cosy * cosz + cosx * siny * sinz
this.elements[1] = cosx * siny * cosz + sinx * cosy * sinz
this.elements[2] = cosx * cosy * sinz - sinx * siny * cosz
this.elements[3] = cosx * cosy * cosz - sinx * siny * sinz
} else if (this.axisOrder === 'XZY') {
this.elements[0] = sinx * cosy * cosz - cosx * siny * sinz
this.elements[1] = cosx * siny * cosz - sinx * cosy * sinz
this.elements[2] = cosx * cosy * sinz + sinx * siny * cosz
this.elements[3] = cosx * cosy * cosz + sinx * siny * sinz
}
return this
}
/**
* Set a {@link Quat} from a rotation axis {@link Vec3 | vector} and an angle
* @param axis - normalized {@link Vec3 | vector} around which to rotate
* @param angle - angle (in radians) to rotate
* @returns - {@link Quat} after having applied the rotation
*/
setFromAxisAngle(axis: Vec3, angle = 0): Quat {
// https://github.com/mrdoob/three.js/blob/dev/src/math/Quaternion.js#L275
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
// assumes axis is normalized
const halfAngle = angle / 2,
s = Math.sin(halfAngle)
this.elements[0] = axis.x * s
this.elements[1] = axis.y * s
this.elements[2] = axis.z * s
this.elements[3] = Math.cos(halfAngle)
return this
}
/**
* Set a {@link Quat} from a rotation {@link Mat4 | matrix}
* @param matrix - rotation {@link Mat4 | matrix} to use
* @returns - {@link Quat} after having applied the rotation
*/
setFromRotationMatrix(matrix: Mat4): Quat {
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
const te = matrix.elements,
m11 = te[0],
m12 = te[4],
m13 = te[8],
m21 = te[1],
m22 = te[5],
m23 = te[9],
m31 = te[2],
m32 = te[6],
m33 = te[10],
trace = m11 + m22 + m33
if (trace > 0) {
const s = 0.5 / Math.sqrt(trace + 1.0)
this.elements[3] = 0.25 / s
this.elements[0] = (m32 - m23) * s
this.elements[1] = (m13 - m31) * s
this.elements[2] = (m21 - m12) * s
} else if (m11 > m22 && m11 > m33) {
const s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33)
this.elements[3] = (m32 - m23) / s
this.elements[0] = 0.25 * s
this.elements[1] = (m12 + m21) / s
this.elements[2] = (m13 + m31) / s
} else if (m22 > m33) {
const s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33)
this.elements[3] = (m13 - m31) / s
this.elements[0] = (m12 + m21) / s
this.elements[1] = 0.25 * s
this.elements[2] = (m23 + m32) / s
} else {
const s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22)
this.elements[3] = (m21 - m12) / s
this.elements[0] = (m13 + m31) / s
this.elements[1] = (m23 + m32) / s
this.elements[2] = 0.25 * s
}
return this
}
}