| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | |
| | | 3 | | namespace FixedMathSharp |
| | | 4 | | { |
| | | 5 | | public static partial class Vector3dExtensions |
| | | 6 | | { |
| | | 7 | | #region Vector3d Operations |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Clamps each component of the vector to the range [-1, 1] in place and returns the modified vector. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <param name="v">The vector to clamp.</param> |
| | | 13 | | /// <returns>The clamped vector with each component between -1 and 1.</returns> |
| | | 14 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 15 | | public static Vector3d ClampOneInPlace(this Vector3d v) |
| | 1 | 16 | | { |
| | 1 | 17 | | v.x = v.x.ClampOne(); |
| | 1 | 18 | | v.y = v.y.ClampOne(); |
| | 1 | 19 | | v.z = v.z.ClampOne(); |
| | 1 | 20 | | return v; |
| | 1 | 21 | | } |
| | | 22 | | |
| | | 23 | | public static Vector3d ClampMagnitude(this Vector3d value, Fixed64 maxMagnitude) |
| | 1 | 24 | | { |
| | 1 | 25 | | return Vector3d.ClampMagnitude(value, maxMagnitude); |
| | 1 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Checks if the distance between two vectors is less than or equal to a specified factor. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="me">The current vector.</param> |
| | | 32 | | /// <param name="other">The vector to compare distance to.</param> |
| | | 33 | | /// <param name="factor">The maximum allowable distance.</param> |
| | | 34 | | /// <returns>True if the distance between the vectors is less than or equal to the factor, false otherwise.</ret |
| | | 35 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 36 | | public static bool CheckDistance(this Vector3d me, Vector3d other, Fixed64 factor) |
| | 1 | 37 | | { |
| | 1 | 38 | | var dis = Vector3d.Distance(me, other); |
| | 1 | 39 | | return dis <= factor; |
| | 1 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc cref="Vector3d.Rotate(Vector3d, Vector3d, FixedQuaternion)" /> |
| | | 43 | | public static Vector3d Rotate(this Vector3d source, Vector3d position, FixedQuaternion rotation) |
| | 1 | 44 | | { |
| | 1 | 45 | | return Vector3d.Rotate(source, position, rotation); |
| | 1 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc cref="Vector3d.InverseRotate(Vector3d, Vector3d, FixedQuaternion)" /> |
| | | 49 | | public static Vector3d InverseRotate(this Vector3d source, Vector3d position, FixedQuaternion rotation) |
| | 1 | 50 | | { |
| | 1 | 51 | | return Vector3d.InverseRotate(source, position, rotation); |
| | 1 | 52 | | } |
| | | 53 | | |
| | | 54 | | #endregion |
| | | 55 | | |
| | | 56 | | #region Conversion |
| | | 57 | | |
| | | 58 | | /// <inheritdoc cref="Vector3d.ToDegrees(Vector3d)" /> |
| | | 59 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 60 | | public static Vector3d ToDegrees(this Vector3d radians) |
| | 1 | 61 | | { |
| | 1 | 62 | | return Vector3d.ToDegrees(radians); |
| | 1 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <inheritdoc cref="Vector3d.ToRadians(Vector3d)" /> |
| | | 66 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 67 | | public static Vector3d ToRadians(this Vector3d degrees) |
| | 1 | 68 | | { |
| | 1 | 69 | | return Vector3d.ToRadians(degrees); |
| | 1 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <inheritdoc cref="Vector3d.Abs(Vector3d)" /> |
| | | 73 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 74 | | public static Vector3d Abs(this Vector3d value) |
| | 1 | 75 | | { |
| | 1 | 76 | | return Vector3d.Abs(value); |
| | 1 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <inheritdoc cref="Vector3d.Sign(Vector3d)" /> |
| | | 80 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 81 | | public static Vector3d Sign(Vector3d value) |
| | 1 | 82 | | { |
| | 1 | 83 | | return Vector3d.Sign(value); |
| | 1 | 84 | | } |
| | | 85 | | |
| | | 86 | | #endregion |
| | | 87 | | |
| | | 88 | | #region Equality |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Compares two vectors for approximate equality, allowing a fixed absolute difference. |
| | | 92 | | /// </summary> |
| | | 93 | | /// <param name="me">The current vector.</param> |
| | | 94 | | /// <param name="other">The vector to compare against.</param> |
| | | 95 | | /// <param name="allowedDifference">The allowed absolute difference between each component.</param> |
| | | 96 | | /// <returns>True if the components are within the allowed difference, false otherwise.</returns> |
| | | 97 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 98 | | public static bool FuzzyEqualAbsolute(this Vector3d me, Vector3d other, Fixed64 allowedDifference) |
| | 3 | 99 | | { |
| | 3 | 100 | | return (me.x - other.x).Abs() <= allowedDifference && |
| | 3 | 101 | | (me.y - other.y).Abs() <= allowedDifference && |
| | 3 | 102 | | (me.z - other.z).Abs() <= allowedDifference; |
| | 3 | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Compares two vectors for approximate equality, allowing a fractional difference (percentage). |
| | | 107 | | /// Handles zero components by only using the allowed percentage difference. |
| | | 108 | | /// </summary> |
| | | 109 | | /// <param name="me">The current vector.</param> |
| | | 110 | | /// <param name="other">The vector 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 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 114 | | public static bool FuzzyEqual(this Vector3d me, Vector3d other, Fixed64? percentage = null) |
| | 25 | 115 | | { |
| | 25 | 116 | | Fixed64 p = percentage ?? Fixed64.Epsilon; |
| | 25 | 117 | | return me.x.FuzzyComponentEqual(other.x, p) && |
| | 25 | 118 | | me.y.FuzzyComponentEqual(other.y, p) && |
| | 25 | 119 | | me.z.FuzzyComponentEqual(other.z, p); |
| | 25 | 120 | | } |
| | | 121 | | |
| | | 122 | | #endregion |
| | | 123 | | } |
| | | 124 | | } |