< Summary

Information
Class: Gravitas.Queries.ColliderPlanarProjection
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/Queries/3D/Geometry/ColliderPlanarProjection.cs
Line coverage
100%
Covered lines: 144
Uncovered lines: 0
Coverable lines: 144
Total lines: 242
Line coverage: 100%
Branch coverage
100%
Covered branches: 44
Total branches: 44
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TryGetRelation(...)100%1616100%
TryGetCompoundRelation(...)100%88100%
TryGetMeshRelation(...)100%1212100%
ShouldKeepCurrent(...)100%44100%
ShouldKeepCurrent(...)100%44100%
GetCanonicalQueryPoint(...)100%11100%

File(s)

/home/runner/work/Gravitas/Gravitas/src/Gravitas/Queries/3D/Geometry/ColliderPlanarProjection.cs

#LineLine coverage
 1//=======================================================================
 2// ColliderPlanarProjection.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;
 11
 12namespace Gravitas.Queries;
 13
 14/// <summary>
 15/// Dispatches exact X/Z projected-circle relations for built-in 3D colliders.
 16/// </summary>
 17internal static class ColliderPlanarProjection
 18{
 19    internal static bool TryGetRelation(
 20        LSCollider collider,
 21        Vector2d circleCenter,
 22        Fixed64 circleRadius,
 23        out ProjectedSurfaceRelation relation)
 24    {
 174725        if (collider is LSCompoundCollider compound)
 26        {
 21327            return TryGetCompoundRelation(
 21328                compound,
 21329                circleCenter,
 21330                circleRadius,
 21331                out relation);
 32        }
 153433        if (collider is LSMeshCollider mesh)
 34        {
 21835            return TryGetMeshRelation(
 21836                mesh,
 21837                circleCenter,
 21838                circleRadius,
 21839                out relation);
 40        }
 41
 42        bool found;
 43        PlanarProjectionRelation planar;
 44        switch (collider)
 45        {
 46            case LSSphereCollider sphere:
 46447                found = WidePlanarProjection.TryGetSphereRelation(
 46448                    circleCenter,
 46449                    circleRadius,
 46450                    sphere.Center,
 46451                    sphere.ScaledRadius,
 46452                    out planar);
 46453                break;
 54            case LSCapsuleCollider capsule:
 21455                found = WidePlanarProjection
 21456                    .TryGetCenteredCapsuleRelation(
 21457                        circleCenter,
 21458                        circleRadius,
 21459                        capsule.Center,
 21460                        capsule.Rotation,
 21461                        Vector3d.Up,
 21462                        capsule.AxisLength,
 21463                        capsule.ScaledRadius,
 21464                        out planar);
 21465                break;
 66            case LSCylinderCollider cylinder:
 21467                found = WidePlanarProjection
 21468                    .TryGetCenteredCylinderRelation(
 21469                        circleCenter,
 21470                        circleRadius,
 21471                        cylinder.Center,
 21472                        cylinder.Rotation,
 21473                        cylinder.Height,
 21474                        cylinder.ScaledRadius,
 21475                        out planar);
 21476                break;
 77            case LSConeCollider cone:
 21178                found = WidePlanarProjection
 21179                    .TryGetCenteredConeRelation(
 21180                        circleCenter,
 21181                        circleRadius,
 21182                        cone.Center,
 21183                        cone.Rotation,
 21184                        cone.Height,
 21185                        cone.ScaledRadius,
 21186                        out planar);
 21187                break;
 88            case LSCuboidCollider cuboid:
 21289                found = WidePlanarProjection
 21290                    .TryGetOrientedBoxRelation(
 21291                        circleCenter,
 21292                        circleRadius,
 21293                        cuboid.OrientedBox,
 21294                        out planar);
 21295                break;
 96            default:
 197                relation = default;
 198                return false;
 99        }
 100
 1315101        if (!found)
 102        {
 5103            relation = default;
 5104            return false;
 105        }
 106
 1310107        Vector3d queryPoint = GetCanonicalQueryPoint(
 1310108            collider,
 1310109            circleCenter);
 1310110        FixedPointAnchor anchor =
 1310111            collider.GetClosestSurfaceAnchor(
 1310112                queryPoint,
 1310113                out Vector3d normal);
 1310114        relation = new ProjectedSurfaceRelation(
 1310115            planar.Distance,
 1310116            planar.Offset,
 1310117            planar.Direction,
 1310118            anchor,
 1310119            normal,
 1310120            planar.IsContained);
 1310121        return true;
 122    }
 123
 124    private static bool TryGetCompoundRelation(
 125        LSCompoundCollider compound,
 126        Vector2d circleCenter,
 127        Fixed64 circleRadius,
 128        out ProjectedSurfaceRelation relation)
 129    {
 213130        bool found = false;
 213131        relation = default;
 862132        for (int index = 0; index < compound.PartCount; index++)
 133        {
 218134            if (!TryGetRelation(
 218135                    compound.GetPartCollider(index),
 218136                    circleCenter,
 218137                    circleRadius,
 218138                    out ProjectedSurfaceRelation candidate)
 218139                || (found
 218140                    && ShouldKeepCurrent(candidate, relation)))
 141            {
 142                continue;
 143            }
 144
 215145            found = true;
 215146            relation = candidate;
 147        }
 148
 213149        return found;
 150    }
 151
 152    private static bool TryGetMeshRelation(
 153        LSMeshCollider collider,
 154        Vector2d circleCenter,
 155        Fixed64 circleRadius,
 156        out ProjectedSurfaceRelation relation)
 157    {
 218158        PhysicsMesh mesh = collider.Mesh;
 218159        bool found = false;
 218160        PlanarProjectionRelation best = default;
 218161        int bestTriangle = -1;
 218162        FixedTriangle bestGeometry = default;
 890163        for (int index = 0; index < mesh.TriangleCount; index++)
 164        {
 227165            mesh.GetGeometryTriangleVertices(
 227166                index,
 227167                out Vector3d first,
 227168                out Vector3d second,
 227169                out Vector3d third);
 227170            var triangle = new FixedTriangle(first, second, third);
 227171            if (!WidePlanarProjection.TryGetTriangleRelation(
 227172                    circleCenter,
 227173                    circleRadius,
 227174                    triangle,
 227175                    mesh.GeometryFrame,
 227176                    out PlanarProjectionRelation candidate)
 227177                || (found && ShouldKeepCurrent(candidate, best)))
 178            {
 179                continue;
 180            }
 181
 219182            found = true;
 219183            best = candidate;
 219184            bestTriangle = index;
 219185            bestGeometry = triangle;
 186        }
 187
 218188        if (!found)
 189        {
 1190            relation = default;
 1191            return false;
 192        }
 193
 217194        var queryAnchor = new FixedPointAnchor(
 217195            GetCanonicalQueryPoint(
 217196                collider,
 217197                circleCenter),
 217198            FixedQuaternion.Identity,
 217199            Vector3d.Zero);
 200        FixedPointAnchor contact;
 217201        if (mesh.GeometryFrame.IsRigid)
 216202            contact = bestGeometry.GetClosestPointAnchor(mesh.Origin, mesh.Rotation, queryAnchor);
 203        else
 204        {
 205            // A finite closest witness lies within the mesh's admitted reach;
 206            // its offset from the rounded physical center always fits Q32.32.
 1207            bool represented = WideTriangleRelations.TryGetClosestPointAnchor(bestGeometry, mesh.GeometryFrame,
 1208                GetCanonicalQueryPoint(collider, circleCenter), mesh.Origin, out contact);
 209            System.Diagnostics.Debug.Assert(represented);
 210        }
 217211        relation = new ProjectedSurfaceRelation(
 217212            best.Distance,
 217213            best.Offset,
 217214            best.Direction,
 217215            contact,
 217216            mesh.GetFaceNormalWorld(bestTriangle),
 217217            best.IsContained);
 217218        return true;
 219    }
 220
 221    private static bool ShouldKeepCurrent(
 222        ProjectedSurfaceRelation candidate,
 223        ProjectedSurfaceRelation current) =>
 4224        candidate.Distance > current.Distance
 4225        || (candidate.Distance == current.Distance
 4226            && candidate.IsContained.CompareTo(current.IsContained) <= 0);
 227
 228    private static bool ShouldKeepCurrent(
 229        PlanarProjectionRelation candidate,
 230        PlanarProjectionRelation current) =>
 7231        candidate.Distance > current.Distance
 7232        || (candidate.Distance == current.Distance
 7233            && candidate.IsContained.CompareTo(current.IsContained) <= 0);
 234
 235    private static Vector3d GetCanonicalQueryPoint(
 236        LSCollider collider,
 237        Vector2d circleCenter) =>
 1528238        new(
 1528239            circleCenter.X,
 1528240            collider.Center.Y,
 1528241            circleCenter.Y);
 242}