| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | using System.Text.Json.Serialization; |
| | | 5 | | |
| | | 6 | | namespace 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] |
| | | 13 | | public 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) |
| | 518 | 40 | | { |
| | 518 | 41 | | Normal = normal; |
| | 518 | 42 | | D = d; |
| | 518 | 43 | | } |
| | | 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) |
| | 234 | 49 | | : this(new Vector3d(x, y, z), d) |
| | 234 | 50 | | { |
| | 234 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Initializes a new plane that contains the specified points. |
| | | 55 | | /// </summary> |
| | | 56 | | public FixedPlane(Vector3d a, Vector3d b, Vector3d c) |
| | 1 | 57 | | { |
| | 1 | 58 | | Vector3d ab = b - a; |
| | 1 | 59 | | Vector3d ac = c - a; |
| | | 60 | | |
| | 1 | 61 | | Normal = Vector3d.Cross(ab, ac).Normalize(); |
| | 1 | 62 | | D = -Vector3d.Dot(Normal, a); |
| | 1 | 63 | | } |
| | | 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) |
| | 1 | 69 | | { |
| | 1 | 70 | | Normal = normal; |
| | 1 | 71 | | D = -Vector3d.Dot(pointOnPlane, normal); |
| | 1 | 72 | | } |
| | | 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) |
| | 509 | 83 | | { |
| | 509 | 84 | | return Vector3d.Dot(Normal, value) + D; |
| | 509 | 85 | | } |
| | | 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) |
| | 26 | 92 | | { |
| | 26 | 93 | | return Vector3d.Dot(Normal, value); |
| | 26 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Normalizes this plane in place. |
| | | 98 | | /// </summary> |
| | | 99 | | public void Normalize() |
| | 1 | 100 | | { |
| | 1 | 101 | | this = Normalize(this); |
| | 1 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Gets a normalized copy of the specified plane. |
| | | 106 | | /// </summary> |
| | | 107 | | public static FixedPlane Normalize(FixedPlane value) |
| | 261 | 108 | | { |
| | 261 | 109 | | Fixed64 length = value.Normal.Magnitude; |
| | 261 | 110 | | if (length == Fixed64.Zero) |
| | 1 | 111 | | throw new InvalidOperationException("Cannot normalize a plane with a zero-length normal."); |
| | | 112 | | |
| | 260 | 113 | | Fixed64 factor = Fixed64.One / length; |
| | 260 | 114 | | return new FixedPlane(value.Normal * factor, value.D * factor); |
| | 260 | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Classifies a bounding box relative to this plane. |
| | | 119 | | /// </summary> |
| | | 120 | | public FixedPlaneIntersectionType Intersects(BoundingBox box) |
| | 51 | 121 | | { |
| | 51 | 122 | | return IntersectsBoxLike(box.Min, box.Max); |
| | 51 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Classifies a bounding area relative to this plane. |
| | | 127 | | /// </summary> |
| | | 128 | | public FixedPlaneIntersectionType Intersects(BoundingArea area) |
| | 23 | 129 | | { |
| | 23 | 130 | | return IntersectsBoxLike(area.Min, area.Max); |
| | 23 | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Classifies a bounding sphere relative to this plane. |
| | | 135 | | /// </summary> |
| | | 136 | | public FixedPlaneIntersectionType Intersects(BoundingSphere sphere) |
| | 48 | 137 | | { |
| | 48 | 138 | | Fixed64 distance = DotCoordinate(sphere.Center); |
| | | 139 | | |
| | 48 | 140 | | if (distance > sphere.Radius) |
| | 3 | 141 | | return FixedPlaneIntersectionType.Front; |
| | | 142 | | |
| | 45 | 143 | | if (distance < -sphere.Radius) |
| | 24 | 144 | | return FixedPlaneIntersectionType.Back; |
| | | 145 | | |
| | 21 | 146 | | return FixedPlaneIntersectionType.Intersecting; |
| | 48 | 147 | | } |
| | | 148 | | |
| | | 149 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 150 | | internal FixedPlaneIntersectionType Intersects(Vector3d point) |
| | 18 | 151 | | { |
| | 18 | 152 | | Fixed64 distance = DotCoordinate(point); |
| | | 153 | | |
| | 18 | 154 | | if (distance > Fixed64.Zero) |
| | 9 | 155 | | return FixedPlaneIntersectionType.Front; |
| | | 156 | | |
| | 9 | 157 | | if (distance < Fixed64.Zero) |
| | 9 | 158 | | return FixedPlaneIntersectionType.Back; |
| | | 159 | | |
| | 0 | 160 | | return FixedPlaneIntersectionType.Intersecting; |
| | 18 | 161 | | } |
| | | 162 | | |
| | | 163 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 164 | | private FixedPlaneIntersectionType IntersectsBoxLike(Vector3d min, Vector3d max) |
| | 74 | 165 | | { |
| | 74 | 166 | | Vector3d positive = new( |
| | 74 | 167 | | Normal.x >= Fixed64.Zero ? max.x : min.x, |
| | 74 | 168 | | Normal.y >= Fixed64.Zero ? max.y : min.y, |
| | 74 | 169 | | Normal.z >= Fixed64.Zero ? max.z : min.z); |
| | | 170 | | |
| | 74 | 171 | | if (DotCoordinate(positive) < Fixed64.Zero) |
| | 34 | 172 | | return FixedPlaneIntersectionType.Back; |
| | | 173 | | |
| | 40 | 174 | | Vector3d negative = new( |
| | 40 | 175 | | Normal.x >= Fixed64.Zero ? min.x : max.x, |
| | 40 | 176 | | Normal.y >= Fixed64.Zero ? min.y : max.y, |
| | 40 | 177 | | Normal.z >= Fixed64.Zero ? min.z : max.z); |
| | | 178 | | |
| | 40 | 179 | | if (DotCoordinate(negative) > Fixed64.Zero) |
| | 7 | 180 | | return FixedPlaneIntersectionType.Front; |
| | | 181 | | |
| | 33 | 182 | | return FixedPlaneIntersectionType.Intersecting; |
| | 74 | 183 | | } |
| | | 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) |
| | 1 | 189 | | { |
| | 1 | 190 | | normal = Normal; |
| | 1 | 191 | | d = D; |
| | 1 | 192 | | } |
| | | 193 | | |
| | | 194 | | #endregion |
| | | 195 | | |
| | | 196 | | #region Operators |
| | | 197 | | |
| | | 198 | | /// <summary> |
| | | 199 | | /// Determines whether two planes are equal. |
| | | 200 | | /// </summary> |
| | | 201 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 202 | | 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)] |
| | 37 | 208 | | 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)] |
| | 2 | 216 | | public override bool Equals(object? obj) => obj is FixedPlane other && Equals(other); |
| | | 217 | | |
| | | 218 | | /// <inheritdoc/> |
| | | 219 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 50 | 220 | | public bool Equals(FixedPlane other) => Normal.Equals(other.Normal) && D.Equals(other.D); |
| | | 221 | | |
| | | 222 | | /// <inheritdoc/> |
| | | 223 | | public override int GetHashCode() |
| | 14 | 224 | | { |
| | | 225 | | unchecked |
| | 14 | 226 | | { |
| | 14 | 227 | | int hash = 17; |
| | 14 | 228 | | hash = hash * 23 + Normal.GetHashCode(); |
| | 14 | 229 | | hash = hash * 23 + D.GetHashCode(); |
| | 14 | 230 | | return hash; |
| | | 231 | | } |
| | 14 | 232 | | } |
| | | 233 | | |
| | | 234 | | #endregion |
| | | 235 | | } |