< Summary

Information
Class: Gravitas.CollisionHandling.ContinuousCollisionBounds
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Continuous/Rotational/ContinuousCollisionBounds.cs
Line coverage
100%
Covered lines: 106
Uncovered lines: 0
Coverable lines: 106
Total lines: 205
Line coverage: 100%
Branch coverage
100%
Covered branches: 120
Total branches: 120
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AreSeparated(...)100%2828100%
AreSeparated(...)100%2020100%
AreSeparated(...)100%2424100%
IsMeshSeparated(...)100%22100%
IsMeshSeparated(...)100%2626100%
IsScaledMeshSeparated(...)100%2020100%

File(s)

/home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Continuous/Rotational/ContinuousCollisionBounds.cs

#LineLine coverage
 1//=======================================================================
 2// ContinuousCollisionBounds.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;
 10using Gravitas.Colliders;
 11using SwiftCollections.Query;
 12
 13namespace Gravitas.CollisionHandling;
 14
 15/// <summary>
 16/// Certifies empty rotational intervals from midpoint geometry and the outward-rounded
 17/// bound on both participants' relative travel. Every leaf pair must be separated.
 18/// </summary>
 19internal static class ContinuousCollisionBounds
 20{
 21    internal static bool AreSeparated(LSCollider source, LSCollider target, Fixed64 motionBound)
 22    {
 25723        if (ContinuousCollisionMath.AreBoundsSeparatedByMoreThan(source.Bounds, target.Bounds, motionBound))
 5424            return true;
 25
 20326        var sourceCompound = source as LSCompoundCollider;
 20327        var targetCompound = target as LSCompoundCollider;
 51028        for (int i = 0; i < (sourceCompound?.PartCount ?? 1); i++)
 52629            for (int j = 0; j < (targetCompound?.PartCount ?? 1); j++)
 30            {
 21131                LSCollider first = sourceCompound?.GetPartCollider(i) ?? source;
 21132                LSCollider second = targetCompound?.GetPartCollider(j) ?? target;
 21133                if (ContinuousCollisionMath.AreBoundsSeparatedByMoreThan(first.Bounds, second.Bounds, motionBound))
 34                    continue;
 20335                if (second is LSMeshCollider meshTarget && IsMeshSeparated(meshTarget, first, motionBound))
 36                    continue;
 17937                if (first is LSMeshCollider meshSource && IsMeshSeparated(meshSource, second, motionBound))
 38                    continue;
 15439                return false;
 40            }
 4941        return true;
 42    }
 43
 44    internal static bool AreSeparated(LSCollider2D source, LSCollider2D target, Fixed64 motionBound)
 45    {
 37846        if (ContinuousCollisionMath.AreBoundsSeparatedByMoreThan(source.Bounds, target.Bounds, motionBound))
 16047            return true;
 48
 21849        var sourceCompound = source as LSCompoundCollider2D;
 21850        var targetCompound = target as LSCompoundCollider2D;
 46451        for (int i = 0; i < (sourceCompound?.PartCount ?? 1); i++)
 49252            for (int j = 0; j < (targetCompound?.PartCount ?? 1); j++)
 53            {
 23254                LSCollider2D first = sourceCompound?.GetPartCollider(i) ?? source;
 23255                LSCollider2D second = targetCompound?.GetPartCollider(j) ?? target;
 23256                if (!ContinuousCollisionMath.AreBoundsSeparatedByMoreThan(first.Bounds, second.Bounds, motionBound))
 20457                    return false;
 58            }
 1459        return true;
 60    }
 61
 62    internal static bool AreSeparated(LSCollider source, LSCollider2D target, Fixed64 motionBound)
 63    {
 716164        if (ContinuousCollisionMath.AreBoundsSeparatedByMoreThan(source.Bounds, target.MixedBounds3D, motionBound))
 274765            return true;
 66
 441467        var sourceCompound = source as LSCompoundCollider;
 441468        var targetCompound = target as LSCompoundCollider2D;
 901669        for (int i = 0; i < (sourceCompound?.PartCount ?? 1); i++)
 910470            for (int j = 0; j < (targetCompound?.PartCount ?? 1); j++)
 71            {
 445872                LSCollider first = sourceCompound?.GetPartCollider(i) ?? source;
 445873                LSCollider2D second = targetCompound?.GetPartCollider(j) ?? target;
 445874                if (ContinuousCollisionMath.AreBoundsSeparatedByMoreThan(first.Bounds, second.MixedBounds3D, motionBound
 75                    continue;
 437076                if (first is LSMeshCollider mesh)
 77                {
 50978                    ColliderCanonicalBounds2D.GetMixedRelativeBounds(second, mesh.Mesh.Origin, mesh.Mesh.Rotation,
 50979                        out FixedBoundBox localBounds, out FixedOrientedBox orientedBounds);
 50980                    if (IsMeshSeparated(mesh, localBounds, motionBound, orientedBounds))
 81                        continue;
 82                }
 434483                return false;
 84            }
 7085        return true;
 86    }
 87
 88    private static bool IsMeshSeparated(LSMeshCollider mesh, LSCollider other, Fixed64 motionBound)
 89    {
 90        // Admitted local capsule extents fit; retain clipping only in the final mesh-relative frame.
 21091        return IsMeshSeparated(mesh, ColliderCanonicalBounds.GetRelativeBounds(other,
 21092            mesh.Mesh.Origin, mesh.Mesh.Rotation), motionBound, (other as LSCuboidCollider)?.OrientedBox);
 93    }
 94
 95    private static bool IsMeshSeparated(LSMeshCollider mesh, FixedBoundBox localBounds, Fixed64 motionBound,
 96        FixedOrientedBox? orientedBox = null)
 97    {
 98        // An empty boundary query cannot certify the empty interior of a solid hull.
 71999        if (mesh.Mode == MeshColliderMode.Convex && mesh.IsClosedSurface)
 43100            return false;
 101
 676102        if (!mesh.Mesh.GeometryFrame.IsRigid)
 17103            return IsScaledMeshSeparated(mesh, localBounds, motionBound, orientedBox);
 104
 105        // The combined source/target motion ball fits in this midpoint-frame box.
 106        // Only mesh-local endpoints are clipped; all stored triangles lie in that domain.
 659107        Vector3d expansion = Vector3d.One * motionBound;
 659108        var candidates = mesh.Context.CollisionScratch.MeshTriangleCandidatesA;
 659109        FixedBoundBox expanded = FixedBoundBox.FromMinMax(localBounds.Min - expansion, localBounds.Max + expansion);
 659110        mesh.Mesh.GetTrianglesInGeometryBounds(new FixedBoundVolume(expanded.Min, expanded.Max), candidates);
 659111        if (candidates.Count == 0)
 16112            return true;
 113
 114        // A triangle's box also contains empty corners. Use the existing exact
 115        // finite-triangle SAT to certify them, without constructing world witnesses.
 116        FixedOrientedBox box;
 117        Vector3d triangleOrigin;
 118        FixedQuaternion triangleRotation;
 643119        if (orientedBox is FixedOrientedBox rigidBox)
 120        {
 121            // Preserve the shape enclosure's orientation: its AABB can cover empty corners
 122            // throughout rotation. The same motion ball fits along each box axis.
 568123            if (!Vector3d.TryAdd(rigidBox.HalfExtents, expansion, out Vector3d scope))
 1124                return false;
 567125            box = new FixedOrientedBox(rigidBox.Center, rigidBox.Orientation, scope);
 567126            triangleOrigin = mesh.Mesh.Origin;
 567127            triangleRotation = mesh.Mesh.Rotation;
 128        }
 129        else
 130        {
 75131            Vector3d center = expanded.Center;
 75132            bool scopeResolved = Vector3d.TrySubtract(center, expanded.Min, out Vector3d lowerScope)
 75133                & Vector3d.TrySubtract(expanded.Max, center, out Vector3d upperScope);
 75134            if (!scopeResolved)
 1135                return false;
 74136            Vector3d scope = new(FixedMath.Max(lowerScope.X, upperScope.X),
 74137                FixedMath.Max(lowerScope.Y, upperScope.Y), FixedMath.Max(lowerScope.Z, upperScope.Z));
 74138            if (scope.X == Fixed64.Zero || scope.Y == Fixed64.Zero || scope.Z == Fixed64.Zero)
 10139                return false;
 64140            box = new FixedOrientedBox(center, FixedQuaternion.Identity, scope);
 64141            triangleOrigin = Vector3d.Zero;
 64142            triangleRotation = FixedQuaternion.Identity;
 143        }
 1730144        for (int i = 0; i < candidates.Count; i++)
 145        {
 811146            mesh.Mesh.GetGeometryTriangleVertices(candidates[i], out Vector3d first, out Vector3d second, out Vector3d t
 811147            var triangle = new FixedTriangle(first, second, third);
 148            // Degeneracy is a distinct false result, so it cannot prove separation.
 811149            if (triangle.IsDegenerate || box.TryGetTriangleContact(triangleOrigin, triangleRotation, triangle, out _))
 577150                return false;
 151        }
 54152        return true;
 153    }
 154    private static bool IsScaledMeshSeparated(LSMeshCollider collider, FixedBoundBox physicalBounds,
 155        Fixed64 motionBound, FixedOrientedBox? orientedBox)
 156    {
 17157        PhysicsMesh mesh = collider.Mesh;
 17158        Vector3d expansion = Vector3d.One * motionBound;
 17159        FixedBoundBox expanded = FixedBoundBox.FromMinMax(physicalBounds.Min - expansion, physicalBounds.Max + expansion
 160        // Motion is a physical sphere. Expand before converting to the authored
 161        // chart, where nonuniform scale would otherwise distort that sphere.
 17162        var physicalFrame = ScaledRigidFrame3d.Rigid(mesh.Origin, mesh.Rotation);
 17163        bool transformed = physicalFrame.TryGetBoundsIn(expanded.Min, expanded.Max, mesh.GeometryFrame,
 17164            out FixedBoundBox candidatesBounds);
 165        // Collider scale admission guarantees an invertible target mapping.
 166        System.Diagnostics.Debug.Assert(transformed);
 17167        var candidates = collider.Context.CollisionScratch.MeshTriangleCandidatesA;
 17168        mesh.GetTrianglesInGeometryBounds(new FixedBoundVolume(candidatesBounds.Min, candidatesBounds.Max), candidates);
 18169        if (candidates.Count == 0) return true;
 170        ScaledRigidFrame3d boxFrame;
 171        Vector3d scope;
 16172        if (orientedBox is FixedOrientedBox rigid)
 173        {
 10174            if (!Vector3d.TryAdd(rigid.HalfExtents, expansion, out scope)) return false;
 6175            boxFrame = ScaledRigidFrame3d.Rigid(rigid.Center, rigid.Orientation);
 176        }
 177        else
 178        {
 8179            Vector3d center = expanded.Center;
 8180            if (!Vector3d.TrySubtract(center, expanded.Min, out Vector3d lower)
 10181                || !Vector3d.TrySubtract(expanded.Max, center, out Vector3d upper)) return false;
 6182            scope = new Vector3d(FixedMath.Max(lower.X, upper.X), FixedMath.Max(lower.Y, upper.Y),
 6183                FixedMath.Max(lower.Z, upper.Z));
 6184            boxFrame = new ScaledRigidFrame3d(mesh.Origin, mesh.Rotation, center, Vector3d.One,
 6185                mesh.Rotation, Vector3d.Zero, Vector3d.One);
 186        }
 16187        if (scope.X == Fixed64.Zero || scope.Y == Fixed64.Zero || scope.Z == Fixed64.Zero) return false;
 8188        System.Span<Vector3d> boxVertices = stackalloc Vector3d[8];
 8189        WideOrientedBox.GetBoxHullVertices(scope, boxVertices);
 8190        System.Span<Vector3d> points = stackalloc Vector3d[3];
 8191        System.ReadOnlySpan<int> face = stackalloc int[3] { 0, 1, 2 };
 8192        System.ReadOnlySpan<int> edges = stackalloc int[6] { 0, 1, 1, 2, 2, 0 };
 24193        for (int index = 0; index < candidates.Count; index++)
 194        {
 8195            mesh.GetGeometryTriangleVertices(candidates[index], out points[0], out points[1], out points[2]);
 196            // Authored mesh admission rejects degenerate triangles, and the
 197            // positive-scale affine mapping preserves their exact nonzero area.
 8198            if (WideOrientedBox.TryGetConvexHullPenetration(boxFrame, boxVertices,
 8199                    WideOrientedBox.BoxHullTriangles, WideOrientedBox.BoxHullEdges, mesh.GeometryFrame,
 12200                    points, face, edges, out _)) return false;
 201        }
 4202        return true;
 203    }
 204
 205}