< Summary

Information
Class: Gravitas.CollisionHandling.MeshSlabSurfaceBoundary
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Detection/3D/Mesh/Surfaces/MeshSlabSurfaceBoundary.cs
Line coverage
100%
Covered lines: 73
Uncovered lines: 0
Coverable lines: 73
Total lines: 175
Line coverage: 100%
Branch coverage
100%
Covered branches: 48
Total branches: 48
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
AppendSupportFallback(...)100%1010100%
BuildSurfaceContacts(...)100%3838100%

File(s)

/home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Detection/3D/Mesh/Surfaces/MeshSlabSurfaceBoundary.cs

#LineLine coverage
 1//=======================================================================
 2// MeshSlabSurfaceBoundary.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 System;
 9using FixedMathSharp;
 10using FixedMathSharp.Geometry;
 11using Gravitas.Colliders;
 12using Gravitas.Materials;
 13using SwiftCollections;
 14
 15namespace Gravitas.CollisionHandling;
 16
 17/// <summary>Preserves matched finite slab contacts on a trusted surface's genuine perimeter.</summary>
 18internal sealed class MeshSlabSurfaceBoundary
 19{
 1019120    private readonly SwiftHashSet<int> _visitedCorners = new();
 1019121    private readonly SwiftList<int> _supportVertices = new();
 1019122    private readonly SwiftList<WideAxis3> _supportFan = new();
 23
 24    internal void AppendSupportFallback(LSMeshCollider collider, LSCollider2D? embedded,
 25        int surfaceTriangle, MeshSlabFaceRegions faces, ContactManifold manifold, int namespace3D, int namespace2D,
 26        PhysicsMaterial targetMaterial)
 27    {
 1228        PhysicsMesh mesh = collider.Mesh;
 1229        ReadOnlySpan<int> boundary = mesh.GetCanonicalSurfaceBoundaryVertexPairs(surfaceTriangle);
 1230        ReadOnlySpan<Vector3d> vertices = mesh.GeometryVertices;
 1231        FiniteSlabRayFrame frame = faces.Frame;
 2432        _supportVertices.FastClear(); _visitedCorners.Clear();
 34433        foreach (int vertex in boundary)
 24034            if (_visitedCorners.Add(vertex)) _supportVertices.Add(vertex);
 1235        _supportFan.EnsureCapacity(_supportVertices.Count);
 1236        bool found = false;
 1237        FixedContactAnchors best = default;
 1238        int bestFeature = 0;
 18439        for (int edge = 0; edge < boundary.Length; edge += 2)
 40        {
 16041            int first = boundary[edge], second = boundary[edge + 1];
 8042            Keep(first, second, checked(3 * (edge / 2)), false);
 10043            if (_visitedCorners.Remove(first)) Keep(first, first, checked(3 * (edge / 2) + 1), true);
 14044            if (_visitedCorners.Remove(second)) Keep(second, second, checked(3 * (edge / 2) + 2), true);
 45        }
 46        // A farthest owner/slab pair supplies an actual boundary vertex with a
 47        // global support normal, so the complete vertex charts cannot be empty.
 48        System.Diagnostics.Debug.Assert(found);
 1249        manifold.AddContact(new ContactAnchor(best.FirstAnchor), new ContactAnchor(best.SecondAnchor),
 1250            best.Depth, best.Normal, collider.Material, targetMaterial, best.DepthIsClamped,
 1251            featureNamespaceA: namespace3D, featureNamespaceB: namespace2D,
 1252            group: new ContactGroupKey(namespace3D, namespace2D, mesh.GetCanonicalSurfaceOrdinal(surfaceTriangle), 0, -b
 53
 54        void Keep(int first, int second, int feature, bool pointOnly)
 55        {
 56            _supportFan.FastClear();
 57            foreach (int vertex in _supportVertices.AsReadOnlySpan())
 58                if (vertex != first) _supportFan.Add(-mesh.GeometryFrame.GetWorldDirectionNumerators(
 59                    MeshSurfaceBoundaryTopology.Ray(mesh.GeometryVertices[first], mesh.GeometryVertices[vertex])));
 60            // Global support makes the matched exit a separating plane for the
 61            // whole fill; a local hole-edge exit need not clear another region.
 62            Span<Vector2d> localBox = stackalloc Vector2d[4];
 63            if (!SegmentSlabSurfaceContact.TryGetFeaturePenetration(new FixedSegment(mesh.GeometryVertices[first], mesh.
 64                mesh.GeometryFrame, frame.Center, frame.SlabRotation, frame.Section.RadialFrame.Length,
 65                frame.RadiusValue, frame.HalfThickness, CollisionDetectionMixed.GetConvexPrismOffsets(embedded, localBox
 66                _supportFan.AsReadOnlySpan(), !pointOnly, out FixedContactAnchors contact,
 67                fullHeight: frame.FullHeight != Fixed64.Zero)) return;
 68            if (found && (contact.DepthIsClamped && !best.DepthIsClamped
 69                || contact.DepthIsClamped == best.DepthIsClamped && contact.Depth >= best.Depth)) return;
 70            // Only final depth ranking is Q32: unclamped candidate order differs
 71            // from exact ranking by at most one raw unit; canonical ties stay first.
 72            found = true; best = contact; bestFeature = feature;
 73        }
 1274    }
 75
 76    internal void BuildSurfaceContacts(LSMeshCollider collider, LSCollider2D? embedded,
 77        int surfaceTriangle, MeshSurfaceConnectivity connectivity, MeshSlabFaceRegions faces,
 78        ContactManifold manifold, int namespace3D, int namespace2D, PhysicsMaterial targetMaterial)
 79    {
 1101780        PhysicsMesh mesh = collider.Mesh;
 1101781        int owner = mesh.GetManifoldSurfaceOwner(surfaceTriangle);
 1101782        int surface = mesh.GetCanonicalSurfaceOrdinal(surfaceTriangle);
 1101783        ReadOnlySpan<int> boundary = mesh.GetCanonicalSurfaceBoundaryVertexPairs(surfaceTriangle);
 1101784        ReadOnlySpan<Vector3d> vertices = mesh.GeometryVertices;
 1101785        FiniteSlabRayFrame frame = faces.Frame;
 1101786        Fixed64 length = frame.Section.RadialFrame.Length;
 1101787        Fixed64 radius = frame.RadiusValue;
 1101788        Fixed64 halfThickness = frame.HalfThickness;
 1101789        Span<Vector2d> box = stackalloc Vector2d[4];
 1101790        ReadOnlySpan<Vector2d> offsets = !faces.PrismOffsets.IsEmpty ? faces.PrismOffsets.Span
 1101791            : CollisionDetectionMixed.GetConvexPrismOffsets(embedded, box);
 92        // Orientation belongs to the ray frame's canonical plane, not authored winding.
 1101793        WideDirection3d worldNormal = new WideDirection3d(frame.Plane.AuthoredNormal).Transform(mesh.GeometryFrame.Geome
 1101794        WideAxis3 normal = new(Signed320.NarrowValue(worldNormal.X), Signed320.NarrowValue(worldNormal.Y), Signed320.Nar
 1101795        _visitedCorners.Clear();
 1101796        MeshSurfaceBoundaryDomain? domain = faces.JoinedBoundary;
 1101797        if (domain != null)
 98        {
 9648299            for (int span = 0; span < domain.Spans.Length; span++)
 100            {
 38612101                var piece = domain.Spans[span];
 38612102                if (faces.IsJoinedSpan(span) || !frame.Section.IntersectsSegment(new FixedSegment(piece.Start, piece.End
 224103                int region = MeshSurfaceBoundaryTopology.FindEdgeRegion(mesh, owner, piece.First, piece.Second,
 224104                    connectivity, frame.Section, faces.PrismOffsets);
 224105                int feature = checked(3 * span);
 224106                RationalSegmentInterval clip = faces.GetRetainedInterval(ref span);
 107                // An intersected trusted perimeter has an admitted incident triangle.
 108                System.Diagnostics.Debug.Assert(region >= 0);
 224109                KeepFeature(piece.First, piece.Second, feature, region, false, offsets, clip);
 110            }
 96378111            for (int edge = 0; edge < boundary.Length; edge += 2)
 112            {
 77120113                int first = boundary[edge], second = boundary[edge + 1];
 38560114                int region = MeshSurfaceBoundaryTopology.FindEdgeRegion(mesh, owner, first, second,
 38560115                    connectivity, frame.Section, faces.PrismOffsets);
 38560116                if (region < 0) continue;
 4000117                int feature = checked(3 * (domain.Spans.Length + edge / 2));
 4000118                if (_visitedCorners.Add(first) && frame.Section.ContainsPoint(vertices[first], offsets))
 42119                    KeepFeature(first, second, feature + 1, region, true, offsets);
 4000120                if (_visitedCorners.Add(second) && frame.Section.ContainsPoint(vertices[second], offsets))
 150121                    KeepFeature(second, first, feature + 2, region, true, offsets);
 122            }
 9629123            return;
 124        }
 125        // Joined prism frames always retain their prepared boundary above.
 126        // Isolated Prepare clears this member-owned offset memory.
 127        System.Diagnostics.Debug.Assert(faces.PrismOffsets.IsEmpty);
 14142128        for (int edge = 0; edge < boundary.Length; edge += 2)
 129        {
 11366130            int first = boundary[edge], second = boundary[edge + 1];
 5683131            int feature = checked(3 * (edge / 2));
 5683132            int region = connectivity.RegionCount == 1
 5683133                ? frame.Section.IntersectsSegment(new FixedSegment(vertices[first], vertices[second]), offsets) ? 0 : -1
 5683134                : MeshSurfaceBoundaryTopology.FindEdgeRegion(mesh, owner, first, second, connectivity, frame.Section, em
 5683135            if (region < 0) continue;
 723136            KeepFeature(first, second, feature, region, pointOnly: false, offsets);
 723137            if (_visitedCorners.Add(first) && frame.Section.ContainsPoint(vertices[first], offsets))
 22138                KeepFeature(first, second, feature + 1, region, pointOnly: true, offsets);
 723139            if (_visitedCorners.Add(second) && frame.Section.ContainsPoint(vertices[second], offsets))
 34140                KeepFeature(second, first, feature + 2, region, pointOnly: true, offsets);
 141        }
 142
 143        void KeepFeature(int first, int second, int ordinal, int region, bool pointOnly, ReadOnlySpan<Vector2d> prismOff
 144            in RationalSegmentInterval clip = default)
 145        {
 146            bool suppress = false;
 147            ReadOnlySpan<WideAxis3> joinedFan = domain != null && pointOnly
 148                ? faces.GetJoinedCornerFan(collider, surfaceTriangle, first, out suppress) : default;
 149            if (suppress) return;
 150            Span<WideAxis3> fan = stackalloc WideAxis3[Math.Max(5, joinedFan.Length + 1)];
 151            int count;
 152            if (joinedFan.IsEmpty)
 153            {
 154                count = MeshSurfaceBoundaryTopology.GetIncidentFan(mesh, owner, first, second, !pointOnly, fan);
 155                for (int index = 0; index < count; index++)
 156                    fan[index] = mesh.GeometryFrame.GetWorldDirectionNumerators(fan[index]);
 157            }
 158            else { joinedFan.CopyTo(fan); count = joinedFan.Length; }
 159            // Topology admits the contact normal p-q. The mathematics owner
 160            // admits the opposite exit ray q-p; negate its halfspaces once.
 161            for (int index = 0; index < count; index++) fan[index] = -fan[index];
 162            fan[count++] = faces.Orientation > 0 ? -normal : normal;
 163            var segment = new FixedSegment(mesh.GeometryVertices[first],
 164                mesh.GeometryVertices[pointOnly ? first : second]);
 165            if (!SegmentSlabSurfaceContact.TryGetContact(segment, mesh.GeometryFrame,
 166                frame.Center, frame.SlabRotation, length, radius, halfThickness, prismOffsets, fan[..count],
 167                interiorOnly: !pointOnly, clip, out FixedContactAnchors contact, fullHeight: frame.FullHeight != Fixed64
 168            var group = new ContactGroupKey(namespace3D, namespace2D, surface, 0, checked(-ordinal - 1),
 169                sourceIdA: faces.SourceId, sourceOrderA: faces.SourceOrder);
 170            manifold.AddContact(new ContactAnchor(contact.FirstAnchor), new ContactAnchor(contact.SecondAnchor),
 171                contact.Depth, contact.Normal, collider.Material, targetMaterial,
 172                contact.DepthIsClamped, featureNamespaceA: namespace3D, featureNamespaceB: namespace2D, group: group);
 173        }
 1388174    }
 175}