| | | 1 | | //======================================================================= |
| | | 2 | | // ExactMassPoint3D.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 3D mass-property point that may lie outside the scalar |
| | | 16 | | /// coordinate domain while remaining available to aggregate operations. |
| | | 17 | | /// </summary> |
| | | 18 | | internal readonly struct ExactMassPoint3D |
| | | 19 | | { |
| | | 20 | | internal readonly Signed320 XNumerator; |
| | | 21 | | internal readonly Signed320 YNumerator; |
| | | 22 | | internal readonly Signed320 ZNumerator; |
| | | 23 | | |
| | | 24 | | internal ExactMassPoint3D( |
| | | 25 | | Signed320 xNumerator, |
| | | 26 | | Signed320 yNumerator, |
| | | 27 | | Signed320 zNumerator) |
| | | 28 | | { |
| | 67530 | 29 | | XNumerator = xNumerator; |
| | 67530 | 30 | | YNumerator = yNumerator; |
| | 67530 | 31 | | ZNumerator = zNumerator; |
| | 67530 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary>Creates a mass point from a representable vector.</summary> |
| | | 35 | | internal static ExactMassPoint3D FromPoint(Vector3d point) => |
| | 667 | 36 | | ExactMassProperties.CreatePoint(point); |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Creates the scale-invariant quaternion composition |
| | | 40 | | /// <c>outerPoint * outerScale + innerOffset * innerScale + |
| | | 41 | | /// innerRotation * innerDisplacement</c>. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <remarks> |
| | | 44 | | /// The quaternion rotation uses the same rational basis as full-domain |
| | | 45 | | /// geometry and retains 64 fractional guard bits for later aggregation. |
| | | 46 | | /// </remarks> |
| | | 47 | | /// <exception cref="ArgumentException"> |
| | | 48 | | /// <paramref name="innerRotation"/> is not normalized. |
| | | 49 | | /// </exception> |
| | | 50 | | internal static ExactMassPoint3D CreateScaledLocalComposition( |
| | | 51 | | Vector3d outerPoint, |
| | | 52 | | Vector3d outerScale, |
| | | 53 | | Vector3d innerOffset, |
| | | 54 | | Vector3d innerScale, |
| | | 55 | | Vector3d innerDisplacement, |
| | | 56 | | FixedQuaternion innerRotation) |
| | | 57 | | { |
| | 66864 | 58 | | if (!innerRotation.IsNormalized()) |
| | | 59 | | { |
| | 1 | 60 | | throw new ArgumentException( |
| | 1 | 61 | | "The mass-point rotation must be normalized.", |
| | 1 | 62 | | nameof(innerRotation)); |
| | | 63 | | } |
| | | 64 | | |
| | 66863 | 65 | | return ExactMassProperties.CreatePoint( |
| | 66863 | 66 | | outerPoint, |
| | 66863 | 67 | | outerScale, |
| | 66863 | 68 | | innerOffset, |
| | 66863 | 69 | | innerScale, |
| | 66863 | 70 | | innerDisplacement, |
| | 66863 | 71 | | innerRotation); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary>Attempts to materialize this point as a Q32.32 vector.</summary> |
| | | 75 | | internal bool TryGetPoint(out Vector3d point) => |
| | 55855 | 76 | | ExactMassProperties.TryGetPoint(this, out point); |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Attempts to obtain the exact positive-weight average of semantic mass |
| | | 80 | | /// points with one final conversion per component. |
| | | 81 | | /// </summary> |
| | | 82 | | internal static bool TryGetWeightedAverage( |
| | | 83 | | ReadOnlySpan<ExactMassPoint3D> points, |
| | | 84 | | ReadOnlySpan<ExactMassWeight> weights, |
| | | 85 | | out Vector3d average) => |
| | 671 | 86 | | ExactMassProperties.TryGetWeightedAverage( |
| | 671 | 87 | | points, |
| | 671 | 88 | | weights, |
| | 671 | 89 | | out average); |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Attempts to add this point's parallel-axis contribution to a tensor |
| | | 93 | | /// about <paramref name="referencePoint"/>. |
| | | 94 | | /// </summary> |
| | | 95 | | internal bool TryAddParallelAxisTensor( |
| | | 96 | | Fixed3x3 centerTensor, |
| | | 97 | | Fixed64 mass, |
| | | 98 | | Vector3d referencePoint, |
| | | 99 | | out Fixed3x3 tensor) => |
| | 10383 | 100 | | ExactMassProperties.TryAddParallelAxisTensor( |
| | 10383 | 101 | | this, |
| | 10383 | 102 | | centerTensor, |
| | 10383 | 103 | | mass, |
| | 10383 | 104 | | referencePoint, |
| | 10383 | 105 | | out tensor); |
| | | 106 | | } |