< Summary

Information
Class: Gravitas.Colliders.ColliderCanonicalBounds
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/Colliders/Geometry/ColliderCanonicalBounds.cs
Line coverage
100%
Covered lines: 237
Uncovered lines: 0
Coverable lines: 237
Total lines: 379
Line coverage: 100%
Branch coverage
100%
Covered branches: 68
Total branches: 68
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/Gravitas/Gravitas/src/Gravitas/Colliders/Geometry/ColliderCanonicalBounds.cs

#LineLine coverage
 1//=======================================================================
 2// ColliderCanonicalBounds.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
 8using FixedMathSharp;
 9using FixedMathSharp.Geometry;
 10
 11namespace Gravitas.Colliders;
 12
 13/// <summary>
 14/// Contains methods for computing canonical bounds of colliders,
 15/// including proxy radii and relative bounds, based on collider type and properties.
 16/// </summary>
 17internal static class ColliderCanonicalBounds
 18{
 19    internal static Fixed64 GetCurrentCenteredProxyRadius(
 20        LSCollider collider)
 21    {
 22        switch (collider)
 23        {
 24            case LSSphereCollider:
 225                return collider.GetCurrentScaledRadius();
 26            case LSCapsuleCollider capsule:
 227                return GetCurrentCapsuleCenteredProxyRadius(capsule);
 28            case LSCuboidCollider cuboid:
 329                return GetCurrentCuboidCenteredProxyRadius(cuboid);
 30            case LSCylinderCollider cylinder:
 231                return GetCurrentFiniteAxisCenteredProxyRadius(
 232                    cylinder,
 233                    cylinder.Size.Y);
 34            case LSConeCollider cone:
 135                return GetCurrentFiniteAxisCenteredProxyRadius(
 136                    cone,
 137                    cone.Size.Y);
 38            case LSMeshCollider mesh:
 39                {
 240                    mesh.GetCurrentShapeScales(
 241                        out Vector3d ownerScale,
 242                        out Vector3d partScale);
 243                    return mesh.GeometryMesh.GetScaledLocalRadius(
 244                        ownerScale,
 245                        partScale);
 46                }
 47            case LSCompoundCollider compound:
 648                return GetCurrentCompoundCenteredProxyRadius(
 649                    compound);
 50            default:
 151                return Fixed64.MaxValue;
 52        }
 53    }
 54
 55    internal static FixedBoundBox GetRelativeBounds(
 56        LSCollider collider,
 57        Vector3d referenceOrigin,
 58        FixedQuaternion referenceRotation = default)
 59    {
 6950360        if (referenceRotation == default)
 4745461            referenceRotation = FixedQuaternion.Identity;
 6950362        if (collider is LSCompoundCollider compound)
 63        {
 2016464            return GetCompoundRelativeBounds(
 2016465                compound,
 2016466                referenceOrigin,
 2016467                referenceRotation);
 68        }
 4933969        if (collider is LSMeshCollider affine && !affine.Mesh.GeometryFrame.IsRigid)
 70        {
 7071            PhysicsMesh geometry = affine.Mesh;
 72            // Enclose the exact authored map directly in the requested physical
 73            // frame. Rounding inspection vertices before this transform can
 74            // shrink the source enclosure by a lattice unit.
 7075            bool represented = geometry.GeometryFrame.TryGetBoundsIn(geometry.GeometryLocalBounds.Min,
 7076                geometry.GeometryLocalBounds.Max, ScaledRigidFrame3d.Rigid(referenceOrigin, referenceRotation),
 7077                out FixedBoundBox bounds);
 78            System.Diagnostics.Debug.Assert(represented);
 7079            return bounds;
 80        }
 4926981        if (!TryGetLocalBounds(
 4926982            collider,
 4926983            out Vector3d localMin,
 4926984            out Vector3d localMax))
 85        {
 186            return FixedBoundBox.FromMinMax(
 187                new Vector3d(
 188                    Fixed64.MinValue,
 189                    Fixed64.MinValue,
 190                    Fixed64.MinValue),
 191                new Vector3d(
 192                    Fixed64.MaxValue,
 193                    Fixed64.MaxValue,
 194                    Fixed64.MaxValue));
 95        }
 4926896        Vector3d sourceOrigin = collider is LSMeshCollider mesh
 4926897            ? mesh.Mesh.Origin
 4926898            : collider.CanonicalCenter;
 4926899        FixedQuaternion sourceRotation = collider is LSMeshCollider meshCollider
 49268100            ? meshCollider.Mesh.Rotation
 49268101            : collider.CanonicalRotation;
 49268102        return FixedBoundBox.FromRelativeRotatedBoundsClippedToDomain(
 49268103            sourceOrigin,
 49268104            sourceRotation,
 49268105            localMin,
 49268106            localMax,
 49268107            referenceOrigin,
 49268108            referenceRotation);
 109    }
 110
 111    internal static Fixed64 GetGroundProbeRadius(
 112        LSCollider collider)
 113    {
 179968114        if (collider is LSSphereCollider sphere)
 60482115            return sphere.ScaledRadius;
 119486116        if (collider is LSCapsuleCollider capsule)
 26029117            return capsule.ScaledRadius;
 93457118        if (collider is LSCylinderCollider cylinder)
 7655119            return cylinder.ScaledRadius;
 85802120        if (collider is not (LSCuboidCollider or LSCompoundCollider))
 38350121            return Fixed64.Zero;
 122
 47452123        Vector3d extents = GetMaximumAbsoluteExtents(
 47452124            collider,
 47452125            collider.CanonicalCenter);
 47452126        return FixedMath.Min(extents.X, extents.Z);
 127    }
 128
 129    internal static Vector3d GetMaximumAbsoluteExtents(
 130        LSCollider collider,
 131        Vector3d referenceOrigin)
 132    {
 47453133        FixedBoundBox bounds = GetRelativeBounds(
 47453134            collider,
 47453135            referenceOrigin);
 47453136        return new Vector3d(
 47453137            FixedMath.Max(bounds.Min.X.Abs(), bounds.Max.X.Abs()),
 47453138            FixedMath.Max(bounds.Min.Y.Abs(), bounds.Max.Y.Abs()),
 47453139            FixedMath.Max(bounds.Min.Z.Abs(), bounds.Max.Z.Abs()));
 140    }
 141
 142    private static FixedBoundBox GetCompoundRelativeBounds(
 143        LSCompoundCollider compound,
 144        Vector3d referenceOrigin,
 145        FixedQuaternion referenceRotation)
 146    {
 20164147        FixedBoundBox bounds = GetRelativeBounds(
 20164148            compound.GetPartCollider(0),
 20164149            referenceOrigin,
 20164150            referenceRotation);
 20164151        Vector3d minimum = bounds.Min;
 20164152        Vector3d maximum = bounds.Max;
 41458153        for (int index = 1; index < compound.PartCount; index++)
 154        {
 565155            bounds = GetRelativeBounds(
 565156                compound.GetPartCollider(index),
 565157                referenceOrigin,
 565158                referenceRotation);
 565159            minimum = Vector3d.Min(minimum, bounds.Min);
 565160            maximum = Vector3d.Max(maximum, bounds.Max);
 161        }
 162
 20164163        return FixedBoundBox.FromMinMax(minimum, maximum);
 164    }
 165
 166    private static bool TryGetLocalBounds(
 167        LSCollider collider,
 168        out Vector3d minimum,
 169        out Vector3d maximum)
 170    {
 171        Vector3d halfExtents;
 172        switch (collider)
 173        {
 174            case LSSphereCollider sphere:
 5124175                halfExtents = Vector3d.One * sphere.ScaledRadius;
 5124176                break;
 177            case LSCapsuleCollider capsule:
 178                {
 4840179                    FixedBoundBox bounds =
 4840180                        FixedBoundBox.FromCenteredCapsuleClippedToDomain(
 4840181                            Vector3d.Zero,
 4840182                            Vector3d.Up,
 4840183                            capsule.AxisLength,
 4840184                            capsule.ScaledRadius);
 4840185                    minimum = bounds.Min;
 4840186                    maximum = bounds.Max;
 4840187                    return true;
 188                }
 189            case LSCuboidCollider cuboid:
 30957190                halfExtents = cuboid.OrientedBox.HalfExtents;
 30957191                break;
 192            case LSCylinderCollider cylinder:
 193                {
 2698194                    FixedBoundBox bounds =
 2698195                        FixedBoundBox.FromCenteredFiniteCylinderClippedToDomain(
 2698196                            Vector3d.Zero,
 2698197                            Vector3d.Up,
 2698198                            cylinder.Height,
 2698199                            cylinder.ScaledRadius);
 2698200                    minimum = bounds.Min;
 2698201                    maximum = bounds.Max;
 2698202                    return true;
 203                }
 204            case LSConeCollider cone:
 205                {
 2792206                    FixedBoundBox bounds =
 2792207                        FixedBoundBox.FromCenteredFiniteConeClippedToDomain(
 2792208                            Vector3d.Zero,
 2792209                            Vector3d.Up,
 2792210                            cone.Height,
 2792211                            cone.ScaledRadius);
 2792212                    minimum = bounds.Min;
 2792213                    maximum = bounds.Max;
 2792214                    return true;
 215                }
 216            case LSMeshCollider mesh:
 2857217                minimum = mesh.Mesh.GeometryLocalBounds.Min;
 2857218                maximum = mesh.Mesh.GeometryLocalBounds.Max;
 2857219                return true;
 220            default:
 1221                minimum = default;
 1222                maximum = default;
 1223                return false;
 224        }
 225
 36081226        minimum = -halfExtents;
 36081227        maximum = halfExtents;
 36081228        return true;
 229    }
 230
 231    private static Fixed64 GetFiniteAxisLocalRadius(
 232        FixedBoundBox localBounds)
 233    {
 3234        Vector3d extents = new(
 3235            FixedMath.Max(localBounds.Min.X.Abs(), localBounds.Max.X.Abs()),
 3236            FixedMath.Max(localBounds.Min.Y.Abs(), localBounds.Max.Y.Abs()),
 3237            FixedMath.Max(localBounds.Min.Z.Abs(), localBounds.Max.Z.Abs()));
 3238        return extents.TryGetMagnitudeCeiling(
 3239            out Fixed64 radius)
 3240            ? radius
 3241            : Fixed64.MaxValue;
 242    }
 243
 244    private static Fixed64 GetCurrentCuboidCenteredProxyRadius(
 245        LSCuboidCollider cuboid)
 246    {
 3247        cuboid.GetCurrentShapeScales(
 3248            out Vector3d ownerScale,
 3249            out Vector3d partScale);
 3250        Vector3d halfExtents =
 3251            ColliderScalePolicy.ScalePositive(
 3252                cuboid.Size,
 3253                ownerScale,
 3254                partScale,
 3255                Fixed64.Two);
 3256        return halfExtents.TryGetMagnitudeCeiling(
 3257            out Fixed64 radius)
 3258            ? radius
 3259            : Fixed64.MaxValue;
 260    }
 261
 262    private static Fixed64 GetCurrentCapsuleCenteredProxyRadius(
 263        LSCapsuleCollider capsule)
 264    {
 2265        capsule.GetCurrentShapeScales(
 2266            out Vector3d ownerScale,
 2267            out Vector3d partScale);
 2268        Fixed64 radiusX = ColliderScalePolicy.ScalePositive(
 2269            capsule.Radius,
 2270            ownerScale.X,
 2271            partScale.X);
 2272        Fixed64 radiusZ = ColliderScalePolicy.ScalePositive(
 2273            capsule.Radius,
 2274            ownerScale.Z,
 2275            partScale.Z);
 2276        if (!Fixed64.TryMultiplySubtractClamped(
 2277                capsule.Size.Y,
 2278                ownerScale.Y,
 2279                partScale.Y,
 2280                Fixed64.One,
 2281                capsule.Radius,
 2282                Fixed64.Two,
 2283                ownerScale.X,
 2284                partScale.X,
 2285                out Fixed64 axisLengthX)
 2286            || !Fixed64.TryMultiplySubtractClamped(
 2287                capsule.Size.Y,
 2288                ownerScale.Y,
 2289                partScale.Y,
 2290                Fixed64.One,
 2291                capsule.Radius,
 2292                Fixed64.Two,
 2293                ownerScale.Z,
 2294                partScale.Z,
 2295                out Fixed64 axisLengthZ))
 296        {
 1297            return Fixed64.MaxValue;
 298        }
 299
 1300        FixedBoundBox localBounds =
 1301            FixedBoundBox.FromCenteredCapsuleClippedToDomain(
 1302                Vector3d.Zero,
 1303                Vector3d.Up,
 1304                FixedMath.Min(axisLengthX, axisLengthZ),
 1305                FixedMath.Max(radiusX, radiusZ));
 1306        return localBounds.Max.Y;
 307    }
 308
 309    private static Fixed64 GetCurrentFiniteAxisCenteredProxyRadius(
 310        LSCollider collider,
 311        Fixed64 authoredHeight)
 312    {
 3313        collider.GetCurrentShapeScales(
 3314            out Vector3d ownerScale,
 3315            out Vector3d partScale);
 3316        Fixed64 radiusX = ColliderScalePolicy.ScalePositive(
 3317            collider.Radius,
 3318            ownerScale.X,
 3319            partScale.X);
 3320        Fixed64 radiusZ = ColliderScalePolicy.ScalePositive(
 3321            collider.Radius,
 3322            ownerScale.Z,
 3323            partScale.Z);
 3324        Fixed64 height = ColliderScalePolicy.ScalePositive(
 3325            authoredHeight,
 3326            ownerScale.Y,
 3327            partScale.Y);
 3328        return GetFiniteAxisLocalRadius(
 3329            FixedBoundBox.FromCenteredFiniteCylinderClippedToDomain(
 3330                Vector3d.Zero,
 3331                Vector3d.Up,
 3332                height,
 3333                FixedMath.Max(radiusX, radiusZ)));
 334    }
 335
 336    internal static Fixed64 GetCompoundCenteredProxyRadius(LSCompoundCollider compound, out Fixed64 rotationalRadius)
 337    {
 20164338        Fixed64 radius = Fixed64.Zero;
 20164339        rotationalRadius = Fixed64.Zero;
 81786340        for (int index = 0; index < compound.PartCount; index++)
 341        {
 20729342            var part = compound.GetPartCollider(index);
 20729343            Vector3d offset = part.CanonicalCenter - compound.CanonicalCenter;
 20729344            radius = FixedMath.Max(radius, ColliderGeometryPolicy.Add(
 20729345                ColliderGeometryPolicy.Magnitude(offset), part.CanonicalCenteredProxyRadius));
 346            // Retain a separate all-angle enclosure for the frame's CCD proxy.
 20729347            rotationalRadius = FixedMath.Max(rotationalRadius, part.CanonicalPivotProxyRadius);
 348        }
 20164349        return radius;
 350    }
 351
 352    private static Fixed64 GetCurrentCompoundCenteredProxyRadius(
 353        LSCompoundCollider compound)
 354    {
 6355        Fixed64 bestRadius = Fixed64.Zero;
 30356        for (int index = 0; index < compound.PartCount; index++)
 357        {
 10358            LSCollider part = compound.GetPartCollider(index);
 10359            bool distanceResolved =
 10360                part.TryGetCurrentScaledOffset(
 10361                    out Vector3d centerOffset)
 10362                & centerOffset.TryGetMagnitudeCeiling(
 10363                    out Fixed64 distance);
 10364            bool radiusResolved = Fixed64.TryAdd(
 10365                distance,
 10366                GetCurrentCenteredProxyRadius(part),
 10367                out Fixed64 radius);
 10368            if (!(distanceResolved
 10369                & radiusResolved))
 370            {
 1371                return Fixed64.MaxValue;
 372            }
 373
 9374            bestRadius = FixedMath.Max(bestRadius, radius);
 375        }
 376
 5377        return bestRadius;
 378    }
 379}