| | | 1 | | //======================================================================= |
| | | 2 | | // FixedQuaternion.Extensions.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | |
| | | 10 | | namespace FixedMathSharp; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provides extension methods for performing operations and comparisons on FixedQuaternion instances, including |
| | | 14 | | /// approximate equality checks and angular velocity calculations. |
| | | 15 | | /// </summary> |
| | | 16 | | public static partial class FixedQuaternionExtensions |
| | | 17 | | { |
| | | 18 | | #region Operations |
| | | 19 | | |
| | | 20 | | /// <inheritdoc cref="FixedQuaternion.ToAngularVelocity" /> |
| | | 21 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 22 | | public static Vector3d ToAngularVelocity( |
| | | 23 | | this FixedQuaternion currentRotation, |
| | | 24 | | FixedQuaternion previousRotation, |
| | 1 | 25 | | Fixed64 deltaTime) => FixedQuaternion.ToAngularVelocity(currentRotation, previousRotation, deltaTime); |
| | | 26 | | |
| | | 27 | | /// <inheritdoc cref="FixedQuaternion.Lerp(FixedQuaternion, FixedQuaternion, Fixed64)" /> |
| | | 28 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 29 | | public static FixedQuaternion Lerp(this FixedQuaternion start, FixedQuaternion end, Fixed64 amount) => |
| | 1 | 30 | | FixedQuaternion.Lerp(start, end, amount); |
| | | 31 | | |
| | | 32 | | /// <inheritdoc cref="FixedQuaternion.Slerp(FixedQuaternion, FixedQuaternion, Fixed64)" /> |
| | | 33 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 34 | | public static FixedQuaternion Slerp(this FixedQuaternion start, FixedQuaternion end, Fixed64 amount) => |
| | 1 | 35 | | FixedQuaternion.Slerp(start, end, amount); |
| | | 36 | | |
| | | 37 | | /// <inheritdoc cref="FixedQuaternion.Angle(FixedQuaternion, FixedQuaternion)" /> |
| | | 38 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 39 | | public static Fixed64 Angle(this FixedQuaternion start, FixedQuaternion end) => FixedQuaternion.Angle(start, end); |
| | | 40 | | |
| | | 41 | | /// <inheritdoc cref="FixedQuaternion.Dot(FixedQuaternion, FixedQuaternion)" /> |
| | | 42 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 43 | | public static Fixed64 Dot(this FixedQuaternion lhs, FixedQuaternion rhs) => FixedQuaternion.Dot(lhs, rhs); |
| | | 44 | | |
| | | 45 | | /// <inheritdoc cref="FixedQuaternion.QuaternionLog(FixedQuaternion)" /> |
| | | 46 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 47 | | public static Vector3d QuaternionLog(this FixedQuaternion q) => FixedQuaternion.QuaternionLog(q); |
| | | 48 | | |
| | | 49 | | #endregion |
| | | 50 | | |
| | | 51 | | #region Equality |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Compares two quaternions for approximate equality, allowing a fixed absolute difference between components. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="q1">The current quaternion.</param> |
| | | 57 | | /// <param name="q2">The quaternion to compare against.</param> |
| | | 58 | | /// <param name="allowedDifference">The allowed absolute difference between each component.</param> |
| | | 59 | | /// <returns>True if the components are within the allowed difference, false otherwise.</returns> |
| | | 60 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 61 | | public static bool FuzzyEqualAbsolute(this FixedQuaternion q1, FixedQuaternion q2, Fixed64 allowedDifference) => |
| | 6 | 62 | | (q1.X - q2.X).Abs() <= allowedDifference && |
| | 6 | 63 | | (q1.Y - q2.Y).Abs() <= allowedDifference && |
| | 6 | 64 | | (q1.Z - q2.Z).Abs() <= allowedDifference && |
| | 6 | 65 | | (q1.W - q2.W).Abs() <= allowedDifference; |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Compares two quaternions for approximate equality, allowing a fractional percentage (defaults to ~1%) difference |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="q1">The current quaternion.</param> |
| | | 71 | | /// <param name="q2">The quaternion to compare against.</param> |
| | | 72 | | /// <param name="percentage">The allowed fractional difference (percentage) for each component.</param> |
| | | 73 | | /// <returns>True if the components are within the allowed percentage difference, false otherwise.</returns> |
| | | 74 | | public static bool FuzzyEqual(this FixedQuaternion q1, FixedQuaternion q2, Fixed64? percentage = null) |
| | | 75 | | { |
| | 50 | 76 | | Fixed64 p = percentage ?? Fixed64.Epsilon; |
| | 50 | 77 | | return q1.X.FuzzyComponentEqual(q2.X, p) && |
| | 50 | 78 | | q1.Y.FuzzyComponentEqual(q2.Y, p) && |
| | 50 | 79 | | q1.Z.FuzzyComponentEqual(q2.Z, p) && |
| | 50 | 80 | | q1.W.FuzzyComponentEqual(q2.W, p); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | #endregion |
| | | 84 | | } |