| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | using System.Text.Json.Serialization; |
| | | 6 | | |
| | | 7 | | namespace FixedMathSharp |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents a spherical bounding volume with fixed-point precision, optimized for fast, rotationally invariant sp |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// The BoundingSphere provides a simple yet effective way to represent the spatial extent of objects, especially wh |
| | | 14 | | /// Compared to BoundingBox, it offers faster intersection checks but is less precise in tightly fitting non-spheric |
| | | 15 | | /// |
| | | 16 | | /// Use Cases: |
| | | 17 | | /// - Ideal for broad-phase collision detection, proximity checks, and culling in physics engines and rendering pipe |
| | | 18 | | /// - Useful when fast, rotationally invariant checks are needed, such as detecting overlaps or distances between mo |
| | | 19 | | /// - Suitable for encapsulating objects with roughly spherical shapes or objects that rotate frequently, where the |
| | | 20 | | /// </remarks> |
| | | 21 | | [Serializable] |
| | | 22 | | [MemoryPackable] |
| | | 23 | | public partial struct BoundingSphere : IEquatable<BoundingSphere> |
| | | 24 | | { |
| | | 25 | | #region Fields |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// The center point of the sphere. |
| | | 29 | | /// </summary> |
| | | 30 | | [JsonInclude] |
| | | 31 | | [MemoryPackOrder(0)] |
| | | 32 | | public Vector3d Center; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// The radius of the sphere. |
| | | 36 | | /// </summary> |
| | | 37 | | [JsonInclude] |
| | | 38 | | [MemoryPackOrder(1)] |
| | | 39 | | public Fixed64 Radius; |
| | | 40 | | |
| | | 41 | | #endregion |
| | | 42 | | |
| | | 43 | | #region Constructors |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Initializes a new instance of the BoundingSphere struct with the specified center and radius. |
| | | 47 | | /// </summary> |
| | | 48 | | [JsonConstructor] |
| | | 49 | | public BoundingSphere(Vector3d center, Fixed64 radius) |
| | 77 | 50 | | { |
| | 77 | 51 | | Center = center; |
| | 77 | 52 | | Radius = radius; |
| | 77 | 53 | | } |
| | | 54 | | |
| | | 55 | | #endregion |
| | | 56 | | |
| | | 57 | | #region Properties |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets the coordinates of the minimum corner of the bounding box that contains the sphere. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <remarks> |
| | | 63 | | /// The minimum corner is calculated by subtracting the radius from each component of the sphere's center. |
| | | 64 | | /// This property is useful for spatial queries and bounding box calculations. |
| | | 65 | | /// </remarks> |
| | | 66 | | [JsonIgnore] |
| | | 67 | | [MemoryPackIgnore] |
| | | 68 | | public Vector3d Min |
| | | 69 | | { |
| | | 70 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 7 | 71 | | get => Center - new Vector3d(Radius, Radius, Radius); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Gets the coordinates of the maximum corner of the bounding box that contains the sphere. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <remarks> |
| | | 78 | | /// The maximum corner is calculated as the center of the sphere plus the radius in each dimension. |
| | | 79 | | /// This property is useful for spatial queries and bounding volume calculations. |
| | | 80 | | /// </remarks> |
| | | 81 | | [JsonIgnore] |
| | | 82 | | [MemoryPackIgnore] |
| | | 83 | | public Vector3d Max |
| | | 84 | | { |
| | | 85 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 5 | 86 | | get => Center + new Vector3d(Radius, Radius, Radius); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// The squared radius of the sphere. |
| | | 91 | | /// </summary> |
| | | 92 | | [JsonIgnore] |
| | | 93 | | [MemoryPackIgnore] |
| | | 94 | | public Fixed64 SqrRadius |
| | | 95 | | { |
| | | 96 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 81 | 97 | | get => Radius * Radius; |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | #endregion |
| | | 101 | | |
| | | 102 | | #region Methods (Static) |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Creates a bounding sphere that contains the specified axis-aligned bounding box. |
| | | 106 | | /// </summary> |
| | | 107 | | public static BoundingSphere CreateFromBoundingBox(BoundingBox box) |
| | 1 | 108 | | { |
| | 1 | 109 | | Vector3d center = (box.Min + box.Max) * Fixed64.Half; |
| | 1 | 110 | | Fixed64 radius = Vector3d.Distance(center, box.Max); |
| | | 111 | | |
| | 1 | 112 | | return new BoundingSphere(center, radius); |
| | 1 | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Creates a bounding sphere that contains the specified frustum. |
| | | 117 | | /// </summary> |
| | | 118 | | public static BoundingSphere CreateFromFrustum(BoundingFrustum frustum) |
| | 2 | 119 | | { |
| | 2 | 120 | | if (frustum == null) |
| | 1 | 121 | | throw new ArgumentNullException(nameof(frustum)); |
| | | 122 | | |
| | 1 | 123 | | return CreateFromPoints(frustum.GetCorners()); |
| | 1 | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Creates a bounding sphere that contains the specified points. |
| | | 128 | | /// </summary> |
| | | 129 | | public static BoundingSphere CreateFromPoints(IEnumerable<Vector3d> points) |
| | 6 | 130 | | { |
| | 6 | 131 | | if (points == null) |
| | 1 | 132 | | throw new ArgumentNullException(nameof(points)); |
| | | 133 | | |
| | 5 | 134 | | if (points is IReadOnlyList<Vector3d> pointList) |
| | 4 | 135 | | return CreateFromPointList(pointList); |
| | | 136 | | |
| | 1 | 137 | | var materialized = new List<Vector3d>(); |
| | 11 | 138 | | foreach (Vector3d point in points) |
| | 4 | 139 | | materialized.Add(point); |
| | | 140 | | |
| | 1 | 141 | | return CreateFromPointList(materialized); |
| | 4 | 142 | | } |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Creates the smallest sphere that contains the two specified spheres. |
| | | 146 | | /// </summary> |
| | | 147 | | public static BoundingSphere CreateMerged(BoundingSphere original, BoundingSphere additional) |
| | 5 | 148 | | { |
| | 5 | 149 | | Vector3d centerOffset = additional.Center - original.Center; |
| | 5 | 150 | | Fixed64 distance = centerOffset.Magnitude; |
| | | 151 | | |
| | 5 | 152 | | if (distance + additional.Radius <= original.Radius) |
| | 2 | 153 | | return original; |
| | | 154 | | |
| | 3 | 155 | | if (distance + original.Radius <= additional.Radius) |
| | 2 | 156 | | return additional; |
| | | 157 | | |
| | 1 | 158 | | Fixed64 radius = (distance + original.Radius + additional.Radius) * Fixed64.Half; |
| | 1 | 159 | | Vector3d center = original.Center + centerOffset * ((radius - original.Radius) / distance); |
| | | 160 | | |
| | 1 | 161 | | return new BoundingSphere(center, radius); |
| | 5 | 162 | | } |
| | | 163 | | |
| | | 164 | | private static BoundingSphere CreateFromPointList(IReadOnlyList<Vector3d> points) |
| | 5 | 165 | | { |
| | 5 | 166 | | if (points.Count == 0) |
| | 1 | 167 | | throw new ArgumentException("At least one point is required.", nameof(points)); |
| | | 168 | | |
| | 4 | 169 | | Vector3d minX = points[0]; |
| | 4 | 170 | | Vector3d maxX = points[0]; |
| | 4 | 171 | | Vector3d minY = points[0]; |
| | 4 | 172 | | Vector3d maxY = points[0]; |
| | 4 | 173 | | Vector3d minZ = points[0]; |
| | 4 | 174 | | Vector3d maxZ = points[0]; |
| | | 175 | | |
| | 40 | 176 | | for (int i = 1; i < points.Count; i++) |
| | 16 | 177 | | { |
| | 16 | 178 | | Vector3d point = points[i]; |
| | | 179 | | |
| | 17 | 180 | | if (point.x < minX.x) minX = point; |
| | 20 | 181 | | if (point.x > maxX.x) maxX = point; |
| | 17 | 182 | | if (point.y < minY.y) minY = point; |
| | 19 | 183 | | if (point.y > maxY.y) maxY = point; |
| | 17 | 184 | | if (point.z < minZ.z) minZ = point; |
| | 18 | 185 | | if (point.z > maxZ.z) maxZ = point; |
| | 16 | 186 | | } |
| | | 187 | | |
| | 4 | 188 | | Fixed64 sqDistX = Vector3d.SqrDistance(maxX, minX); |
| | 4 | 189 | | Fixed64 sqDistY = Vector3d.SqrDistance(maxY, minY); |
| | 4 | 190 | | Fixed64 sqDistZ = Vector3d.SqrDistance(maxZ, minZ); |
| | | 191 | | |
| | 4 | 192 | | Vector3d min = minX; |
| | 4 | 193 | | Vector3d max = maxX; |
| | 4 | 194 | | Fixed64 largestDistance = sqDistX; |
| | | 195 | | |
| | 4 | 196 | | if (sqDistY > largestDistance) |
| | 3 | 197 | | { |
| | 3 | 198 | | min = minY; |
| | 3 | 199 | | max = maxY; |
| | 3 | 200 | | largestDistance = sqDistY; |
| | 3 | 201 | | } |
| | | 202 | | |
| | 4 | 203 | | if (sqDistZ > largestDistance) |
| | 1 | 204 | | { |
| | 1 | 205 | | min = minZ; |
| | 1 | 206 | | max = maxZ; |
| | 1 | 207 | | } |
| | | 208 | | |
| | 4 | 209 | | Vector3d center = (min + max) * Fixed64.Half; |
| | 4 | 210 | | Fixed64 radius = Vector3d.Distance(max, center); |
| | 4 | 211 | | Fixed64 sqRadius = radius * radius; |
| | | 212 | | |
| | 48 | 213 | | for (int i = 0; i < points.Count; i++) |
| | 20 | 214 | | { |
| | 20 | 215 | | Vector3d diff = points[i] - center; |
| | 20 | 216 | | Fixed64 sqDistance = diff.SqrMagnitude; |
| | 20 | 217 | | if (sqDistance <= sqRadius) |
| | 14 | 218 | | continue; |
| | | 219 | | |
| | 6 | 220 | | Fixed64 distance = FixedMath.Sqrt(sqDistance); |
| | 6 | 221 | | if (distance == Fixed64.Zero) |
| | 0 | 222 | | continue; |
| | | 223 | | |
| | 6 | 224 | | Fixed64 newRadius = (radius + distance) * Fixed64.Half; |
| | 6 | 225 | | center += diff * ((distance - radius) / (Fixed64.Two * distance)); |
| | 6 | 226 | | radius = newRadius; |
| | 6 | 227 | | sqRadius = EnsureRadiusContainsPoint(points[i], center, ref radius); |
| | 6 | 228 | | } |
| | | 229 | | |
| | 4 | 230 | | return new BoundingSphere(center, radius); |
| | 4 | 231 | | } |
| | | 232 | | |
| | | 233 | | private static Fixed64 EnsureRadiusContainsPoint(Vector3d point, Vector3d center, ref Fixed64 radius) |
| | 6 | 234 | | { |
| | 6 | 235 | | Fixed64 sqRadius = radius * radius; |
| | 6 | 236 | | Fixed64 sqDistance = Vector3d.SqrDistance(point, center); |
| | | 237 | | |
| | 6 | 238 | | if (sqDistance <= sqRadius) |
| | 1 | 239 | | return sqRadius; |
| | | 240 | | |
| | 5 | 241 | | radius = FixedMath.Sqrt(sqDistance); |
| | 5 | 242 | | sqRadius = radius * radius; |
| | | 243 | | |
| | 5 | 244 | | if (sqRadius < sqDistance) |
| | 3 | 245 | | { |
| | 3 | 246 | | radius += Fixed64.MinIncrement; |
| | 3 | 247 | | sqRadius = radius * radius; |
| | 3 | 248 | | } |
| | | 249 | | |
| | 5 | 250 | | return sqRadius; |
| | 6 | 251 | | } |
| | | 252 | | |
| | | 253 | | #endregion |
| | | 254 | | |
| | | 255 | | #region Methods (Instance) |
| | | 256 | | |
| | | 257 | | /// <summary> |
| | | 258 | | /// Checks if a point is inside the sphere. |
| | | 259 | | /// </summary> |
| | | 260 | | /// <param name="point">The point to check.</param> |
| | | 261 | | /// <returns>True if the point is inside the sphere, otherwise false.</returns> |
| | | 262 | | public bool Contains(Vector3d point) |
| | 63 | 263 | | { |
| | 63 | 264 | | return Vector3d.SqrDistance(Center, point) <= SqrRadius; |
| | 63 | 265 | | } |
| | | 266 | | |
| | | 267 | | /// <summary> |
| | | 268 | | /// Tests a bounding box against this sphere. |
| | | 269 | | /// </summary> |
| | | 270 | | public ContainmentType Contains(BoundingBox box) |
| | 5 | 271 | | { |
| | 5 | 272 | | return ContainsBoxLike(box.Min, box.Max); |
| | 5 | 273 | | } |
| | | 274 | | |
| | | 275 | | /// <summary> |
| | | 276 | | /// Tests a bounding area against this sphere. |
| | | 277 | | /// </summary> |
| | | 278 | | public ContainmentType Contains(BoundingArea area) |
| | 5 | 279 | | { |
| | 5 | 280 | | return ContainsBoxLike(area.Min, area.Max); |
| | 5 | 281 | | } |
| | | 282 | | |
| | | 283 | | /// <summary> |
| | | 284 | | /// Tests another sphere against this sphere. |
| | | 285 | | /// </summary> |
| | | 286 | | public ContainmentType Contains(BoundingSphere sphere) |
| | 8 | 287 | | { |
| | 8 | 288 | | Fixed64 sqDistance = Vector3d.SqrDistance(Center, sphere.Center); |
| | 8 | 289 | | Fixed64 combinedRadius = Radius + sphere.Radius; |
| | | 290 | | |
| | 8 | 291 | | if (sqDistance > combinedRadius * combinedRadius) |
| | 2 | 292 | | return ContainmentType.Disjoint; |
| | | 293 | | |
| | 6 | 294 | | Fixed64 radiusDifference = Radius - sphere.Radius; |
| | 6 | 295 | | if (radiusDifference >= Fixed64.Zero && sqDistance <= radiusDifference * radiusDifference) |
| | 1 | 296 | | return ContainmentType.Contains; |
| | | 297 | | |
| | 5 | 298 | | return ContainmentType.Intersects; |
| | 8 | 299 | | } |
| | | 300 | | |
| | | 301 | | /// <summary> |
| | | 302 | | /// Tests a frustum against this sphere. |
| | | 303 | | /// </summary> |
| | | 304 | | public ContainmentType Contains(BoundingFrustum frustum) |
| | 4 | 305 | | { |
| | 4 | 306 | | if (frustum == null) |
| | 1 | 307 | | throw new ArgumentNullException(nameof(frustum)); |
| | | 308 | | |
| | 3 | 309 | | Vector3d[] corners = frustum.GetCorners(); |
| | 3 | 310 | | bool containsAllCorners = true; |
| | | 311 | | |
| | 22 | 312 | | for (int i = 0; i < corners.Length; i++) |
| | 10 | 313 | | { |
| | 10 | 314 | | if (!Contains(corners[i])) |
| | 2 | 315 | | { |
| | 2 | 316 | | containsAllCorners = false; |
| | 2 | 317 | | break; |
| | | 318 | | } |
| | 8 | 319 | | } |
| | | 320 | | |
| | 3 | 321 | | if (containsAllCorners) |
| | 1 | 322 | | return ContainmentType.Contains; |
| | | 323 | | |
| | 2 | 324 | | return frustum.Intersects(this) |
| | 2 | 325 | | ? ContainmentType.Intersects |
| | 2 | 326 | | : ContainmentType.Disjoint; |
| | 3 | 327 | | } |
| | | 328 | | |
| | | 329 | | /// <summary> |
| | | 330 | | /// Checks whether a bounding box intersects this sphere. |
| | | 331 | | /// </summary> |
| | | 332 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 333 | | public bool Intersects(BoundingBox box) => Contains(box) != ContainmentType.Disjoint; |
| | | 334 | | |
| | | 335 | | /// <summary> |
| | | 336 | | /// Checks whether a bounding area intersects this sphere. |
| | | 337 | | /// </summary> |
| | | 338 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 339 | | public bool Intersects(BoundingArea area) => Contains(area) != ContainmentType.Disjoint; |
| | | 340 | | |
| | | 341 | | /// <summary> |
| | | 342 | | /// Checks whether another sphere intersects this sphere. |
| | | 343 | | /// </summary> |
| | | 344 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 4 | 345 | | public bool Intersects(BoundingSphere sphere) => Contains(sphere) != ContainmentType.Disjoint; |
| | | 346 | | |
| | | 347 | | /// <summary> |
| | | 348 | | /// Checks whether a frustum intersects this sphere. |
| | | 349 | | /// </summary> |
| | | 350 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 351 | | public bool Intersects(BoundingFrustum frustum) |
| | 2 | 352 | | { |
| | 2 | 353 | | if (frustum == null) |
| | 1 | 354 | | throw new ArgumentNullException(nameof(frustum)); |
| | | 355 | | |
| | 1 | 356 | | return frustum.Intersects(this); |
| | 1 | 357 | | } |
| | | 358 | | |
| | | 359 | | /// <summary> |
| | | 360 | | /// Classifies this sphere relative to a plane. |
| | | 361 | | /// </summary> |
| | | 362 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 363 | | public FixedPlaneIntersectionType Intersects(FixedPlane plane) => plane.Intersects(this); |
| | | 364 | | |
| | | 365 | | /// <summary> |
| | | 366 | | /// Finds the first forward ray intersection with this sphere. |
| | | 367 | | /// </summary> |
| | | 368 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 369 | | public Fixed64? Intersects(FixedRay ray) => ray.Intersects(this); |
| | | 370 | | |
| | | 371 | | /// <summary> |
| | | 372 | | /// Projects a point onto the bounding sphere. If the point is outside the sphere, it returns the closest point |
| | | 373 | | /// </summary> |
| | | 374 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 375 | | public Vector3d ProjectPoint(Vector3d point) |
| | 3 | 376 | | { |
| | 3 | 377 | | var direction = point - Center; |
| | 4 | 378 | | if (direction.IsZero) return Center; // If the point is the center, return the center itself |
| | | 379 | | |
| | 2 | 380 | | return Center + direction.Normalize() * Radius; |
| | 3 | 381 | | } |
| | | 382 | | |
| | | 383 | | /// <summary> |
| | | 384 | | /// Clamps a point to this sphere, returning the point unchanged when it is already inside the sphere. |
| | | 385 | | /// </summary> |
| | | 386 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 387 | | public Vector3d ClampPoint(Vector3d point) |
| | 3 | 388 | | { |
| | 3 | 389 | | if (Contains(point)) |
| | 2 | 390 | | return point; |
| | | 391 | | |
| | 1 | 392 | | return ProjectPoint(point); |
| | 3 | 393 | | } |
| | | 394 | | |
| | | 395 | | /// <summary> |
| | | 396 | | /// Calculates the distance from a point to the surface of the sphere. |
| | | 397 | | /// </summary> |
| | | 398 | | /// <param name="point">The point to calculate the distance from.</param> |
| | | 399 | | /// <returns>The distance from the point to the surface of the sphere.</returns> |
| | | 400 | | public Fixed64 DistanceToSurface(Vector3d point) |
| | 4 | 401 | | { |
| | 4 | 402 | | return Vector3d.Distance(Center, point) - Radius; |
| | 4 | 403 | | } |
| | | 404 | | |
| | | 405 | | /// <summary> |
| | | 406 | | /// Creates a sphere that contains this sphere transformed by the specified matrix. |
| | | 407 | | /// </summary> |
| | | 408 | | public BoundingSphere Transform(Fixed4x4 matrix) |
| | 1 | 409 | | { |
| | 1 | 410 | | Vector3d center = Fixed4x4.TransformPoint(matrix, Center); |
| | 1 | 411 | | Fixed64 scale = GetMaxBasisScale(matrix); |
| | | 412 | | |
| | 1 | 413 | | return new BoundingSphere(center, Radius * scale); |
| | 1 | 414 | | } |
| | | 415 | | |
| | | 416 | | /// <summary> |
| | | 417 | | /// Deconstructs this sphere into its center and radius. |
| | | 418 | | /// </summary> |
| | | 419 | | public void Deconstruct(out Vector3d center, out Fixed64 radius) |
| | 0 | 420 | | { |
| | 0 | 421 | | center = Center; |
| | 0 | 422 | | radius = Radius; |
| | 0 | 423 | | } |
| | | 424 | | |
| | | 425 | | private ContainmentType ContainsBoxLike(Vector3d min, Vector3d max) |
| | 10 | 426 | | { |
| | 10 | 427 | | bool containsAllCorners = |
| | 10 | 428 | | Contains(new Vector3d(min.x, min.y, min.z)) && |
| | 10 | 429 | | Contains(new Vector3d(max.x, min.y, min.z)) && |
| | 10 | 430 | | Contains(new Vector3d(min.x, max.y, min.z)) && |
| | 10 | 431 | | Contains(new Vector3d(max.x, max.y, min.z)) && |
| | 10 | 432 | | Contains(new Vector3d(min.x, min.y, max.z)) && |
| | 10 | 433 | | Contains(new Vector3d(max.x, min.y, max.z)) && |
| | 10 | 434 | | Contains(new Vector3d(min.x, max.y, max.z)) && |
| | 10 | 435 | | Contains(new Vector3d(max.x, max.y, max.z)); |
| | | 436 | | |
| | 10 | 437 | | if (containsAllCorners) |
| | 3 | 438 | | return ContainmentType.Contains; |
| | | 439 | | |
| | 7 | 440 | | Vector3d closest = new( |
| | 7 | 441 | | FixedMath.Clamp(Center.x, min.x, max.x), |
| | 7 | 442 | | FixedMath.Clamp(Center.y, min.y, max.y), |
| | 7 | 443 | | FixedMath.Clamp(Center.z, min.z, max.z)); |
| | | 444 | | |
| | 7 | 445 | | return Vector3d.SqrDistance(Center, closest) <= SqrRadius |
| | 7 | 446 | | ? ContainmentType.Intersects |
| | 7 | 447 | | : ContainmentType.Disjoint; |
| | 10 | 448 | | } |
| | | 449 | | |
| | | 450 | | private static Fixed64 GetMaxBasisScale(Fixed4x4 matrix) |
| | 1 | 451 | | { |
| | 1 | 452 | | Fixed64 row0 = matrix.m00 * matrix.m00 + matrix.m01 * matrix.m01 + matrix.m02 * matrix.m02; |
| | 1 | 453 | | Fixed64 row1 = matrix.m10 * matrix.m10 + matrix.m11 * matrix.m11 + matrix.m12 * matrix.m12; |
| | 1 | 454 | | Fixed64 row2 = matrix.m20 * matrix.m20 + matrix.m21 * matrix.m21 + matrix.m22 * matrix.m22; |
| | | 455 | | |
| | 1 | 456 | | return FixedMath.Sqrt(FixedMath.Max(row0, FixedMath.Max(row1, row2))); |
| | 1 | 457 | | } |
| | | 458 | | |
| | | 459 | | #endregion |
| | | 460 | | |
| | | 461 | | #region Operators |
| | | 462 | | |
| | | 463 | | /// <summary> |
| | | 464 | | /// Determines whether two BoundingSphere instances are equal. |
| | | 465 | | /// </summary> |
| | | 466 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 467 | | public static bool operator ==(BoundingSphere left, BoundingSphere right) => left.Equals(right); |
| | | 468 | | |
| | | 469 | | /// <summary> |
| | | 470 | | /// Determines whether two BoundingSphere instances are not equal. |
| | | 471 | | /// </summary> |
| | | 472 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 473 | | public static bool operator !=(BoundingSphere left, BoundingSphere right) => !left.Equals(right); |
| | | 474 | | |
| | | 475 | | #endregion |
| | | 476 | | |
| | | 477 | | #region Equality and HashCode Overrides |
| | | 478 | | |
| | | 479 | | /// <inheritdoc/> |
| | | 480 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 481 | | public override bool Equals(object? obj) => obj is BoundingSphere other && Equals(other); |
| | | 482 | | |
| | | 483 | | /// <inheritdoc/> |
| | | 484 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 10 | 485 | | public bool Equals(BoundingSphere other) => Center.Equals(other.Center) && Radius.Equals(other.Radius); |
| | | 486 | | |
| | | 487 | | /// <inheritdoc/> |
| | | 488 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 489 | | public override int GetHashCode() |
| | 2 | 490 | | { |
| | | 491 | | unchecked |
| | 2 | 492 | | { |
| | 2 | 493 | | int hash = 17; |
| | 2 | 494 | | hash = hash * 23 + Center.GetHashCode(); |
| | 2 | 495 | | hash = hash * 23 + Radius.GetHashCode(); |
| | 2 | 496 | | return hash; |
| | | 497 | | } |
| | 2 | 498 | | } |
| | | 499 | | |
| | | 500 | | /// <summary> |
| | | 501 | | /// Returns a string that represents the current BoundingSphere. |
| | | 502 | | /// </summary> |
| | | 503 | | public override string ToString() |
| | 0 | 504 | | { |
| | 0 | 505 | | return $"{{Center:{Center} Radius:{Radius}}}"; |
| | 0 | 506 | | } |
| | | 507 | | |
| | | 508 | | #endregion |
| | | 509 | | } |
| | | 510 | | } |