| | | 1 | | //======================================================================= |
| | | 2 | | // FixedTriangle2d.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 triangle in two-dimensional fixed-point space. |
| | | 17 | | /// </summary> |
| | | 18 | | [Serializable] |
| | | 19 | | [MemoryPackable] |
| | | 20 | | public partial struct FixedTriangle2d : IEquatable<FixedTriangle2d> |
| | | 21 | | { |
| | | 22 | | #region Constants |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// The number of vertices in a triangle. |
| | | 26 | | /// </summary> |
| | | 27 | | public const int VertexCount = 3; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// The number of edges in a triangle. |
| | | 31 | | /// </summary> |
| | | 32 | | public const int EdgeCount = 3; |
| | | 33 | | |
| | | 34 | | #endregion |
| | | 35 | | |
| | | 36 | | #region Fields |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// The first vertex. |
| | | 40 | | /// </summary> |
| | | 41 | | [JsonInclude] |
| | | 42 | | [MemoryPackOrder(0)] |
| | | 43 | | public Vector2d A; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// The second vertex. |
| | | 47 | | /// </summary> |
| | | 48 | | [JsonInclude] |
| | | 49 | | [MemoryPackOrder(1)] |
| | | 50 | | public Vector2d B; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// The third vertex. |
| | | 54 | | /// </summary> |
| | | 55 | | [JsonInclude] |
| | | 56 | | [MemoryPackOrder(2)] |
| | | 57 | | public Vector2d C; |
| | | 58 | | |
| | | 59 | | #endregion |
| | | 60 | | |
| | | 61 | | #region Constructors |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Initializes a triangle from ordered vertices. |
| | | 65 | | /// </summary> |
| | | 66 | | [JsonConstructor] |
| | | 67 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 68 | | public FixedTriangle2d(Vector2d a, Vector2d b, Vector2d c) |
| | | 69 | | { |
| | 23 | 70 | | A = a; |
| | 23 | 71 | | B = b; |
| | 23 | 72 | | C = c; |
| | 23 | 73 | | } |
| | | 74 | | |
| | | 75 | | #endregion |
| | | 76 | | |
| | | 77 | | #region Properties |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// The signed area of the triangle. Positive values indicate counter-clockwise winding. |
| | | 81 | | /// </summary> |
| | | 82 | | [JsonIgnore] |
| | | 83 | | [MemoryPackIgnore] |
| | | 84 | | public Fixed64 SignedArea |
| | | 85 | | { |
| | | 86 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 32 | 87 | | get => Vector2d.CrossProduct(B - A, C - A) * Fixed64.Half; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// The non-negative area of the triangle. |
| | | 92 | | /// </summary> |
| | | 93 | | [JsonIgnore] |
| | | 94 | | [MemoryPackIgnore] |
| | | 95 | | public Fixed64 Area |
| | | 96 | | { |
| | | 97 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 7 | 98 | | get => FixedMath.Abs(SignedArea); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// The normalized axis-aligned area that contains all vertices. |
| | | 103 | | /// </summary> |
| | | 104 | | [JsonIgnore] |
| | | 105 | | [MemoryPackIgnore] |
| | | 106 | | public FixedBoundArea Bounds |
| | | 107 | | { |
| | | 108 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 109 | | get => FixedBoundArea.FromMinMax(ComponentMin(ComponentMin(A, B), C), ComponentMax(ComponentMax(A, B), C)); |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// The arithmetic center of the three vertices. |
| | | 114 | | /// </summary> |
| | | 115 | | [JsonIgnore] |
| | | 116 | | [MemoryPackIgnore] |
| | | 117 | | public Vector2d Centroid |
| | | 118 | | { |
| | | 119 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 120 | | get => (A + B + C) / (Fixed64)3; |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Returns true when the triangle has no positive area. |
| | | 125 | | /// </summary> |
| | | 126 | | [JsonIgnore] |
| | | 127 | | [MemoryPackIgnore] |
| | | 128 | | public bool IsDegenerate |
| | | 129 | | { |
| | | 130 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 3 | 131 | | get => Area <= Fixed64.Epsilon; |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | #endregion |
| | | 135 | | |
| | | 136 | | #region Geometry Access |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Gets a vertex by stable index: 0 = A, 1 = B, 2 = C. |
| | | 140 | | /// </summary> |
| | | 141 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 142 | | public Vector2d GetVertex(int index) => |
| | 5 | 143 | | index switch |
| | 5 | 144 | | { |
| | 1 | 145 | | 0 => A, |
| | 1 | 146 | | 1 => B, |
| | 1 | 147 | | 2 => C, |
| | 2 | 148 | | _ => throw new ArgumentOutOfRangeException(nameof(index), $"Vertex index must be between 0 and {VertexCount |
| | 5 | 149 | | }; |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Gets an edge by stable index: 0 = AB, 1 = BC, 2 = CA. |
| | | 153 | | /// </summary> |
| | | 154 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 155 | | public FixedSegment2d GetEdge(int index) => |
| | 42 | 156 | | index switch |
| | 42 | 157 | | { |
| | 15 | 158 | | 0 => new FixedSegment2d(A, B), |
| | 13 | 159 | | 1 => new FixedSegment2d(B, C), |
| | 12 | 160 | | 2 => new FixedSegment2d(C, A), |
| | 2 | 161 | | _ => throw new ArgumentOutOfRangeException(nameof(index), $"Edge index must be between 0 and {EdgeCount - 1} |
| | 42 | 162 | | }; |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// Gets the point represented by barycentric weights for vertices B and C. |
| | | 166 | | /// </summary> |
| | | 167 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 168 | | public Vector2d GetPoint(Fixed64 weightB, Fixed64 weightC) |
| | | 169 | | { |
| | 6 | 170 | | return Vector2d.BarycentricCoordinates(A, B, C, weightB, weightC); |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | #endregion |
| | | 174 | | |
| | | 175 | | #region Spatial Queries |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Computes barycentric weights for a point relative to this triangle. |
| | | 179 | | /// </summary> |
| | | 180 | | /// <returns> |
| | | 181 | | /// True when the triangle has non-degenerate area; false when weights cannot be solved. |
| | | 182 | | /// </returns> |
| | | 183 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 184 | | public bool TryGetBarycentricWeights(Vector2d point, out Fixed64 weightA, out Fixed64 weightB, out Fixed64 weightC) |
| | | 185 | | { |
| | 3 | 186 | | Vector2d ab = B - A; |
| | 3 | 187 | | Vector2d ac = C - A; |
| | 3 | 188 | | Fixed64 denominator = Vector2d.CrossProduct(ab, ac); |
| | 3 | 189 | | if (FixedMath.Abs(denominator) <= Fixed64.Epsilon) |
| | | 190 | | { |
| | 1 | 191 | | weightA = Fixed64.Zero; |
| | 1 | 192 | | weightB = Fixed64.Zero; |
| | 1 | 193 | | weightC = Fixed64.Zero; |
| | 1 | 194 | | return false; |
| | | 195 | | } |
| | | 196 | | |
| | 2 | 197 | | Vector2d ap = point - A; |
| | 2 | 198 | | weightB = Vector2d.CrossProduct(ap, ac) / denominator; |
| | 2 | 199 | | weightC = Vector2d.CrossProduct(ab, ap) / denominator; |
| | 2 | 200 | | weightA = Fixed64.One - weightB - weightC; |
| | 2 | 201 | | return true; |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | /// <summary> |
| | | 205 | | /// Determines whether the point is inside the triangle, including edges and vertices. |
| | | 206 | | /// </summary> |
| | | 207 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 208 | | public bool Contains(Vector2d point) |
| | | 209 | | { |
| | 22 | 210 | | Fixed64 signedArea = SignedArea; |
| | 22 | 211 | | if (FixedMath.Abs(signedArea) <= Fixed64.Epsilon) |
| | 7 | 212 | | return IsPointOnAnyEdge(point); |
| | | 213 | | |
| | 15 | 214 | | Fixed64 ab = Vector2d.CrossProduct(B - A, point - A); |
| | 15 | 215 | | Fixed64 bc = Vector2d.CrossProduct(C - B, point - B); |
| | 15 | 216 | | Fixed64 ca = Vector2d.CrossProduct(A - C, point - C); |
| | | 217 | | |
| | 15 | 218 | | return signedArea > Fixed64.Zero |
| | 15 | 219 | | ? ab >= -Fixed64.Epsilon && bc >= -Fixed64.Epsilon && ca >= -Fixed64.Epsilon |
| | 15 | 220 | | : ab <= Fixed64.Epsilon && bc <= Fixed64.Epsilon && ca <= Fixed64.Epsilon; |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | /// <summary> |
| | | 224 | | /// Finds the closest point on or inside this triangle to the supplied point. |
| | | 225 | | /// </summary> |
| | | 226 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 227 | | public Vector2d ClosestPoint(Vector2d point) |
| | | 228 | | { |
| | 9 | 229 | | return Contains(point) ? point : ClosestPointOnEdges(point); |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | /// <summary> |
| | | 233 | | /// Computes the squared distance from the supplied point to this triangle. |
| | | 234 | | /// </summary> |
| | | 235 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 236 | | public Fixed64 DistanceSquared(Vector2d point) |
| | | 237 | | { |
| | 3 | 238 | | return Vector2d.DistanceSquared(point, ClosestPoint(point)); |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | #endregion |
| | | 242 | | |
| | | 243 | | #region Deconstruction |
| | | 244 | | |
| | | 245 | | /// <summary> |
| | | 246 | | /// Deconstructs the triangle into ordered vertices. |
| | | 247 | | /// </summary> |
| | | 248 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 249 | | public void Deconstruct(out Vector2d a, out Vector2d b, out Vector2d c) |
| | | 250 | | { |
| | 1 | 251 | | a = A; |
| | 1 | 252 | | b = B; |
| | 1 | 253 | | c = C; |
| | 1 | 254 | | } |
| | | 255 | | |
| | | 256 | | #endregion |
| | | 257 | | |
| | | 258 | | #region Operators |
| | | 259 | | |
| | | 260 | | /// <summary> |
| | | 261 | | /// Determines whether two triangles have the same ordered vertices. |
| | | 262 | | /// </summary> |
| | | 263 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 264 | | public static bool operator ==(FixedTriangle2d left, FixedTriangle2d right) => left.Equals(right); |
| | | 265 | | |
| | | 266 | | /// <summary> |
| | | 267 | | /// Determines whether two triangles have different ordered vertices. |
| | | 268 | | /// </summary> |
| | | 269 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 270 | | public static bool operator !=(FixedTriangle2d left, FixedTriangle2d right) => !left.Equals(right); |
| | | 271 | | |
| | | 272 | | #endregion |
| | | 273 | | |
| | | 274 | | #region Equality |
| | | 275 | | |
| | | 276 | | /// <inheritdoc /> |
| | | 277 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 278 | | public bool Equals(FixedTriangle2d other) |
| | | 279 | | { |
| | 7 | 280 | | return A == other.A && B == other.B && C == other.C; |
| | | 281 | | } |
| | | 282 | | |
| | | 283 | | /// <inheritdoc /> |
| | | 284 | | public override bool Equals(object? obj) |
| | | 285 | | { |
| | 1 | 286 | | return obj is FixedTriangle2d other && Equals(other); |
| | | 287 | | } |
| | | 288 | | |
| | | 289 | | /// <inheritdoc /> |
| | | 290 | | public override int GetHashCode() |
| | | 291 | | { |
| | | 292 | | unchecked |
| | | 293 | | { |
| | 2 | 294 | | int hash = 17; |
| | 2 | 295 | | hash = (hash * 31) + A.StateHash; |
| | 2 | 296 | | hash = (hash * 31) + B.StateHash; |
| | 2 | 297 | | hash = (hash * 31) + C.StateHash; |
| | 2 | 298 | | return hash; |
| | | 299 | | } |
| | | 300 | | } |
| | | 301 | | |
| | | 302 | | #endregion |
| | | 303 | | |
| | | 304 | | #region Helpers |
| | | 305 | | |
| | | 306 | | private bool IsPointOnAnyEdge(Vector2d point) |
| | | 307 | | { |
| | 7 | 308 | | return GetEdge(0).DistanceSquared(point) <= Fixed64.Epsilon |
| | 7 | 309 | | || GetEdge(1).DistanceSquared(point) <= Fixed64.Epsilon |
| | 7 | 310 | | || GetEdge(2).DistanceSquared(point) <= Fixed64.Epsilon; |
| | | 311 | | } |
| | | 312 | | |
| | | 313 | | private Vector2d ClosestPointOnEdges(Vector2d point) |
| | | 314 | | { |
| | 7 | 315 | | Vector2d best = GetEdge(0).ClosestPoint(point); |
| | 7 | 316 | | Fixed64 bestDistance = Vector2d.DistanceSquared(point, best); |
| | 7 | 317 | | TrySetCloserPoint(GetEdge(1), point, ref best, ref bestDistance); |
| | 7 | 318 | | TrySetCloserPoint(GetEdge(2), point, ref best, ref bestDistance); |
| | 7 | 319 | | return best; |
| | | 320 | | } |
| | | 321 | | |
| | | 322 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 323 | | private static void TrySetCloserPoint(FixedSegment2d edge, Vector2d point, ref Vector2d best, ref Fixed64 bestDistan |
| | | 324 | | { |
| | 14 | 325 | | Vector2d candidate = edge.ClosestPoint(point); |
| | 14 | 326 | | Fixed64 distance = Vector2d.DistanceSquared(point, candidate); |
| | 14 | 327 | | if (distance >= bestDistance) |
| | 11 | 328 | | return; |
| | | 329 | | |
| | 3 | 330 | | bestDistance = distance; |
| | 3 | 331 | | best = candidate; |
| | 3 | 332 | | } |
| | | 333 | | |
| | | 334 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 335 | | private static Vector2d ComponentMin(Vector2d a, Vector2d b) |
| | | 336 | | { |
| | 4 | 337 | | return new Vector2d(FixedMath.Min(a.X, b.X), FixedMath.Min(a.Y, b.Y)); |
| | | 338 | | } |
| | | 339 | | |
| | | 340 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 341 | | private static Vector2d ComponentMax(Vector2d a, Vector2d b) |
| | | 342 | | { |
| | 4 | 343 | | return new Vector2d(FixedMath.Max(a.X, b.X), FixedMath.Max(a.Y, b.Y)); |
| | | 344 | | } |
| | | 345 | | |
| | | 346 | | #endregion |
| | | 347 | | } |