| | | 1 | | //======================================================================= |
| | | 2 | | // FixedCurveKey.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 | | /// Represents a keyframe in a <see cref="FixedCurve"/>, defining a value at a specific time. |
| | | 16 | | /// </summary> |
| | | 17 | | [Serializable] |
| | | 18 | | [MemoryPackable] |
| | | 19 | | public partial struct FixedCurveKey : IEquatable<FixedCurveKey> |
| | | 20 | | { |
| | | 21 | | #region Fields |
| | | 22 | | |
| | | 23 | | /// <summary>The time at which this keyframe occurs.</summary> |
| | | 24 | | [JsonInclude] |
| | | 25 | | [MemoryPackOrder(0)] |
| | | 26 | | public Fixed64 Time; |
| | | 27 | | |
| | | 28 | | /// <summary>The value of the curve at this keyframe.</summary> |
| | | 29 | | [JsonInclude] |
| | | 30 | | [MemoryPackOrder(1)] |
| | | 31 | | public Fixed64 Value; |
| | | 32 | | |
| | | 33 | | /// <summary>The incoming tangent for cubic interpolation.</summary> |
| | | 34 | | [JsonInclude] |
| | | 35 | | [MemoryPackOrder(2)] |
| | | 36 | | public Fixed64 InTangent; |
| | | 37 | | |
| | | 38 | | /// <summary>The outgoing tangent for cubic interpolation.</summary> |
| | | 39 | | [JsonInclude] |
| | | 40 | | [MemoryPackOrder(3)] |
| | | 41 | | public Fixed64 OutTangent; |
| | | 42 | | |
| | | 43 | | #endregion |
| | | 44 | | |
| | | 45 | | #region Constructors |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Creates a keyframe with a specified time and value. |
| | | 49 | | /// </summary> |
| | | 50 | | public FixedCurveKey(Fixed64 time, Fixed64 value) |
| | 6 | 51 | | : this(time, value, Fixed64.Zero, Fixed64.Zero) { } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Creates a keyframe with a specified time and value, using integer parameters for convenience. |
| | | 55 | | /// </summary> |
| | | 56 | | public FixedCurveKey(int time, int value) |
| | 74 | 57 | | : this(new(time), new(value), Fixed64.Zero, Fixed64.Zero) { } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Creates a keyframe with optional tangents for cubic interpolation. |
| | | 61 | | /// </summary> |
| | | 62 | | [JsonConstructor] |
| | | 63 | | public FixedCurveKey(Fixed64 time, Fixed64 value, Fixed64 inTangent, Fixed64 outTangent) |
| | | 64 | | { |
| | 49 | 65 | | Time = time; |
| | 49 | 66 | | Value = value; |
| | 49 | 67 | | InTangent = inTangent; |
| | 49 | 68 | | OutTangent = outTangent; |
| | 49 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Creates a keyframe with optional tangents for cubic interpolation, using integer parameters for convenience. |
| | | 73 | | /// </summary> |
| | | 74 | | public FixedCurveKey(int time, int value, int inTangent, int outTangent) |
| | 4 | 75 | | : this(new Fixed64(time), new Fixed64(value), new Fixed64(inTangent), new Fixed64(outTangent)) { } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Creates a keyframe with a specified time and value. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <remarks> |
| | | 81 | | /// Values are converted through <see cref="Fixed64.FromDouble(double)"/>, so non-finite values |
| | | 82 | | /// throw <see cref="ArgumentOutOfRangeException"/> and finite values outside the Q32.32 range |
| | | 83 | | /// throw <see cref="OverflowException"/>. |
| | | 84 | | /// </remarks> |
| | | 85 | | public static FixedCurveKey FromDouble(double time, double value) => |
| | 2 | 86 | | new(Fixed64.FromDouble(time), Fixed64.FromDouble(value)); |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Creates a keyframe with optional tangents for cubic interpolation. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <remarks> |
| | | 92 | | /// Values are converted through <see cref="Fixed64.FromDouble(double)"/>, so non-finite values |
| | | 93 | | /// throw <see cref="ArgumentOutOfRangeException"/> and finite values outside the Q32.32 range |
| | | 94 | | /// throw <see cref="OverflowException"/>. |
| | | 95 | | /// </remarks> |
| | | 96 | | public static FixedCurveKey FromDouble(double time, double value, double inTangent, double outTangent) => |
| | 3 | 97 | | new(Fixed64.FromDouble(time), |
| | 3 | 98 | | Fixed64.FromDouble(value), |
| | 3 | 99 | | Fixed64.FromDouble(inTangent), |
| | 3 | 100 | | Fixed64.FromDouble(outTangent)); |
| | | 101 | | |
| | | 102 | | #endregion |
| | | 103 | | |
| | | 104 | | #region Equality |
| | | 105 | | |
| | | 106 | | /// <inheritdoc/> |
| | | 107 | | public bool Equals(FixedCurveKey other) => |
| | 19 | 108 | | Time == other.Time && |
| | 19 | 109 | | Value == other.Value && |
| | 19 | 110 | | InTangent == other.InTangent && |
| | 19 | 111 | | OutTangent == other.OutTangent; |
| | | 112 | | |
| | | 113 | | /// <inheritdoc/> |
| | 2 | 114 | | public override bool Equals(object? obj) => obj is FixedCurveKey other && Equals(other); |
| | | 115 | | |
| | | 116 | | /// <inheritdoc/> |
| | | 117 | | public override int GetHashCode() |
| | | 118 | | { |
| | | 119 | | unchecked |
| | | 120 | | { |
| | 7 | 121 | | int hash = 17; |
| | 7 | 122 | | hash = (hash * 31) + Time.GetHashCode(); |
| | 7 | 123 | | hash = (hash * 31) + Value.GetHashCode(); |
| | 7 | 124 | | hash = (hash * 31) + InTangent.GetHashCode(); |
| | 7 | 125 | | hash = (hash * 31) + OutTangent.GetHashCode(); |
| | 7 | 126 | | return hash; |
| | | 127 | | } |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Determines whether two FixedCurveKey instances are equal. |
| | | 132 | | /// </summary> |
| | 18 | 133 | | public static bool operator ==(FixedCurveKey left, FixedCurveKey right) => left.Equals(right); |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Determines whether two FixedCurveKey instances are not equal. |
| | | 137 | | /// </summary> |
| | 17 | 138 | | public static bool operator !=(FixedCurveKey left, FixedCurveKey right) => !(left == right); |
| | | 139 | | |
| | | 140 | | #endregion |
| | | 141 | | } |