| | | 1 | | //======================================================================= |
| | | 2 | | // FixedCurve.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 MemoryPack; |
| | | 9 | | using System; |
| | | 10 | | using System.Text.Json.Serialization; |
| | | 11 | | |
| | | 12 | | namespace FixedMathSharp; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// A deterministic fixed-point curve that interpolates values between keyframes. |
| | | 16 | | /// Used for animations, physics calculations, and procedural data. |
| | | 17 | | /// </summary> |
| | | 18 | | [Serializable] |
| | | 19 | | [MemoryPackable] |
| | | 20 | | public partial struct FixedCurve : IEquatable<FixedCurve> |
| | | 21 | | { |
| | 1 | 22 | | private static readonly Comparison<FixedCurveKey> CompareKeyframesByTime = (left, right) => left.Time.CompareTo(righ |
| | | 23 | | |
| | | 24 | | #region Constructors |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a new instance of the <see cref="FixedCurve"/> with a default linear interpolation mode. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="keyframes">The keyframes defining the curve.</param> |
| | | 30 | | public FixedCurve(params FixedCurveKey[] keyframes) |
| | 10 | 31 | | : this(FixedCurveMode.Linear, keyframes) { } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Initializes a new instance of the <see cref="FixedCurve"/> with a specified interpolation mode. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="mode">The interpolation method to use.</param> |
| | | 37 | | /// <param name="keyframes">The keyframes defining the curve.</param> |
| | | 38 | | [JsonConstructor] |
| | | 39 | | [MemoryPackConstructor] |
| | | 40 | | public FixedCurve(FixedCurveMode mode, params FixedCurveKey[] keyframes) |
| | | 41 | | { |
| | 22 | 42 | | if (keyframes is null) |
| | 1 | 43 | | Keyframes = Array.Empty<FixedCurveKey>(); |
| | | 44 | | else |
| | | 45 | | { |
| | 21 | 46 | | if (keyframes.Length > 1) |
| | 19 | 47 | | Array.Sort(keyframes, CompareKeyframesByTime); |
| | | 48 | | |
| | 21 | 49 | | Keyframes = keyframes; |
| | | 50 | | } |
| | | 51 | | |
| | 22 | 52 | | Mode = mode; |
| | 22 | 53 | | } |
| | | 54 | | |
| | | 55 | | #endregion |
| | | 56 | | |
| | | 57 | | #region Properties |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets the mode used for the fixed curve calculation. |
| | | 61 | | /// </summary> |
| | | 62 | | [JsonInclude] |
| | | 63 | | [MemoryPackOrder(0)] |
| | | 64 | | [MemoryPackAllowSerialize] |
| | | 65 | | public FixedCurveMode Mode { get; private set; } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets the collection of keyframes that define the curve. |
| | | 69 | | /// </summary> |
| | | 70 | | [JsonInclude] |
| | | 71 | | [MemoryPackOrder(1)] |
| | | 72 | | public FixedCurveKey[] Keyframes { get; private set; } |
| | | 73 | | |
| | | 74 | | #endregion |
| | | 75 | | |
| | | 76 | | #region Methods |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Evaluates the curve at a given time using the specified interpolation mode. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="time">The time at which to evaluate the curve.</param> |
| | | 82 | | /// <returns>The interpolated value at the given time.</returns> |
| | | 83 | | public Fixed64 Evaluate(Fixed64 time) |
| | | 84 | | { |
| | 24 | 85 | | if (Keyframes.Length == 0) return Fixed64.One; |
| | | 86 | | |
| | | 87 | | // Clamp input within the keyframe range |
| | 29 | 88 | | if (time <= Keyframes[0].Time) return Keyframes[0].Value; |
| | 22 | 89 | | if (time >= Keyframes[^1].Time) return Keyframes[^1].Value; |
| | | 90 | | |
| | | 91 | | // Find the surrounding keyframes. The constructor sorts keyframes, and the range checks above guarantee |
| | | 92 | | // that an interior time belongs to one of these segments. |
| | 8 | 93 | | int segmentIndex = Keyframes.Length - 2; |
| | 24 | 94 | | for (int i = 0; i < Keyframes.Length - 1; i++) |
| | | 95 | | { |
| | 12 | 96 | | if (time < Keyframes[i + 1].Time) |
| | | 97 | | { |
| | 8 | 98 | | segmentIndex = i; |
| | 8 | 99 | | break; |
| | | 100 | | } |
| | | 101 | | } |
| | | 102 | | |
| | 8 | 103 | | FixedCurveKey current = Keyframes[segmentIndex]; |
| | 8 | 104 | | FixedCurveKey next = Keyframes[segmentIndex + 1]; |
| | 8 | 105 | | Fixed64 t = (time - current.Time) / (next.Time - current.Time); |
| | | 106 | | |
| | 8 | 107 | | return Mode switch |
| | 8 | 108 | | { |
| | 3 | 109 | | FixedCurveMode.Step => current.Value, |
| | 1 | 110 | | FixedCurveMode.Smooth => FixedMath.SmoothStep(current.Value, next.Value, t), |
| | 1 | 111 | | FixedCurveMode.Cubic => FixedMath.CubicInterpolate(current.Value, next.Value, current.OutTangent, next.InTan |
| | 3 | 112 | | _ => FixedMath.Lerp(current.Value, next.Value, t), |
| | 8 | 113 | | }; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | #endregion |
| | | 117 | | |
| | | 118 | | #region Equality |
| | | 119 | | |
| | | 120 | | /// <inheritdoc/> |
| | | 121 | | public bool Equals(FixedCurve other) |
| | | 122 | | { |
| | 8 | 123 | | if (Mode != other.Mode || Keyframes.Length != other.Keyframes.Length) |
| | 1 | 124 | | return false; |
| | | 125 | | |
| | 44 | 126 | | for (int i = 0; i < Keyframes.Length; i++) |
| | | 127 | | { |
| | 16 | 128 | | if (Keyframes[i] != other.Keyframes[i]) |
| | 1 | 129 | | return false; |
| | | 130 | | } |
| | | 131 | | |
| | 6 | 132 | | return true; |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <inheritdoc/> |
| | 3 | 136 | | public override bool Equals(object? obj) => obj is FixedCurve other && Equals(other); |
| | | 137 | | |
| | | 138 | | /// <inheritdoc/> |
| | | 139 | | public override int GetHashCode() |
| | | 140 | | { |
| | | 141 | | unchecked |
| | | 142 | | { |
| | 2 | 143 | | int hash = (int)Mode; |
| | 12 | 144 | | foreach (var key in Keyframes) |
| | 4 | 145 | | hash = (hash * 31) ^ key.GetHashCode(); |
| | 2 | 146 | | return hash; |
| | | 147 | | } |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Determines whether two FixedCurve instances are equal. |
| | | 152 | | /// </summary> |
| | 3 | 153 | | public static bool operator ==(FixedCurve left, FixedCurve right) => left.Equals(right); |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Determines whether two FixedCurve instances are not equal. |
| | | 157 | | /// </summary> |
| | 2 | 158 | | public static bool operator !=(FixedCurve left, FixedCurve right) => !(left == right); |
| | | 159 | | |
| | | 160 | | #endregion |
| | | 161 | | } |