< Summary

Information
Class: FixedMathSharp.FixedPlane
Assembly: FixedMathSharp
File(s): /home/runner/work/FixedMathSharp/FixedMathSharp/src/FixedMathSharp/Geometry/Primitives/FixedPlane.cs
Line coverage
98%
Covered lines: 84
Uncovered lines: 1
Coverable lines: 85
Total lines: 235
Line coverage: 98.8%
Branch coverage
96%
Covered branches: 29
Total branches: 30
Branch coverage: 96.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
DotCoordinate(...)100%11100%
DotNormal(...)100%11100%
Normalize()100%11100%
Normalize(...)100%22100%
Intersects(...)100%11100%
Intersects(...)100%11100%
Intersects(...)100%44100%
Intersects(...)75%4487.5%
IntersectsBoxLike(...)100%1616100%
Deconstruct(...)100%11100%
op_Equality(...)100%11100%
op_Inequality(...)100%11100%
Equals(...)100%22100%
Equals(...)100%22100%
GetHashCode()100%11100%

File(s)

/home/runner/work/FixedMathSharp/FixedMathSharp/src/FixedMathSharp/Geometry/Primitives/FixedPlane.cs

#LineLine coverage
 1using MemoryPack;
 2using System;
 3using System.Runtime.CompilerServices;
 4using System.Text.Json.Serialization;
 5
 6namespace FixedMathSharp;
 7
 8/// <summary>
 9/// Represents a plane in 3D space using a normal vector and a distance from the origin.
 10/// </summary>
 11[Serializable]
 12[MemoryPackable]
 13public partial struct FixedPlane : IEquatable<FixedPlane>
 14{
 15    #region Fields
 16
 17    /// <summary>
 18    /// The plane normal.
 19    /// </summary>
 20    [JsonInclude]
 21    [MemoryPackOrder(0)]
 22    public Vector3d Normal;
 23
 24    /// <summary>
 25    /// The plane distance component.
 26    /// </summary>
 27    [JsonInclude]
 28    [MemoryPackOrder(1)]
 29    public Fixed64 D;
 30
 31    #endregion
 32
 33    #region Constructors
 34
 35    /// <summary>
 36    /// Initializes a new plane from a normal and distance component.
 37    /// </summary>
 38    [JsonConstructor]
 39    public FixedPlane(Vector3d normal, Fixed64 d)
 51840    {
 51841        Normal = normal;
 51842        D = d;
 51843    }
 44
 45    /// <summary>
 46    /// Initializes a new plane from the normal components and distance component.
 47    /// </summary>
 48    public FixedPlane(Fixed64 x, Fixed64 y, Fixed64 z, Fixed64 d)
 23449        : this(new Vector3d(x, y, z), d)
 23450    {
 23451    }
 52
 53    /// <summary>
 54    /// Initializes a new plane that contains the specified points.
 55    /// </summary>
 56    public FixedPlane(Vector3d a, Vector3d b, Vector3d c)
 157    {
 158        Vector3d ab = b - a;
 159        Vector3d ac = c - a;
 60
 161        Normal = Vector3d.Cross(ab, ac).Normalize();
 162        D = -Vector3d.Dot(Normal, a);
 163    }
 64
 65    /// <summary>
 66    /// Initializes a new plane that contains the specified point with the specified normal.
 67    /// </summary>
 68    public FixedPlane(Vector3d pointOnPlane, Vector3d normal)
 169    {
 170        Normal = normal;
 171        D = -Vector3d.Dot(pointOnPlane, normal);
 172    }
 73
 74    #endregion
 75
 76    #region Methods
 77
 78    /// <summary>
 79    /// Gets the dot product of the specified coordinate and this plane.
 80    /// </summary>
 81    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 82    public Fixed64 DotCoordinate(Vector3d value)
 50983    {
 50984        return Vector3d.Dot(Normal, value) + D;
 50985    }
 86
 87    /// <summary>
 88    /// Gets the dot product of the specified vector and this plane's normal.
 89    /// </summary>
 90    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 91    public Fixed64 DotNormal(Vector3d value)
 2692    {
 2693        return Vector3d.Dot(Normal, value);
 2694    }
 95
 96    /// <summary>
 97    /// Normalizes this plane in place.
 98    /// </summary>
 99    public void Normalize()
 1100    {
 1101        this = Normalize(this);
 1102    }
 103
 104    /// <summary>
 105    /// Gets a normalized copy of the specified plane.
 106    /// </summary>
 107    public static FixedPlane Normalize(FixedPlane value)
 261108    {
 261109        Fixed64 length = value.Normal.Magnitude;
 261110        if (length == Fixed64.Zero)
 1111            throw new InvalidOperationException("Cannot normalize a plane with a zero-length normal.");
 112
 260113        Fixed64 factor = Fixed64.One / length;
 260114        return new FixedPlane(value.Normal * factor, value.D * factor);
 260115    }
 116
 117    /// <summary>
 118    /// Classifies a bounding box relative to this plane.
 119    /// </summary>
 120    public FixedPlaneIntersectionType Intersects(BoundingBox box)
 51121    {
 51122        return IntersectsBoxLike(box.Min, box.Max);
 51123    }
 124
 125    /// <summary>
 126    /// Classifies a bounding area relative to this plane.
 127    /// </summary>
 128    public FixedPlaneIntersectionType Intersects(BoundingArea area)
 23129    {
 23130        return IntersectsBoxLike(area.Min, area.Max);
 23131    }
 132
 133    /// <summary>
 134    /// Classifies a bounding sphere relative to this plane.
 135    /// </summary>
 136    public FixedPlaneIntersectionType Intersects(BoundingSphere sphere)
 48137    {
 48138        Fixed64 distance = DotCoordinate(sphere.Center);
 139
 48140        if (distance > sphere.Radius)
 3141            return FixedPlaneIntersectionType.Front;
 142
 45143        if (distance < -sphere.Radius)
 24144            return FixedPlaneIntersectionType.Back;
 145
 21146        return FixedPlaneIntersectionType.Intersecting;
 48147    }
 148
 149    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 150    internal FixedPlaneIntersectionType Intersects(Vector3d point)
 18151    {
 18152        Fixed64 distance = DotCoordinate(point);
 153
 18154        if (distance > Fixed64.Zero)
 9155            return FixedPlaneIntersectionType.Front;
 156
 9157        if (distance < Fixed64.Zero)
 9158            return FixedPlaneIntersectionType.Back;
 159
 0160        return FixedPlaneIntersectionType.Intersecting;
 18161    }
 162
 163    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 164    private FixedPlaneIntersectionType IntersectsBoxLike(Vector3d min, Vector3d max)
 74165    {
 74166        Vector3d positive = new(
 74167            Normal.x >= Fixed64.Zero ? max.x : min.x,
 74168            Normal.y >= Fixed64.Zero ? max.y : min.y,
 74169            Normal.z >= Fixed64.Zero ? max.z : min.z);
 170
 74171        if (DotCoordinate(positive) < Fixed64.Zero)
 34172            return FixedPlaneIntersectionType.Back;
 173
 40174        Vector3d negative = new(
 40175            Normal.x >= Fixed64.Zero ? min.x : max.x,
 40176            Normal.y >= Fixed64.Zero ? min.y : max.y,
 40177            Normal.z >= Fixed64.Zero ? min.z : max.z);
 178
 40179        if (DotCoordinate(negative) > Fixed64.Zero)
 7180            return FixedPlaneIntersectionType.Front;
 181
 33182        return FixedPlaneIntersectionType.Intersecting;
 74183    }
 184
 185    /// <summary>
 186    /// Deconstructs the plane into its normal and distance components.
 187    /// </summary>
 188    public void Deconstruct(out Vector3d normal, out Fixed64 d)
 1189    {
 1190        normal = Normal;
 1191        d = D;
 1192    }
 193
 194    #endregion
 195
 196    #region Operators
 197
 198    /// <summary>
 199    /// Determines whether two planes are equal.
 200    /// </summary>
 201    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 2202    public static bool operator ==(FixedPlane left, FixedPlane right) => left.Equals(right);
 203
 204    /// <summary>
 205    /// Determines whether two planes are not equal.
 206    /// </summary>
 207    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 37208    public static bool operator !=(FixedPlane left, FixedPlane right) => !left.Equals(right);
 209
 210    #endregion
 211
 212    #region Equality
 213
 214    /// <inheritdoc/>
 215    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 2216    public override bool Equals(object? obj) => obj is FixedPlane other && Equals(other);
 217
 218    /// <inheritdoc/>
 219    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 50220    public bool Equals(FixedPlane other) => Normal.Equals(other.Normal) && D.Equals(other.D);
 221
 222    /// <inheritdoc/>
 223    public override int GetHashCode()
 14224    {
 225        unchecked
 14226        {
 14227            int hash = 17;
 14228            hash = hash * 23 + Normal.GetHashCode();
 14229            hash = hash * 23 + D.GetHashCode();
 14230            return hash;
 231        }
 14232    }
 233
 234    #endregion
 235}