| | | 1 | | //======================================================================= |
| | | 2 | | // Vector3d.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 FixedMathSharp.Bounds; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | |
| | | 11 | | namespace FixedMathSharp; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Provides extension methods for the Vector3d type to support additional vector operations, comparisons, and conversio |
| | | 15 | | /// </summary> |
| | | 16 | | public static partial class Vector3dExtensions |
| | | 17 | | { |
| | | 18 | | #region Vector3d Operations |
| | | 19 | | |
| | | 20 | | /// <inheritdoc cref="Vector3d.Clamp(Vector3d, Vector3d, Vector3d)" /> |
| | | 21 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 22 | | public static Vector3d Clamp(this Vector3d value, Vector3d min, Vector3d max) => Vector3d.Clamp(value, min, max); |
| | | 23 | | |
| | | 24 | | /// <inheritdoc cref="Vector3d.Clamp(Vector3d, Vector3d, Vector3d)" /> |
| | | 25 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 26 | | public static Vector3d ClampOne(this Vector3d value) => Vector3d.Clamp(value, Vector3d.Negative, Vector3d.One); |
| | | 27 | | |
| | | 28 | | /// <inheritdoc cref="Vector3d.ClampMagnitude(Vector3d, Fixed64)" /> |
| | | 29 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 30 | | public static Vector3d ClampMagnitude(this Vector3d value, Fixed64 maxMagnitude) => |
| | 1 | 31 | | Vector3d.ClampMagnitude(value, maxMagnitude); |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Checks if the distance between two vectors is less than or equal to a specified factor. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="me">The current vector.</param> |
| | | 37 | | /// <param name="other">The vector to compare distance to.</param> |
| | | 38 | | /// <param name="factor">The maximum allowable distance.</param> |
| | | 39 | | /// <returns>True if the distance between the vectors is less than or equal to the factor, false otherwise.</returns |
| | | 40 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 41 | | public static bool CheckDistance(this Vector3d me, Vector3d other, Fixed64 factor) |
| | | 42 | | { |
| | 2 | 43 | | if (factor < Fixed64.Zero) |
| | 1 | 44 | | return false; |
| | | 45 | | |
| | 1 | 46 | | return Vector3d.DistanceSquared(me, other) <= factor * factor; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc cref="Vector3d.Distance(Vector3d, Vector3d)" /> |
| | | 50 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 51 | | public static Fixed64 Distance(this Vector3d start, Vector3d end) => Vector3d.Distance(start, end); |
| | | 52 | | |
| | | 53 | | /// <inheritdoc cref="Vector3d.DistanceSquared(Vector3d, Vector3d)" /> |
| | | 54 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 55 | | public static Fixed64 DistanceSquared(this Vector3d start, Vector3d end) => Vector3d.DistanceSquared(start, end); |
| | | 56 | | |
| | | 57 | | /// <inheritdoc cref="Vector3d.Dot(Vector3d, Vector3d)" /> |
| | | 58 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 59 | | public static Fixed64 Dot(this Vector3d lhs, Vector3d rhs) => Vector3d.Dot(lhs, rhs); |
| | | 60 | | |
| | | 61 | | /// <inheritdoc cref="Vector3d.Cross(Vector3d, Vector3d)" /> |
| | | 62 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 63 | | public static Vector3d Cross(this Vector3d lhs, Vector3d rhs) => Vector3d.Cross(lhs, rhs); |
| | | 64 | | |
| | | 65 | | /// <inheritdoc cref="Vector3d.CrossProduct(Vector3d, Vector3d)" /> |
| | | 66 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 67 | | public static Fixed64 CrossProduct(this Vector3d lhs, Vector3d rhs) => Vector3d.CrossProduct(lhs, rhs); |
| | | 68 | | |
| | | 69 | | /// <inheritdoc cref="Vector3d.Lerp(Vector3d, Vector3d, Fixed64)" /> |
| | | 70 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 71 | | public static Vector3d Lerp(this Vector3d start, Vector3d end, Fixed64 amount) => Vector3d.Lerp(start, end, amount); |
| | | 72 | | |
| | | 73 | | /// <inheritdoc cref="Vector3d.UnclampedLerp(Vector3d, Vector3d, Fixed64)" /> |
| | | 74 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 75 | | public static Vector3d UnclampedLerp(this Vector3d start, Vector3d end, Fixed64 amount) => |
| | 1 | 76 | | Vector3d.UnclampedLerp(start, end, amount); |
| | | 77 | | |
| | | 78 | | /// <inheritdoc cref="Vector3d.Slerp(Vector3d, Vector3d, Fixed64)" /> |
| | | 79 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 80 | | public static Vector3d Slerp(this Vector3d start, Vector3d end, Fixed64 amount) => Vector3d.Slerp(start, end, amount |
| | | 81 | | |
| | | 82 | | /// <inheritdoc cref="Vector3d.Project(Vector3d, Vector3d)" /> |
| | | 83 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 84 | | public static Vector3d Project(this Vector3d vector, Vector3d onNormal) => Vector3d.Project(vector, onNormal); |
| | | 85 | | |
| | | 86 | | /// <inheritdoc cref="Vector3d.ProjectOnPlane(Vector3d, Vector3d)" /> |
| | | 87 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 88 | | public static Vector3d ProjectOnPlane(this Vector3d vector, Vector3d planeNormal) => |
| | 1 | 89 | | Vector3d.ProjectOnPlane(vector, planeNormal); |
| | | 90 | | |
| | | 91 | | /// <inheritdoc cref="Vector3d.ProjectOnPlane(Vector3d, FixedPlane)" /> |
| | | 92 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 93 | | public static Vector3d ProjectOnPlane(this Vector3d point, FixedPlane plane) => Vector3d.ProjectOnPlane(point, plane |
| | | 94 | | |
| | | 95 | | /// <inheritdoc cref="Vector3d.Angle(Vector3d, Vector3d)" /> |
| | | 96 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 97 | | public static Fixed64 Angle(this Vector3d from, Vector3d to) => Vector3d.Angle(from, to); |
| | | 98 | | |
| | | 99 | | /// <inheritdoc cref="Vector3d.Reflect(Vector3d, Vector3d)" /> |
| | | 100 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 101 | | public static Vector3d Reflect(this Vector3d vector, Vector3d normal) => Vector3d.Reflect(vector, normal); |
| | | 102 | | |
| | | 103 | | /// <inheritdoc cref="Vector3d.Rotate(Vector3d, Vector3d, FixedQuaternion)" /> |
| | | 104 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 105 | | public static Vector3d Rotate(this Vector3d source, Vector3d position, FixedQuaternion rotation) => |
| | 1 | 106 | | Vector3d.Rotate(source, position, rotation); |
| | | 107 | | |
| | | 108 | | /// <inheritdoc cref="Vector3d.InverseRotate(Vector3d, Vector3d, FixedQuaternion)" /> |
| | | 109 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 110 | | public static Vector3d InverseRotate(this Vector3d source, Vector3d position, FixedQuaternion rotation) => |
| | 1 | 111 | | Vector3d.InverseRotate(source, position, rotation); |
| | | 112 | | |
| | | 113 | | /// <inheritdoc cref="Vector3d.Transform(Vector3d, Fixed4x4)" /> |
| | | 114 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 115 | | public static Vector3d Transform(this Vector3d vector, Fixed4x4 matrix) => Vector3d.Transform(vector, matrix); |
| | | 116 | | |
| | | 117 | | #endregion |
| | | 118 | | |
| | | 119 | | #region Conversion |
| | | 120 | | |
| | | 121 | | /// <inheritdoc cref="Vector3d.ToDegrees(Vector3d)" /> |
| | | 122 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 123 | | public static Vector3d ToDegrees(this Vector3d radians) => Vector3d.ToDegrees(radians); |
| | | 124 | | |
| | | 125 | | /// <inheritdoc cref="Vector3d.ToRadians(Vector3d)" /> |
| | | 126 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 127 | | public static Vector3d ToRadians(this Vector3d degrees) => Vector3d.ToRadians(degrees); |
| | | 128 | | |
| | | 129 | | /// <inheritdoc cref="Vector3d.Abs(Vector3d)" /> |
| | | 130 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 131 | | public static Vector3d Abs(this Vector3d value) => Vector3d.Abs(value); |
| | | 132 | | |
| | | 133 | | /// <inheritdoc cref="Vector3d.Sign(Vector3d)" /> |
| | | 134 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 135 | | public static Vector3d Sign(this Vector3d value) => Vector3d.Sign(value); |
| | | 136 | | |
| | | 137 | | #endregion |
| | | 138 | | |
| | | 139 | | #region Equality |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Compares two vectors for approximate equality, allowing a fixed absolute difference. |
| | | 143 | | /// </summary> |
| | | 144 | | /// <param name="me">The current vector.</param> |
| | | 145 | | /// <param name="other">The vector to compare against.</param> |
| | | 146 | | /// <param name="allowedDifference">The allowed absolute difference between each component.</param> |
| | | 147 | | /// <returns>True if the components are within the allowed difference, false otherwise.</returns> |
| | | 148 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 149 | | public static bool FuzzyEqualAbsolute(this Vector3d me, Vector3d other, Fixed64 allowedDifference) => |
| | 6 | 150 | | (me.X - other.X).Abs() <= allowedDifference |
| | 6 | 151 | | && (me.Y - other.Y).Abs() <= allowedDifference |
| | 6 | 152 | | && (me.Z - other.Z).Abs() <= allowedDifference; |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Compares two vectors for approximate equality, allowing a fractional difference (percentage). |
| | | 156 | | /// Handles zero components by only using the allowed percentage difference. |
| | | 157 | | /// </summary> |
| | | 158 | | /// <param name="me">The current vector.</param> |
| | | 159 | | /// <param name="other">The vector to compare against.</param> |
| | | 160 | | /// <param name="percentage">The allowed fractional difference (percentage) for each component.</param> |
| | | 161 | | /// <returns>True if the components are within the allowed percentage difference, false otherwise.</returns> |
| | | 162 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 163 | | public static bool FuzzyEqual(this Vector3d me, Vector3d other, Fixed64? percentage = null) |
| | | 164 | | { |
| | 62 | 165 | | Fixed64 p = percentage ?? Fixed64.Epsilon; |
| | 62 | 166 | | return me.X.FuzzyComponentEqual(other.X, p) && |
| | 62 | 167 | | me.Y.FuzzyComponentEqual(other.Y, p) && |
| | 62 | 168 | | me.Z.FuzzyComponentEqual(other.Z, p); |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | #endregion |
| | | 172 | | } |