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