< Summary

Information
Class: SwiftCollections.Query.FixedBoundVolume
Assembly: SwiftCollections.FixedMathSharp
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections.FixedMathSharp/Query/Shared/Volume/FixedBoundVolume.cs
Line coverage
100%
Covered lines: 36
Uncovered lines: 0
Coverable lines: 36
Total lines: 179
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()50%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(...)100%22100%
op_Equality(...)100%11100%
op_Inequality(...)100%11100%
GetHashCode()100%11100%
ToString()100%11100%

File(s)

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections.FixedMathSharp/Query/Shared/Volume/FixedBoundVolume.cs

#LineLine coverage
 1//=======================================================================
 2// FixedBoundVolume.cs
 3//=======================================================================
 4// MIT License, Copyright (c) 2024–present David Oravsky (mrdav30)
 5// See LICENSE file in the project root for full license information.
 6//=======================================================================
 7
 8using FixedMathSharp;
 9using System;
 10using System.Runtime.CompilerServices;
 11
 12namespace SwiftCollections.Query;
 13
 14/// <summary>
 15/// Represents an axis-aligned bounding box (AABB) in 3D space using fixed-point math.
 16/// </summary>
 17public struct FixedBoundVolume : IBoundVolume<FixedBoundVolume>, IEquatable<FixedBoundVolume>
 18{
 19    private Vector3d _min;
 20    private Vector3d _max;
 21    private Vector3d _center;
 22    private Vector3d _size;
 23    private Fixed64 _volume;
 24    private bool _isDirty;
 25
 26    /// <summary>
 27    /// Initializes a new instance of the <see cref="FixedBoundVolume"/> struct.
 28    /// </summary>
 29    /// <param name="min">The minimum point of the volume.</param>
 30    /// <param name="max">The maximum point of the volume.</param>
 31    public FixedBoundVolume(Vector3d min, Vector3d max)
 32    {
 11733        _min = min;
 11734        _max = max;
 11735        _center = default;
 11736        _size = default;
 11737        _volume = default;
 11738        _isDirty = true;
 11739    }
 40
 41    /// <summary>
 42    /// Gets the minimum point of the bounding volume.
 43    /// </summary>
 81444    public readonly Vector3d Min => _min;
 45
 46    /// <summary>
 47    /// Gets the maximum point of the bounding volume.
 48    /// </summary>
 77049    public readonly Vector3d Max => _max;
 50
 51    /// <summary>
 52    /// Gets the center point of the bounding volume.
 53    /// </summary>
 54    public Vector3d Center
 55    {
 56        get
 57        {
 258            if (_isDirty)
 259                RecalculateMeta();
 260            return _center;
 61        }
 62    }
 63
 64    /// <summary>
 65    /// Gets the axis-aligned size of the bounding volume.
 66    /// </summary>
 67    public Vector3d Size
 68    {
 69        get
 70        {
 571            if (_isDirty)
 472                RecalculateMeta();
 573            return _size;
 74        }
 75    }
 76
 77    /// <summary>
 78    /// Gets the volume of the bounding box.
 79    /// </summary>
 80    public Fixed64 Volume
 81    {
 82        get
 83        {
 584            if (_isDirty)
 385                RecalculateMeta();
 586            return _volume;
 87        }
 88    }
 89
 90    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 91    private void RecalculateMeta()
 92    {
 993        _center = (_min + _max) * Fixed64.Half;
 994        _size = _max - _min;
 995        _volume = _size.X * _size.Y * _size.Z;
 996        _isDirty = false;
 997    }
 98
 99    /// <summary>
 100    /// Creates a new volume that represents the union of this volume and the specified volume.
 101    /// </summary>
 102    /// <remarks>
 103    /// The resulting volume is the smallest axis-aligned bounding box that fully contains both input volumes.
 104    /// </remarks>
 105    /// <param name="other">
 106    /// The volume to combine with this volume.
 107    /// The resulting volume will encompass both this volume and the specified volume.
 108    /// </param>
 109    /// <returns>A new FixedBoundVolume that contains both this volume and the specified volume.</returns>
 110    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 111    public readonly FixedBoundVolume Union(FixedBoundVolume other)
 112    {
 6113        return new FixedBoundVolume(Vector3d.Min(Min, other.Min), Vector3d.Max(Max, other.Max));
 114    }
 115
 116    /// <summary>
 117    /// Determines whether this volume intersects with the specified volume.
 118    /// </summary>
 119    /// <param name="other">The volume to test for intersection with this volume.</param>
 120    /// <returns>true if the volumes intersect; otherwise, false.</returns>
 121    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 122    public readonly bool Intersects(FixedBoundVolume other)
 123    {
 117124        return !(Min.X > other.Max.X || Max.X < other.Min.X ||
 117125                 Min.Y > other.Max.Y || Max.Y < other.Min.Y ||
 117126                 Min.Z > other.Max.Z || Max.Z < other.Min.Z);
 127    }
 128
 129    /// <summary>
 130    /// Calculates the additional volume required to expand the current volume to fully contain the specified volume.
 131    /// </summary>
 132    /// <param name="other">The volume to be encompassed by the current volume.</param>
 133    /// <returns>
 134    /// The minimum additional volume needed to contain the specified volume.
 135    /// Returns 0 if the current volume already contains the specified volume.
 136    /// </returns>
 137    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 138    public long GetCost(FixedBoundVolume other)
 139    {
 2140        Fixed64 delta = Union(other).Volume - Volume;
 2141        return delta <= Fixed64.Zero ? 0L : (long)delta.FloorToInt();
 142    }
 143
 144    /// <summary>
 145    /// Determines whether the bounds of this volume are equal to those of the specified volume.
 146    /// </summary>
 147    /// <param name="other">A <see cref="FixedBoundVolume"/> to compare with the current volume.</param>
 148    /// <returns>true if both the minimum and maximum bounds of the volumes are equal; otherwise, false.</returns>
 149    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 150    public readonly bool BoundsEquals(FixedBoundVolume other)
 151    {
 10152        return Min == other.Min && Max == other.Max;
 153    }
 154
 155    /// <inheritdoc/>
 5156    public readonly bool Equals(FixedBoundVolume other) => BoundsEquals(other);
 157
 158    /// <inheritdoc/>
 2159    public override readonly bool Equals(object obj) => obj is FixedBoundVolume other && BoundsEquals(other);
 160
 161    /// <summary>
 162    /// Determines whether two BoundVolume instances are equal.
 163    /// </summary>
 4164    public static bool operator ==(FixedBoundVolume left, FixedBoundVolume right) => left.Equals(right);
 165
 166    /// <summary>
 167    /// Determines whether two BoundVolume instances are not equal.
 168    /// </summary>
 2169    public static bool operator !=(FixedBoundVolume left, FixedBoundVolume right) => !(left == right);
 170
 171    /// <inheritdoc/>
 2172    public override readonly int GetHashCode() => HashCode.Combine(Min, Max);
 173
 174    /// <summary>
 175    /// Returns a string that represents the current object, including the minimum and maximum values.
 176    /// </summary>
 177    /// <returns>A string in the format "Min: {Min}, Max: {Max}" that displays the minimum and maximum values of the obj
 1178    public override readonly string ToString() => $"Min: {Min}, Max: {Max}";
 179}