< Summary

Information
Class: SwiftCollections.Query.BoundVolume
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Query/BoundingVolume/Volume/BoundVolume.cs
Line coverage
87%
Covered lines: 55
Uncovered lines: 8
Coverable lines: 63
Total lines: 162
Line coverage: 87.3%
Branch coverage
100%
Covered branches: 28
Total branches: 28
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%
get_Min()100%11100%
set_Min(...)100%210%
get_Max()100%11100%
set_Max(...)100%210%
get_Center()100%22100%
get_Size()100%22100%
get_Volume()100%22100%
RecalculateMeta()100%11100%
Union(...)100%44100%
Union(...)100%11100%
Intersects(...)100%1414100%
GetCost(...)100%44100%
ToString()100%11100%

File(s)

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Query/BoundingVolume/Volume/BoundVolume.cs

#LineLine coverage
 1
 2using System;
 3using System.Numerics;
 4using System.Runtime.CompilerServices;
 5
 6namespace SwiftCollections.Query
 7{
 8    /// <summary>
 9    /// Represents an axis-aligned bounding box (AABB) in 3D space.
 10    /// </summary>
 11    public struct BoundVolume : IBoundVolume
 12    {
 13        /// <summary>
 14        /// The minimum point of the bounding volume.
 15        /// </summary>
 16        private Vector3 _min;
 17
 18        /// <summary>
 19        /// The maximum point of the bounding volume.
 20        /// </summary>
 21        private Vector3 _max;
 22
 23        /// <summary>
 24        /// The center of the bounding volume as the midpoint of the minimum and maximum points.
 25        /// </summary>
 26        private Vector3 _center;
 27
 28        /// <summary>
 29        /// The size of the bounding volume as the difference between the maximum and minimum points.
 30        /// </summary>
 31        private Vector3 _size;
 32
 33        /// <summary>
 34        /// The volume of the bounding box, calculated as the product of its dimensions.
 35        /// </summary>
 36        private double _volume;
 37
 38        /// <summary>
 39        /// Marks the bounding volume as dirty, indicating its properties need recalculation.
 40        /// </summary>
 41        private bool _isDirty;
 42
 43        public BoundVolume(Vector3 min, Vector3 max)
 36697744        {
 36697745            _min = min;
 36697746            _max = max;
 47
 36697748            _isDirty = true;
 36697749            _center = default;
 36697750            _size = default;
 36697751            _volume = default;
 36697752        }
 53
 54        /// <inheritdoc cref="_min"/>
 55        public Vector3 Min
 56        {
 224154357            get => _min;
 58            private set
 059            {
 060                _isDirty = true;
 061                _min = value;
 062            }
 63        }
 64
 65        /// <inheritdoc cref="_max"/>
 66        public Vector3 Max
 67        {
 224154368            get => _max;
 69            private set
 070            {
 071                _isDirty = true;
 072                _max = value;
 073            }
 74        }
 75
 76        /// <inheritdoc cref="_center"/>
 77        public Vector3 Center
 78        {
 79            get
 480            {
 481                if (_isDirty)
 182                    RecalculateMeta();
 483                return _center;
 484            }
 85        }
 86
 87        /// <inheritdoc cref="_size"/>
 88        public Vector3 Size
 89        {
 90            get
 291            {
 292                if (_isDirty)
 193                    RecalculateMeta();
 294                return _size;
 295            }
 96        }
 97
 98        /// <inheritdoc cref="_volume"/>
 99        public double Volume
 100        {
 101            get
 356793102            {
 356793103                if (_isDirty)
 356792104                    RecalculateMeta();
 356793105                return _volume;
 356793106            }
 107        }
 108
 109        /// <summary>
 110        /// Forces recalculation of the bounding volume's metadata, such as center, size, and volume.
 111        /// </summary>
 112        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 113        private void RecalculateMeta()
 356794114        {
 356794115            _center = (_min + _max) / 2;
 356794116            _size = _max - _min;
 356794117            _volume = _size.X * _size.Y * _size.Z;
 356794118            _isDirty = false;
 356794119        }
 120
 121        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 122        public IBoundVolume Union(IBoundVolume other)
 173163123        {
 173163124            if (other is not BoundVolume otherBV)
 1125                throw new ArgumentException($"Mismatched bounding volume type detected!: {nameof(other)}");
 126
 173162127            return Union(otherBV);
 173162128        }
 129
 130        /// <inheritdoc cref="Union(IBoundVolume)"/>
 131        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 132        public BoundVolume Union(BoundVolume other)
 351559133        {
 351559134            return new BoundVolume(
 351559135                new Vector3(Math.Min(Min.X, other.Min.X), Math.Min(Min.Y, other.Min.Y), Math.Min(Min.Z, other.Min.Z)),
 351559136                new Vector3(Math.Max(Max.X, other.Max.X), Math.Max(Max.Y, other.Max.Y), Math.Max(Max.Z, other.Max.Z))
 351559137            );
 351559138        }
 139
 140        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 141        public bool Intersects(IBoundVolume other)
 22286142        {
 22286143            if (other is not BoundVolume otherBVH)
 1144                throw new ArgumentException($"Mismatched bounding volume type detected!: {nameof(other)}");
 145
 22285146            return !(Min.X > otherBVH.Max.X || Max.X < otherBVH.Min.X ||
 22285147                     Min.Y > otherBVH.Max.Y || Max.Y < otherBVH.Min.Y ||
 22285148                     Min.Z > otherBVH.Max.Z || Max.Z < otherBVH.Min.Z);
 22285149        }
 150
 151        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 152        public int GetCost(IBoundVolume other)
 178397153        {
 178397154            if (other is not BoundVolume otherBVH)
 1155                throw new ArgumentException($"Mismatched bounding volume type detected!: {nameof(other)}");
 156
 178396157            return (int)Math.Floor(Union(otherBVH).Volume - otherBVH.Volume);
 178396158        }
 159
 1160        public override string ToString() => $"Min: {Min}, Max: {Max}";
 161    }
 162}