| | | 1 | | //======================================================================= |
| | | 2 | | // Fixed3x3.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 the Fixed3x3 structure, enabling additional transformation and comparison operations. |
| | | 14 | | /// </summary> |
| | | 15 | | public static class Fixed3x3Extensions |
| | | 16 | | { |
| | | 17 | | #region Transformations |
| | | 18 | | |
| | | 19 | | /// <inheritdoc cref="Fixed3x3.ExtractScale(Fixed3x3)" /> |
| | | 20 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 21 | | public static Vector3d ExtractScale(this Fixed3x3 matrix) => Fixed3x3.ExtractScale(matrix); |
| | | 22 | | |
| | | 23 | | /// <inheritdoc cref="Fixed3x3.ExtractLossyScale(Fixed3x3)" /> |
| | | 24 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 25 | | public static Vector3d ExtractLossyScale(this Fixed3x3 matrix) => Fixed3x3.ExtractLossyScale(matrix); |
| | | 26 | | |
| | | 27 | | /// <inheritdoc cref="Fixed3x3.SetScale(Fixed3x3, Vector3d)" /> |
| | | 28 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 29 | | public static Fixed3x3 SetScale(this ref Fixed3x3 matrix, Vector3d localScale) => |
| | 4 | 30 | | matrix = Fixed3x3.SetScale(matrix, localScale); |
| | | 31 | | |
| | | 32 | | /// <inheritdoc cref="Fixed3x3.SetGlobalScale(Fixed3x3, Vector3d)" /> |
| | | 33 | | public static Fixed3x3 SetGlobalScale(this ref Fixed3x3 matrix, Vector3d globalScale) => |
| | 2 | 34 | | matrix = Fixed3x3.SetGlobalScale(matrix, globalScale); |
| | | 35 | | |
| | | 36 | | /// <inheritdoc cref="Fixed3x3.Lerp(Fixed3x3, Fixed3x3, Fixed64)" /> |
| | | 37 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 38 | | public static Fixed3x3 Lerp(this Fixed3x3 matrix, Fixed3x3 target, Fixed64 amount) => |
| | 1 | 39 | | Fixed3x3.Lerp(matrix, target, amount); |
| | | 40 | | |
| | | 41 | | /// <inheritdoc cref="Fixed3x3.Transpose(Fixed3x3)" /> |
| | | 42 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 43 | | public static Fixed3x3 Transpose(this Fixed3x3 matrix) => Fixed3x3.Transpose(matrix); |
| | | 44 | | |
| | | 45 | | /// <inheritdoc cref="Fixed3x3.TransformDirection(Fixed3x3, Vector3d)" /> |
| | | 46 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 47 | | public static Vector3d TransformDirection(this Fixed3x3 matrix, Vector3d direction) => |
| | 1 | 48 | | Fixed3x3.TransformDirection(matrix, direction); |
| | | 49 | | |
| | | 50 | | /// <inheritdoc cref="Fixed3x3.InverseTransformDirection(Fixed3x3, Vector3d)" /> |
| | | 51 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 52 | | public static Vector3d InverseTransformDirection(this Fixed3x3 matrix, Vector3d direction) => |
| | 1 | 53 | | Fixed3x3.InverseTransformDirection(matrix, direction); |
| | | 54 | | |
| | | 55 | | #endregion |
| | | 56 | | |
| | | 57 | | #region Equality |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Compares two Fixed3x3 for approximate equality, allowing a fixed absolute difference between components. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="f1">The current Fixed3x3.</param> |
| | | 63 | | /// <param name="f2">The Fixed3x3 to compare against.</param> |
| | | 64 | | /// <param name="allowedDifference">The allowed absolute difference between each component.</param> |
| | | 65 | | /// <returns>True if the components are within the allowed difference, false otherwise.</returns> |
| | | 66 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 67 | | public static bool FuzzyEqualAbsolute(this Fixed3x3 f1, Fixed3x3 f2, Fixed64 allowedDifference) => |
| | 11 | 68 | | (f1.M11 - f2.M11).Abs() <= allowedDifference && |
| | 11 | 69 | | (f1.M12 - f2.M12).Abs() <= allowedDifference && |
| | 11 | 70 | | (f1.M13 - f2.M13).Abs() <= allowedDifference && |
| | 11 | 71 | | (f1.M21 - f2.M21).Abs() <= allowedDifference && |
| | 11 | 72 | | (f1.M22 - f2.M22).Abs() <= allowedDifference && |
| | 11 | 73 | | (f1.M23 - f2.M23).Abs() <= allowedDifference && |
| | 11 | 74 | | (f1.M31 - f2.M31).Abs() <= allowedDifference && |
| | 11 | 75 | | (f1.M32 - f2.M32).Abs() <= allowedDifference && |
| | 11 | 76 | | (f1.M33 - f2.M33).Abs() <= allowedDifference; |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Compares two Fixed3x3 for approximate equality, allowing a fractional percentage (defaults to ~1%) difference be |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="f1">The current Fixed3x3.</param> |
| | | 82 | | /// <param name="f2">The Fixed3x3 to compare against.</param> |
| | | 83 | | /// <param name="percentage">The allowed fractional difference (percentage) for each component.</param> |
| | | 84 | | /// <returns>True if the components are within the allowed percentage difference, false otherwise.</returns> |
| | | 85 | | public static bool FuzzyEqual(this Fixed3x3 f1, Fixed3x3 f2, Fixed64? percentage = null) |
| | | 86 | | { |
| | 12 | 87 | | Fixed64 p = percentage ?? Fixed64.Epsilon; |
| | 12 | 88 | | return f1.M11.FuzzyComponentEqual(f2.M11, p) && |
| | 12 | 89 | | f1.M12.FuzzyComponentEqual(f2.M12, p) && |
| | 12 | 90 | | f1.M13.FuzzyComponentEqual(f2.M13, p) && |
| | 12 | 91 | | f1.M21.FuzzyComponentEqual(f2.M21, p) && |
| | 12 | 92 | | f1.M22.FuzzyComponentEqual(f2.M22, p) && |
| | 12 | 93 | | f1.M23.FuzzyComponentEqual(f2.M23, p) && |
| | 12 | 94 | | f1.M31.FuzzyComponentEqual(f2.M31, p) && |
| | 12 | 95 | | f1.M32.FuzzyComponentEqual(f2.M32, p) && |
| | 12 | 96 | | f1.M33.FuzzyComponentEqual(f2.M33, p); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | #endregion |
| | | 100 | | } |