< Summary

Information
Class: SwiftCollections.Query.BoundVolume
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Query/BoundingVolume/Volume/BoundVolume.cs
Line coverage
100%
Covered lines: 42
Uncovered lines: 0
Coverable lines: 42
Total lines: 170
Line coverage: 100%
Branch coverage
95%
Covered branches: 21
Total branches: 22
Branch coverage: 95.4%
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%
get_Max()100%11100%
get_Center()100%22100%
get_Size()100%22100%
get_Volume()100%22100%
RecalculateMeta()100%11100%
Union(...)100%11100%
Intersects(...)100%1010100%
GetCost(...)100%22100%
BoundsEquals(...)100%22100%
Equals(...)100%11100%
Equals(...)50%22100%
op_Equality(...)100%11100%
op_Inequality(...)100%11100%
GetHashCode()100%11100%
ToString()100%11100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Numerics;
 3using System.Runtime.CompilerServices;
 4
 5namespace SwiftCollections.Query;
 6
 7/// <summary>
 8/// Represents an axis-aligned bounding box (AABB) in 3D space.
 9/// </summary>
 10public struct BoundVolume : IBoundVolume<BoundVolume>, IEquatable<BoundVolume>
 11{
 12    /// <summary>
 13    /// The minimum point of the bounding volume.
 14    /// </summary>
 15    private Vector3 _min;
 16
 17    /// <summary>
 18    /// The maximum point of the bounding volume.
 19    /// </summary>
 20    private Vector3 _max;
 21
 22    /// <summary>
 23    /// The center of the bounding volume as the midpoint of the minimum and maximum points.
 24    /// </summary>
 25    private Vector3 _center;
 26
 27    /// <summary>
 28    /// The size of the bounding volume as the difference between the maximum and minimum points.
 29    /// </summary>
 30    private Vector3 _size;
 31
 32    /// <summary>
 33    /// The volume of the bounding box, calculated as the product of its dimensions.
 34    /// </summary>
 35    private double _volume;
 36
 37    /// <summary>
 38    /// Marks the bounding volume as dirty, indicating its properties need recalculation.
 39    /// </summary>
 40    private bool _isDirty;
 41
 42    /// <summary>
 43    /// Initializes a new instance of the <see cref="BoundVolume"/> struct with the specified minimum and maximum points
 44    /// </summary>
 45    /// <param name="min">The minimum point of the bounding volume.</param>
 46    /// <param name="max">The maximum point of the bounding volume.</param>
 47    public BoundVolume(Vector3 min, Vector3 max)
 48    {
 41766549        _min = min;
 41766550        _max = max;
 51
 41766552        _isDirty = true;
 41766553        _center = default;
 41766554        _size = default;
 41766555        _volume = default;
 41766556    }
 57
 58    /// <inheritdoc cref="_min"/>
 280410959    public readonly Vector3 Min => _min;
 60
 61    /// <inheritdoc cref="_max"/>
 280407762    public readonly Vector3 Max => _max;
 63
 64    /// <inheritdoc cref="_center"/>
 65    public Vector3 Center
 66    {
 67        get
 68        {
 669            if (_isDirty)
 370                RecalculateMeta();
 671            return _center;
 72        }
 73    }
 74
 75    /// <inheritdoc cref="_size"/>
 76    public Vector3 Size
 77    {
 78        get
 79        {
 680            if (_isDirty)
 581                RecalculateMeta();
 682            return _size;
 83        }
 84    }
 85
 86    /// <inheritdoc cref="_volume"/>
 87    public double Volume
 88    {
 89        get
 90        {
 25201491            if (_isDirty)
 25201392                RecalculateMeta();
 25201493            return _volume;
 94        }
 95    }
 96
 97    /// <summary>
 98    /// Forces recalculation of the bounding volume's metadata, such as center, size, and volume.
 99    /// </summary>
 100    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 101    private void RecalculateMeta()
 102    {
 252021103        _center = (_min + _max) / 2;
 252021104        _size = _max - _min;
 252021105        _volume = _size.X * _size.Y * _size.Z;
 252021106        _isDirty = false;
 252021107    }
 108
 109    /// <inheritdoc />
 110    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 111    public readonly BoundVolume Union(BoundVolume other)
 112    {
 404330113        return new BoundVolume(
 404330114            new Vector3(Math.Min(Min.X, other.Min.X), Math.Min(Min.Y, other.Min.Y), Math.Min(Min.Z, other.Min.Z)),
 404330115            new Vector3(Math.Max(Max.X, other.Max.X), Math.Max(Max.Y, other.Max.Y), Math.Max(Max.Z, other.Max.Z))
 404330116        );
 117    }
 118
 119    /// <inheritdoc />
 120    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 121    public readonly bool Intersects(BoundVolume other)
 122    {
 21142123        return !(Min.X > other.Max.X || Max.X < other.Min.X ||
 21142124                 Min.Y > other.Max.Y || Max.Y < other.Min.Y ||
 21142125                 Min.Z > other.Max.Z || Max.Z < other.Min.Z);
 126    }
 127
 128    /// <inheritdoc />
 129    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 130    public readonly long GetCost(BoundVolume other)
 131    {
 252013132        BoundVolume union = Union(other);
 252013133        Vector3 size = Max - Min;
 252013134        double volume = size.X * size.Y * size.Z;
 252013135        double delta = union.Volume - volume;
 252013136        return delta >= (double)long.MaxValue ? long.MaxValue : (long)delta;
 137    }
 138
 139    /// <inheritdoc />
 140    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 141    public readonly bool BoundsEquals(BoundVolume other)
 142    {
 15143        return Min.Equals(other.Min) && Max.Equals(other.Max);
 144    }
 145
 146    /// <inheritdoc />
 5147    public readonly bool Equals(BoundVolume other) => BoundsEquals(other);
 148
 149    /// <inheritdoc />
 1150    public override readonly bool Equals(object? obj) => obj is BoundVolume other && BoundsEquals(other);
 151
 152    /// <summary>
 153    /// Determines whether two BoundVolume instances are equal.
 154    /// </summary>
 4155    public static bool operator ==(BoundVolume left, BoundVolume right) => left.Equals(right);
 156
 157    /// <summary>
 158    /// Determines whether two BoundVolume instances are not equal.
 159    /// </summary>
 2160    public static bool operator !=(BoundVolume left, BoundVolume right) => !(left == right);
 161
 162    /// <inheritdoc />
 2163    public override readonly int GetHashCode() => HashCode.Combine(Min, Max);
 164
 165    /// <summary>
 166    /// Returns a string that represents the current object, including the minimum and maximum values.
 167    /// </summary>
 168    /// <returns>A string in the format "Min: {Min}, Max: {Max}" that displays the minimum and maximum values of the obj
 1169    public override readonly string ToString() => $"Min: {Min}, Max: {Max}";
 170}