1
0
mirror of https://github.com/oliverbooth/VpSharp synced 2024-11-10 02:35:42 +00:00

Convert Quaternion extensions to AI-generated implementations

Yes, seriously. ChatGPT sure is nice.
This commit is contained in:
Oliver Booth 2022-12-05 19:37:36 +00:00
parent d40129856a
commit 02f1d1394c
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
2 changed files with 18 additions and 67 deletions

View File

@ -384,7 +384,7 @@ public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
lock (_client.Lock) lock (_client.Lock)
{ {
(double x, double y, double z) = position; (double x, double y, double z) = position;
(double pitch, double yaw, double _) = rotation.ToEulerAngles(false); (double pitch, double yaw, double _) = rotation.ToEulerAngles();
_ = vp_double_set(handle, FloatAttribute.MyX, x); _ = vp_double_set(handle, FloatAttribute.MyX, x);
_ = vp_double_set(handle, FloatAttribute.MyY, y); _ = vp_double_set(handle, FloatAttribute.MyY, y);
@ -404,7 +404,7 @@ public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
lock (_client.Lock) lock (_client.Lock)
{ {
(float x, float y, float z) = (Vector3)position; (float x, float y, float z) = (Vector3)position;
(float pitch, float yaw, float _) = (Vector3)rotation.ToEulerAngles(false); (float pitch, float yaw, float _) = (Vector3)rotation.ToEulerAngles();
var reason = (ReasonCode)vp_teleport_avatar(handle, Session, world, x, y, z, yaw, pitch); var reason = (ReasonCode)vp_teleport_avatar(handle, Session, world, x, y, z, yaw, pitch);
if (reason == ReasonCode.NotInWorld) if (reason == ReasonCode.NotInWorld)

View File

@ -11,70 +11,31 @@ public static class QuaternionExtensions
/// Converts this quaternion to a <see cref="Vector3d" /> containing an Euler representation of the rotation. /// Converts this quaternion to a <see cref="Vector3d" /> containing an Euler representation of the rotation.
/// </summary> /// </summary>
/// <param name="value">The quaternion to convert.</param> /// <param name="value">The quaternion to convert.</param>
/// <param name="radians">
/// <see langword="true" /> if the resulting vector should be in radians; or <see langword="false" /> if the resulting
/// vector should be in degrees.
/// </param>
/// <returns>The Euler representation of <paramref name="value" />.</returns> /// <returns>The Euler representation of <paramref name="value" />.</returns>
/// <see href="https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/" /> /// <see href="https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/" />
public static Vector3d ToEulerAngles(this Quaternion value, bool radians = true) public static Vector3d ToEulerAngles(this Quaternion value)
{ {
double a = (2.0 * value.Y * value.W) - (2.0 * value.X * value.Z); value = Quaternion.Normalize(value);
double b = 1.0 - (2.0 * value.Y * value.Y) - (2.0 * value.Z * value.Z); double x = Math.Atan2(2 * (value.X * value.W - value.Y * value.Z), 1 - 2 * (value.X * value.X + value.Z * value.Z));
double y = -Math.Atan2(a, b); double y = Math.Asin(2 * (value.X * value.Z + value.Y * value.W));
double z = Math.Atan2(2 * (value.Z * value.W - value.X * value.Y), 1 - 2 * (value.Y * value.Y + value.Z * value.Z));
a = 2.0 * value.X * value.Y; return new Vector3d(x, y, z) * (180 / Math.PI);
b = 2.0 * value.Z * value.W;
double z = Math.Asin(a + b);
a = (2.0 * value.X * value.W) - (2.0 * value.Y * value.Z);
b = 1.0 - (2.0 * value.X * value.X) - (2.0 * value.Z * value.Z);
double x = Math.Atan2(a, b);
if (!radians)
{
x = 180.0 / Math.PI * x;
y = 180.0 / Math.PI * y;
z = 180.0 / Math.PI * z;
}
return new Vector3d(x, y, z);
} }
/// <summary> /// <summary>
/// Converts this quaternion to a <see cref="Vector3d" /> containing an Euler representation of the rotation. /// Converts this quaternion to a <see cref="Vector3d" /> containing an Euler representation of the rotation.
/// </summary> /// </summary>
/// <param name="value">The quaternion to convert.</param> /// <param name="value">The quaternion to convert.</param>
/// <param name="radians">
/// <see langword="true" /> if the resulting vector should be in radians; or <see langword="false" /> if the resulting
/// vector should be in degrees.
/// </param>
/// <returns>The Euler representation of <paramref name="value" />.</returns> /// <returns>The Euler representation of <paramref name="value" />.</returns>
public static Vector3d ToEulerAnglesF(this Quaternion value, bool radians = true) public static Vector3 ToEulerAnglesF(this Quaternion value)
{ {
float a = (2.0f * value.Y * value.W) - (2.0f * value.X * value.Z); value = Quaternion.Normalize(value);
float b = 1.0f - (2.0f * value.Y * value.Y) - (2.0f * value.Z * value.Z); float x = MathF.Atan2(2 * (value.X * value.W - value.Y * value.Z), 1 - 2 * (value.X * value.X + value.Z * value.Z));
float y = -MathF.Atan2(a, b); float y = MathF.Asin(2 * (value.X * value.Z + value.Y * value.W));
float z = MathF.Atan2(2 * (value.Z * value.W - value.X * value.Y), 1 - 2 * (value.Y * value.Y + value.Z * value.Z));
a = 2.0f * value.X * value.Y; return new Vector3(x, y, z) * (180 / MathF.PI);
b = 2.0f * value.Z * value.W;
float z = MathF.Asin(a + b);
a = (2.0f * value.X * value.W) - (2.0f * value.Y * value.Z);
b = 1.0f - (2.0f * value.X * value.X) - (2.0f * value.Z * value.Z);
float x = MathF.Atan2(a, b);
if (!radians)
{
x = 180.0f / MathF.PI * x;
y = 180.0f / MathF.PI * y;
z = 180.0f / MathF.PI * z;
}
return new Vector3d(x, y, z);
} }
//
#pragma warning disable CA1021 #pragma warning disable CA1021
/// <summary> /// <summary>
/// Converts this quaternion to an axis/angle pair. /// Converts this quaternion to an axis/angle pair.
@ -85,13 +46,8 @@ public static class QuaternionExtensions
/// <see href="https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm"/> /// <see href="https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm"/>
public static void ToAxisAngle(this Quaternion value, out Vector3 axis, out float angle) public static void ToAxisAngle(this Quaternion value, out Vector3 axis, out float angle)
{ {
angle = 2.0f * MathF.Acos(value.W); angle = 2 * MathF.Acos(value.W);
axis = Vector3.Normalize(new Vector3(value.X, value.Y, value.Z));
float x = value.X / MathF.Sqrt(1.0f - (value.W * value.W));
float y = value.Y / MathF.Sqrt(1.0f - (value.W * value.W));
float z = value.Z / MathF.Sqrt(1.0f - (value.W * value.W));
axis = new Vector3(x, y, z);
} }
/// <summary> /// <summary>
@ -102,13 +58,8 @@ public static class QuaternionExtensions
/// <param name="angle">The angle value.</param> /// <param name="angle">The angle value.</param>
public static void ToAxisAngle(this Quaternion value, out Vector3d axis, out double angle) public static void ToAxisAngle(this Quaternion value, out Vector3d axis, out double angle)
{ {
angle = 2.0 * Math.Acos(value.W); angle = 2 * Math.Acos(value.W);
axis = Vector3d.Normalize(new Vector3d(value.X, value.Y, value.Z));
double x = value.X / Math.Sqrt(1.0 - (value.W * value.W));
double y = value.Y / Math.Sqrt(1.0 - (value.W * value.W));
double z = value.Z / Math.Sqrt(1.0 - (value.W * value.W));
axis = new Vector3d(x, y, z);
} }
#pragma warning restore CA1021 #pragma warning restore CA1021
} }