| | | 1 | | //======================================================================= |
| | | 2 | | // FixedRay.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 | | using System.Text.Json.Serialization; |
| | | 11 | | using MemoryPack; |
| | | 12 | | |
| | | 13 | | namespace FixedMathSharp.Geometry; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Represents a ray with an origin and direction in three-dimensional space. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <remarks> |
| | | 19 | | /// Intersection methods return the ray parameter for the first forward hit. If <see cref="Direction"/> is normalized, |
| | | 20 | | /// that parameter is also the distance from <see cref="Position"/>. |
| | | 21 | | /// </remarks> |
| | | 22 | | [Serializable] |
| | | 23 | | [MemoryPackable] |
| | | 24 | | public partial struct FixedRay : IEquatable<FixedRay> |
| | | 25 | | { |
| | | 26 | | #region Fields |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// The origin of the ray. |
| | | 30 | | /// </summary> |
| | | 31 | | [JsonInclude] |
| | | 32 | | [MemoryPackOrder(0)] |
| | | 33 | | public Vector3d Position; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// The direction of the ray. |
| | | 37 | | /// </summary> |
| | | 38 | | [JsonInclude] |
| | | 39 | | [MemoryPackOrder(1)] |
| | | 40 | | public Vector3d Direction; |
| | | 41 | | |
| | | 42 | | #endregion |
| | | 43 | | |
| | | 44 | | #region Constructors |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Initializes a new ray with the specified origin and direction. |
| | | 48 | | /// </summary> |
| | | 49 | | [JsonConstructor] |
| | | 50 | | public FixedRay(Vector3d position, Vector3d direction) |
| | | 51 | | { |
| | 104 | 52 | | Position = position; |
| | 104 | 53 | | Direction = direction; |
| | 104 | 54 | | } |
| | | 55 | | |
| | | 56 | | #endregion |
| | | 57 | | |
| | | 58 | | #region Methods |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets the point at the specified ray parameter with one final |
| | | 62 | | /// round-half-to-even conversion per coordinate. |
| | | 63 | | /// </summary> |
| | | 64 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 65 | | public Vector3d GetPoint(Fixed64 parameter) => new( |
| | 1 | 66 | | Fixed64.MultiplyAdd(Direction.X, parameter, Position.X), |
| | 1 | 67 | | Fixed64.MultiplyAdd(Direction.Y, parameter, Position.Y), |
| | 1 | 68 | | Fixed64.MultiplyAdd(Direction.Z, parameter, Position.Z)); |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Attempts to get the point at the specified ray parameter with one final |
| | | 72 | | /// round-half-to-even conversion per coordinate. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="parameter">The parametric distance along the ray direction.</param> |
| | | 75 | | /// <param name="point"> |
| | | 76 | | /// The point when every final coordinate is representable; otherwise, |
| | | 77 | | /// <see langword="default"/>. |
| | | 78 | | /// </param> |
| | | 79 | | /// <returns> |
| | | 80 | | /// <see langword="true"/> when every final coordinate is representable; |
| | | 81 | | /// otherwise, <see langword="false"/>. |
| | | 82 | | /// </returns> |
| | | 83 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 84 | | public readonly bool TryGetPoint(Fixed64 parameter, out Vector3d point) |
| | | 85 | | { |
| | 7 | 86 | | if (!Fixed64.TryMultiplyAdd(Direction.X, parameter, Position.X, out Fixed64 x) |
| | 7 | 87 | | || !Fixed64.TryMultiplyAdd(Direction.Y, parameter, Position.Y, out Fixed64 y) |
| | 7 | 88 | | || !Fixed64.TryMultiplyAdd(Direction.Z, parameter, Position.Z, out Fixed64 z)) |
| | | 89 | | { |
| | 3 | 90 | | point = default; |
| | 3 | 91 | | return false; |
| | | 92 | | } |
| | | 93 | | |
| | 4 | 94 | | point = new Vector3d(x, y, z); |
| | 4 | 95 | | return true; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Finds the first forward intersection with the specified plane. |
| | | 100 | | /// </summary> |
| | | 101 | | public Fixed64? Intersects(FixedPlane plane) |
| | | 102 | | { |
| | 3 | 103 | | Fixed64 denominator = plane.DotNormal(Direction); |
| | 3 | 104 | | if (IsNearlyZero(denominator)) |
| | 1 | 105 | | return null; |
| | | 106 | | |
| | 2 | 107 | | Fixed64 t = -plane.DotCoordinate(Position) / denominator; |
| | 2 | 108 | | return t < Fixed64.Zero ? null : t; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Finds the first forward intersection with the specified bounding box. |
| | | 113 | | /// </summary> |
| | | 114 | | public Fixed64? Intersects(FixedBoundBox box) |
| | | 115 | | { |
| | 15 | 116 | | return IntersectsBoxLike(box.Min, box.Max); |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Finds the first forward intersection with the specified bounding sphere. |
| | | 121 | | /// </summary> |
| | | 122 | | public Fixed64? Intersects(FixedBoundSphere sphere) => |
| | 19 | 123 | | WideRayIntersection.Intersects(Position, Direction, sphere, Fixed64.MaxValue); |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Finds the first forward intersection with the specified bounding sphere |
| | | 127 | | /// at or before <paramref name="maxParameter"/>. |
| | | 128 | | /// </summary> |
| | | 129 | | /// <remarks> |
| | | 130 | | /// Offset differences, quadratic products, the discriminant, and root |
| | | 131 | | /// ordering are evaluated without fixed-point saturation. The returned |
| | | 132 | | /// parameter uses deterministic round-half-to-even conversion. |
| | | 133 | | /// </remarks> |
| | | 134 | | public Fixed64? Intersects(FixedBoundSphere sphere, Fixed64 maxParameter) => |
| | 5 | 135 | | WideRayIntersection.Intersects(Position, Direction, sphere, maxParameter); |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Finds the first forward intersection with the specified bounding sphere, |
| | | 139 | | /// expanded by <paramref name="radiusExpansion"/>, at or before |
| | | 140 | | /// <paramref name="maxParameter"/>. |
| | | 141 | | /// </summary> |
| | | 142 | | /// <remarks> |
| | | 143 | | /// The two radii are combined in wide arithmetic, so their sum may exceed |
| | | 144 | | /// <see cref="Fixed64.MaxValue"/> without saturation. |
| | | 145 | | /// </remarks> |
| | | 146 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 147 | | /// <paramref name="radiusExpansion"/> is negative. |
| | | 148 | | /// </exception> |
| | | 149 | | public Fixed64? Intersects( |
| | | 150 | | FixedBoundSphere sphere, |
| | | 151 | | Fixed64 radiusExpansion, |
| | | 152 | | Fixed64 maxParameter) => |
| | 6 | 153 | | WideRayIntersection.Intersects(Position, Direction, sphere, radiusExpansion, maxParameter); |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Gets the closed parameter interval where this ray overlaps the sphere, |
| | | 157 | | /// clipped to <c>[0, <paramref name="maxParameter"/>]</c>. |
| | | 158 | | /// </summary> |
| | | 159 | | /// <remarks> |
| | | 160 | | /// Direction need not be normalized. Exact root clipping precedes |
| | | 161 | | /// deterministic round-half-to-even conversion of both endpoints. |
| | | 162 | | /// </remarks> |
| | | 163 | | public bool TryGetIntersectionInterval( |
| | | 164 | | FixedBoundSphere sphere, |
| | | 165 | | Fixed64 maxParameter, |
| | | 166 | | out Fixed64 entry, |
| | | 167 | | out Fixed64 exit) => |
| | 16 | 168 | | WideRayIntersection.TryGetInterval( |
| | 16 | 169 | | Position, |
| | 16 | 170 | | Direction, |
| | 16 | 171 | | sphere, |
| | 16 | 172 | | maxParameter, |
| | 16 | 173 | | out entry, |
| | 16 | 174 | | out exit); |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Gets the closed parameter interval where this ray overlaps the sphere |
| | | 178 | | /// expanded by <paramref name="radiusExpansion"/>, clipped to |
| | | 179 | | /// <c>[0, <paramref name="maxParameter"/>]</c>. |
| | | 180 | | /// </summary> |
| | | 181 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 182 | | /// <paramref name="radiusExpansion"/> is negative. |
| | | 183 | | /// </exception> |
| | | 184 | | public bool TryGetIntersectionInterval( |
| | | 185 | | FixedBoundSphere sphere, |
| | | 186 | | Fixed64 radiusExpansion, |
| | | 187 | | Fixed64 maxParameter, |
| | | 188 | | out Fixed64 entry, |
| | | 189 | | out Fixed64 exit) => |
| | 2 | 190 | | WideRayIntersection.TryGetInterval( |
| | 2 | 191 | | Position, |
| | 2 | 192 | | Direction, |
| | 2 | 193 | | sphere, |
| | 2 | 194 | | radiusExpansion, |
| | 2 | 195 | | maxParameter, |
| | 2 | 196 | | out entry, |
| | 2 | 197 | | out exit); |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// Finds the first forward intersection with the specified frustum. |
| | | 201 | | /// </summary> |
| | | 202 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 203 | | public Fixed64? Intersects(FixedBoundFrustum frustum) |
| | | 204 | | { |
| | 4 | 205 | | return frustum.Intersects(this); |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | private Fixed64? IntersectsBoxLike(Vector3d min, Vector3d max) |
| | | 209 | | { |
| | 15 | 210 | | Fixed64 tMin = Fixed64.Zero; |
| | 15 | 211 | | Fixed64 tMax = Fixed64.MaxValue; |
| | | 212 | | |
| | 15 | 213 | | if (!ClipAxis(Position.X, Direction.X, min.X, max.X, ref tMin, ref tMax)) |
| | 3 | 214 | | return null; |
| | | 215 | | |
| | 12 | 216 | | if (!ClipAxis(Position.Y, Direction.Y, min.Y, max.Y, ref tMin, ref tMax)) |
| | 2 | 217 | | return null; |
| | | 218 | | |
| | 10 | 219 | | if (!ClipAxis(Position.Z, Direction.Z, min.Z, max.Z, ref tMin, ref tMax)) |
| | 2 | 220 | | return null; |
| | | 221 | | |
| | 8 | 222 | | return tMin; |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 226 | | private static bool ClipAxis( |
| | | 227 | | Fixed64 position, |
| | | 228 | | Fixed64 direction, |
| | | 229 | | Fixed64 min, |
| | | 230 | | Fixed64 max, |
| | | 231 | | ref Fixed64 tMin, |
| | | 232 | | ref Fixed64 tMax) |
| | | 233 | | { |
| | 37 | 234 | | if (direction == Fixed64.Zero) |
| | 22 | 235 | | return position >= min && position <= max; |
| | | 236 | | |
| | 15 | 237 | | Fixed64 t1 = (min - position) / direction; |
| | 15 | 238 | | Fixed64 t2 = (max - position) / direction; |
| | | 239 | | |
| | 15 | 240 | | if (t1 > t2) |
| | | 241 | | { |
| | 2 | 242 | | Fixed64 temp = t1; |
| | 2 | 243 | | t1 = t2; |
| | 2 | 244 | | t2 = temp; |
| | | 245 | | } |
| | | 246 | | |
| | 15 | 247 | | if (t1 > tMin) |
| | 8 | 248 | | tMin = t1; |
| | | 249 | | |
| | 15 | 250 | | if (t2 < tMax) |
| | 12 | 251 | | tMax = t2; |
| | | 252 | | |
| | 15 | 253 | | return tMin <= tMax; |
| | | 254 | | } |
| | | 255 | | |
| | | 256 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 257 | | internal static bool IsNearlyZero(Fixed64 value) |
| | | 258 | | { |
| | 55 | 259 | | return value.Abs() <= Fixed64.Epsilon; |
| | | 260 | | } |
| | | 261 | | |
| | | 262 | | /// <summary> |
| | | 263 | | /// Deconstructs the ray into its origin and direction. |
| | | 264 | | /// </summary> |
| | | 265 | | public void Deconstruct(out Vector3d position, out Vector3d direction) |
| | | 266 | | { |
| | 1 | 267 | | position = Position; |
| | 1 | 268 | | direction = Direction; |
| | 1 | 269 | | } |
| | | 270 | | |
| | | 271 | | #endregion |
| | | 272 | | |
| | | 273 | | #region Operators |
| | | 274 | | |
| | | 275 | | /// <summary> |
| | | 276 | | /// Determines whether two rays are equal. |
| | | 277 | | /// </summary> |
| | | 278 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 279 | | public static bool operator ==(FixedRay left, FixedRay right) => left.Equals(right); |
| | | 280 | | |
| | | 281 | | /// <summary> |
| | | 282 | | /// Determines whether two rays are not equal. |
| | | 283 | | /// </summary> |
| | | 284 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 285 | | public static bool operator !=(FixedRay left, FixedRay right) => !left.Equals(right); |
| | | 286 | | |
| | | 287 | | #endregion |
| | | 288 | | |
| | | 289 | | #region Equality |
| | | 290 | | |
| | | 291 | | /// <inheritdoc/> |
| | | 292 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 293 | | public override bool Equals(object? obj) => obj is FixedRay other && Equals(other); |
| | | 294 | | |
| | | 295 | | /// <inheritdoc/> |
| | | 296 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 297 | | public bool Equals(FixedRay other) |
| | | 298 | | { |
| | 9 | 299 | | return Position.Equals(other.Position) && Direction.Equals(other.Direction); |
| | | 300 | | } |
| | | 301 | | |
| | | 302 | | /// <inheritdoc/> |
| | | 303 | | public override int GetHashCode() |
| | | 304 | | { |
| | | 305 | | unchecked |
| | | 306 | | { |
| | 2 | 307 | | int hash = 17; |
| | 2 | 308 | | hash = hash * 23 + Position.GetHashCode(); |
| | 2 | 309 | | hash = hash * 23 + Direction.GetHashCode(); |
| | 2 | 310 | | return hash; |
| | | 311 | | } |
| | | 312 | | } |
| | | 313 | | |
| | | 314 | | #endregion |
| | | 315 | | |
| | | 316 | | /// <summary> |
| | | 317 | | /// Gets the closed parameter interval where this ray overlaps a capsule, |
| | | 318 | | /// clipped to <c>[0, <paramref name="maxParameter"/>]</c>. |
| | | 319 | | /// </summary> |
| | | 320 | | public readonly bool TryGetCapsuleIntersectionInterval( |
| | | 321 | | FixedSegment capsuleAxis, |
| | | 322 | | Fixed64 radius, |
| | | 323 | | Fixed64 maxParameter, |
| | | 324 | | out Fixed64 entryParameter, |
| | | 325 | | out Fixed64 exitParameter) => |
| | 3 | 326 | | TryGetCapsuleIntersectionInterval( |
| | 3 | 327 | | capsuleAxis, radius, Fixed64.Zero, maxParameter, |
| | 3 | 328 | | out entryParameter, out exitParameter, out _, out _); |
| | | 329 | | |
| | | 330 | | /// <summary> |
| | | 331 | | /// Gets the closed parameter interval where this ray overlaps a radially |
| | | 332 | | /// expanded capsule and reports exact bounded-endpoint containment. |
| | | 333 | | /// </summary> |
| | | 334 | | /// <remarks> |
| | | 335 | | /// Direction need not be normalized. When it is normalized, returned |
| | | 336 | | /// parameters are physical distances. The origin is tested inclusively; |
| | | 337 | | /// the point at <paramref name="maxParameter"/> is tested strictly. |
| | | 338 | | /// </remarks> |
| | | 339 | | public readonly bool TryGetCapsuleIntersectionInterval( |
| | | 340 | | FixedSegment capsuleAxis, |
| | | 341 | | Fixed64 radius, |
| | | 342 | | Fixed64 radiusExpansion, |
| | | 343 | | Fixed64 maxParameter, |
| | | 344 | | out Fixed64 entryParameter, |
| | | 345 | | out Fixed64 exitParameter, |
| | | 346 | | out bool originContained, |
| | | 347 | | out bool maximumContainedStrict) |
| | | 348 | | { |
| | 9 | 349 | | if (radius < Fixed64.Zero) |
| | 1 | 350 | | throw new ArgumentOutOfRangeException(nameof(radius)); |
| | 8 | 351 | | if (radiusExpansion < Fixed64.Zero) |
| | 1 | 352 | | throw new ArgumentOutOfRangeException(nameof(radiusExpansion)); |
| | 7 | 353 | | if (!ValidateMaximum( |
| | 7 | 354 | | maxParameter, |
| | 7 | 355 | | out entryParameter, |
| | 7 | 356 | | out exitParameter, |
| | 7 | 357 | | out originContained, |
| | 7 | 358 | | out maximumContainedStrict)) |
| | | 359 | | { |
| | 2 | 360 | | return false; |
| | | 361 | | } |
| | | 362 | | |
| | 5 | 363 | | return WideFiniteAxisIntersection.TryGetCapsuleInterval( |
| | 5 | 364 | | this, maxParameter, capsuleAxis, radius, radiusExpansion, |
| | 5 | 365 | | out entryParameter, out exitParameter, |
| | 5 | 366 | | out originContained, out maximumContainedStrict); |
| | | 367 | | } |
| | | 368 | | |
| | | 369 | | /// <summary> |
| | | 370 | | /// Gets the closed parameter interval where this ray overlaps a centered |
| | | 371 | | /// capsule, clipped to <c>[0, <paramref name="maxParameter"/>]</c>. |
| | | 372 | | /// </summary> |
| | | 373 | | public readonly bool TryGetCapsuleIntersectionInterval( |
| | | 374 | | Vector3d center, |
| | | 375 | | Vector3d axisDirection, |
| | | 376 | | Fixed64 axisLength, |
| | | 377 | | Fixed64 radius, |
| | | 378 | | Fixed64 maxParameter, |
| | | 379 | | out Fixed64 entryParameter, |
| | | 380 | | out Fixed64 exitParameter) => |
| | 3 | 381 | | TryGetCapsuleIntersectionInterval( |
| | 3 | 382 | | center, axisDirection, axisLength, radius, Fixed64.Zero, maxParameter, |
| | 3 | 383 | | out entryParameter, out exitParameter, out _, out _); |
| | | 384 | | |
| | | 385 | | /// <summary> |
| | | 386 | | /// Gets the closed parameter interval where this ray overlaps a centered, |
| | | 387 | | /// radially expanded capsule and reports exact endpoint containment. |
| | | 388 | | /// </summary> |
| | | 389 | | public readonly bool TryGetCapsuleIntersectionInterval( |
| | | 390 | | Vector3d center, |
| | | 391 | | Vector3d axisDirection, |
| | | 392 | | Fixed64 axisLength, |
| | | 393 | | Fixed64 radius, |
| | | 394 | | Fixed64 radiusExpansion, |
| | | 395 | | Fixed64 maxParameter, |
| | | 396 | | out Fixed64 entryParameter, |
| | | 397 | | out Fixed64 exitParameter, |
| | | 398 | | out bool originContained, |
| | | 399 | | out bool maximumContainedStrict) |
| | | 400 | | { |
| | 10 | 401 | | ValidateCenteredCapsule(axisDirection, axisLength, radius, radiusExpansion); |
| | 6 | 402 | | if (!ValidateMaximum( |
| | 6 | 403 | | maxParameter, |
| | 6 | 404 | | out entryParameter, |
| | 6 | 405 | | out exitParameter, |
| | 6 | 406 | | out originContained, |
| | 6 | 407 | | out maximumContainedStrict)) |
| | | 408 | | { |
| | 1 | 409 | | return false; |
| | | 410 | | } |
| | | 411 | | |
| | 5 | 412 | | return WideFiniteAxisIntersection.TryGetCapsuleInterval( |
| | 5 | 413 | | this, maxParameter, center, axisDirection, axisLength, radius, radiusExpansion, |
| | 5 | 414 | | out entryParameter, out exitParameter, |
| | 5 | 415 | | out originContained, out maximumContainedStrict); |
| | | 416 | | } |
| | | 417 | | |
| | | 418 | | /// <summary> |
| | | 419 | | /// Gets the closed parameter interval where this ray overlaps a finite |
| | | 420 | | /// cylinder, clipped to <c>[0, <paramref name="maxParameter"/>]</c>. |
| | | 421 | | /// </summary> |
| | | 422 | | public readonly bool TryGetFiniteCylinderIntersectionInterval( |
| | | 423 | | FixedSegment cylinderAxis, |
| | | 424 | | Fixed64 radius, |
| | | 425 | | Fixed64 maxParameter, |
| | | 426 | | out Fixed64 entryParameter, |
| | | 427 | | out Fixed64 exitParameter) => |
| | 1 | 428 | | TryGetFiniteCylinderIntersectionInterval( |
| | 1 | 429 | | cylinderAxis, radius, Fixed64.Zero, maxParameter, |
| | 1 | 430 | | out entryParameter, out exitParameter, out _, out _); |
| | | 431 | | |
| | | 432 | | /// <summary> |
| | | 433 | | /// Gets the closed parameter interval where this ray overlaps a radially |
| | | 434 | | /// expanded finite cylinder and reports exact endpoint containment. |
| | | 435 | | /// </summary> |
| | | 436 | | public readonly bool TryGetFiniteCylinderIntersectionInterval( |
| | | 437 | | FixedSegment cylinderAxis, |
| | | 438 | | Fixed64 radius, |
| | | 439 | | Fixed64 radiusExpansion, |
| | | 440 | | Fixed64 maxParameter, |
| | | 441 | | out Fixed64 entryParameter, |
| | | 442 | | out Fixed64 exitParameter, |
| | | 443 | | out bool originContained, |
| | | 444 | | out bool maximumContainedStrict) |
| | | 445 | | { |
| | 7 | 446 | | if (cylinderAxis.Start == cylinderAxis.End) |
| | 1 | 447 | | throw new ArgumentException("A finite cylinder axis must have nonzero length.", nameof(cylinderAxis)); |
| | 6 | 448 | | if (radius < Fixed64.Zero) |
| | 1 | 449 | | throw new ArgumentOutOfRangeException(nameof(radius)); |
| | 5 | 450 | | if (radiusExpansion < Fixed64.Zero) |
| | 1 | 451 | | throw new ArgumentOutOfRangeException(nameof(radiusExpansion)); |
| | 4 | 452 | | if (!ValidateMaximum( |
| | 4 | 453 | | maxParameter, |
| | 4 | 454 | | out entryParameter, |
| | 4 | 455 | | out exitParameter, |
| | 4 | 456 | | out originContained, |
| | 4 | 457 | | out maximumContainedStrict)) |
| | | 458 | | { |
| | 1 | 459 | | return false; |
| | | 460 | | } |
| | | 461 | | |
| | 3 | 462 | | return WideFiniteAxisIntersection.TryGetFiniteCylinderInterval( |
| | 3 | 463 | | this, maxParameter, cylinderAxis, radius, radiusExpansion, |
| | 3 | 464 | | out entryParameter, out exitParameter, |
| | 3 | 465 | | out originContained, out maximumContainedStrict); |
| | | 466 | | } |
| | | 467 | | |
| | | 468 | | /// <summary> |
| | | 469 | | /// Gets the closed parameter interval where this ray overlaps a centered, |
| | | 470 | | /// affinely expanded finite cylinder. |
| | | 471 | | /// </summary> |
| | | 472 | | public readonly bool TryGetFiniteCylinderIntersectionInterval( |
| | | 473 | | Vector3d center, |
| | | 474 | | Vector3d axisDirection, |
| | | 475 | | Fixed64 axisLength, |
| | | 476 | | Fixed64 radius, |
| | | 477 | | Fixed64 radiusExpansion, |
| | | 478 | | Fixed64 axialExpansion, |
| | | 479 | | Fixed64 maxParameter, |
| | | 480 | | out Fixed64 entryParameter, |
| | | 481 | | out Fixed64 exitParameter) => |
| | 2 | 482 | | TryGetFiniteCylinderIntersectionInterval( |
| | 2 | 483 | | center, axisDirection, axisLength, |
| | 2 | 484 | | radius, radiusExpansion, axialExpansion, maxParameter, |
| | 2 | 485 | | out entryParameter, out exitParameter, out _, out _); |
| | | 486 | | |
| | | 487 | | /// <summary> |
| | | 488 | | /// Gets the closed parameter interval where this ray overlaps a centered, |
| | | 489 | | /// affinely expanded finite cylinder and reports exact endpoint containment. |
| | | 490 | | /// </summary> |
| | | 491 | | public readonly bool TryGetFiniteCylinderIntersectionInterval( |
| | | 492 | | Vector3d center, |
| | | 493 | | Vector3d axisDirection, |
| | | 494 | | Fixed64 axisLength, |
| | | 495 | | Fixed64 radius, |
| | | 496 | | Fixed64 radiusExpansion, |
| | | 497 | | Fixed64 axialExpansion, |
| | | 498 | | Fixed64 maxParameter, |
| | | 499 | | out Fixed64 entryParameter, |
| | | 500 | | out Fixed64 exitParameter, |
| | | 501 | | out bool originContained, |
| | | 502 | | out bool maximumContainedStrict) |
| | | 503 | | { |
| | 11 | 504 | | if (!axisDirection.IsNormalized()) |
| | 1 | 505 | | throw new ArgumentException("Finite cylinder axis direction must be normalized.", nameof(axisDirection)); |
| | 10 | 506 | | if (axisLength <= Fixed64.Zero) |
| | 1 | 507 | | throw new ArgumentOutOfRangeException(nameof(axisLength)); |
| | 9 | 508 | | ValidateCylinderExpansions(radius, radiusExpansion, axialExpansion); |
| | 6 | 509 | | if (!ValidateMaximum( |
| | 6 | 510 | | maxParameter, |
| | 6 | 511 | | out entryParameter, |
| | 6 | 512 | | out exitParameter, |
| | 6 | 513 | | out originContained, |
| | 6 | 514 | | out maximumContainedStrict)) |
| | | 515 | | { |
| | 1 | 516 | | return false; |
| | | 517 | | } |
| | | 518 | | |
| | 5 | 519 | | return WideFiniteAxisIntersection.TryGetFiniteCylinderInterval( |
| | 5 | 520 | | this, maxParameter, center, axisDirection, axisLength, |
| | 5 | 521 | | radius, radiusExpansion, axialExpansion, |
| | 5 | 522 | | out entryParameter, out exitParameter, |
| | 5 | 523 | | out originContained, out maximumContainedStrict); |
| | | 524 | | } |
| | | 525 | | |
| | | 526 | | private static void ValidateCenteredCapsule( |
| | | 527 | | Vector3d axisDirection, |
| | | 528 | | Fixed64 axisLength, |
| | | 529 | | Fixed64 radius, |
| | | 530 | | Fixed64 radiusExpansion) |
| | | 531 | | { |
| | 10 | 532 | | if (!axisDirection.IsNormalized()) |
| | 1 | 533 | | throw new ArgumentException("Capsule axis direction must be normalized.", nameof(axisDirection)); |
| | 9 | 534 | | if (axisLength < Fixed64.Zero) |
| | 1 | 535 | | throw new ArgumentOutOfRangeException(nameof(axisLength)); |
| | 8 | 536 | | if (radius < Fixed64.Zero) |
| | 1 | 537 | | throw new ArgumentOutOfRangeException(nameof(radius)); |
| | 7 | 538 | | if (radiusExpansion < Fixed64.Zero) |
| | 1 | 539 | | throw new ArgumentOutOfRangeException(nameof(radiusExpansion)); |
| | 6 | 540 | | } |
| | | 541 | | |
| | | 542 | | private static void ValidateCylinderExpansions( |
| | | 543 | | Fixed64 radius, |
| | | 544 | | Fixed64 radiusExpansion, |
| | | 545 | | Fixed64 axialExpansion) |
| | | 546 | | { |
| | 9 | 547 | | if (radius < Fixed64.Zero) |
| | 1 | 548 | | throw new ArgumentOutOfRangeException(nameof(radius)); |
| | 8 | 549 | | if (radiusExpansion < Fixed64.Zero) |
| | 1 | 550 | | throw new ArgumentOutOfRangeException(nameof(radiusExpansion)); |
| | 7 | 551 | | if (axialExpansion < Fixed64.Zero) |
| | 1 | 552 | | throw new ArgumentOutOfRangeException(nameof(axialExpansion)); |
| | 6 | 553 | | } |
| | | 554 | | |
| | | 555 | | private static bool ValidateMaximum( |
| | | 556 | | Fixed64 maxParameter, |
| | | 557 | | out Fixed64 entryParameter, |
| | | 558 | | out Fixed64 exitParameter, |
| | | 559 | | out bool originContained, |
| | | 560 | | out bool maximumContainedStrict) |
| | | 561 | | { |
| | 23 | 562 | | entryParameter = default; |
| | 23 | 563 | | exitParameter = default; |
| | 23 | 564 | | originContained = false; |
| | 23 | 565 | | maximumContainedStrict = false; |
| | 23 | 566 | | return maxParameter >= Fixed64.Zero; |
| | | 567 | | } |
| | | 568 | | } |