| | | 1 | | //======================================================================= |
| | | 2 | | // FixedSegment.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 | | |
| | | 8 | | using MemoryPack; |
| | | 9 | | using System; |
| | | 10 | | using System.Runtime.CompilerServices; |
| | | 11 | | using System.Text.Json.Serialization; |
| | | 12 | | |
| | | 13 | | namespace FixedMathSharp.Bounds; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Represents a finite line segment in three-dimensional fixed-point space. |
| | | 17 | | /// </summary> |
| | | 18 | | [Serializable] |
| | | 19 | | [MemoryPackable] |
| | | 20 | | public partial struct FixedSegment : IEquatable<FixedSegment> |
| | | 21 | | { |
| | | 22 | | #region Fields |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// The start point of the segment. |
| | | 26 | | /// </summary> |
| | | 27 | | [JsonInclude] |
| | | 28 | | [MemoryPackOrder(0)] |
| | | 29 | | public Vector3d Start; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// The end point of the segment. |
| | | 33 | | /// </summary> |
| | | 34 | | [JsonInclude] |
| | | 35 | | [MemoryPackOrder(1)] |
| | | 36 | | public Vector3d End; |
| | | 37 | | |
| | | 38 | | #endregion |
| | | 39 | | |
| | | 40 | | #region Constructors |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Initializes a new segment from start and end points. |
| | | 44 | | /// </summary> |
| | | 45 | | [JsonConstructor] |
| | | 46 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 47 | | public FixedSegment(Vector3d start, Vector3d end) |
| | | 48 | | { |
| | 42 | 49 | | Start = start; |
| | 42 | 50 | | End = end; |
| | 42 | 51 | | } |
| | | 52 | | |
| | | 53 | | #endregion |
| | | 54 | | |
| | | 55 | | #region Properties |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// The vector from <see cref="Start"/> to <see cref="End"/>. |
| | | 59 | | /// </summary> |
| | | 60 | | [JsonIgnore] |
| | | 61 | | [MemoryPackIgnore] |
| | | 62 | | public Vector3d Delta |
| | | 63 | | { |
| | | 64 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 6 | 65 | | get => End - Start; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// The segment length. |
| | | 70 | | /// </summary> |
| | | 71 | | [JsonIgnore] |
| | | 72 | | [MemoryPackIgnore] |
| | | 73 | | public Fixed64 Length |
| | | 74 | | { |
| | | 75 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 76 | | get => Delta.Magnitude; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// The squared segment length. |
| | | 81 | | /// </summary> |
| | | 82 | | [JsonIgnore] |
| | | 83 | | [MemoryPackIgnore] |
| | | 84 | | public Fixed64 LengthSquared |
| | | 85 | | { |
| | | 86 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 87 | | get => Delta.MagnitudeSquared; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// The normalized axis-aligned box that contains this segment. |
| | | 92 | | /// </summary> |
| | | 93 | | [JsonIgnore] |
| | | 94 | | [MemoryPackIgnore] |
| | | 95 | | public FixedBoundBox Bounds |
| | | 96 | | { |
| | | 97 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 5 | 98 | | get => FixedBoundBox.FromMinMax(Start, End); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | #endregion |
| | | 102 | | |
| | | 103 | | #region Spatial Queries |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Finds the closest point on this finite segment to the supplied point. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <remarks> |
| | | 109 | | /// Zero-length segments deterministically return <see cref="Start"/>. |
| | | 110 | | /// </remarks> |
| | | 111 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 31 | 112 | | public Vector3d ClosestPoint(Vector3d point) => Vector3d.ClosestPointOnLineSegment(point, Start, End); |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Computes the squared distance from the supplied point to this finite segment. |
| | | 116 | | /// </summary> |
| | | 117 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 118 | | public Fixed64 DistanceSquared(Vector3d point) |
| | | 119 | | { |
| | 3 | 120 | | return Vector3d.DistanceSquared(point, ClosestPoint(point)); |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | #endregion |
| | | 124 | | |
| | | 125 | | #region Deconstruction |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Deconstructs the segment into start and end points. |
| | | 129 | | /// </summary> |
| | | 130 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 131 | | public void Deconstruct(out Vector3d start, out Vector3d end) |
| | | 132 | | { |
| | 1 | 133 | | start = Start; |
| | 1 | 134 | | end = End; |
| | 1 | 135 | | } |
| | | 136 | | |
| | | 137 | | #endregion |
| | | 138 | | |
| | | 139 | | #region Operators |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Determines whether two segments have the same ordered endpoints. |
| | | 143 | | /// </summary> |
| | | 144 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 145 | | public static bool operator ==(FixedSegment left, FixedSegment right) => left.Equals(right); |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Determines whether two segments have different ordered endpoints. |
| | | 149 | | /// </summary> |
| | | 150 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 151 | | public static bool operator !=(FixedSegment left, FixedSegment right) => !left.Equals(right); |
| | | 152 | | |
| | | 153 | | #endregion |
| | | 154 | | |
| | | 155 | | #region Equality |
| | | 156 | | |
| | | 157 | | /// <inheritdoc /> |
| | | 158 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 159 | | public bool Equals(FixedSegment other) |
| | | 160 | | { |
| | 10 | 161 | | return Start == other.Start && End == other.End; |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | /// <inheritdoc /> |
| | | 165 | | public override bool Equals(object? obj) |
| | | 166 | | { |
| | 1 | 167 | | return obj is FixedSegment other && Equals(other); |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | /// <inheritdoc /> |
| | | 171 | | public override int GetHashCode() |
| | | 172 | | { |
| | | 173 | | unchecked |
| | | 174 | | { |
| | 2 | 175 | | int hash = 17; |
| | 2 | 176 | | hash = (hash * 31) + Start.StateHash; |
| | 2 | 177 | | hash = (hash * 31) + End.StateHash; |
| | 2 | 178 | | return hash; |
| | | 179 | | } |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | #endregion |
| | | 183 | | } |