| | | 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 ray with an origin and direction in three-dimensional space. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// Intersection methods return the ray parameter for the first forward hit. If <see cref="Direction"/> is normalized, |
| | | 13 | | /// that parameter is also the distance from <see cref="Position"/>. |
| | | 14 | | /// </remarks> |
| | | 15 | | [Serializable] |
| | | 16 | | [MemoryPackable] |
| | | 17 | | public partial struct FixedRay : IEquatable<FixedRay> |
| | | 18 | | { |
| | | 19 | | #region Fields |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// The origin of the ray. |
| | | 23 | | /// </summary> |
| | | 24 | | [JsonInclude] |
| | | 25 | | [MemoryPackOrder(0)] |
| | | 26 | | public Vector3d Position; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// The direction of the ray. |
| | | 30 | | /// </summary> |
| | | 31 | | [JsonInclude] |
| | | 32 | | [MemoryPackOrder(1)] |
| | | 33 | | public Vector3d Direction; |
| | | 34 | | |
| | | 35 | | #endregion |
| | | 36 | | |
| | | 37 | | #region Constructors |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Initializes a new ray with the specified origin and direction. |
| | | 41 | | /// </summary> |
| | | 42 | | [JsonConstructor] |
| | | 43 | | public FixedRay(Vector3d position, Vector3d direction) |
| | 33 | 44 | | { |
| | 33 | 45 | | Position = position; |
| | 33 | 46 | | Direction = direction; |
| | 33 | 47 | | } |
| | | 48 | | |
| | | 49 | | #endregion |
| | | 50 | | |
| | | 51 | | #region Methods |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Finds the first forward intersection with the specified plane. |
| | | 55 | | /// </summary> |
| | | 56 | | public Fixed64? Intersects(FixedPlane plane) |
| | 3 | 57 | | { |
| | 3 | 58 | | Fixed64 denominator = plane.DotNormal(Direction); |
| | 3 | 59 | | if (IsNearlyZero(denominator)) |
| | 1 | 60 | | return null; |
| | | 61 | | |
| | 2 | 62 | | Fixed64 t = -plane.DotCoordinate(Position) / denominator; |
| | 2 | 63 | | return t < Fixed64.Zero ? null : t; |
| | 3 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Finds the first forward intersection with the specified bounding box. |
| | | 68 | | /// </summary> |
| | | 69 | | public Fixed64? Intersects(BoundingBox box) |
| | 9 | 70 | | { |
| | 9 | 71 | | return IntersectsBoxLike(box.Min, box.Max); |
| | 9 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Finds the first forward intersection with the specified bounding area. |
| | | 76 | | /// </summary> |
| | | 77 | | public Fixed64? Intersects(BoundingArea area) |
| | 1 | 78 | | { |
| | 1 | 79 | | return IntersectsBoxLike(area.Min, area.Max); |
| | 1 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Finds the first forward intersection with the specified bounding sphere. |
| | | 84 | | /// </summary> |
| | | 85 | | public Fixed64? Intersects(BoundingSphere sphere) |
| | 7 | 86 | | { |
| | 7 | 87 | | Fixed64 directionLengthSquared = Direction.SqrMagnitude; |
| | 7 | 88 | | if (directionLengthSquared == Fixed64.Zero) |
| | 2 | 89 | | return sphere.Contains(Position) ? Fixed64.Zero : null; |
| | | 90 | | |
| | 5 | 91 | | Vector3d offset = Position - sphere.Center; |
| | 5 | 92 | | Fixed64 c = Vector3d.Dot(offset, offset) - sphere.SqrRadius; |
| | 5 | 93 | | if (c <= Fixed64.Zero) |
| | 1 | 94 | | return Fixed64.Zero; |
| | | 95 | | |
| | 4 | 96 | | Fixed64 b = Vector3d.Dot(offset, Direction); |
| | 4 | 97 | | if (b > Fixed64.Zero) |
| | 1 | 98 | | return null; |
| | | 99 | | |
| | 3 | 100 | | Fixed64 discriminant = (b * b) - (directionLengthSquared * c); |
| | 3 | 101 | | if (discriminant < Fixed64.Zero) |
| | 1 | 102 | | return null; |
| | | 103 | | |
| | 2 | 104 | | Fixed64 t = (-b - FixedMath.Sqrt(discriminant)) / directionLengthSquared; |
| | 2 | 105 | | return t < Fixed64.Zero ? null : t; |
| | 7 | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Finds the first forward intersection with the specified frustum. |
| | | 110 | | /// </summary> |
| | | 111 | | public Fixed64? Intersects(BoundingFrustum frustum) |
| | 5 | 112 | | { |
| | 5 | 113 | | if (frustum == null) |
| | 1 | 114 | | throw new ArgumentNullException(nameof(frustum)); |
| | | 115 | | |
| | 4 | 116 | | return frustum.Intersects(this); |
| | 4 | 117 | | } |
| | | 118 | | |
| | | 119 | | private Fixed64? IntersectsBoxLike(Vector3d min, Vector3d max) |
| | 10 | 120 | | { |
| | 10 | 121 | | Fixed64 tMin = Fixed64.Zero; |
| | 10 | 122 | | Fixed64 tMax = Fixed64.MAX_VALUE; |
| | | 123 | | |
| | 10 | 124 | | if (!ClipAxis(Position.x, Direction.x, min.x, max.x, ref tMin, ref tMax)) |
| | 2 | 125 | | return null; |
| | | 126 | | |
| | 8 | 127 | | if (!ClipAxis(Position.y, Direction.y, min.y, max.y, ref tMin, ref tMax)) |
| | 1 | 128 | | return null; |
| | | 129 | | |
| | 7 | 130 | | if (!ClipAxis(Position.z, Direction.z, min.z, max.z, ref tMin, ref tMax)) |
| | 2 | 131 | | return null; |
| | | 132 | | |
| | 5 | 133 | | return tMax < Fixed64.Zero ? null : tMin; |
| | 10 | 134 | | } |
| | | 135 | | |
| | | 136 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 137 | | private static bool ClipAxis( |
| | | 138 | | Fixed64 position, |
| | | 139 | | Fixed64 direction, |
| | | 140 | | Fixed64 min, |
| | | 141 | | Fixed64 max, |
| | | 142 | | ref Fixed64 tMin, |
| | | 143 | | ref Fixed64 tMax) |
| | 25 | 144 | | { |
| | 25 | 145 | | if (IsNearlyZero(direction)) |
| | 17 | 146 | | return position >= min && position <= max; |
| | | 147 | | |
| | 8 | 148 | | Fixed64 t1 = (min - position) / direction; |
| | 8 | 149 | | Fixed64 t2 = (max - position) / direction; |
| | | 150 | | |
| | 8 | 151 | | if (t1 > t2) |
| | 1 | 152 | | { |
| | 1 | 153 | | Fixed64 temp = t1; |
| | 1 | 154 | | t1 = t2; |
| | 1 | 155 | | t2 = temp; |
| | 1 | 156 | | } |
| | | 157 | | |
| | 8 | 158 | | if (t1 > tMin) |
| | 4 | 159 | | tMin = t1; |
| | | 160 | | |
| | 8 | 161 | | if (t2 < tMax) |
| | 8 | 162 | | tMax = t2; |
| | | 163 | | |
| | 8 | 164 | | return tMin <= tMax; |
| | 25 | 165 | | } |
| | | 166 | | |
| | | 167 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 168 | | internal static bool IsNearlyZero(Fixed64 value) |
| | 51 | 169 | | { |
| | 51 | 170 | | return value.Abs() <= Fixed64.Epsilon; |
| | 51 | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Deconstructs the ray into its origin and direction. |
| | | 175 | | /// </summary> |
| | | 176 | | public void Deconstruct(out Vector3d position, out Vector3d direction) |
| | 1 | 177 | | { |
| | 1 | 178 | | position = Position; |
| | 1 | 179 | | direction = Direction; |
| | 1 | 180 | | } |
| | | 181 | | |
| | | 182 | | #endregion |
| | | 183 | | |
| | | 184 | | #region Operators |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Determines whether two rays are equal. |
| | | 188 | | /// </summary> |
| | | 189 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 190 | | public static bool operator ==(FixedRay left, FixedRay right) => left.Equals(right); |
| | | 191 | | |
| | | 192 | | /// <summary> |
| | | 193 | | /// Determines whether two rays are not equal. |
| | | 194 | | /// </summary> |
| | | 195 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 196 | | public static bool operator !=(FixedRay left, FixedRay right) => !left.Equals(right); |
| | | 197 | | |
| | | 198 | | #endregion |
| | | 199 | | |
| | | 200 | | #region Equality |
| | | 201 | | |
| | | 202 | | /// <inheritdoc/> |
| | | 203 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 204 | | public override bool Equals(object? obj) => obj is FixedRay other && Equals(other); |
| | | 205 | | |
| | | 206 | | /// <inheritdoc/> |
| | | 207 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 208 | | public bool Equals(FixedRay other) |
| | 8 | 209 | | { |
| | 8 | 210 | | return Position.Equals(other.Position) && Direction.Equals(other.Direction); |
| | 8 | 211 | | } |
| | | 212 | | |
| | | 213 | | /// <inheritdoc/> |
| | | 214 | | public override int GetHashCode() |
| | 2 | 215 | | { |
| | | 216 | | unchecked |
| | 2 | 217 | | { |
| | 2 | 218 | | int hash = 17; |
| | 2 | 219 | | hash = hash * 23 + Position.GetHashCode(); |
| | 2 | 220 | | hash = hash * 23 + Direction.GetHashCode(); |
| | 2 | 221 | | return hash; |
| | | 222 | | } |
| | 2 | 223 | | } |
| | | 224 | | |
| | | 225 | | #endregion |
| | | 226 | | } |