| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Text.Json.Serialization; |
| | | 5 | | |
| | | 6 | | namespace FixedMathSharp; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Specifies the interpolation method used when evaluating a <see cref="FixedCurve"/>. |
| | | 10 | | /// </summary> |
| | | 11 | | public enum FixedCurveMode |
| | | 12 | | { |
| | | 13 | | /// <summary>Linear interpolation between keyframes.</summary> |
| | | 14 | | Linear, |
| | | 15 | | |
| | | 16 | | /// <summary>Step interpolation, instantly jumping between keyframe values.</summary> |
| | | 17 | | Step, |
| | | 18 | | |
| | | 19 | | /// <summary>Smooth interpolation using a cosine function (SmoothStep).</summary> |
| | | 20 | | Smooth, |
| | | 21 | | |
| | | 22 | | /// <summary>Cubic interpolation for smoother curves using tangents.</summary> |
| | | 23 | | Cubic |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// A deterministic fixed-point curve that interpolates values between keyframes. |
| | | 28 | | /// Used for animations, physics calculations, and procedural data. |
| | | 29 | | /// </summary> |
| | | 30 | | [Serializable] |
| | | 31 | | [MemoryPackable] |
| | | 32 | | public partial class FixedCurve : IEquatable<FixedCurve> |
| | | 33 | | { |
| | | 34 | | #region Constructors |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Initializes a new instance of the <see cref="FixedCurve"/> with a default linear interpolation mode. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="keyframes">The keyframes defining the curve.</param> |
| | | 40 | | public FixedCurve(params FixedCurveKey[] keyframes) |
| | 15 | 41 | | : this(FixedCurveMode.Linear, keyframes) { } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Initializes a new instance of the <see cref="FixedCurve"/> with a specified interpolation mode. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="mode">The interpolation method to use.</param> |
| | | 47 | | /// <param name="keyframes">The keyframes defining the curve.</param> |
| | | 48 | | [JsonConstructor] |
| | | 49 | | [MemoryPackConstructor] |
| | 20 | 50 | | public FixedCurve(FixedCurveMode mode, params FixedCurveKey[] keyframes) |
| | 20 | 51 | | { |
| | 20 | 52 | | Keyframes = keyframes?.Length > 1 |
| | 41 | 53 | | ? keyframes.OrderBy(k => k.Time).ToArray() |
| | 20 | 54 | | : keyframes?.Clone() as FixedCurveKey[] ?? Array.Empty<FixedCurveKey>(); |
| | 20 | 55 | | Mode = mode; |
| | 20 | 56 | | } |
| | | 57 | | |
| | | 58 | | #endregion |
| | | 59 | | |
| | | 60 | | #region Properties |
| | | 61 | | |
| | | 62 | | [JsonInclude] |
| | | 63 | | [MemoryPackOrder(0)] |
| | 43 | 64 | | public FixedCurveMode Mode { get; private set; } |
| | | 65 | | |
| | | 66 | | [JsonInclude] |
| | | 67 | | [MemoryPackOrder(1)] |
| | 174 | 68 | | public FixedCurveKey[] Keyframes { get; private set; } |
| | | 69 | | |
| | | 70 | | #endregion |
| | | 71 | | |
| | | 72 | | #region Methods |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Evaluates the curve at a given time using the specified interpolation mode. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <param name="time">The time at which to evaluate the curve.</param> |
| | | 78 | | /// <returns>The interpolated value at the given time.</returns> |
| | | 79 | | public Fixed64 Evaluate(Fixed64 time) |
| | 22 | 80 | | { |
| | 23 | 81 | | if (Keyframes.Length == 0) return Fixed64.One; |
| | | 82 | | |
| | | 83 | | // Clamp input within the keyframe range |
| | 28 | 84 | | if (time <= Keyframes[0].Time) return Keyframes[0].Value; |
| | 21 | 85 | | if (time >= Keyframes[^1].Time) return Keyframes[^1].Value; |
| | | 86 | | |
| | | 87 | | // Find the surrounding keyframes |
| | 20 | 88 | | for (int i = 0; i < Keyframes.Length - 1; i++) |
| | 10 | 89 | | { |
| | 10 | 90 | | if (time >= Keyframes[i].Time && time < Keyframes[i + 1].Time) |
| | 7 | 91 | | { |
| | | 92 | | // Compute interpolation factor |
| | 7 | 93 | | Fixed64 t = (time - Keyframes[i].Time) / (Keyframes[i + 1].Time - Keyframes[i].Time); |
| | | 94 | | |
| | | 95 | | // Choose interpolation method |
| | 7 | 96 | | return Mode switch |
| | 7 | 97 | | { |
| | 3 | 98 | | FixedCurveMode.Step => Keyframes[i].Value,// Immediate transition |
| | 1 | 99 | | FixedCurveMode.Smooth => FixedMath.SmoothStep(Keyframes[i].Value, Keyframes[i + 1].Value, t), |
| | 1 | 100 | | FixedCurveMode.Cubic => FixedMath.CubicInterpolate( |
| | 1 | 101 | | Keyframes[i].Value, Keyframes[i + 1].Value, |
| | 1 | 102 | | Keyframes[i].OutTangent, Keyframes[i + 1].InTangent, t), |
| | 2 | 103 | | _ => FixedMath.LinearInterpolate(Keyframes[i].Value, Keyframes[i + 1].Value, t), |
| | 7 | 104 | | }; |
| | | 105 | | } |
| | 3 | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | return Fixed64.One; // Fallback (should never be hit) |
| | 22 | 109 | | } |
| | | 110 | | |
| | | 111 | | #endregion |
| | | 112 | | |
| | | 113 | | #region Equality |
| | | 114 | | |
| | | 115 | | public bool Equals(FixedCurve? other) |
| | 8 | 116 | | { |
| | 10 | 117 | | if (other is null) return false; |
| | 6 | 118 | | if (ReferenceEquals(this, other)) return true; |
| | 6 | 119 | | return Mode == other.Mode && Keyframes.SequenceEqual(other.Keyframes); |
| | 8 | 120 | | } |
| | | 121 | | |
| | 2 | 122 | | public override bool Equals(object? obj) => obj is FixedCurve other && Equals(other); |
| | | 123 | | |
| | | 124 | | public override int GetHashCode() |
| | 2 | 125 | | { |
| | | 126 | | unchecked |
| | 2 | 127 | | { |
| | 2 | 128 | | int hash = (int)Mode; |
| | 14 | 129 | | foreach (var key in Keyframes) |
| | 4 | 130 | | hash = (hash * 31) ^ key.GetHashCode(); |
| | 2 | 131 | | return hash; |
| | | 132 | | } |
| | 2 | 133 | | } |
| | | 134 | | |
| | 7 | 135 | | public static bool operator ==(FixedCurve? left, FixedCurve? right) => left?.Equals(right) ?? right is null; |
| | | 136 | | |
| | 2 | 137 | | public static bool operator !=(FixedCurve? left, FixedCurve? right) => !(left == right); |
| | | 138 | | |
| | | 139 | | #endregion |
| | | 140 | | } |