-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfQuaternion.cs
446 lines (382 loc) · 12.5 KB
/
fQuaternion.cs
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
using System;
using System.Runtime.InteropServices;
using UnityEngine;
[StructLayout(LayoutKind.Explicit)]
public struct fQuaternion
{
[FieldOffset(0)]
public ffloat x;
[FieldOffset(4)]
public ffloat y;
[FieldOffset(8)]
public ffloat z;
[FieldOffset(12)]
public ffloat w;
#region Defines
static readonly fQuaternion identityQuaternion = new fQuaternion(0, 0, 0, 1);
public static fQuaternion identity { get { return identityQuaternion; } }
#endregion
#region Construtor
public fQuaternion(ffloat x, ffloat y, ffloat z, ffloat w)
{
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public fQuaternion(int x, int y, int z, int w)
{
this.x = (ffloat)x;
this.y = (ffloat)y;
this.z = (ffloat)z;
this.w = (ffloat)w;
}
public fQuaternion(float x, float y, float z, float w)
{
this.x = (ffloat)x;
this.y = (ffloat)y;
this.z = (ffloat)z;
this.w = (ffloat)w;
}
public fQuaternion(fQuaternion other)
{
x = other.x;
y = other.y;
z = other.z;
w = other.w;
}
public fQuaternion(Quaternion other)
{
x = (ffloat)other.x;
y = (ffloat)other.y;
z = (ffloat)other.z;
w = (ffloat)other.w;
}
public fQuaternion(fVector3 vec, ffloat f)
{
x = vec.x;
y = vec.y;
z = vec.z;
w = f;
}
#endregion
fVector3 xyz {
get {
return new fVector3(x, y, z);
}
}
ffloat length {
get {
return ffloat.Sqrt(sqrLength);
}
}
ffloat sqrLength {
get {
return x * x + y * y + z * z + w * w;
}
}
public fVector3 eulerAngles {
get {
return ToEuler(this);
}
set {
this = Euler(value);
}
}
public void Set(ffloat newX, ffloat newY, ffloat newZ, ffloat newW)
{
this.x = newX;
this.y = newY;
this.z = newZ;
this.w = newW;
}
public void Scale(ffloat scale)
{
this.x *= scale;
this.y *= scale;
this.z *= scale;
this.w *= scale;
}
void Normalize()
{
ffloat scale = ffloat.One / length;
x *= scale;
y *= scale;
z *= scale;
w *= scale;
}
void Conjugate()
{
x = -x;
y = -y;
z = -z;
}
public override string ToString()
{
return String.Format("({0:F2}, {1:F2}, {2:F2}, {3:F2})", new object[] { x.ToFloat(), y.ToFloat(), z.ToFloat(), w.ToFloat() });
}
#region Arithmetic Operators
public static fQuaternion operator *(fQuaternion lhs, fQuaternion rhs)
{
return new fQuaternion(
lhs.w * rhs.x + lhs.x * rhs.w + lhs.y * rhs.z - lhs.z * rhs.y,
lhs.w * rhs.y + lhs.y * rhs.w + lhs.z * rhs.x - lhs.x * rhs.z,
lhs.w * rhs.z + lhs.z * rhs.w + lhs.x * rhs.y - lhs.y * rhs.x,
lhs.w * rhs.w - lhs.x * rhs.x - lhs.y * rhs.y - lhs.z * rhs.z);
}
public static fVector3 operator *(fQuaternion rotation, fVector3 point)
{
// nVidia SDK implementation
fVector3 qvec = new fVector3(rotation.x, rotation.y, rotation.z);
fVector3 uv = fVector3.Cross(qvec, point);
fVector3 uuv = fVector3.Cross(qvec, uv);
uv *= 2 * rotation.w;
uuv *= 2;
return point + uv + uuv;
}
#endregion
#region Comparison Operators
static bool IsEqualUsingDot(ffloat dot)
{
return dot > (ffloat.One - ffloat.Epsilon);
}
public static bool operator ==(fQuaternion lhs, fQuaternion rhs)
{
return IsEqualUsingDot(fQuaternion.Dot(lhs, rhs));
}
public static bool operator !=(fQuaternion lhs, fQuaternion rhs)
{
return !(lhs == rhs);
}
public override bool Equals(object obj)
{
if (!(obj is fQuaternion))
return false;
fQuaternion other = (fQuaternion)obj;
return x.Equals(other.x) &&
y.Equals(other.y) &&
z.Equals(other.z) &&
w.Equals(other.w);
}
public override int GetHashCode()
{
return x.GetHashCode() ^ y.GetHashCode() << 2 ^ z.GetHashCode() >> 2 ^ w.GetHashCode() >> 1;
}
#endregion
#region Conversion Operators
public static fQuaternion Euler(ffloat x, ffloat y, ffloat z)
{
ffloat halfRad = ffloat.Deg2Rad * ffloat.Half;
x *= halfRad;
y *= halfRad;
z *= halfRad;
ffloat cx = ffloat.Cos(x);
ffloat cy = ffloat.Cos(y);
ffloat cz = ffloat.Cos(z);
ffloat sx = ffloat.Sin(x);
ffloat sy = ffloat.Sin(y);
ffloat sz = ffloat.Sin(z);
return new fQuaternion(
sx * cy * cz + cx * sy * sz,
cx * sy * cz - sx * cy * sz,
cx * cy * sz - sx * sy * cz,
cx * cy * cz + sx * sy * sz);
}
public static fQuaternion Euler(fVector3 euler)
{
return Euler(euler.x, euler.y, euler.z);
}
public static fQuaternion Euler(float x, float y, float z)
{
return Euler((ffloat)x, (ffloat)y, (ffloat)z);
}
public static fQuaternion AngleAxis(ffloat angle, fVector3 axis)
{
ffloat halfRad = angle * ffloat.Deg2Rad * ffloat.Half;
return new fQuaternion((axis * ffloat.Sin(halfRad)).normalized, ffloat.Cos(halfRad));
}
public static fQuaternion LookRotation(fVector3 position)
{
return LookRotation(position, fVector3.up);
}
public static fQuaternion LookRotation(fVector3 position, fVector3 upVector)
{
fVector3 forward = position.normalized;
fVector3 right = fVector3.Cross(upVector, forward);
upVector = fVector3.Cross(forward, right);
ffloat m00 = right.x;
ffloat m01 = right.y;
ffloat m02 = right.z;
ffloat m10 = upVector.x;
ffloat m11 = upVector.y;
ffloat m12 = upVector.z;
ffloat m20 = forward.x;
ffloat m21 = forward.y;
ffloat m22 = forward.z;
ffloat diag = m00 + m11 + m22;
fQuaternion result = new fQuaternion();
if (diag > ffloat.Zero) {
ffloat scale = ffloat.Sqrt(diag + ffloat.One);
result.w = scale * ffloat.Half;
scale = ffloat.Half / scale;
result.x = (m12 - m21) * scale;
result.y = (m20 - m02) * scale;
result.z = (m01 - m10) * scale;
}
else if ((m00 >= m11) && (m00 >= m22)) {
ffloat scale = ffloat.Sqrt(((ffloat.One + m00) - m11) - m22);
result.x = ffloat.Half * scale;
scale = ffloat.Half / scale;
result.y = (m01 + m10) * scale;
result.z = (m02 + m20) * scale;
result.w = (m12 - m21) * scale;
}
else if (m11 > m22) {
ffloat scale = ffloat.Sqrt(((ffloat.One + m11) - m00) - m22);
result.y = ffloat.Half * scale;
scale = ffloat.Half / scale;
result.x = (m10 + m01) * scale;
result.z = (m21 + m12) * scale;
result.w = (m20 - m02) * scale;
}
else {
ffloat scale = ffloat.Sqrt(((ffloat.One + m22) - m00) - m11);
result.z = ffloat.Half * scale;
scale = ffloat.Half / scale;
result.x = (m20 + m02) * scale;
result.y = (m21 + m12) * scale;
result.w = (m01 - m10) * scale;
}
return result;
}
public static fQuaternion FromToRotation(fVector3 from, fVector3 to)
{
fVector3 fn = from.normalized;
fVector3 tn = to.normalized;
ffloat dot = fVector3.Dot(fn, tn);
if (dot <= -ffloat.One) {
fVector3 tmp = fVector3.Cross(fVector3.right, fn);
if (tmp.magnitude < ffloat.Epsilon)
tmp = fVector3.Cross(fVector3.up, fn);
tmp.Normalize();
return AngleAxis(ffloat.Pi, tmp);
}
else if (dot >= ffloat.One) {
return identity;
}
ffloat scale = ffloat.Sqrt((ffloat.One + dot) * 2);
ffloat inv = ffloat.One / scale;
fVector3 cross = fVector3.Cross(fn, tn);
fQuaternion result = new fQuaternion(cross * inv, scale * ffloat.Half);
result.Normalize();
return result;
}
public static fVector3 ToEuler(fQuaternion rot)
{
ffloat x2 = rot.x * rot.x;
ffloat y2 = rot.y * rot.y;
ffloat z2 = rot.z * rot.z;
ffloat w2 = rot.w * rot.w;
ffloat unit = x2 + y2 + z2 + w2;
ffloat test = rot.x * rot.w - rot.y * rot.z;
if (test >= ffloat.Half * unit) {
return new fVector3(2 * ffloat.Atan2(rot.x, rot.w), ffloat.HalfPi, ffloat.Zero);
}
if (test <= -ffloat.Half * unit) {
return new fVector3(-2 * ffloat.Atan2(rot.x, rot.w), -ffloat.HalfPi, ffloat.Zero);
}
return NormalizedAngleVector(
ffloat.Asin(2 * test),
ffloat.Atan2(2 * rot.w * rot.y + 2 * rot.z * rot.x, ffloat.One - 2 * (x2 + y2)),
ffloat.Atan2(2 * rot.w * rot.z + 2 * rot.x * rot.y, ffloat.One - 2 * (z2 + x2)));
}
static fVector3 NormalizedAngleVector(ffloat x, ffloat y, ffloat z)
{
return new fVector3(NormalizeAngle(x), NormalizeAngle(y), NormalizeAngle(z));
}
static ffloat NormalizeAngle(ffloat rad)
{
ffloat oneRound = new ffloat(360);
ffloat degree = rad * ffloat.Rad2Deg;
while (degree > oneRound)
degree -= oneRound;
while (degree < ffloat.Zero)
degree += oneRound;
return degree;
}
public static fQuaternion Inverse(fQuaternion rotation)
{
ffloat inv = ffloat.One / (rotation.x * rotation.x + rotation.y * rotation.y + rotation.z * rotation.z + rotation.w * rotation.w);
return new fQuaternion(-rotation.x * inv, -rotation.y * inv, -rotation.z * inv, rotation.w * inv);
}
public Quaternion ToUnityQuaternion()
{
return new Quaternion(x.ToFloat(), y.ToFloat(), z.ToFloat(), w.ToFloat());
}
#endregion
#region MathFunctions
public static ffloat Angle(fQuaternion lhs, fQuaternion rhs)
{
ffloat dot = Dot(lhs, rhs);
return (!IsEqualUsingDot(dot)) ? ffloat.Acos(ffloat.Min(ffloat.Abs(dot), ffloat.One)) * 2 * ffloat.Rad2Deg : ffloat.Zero;
}
public static ffloat Dot(fQuaternion lhs, fQuaternion rhs)
{
return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z + lhs.w * rhs.w;
}
public static fQuaternion Lerp(fQuaternion from, fQuaternion to, ffloat t)
{
fQuaternion result;
ffloat inv = ffloat.One - t;
if (Dot(from, to) > ffloat.Zero) {
result = new fQuaternion(
inv * from.x + t * to.x,
inv * from.y + t * to.y,
inv * from.z + t * to.z,
inv * from.w + t * to.w);
}
else {
result = new fQuaternion(
inv * from.x - t * to.x,
inv * from.y - t * to.y,
inv * from.z - t * to.z,
inv * from.w - t * to.w);
}
return result;
}
public static fQuaternion Slerp(fQuaternion from, fQuaternion to, ffloat t)
{
if (from.sqrLength == ffloat.Zero) {
if (to.sqrLength == ffloat.Zero)
return identity;
return to;
}
else if (to.sqrLength == ffloat.Zero)
return from;
ffloat cosHalf = from.w * to.w + fVector3.Dot(from.xyz, to.xyz);
if (cosHalf >= ffloat.One || cosHalf <= -ffloat.One)
return from;
else if (cosHalf <= ffloat.Zero) {
to.Conjugate();
cosHalf = -cosHalf;
}
ffloat fromRate = ffloat.One - t;
ffloat toRate = t;
if (cosHalf <= ffloat.One) {
ffloat half = ffloat.Acos(cosHalf);
ffloat sinHalf = ffloat.Sin(half);
ffloat overSinHalf = ffloat.One / sinHalf;
fromRate = ffloat.Sin(half * fromRate) * overSinHalf;
toRate = ffloat.Sin(half * toRate) * overSinHalf;
}
return new fQuaternion(fromRate * from.xyz + toRate * to.xyz, fromRate * from.w + toRate * to.w);
}
public static fQuaternion RotateTowards(fQuaternion from, fQuaternion to, ffloat maxDegree)
{
ffloat angle = Angle(from, to);
if (angle == ffloat.Zero)
return to;
return Slerp(from, to, ffloat.Min(ffloat.One, maxDegree / angle));
}
#endregion
}