| | | 1 | | //======================================================================= |
| | | 2 | | // RaycastSegmentWorker.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using FixedMathSharp; |
| | | 9 | | using FixedMathSharp.Geometry; |
| | | 10 | | using Gravitas.Colliders; |
| | | 11 | | using SwiftCollections; |
| | | 12 | | using SwiftCollections.Query; |
| | | 13 | | |
| | | 14 | | namespace Gravitas.Queries; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Stores segment data used by one context-owned raycast service while checking collider overlaps. |
| | | 18 | | /// </summary> |
| | | 19 | | public sealed class RaycastSegmentWorker |
| | | 20 | | { |
| | | 21 | | private Vector3d _cachedOrigin; |
| | | 22 | | private Vector3d _cachedEnd; |
| | | 23 | | private Vector3d _cachedSegment; |
| | | 24 | | private Vector3d _segmentDirection; |
| | | 25 | | private Fixed64 _segmentLength; |
| | | 26 | | private bool _segmentIsValid; |
| | | 27 | | private bool _calculateIntersections; |
| | 10313 | 28 | | private readonly SwiftList<int> _meshTriangleBuffer = new(); |
| | | 29 | | |
| | 1 | 30 | | internal Vector3d SegmentDirection => _segmentDirection; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Prepares this worker for overlap checks against the line segment between two points. |
| | | 34 | | /// </summary> |
| | | 35 | | public void PrepareSegmentCheck(Vector3d p1, Vector3d p2, bool calculateIntersectionPoints = true) |
| | | 36 | | { |
| | 3938 | 37 | | _cachedOrigin = p1; |
| | 3938 | 38 | | _cachedEnd = p2; |
| | | 39 | | |
| | 3938 | 40 | | if (!Vector3d.TrySubtract(p2, p1, out Vector3d segment) |
| | 3938 | 41 | | || !Vector3d.TryGetMagnitude(segment, out _segmentLength)) |
| | | 42 | | { |
| | 2 | 43 | | _segmentLength = Fixed64.Zero; |
| | 2 | 44 | | _cachedSegment = Vector3d.Zero; |
| | 2 | 45 | | _segmentDirection = Vector3d.Zero; |
| | 2 | 46 | | _segmentIsValid = false; |
| | 2 | 47 | | _calculateIntersections = calculateIntersectionPoints; |
| | 2 | 48 | | return; |
| | | 49 | | } |
| | | 50 | | |
| | 3936 | 51 | | _cachedSegment = segment; |
| | 3936 | 52 | | _segmentDirection = _segmentLength == Fixed64.Zero ? Vector3d.Zero : segment.Normalized; |
| | 3936 | 53 | | _segmentIsValid = true; |
| | 3936 | 54 | | _calculateIntersections = calculateIntersectionPoints; |
| | 3936 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Checks whether a sphere collider overlaps the prepared segment. |
| | | 59 | | /// </summary> |
| | | 60 | | public bool CheckSphereOverlaps(LSSphereCollider sphereCollider, ref SwiftList<Vector3d> outputIntersectionPoints) = |
| | 1386 | 61 | | CheckSphereOverlaps( |
| | 1386 | 62 | | new FixedBoundSphere(sphereCollider.Center, sphereCollider.ScaledRadius), |
| | 1386 | 63 | | ref outputIntersectionPoints); |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Checks whether a sphere overlaps this worker's prepared ray segment. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <param name="sphere">The sphere bound.</param> |
| | | 69 | | /// <param name="outputIntersectionPoints"> |
| | | 70 | | /// Receives segment points reconstructed from exact physical intersection distances. |
| | | 71 | | /// </param> |
| | | 72 | | /// <returns><see langword="true"/> when the prepared segment overlaps the sphere.</returns> |
| | | 73 | | public bool CheckSphereOverlaps( |
| | | 74 | | FixedBoundSphere sphere, |
| | | 75 | | ref SwiftList<Vector3d> outputIntersectionPoints) |
| | | 76 | | { |
| | 1399 | 77 | | if (!_segmentIsValid) |
| | 2 | 78 | | return false; |
| | | 79 | | |
| | 1397 | 80 | | if (_cachedSegment.IsZero) |
| | 3 | 81 | | return CheckPointInsideSphere(sphere, ref outputIntersectionPoints); |
| | | 82 | | |
| | 1394 | 83 | | var query = new FixedSegment(_cachedOrigin, _cachedEnd); |
| | 1394 | 84 | | if (!query.TryGetSphereIntersectionDistanceInterval( |
| | 1394 | 85 | | sphere, |
| | 1394 | 86 | | Fixed64.Zero, |
| | 1394 | 87 | | _segmentLength, |
| | 1394 | 88 | | out Fixed64 entry, |
| | 1394 | 89 | | out Fixed64 exit, |
| | 1394 | 90 | | out bool startContained, |
| | 1394 | 91 | | out bool endContainedStrict)) |
| | | 92 | | { |
| | 23 | 93 | | return false; |
| | | 94 | | } |
| | | 95 | | |
| | 1371 | 96 | | if (!_calculateIntersections) |
| | 1 | 97 | | return true; |
| | | 98 | | |
| | 1370 | 99 | | AddFiniteAxisIntersectionInterval( |
| | 1370 | 100 | | entry, |
| | 1370 | 101 | | exit, |
| | 1370 | 102 | | startContained, |
| | 1370 | 103 | | endContainedStrict, |
| | 1370 | 104 | | ref outputIntersectionPoints); |
| | 1370 | 105 | | return true; |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Checks whether a capsule collider overlaps the prepared segment. |
| | | 110 | | /// </summary> |
| | | 111 | | public bool CheckCapsuleOverlaps(LSCapsuleCollider capsuleCollider, ref SwiftList<Vector3d> outputIntersectionPoints |
| | | 112 | | { |
| | 448 | 113 | | if (!_segmentIsValid) |
| | 1 | 114 | | return false; |
| | | 115 | | |
| | 447 | 116 | | var query = new FixedSegment(_cachedOrigin, _cachedEnd); |
| | 447 | 117 | | if (!query.TryGetCapsuleIntersectionDistanceInterval( |
| | 447 | 118 | | capsuleCollider.Center, |
| | 447 | 119 | | capsuleCollider.Rotation, |
| | 447 | 120 | | capsuleCollider.AxisLength, |
| | 447 | 121 | | capsuleCollider.ScaledRadius, |
| | 447 | 122 | | Fixed64.Zero, |
| | 447 | 123 | | _segmentLength, |
| | 447 | 124 | | out Fixed64 entry, |
| | 447 | 125 | | out Fixed64 exit, |
| | 447 | 126 | | out bool startContained, |
| | 447 | 127 | | out bool endContainedStrict)) |
| | | 128 | | { |
| | 4 | 129 | | return false; |
| | | 130 | | } |
| | | 131 | | |
| | 443 | 132 | | if (!_calculateIntersections) |
| | 1 | 133 | | return true; |
| | | 134 | | |
| | 442 | 135 | | AddFiniteAxisIntersectionInterval( |
| | 442 | 136 | | entry, |
| | 442 | 137 | | exit, |
| | 442 | 138 | | startContained, |
| | 442 | 139 | | endContainedStrict, |
| | 442 | 140 | | ref outputIntersectionPoints); |
| | 442 | 141 | | return true; |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Checks whether a cylinder collider overlaps the prepared segment. |
| | | 146 | | /// </summary> |
| | | 147 | | public bool CheckCylinderOverlaps(LSCylinderCollider cylinderCollider, ref SwiftList<Vector3d> outputIntersectionPoi |
| | | 148 | | { |
| | 30 | 149 | | if (!_segmentIsValid) |
| | 1 | 150 | | return false; |
| | | 151 | | |
| | 29 | 152 | | var query = new FixedSegment(_cachedOrigin, _cachedEnd); |
| | 29 | 153 | | if (!query.TryGetFiniteCylinderIntersectionDistanceInterval( |
| | 29 | 154 | | cylinderCollider.Center, |
| | 29 | 155 | | cylinderCollider.Rotation, |
| | 29 | 156 | | cylinderCollider.Height, |
| | 29 | 157 | | cylinderCollider.ScaledRadius, |
| | 29 | 158 | | Fixed64.Zero, |
| | 29 | 159 | | Fixed64.Zero, |
| | 29 | 160 | | _segmentLength, |
| | 29 | 161 | | out Fixed64 entry, |
| | 29 | 162 | | out Fixed64 exit, |
| | 29 | 163 | | out bool startContained, |
| | 29 | 164 | | out bool endContainedStrict)) |
| | | 165 | | { |
| | 7 | 166 | | return false; |
| | | 167 | | } |
| | | 168 | | |
| | 22 | 169 | | if (!_calculateIntersections) |
| | 1 | 170 | | return true; |
| | | 171 | | |
| | 21 | 172 | | AddFiniteAxisIntersectionInterval( |
| | 21 | 173 | | entry, |
| | 21 | 174 | | exit, |
| | 21 | 175 | | startContained, |
| | 21 | 176 | | endContainedStrict, |
| | 21 | 177 | | ref outputIntersectionPoints); |
| | 21 | 178 | | return true; |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | /// <summary> |
| | | 182 | | /// Checks whether a cone collider overlaps the prepared segment. |
| | | 183 | | /// </summary> |
| | | 184 | | public bool CheckConeOverlaps(LSConeCollider coneCollider, ref SwiftList<Vector3d> outputIntersectionPoints) |
| | | 185 | | { |
| | 851 | 186 | | if (!_segmentIsValid) |
| | 1 | 187 | | return false; |
| | | 188 | | |
| | 850 | 189 | | var query = new FixedSegment(_cachedOrigin, _cachedEnd); |
| | 850 | 190 | | if (!query.TryGetCenteredFiniteConeIntersectionDistanceInterval( |
| | 850 | 191 | | coneCollider.Center, |
| | 850 | 192 | | coneCollider.Rotation, |
| | 850 | 193 | | coneCollider.Height, |
| | 850 | 194 | | coneCollider.ScaledRadius, |
| | 850 | 195 | | _segmentLength, |
| | 850 | 196 | | out Fixed64 entry, |
| | 850 | 197 | | out Fixed64 exit, |
| | 850 | 198 | | out bool startContained, |
| | 850 | 199 | | out bool endContainedStrict)) |
| | | 200 | | { |
| | 11 | 201 | | return false; |
| | | 202 | | } |
| | | 203 | | |
| | 839 | 204 | | if (!_calculateIntersections) |
| | 3 | 205 | | return true; |
| | | 206 | | |
| | 836 | 207 | | AddFiniteAxisIntersectionInterval( |
| | 836 | 208 | | entry, |
| | 836 | 209 | | exit, |
| | 836 | 210 | | startContained, |
| | 836 | 211 | | endContainedStrict, |
| | 836 | 212 | | ref outputIntersectionPoints); |
| | | 213 | | |
| | 836 | 214 | | return true; |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | /// <summary> |
| | | 218 | | /// Checks whether a mesh collider overlaps the prepared segment. |
| | | 219 | | /// </summary> |
| | | 220 | | public bool CheckMeshOverlaps(LSMeshCollider meshCollider, ref SwiftList<Vector3d> outputIntersectionPoints) |
| | | 221 | | { |
| | 1371 | 222 | | if (!_segmentIsValid) |
| | 1 | 223 | | return false; |
| | | 224 | | |
| | 1370 | 225 | | if (!meshCollider.Mesh.GeometryFrame.IsRigid) |
| | | 226 | | { |
| | 276 | 227 | | PhysicsMesh mesh = meshCollider.Mesh; |
| | 276 | 228 | | mesh.GetTrianglesInWorldBounds(CreateSegmentBounds(_cachedOrigin, _cachedEnd), _meshTriangleBuffer); |
| | 276 | 229 | | bool found = false; |
| | 1053 | 230 | | foreach (int index in _meshTriangleBuffer.AsReadOnlySpan()) |
| | | 231 | | { |
| | 252 | 232 | | mesh.GetGeometryTriangleVertices(index, out Vector3d a, out Vector3d b, out Vector3d c); |
| | 252 | 233 | | if (!WideTriangleRelations.TryGetSegmentIntersection(new FixedTriangle(a, b, c), mesh.GeometryFrame, |
| | 252 | 234 | | new FixedSegment(_cachedOrigin, _cachedEnd), out Vector3d entry, out Vector3d exit)) continue; |
| | 235 | 235 | | if (!_calculateIntersections) return true; |
| | 229 | 236 | | AddIntersectionPoint(entry, ref outputIntersectionPoints); |
| | 237 | 237 | | if (exit != entry) AddIntersectionPoint(exit, ref outputIntersectionPoints); |
| | 229 | 238 | | found = true; |
| | | 239 | | } |
| | 273 | 240 | | return found; |
| | | 241 | | } |
| | | 242 | | |
| | 1094 | 243 | | if (!meshCollider.Mesh.TryConvertWorldToRigidGeometry( |
| | 1094 | 244 | | _cachedOrigin, |
| | 1094 | 245 | | out Vector3d localOrigin) |
| | 1094 | 246 | | || !meshCollider.Mesh.TryConvertWorldToRigidGeometry( |
| | 1094 | 247 | | _cachedEnd, |
| | 1094 | 248 | | out Vector3d localEnd)) |
| | | 249 | | { |
| | 2 | 250 | | return false; |
| | | 251 | | } |
| | 1092 | 252 | | Vector3d localSegment = localEnd - localOrigin; |
| | 1092 | 253 | | Fixed64 localSegmentLengthSqr = localSegment.MagnitudeSquared; |
| | 1092 | 254 | | Fixed64 localSegmentLength = localSegmentLengthSqr == Fixed64.Zero ? Fixed64.Zero : localSegment.Magnitude; |
| | 1092 | 255 | | Vector3d localSegmentDirection = localSegmentLength == Fixed64.Zero ? Vector3d.Zero : localSegment.Normalized; |
| | | 256 | | |
| | 1092 | 257 | | _meshTriangleBuffer.FastClear(); |
| | 1092 | 258 | | meshCollider.Mesh.GetTrianglesInGeometryBounds(CreateSegmentBounds(localOrigin, localEnd), _meshTriangleBuffer); |
| | 1092 | 259 | | bool intersects = false; |
| | 5280 | 260 | | for (int i = 0; i < _meshTriangleBuffer.Count; i++) |
| | | 261 | | { |
| | 1552 | 262 | | int triangleIndex = _meshTriangleBuffer[i]; |
| | 1552 | 263 | | meshCollider.Mesh.GetGeometryTriangleVertices(triangleIndex, out Vector3d first, out Vector3d second, out Ve |
| | 1552 | 264 | | if (!TryAddLocalTriangleIntersection( |
| | 1552 | 265 | | meshCollider.Mesh, |
| | 1552 | 266 | | first, |
| | 1552 | 267 | | second, |
| | 1552 | 268 | | third, |
| | 1552 | 269 | | meshCollider.Mesh.GetScaledLocalFaceNormal(triangleIndex), |
| | 1552 | 270 | | localOrigin, |
| | 1552 | 271 | | localEnd, |
| | 1552 | 272 | | localSegmentDirection, |
| | 1552 | 273 | | localSegmentLength, |
| | 1552 | 274 | | localSegmentLengthSqr, |
| | 1552 | 275 | | ref outputIntersectionPoints)) |
| | | 276 | | { |
| | | 277 | | continue; |
| | | 278 | | } |
| | | 279 | | |
| | 1249 | 280 | | intersects = true; |
| | 1249 | 281 | | if (!_calculateIntersections) |
| | 4 | 282 | | return true; |
| | | 283 | | } |
| | | 284 | | |
| | 1088 | 285 | | return intersects; |
| | | 286 | | } |
| | | 287 | | |
| | | 288 | | /// <summary> |
| | | 289 | | /// Checks whether a cuboid's axis-aligned bounds overlap the prepared segment. |
| | | 290 | | /// </summary> |
| | | 291 | | public bool CheckAABBoxOverlaps(LSCuboidCollider aabox, ref SwiftList<Vector3d> outputIntersectionPoints) => |
| | 439 | 292 | | CheckAABBoxOverlaps(aabox.BoundsMin, aabox.BoundsMax, ref outputIntersectionPoints); |
| | | 293 | | |
| | | 294 | | /// <summary> |
| | | 295 | | /// Checks whether an axis-aligned bounding box overlaps this worker's prepared ray segment. |
| | | 296 | | /// </summary> |
| | | 297 | | public bool CheckAABBoxOverlaps(Vector3d min, Vector3d max, ref SwiftList<Vector3d> outputIntersectionPoints) |
| | | 298 | | { |
| | 457 | 299 | | if (!_segmentIsValid) |
| | 1 | 300 | | return false; |
| | | 301 | | |
| | 456 | 302 | | if (_cachedSegment.IsZero) |
| | 11 | 303 | | return CheckPointInsideBox(min, max, ref outputIntersectionPoints); |
| | | 304 | | |
| | 445 | 305 | | if (!SweepBoundsUtility.TryClipSegment( |
| | 445 | 306 | | _cachedOrigin, |
| | 445 | 307 | | _segmentDirection, |
| | 445 | 308 | | _segmentLength, |
| | 445 | 309 | | min, |
| | 445 | 310 | | max, |
| | 445 | 311 | | out Fixed64 entry, |
| | 445 | 312 | | out Fixed64 exit)) |
| | | 313 | | { |
| | 17 | 314 | | return false; |
| | | 315 | | } |
| | | 316 | | |
| | 428 | 317 | | if (_calculateIntersections) |
| | | 318 | | { |
| | 427 | 319 | | outputIntersectionPoints.Add(_cachedOrigin + _segmentDirection * entry); |
| | 427 | 320 | | if (exit != entry) |
| | 422 | 321 | | outputIntersectionPoints.Add(_cachedOrigin + _segmentDirection * exit); |
| | | 322 | | } |
| | | 323 | | |
| | 428 | 324 | | return true; |
| | | 325 | | } |
| | | 326 | | |
| | | 327 | | /// <summary> |
| | | 328 | | /// Checks whether an oriented bounding box overlaps this worker's prepared ray segment. |
| | | 329 | | /// </summary> |
| | | 330 | | public bool CheckOBBoxOverlaps(LSCuboidCollider oobox, ref SwiftList<Vector3d> outputIntersectionPoints) |
| | | 331 | | { |
| | 245 | 332 | | if (!_segmentIsValid) |
| | 1 | 333 | | return false; |
| | | 334 | | |
| | 244 | 335 | | var ray = new FixedRay(_cachedOrigin, _cachedSegment); |
| | 244 | 336 | | if (!oobox.OrientedBox.TryGetRayIntersectionInterval( |
| | 244 | 337 | | ray, |
| | 244 | 338 | | Fixed64.One, |
| | 244 | 339 | | out Fixed64 entry, |
| | 244 | 340 | | out Fixed64 exit)) |
| | 18 | 341 | | return false; |
| | | 342 | | |
| | 226 | 343 | | if (_calculateIntersections) |
| | | 344 | | { |
| | | 345 | | // The interval is clipped to this segment, so both parameters are |
| | | 346 | | // convex combinations of its representable endpoints. |
| | 225 | 347 | | _ = ray.TryGetPoint(entry, out Vector3d entryPoint); |
| | 225 | 348 | | Vector3d exitPoint = default; |
| | 225 | 349 | | if (exit != entry) |
| | 16 | 350 | | _ = ray.TryGetPoint(exit, out exitPoint); |
| | | 351 | | |
| | 225 | 352 | | AddIntersectionPoint(entryPoint, ref outputIntersectionPoints); |
| | 225 | 353 | | if (exit != entry) |
| | 16 | 354 | | AddIntersectionPoint(exitPoint, ref outputIntersectionPoints); |
| | | 355 | | } |
| | | 356 | | |
| | 226 | 357 | | return true; |
| | | 358 | | } |
| | | 359 | | |
| | | 360 | | private bool TryAddLocalTriangleIntersection( |
| | | 361 | | PhysicsMesh mesh, |
| | | 362 | | Vector3d first, |
| | | 363 | | Vector3d second, |
| | | 364 | | Vector3d third, |
| | | 365 | | Vector3d normal, |
| | | 366 | | Vector3d localOrigin, |
| | | 367 | | Vector3d localEnd, |
| | | 368 | | Vector3d localSegmentDirection, |
| | | 369 | | Fixed64 localSegmentLength, |
| | | 370 | | Fixed64 localSegmentLengthSqr, |
| | | 371 | | ref SwiftList<Vector3d> outputIntersectionPoints) |
| | | 372 | | { |
| | 1552 | 373 | | var triangle = new FixedTriangle(first, second, third); |
| | 1552 | 374 | | if (localSegmentLengthSqr == Fixed64.Zero) |
| | | 375 | | { |
| | 8 | 376 | | if (Vector3d.Dot(localOrigin - first, normal).Abs() > Fixed64.Epsilon |
| | 8 | 377 | | || !triangle.ContainsProjection(localOrigin)) |
| | | 378 | | { |
| | 5 | 379 | | return false; |
| | | 380 | | } |
| | | 381 | | |
| | | 382 | | // localOrigin was obtained from this representable world endpoint. |
| | 3 | 383 | | _ = mesh.GeometryFrame.TryGetPoint(localOrigin, out Vector3d worldOrigin); |
| | | 384 | | |
| | 3 | 385 | | AddIntersectionPoint(worldOrigin, ref outputIntersectionPoints); |
| | 3 | 386 | | return true; |
| | | 387 | | } |
| | | 388 | | |
| | 1544 | 389 | | Fixed64 denominator = Vector3d.Dot(normal, localSegmentDirection); |
| | 1544 | 390 | | if (denominator.Abs() <= Fixed64.Epsilon) |
| | | 391 | | { |
| | 377 | 392 | | if (Vector3d.Dot(localOrigin - first, normal).Abs() > Fixed64.Epsilon) |
| | 42 | 393 | | return false; |
| | | 394 | | |
| | 335 | 395 | | bool found = false; |
| | 335 | 396 | | if (triangle.ContainsProjection(localOrigin)) |
| | | 397 | | { |
| | 160 | 398 | | _ = mesh.GeometryFrame.TryGetPoint(localOrigin, out Vector3d worldOrigin); |
| | | 399 | | |
| | 160 | 400 | | AddIntersectionPoint(worldOrigin, ref outputIntersectionPoints); |
| | 160 | 401 | | found = true; |
| | | 402 | | } |
| | | 403 | | |
| | 335 | 404 | | if (triangle.ContainsProjection(localEnd)) |
| | | 405 | | { |
| | 228 | 406 | | _ = mesh.GeometryFrame.TryGetPoint(localEnd, out Vector3d worldEnd); |
| | | 407 | | |
| | 228 | 408 | | AddIntersectionPoint(worldEnd, ref outputIntersectionPoints); |
| | 228 | 409 | | found = true; |
| | | 410 | | } |
| | | 411 | | |
| | 335 | 412 | | return found; |
| | | 413 | | } |
| | | 414 | | |
| | 1167 | 415 | | Fixed64 distance = Vector3d.Dot(first - localOrigin, normal) / denominator; |
| | 1167 | 416 | | if (distance < Fixed64.Zero || distance > localSegmentLength) |
| | 26 | 417 | | return false; |
| | | 418 | | |
| | 1141 | 419 | | Vector3d localPoint = localOrigin + localSegmentDirection * distance; |
| | 1141 | 420 | | if (!triangle.ContainsProjection(localPoint)) |
| | 143 | 421 | | return false; |
| | | 422 | | |
| | | 423 | | // A point on the local segment maps to the same convex combination of |
| | | 424 | | // the already-representable world endpoints. |
| | 998 | 425 | | _ = mesh.GeometryFrame.TryGetPoint(localPoint, out Vector3d worldPoint); |
| | | 426 | | |
| | 998 | 427 | | AddIntersectionPoint(worldPoint, ref outputIntersectionPoints); |
| | 998 | 428 | | return true; |
| | | 429 | | } |
| | | 430 | | |
| | | 431 | | private void AddIntersectionPoint(Vector3d point, ref SwiftList<Vector3d> outputIntersectionPoints) |
| | | 432 | | { |
| | 1867 | 433 | | if (!_calculateIntersections) |
| | 5 | 434 | | return; |
| | | 435 | | |
| | 4174 | 436 | | for (int i = 0; i < outputIntersectionPoints.Count; i++) |
| | | 437 | | { |
| | 845 | 438 | | if (Vector3d.DistanceSquared(outputIntersectionPoints[i], point) <= Fixed64.Epsilon) |
| | 620 | 439 | | return; |
| | | 440 | | } |
| | | 441 | | |
| | 1242 | 442 | | outputIntersectionPoints.Add(point); |
| | 1242 | 443 | | } |
| | | 444 | | |
| | | 445 | | private static FixedBoundVolume CreateSegmentBounds(Vector3d origin, Vector3d end) |
| | | 446 | | { |
| | 1368 | 447 | | Vector3d min = Vector3d.Min(origin, end); |
| | 1368 | 448 | | Vector3d max = Vector3d.Max(origin, end); |
| | 1368 | 449 | | return new FixedBoundVolume(min, max); |
| | | 450 | | } |
| | | 451 | | |
| | | 452 | | private bool CheckPointInsideSphere( |
| | | 453 | | FixedBoundSphere sphere, |
| | | 454 | | ref SwiftList<Vector3d> outputIntersectionPoints) |
| | | 455 | | { |
| | 3 | 456 | | if (!sphere.Contains(_cachedOrigin)) |
| | 1 | 457 | | return false; |
| | | 458 | | |
| | 2 | 459 | | if (_calculateIntersections) |
| | 1 | 460 | | outputIntersectionPoints.Add(_cachedOrigin); |
| | | 461 | | |
| | 2 | 462 | | return true; |
| | | 463 | | } |
| | | 464 | | |
| | | 465 | | private void AddFiniteAxisIntersectionInterval( |
| | | 466 | | Fixed64 entry, |
| | | 467 | | Fixed64 exit, |
| | | 468 | | bool startContained, |
| | | 469 | | bool endInsideStrict, |
| | | 470 | | ref SwiftList<Vector3d> outputIntersectionPoints) |
| | | 471 | | { |
| | 2669 | 472 | | AddSegmentIntersectionPoint(entry, ref outputIntersectionPoints); |
| | 2669 | 473 | | if (!startContained |
| | 2669 | 474 | | && exit != entry |
| | 2669 | 475 | | && (exit < _segmentLength || !endInsideStrict)) |
| | | 476 | | { |
| | 2011 | 477 | | AddSegmentIntersectionPoint(exit, ref outputIntersectionPoints); |
| | | 478 | | } |
| | | 479 | | |
| | 2669 | 480 | | } |
| | | 481 | | |
| | | 482 | | private void AddSegmentIntersectionPoint( |
| | | 483 | | Fixed64 distance, |
| | | 484 | | ref SwiftList<Vector3d> outputIntersectionPoints) => |
| | 4680 | 485 | | outputIntersectionPoints.Add(new FixedSegment(_cachedOrigin, _cachedEnd) |
| | 4680 | 486 | | .GetPointAtDistance(distance, _segmentLength)); |
| | | 487 | | |
| | | 488 | | private bool CheckPointInsideBox( |
| | | 489 | | Vector3d min, |
| | | 490 | | Vector3d max, |
| | | 491 | | ref SwiftList<Vector3d> outputIntersectionPoints) |
| | | 492 | | { |
| | 11 | 493 | | if (_cachedOrigin.X < min.X || _cachedOrigin.X > max.X |
| | 11 | 494 | | || _cachedOrigin.Y < min.Y || _cachedOrigin.Y > max.Y |
| | 11 | 495 | | || _cachedOrigin.Z < min.Z || _cachedOrigin.Z > max.Z) |
| | | 496 | | { |
| | 6 | 497 | | return false; |
| | | 498 | | } |
| | | 499 | | |
| | 5 | 500 | | if (_calculateIntersections) |
| | 4 | 501 | | outputIntersectionPoints.Add(_cachedOrigin); |
| | | 502 | | |
| | 5 | 503 | | return true; |
| | | 504 | | } |
| | | 505 | | |
| | | 506 | | } |