| | | 1 | | //======================================================================= |
| | | 2 | | // FixedBoundFrustum.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 System; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | |
| | | 11 | | namespace FixedMathSharp.Bounds; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Represents a frustum bounded by six clipping planes. |
| | | 15 | | /// </summary> |
| | | 16 | | public struct FixedBoundFrustum : IEquatable<FixedBoundFrustum> |
| | | 17 | | { |
| | | 18 | | #region Constants |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// The number of planes in a frustum. |
| | | 22 | | /// </summary> |
| | | 23 | | public const int PlaneCount = 6; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// The number of corner points in a frustum. |
| | | 27 | | /// </summary> |
| | | 28 | | public const int CornerCount = 8; |
| | | 29 | | |
| | | 30 | | #endregion |
| | | 31 | | |
| | | 32 | | #region Fields |
| | | 33 | | |
| | | 34 | | private const int EdgeCount = 12; |
| | 1 | 35 | | private static readonly int[] s_edgeStarts = { 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3 }; |
| | 1 | 36 | | private static readonly int[] s_edgeEnds = { 1, 2, 3, 0, 5, 6, 7, 4, 4, 5, 6, 7 }; |
| | | 37 | | |
| | | 38 | | private bool _hasMatrix; |
| | | 39 | | private Fixed4x4 _matrix; |
| | | 40 | | private FixedPlane _near; |
| | | 41 | | private FixedPlane _far; |
| | | 42 | | private FixedPlane _left; |
| | | 43 | | private FixedPlane _right; |
| | | 44 | | private FixedPlane _top; |
| | | 45 | | private FixedPlane _bottom; |
| | | 46 | | private Vector3d _nearTopLeft; |
| | | 47 | | private Vector3d _nearTopRight; |
| | | 48 | | private Vector3d _nearBottomRight; |
| | | 49 | | private Vector3d _nearBottomLeft; |
| | | 50 | | private Vector3d _farTopLeft; |
| | | 51 | | private Vector3d _farTopRight; |
| | | 52 | | private Vector3d _farBottomRight; |
| | | 53 | | private Vector3d _farBottomLeft; |
| | | 54 | | |
| | | 55 | | #endregion |
| | | 56 | | |
| | | 57 | | #region Constructors |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Initializes a new frustum by extracting the planes and corners from a combined view-projection matrix. |
| | | 61 | | /// </summary> |
| | | 62 | | public FixedBoundFrustum(Fixed4x4 matrix) |
| | | 63 | | { |
| | 47 | 64 | | this = default; |
| | 47 | 65 | | SetMatrix(matrix); |
| | 47 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Initializes a new frustum from six clipping planes. |
| | | 70 | | /// </summary> |
| | | 71 | | public FixedBoundFrustum( |
| | | 72 | | FixedPlane near, |
| | | 73 | | FixedPlane far, |
| | | 74 | | FixedPlane left, |
| | | 75 | | FixedPlane right, |
| | | 76 | | FixedPlane top, |
| | | 77 | | FixedPlane bottom) |
| | | 78 | | { |
| | 8 | 79 | | this = default; |
| | 8 | 80 | | SetPlanes(near, far, left, right, top, bottom); |
| | 7 | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Initializes a new frustum from six clipping planes in near, far, left, right, top, bottom order. |
| | | 85 | | /// </summary> |
| | | 86 | | public FixedBoundFrustum(FixedPlane[] planes) |
| | | 87 | | { |
| | 3 | 88 | | if (planes is null) |
| | 1 | 89 | | throw new ArgumentNullException(nameof(planes), "Cannot create a frustum from a null plane array."); |
| | | 90 | | |
| | 2 | 91 | | if (planes.Length != PlaneCount) |
| | 1 | 92 | | throw new ArgumentException($"A frustum must be defined by exactly {PlaneCount} planes."); |
| | | 93 | | |
| | 1 | 94 | | this = default; |
| | 1 | 95 | | SetPlanes(planes); |
| | 1 | 96 | | } |
| | | 97 | | |
| | | 98 | | #endregion |
| | | 99 | | |
| | | 100 | | #region Properties |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Gets a value indicating whether this frustum was created from a matrix source. |
| | | 104 | | /// </summary> |
| | | 105 | | public bool HasMatrix |
| | | 106 | | { |
| | | 107 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 108 | | get => _hasMatrix; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Gets or sets the matrix source used to define this frustum. |
| | | 113 | | /// </summary> |
| | | 114 | | /// <exception cref="InvalidOperationException"> |
| | | 115 | | /// Thrown when reading the matrix from a frustum that was created from planes. |
| | | 116 | | /// </exception> |
| | | 117 | | public Fixed4x4 Matrix |
| | | 118 | | { |
| | | 119 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 120 | | get => _hasMatrix ? _matrix : throw new InvalidOperationException("This frustum was not created from a matrix.") |
| | 1 | 121 | | set => SetMatrix(value); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Gets the near clipping plane. |
| | | 126 | | /// </summary> |
| | | 127 | | public FixedPlane Near |
| | | 128 | | { |
| | | 129 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 130 | | get => _near; |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Gets the far clipping plane. |
| | | 135 | | /// </summary> |
| | | 136 | | public FixedPlane Far |
| | | 137 | | { |
| | | 138 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 139 | | get => _far; |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Gets the left clipping plane. |
| | | 144 | | /// </summary> |
| | | 145 | | public FixedPlane Left |
| | | 146 | | { |
| | | 147 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 148 | | get => _left; |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Gets the right clipping plane. |
| | | 153 | | /// </summary> |
| | | 154 | | public FixedPlane Right |
| | | 155 | | { |
| | | 156 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 157 | | get => _right; |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Gets the top clipping plane. |
| | | 162 | | /// </summary> |
| | | 163 | | public FixedPlane Top |
| | | 164 | | { |
| | | 165 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 166 | | get => _top; |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Gets the bottom clipping plane. |
| | | 171 | | /// </summary> |
| | | 172 | | public FixedPlane Bottom |
| | | 173 | | { |
| | | 174 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 175 | | get => _bottom; |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// Gets the minimum corner of the axis-aligned box that encloses the frustum. |
| | | 180 | | /// </summary> |
| | | 181 | | public Vector3d Min { get; private set; } |
| | | 182 | | |
| | | 183 | | /// <summary> |
| | | 184 | | /// Gets the maximum corner of the axis-aligned box that encloses the frustum. |
| | | 185 | | /// </summary> |
| | | 186 | | public Vector3d Max { get; private set; } |
| | | 187 | | |
| | | 188 | | #endregion |
| | | 189 | | |
| | | 190 | | #region Methods |
| | | 191 | | |
| | | 192 | | /// <summary> |
| | | 193 | | /// Tests a point against this frustum. |
| | | 194 | | /// </summary> |
| | | 195 | | public FixedEnclosureType Contains(Vector3d point) |
| | | 196 | | { |
| | 44 | 197 | | return _near.DotCoordinate(point) > Fixed64.Zero |
| | 44 | 198 | | || _far.DotCoordinate(point) > Fixed64.Zero |
| | 44 | 199 | | || _left.DotCoordinate(point) > Fixed64.Zero |
| | 44 | 200 | | || _right.DotCoordinate(point) > Fixed64.Zero |
| | 44 | 201 | | || _top.DotCoordinate(point) > Fixed64.Zero |
| | 44 | 202 | | || _bottom.DotCoordinate(point) > Fixed64.Zero |
| | 44 | 203 | | ? FixedEnclosureType.Disjoint |
| | 44 | 204 | | : FixedEnclosureType.Contains; |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | /// <summary> |
| | | 208 | | /// Tests a bounding box against this frustum. |
| | | 209 | | /// </summary> |
| | | 210 | | public FixedEnclosureType Contains(FixedBoundBox box) |
| | | 211 | | { |
| | 11 | 212 | | bool intersects = false; |
| | | 213 | | |
| | 11 | 214 | | if (DisjointOrIntersects(_near, box, ref intersects) |
| | 11 | 215 | | || DisjointOrIntersects(_far, box, ref intersects) |
| | 11 | 216 | | || DisjointOrIntersects(_left, box, ref intersects) |
| | 11 | 217 | | || DisjointOrIntersects(_right, box, ref intersects) |
| | 11 | 218 | | || DisjointOrIntersects(_top, box, ref intersects) |
| | 11 | 219 | | || DisjointOrIntersects(_bottom, box, ref intersects)) |
| | 3 | 220 | | return FixedEnclosureType.Disjoint; |
| | | 221 | | |
| | 8 | 222 | | return intersects ? FixedEnclosureType.Intersects : FixedEnclosureType.Contains; |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | /// <summary> |
| | | 226 | | /// Tests a bounding sphere against this frustum. |
| | | 227 | | /// </summary> |
| | | 228 | | public FixedEnclosureType Contains(FixedBoundSphere sphere) |
| | | 229 | | { |
| | 8 | 230 | | bool intersects = false; |
| | | 231 | | |
| | 8 | 232 | | if (DisjointOrIntersects(_near, sphere, ref intersects) |
| | 8 | 233 | | || DisjointOrIntersects(_far, sphere, ref intersects) |
| | 8 | 234 | | || DisjointOrIntersects(_left, sphere, ref intersects) |
| | 8 | 235 | | || DisjointOrIntersects(_right, sphere, ref intersects) |
| | 8 | 236 | | || DisjointOrIntersects(_top, sphere, ref intersects) |
| | 8 | 237 | | || DisjointOrIntersects(_bottom, sphere, ref intersects)) |
| | 2 | 238 | | return FixedEnclosureType.Disjoint; |
| | | 239 | | |
| | 6 | 240 | | return intersects ? FixedEnclosureType.Intersects : FixedEnclosureType.Contains; |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | /// <summary> |
| | | 244 | | /// Tests another frustum against this frustum. |
| | | 245 | | /// </summary> |
| | | 246 | | public FixedEnclosureType Contains(FixedBoundFrustum frustum) |
| | | 247 | | { |
| | 13 | 248 | | if (Equals(frustum)) |
| | 1 | 249 | | return FixedEnclosureType.Contains; |
| | | 250 | | |
| | 12 | 251 | | bool containsAllCorners = true; |
| | 56 | 252 | | for (int i = 0; i < CornerCount; i++) |
| | | 253 | | { |
| | 26 | 254 | | if (Contains(frustum.GetCorner(i)) == FixedEnclosureType.Disjoint) |
| | | 255 | | { |
| | 10 | 256 | | containsAllCorners = false; |
| | 10 | 257 | | break; |
| | | 258 | | } |
| | | 259 | | } |
| | | 260 | | |
| | 12 | 261 | | if (containsAllCorners) |
| | 2 | 262 | | return FixedEnclosureType.Contains; |
| | | 263 | | |
| | 10 | 264 | | return IntersectsFrustum(frustum) |
| | 10 | 265 | | ? FixedEnclosureType.Intersects |
| | 10 | 266 | | : FixedEnclosureType.Disjoint; |
| | | 267 | | } |
| | | 268 | | |
| | | 269 | | /// <summary> |
| | | 270 | | /// Checks whether a bounding box intersects this frustum. |
| | | 271 | | /// </summary> |
| | | 272 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 7 | 273 | | public bool Intersects(FixedBoundBox box) => Contains(box) != FixedEnclosureType.Disjoint; |
| | | 274 | | |
| | | 275 | | /// <summary> |
| | | 276 | | /// Checks whether a bounding sphere intersects this frustum. |
| | | 277 | | /// </summary> |
| | | 278 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 6 | 279 | | public bool Intersects(FixedBoundSphere sphere) => Contains(sphere) != FixedEnclosureType.Disjoint; |
| | | 280 | | |
| | | 281 | | /// <summary> |
| | | 282 | | /// Checks whether another frustum intersects this frustum. |
| | | 283 | | /// </summary> |
| | | 284 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 6 | 285 | | public bool Intersects(FixedBoundFrustum frustum) => Contains(frustum) != FixedEnclosureType.Disjoint; |
| | | 286 | | |
| | | 287 | | /// <summary> |
| | | 288 | | /// Finds the first forward intersection between the specified ray and this frustum. |
| | | 289 | | /// </summary> |
| | | 290 | | public Fixed64? Intersects(FixedRay ray) |
| | | 291 | | { |
| | 7 | 292 | | Fixed64 tEnter = Fixed64.Zero; |
| | 7 | 293 | | Fixed64 tExit = Fixed64.MaxValue; |
| | | 294 | | |
| | 7 | 295 | | if (!ClipRay(_near, ray, ref tEnter, ref tExit) |
| | 7 | 296 | | || !ClipRay(_far, ray, ref tEnter, ref tExit) |
| | 7 | 297 | | || !ClipRay(_left, ray, ref tEnter, ref tExit) |
| | 7 | 298 | | || !ClipRay(_right, ray, ref tEnter, ref tExit) |
| | 7 | 299 | | || !ClipRay(_top, ray, ref tEnter, ref tExit) |
| | 7 | 300 | | || !ClipRay(_bottom, ray, ref tEnter, ref tExit)) |
| | 3 | 301 | | return null; |
| | | 302 | | |
| | 4 | 303 | | return tEnter; |
| | | 304 | | } |
| | | 305 | | |
| | | 306 | | /// <summary> |
| | | 307 | | /// Classifies this frustum relative to a plane. |
| | | 308 | | /// </summary> |
| | | 309 | | public FixedPlaneIntersectionType Intersects(FixedPlane plane) |
| | | 310 | | { |
| | 4 | 311 | | FixedPlaneIntersectionType result = plane.Intersects(_nearTopLeft); |
| | | 312 | | |
| | 36 | 313 | | for (int i = 1; i < CornerCount; i++) |
| | | 314 | | { |
| | 16 | 315 | | if (plane.Intersects(GetCorner(i)) != result) |
| | 2 | 316 | | return FixedPlaneIntersectionType.Intersecting; |
| | | 317 | | } |
| | | 318 | | |
| | 2 | 319 | | return result; |
| | | 320 | | } |
| | | 321 | | |
| | | 322 | | /// <summary> |
| | | 323 | | /// Clamps a point to this frustum, returning the point unchanged when it is already inside. |
| | | 324 | | /// </summary> |
| | | 325 | | public Vector3d ClampPoint(Vector3d point) |
| | | 326 | | { |
| | 5 | 327 | | if (Contains(point) != FixedEnclosureType.Disjoint) |
| | 1 | 328 | | return point; |
| | | 329 | | |
| | 4 | 330 | | Vector3d best = _nearTopLeft; |
| | 4 | 331 | | Fixed64 bestDistance = Vector3d.DistanceSquared(point, best); |
| | | 332 | | |
| | 64 | 333 | | for (int i = 1; i < CornerCount; i++) |
| | 28 | 334 | | UpdateNearestCandidate(point, GetCorner(i), ref best, ref bestDistance); |
| | | 335 | | |
| | 56 | 336 | | for (int i = 0; i < PlaneCount; i++) |
| | | 337 | | { |
| | 24 | 338 | | Vector3d candidate = Vector3d.ProjectOnPlane(point, GetPlane(i)); |
| | 24 | 339 | | if (IsInside(candidate)) |
| | 4 | 340 | | UpdateNearestCandidate(point, candidate, ref best, ref bestDistance); |
| | | 341 | | } |
| | | 342 | | |
| | 104 | 343 | | for (int i = 0; i < EdgeCount; i++) |
| | | 344 | | { |
| | 48 | 345 | | Vector3d candidate = Vector3d.ClosestPointOnLineSegment(point, GetCorner(GetEdgeStart(i)), GetCorner(GetEdge |
| | 48 | 346 | | UpdateNearestCandidate(point, candidate, ref best, ref bestDistance); |
| | | 347 | | } |
| | | 348 | | |
| | 4 | 349 | | return best; |
| | | 350 | | } |
| | | 351 | | |
| | | 352 | | /// <summary> |
| | | 353 | | /// Returns a copy of the frustum corner array. |
| | | 354 | | /// </summary> |
| | | 355 | | public Vector3d[] GetCorners() |
| | | 356 | | { |
| | 14 | 357 | | var corners = new Vector3d[CornerCount]; |
| | 14 | 358 | | GetCorners(corners); |
| | 14 | 359 | | return corners; |
| | | 360 | | } |
| | | 361 | | |
| | | 362 | | /// <summary> |
| | | 363 | | /// Copies this frustum's corners into the specified array. |
| | | 364 | | /// </summary> |
| | | 365 | | public void GetCorners(Vector3d[] corners) |
| | | 366 | | { |
| | 19 | 367 | | if (corners is null) |
| | 1 | 368 | | throw new ArgumentNullException(nameof(corners), "Cannot copy corners to a null array."); |
| | | 369 | | |
| | 18 | 370 | | if (corners.Length < CornerCount) |
| | 1 | 371 | | throw new ArgumentOutOfRangeException(nameof(corners), $"The destination array must have at least {CornerCou |
| | | 372 | | |
| | 17 | 373 | | corners[0] = _nearTopLeft; |
| | 17 | 374 | | corners[1] = _nearTopRight; |
| | 17 | 375 | | corners[2] = _nearBottomRight; |
| | 17 | 376 | | corners[3] = _nearBottomLeft; |
| | 17 | 377 | | corners[4] = _farTopLeft; |
| | 17 | 378 | | corners[5] = _farTopRight; |
| | 17 | 379 | | corners[6] = _farBottomRight; |
| | 17 | 380 | | corners[7] = _farBottomLeft; |
| | 17 | 381 | | } |
| | | 382 | | |
| | | 383 | | /// <summary> |
| | | 384 | | /// Returns a copy of the frustum plane array in near, far, left, right, top, bottom order. |
| | | 385 | | /// </summary> |
| | | 386 | | public FixedPlane[] GetPlanes() |
| | | 387 | | { |
| | 2 | 388 | | var planes = new FixedPlane[PlaneCount]; |
| | 2 | 389 | | GetPlanes(planes); |
| | 2 | 390 | | return planes; |
| | | 391 | | } |
| | | 392 | | |
| | | 393 | | /// <summary> |
| | | 394 | | /// Copies this frustum's planes into the specified array in near, far, left, right, top, bottom order. |
| | | 395 | | /// </summary> |
| | | 396 | | public void GetPlanes(FixedPlane[] planes) |
| | | 397 | | { |
| | 7 | 398 | | if (planes is null) |
| | 1 | 399 | | throw new ArgumentNullException(nameof(planes), "Cannot copy planes to a null array."); |
| | | 400 | | |
| | 6 | 401 | | if (planes.Length < PlaneCount) |
| | 1 | 402 | | throw new ArgumentOutOfRangeException(nameof(planes), $"The destination array must have at least {PlaneCount |
| | | 403 | | |
| | 5 | 404 | | planes[0] = _near; |
| | 5 | 405 | | planes[1] = _far; |
| | 5 | 406 | | planes[2] = _left; |
| | 5 | 407 | | planes[3] = _right; |
| | 5 | 408 | | planes[4] = _top; |
| | 5 | 409 | | planes[5] = _bottom; |
| | 5 | 410 | | } |
| | | 411 | | |
| | | 412 | | /// <summary> |
| | | 413 | | /// Gets the corner at the specified stable index without allocating. |
| | | 414 | | /// </summary> |
| | | 415 | | public Vector3d GetCorner(int index) |
| | | 416 | | { |
| | | 417 | | switch (index) |
| | | 418 | | { |
| | 123 | 419 | | case 0: return _nearTopLeft; |
| | 707 | 420 | | case 1: return _nearTopRight; |
| | 701 | 421 | | case 2: return _nearBottomRight; |
| | 701 | 422 | | case 3: return _nearBottomLeft; |
| | 701 | 423 | | case 4: return _farTopLeft; |
| | 701 | 424 | | case 5: return _farTopRight; |
| | 701 | 425 | | case 6: return _farBottomRight; |
| | 701 | 426 | | case 7: return _farBottomLeft; |
| | | 427 | | default: |
| | 2 | 428 | | throw new ArgumentOutOfRangeException(nameof(index), $"Corner index must be between 0 and {CornerCount - |
| | | 429 | | } |
| | | 430 | | } |
| | | 431 | | |
| | | 432 | | /// <summary> |
| | | 433 | | /// Gets the plane at the specified stable index in near, far, left, right, top, bottom order without allocating. |
| | | 434 | | /// </summary> |
| | | 435 | | public FixedPlane GetPlane(int index) |
| | | 436 | | { |
| | | 437 | | switch (index) |
| | | 438 | | { |
| | 48 | 439 | | case 0: return _near; |
| | 43 | 440 | | case 1: return _far; |
| | 35 | 441 | | case 2: return _left; |
| | 31 | 442 | | case 3: return _right; |
| | 18 | 443 | | case 4: return _top; |
| | 16 | 444 | | case 5: return _bottom; |
| | | 445 | | default: |
| | 2 | 446 | | throw new ArgumentOutOfRangeException(nameof(index), $"Plane index must be between 0 and {PlaneCount - 1 |
| | | 447 | | } |
| | | 448 | | } |
| | | 449 | | |
| | | 450 | | private void SetMatrix(Fixed4x4 matrix) |
| | | 451 | | { |
| | 48 | 452 | | _matrix = matrix; |
| | 48 | 453 | | _hasMatrix = true; |
| | 48 | 454 | | CreatePlanes(matrix); |
| | 48 | 455 | | CreateCorners(); |
| | 48 | 456 | | UpdateBounds(); |
| | 48 | 457 | | } |
| | | 458 | | |
| | | 459 | | private void SetPlanes(FixedPlane[] planes) |
| | | 460 | | { |
| | 1 | 461 | | _near = FixedPlane.GetNormalized(planes[0]); |
| | 1 | 462 | | _far = FixedPlane.GetNormalized(planes[1]); |
| | 1 | 463 | | _left = FixedPlane.GetNormalized(planes[2]); |
| | 1 | 464 | | _right = FixedPlane.GetNormalized(planes[3]); |
| | 1 | 465 | | _top = FixedPlane.GetNormalized(planes[4]); |
| | 1 | 466 | | _bottom = FixedPlane.GetNormalized(planes[5]); |
| | | 467 | | |
| | 1 | 468 | | _hasMatrix = false; |
| | 1 | 469 | | _matrix = default; |
| | 1 | 470 | | CreateCorners(); |
| | 1 | 471 | | UpdateBounds(); |
| | 1 | 472 | | } |
| | | 473 | | |
| | | 474 | | private void SetPlanes( |
| | | 475 | | FixedPlane near, |
| | | 476 | | FixedPlane far, |
| | | 477 | | FixedPlane left, |
| | | 478 | | FixedPlane right, |
| | | 479 | | FixedPlane top, |
| | | 480 | | FixedPlane bottom) |
| | | 481 | | { |
| | 8 | 482 | | _near = FixedPlane.GetNormalized(near); |
| | 8 | 483 | | _far = FixedPlane.GetNormalized(far); |
| | 8 | 484 | | _left = FixedPlane.GetNormalized(left); |
| | 8 | 485 | | _right = FixedPlane.GetNormalized(right); |
| | 8 | 486 | | _top = FixedPlane.GetNormalized(top); |
| | 8 | 487 | | _bottom = FixedPlane.GetNormalized(bottom); |
| | | 488 | | |
| | 8 | 489 | | _hasMatrix = false; |
| | 8 | 490 | | _matrix = default; |
| | 8 | 491 | | CreateCorners(); |
| | 7 | 492 | | UpdateBounds(); |
| | 7 | 493 | | } |
| | | 494 | | |
| | | 495 | | private void CreatePlanes(Fixed4x4 matrix) |
| | | 496 | | { |
| | 48 | 497 | | _near = FixedPlane.GetNormalized(new FixedPlane(-matrix.M13, -matrix.M23, -matrix.M33, -matrix.M43)); |
| | 48 | 498 | | _far = FixedPlane.GetNormalized(new FixedPlane(matrix.M13 - matrix.M14, matrix.M23 - matrix.M24, matrix.M33 - ma |
| | 48 | 499 | | _left = FixedPlane.GetNormalized(new FixedPlane(-matrix.M14 - matrix.M11, -matrix.M24 - matrix.M21, -matrix.M34 |
| | 48 | 500 | | _right = FixedPlane.GetNormalized(new FixedPlane(matrix.M11 - matrix.M14, matrix.M21 - matrix.M24, matrix.M31 - |
| | 48 | 501 | | _top = FixedPlane.GetNormalized(new FixedPlane(matrix.M12 - matrix.M14, matrix.M22 - matrix.M24, matrix.M32 - ma |
| | 48 | 502 | | _bottom = FixedPlane.GetNormalized(new FixedPlane(-matrix.M14 - matrix.M12, -matrix.M24 - matrix.M22, -matrix.M3 |
| | 48 | 503 | | } |
| | | 504 | | |
| | | 505 | | private void CreateCorners() |
| | | 506 | | { |
| | 57 | 507 | | _nearTopLeft = IntersectionPoint(_near, _left, _top); |
| | 56 | 508 | | _nearTopRight = IntersectionPoint(_near, _right, _top); |
| | 56 | 509 | | _nearBottomRight = IntersectionPoint(_near, _right, _bottom); |
| | 56 | 510 | | _nearBottomLeft = IntersectionPoint(_near, _left, _bottom); |
| | 56 | 511 | | _farTopLeft = IntersectionPoint(_far, _left, _top); |
| | 56 | 512 | | _farTopRight = IntersectionPoint(_far, _right, _top); |
| | 56 | 513 | | _farBottomRight = IntersectionPoint(_far, _right, _bottom); |
| | 56 | 514 | | _farBottomLeft = IntersectionPoint(_far, _left, _bottom); |
| | 56 | 515 | | } |
| | | 516 | | |
| | | 517 | | private void UpdateBounds() |
| | | 518 | | { |
| | 56 | 519 | | Vector3d min = _nearTopLeft; |
| | 56 | 520 | | Vector3d max = _nearTopLeft; |
| | | 521 | | |
| | 896 | 522 | | for (int i = 1; i < CornerCount; i++) |
| | | 523 | | { |
| | 392 | 524 | | Vector3d corner = GetCorner(i); |
| | 392 | 525 | | min = Vector3d.Min(min, corner); |
| | 392 | 526 | | max = Vector3d.Max(max, corner); |
| | | 527 | | } |
| | | 528 | | |
| | 56 | 529 | | Min = min; |
| | 56 | 530 | | Max = max; |
| | 56 | 531 | | } |
| | | 532 | | |
| | | 533 | | private static Vector3d IntersectionPoint(FixedPlane a, FixedPlane b, FixedPlane c) |
| | | 534 | | { |
| | 449 | 535 | | Vector3d cross = Vector3d.Cross(b.Normal, c.Normal); |
| | 449 | 536 | | Fixed64 denominator = Vector3d.Dot(a.Normal, cross); |
| | | 537 | | |
| | 449 | 538 | | if (denominator == Fixed64.Zero) |
| | 1 | 539 | | throw new InvalidOperationException("Frustum planes do not intersect at a unique point."); |
| | | 540 | | |
| | 448 | 541 | | Vector3d v1 = cross * a.D; |
| | 448 | 542 | | Vector3d v2 = Vector3d.Cross(c.Normal, a.Normal) * b.D; |
| | 448 | 543 | | Vector3d v3 = Vector3d.Cross(a.Normal, b.Normal) * c.D; |
| | | 544 | | |
| | 448 | 545 | | return -(v1 + v2 + v3) / denominator; |
| | | 546 | | } |
| | | 547 | | |
| | | 548 | | private bool IsInside(Vector3d point) |
| | | 549 | | { |
| | 198 | 550 | | for (int i = 0; i < PlaneCount; i++) |
| | | 551 | | { |
| | 95 | 552 | | if (GetPlane(i).DotCoordinate(point) > Fixed64.Zero) |
| | 20 | 553 | | return false; |
| | | 554 | | } |
| | | 555 | | |
| | 4 | 556 | | return true; |
| | | 557 | | } |
| | | 558 | | |
| | | 559 | | private static void UpdateNearestCandidate( |
| | | 560 | | Vector3d point, |
| | | 561 | | Vector3d candidate, |
| | | 562 | | ref Vector3d best, |
| | | 563 | | ref Fixed64 bestDistance) |
| | | 564 | | { |
| | 80 | 565 | | Fixed64 candidateDistance = Vector3d.DistanceSquared(point, candidate); |
| | 80 | 566 | | if (candidateDistance >= bestDistance) |
| | 71 | 567 | | return; |
| | | 568 | | |
| | 9 | 569 | | best = candidate; |
| | 9 | 570 | | bestDistance = candidateDistance; |
| | 9 | 571 | | } |
| | | 572 | | |
| | | 573 | | private bool IntersectsFrustum(in FixedBoundFrustum other) |
| | | 574 | | { |
| | 84 | 575 | | for (int i = 0; i < PlaneCount; i++) |
| | | 576 | | { |
| | 38 | 577 | | if (Separates(GetPlane(i).Normal, this, other)) |
| | 4 | 578 | | return false; |
| | | 579 | | |
| | 34 | 580 | | if (Separates(other.GetPlane(i).Normal, this, other)) |
| | 2 | 581 | | return false; |
| | | 582 | | } |
| | | 583 | | |
| | 56 | 584 | | for (int i = 0; i < EdgeCount; i++) |
| | | 585 | | { |
| | 26 | 586 | | Vector3d edgeA = GetCorner(GetEdgeEnd(i)) - GetCorner(GetEdgeStart(i)); |
| | | 587 | | |
| | 628 | 588 | | for (int j = 0; j < EdgeCount; j++) |
| | | 589 | | { |
| | 290 | 590 | | Vector3d edgeB = other.GetCorner(GetEdgeEnd(j)) - other.GetCorner(GetEdgeStart(j)); |
| | 290 | 591 | | Vector3d axis = Vector3d.Cross(edgeA, edgeB); |
| | | 592 | | |
| | 290 | 593 | | if (axis.MagnitudeSquared <= Fixed64.Epsilon) |
| | | 594 | | continue; |
| | | 595 | | |
| | 194 | 596 | | if (Separates(axis, this, other)) |
| | 2 | 597 | | return false; |
| | | 598 | | } |
| | | 599 | | } |
| | | 600 | | |
| | 2 | 601 | | return true; |
| | | 602 | | } |
| | | 603 | | |
| | | 604 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 605 | | private static bool DisjointOrIntersects(FixedPlane plane, FixedBoundBox box, ref bool intersects) |
| | | 606 | | { |
| | 60 | 607 | | switch (plane.Intersects(box)) |
| | | 608 | | { |
| | | 609 | | case FixedPlaneIntersectionType.Front: |
| | 3 | 610 | | return true; |
| | | 611 | | case FixedPlaneIntersectionType.Intersecting: |
| | 18 | 612 | | intersects = true; |
| | | 613 | | break; |
| | | 614 | | } |
| | | 615 | | |
| | 57 | 616 | | return false; |
| | | 617 | | } |
| | | 618 | | |
| | | 619 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 620 | | private static bool DisjointOrIntersects(FixedPlane plane, FixedBoundSphere sphere, ref bool intersects) |
| | | 621 | | { |
| | 44 | 622 | | switch (plane.Intersects(sphere)) |
| | | 623 | | { |
| | | 624 | | case FixedPlaneIntersectionType.Front: |
| | 2 | 625 | | return true; |
| | | 626 | | case FixedPlaneIntersectionType.Intersecting: |
| | 19 | 627 | | intersects = true; |
| | | 628 | | break; |
| | | 629 | | } |
| | | 630 | | |
| | 42 | 631 | | return false; |
| | | 632 | | } |
| | | 633 | | |
| | | 634 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 635 | | private static bool ClipRay(FixedPlane plane, FixedRay ray, ref Fixed64 tEnter, ref Fixed64 tExit) |
| | | 636 | | { |
| | 31 | 637 | | Fixed64 distance = plane.DotCoordinate(ray.Position); |
| | 31 | 638 | | Fixed64 denominator = plane.DotNormal(ray.Direction); |
| | | 639 | | |
| | 31 | 640 | | if (FixedRay.IsNearlyZero(denominator)) |
| | 14 | 641 | | return distance <= Fixed64.Zero; |
| | | 642 | | |
| | 17 | 643 | | Fixed64 t = -distance / denominator; |
| | 17 | 644 | | if (denominator < Fixed64.Zero) |
| | | 645 | | { |
| | 8 | 646 | | if (t > tEnter) |
| | 3 | 647 | | tEnter = t; |
| | | 648 | | } |
| | 9 | 649 | | else if (t < tExit) |
| | | 650 | | { |
| | 7 | 651 | | tExit = t; |
| | | 652 | | } |
| | | 653 | | |
| | 17 | 654 | | return tEnter <= tExit; |
| | | 655 | | } |
| | | 656 | | |
| | | 657 | | private static bool Separates(Vector3d axis, in FixedBoundFrustum a, in FixedBoundFrustum b) |
| | | 658 | | { |
| | 266 | 659 | | Project(axis, a, out Fixed64 minA, out Fixed64 maxA); |
| | 266 | 660 | | Project(axis, b, out Fixed64 minB, out Fixed64 maxB); |
| | | 661 | | |
| | 266 | 662 | | return maxA < minB || maxB < minA; |
| | | 663 | | } |
| | | 664 | | |
| | | 665 | | private static void Project(Vector3d axis, in FixedBoundFrustum frustum, out Fixed64 min, out Fixed64 max) |
| | | 666 | | { |
| | 532 | 667 | | min = Vector3d.Dot(axis, frustum._nearTopLeft); |
| | 532 | 668 | | max = min; |
| | | 669 | | |
| | 8512 | 670 | | for (int i = 1; i < CornerCount; i++) |
| | | 671 | | { |
| | 3724 | 672 | | Fixed64 projected = Vector3d.Dot(axis, frustum.GetCorner(i)); |
| | 3724 | 673 | | if (projected < min) |
| | 336 | 674 | | min = projected; |
| | 3388 | 675 | | else if (projected > max) |
| | 322 | 676 | | max = projected; |
| | | 677 | | } |
| | 532 | 678 | | } |
| | | 679 | | |
| | | 680 | | private static int GetEdgeStart(int edge) |
| | | 681 | | { |
| | 364 | 682 | | return s_edgeStarts[edge]; |
| | | 683 | | } |
| | | 684 | | |
| | | 685 | | private static int GetEdgeEnd(int edge) |
| | | 686 | | { |
| | 364 | 687 | | return s_edgeEnds[edge]; |
| | | 688 | | } |
| | | 689 | | |
| | | 690 | | #endregion |
| | | 691 | | |
| | | 692 | | #region Equality |
| | | 693 | | |
| | | 694 | | /// <inheritdoc/> |
| | | 695 | | public bool Equals(FixedBoundFrustum other) |
| | | 696 | | { |
| | 15 | 697 | | return _near == other._near |
| | 15 | 698 | | && _far == other._far |
| | 15 | 699 | | && _left == other._left |
| | 15 | 700 | | && _right == other._right |
| | 15 | 701 | | && _top == other._top |
| | 15 | 702 | | && _bottom == other._bottom; |
| | | 703 | | } |
| | | 704 | | |
| | | 705 | | /// <inheritdoc/> |
| | 3 | 706 | | public override bool Equals(object? obj) => obj is FixedBoundFrustum other && Equals(other); |
| | | 707 | | |
| | | 708 | | /// <inheritdoc/> |
| | | 709 | | public override int GetHashCode() |
| | | 710 | | { |
| | | 711 | | unchecked |
| | | 712 | | { |
| | 3 | 713 | | int hash = 17; |
| | 3 | 714 | | hash = (hash * 31) + _near.GetHashCode(); |
| | 3 | 715 | | hash = (hash * 31) + _far.GetHashCode(); |
| | 3 | 716 | | hash = (hash * 31) + _left.GetHashCode(); |
| | 3 | 717 | | hash = (hash * 31) + _right.GetHashCode(); |
| | 3 | 718 | | hash = (hash * 31) + _top.GetHashCode(); |
| | 3 | 719 | | hash = (hash * 31) + _bottom.GetHashCode(); |
| | 3 | 720 | | return hash; |
| | | 721 | | } |
| | | 722 | | } |
| | | 723 | | |
| | | 724 | | #endregion |
| | | 725 | | } |