Skip to content

Instantly share code, notes, and snippets.

@hYdos
Created January 10, 2023 23:56
Show Gist options
  • Save hYdos/9c26c39b777cc719b2b8bec954cd8412 to your computer and use it in GitHub Desktop.
Save hYdos/9c26c39b777cc719b2b8bec954cd8412 to your computer and use it in GitHub Desktop.
// Decompiled with JetBrains decompiler
// Type: OpenTK.Quaternion
// Assembly: OpenTK, Version=3.0.1.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4
// MVID: 69EF842D-B0C3-4E6A-8728-BBC5AB5B224C
// Assembly location: D:\Projects\PlayPokeMod\Switch-Toolbox\Toolbox\Lib\OpenTK.dll
using System;
using System.Xml.Serialization;
namespace OpenTK
{
[Serializable]
public struct Quaternion : IEquatable<Quaternion>
{
public Vector3 Xyz;
public float W;
public static readonly Quaternion Identity = new Quaternion(0.0f, 0.0f, 0.0f, 1f);
public Quaternion(Vector3 v, float w)
{
this.Xyz = v;
this.W = w;
}
public Quaternion(float x, float y, float z, float w)
: this(new Vector3(x, y, z), w)
{
}
public Quaternion(float rotationX, float rotationY, float rotationZ)
{
rotationX *= 0.5f;
rotationY *= 0.5f;
rotationZ *= 0.5f;
float num1 = (float) Math.Cos((double) rotationX);
float num2 = (float) Math.Cos((double) rotationY);
float num3 = (float) Math.Cos((double) rotationZ);
float num4 = (float) Math.Sin((double) rotationX);
float num5 = (float) Math.Sin((double) rotationY);
float num6 = (float) Math.Sin((double) rotationZ);
this.W = (float) ((double) num1 * (double) num2 * (double) num3 - (double) num4 * (double) num5 * (double) num6);
this.Xyz.X = (float) ((double) num4 * (double) num2 * (double) num3 + (double) num1 * (double) num5 * (double) num6);
this.Xyz.Y = (float) ((double) num1 * (double) num5 * (double) num3 - (double) num4 * (double) num2 * (double) num6);
this.Xyz.Z = (float) ((double) num1 * (double) num2 * (double) num6 + (double) num4 * (double) num5 * (double) num3);
}
public Quaternion(Vector3 eulerAngles)
: this(eulerAngles.X, eulerAngles.Y, eulerAngles.Z)
{
}
[XmlIgnore]
public float X
{
get => this.Xyz.X;
set => this.Xyz.X = value;
}
[XmlIgnore]
public float Y
{
get => this.Xyz.Y;
set => this.Xyz.Y = value;
}
[XmlIgnore]
public float Z
{
get => this.Xyz.Z;
set => this.Xyz.Z = value;
}
public void ToAxisAngle(out Vector3 axis, out float angle)
{
Vector4 axisAngle = this.ToAxisAngle();
axis = axisAngle.Xyz;
angle = axisAngle.W;
}
public Vector4 ToAxisAngle()
{
Quaternion quaternion = this;
if ((double) Math.Abs(quaternion.W) > 1.0)
quaternion.Normalize();
Vector4 axisAngle = new Vector4();
axisAngle.W = 2f * (float) Math.Acos((double) quaternion.W);
float num = (float) Math.Sqrt(1.0 - (double) quaternion.W * (double) quaternion.W);
axisAngle.Xyz = (double) num <= 9.999999747378752E-05 ? Vector3.UnitX : quaternion.Xyz / num;
return axisAngle;
}
public float Length => (float) Math.Sqrt((double) this.W * (double) this.W + (double) this.Xyz.LengthSquared);
public float LengthSquared => this.W * this.W + this.Xyz.LengthSquared;
public Quaternion Normalized()
{
Quaternion quaternion = this;
quaternion.Normalize();
return quaternion;
}
public void Invert() => this.W = -this.W;
public Quaternion Inverted()
{
Quaternion quaternion = this;
quaternion.Invert();
return quaternion;
}
public void Normalize()
{
float num = 1f / this.Length;
this.Xyz *= num;
this.W *= num;
}
public void Conjugate() => this.Xyz = -this.Xyz;
public static Quaternion Add(Quaternion left, Quaternion right) => new Quaternion(left.Xyz + right.Xyz, left.W + right.W);
public static void Add(ref Quaternion left, ref Quaternion right, out Quaternion result) => result = new Quaternion(left.Xyz + right.Xyz, left.W + right.W);
public static Quaternion Sub(Quaternion left, Quaternion right) => new Quaternion(left.Xyz - right.Xyz, left.W - right.W);
public static void Sub(ref Quaternion left, ref Quaternion right, out Quaternion result) => result = new Quaternion(left.Xyz - right.Xyz, left.W - right.W);
public static Quaternion Multiply(Quaternion left, Quaternion right)
{
Quaternion result;
Quaternion.Multiply(ref left, ref right, out result);
return result;
}
public static void Multiply(ref Quaternion left, ref Quaternion right, out Quaternion result) => result = new Quaternion(right.W * left.Xyz + left.W * right.Xyz + Vector3.Cross(left.Xyz, right.Xyz), left.W * right.W - Vector3.Dot(left.Xyz, right.Xyz));
public static void Multiply(ref Quaternion quaternion, float scale, out Quaternion result) => result = new Quaternion(quaternion.X * scale, quaternion.Y * scale, quaternion.Z * scale, quaternion.W * scale);
public static Quaternion Multiply(Quaternion quaternion, float scale) => new Quaternion(quaternion.X * scale, quaternion.Y * scale, quaternion.Z * scale, quaternion.W * scale);
public static Quaternion Conjugate(Quaternion q) => new Quaternion(-q.Xyz, q.W);
public static void Conjugate(ref Quaternion q, out Quaternion result) => result = new Quaternion(-q.Xyz, q.W);
public static Quaternion Invert(Quaternion q)
{
Quaternion result;
Quaternion.Invert(ref q, out result);
return result;
}
public static void Invert(ref Quaternion q, out Quaternion result)
{
float lengthSquared = q.LengthSquared;
if ((double) lengthSquared != 0.0)
{
float num = 1f / lengthSquared;
result = new Quaternion(q.Xyz * -num, q.W * num);
}
else
result = q;
}
public static Quaternion Normalize(Quaternion q)
{
Quaternion result;
Quaternion.Normalize(ref q, out result);
return result;
}
public static void Normalize(ref Quaternion q, out Quaternion result)
{
float num = 1f / q.Length;
result = new Quaternion(q.Xyz * num, q.W * num);
}
public static Quaternion FromAxisAngle(Vector3 axis, float angle)
{
if ((double) axis.LengthSquared == 0.0)
return Quaternion.Identity;
Quaternion identity = Quaternion.Identity;
angle *= 0.5f;
axis.Normalize();
identity.Xyz = axis * (float) Math.Sin((double) angle);
identity.W = (float) Math.Cos((double) angle);
return Quaternion.Normalize(identity);
}
public static Quaternion FromEulerAngles(float pitch, float yaw, float roll) => new Quaternion(pitch, yaw, roll);
public static Quaternion FromEulerAngles(Vector3 eulerAngles) => new Quaternion(eulerAngles);
public static void FromEulerAngles(ref Vector3 eulerAngles, out Quaternion result)
{
float num1 = (float) Math.Cos((double) eulerAngles.X * 0.5);
float num2 = (float) Math.Cos((double) eulerAngles.Y * 0.5);
float num3 = (float) Math.Cos((double) eulerAngles.Z * 0.5);
float num4 = (float) Math.Sin((double) eulerAngles.X * 0.5);
float num5 = (float) Math.Sin((double) eulerAngles.Y * 0.5);
float num6 = (float) Math.Sin((double) eulerAngles.Z * 0.5);
result.W = (float) ((double) num1 * (double) num2 * (double) num3 - (double) num4 * (double) num5 * (double) num6);
result.Xyz.X = (float) ((double) num4 * (double) num2 * (double) num3 + (double) num1 * (double) num5 * (double) num6);
result.Xyz.Y = (float) ((double) num1 * (double) num5 * (double) num3 - (double) num4 * (double) num2 * (double) num6);
result.Xyz.Z = (float) ((double) num1 * (double) num2 * (double) num6 + (double) num4 * (double) num5 * (double) num3);
}
public static Quaternion FromMatrix(Matrix3 matrix)
{
Quaternion result;
Quaternion.FromMatrix(ref matrix, out result);
return result;
}
public static void FromMatrix(ref Matrix3 matrix, out Quaternion result)
{
float trace = matrix.Trace;
if ((double) trace > 0.0)
{
float num1 = (float) Math.Sqrt((double) trace + 1.0) * 2f;
float num2 = 1f / num1;
result.W = num1 * 0.25f;
result.Xyz.X = (matrix.Row2.Y - matrix.Row1.Z) * num2;
result.Xyz.Y = (matrix.Row0.Z - matrix.Row2.X) * num2;
result.Xyz.Z = (matrix.Row1.X - matrix.Row0.Y) * num2;
}
else
{
float x = matrix.Row0.X;
float y = matrix.Row1.Y;
float z = matrix.Row2.Z;
if ((double) x > (double) y && (double) x > (double) z)
{
float num3 = (float) Math.Sqrt(1.0 + (double) x - (double) y - (double) z) * 2f;
float num4 = 1f / num3;
result.W = (matrix.Row2.Y - matrix.Row1.Z) * num4;
result.Xyz.X = num3 * 0.25f;
result.Xyz.Y = (matrix.Row0.Y + matrix.Row1.X) * num4;
result.Xyz.Z = (matrix.Row0.Z + matrix.Row2.X) * num4;
}
else if ((double) y > (double) z)
{
float num5 = (float) Math.Sqrt(1.0 + (double) y - (double) x - (double) z) * 2f;
float num6 = 1f / num5;
result.W = (matrix.Row0.Z - matrix.Row2.X) * num6;
result.Xyz.X = (matrix.Row0.Y + matrix.Row1.X) * num6;
result.Xyz.Y = num5 * 0.25f;
result.Xyz.Z = (matrix.Row1.Z + matrix.Row2.Y) * num6;
}
else
{
float num7 = (float) Math.Sqrt(1.0 + (double) z - (double) x - (double) y) * 2f;
float num8 = 1f / num7;
result.W = (matrix.Row1.X - matrix.Row0.Y) * num8;
result.Xyz.X = (matrix.Row0.Z + matrix.Row2.X) * num8;
result.Xyz.Y = (matrix.Row1.Z + matrix.Row2.Y) * num8;
result.Xyz.Z = num7 * 0.25f;
}
}
}
public static Quaternion Slerp(Quaternion q1, Quaternion q2, float blend)
{
if ((double) q1.LengthSquared == 0.0)
return (double) q2.LengthSquared == 0.0 ? Quaternion.Identity : q2;
if ((double) q2.LengthSquared == 0.0)
return q1;
float d = q1.W * q2.W + Vector3.Dot(q1.Xyz, q2.Xyz);
if ((double) d >= 1.0 || (double) d <= -1.0)
return q1;
if ((double) d < 0.0)
{
q2.Xyz = -q2.Xyz;
q2.W = -q2.W;
d = -d;
}
float num1;
float num2;
if ((double) d < 0.9900000095367432)
{
float a = (float) Math.Acos((double) d);
float num3 = 1f / (float) Math.Sin((double) a);
num1 = (float) Math.Sin((double) a * (1.0 - (double) blend)) * num3;
num2 = (float) Math.Sin((double) a * (double) blend) * num3;
}
else
{
num1 = 1f - blend;
num2 = blend;
}
Quaternion q = new Quaternion(num1 * q1.Xyz + num2 * q2.Xyz, (float) ((double) num1 * (double) q1.W + (double) num2 * (double) q2.W));
return (double) q.LengthSquared > 0.0 ? Quaternion.Normalize(q) : Quaternion.Identity;
}
public static Quaternion operator +(Quaternion left, Quaternion right)
{
left.Xyz += right.Xyz;
left.W += right.W;
return left;
}
public static Quaternion operator -(Quaternion left, Quaternion right)
{
left.Xyz -= right.Xyz;
left.W -= right.W;
return left;
}
public static Quaternion operator *(Quaternion left, Quaternion right)
{
Quaternion.Multiply(ref left, ref right, out left);
return left;
}
public static Quaternion operator *(Quaternion quaternion, float scale)
{
Quaternion.Multiply(ref quaternion, scale, out quaternion);
return quaternion;
}
public static Quaternion operator *(float scale, Quaternion quaternion) => new Quaternion(quaternion.X * scale, quaternion.Y * scale, quaternion.Z * scale, quaternion.W * scale);
public static bool operator ==(Quaternion left, Quaternion right) => left.Equals(right);
public static bool operator !=(Quaternion left, Quaternion right) => !left.Equals(right);
public override string ToString() => string.Format("V: {0}, W: {1}", (object) this.Xyz, (object) this.W);
public override bool Equals(object other) => other is Quaternion quaternion && this == quaternion;
public override int GetHashCode() => this.Xyz.GetHashCode() * 397 ^ this.W.GetHashCode();
public bool Equals(Quaternion other) => this.Xyz == other.Xyz && (double) this.W == (double) other.W;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment