| | | 1 | | //======================================================================= |
| | | 2 | | // ExactMassPoint2D.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 2D mass-property point that may lie outside the scalar |
| | | 16 | | /// coordinate domain while remaining available to aggregate operations. |
| | | 17 | | /// </summary> |
| | | 18 | | internal readonly struct ExactMassPoint2D |
| | | 19 | | { |
| | | 20 | | internal readonly Signed320 XNumerator; |
| | | 21 | | internal readonly Signed320 YNumerator; |
| | | 22 | | |
| | | 23 | | internal ExactMassPoint2D( |
| | | 24 | | Signed320 xNumerator, |
| | | 25 | | Signed320 yNumerator) |
| | | 26 | | { |
| | 48882 | 27 | | XNumerator = xNumerator; |
| | 48882 | 28 | | YNumerator = yNumerator; |
| | 48882 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary>Creates a mass point from a representable vector.</summary> |
| | | 32 | | internal static ExactMassPoint2D FromPoint(Vector2d point) => |
| | 713 | 33 | | ExactMassProperties.CreatePoint(point); |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Creates the exact planar composition |
| | | 37 | | /// <c>outerPoint * outerScale + innerOffset * innerScale + |
| | | 38 | | /// Rotate(innerDisplacement, innerRotation)</c>. |
| | | 39 | | /// </summary> |
| | | 40 | | internal static ExactMassPoint2D CreateScaledLocalComposition( |
| | | 41 | | Vector2d outerPoint, |
| | | 42 | | Vector2d outerScale, |
| | | 43 | | Vector2d innerOffset, |
| | | 44 | | Vector2d innerScale, |
| | | 45 | | Vector2d innerDisplacement, |
| | | 46 | | Fixed64 innerRotation) => |
| | 48169 | 47 | | ExactMassProperties.CreatePoint( |
| | 48169 | 48 | | outerPoint, |
| | 48169 | 49 | | outerScale, |
| | 48169 | 50 | | innerOffset, |
| | 48169 | 51 | | innerScale, |
| | 48169 | 52 | | innerDisplacement, |
| | 48169 | 53 | | innerRotation); |
| | | 54 | | |
| | | 55 | | /// <summary>Attempts to materialize this point as a Q32.32 vector.</summary> |
| | | 56 | | internal bool TryGetPoint(out Vector2d point) => |
| | 30001 | 57 | | ExactMassProperties.TryGetPoint(this, out point); |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Attempts to obtain the exact positive-weight average of semantic mass |
| | | 61 | | /// points with one final conversion per component. |
| | | 62 | | /// </summary> |
| | | 63 | | internal static bool TryGetWeightedAverage( |
| | | 64 | | ReadOnlySpan<ExactMassPoint2D> points, |
| | | 65 | | ReadOnlySpan<ExactMassWeight> weights, |
| | | 66 | | out Vector2d average) => |
| | 716 | 67 | | ExactMassProperties.TryGetWeightedAverage( |
| | 716 | 68 | | points, |
| | 716 | 69 | | weights, |
| | 716 | 70 | | out average); |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Attempts to add this point's parallel-axis contribution to a scalar |
| | | 74 | | /// moment about <paramref name="referencePoint"/>. |
| | | 75 | | /// </summary> |
| | | 76 | | internal bool TryAddParallelAxisMoment( |
| | | 77 | | Fixed64 centerMoment, |
| | | 78 | | Fixed64 mass, |
| | | 79 | | Vector2d referencePoint, |
| | | 80 | | out Fixed64 moment) => |
| | 17485 | 81 | | ExactMassProperties.TryAddParallelAxisMoment( |
| | 17485 | 82 | | this, |
| | 17485 | 83 | | centerMoment, |
| | 17485 | 84 | | mass, |
| | 17485 | 85 | | referencePoint, |
| | 17485 | 86 | | out moment); |
| | | 87 | | } |