| | | 1 | | //======================================================================= |
| | | 2 | | // Fixed4x4.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 Fixed4x4 structure to simplify common matrix operations |
| | | 14 | | /// such as extracting scale, setting transformation components, and transforming points. |
| | | 15 | | /// </summary> |
| | | 16 | | public static class Fixed4x4Extensions |
| | | 17 | | { |
| | | 18 | | #region Extraction, and Setters |
| | | 19 | | |
| | | 20 | | /// <inheritdoc cref="Fixed4x4.ExtractLossyScale(Fixed4x4)" /> |
| | | 21 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 22 | | public static Vector3d ExtractLossyScale(this Fixed4x4 matrix) => Fixed4x4.ExtractLossyScale(matrix); |
| | | 23 | | |
| | | 24 | | /// <inheritdoc cref="Fixed4x4.ExtractScale(Fixed4x4)" /> |
| | | 25 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 26 | | public static Vector3d ExtractScale(this Fixed4x4 matrix) => Fixed4x4.ExtractScale(matrix); |
| | | 27 | | |
| | | 28 | | /// <inheritdoc cref="Fixed4x4.ExtractTranslation(Fixed4x4)" /> |
| | | 29 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 30 | | public static Vector3d ExtractTranslation(this Fixed4x4 matrix) => Fixed4x4.ExtractTranslation(matrix); |
| | | 31 | | |
| | | 32 | | /// <inheritdoc cref="Fixed4x4.ExtractRotation(Fixed4x4)" /> |
| | | 33 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 34 | | public static FixedQuaternion ExtractRotation(this Fixed4x4 matrix) => Fixed4x4.ExtractRotation(matrix); |
| | | 35 | | |
| | | 36 | | /// <inheritdoc cref="Fixed4x4.Decompose(Fixed4x4, out Vector3d, out FixedQuaternion, out Vector3d)" /> |
| | | 37 | | public static bool Decompose( |
| | | 38 | | this Fixed4x4 matrix, |
| | | 39 | | out Vector3d translation, |
| | | 40 | | out FixedQuaternion rotation, |
| | 1 | 41 | | out Vector3d scale) => Fixed4x4.Decompose(matrix, out translation, out rotation, out scale); |
| | | 42 | | |
| | | 43 | | /// <inheritdoc cref="Fixed4x4.SetGlobalScale(Fixed4x4, Vector3d)" /> |
| | | 44 | | public static Fixed4x4 SetGlobalScale(this ref Fixed4x4 matrix, Vector3d globalScale) => |
| | 3 | 45 | | matrix = Fixed4x4.SetGlobalScale(matrix, globalScale); |
| | | 46 | | |
| | | 47 | | /// <inheritdoc cref="Fixed4x4.SetScale(Fixed4x4, Vector3d)" /> |
| | | 48 | | public static Fixed4x4 SetScale(this ref Fixed4x4 matrix, Vector3d scale) => |
| | 1 | 49 | | matrix = Fixed4x4.SetScale(matrix, scale); |
| | | 50 | | |
| | | 51 | | /// <inheritdoc cref="Fixed4x4.SetTranslation(Fixed4x4, Vector3d)" /> |
| | | 52 | | public static Fixed4x4 SetTranslation(this ref Fixed4x4 matrix, Vector3d position) => |
| | 2 | 53 | | matrix = Fixed4x4.SetTranslation(matrix, position); |
| | | 54 | | |
| | | 55 | | /// <inheritdoc cref="Fixed4x4.SetRotation(Fixed4x4, FixedQuaternion)" /> |
| | | 56 | | public static Fixed4x4 SetRotation(this ref Fixed4x4 matrix, FixedQuaternion rotation) => |
| | 3 | 57 | | matrix = Fixed4x4.SetRotation(matrix, rotation); |
| | | 58 | | |
| | | 59 | | /// <inheritdoc cref="Fixed4x4.NormalizeRotationMatrix(Fixed4x4)" /> |
| | | 60 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 61 | | public static Fixed4x4 NormalizeRotationMatrix(this ref Fixed4x4 matrix) => |
| | 1 | 62 | | matrix = Fixed4x4.NormalizeRotationMatrix(matrix); |
| | | 63 | | |
| | | 64 | | /// <inheritdoc cref="Fixed4x4.Lerp(Fixed4x4, Fixed4x4, Fixed64)" /> |
| | | 65 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 66 | | public static Fixed4x4 Lerp(this Fixed4x4 matrix, Fixed4x4 target, Fixed64 amount) => |
| | 1 | 67 | | Fixed4x4.Lerp(matrix, target, amount); |
| | | 68 | | |
| | | 69 | | /// <inheritdoc cref="Fixed4x4.Transpose(Fixed4x4)" /> |
| | | 70 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 71 | | public static Fixed4x4 Transpose(this Fixed4x4 matrix) => Fixed4x4.Transpose(matrix); |
| | | 72 | | |
| | | 73 | | /// <inheritdoc cref="Fixed4x4.TransformPoint(Fixed4x4, Vector3d)" /> |
| | | 74 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 75 | | public static Vector3d TransformPoint(this Fixed4x4 matrix, Vector3d point) => |
| | 1 | 76 | | Fixed4x4.TransformPoint(matrix, point); |
| | | 77 | | |
| | | 78 | | /// <inheritdoc cref="Fixed4x4.Transform(Fixed4x4, Vector4d)" /> |
| | | 79 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 80 | | public static Vector4d Transform(this Fixed4x4 matrix, Vector4d vector) => |
| | 1 | 81 | | Fixed4x4.Transform(matrix, vector); |
| | | 82 | | |
| | | 83 | | /// <inheritdoc cref="Fixed4x4.InverseTransformPoint(Fixed4x4, Vector3d)" /> |
| | | 84 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 85 | | public static Vector3d InverseTransformPoint(this Fixed4x4 matrix, Vector3d point) => |
| | 1 | 86 | | Fixed4x4.InverseTransformPoint(matrix, point); |
| | | 87 | | |
| | | 88 | | #endregion |
| | | 89 | | |
| | | 90 | | #region Equality |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Compares two Fixed4x4 for approximate equality, allowing a fixed absolute difference between components. |
| | | 94 | | /// </summary> |
| | | 95 | | /// <param name="f1">The current Fixed4x4.</param> |
| | | 96 | | /// <param name="f2">The Fixed4x4 to compare against.</param> |
| | | 97 | | /// <param name="allowedDifference">The allowed absolute difference between each component.</param> |
| | | 98 | | /// <returns>True if the components are within the allowed difference, false otherwise.</returns> |
| | | 99 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 100 | | public static bool FuzzyEqualAbsolute(this Fixed4x4 f1, Fixed4x4 f2, Fixed64 allowedDifference) => |
| | 18 | 101 | | FuzzyEqualAbsoluteRow1(f1, f2, allowedDifference) && |
| | 18 | 102 | | FuzzyEqualAbsoluteRow2(f1, f2, allowedDifference) && |
| | 18 | 103 | | FuzzyEqualAbsoluteRow3(f1, f2, allowedDifference) && |
| | 18 | 104 | | FuzzyEqualAbsoluteRow4(f1, f2, allowedDifference); |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Compares two Fixed4x4 for approximate equality, allowing a fractional percentage (defaults to ~1%) difference be |
| | | 108 | | /// </summary> |
| | | 109 | | /// <param name="f1">The current Fixed4x4.</param> |
| | | 110 | | /// <param name="f2">The Fixed4x4 to compare against.</param> |
| | | 111 | | /// <param name="percentage">The allowed fractional difference (percentage) for each component.</param> |
| | | 112 | | /// <returns>True if the components are within the allowed percentage difference, false otherwise.</returns> |
| | | 113 | | public static bool FuzzyEqual(this Fixed4x4 f1, Fixed4x4 f2, Fixed64? percentage = null) |
| | | 114 | | { |
| | 18 | 115 | | Fixed64 p = percentage ?? Fixed64.Epsilon; |
| | 18 | 116 | | return FuzzyEqualRow1(f1, f2, p) && |
| | 18 | 117 | | FuzzyEqualRow2(f1, f2, p) && |
| | 18 | 118 | | FuzzyEqualRow3(f1, f2, p) && |
| | 18 | 119 | | FuzzyEqualRow4(f1, f2, p); |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 123 | | private static bool FuzzyEqualAbsoluteRow1(Fixed4x4 f1, Fixed4x4 f2, Fixed64 allowedDifference) => |
| | 18 | 124 | | (f1.M11 - f2.M11).Abs() <= allowedDifference && |
| | 18 | 125 | | (f1.M12 - f2.M12).Abs() <= allowedDifference && |
| | 18 | 126 | | (f1.M13 - f2.M13).Abs() <= allowedDifference && |
| | 18 | 127 | | (f1.M14 - f2.M14).Abs() <= allowedDifference; |
| | | 128 | | |
| | | 129 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 130 | | private static bool FuzzyEqualAbsoluteRow2(Fixed4x4 f1, Fixed4x4 f2, Fixed64 allowedDifference) => |
| | 14 | 131 | | (f1.M21 - f2.M21).Abs() <= allowedDifference && |
| | 14 | 132 | | (f1.M22 - f2.M22).Abs() <= allowedDifference && |
| | 14 | 133 | | (f1.M23 - f2.M23).Abs() <= allowedDifference && |
| | 14 | 134 | | (f1.M24 - f2.M24).Abs() <= allowedDifference; |
| | | 135 | | |
| | | 136 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 137 | | private static bool FuzzyEqualAbsoluteRow3(Fixed4x4 f1, Fixed4x4 f2, Fixed64 allowedDifference) => |
| | 10 | 138 | | (f1.M31 - f2.M31).Abs() <= allowedDifference && |
| | 10 | 139 | | (f1.M32 - f2.M32).Abs() <= allowedDifference && |
| | 10 | 140 | | (f1.M33 - f2.M33).Abs() <= allowedDifference && |
| | 10 | 141 | | (f1.M34 - f2.M34).Abs() <= allowedDifference; |
| | | 142 | | |
| | | 143 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 144 | | private static bool FuzzyEqualAbsoluteRow4(Fixed4x4 f1, Fixed4x4 f2, Fixed64 allowedDifference) => |
| | 6 | 145 | | (f1.M41 - f2.M41).Abs() <= allowedDifference && |
| | 6 | 146 | | (f1.M42 - f2.M42).Abs() <= allowedDifference && |
| | 6 | 147 | | (f1.M43 - f2.M43).Abs() <= allowedDifference && |
| | 6 | 148 | | (f1.M44 - f2.M44).Abs() <= allowedDifference; |
| | | 149 | | |
| | | 150 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 151 | | private static bool FuzzyEqualRow1(Fixed4x4 f1, Fixed4x4 f2, Fixed64 percentage) => |
| | 18 | 152 | | f1.M11.FuzzyComponentEqual(f2.M11, percentage) && |
| | 18 | 153 | | f1.M12.FuzzyComponentEqual(f2.M12, percentage) && |
| | 18 | 154 | | f1.M13.FuzzyComponentEqual(f2.M13, percentage) && |
| | 18 | 155 | | f1.M14.FuzzyComponentEqual(f2.M14, percentage); |
| | | 156 | | |
| | | 157 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 158 | | private static bool FuzzyEqualRow2(Fixed4x4 f1, Fixed4x4 f2, Fixed64 percentage) => |
| | 14 | 159 | | f1.M21.FuzzyComponentEqual(f2.M21, percentage) && |
| | 14 | 160 | | f1.M22.FuzzyComponentEqual(f2.M22, percentage) && |
| | 14 | 161 | | f1.M23.FuzzyComponentEqual(f2.M23, percentage) && |
| | 14 | 162 | | f1.M24.FuzzyComponentEqual(f2.M24, percentage); |
| | | 163 | | |
| | | 164 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 165 | | private static bool FuzzyEqualRow3(Fixed4x4 f1, Fixed4x4 f2, Fixed64 percentage) => |
| | 10 | 166 | | f1.M31.FuzzyComponentEqual(f2.M31, percentage) && |
| | 10 | 167 | | f1.M32.FuzzyComponentEqual(f2.M32, percentage) && |
| | 10 | 168 | | f1.M33.FuzzyComponentEqual(f2.M33, percentage) && |
| | 10 | 169 | | f1.M34.FuzzyComponentEqual(f2.M34, percentage); |
| | | 170 | | |
| | | 171 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 172 | | private static bool FuzzyEqualRow4(Fixed4x4 f1, Fixed4x4 f2, Fixed64 percentage) => |
| | 6 | 173 | | f1.M41.FuzzyComponentEqual(f2.M41, percentage) && |
| | 6 | 174 | | f1.M42.FuzzyComponentEqual(f2.M42, percentage) && |
| | 6 | 175 | | f1.M43.FuzzyComponentEqual(f2.M43, percentage) && |
| | 6 | 176 | | f1.M44.FuzzyComponentEqual(f2.M44, percentage); |
| | | 177 | | |
| | | 178 | | #endregion |
| | | 179 | | } |