< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FindEdgeRegion(...)100%11100%
FindEdgeRegion(...)100%11100%
FindEdgeRegion(...)100%11100%
FindEdgeRegion(...)100%11100%
FindSupportEdgeRegion(...)100%11100%
FindSupportEdgeRegion(...)100%11100%
FindEdgeRegion(...)100%3636100%
GetIncidentFan(...)100%4848100%
Project(...)100%44100%
Negate(...)100%11100%
Ray(...)100%11100%
.ctor(...)100%11100%
IntersectsSegment(...)100%11100%
.ctor(...)100%11100%
IntersectsSegment(...)100%11100%
.ctor(...)100%11100%
IntersectsSegment(...)100%22100%
.ctor(...)100%11100%
IntersectsSegment(...)100%22100%
.ctor(...)100%11100%
IntersectsSegment(...)100%11100%

File(s)

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

#LineLine coverage
 1//=======================================================================
 2// MeshSurfaceBoundaryTopology.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;
 12
 13namespace Gravitas.CollisionHandling;
 14
 15/// <summary>Exact inward sectors of a trusted planar surface's genuine perimeter.</summary>
 16internal static class MeshSurfaceBoundaryTopology
 17{
 18    internal static int FindEdgeRegion(PhysicsMesh mesh, int owner, int first, int second,
 19        MeshSurfaceConnectivity connectivity, in ConePlaneRayFrame frame) =>
 147920        FindEdgeRegion(mesh, owner, first, second, connectivity, new ConeSection(frame));
 21
 22    internal static int FindEdgeRegion(PhysicsMesh mesh, int owner, int first, int second,
 23        MeshSurfaceConnectivity connectivity, in CapsuleFiniteSectionFrame frame) =>
 1028524        FindEdgeRegion(mesh, owner, first, second, connectivity, new CapsuleSection(frame));
 25
 26    internal static int FindEdgeRegion(PhysicsMesh mesh, int owner, int first, int second,
 27        MeshSurfaceConnectivity connectivity, in FiniteSlabSectionFrame frame, LSCollider2D? embedded) =>
 4828        FindEdgeRegion(mesh, owner, first, second, connectivity, new SlabSection(frame, embedded));
 29
 30    internal static int FindEdgeRegion(PhysicsMesh mesh, int owner, int first, int second,
 31        MeshSurfaceConnectivity connectivity, in FiniteSlabSectionFrame frame, ReadOnlyMemory<Vector2d> offsets) =>
 3878432        FindEdgeRegion(mesh, owner, first, second, connectivity, new SlabSection(frame, null, offsets));
 33
 34    internal static int FindSupportEdgeRegion(PhysicsMesh mesh, int owner, int first, int second,
 35        MeshSurfaceConnectivity connectivity, MeshSlabFaceRegions faces, LSCollider2D? embedded) =>
 31936        FindEdgeRegion(mesh, owner, first, second, connectivity, new SlabSupport(faces, embedded));
 37
 38    internal static int FindSupportEdgeRegion(PhysicsMesh mesh, int owner, int first, int second,
 39        MeshSurfaceConnectivity connectivity, MeshHullSurfaceContacts faces) =>
 5640        FindEdgeRegion(mesh, owner, first, second, connectivity, new HullSupport(faces));
 41
 42    private static int FindEdgeRegion<TSection>(PhysicsMesh mesh, int owner, int first, int second,
 43        MeshSurfaceConnectivity connectivity, in TSection frame) where TSection : struct, ISection
 44    {
 5097145        ReadOnlySpan<Vector3d> vertices = mesh.GeometryVertices;
 46        // Reduced edges can span many authored subedges. Their first corner's
 47        // triangle need not touch the finite shape; locate an admitted piece.
 5097148        ReadOnlySpan<int> perimeter = mesh.GetManifoldSurfaceBoundaryVertexPairs(owner);
 5151849        if (perimeter.IsEmpty) return connectivity.GetRegionOrdinal(owner);
 5042450        mesh.GetGeometryTriangleVertices(owner, out Vector3d a, out Vector3d b, out Vector3d c);
 5042451        new FixedTriangle(a, b, c).GetExactNormal(out Signed192 nx, out Signed192 ny, out _, out _);
 5042452        int axis = !nx.IsZero ? 0 : !ny.IsZero ? 1 : 2;
 10084853        Vector2d p = Project(vertices[first], axis), q = Project(vertices[second], axis);
 5042454        bool useX = p.X != q.X;
 5042455        Fixed64 low = FixedMath.Min(useX ? p.X : p.Y, useX ? q.X : q.Y);
 5042456        Fixed64 high = FixedMath.Max(useX ? p.X : p.Y, useX ? q.X : q.Y);
 74790857        for (int edge = 0; edge < perimeter.Length; edge += 2)
 58        {
 67449059            int start = perimeter[edge], end = perimeter[edge + 1];
 67449060            Vector2d s = Project(vertices[start], axis), t = Project(vertices[end], axis);
 33724561            if (Vector2d.OrientationSign(p, q, s) != 0 || Vector2d.OrientationSign(p, q, t) != 0
 33724562                || (useX ? s.X : s.Y) < low || (useX ? s.X : s.Y) > high
 33724563                || !frame.IntersectsSegment(new FixedSegment(vertices[start], vertices[end]))) continue;
 64            // A trusted exposed subedge with its start in this canonical span
 65            // cannot leave it: reduction merges monotone collinear chains,
 66            // while the embedding certificate rejects overlap/backtracking.
 6103167            foreach (int incident in mesh.GetWeldedVertexTriangleIndices(start))
 68            {
 2365869                if (mesh.GetWeldedTriangleVertexIndices(incident).IndexOf(end) < 0) continue;
 1400470                int region = connectivity.GetRegionOrdinal(incident);
 2771971                if (region >= 0) return region;
 72            }
 73        }
 3670974        return -1;
 75    }
 76
 77    internal static int GetIncidentFan(PhysicsMesh mesh, int owner, int origin, int opposite,
 78        bool interior, Span<WideAxis3> fan)
 79    {
 1969980        ReadOnlySpan<Vector3d> vertices = mesh.GeometryVertices;
 3939881        Vector3d p = vertices[origin], q = vertices[opposite];
 1969982        mesh.GetGeometryTriangleVertices(owner, out Vector3d a, out Vector3d b, out Vector3d c);
 1969983        new FixedTriangle(a, b, c).GetExactNormal(out Signed192 nx, out Signed192 ny, out _, out _);
 1969984        int axis = !nx.IsZero ? 0 : !ny.IsZero ? 1 : 2;
 3939885        Vector2d projectedOrigin = Project(p, axis), projectedOpposite = Project(q, axis);
 1969986        fan[0] = Ray(p, q);
 1969987        if (interior)
 88        {
 1103589            fan[1] = Negate(fan[0]);
 1103590            int inward = -1;
 1103591            bool useX = projectedOpposite.X != projectedOrigin.X;
 1103592            int direction = useX ? projectedOpposite.X.CompareTo(projectedOrigin.X)
 1103593                : projectedOpposite.Y.CompareTo(projectedOrigin.Y);
 4834994            foreach (int incident in mesh.GetWeldedVertexTriangleIndices(origin))
 95            {
 1865796                if (mesh.GetManifoldSurfaceOwner(incident) != owner) continue;
 1588497                ReadOnlySpan<int> triangle = mesh.GetWeldedTriangleVertexIndices(incident);
 1588498                bool onEdge = false;
 12707299                foreach (int vertex in triangle)
 100                {
 47652101                    Vector2d v = Project(vertices[vertex], axis);
 102                    // Only the local ray direction matters. A positive
 103                    // collinear incident edge supplies the same boundary
 104                    // half-neighborhood after ±edge enforces tangency.
 47652105                    if (v != projectedOrigin && Vector2d.OrientationSign(projectedOrigin, projectedOpposite, v) == 0
 58687106                        && (useX ? v.X.CompareTo(projectedOrigin.X) : v.Y.CompareTo(projectedOrigin.Y)) == direction) on
 107                }
 15884108                if (!onEdge) continue;
 88280109                foreach (int vertex in triangle)
 33105110                    if (Vector2d.OrientationSign(projectedOrigin, projectedOpposite, Project(vertices[vertex], axis)) !=
 11035111                        inward = vertex;
 11035112                break;
 113            }
 114            System.Diagnostics.Debug.Assert(inward >= 0);
 11035115            fan[2] = Ray(p, vertices[inward]);
 11035116            return 3;
 117        }
 8664118        int neighbor = -1;
 8664119        ReadOnlySpan<int> boundary = mesh.GetCanonicalSurfaceBoundaryVertexPairs(owner);
 180746120        for (int edge = 0; edge < boundary.Length; edge += 2)
 121        {
 163418122            int first = boundary[edge], second = boundary[edge + 1];
 88789123            if (first == origin && second != opposite) neighbor = second;
 76213124            else if (second == origin && first != opposite) neighbor = first;
 125        }
 126        System.Diagnostics.Debug.Assert(neighbor >= 0);
 8664127        fan[1] = Ray(p, vertices[neighbor]);
 8664128        Vector2d projectedNeighbor = Project(vertices[neighbor], axis);
 8664129        int orientation = Vector2d.OrientationSign(projectedOrigin, projectedOpposite, projectedNeighbor);
 130        // A certified simple planar boundary has exactly two noncollinear
 131        // corner rays. Check the actual incident sectors before choosing their
 132        // minor positive hull: a reflex fan has only plane-normal support.
 61913133        foreach (int incident in mesh.GetWeldedVertexTriangleIndices(origin))
 134        {
 22375135            if (mesh.GetManifoldSurfaceOwner(incident) != owner) continue;
 114957136            foreach (int vertex in mesh.GetWeldedTriangleVertexIndices(incident))
 137            {
 43104138                Vector2d v = Project(vertices[vertex], axis);
 43104139                if (orientation * Vector2d.OrientationSign(projectedOrigin, projectedOpposite, v) >= 0
 43104140                    && orientation * Vector2d.OrientationSign(projectedOrigin, v, projectedNeighbor) >= 0) continue;
 330141                fan[2] = Negate(fan[0]); fan[3] = Negate(fan[1]);
 165142                return 4;
 143            }
 144        }
 8499145        return 2;
 146    }
 147
 947261148    private static Vector2d Project(Vector3d value, int axis) => axis == 0
 947261149        ? new Vector2d(value.Y, value.Z) : axis == 1 ? new Vector2d(value.X, value.Z) : new Vector2d(value.X, value.Y);
 150
 11365151    private static WideAxis3 Negate(WideAxis3 value) => new(
 11365152        WideArithmetic.Negate(value.X), WideArithmetic.Negate(value.Y), WideArithmetic.Negate(value.Z));
 153
 40574154    internal static WideAxis3 Ray(Vector3d first, Vector3d second) => new(
 40574155        Signed320.ExtendValue(WideArithmetic.SubtractSigned192(Signed192.Raw(second.X), Signed192.Raw(first.X))),
 40574156        Signed320.ExtendValue(WideArithmetic.SubtractSigned192(Signed192.Raw(second.Y), Signed192.Raw(first.Y))),
 40574157        Signed320.ExtendValue(WideArithmetic.SubtractSigned192(Signed192.Raw(second.Z), Signed192.Raw(first.Z))));
 158
 159    private interface ISection { bool IntersectsSegment(FixedSegment segment); }
 160
 161    private readonly struct ConeSection : ISection
 162    {
 163        private readonly ConePlaneRayFrame _frame;
 1479164        internal ConeSection(in ConePlaneRayFrame frame) => _frame = frame;
 1324165        public bool IntersectsSegment(FixedSegment segment) => _frame.IntersectsSegment(segment);
 166    }
 167
 168    private readonly struct CapsuleSection : ISection
 169    {
 170        private readonly CapsuleFiniteSectionFrame _frame;
 10285171        internal CapsuleSection(in CapsuleFiniteSectionFrame frame) => _frame = frame;
 13803172        public bool IntersectsSegment(FixedSegment segment) => _frame.IntersectsSegment(segment);
 173    }
 174
 175    private readonly struct SlabSection : ISection
 176    {
 177        private readonly FiniteSlabSectionFrame frame;
 178        private readonly LSCollider2D? embedded;
 179        private readonly ReadOnlyMemory<Vector2d> offsets;
 180        internal SlabSection(in FiniteSlabSectionFrame frame, LSCollider2D? embedded, ReadOnlyMemory<Vector2d> offsets =
 155328181        { this.frame = frame; this.embedded = embedded; this.offsets = offsets; }
 182        public bool IntersectsSegment(FixedSegment segment)
 183        {
 38788184            Span<Vector2d> box = stackalloc Vector2d[4];
 38788185            return frame.IntersectsSegment(segment, offsets.IsEmpty ? CollisionDetectionMixed.GetConvexPrismOffsets(embe
 186        }
 187    }
 188
 189    private readonly struct SlabSupport : ISection
 190    {
 191        private readonly MeshSlabFaceRegions faces;
 192        private readonly LSCollider2D? embedded;
 193        internal SlabSupport(MeshSlabFaceRegions faces, LSCollider2D? embedded)
 957194        { this.faces = faces; this.embedded = embedded; }
 195        public bool IntersectsSegment(FixedSegment segment)
 196        {
 383197            Span<Vector2d> box = stackalloc Vector2d[4];
 383198            return faces.IntersectsSupport(segment, faces.PrismOffsets.IsEmpty
 383199                ? CollisionDetectionMixed.GetConvexPrismOffsets(embedded, box) : faces.PrismOffsets.Span);
 200        }
 201    }
 202
 203    private readonly struct HullSupport : ISection
 204    {
 205        private readonly MeshHullSurfaceContacts faces;
 56206        internal HullSupport(MeshHullSurfaceContacts faces) => this.faces = faces;
 80207        public bool IntersectsSegment(FixedSegment segment) => faces.IntersectsSupport(segment);
 208    }
 209}

Methods/Properties

FindEdgeRegion(Gravitas.Colliders.PhysicsMesh,System.Int32,System.Int32,System.Int32,Gravitas.CollisionHandling.MeshSurfaceConnectivity,FixedMathSharp.Geometry.ConePlaneRayFrame&)
FindEdgeRegion(Gravitas.Colliders.PhysicsMesh,System.Int32,System.Int32,System.Int32,Gravitas.CollisionHandling.MeshSurfaceConnectivity,FixedMathSharp.Geometry.CapsuleFiniteSectionFrame&)
FindEdgeRegion(Gravitas.Colliders.PhysicsMesh,System.Int32,System.Int32,System.Int32,Gravitas.CollisionHandling.MeshSurfaceConnectivity,FixedMathSharp.Geometry.FiniteSlabSectionFrame&,Gravitas.Colliders.LSCollider2D)
FindEdgeRegion(Gravitas.Colliders.PhysicsMesh,System.Int32,System.Int32,System.Int32,Gravitas.CollisionHandling.MeshSurfaceConnectivity,FixedMathSharp.Geometry.FiniteSlabSectionFrame&,System.ReadOnlyMemory`1<FixedMathSharp.Vector2d>)
FindSupportEdgeRegion(Gravitas.Colliders.PhysicsMesh,System.Int32,System.Int32,System.Int32,Gravitas.CollisionHandling.MeshSurfaceConnectivity,Gravitas.CollisionHandling.MeshSlabFaceRegions,Gravitas.Colliders.LSCollider2D)
FindSupportEdgeRegion(Gravitas.Colliders.PhysicsMesh,System.Int32,System.Int32,System.Int32,Gravitas.CollisionHandling.MeshSurfaceConnectivity,Gravitas.CollisionHandling.MeshHullSurfaceContacts)
FindEdgeRegion(Gravitas.Colliders.PhysicsMesh,System.Int32,System.Int32,System.Int32,Gravitas.CollisionHandling.MeshSurfaceConnectivity,TSection&)
GetIncidentFan(Gravitas.Colliders.PhysicsMesh,System.Int32,System.Int32,System.Int32,System.Boolean,System.Span`1<FixedMathSharp.Geometry.WideAxis3>)
Project(FixedMathSharp.Vector3d,System.Int32)
Negate(FixedMathSharp.Geometry.WideAxis3)
Ray(FixedMathSharp.Vector3d,FixedMathSharp.Vector3d)
.ctor(FixedMathSharp.Geometry.ConePlaneRayFrame&)
IntersectsSegment(FixedMathSharp.Geometry.FixedSegment)
.ctor(FixedMathSharp.Geometry.CapsuleFiniteSectionFrame&)
IntersectsSegment(FixedMathSharp.Geometry.FixedSegment)
.ctor(FixedMathSharp.Geometry.FiniteSlabSectionFrame&,Gravitas.Colliders.LSCollider2D,System.ReadOnlyMemory`1<FixedMathSharp.Vector2d>)
IntersectsSegment(FixedMathSharp.Geometry.FixedSegment)
.ctor(Gravitas.CollisionHandling.MeshSlabFaceRegions,Gravitas.Colliders.LSCollider2D)
IntersectsSegment(FixedMathSharp.Geometry.FixedSegment)
.ctor(Gravitas.CollisionHandling.MeshHullSurfaceContacts)
IntersectsSegment(FixedMathSharp.Geometry.FixedSegment)