/
Q3.cs
346 lines (293 loc) · 11.6 KB
/
Q3.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
/*
* Copyright Lamont Granquist, Sebastien Gaggini and the MechJeb contributors
* SPDX-License-Identifier: LicenseRef-PD-hp OR Unlicense OR CC0-1.0 OR 0BSD OR MIT-0 OR MIT OR LGPL-2.1+
*/
using System;
using System.Globalization;
using static MechJebLib.Utils.Statics;
using static System.Math;
#nullable enable
// ReSharper disable UnusedMember.Global
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable InconsistentNaming
// ReSharper disable NonReadonlyMemberInGetHashCode
namespace MechJebLib.Primitives
{
public struct Q3 : IEquatable<Q3>, IFormattable
{
private const double KEPS = EPS * 2.0; // for equality checking
public double x, y, z, w;
// Access the x, y, z, w components using [0], [1], [2], [3] respectively.
public double this[int index]
{
get
{
switch (index)
{
case 0: return x;
case 1: return y;
case 2: return z;
case 3: return w;
default:
throw new IndexOutOfRangeException("Invalid Q3 index!");
}
}
set
{
switch (index)
{
case 0:
x = value;
break;
case 1:
y = value;
break;
case 2:
z = value;
break;
case 3:
w = value;
break;
default:
throw new IndexOutOfRangeException("Invalid Q3 index!");
}
}
}
public Q3(double x, double y, double z, double w)
{
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public void Set(double X, double Y, double Z, double W)
{
x = X;
y = Y;
z = Z;
w = W;
}
// The identity rotation (RO). This quaternion corresponds to "no rotation": the object
public static Q3 identity { get; } = new Q3(0.0, 0.0, 0.0, 1.0);
// Combines rotations /lhs/ and /rhs/.
public static Q3 operator *(Q3 q1, Q3 q2) =>
new Q3(
q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y,
q1.w * q2.y - q1.x * q2.z + q1.y * q2.w + q1.z * q2.x,
q1.w * q2.z + q1.x * q2.y - q1.y * q2.x + q1.z * q2.w,
q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z
);
// Rotates the point /point/ with /rotation/.
public static V3 operator *(Q3 q, V3 v)
{
double x = q.x * 2.0;
double y = q.y * 2.0;
double z = q.z * 2.0;
double xx = q.x * x;
double yy = q.y * y;
double zz = q.z * z;
double xy = q.x * y;
double xz = q.x * z;
double yz = q.y * z;
double wx = q.w * x;
double wy = q.w * y;
double wz = q.w * z;
var res = new V3(
(1.0 - (yy + zz)) * v.x + (xy - wz) * v.y + (xz + wy) * v.z,
(xy + wz) * v.x + (1.0 - (xx + zz)) * v.y + (yz - wx) * v.z,
(xz - wy) * v.x + (yz + wx) * v.y + (1.0 - (xx + yy)) * v.z
);
return res;
}
// Is the dot product of two quaternions within tolerance for them to be considered equal?
private static bool IsEqualUsingDot(double dot) =>
// Returns false in the presence of NaN values.
dot > 1.0 - KEPS;
// Are two quaternions equal to each other?
public static bool operator ==(Q3 lhs, Q3 rhs) => IsEqualUsingDot(Dot(lhs, rhs));
// Are two quaternions different from each other?
public static bool operator !=(Q3 lhs, Q3 rhs) =>
// Returns true in the presence of NaN values.
!(lhs == rhs);
// The dot product between two rotations.
public static double Dot(Q3 a, Q3 b) => a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
/*
public void SetLookRotation(V3 view)
{
V3 up = V3.up;
SetLookRotation(view, up);
}
// Creates a rotation with the specified /forward/ and /upwards/ directions.
public void SetLookRotation(V3 view, V3 up)
{
this = LookRotation(view, up);
}
*/
// FIXME: small angle precision?
// Returns the angle in radians between two rotations /a/ and /b/.
public static double Angle(Q3 a, Q3 b)
{
double dot = Dot(a, b);
return IsEqualUsingDot(dot) ? 0.0f : Acos(Min(Abs(dot), 1.0)) * 2.0;
}
// FIXME: kill degrees with fire, fix euler angles
// Makes euler angles positive 0/360 with 0.0001 hacked to support old behaviour of Q3ToEuler
private static V3 Internal_MakePositive(V3 euler)
{
double negativeFlip = Rad2Deg(-0.0001f);
double positiveFlip = 360.0f + negativeFlip;
if (euler.x < negativeFlip)
euler.x += 360.0f;
else if (euler.x > positiveFlip)
euler.x -= 360.0f;
if (euler.y < negativeFlip)
euler.y += 360.0f;
else if (euler.y > positiveFlip)
euler.y -= 360.0f;
if (euler.z < negativeFlip)
euler.z += 360.0f;
else if (euler.z > positiveFlip)
euler.z -= 360.0f;
return euler;
}
// this produces the mathmetical ZYX intrinsic euler angles, which is aircraft roll, pitch, yaw
public static V3 ToEulerAngles(Q3 q)
{
var angles = new V3();
// roll (x-axis rotation)
double sinr_cosp = 2 * (q.w * q.x + q.y * q.z);
double cosr_cosp = 1 - 2 * (q.x * q.x + q.y * q.y);
angles.x = Atan2(sinr_cosp, cosr_cosp);
// negative pitch (y-axis rotation)
double sinp = 2 * (q.w * q.y - q.z * q.x);
angles.y = SafeAsin(sinp);
// negative yaw (z-axis rotation)
double siny_cosp = 2 * (q.w * q.z + q.x * q.y);
double cosy_cosp = 1 - 2 * (q.y * q.y + q.z * q.z);
angles.z = Atan2(siny_cosp, cosy_cosp);
return angles;
}
public V3 eulerAngles => ToEulerAngles(this);
// set { this = Internal_FromEulerRad(Deg2Rad(value)); }
/*
public static Q3 Euler(double x, double y, double z) { return Internal_FromEulerRad(Rad2Deg(new V3(x, y, z))); }
public static Q3 Euler(V3 euler) { return Internal_FromEulerRad(Deg2Rad(euler)); }
public void ToAngleAxis(out double angle, out V3 axis) { Internal_ToAxisAngleRad(this, out axis, out angle); angle = Rad2Deg(angle); }
public void SetFromToRotation(V3 fromDirection, V3 toDirection) { this = FromToRotation(fromDirection, toDirection); }
public static Q3 RotateTowards(Q3 from, Q3 to, double maxDegreesDelta)
{
double angle = Q3.Angle(from, to);
if (angle == 0.0f) return to;
return SlerpUnclamped(from, to, Min(1.0f, maxDegreesDelta / angle));
}
*/
public static Q3 LookRotation(V3 forward, V3 upwards = default)
{
if (upwards == default)
upwards = V3.up;
forward = V3.Normalize(forward);
var right = V3.Normalize(V3.Cross(forward, upwards));
upwards = V3.Cross(right, forward);
// FIXME: slurp these into an M3 and write an M3-rotation-matrix-to-Q3 function
double m00 = forward.x;
double m01 = forward.y;
double m02 = forward.z;
double m10 = right.x;
double m11 = right.y;
double m12 = right.z;
double m20 = -upwards.x;
double m21 = -upwards.y;
double m22 = -upwards.z;
double trace = m00 + m11 + m22;
var q = new Q3();
if (trace > 0f)
{
double num = Sqrt(trace + 1);
q.w = num * 0.5;
num = 0.5 / num;
q.x = (m12 - m21) * num;
q.y = (m20 - m02) * num;
q.z = (m01 - m10) * num;
return q;
}
if (m00 >= m11 && m00 >= m22)
{
double num7 = Sqrt(1 + m00 - m11 - m22);
double num4 = 0.5 / num7;
q.x = 0.5 * num7;
q.y = (m01 + m10) * num4;
q.z = (m02 + m20) * num4;
q.w = (m12 - m21) * num4;
return q;
}
if (m11 > m22)
{
double num6 = Sqrt(1 + m11 - m00 - m22);
double num3 = 0.5 / num6;
q.x = (m10 + m01) * num3;
q.y = 0.5 * num6;
q.z = (m21 + m12) * num3;
q.w = (m20 - m02) * num3;
return q;
}
double num5 = Sqrt(1 + m22 - m00 - m11);
double num2 = 0.5 / num5;
q.x = (m20 + m02) * num2;
q.y = (m21 + m12) * num2;
q.z = 0.5 * num5;
q.w = (m01 - m10) * num2;
return q;
}
public static Q3 FromToRotation(V3 from, V3 to)
{
var c = V3.Cross(from, to);
double w = Sqrt(from.sqrMagnitude * to.sqrMagnitude) + V3.Dot(from, to);
return Normalize(new Q3(c.x, c.y, c.z, w));
}
public static Q3 AngleAxis(double angle, V3 axis)
{
var q = new Q3();
V3 a = axis.normalized;
q.x = a.x * Sin(angle / 2.0);
q.y = a.y * Sin(angle / 2.0);
q.z = a.z * Sin(angle / 2.0);
q.w = Cos(angle / 2.0);
return q;
}
public static Q3 Inverse(Q3 q)
{
double mag2 = Dot(q, q);
return new Q3(-q.x / mag2, -q.y / mag2, -q.z / mag2, q.w / mag2);
}
// FIXME: precision
public static Q3 Normalize(Q3 q)
{
double mag = Sqrt(Dot(q, q));
return mag < EPS ? identity : new Q3(q.x / mag, q.y / mag, q.z / mag, q.w / mag);
}
public void Normalize() => this = Normalize(this);
public Q3 normalized => Normalize(this);
public override string ToString() => ToString(null, CultureInfo.InvariantCulture.NumberFormat);
public string ToString(string format) => ToString(format, CultureInfo.InvariantCulture.NumberFormat);
public string ToString(string? format, IFormatProvider formatProvider)
{
if (string.IsNullOrEmpty(format))
format = "G";
return
$"({x.ToString(format, formatProvider)}, {y.ToString(format, formatProvider)}, {z.ToString(format, formatProvider)}, {w.ToString(format, formatProvider)})";
}
public bool Equals(Q3 other) => x.Equals(other.x) && y.Equals(other.y) && z.Equals(other.z) && w.Equals(other.w);
public override bool Equals(object? obj) => obj is Q3 other && Equals(other);
public override int GetHashCode()
{
unchecked
{
int hashCode = x.GetHashCode();
hashCode = (hashCode * 397) ^ y.GetHashCode();
hashCode = (hashCode * 397) ^ z.GetHashCode();
hashCode = (hashCode * 397) ^ w.GetHashCode();
return hashCode;
}
}
}
}