-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathQuaternion.cs
47 lines (41 loc) · 1.2 KB
/
Quaternion.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
using System;
using System.Runtime.InteropServices;
using VRCFaceTracking.Core.Types;
namespace VirtualDesktop.FaceTracking
{
[StructLayout(LayoutKind.Sequential)]
public struct Quaternion
{
#region Static Fields
public static readonly Quaternion Identity = new Quaternion(0.0f, 0.0f, 0.0f, 1.0f);
#endregion
#region Fields
public float X;
public float Y;
public float Z;
public float W;
#endregion
#region Constructor
public Quaternion(float x, float y, float z, float w)
{
X = x;
Y = y;
Z = z;
W = w;
}
#endregion
#region Methods
public Vector2 Cartesian()
{
float magnitude = (float)Math.Sqrt(X*X + Y*Y + Z*Z + W*W);
float Xm = X / magnitude;
float Ym = Y / magnitude;
float Zm = Z / magnitude;
float Wm = W / magnitude;
float pitch = (float)Math.Asin(2 * (Xm*Zm - Wm*Ym));
float yaw = (float)Math.Atan2(2 * (Ym*Zm + Wm*Xm), Wm*Wm - Xm*Xm - Ym*Ym + Zm*Zm);
return new Vector2(pitch, yaw);
}
#endregion
}
}