| | | 1 | | //======================================================================= |
| | | 2 | | // MeshMassProperties.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using FixedMathSharp; |
| | | 9 | | using static Gravitas.Colliders.MeshCheckedMath; |
| | | 10 | | |
| | | 11 | | namespace Gravitas.Colliders; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Stores deterministic closed-volume mass properties for an immutable mesh topology. |
| | | 15 | | /// </summary> |
| | | 16 | | public readonly struct MeshMassProperties |
| | | 17 | | { |
| | | 18 | | /// <summary>Creates deterministic closed-volume mass properties.</summary> |
| | | 19 | | public MeshMassProperties( |
| | | 20 | | Fixed64 volume, |
| | | 21 | | Vector3d centerOfMass, |
| | | 22 | | Vector3d inertiaReferencePoint, |
| | | 23 | | Fixed3x3 unitMassInertiaTensor) |
| | | 24 | | { |
| | 180 | 25 | | Volume = volume; |
| | 180 | 26 | | CenterOfMass = centerOfMass; |
| | 180 | 27 | | InertiaReferencePoint = inertiaReferencePoint; |
| | 180 | 28 | | UnitMassInertiaTensor = unitMassInertiaTensor; |
| | 180 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Absolute solid volume in local mesh units. |
| | | 33 | | /// </summary> |
| | | 34 | | public Fixed64 Volume { get; } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Homogeneous center of mass in local mesh coordinates. |
| | | 38 | | /// </summary> |
| | | 39 | | public Vector3d CenterOfMass { get; } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Local reference point about which <see cref="UnitMassInertiaTensor"/> was computed. |
| | | 43 | | /// </summary> |
| | | 44 | | public Vector3d InertiaReferencePoint { get; } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Solid-volume inertia tensor for unit mass about <see cref="InertiaReferencePoint"/>. |
| | | 48 | | /// </summary> |
| | | 49 | | public Fixed3x3 UnitMassInertiaTensor { get; } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Calculates an inertia tensor for the supplied mass about a local point |
| | | 53 | | /// parallel to the mesh center-of-mass axes. |
| | | 54 | | /// </summary> |
| | | 55 | | public Fixed3x3 CalculateInertiaTensor(Fixed64 mass, Vector3d localReferencePoint) |
| | | 56 | | { |
| | 44 | 57 | | Fixed3x3 referenceTensor = UnitMassInertiaTensor * mass; |
| | 44 | 58 | | Fixed3x3 centerTensor = InertiaTensorMath.SubtractParallelAxisTensor( |
| | 44 | 59 | | referenceTensor, |
| | 44 | 60 | | mass, |
| | 44 | 61 | | InertiaReferencePoint - CenterOfMass); |
| | 44 | 62 | | return InertiaTensorMath.AddParallelAxisTensor(centerTensor, mass, localReferencePoint - CenterOfMass); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | internal MeshMassScaleResult TryScale(Vector3d scale, out MeshMassProperties properties) |
| | | 66 | | { |
| | 68 | 67 | | properties = default; |
| | 68 | 68 | | bool valid = TryMultiply(Volume, scale.X, out Fixed64 scaledVolume); |
| | 68 | 69 | | valid &= TryMultiply(scaledVolume, scale.Y, out scaledVolume); |
| | 68 | 70 | | valid &= TryMultiply(scaledVolume, scale.Z, out scaledVolume); |
| | 68 | 71 | | if (!valid) |
| | | 72 | | { |
| | 5 | 73 | | return MeshMassScaleResult.NonRepresentableVolume; |
| | | 74 | | } |
| | | 75 | | |
| | 63 | 76 | | valid = TrySubtract(InertiaReferencePoint, CenterOfMass, out Vector3d referenceOffset); |
| | 63 | 77 | | valid &= TryCreateParallelAxisTensor(referenceOffset, out Fixed3x3 referenceShift); |
| | 63 | 78 | | valid &= TrySubtract(UnitMassInertiaTensor, referenceShift, out Fixed3x3 centerTensor); |
| | 63 | 79 | | valid &= TryRecoverCovariance(centerTensor, out Fixed3x3 covariance); |
| | 63 | 80 | | valid &= TryScaleCovariance(covariance, scale, out Fixed3x3 scaledCovariance); |
| | 63 | 81 | | valid &= TryCreateInertiaFromCovariance(scaledCovariance, out Fixed3x3 scaledCenterTensor); |
| | 63 | 82 | | valid &= TryMultiply(CenterOfMass, scale, out Vector3d scaledCenter); |
| | 63 | 83 | | valid &= TryMultiply(InertiaReferencePoint, scale, out Vector3d scaledReference); |
| | 63 | 84 | | valid &= TrySubtract(scaledReference, scaledCenter, out Vector3d scaledReferenceOffset); |
| | 63 | 85 | | valid &= TryCreateParallelAxisTensor(scaledReferenceOffset, out Fixed3x3 scaledReferenceShift); |
| | 63 | 86 | | valid &= TryAdd(scaledCenterTensor, scaledReferenceShift, out Fixed3x3 scaledReferenceTensor); |
| | 63 | 87 | | if (!valid) |
| | | 88 | | { |
| | 5 | 89 | | return MeshMassScaleResult.NonRepresentableMassProperties; |
| | | 90 | | } |
| | | 91 | | |
| | 58 | 92 | | properties = new MeshMassProperties( |
| | 58 | 93 | | scaledVolume, |
| | 58 | 94 | | scaledCenter, |
| | 58 | 95 | | scaledReference, |
| | 58 | 96 | | scaledReferenceTensor); |
| | 58 | 97 | | return MeshMassScaleResult.Valid; |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | private static bool TryRecoverCovariance(Fixed3x3 tensor, out Fixed3x3 covariance) |
| | | 101 | | { |
| | 63 | 102 | | covariance = default; |
| | 63 | 103 | | bool valid = TryAdd(tensor.M22, tensor.M33, out Fixed64 xSum); |
| | 63 | 104 | | valid &= TrySubtract(xSum, tensor.M11, out Fixed64 xTwice); |
| | 63 | 105 | | valid &= TryMultiply(xTwice, Fixed64.Half, out Fixed64 x); |
| | 63 | 106 | | valid &= TryAdd(tensor.M11, tensor.M33, out Fixed64 ySum); |
| | 63 | 107 | | valid &= TrySubtract(ySum, tensor.M22, out Fixed64 yTwice); |
| | 63 | 108 | | valid &= TryMultiply(yTwice, Fixed64.Half, out Fixed64 y); |
| | 63 | 109 | | valid &= TryAdd(tensor.M11, tensor.M22, out Fixed64 zSum); |
| | 63 | 110 | | valid &= TrySubtract(zSum, tensor.M33, out Fixed64 zTwice); |
| | 63 | 111 | | valid &= TryMultiply(zTwice, Fixed64.Half, out Fixed64 z); |
| | 63 | 112 | | valid &= TryNegate(tensor.M12, out Fixed64 xy); |
| | 63 | 113 | | valid &= TryNegate(tensor.M13, out Fixed64 xz); |
| | 63 | 114 | | valid &= TryNegate(tensor.M23, out Fixed64 yz); |
| | 63 | 115 | | if (!valid) |
| | | 116 | | { |
| | 1 | 117 | | return false; |
| | | 118 | | } |
| | | 119 | | |
| | 62 | 120 | | covariance = new Fixed3x3(x, xy, xz, xy, y, yz, xz, yz, z); |
| | 62 | 121 | | return true; |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | private static bool TryScaleCovariance(Fixed3x3 covariance, Vector3d scale, out Fixed3x3 scaled) |
| | | 125 | | { |
| | 63 | 126 | | scaled = default; |
| | 63 | 127 | | bool valid = TryMultiply(scale.X, scale.X, out Fixed64 xxScale); |
| | 63 | 128 | | valid &= TryMultiply(scale.Y, scale.Y, out Fixed64 yyScale); |
| | 63 | 129 | | valid &= TryMultiply(scale.Z, scale.Z, out Fixed64 zzScale); |
| | 63 | 130 | | valid &= TryMultiply(scale.X, scale.Y, out Fixed64 xyScale); |
| | 63 | 131 | | valid &= TryMultiply(scale.X, scale.Z, out Fixed64 xzScale); |
| | 63 | 132 | | valid &= TryMultiply(scale.Y, scale.Z, out Fixed64 yzScale); |
| | 63 | 133 | | valid &= TryMultiply(covariance.M11, xxScale, out Fixed64 xx); |
| | 63 | 134 | | valid &= TryMultiply(covariance.M22, yyScale, out Fixed64 yy); |
| | 63 | 135 | | valid &= TryMultiply(covariance.M33, zzScale, out Fixed64 zz); |
| | 63 | 136 | | valid &= TryMultiply(covariance.M12, xyScale, out Fixed64 xy); |
| | 63 | 137 | | valid &= TryMultiply(covariance.M13, xzScale, out Fixed64 xz); |
| | 63 | 138 | | valid &= TryMultiply(covariance.M23, yzScale, out Fixed64 yz); |
| | 63 | 139 | | if (!valid) |
| | | 140 | | { |
| | 2 | 141 | | return false; |
| | | 142 | | } |
| | | 143 | | |
| | 61 | 144 | | scaled = new Fixed3x3(xx, xy, xz, xy, yy, yz, xz, yz, zz); |
| | 61 | 145 | | return true; |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | private static bool TryCreateInertiaFromCovariance(Fixed3x3 covariance, out Fixed3x3 tensor) |
| | | 149 | | { |
| | 63 | 150 | | tensor = default; |
| | 63 | 151 | | bool valid = TryAdd(covariance.M22, covariance.M33, out Fixed64 xx); |
| | 63 | 152 | | valid &= TryAdd(covariance.M11, covariance.M33, out Fixed64 yy); |
| | 63 | 153 | | valid &= TryAdd(covariance.M11, covariance.M22, out Fixed64 zz); |
| | 63 | 154 | | valid &= TryNegate(covariance.M12, out Fixed64 xy); |
| | 63 | 155 | | valid &= TryNegate(covariance.M13, out Fixed64 xz); |
| | 63 | 156 | | valid &= TryNegate(covariance.M23, out Fixed64 yz); |
| | 63 | 157 | | if (!valid) |
| | | 158 | | { |
| | 1 | 159 | | return false; |
| | | 160 | | } |
| | | 161 | | |
| | 62 | 162 | | tensor = new Fixed3x3(xx, xy, xz, xy, yy, yz, xz, yz, zz); |
| | 62 | 163 | | return true; |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | internal enum MeshMassScaleResult |
| | | 169 | | { |
| | | 170 | | Valid, |
| | | 171 | | NonRepresentableVolume, |
| | | 172 | | NonRepresentableMassProperties |
| | | 173 | | } |