RayTriangle8.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Math/Vec8.h>
  5. JPH_NAMESPACE_BEGIN
  6. /// Intersect ray with 8 triangles in SOA format, returns 8 vector of closest points or FLT_MAX if no hit
  7. JPH_INLINE Vec8 RayTriangle8(Vec3Arg inOrigin, Vec3Arg inDirection, Vec8Arg inV0X, Vec8Arg inV0Y, Vec8Arg inV0Z, Vec8Arg inV1X, Vec8Arg inV1Y, Vec8Arg inV1Z, Vec8Arg inV2X, Vec8Arg inV2Y, Vec8Arg inV2Z)
  8. {
  9. // Epsilon
  10. Vec8 epsilon = Vec8::sReplicate(1.0e-12f);
  11. // Zero & one
  12. Vec8 zero = Vec8::sZero();
  13. Vec8 one = Vec8::sReplicate(1.0f);
  14. // Find vectors for two edges sharing inV0
  15. Vec8 e1x = inV1X - inV0X;
  16. Vec8 e1y = inV1Y - inV0Y;
  17. Vec8 e1z = inV1Z - inV0Z;
  18. Vec8 e2x = inV2X - inV0X;
  19. Vec8 e2y = inV2Y - inV0Y;
  20. Vec8 e2z = inV2Z - inV0Z;
  21. // Get direction vector components
  22. Vec8 dx = Vec8::sSplatX(Vec4(inDirection));
  23. Vec8 dy = Vec8::sSplatY(Vec4(inDirection));
  24. Vec8 dz = Vec8::sSplatZ(Vec4(inDirection));
  25. // Begin calculating determinant - also used to calculate u parameter
  26. Vec8 px = dy * e2z - dz * e2y;
  27. Vec8 py = dz * e2x - dx * e2z;
  28. Vec8 pz = dx * e2y - dy * e2x;
  29. // if determinant is near zero, ray lies in plane of triangle
  30. Vec8 det = e1x * px + e1y * py + e1z * pz;
  31. // Check which determinants are near zero
  32. UVec8 det_near_zero = Vec8::sLess(det.Abs(), epsilon);
  33. // Set components of the determinant to 1 that are near zero to avoid dividing by zero
  34. det = Vec8::sSelect(det, Vec8::sReplicate(1.0f), det_near_zero);
  35. // Calculate distance from inV0 to ray origin
  36. Vec8 sx = Vec8::sSplatX(Vec4(inOrigin)) - inV0X;
  37. Vec8 sy = Vec8::sSplatY(Vec4(inOrigin)) - inV0Y;
  38. Vec8 sz = Vec8::sSplatZ(Vec4(inOrigin)) - inV0Z;
  39. // Calculate u parameter and flip sign if determinant was negative
  40. Vec8 u = (sx * px + sy * py + sz * pz) / det;
  41. // Prepare to test v parameter
  42. Vec8 qx = sy * e1z - sz * e1y;
  43. Vec8 qy = sz * e1x - sx * e1z;
  44. Vec8 qz = sx * e1y - sy * e1x;
  45. // Calculate v parameter and flip sign if determinant was negative
  46. Vec8 v = (dx * qx + dy * qy + dz * qz) / det;
  47. // Get intersection point and flip sign if determinant was negative
  48. Vec8 t = (e2x * qx + e2y * qy + e2z * qz) / det;
  49. // Check if there is an intersection
  50. UVec8 no_intersection =
  51. UVec8::sOr
  52. (
  53. UVec8::sOr
  54. (
  55. UVec8::sOr
  56. (
  57. det_near_zero,
  58. Vec8::sLess(u, zero)
  59. ),
  60. UVec8::sOr
  61. (
  62. Vec8::sLess(v, zero),
  63. Vec8::sGreater(u + v, one)
  64. )
  65. ),
  66. Vec8::sLess(t, zero)
  67. );
  68. // Select intersection point or FLT_MAX based on if there is an intersection or not
  69. return Vec8::sSelect(t, Vec8::sReplicate(FLT_MAX), no_intersection);
  70. }
  71. JPH_NAMESPACE_END