| | | 1 | | //======================================================================= |
| | | 2 | | // ExactMassWeight.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 System; |
| | | 9 | | |
| | | 10 | | using FixedMathSharp; |
| | | 11 | | |
| | | 12 | | namespace Gravitas.Colliders; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Represents a nonnegative relative mass-property measure without narrowing |
| | | 16 | | /// products or aggregate sums to <see cref="Fixed64"/>. |
| | | 17 | | /// </summary> |
| | | 18 | | internal readonly struct ExactMassWeight |
| | | 19 | | { |
| | | 20 | | internal readonly Signed320 Numerator; |
| | | 21 | | |
| | | 22 | | internal ExactMassWeight(Signed320 numerator) |
| | | 23 | | { |
| | 179106 | 24 | | Numerator = numerator; |
| | 179106 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary>Gets the zero relative weight.</summary> |
| | 1392 | 28 | | internal static ExactMassWeight Zero => default; |
| | | 29 | | |
| | | 30 | | /// <summary>Gets a unit relative weight.</summary> |
| | | 31 | | internal static ExactMassWeight One => |
| | 46 | 32 | | ExactMassProperties.CreateWeight(Fixed64.One); |
| | | 33 | | |
| | | 34 | | /// <summary>Gets whether this weight is exactly zero.</summary> |
| | 3936 | 35 | | internal bool IsZero => Numerator.IsZero; |
| | | 36 | | |
| | | 37 | | /// <summary>Creates a weight from one nonnegative measure.</summary> |
| | | 38 | | internal static ExactMassWeight FromMeasure(Fixed64 measure) => |
| | 29 | 39 | | ExactMassProperties.CreateWeight(measure); |
| | | 40 | | |
| | | 41 | | /// <summary>Creates a weight from an exact product of nonnegative factors.</summary> |
| | | 42 | | internal static ExactMassWeight FromProduct( |
| | | 43 | | Fixed64 first, |
| | | 44 | | Fixed64 second) => |
| | 1 | 45 | | ExactMassProperties.CreateWeight(first, second); |
| | | 46 | | |
| | | 47 | | /// <summary>Creates a weight from an exact product of nonnegative factors.</summary> |
| | | 48 | | internal static ExactMassWeight FromProduct( |
| | | 49 | | Fixed64 first, |
| | | 50 | | Fixed64 second, |
| | | 51 | | Fixed64 third) => |
| | 4801 | 52 | | ExactMassProperties.CreateWeight(first, second, third); |
| | | 53 | | |
| | | 54 | | /// <summary>Creates a weight from an exact product of nonnegative factors.</summary> |
| | | 55 | | internal static ExactMassWeight FromProduct( |
| | | 56 | | Fixed64 first, |
| | | 57 | | Fixed64 second, |
| | | 58 | | Fixed64 third, |
| | | 59 | | Fixed64 fourth) => |
| | 2559 | 60 | | ExactMassProperties.CreateWeight(first, second, third, fourth); |
| | | 61 | | |
| | | 62 | | /// <summary>Adds another nonnegative weight without scalar narrowing.</summary> |
| | | 63 | | /// <exception cref="OverflowException"> |
| | | 64 | | /// The exact aggregate exceeds the semantic weight domain. |
| | | 65 | | /// </exception> |
| | | 66 | | internal ExactMassWeight Add(ExactMassWeight other) |
| | | 67 | | { |
| | 3523 | 68 | | if (!TryAdd(other, out ExactMassWeight result)) |
| | | 69 | | { |
| | 1 | 70 | | throw new OverflowException( |
| | 1 | 71 | | "The aggregate mass-property weight is outside the semantic weight domain."); |
| | | 72 | | } |
| | | 73 | | |
| | 3522 | 74 | | return result; |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Attempts to add another nonnegative weight without scalar narrowing. |
| | | 79 | | /// </summary> |
| | | 80 | | internal bool TryAdd( |
| | | 81 | | ExactMassWeight other, |
| | | 82 | | out ExactMassWeight result) => |
| | 3714 | 83 | | ExactMassProperties.TryAdd(this, other, out result); |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Attempts to materialize the represented measure as <see cref="Fixed64"/>. |
| | | 87 | | /// </summary> |
| | | 88 | | internal bool TryGetMeasure(out Fixed64 measure) => |
| | 32 | 89 | | ExactMassProperties.TryGetMeasure(this, out measure); |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Attempts to distribute <paramref name="total"/> by this weight's exact |
| | | 93 | | /// share of <paramref name="totalWeight"/>. |
| | | 94 | | /// </summary> |
| | | 95 | | internal bool TryGetProportionalShare( |
| | | 96 | | Fixed64 total, |
| | | 97 | | ExactMassWeight totalWeight, |
| | | 98 | | out Fixed64 share) => |
| | 1995 | 99 | | ExactMassProperties.TryGetProportionalShare( |
| | 1995 | 100 | | this, |
| | 1995 | 101 | | total, |
| | 1995 | 102 | | totalWeight, |
| | 1995 | 103 | | out share); |
| | | 104 | | } |