2
0

RayTriangle.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. JPH_NAMESPACE_BEGIN
  5. /// Intersect ray with triangle, returns closest point or FLT_MAX if no hit (branch less version)
  6. /// Adapted from: http://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
  7. JPH_INLINE float RayTriangle(Vec3Arg inOrigin, Vec3Arg inDirection, Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2)
  8. {
  9. // Epsilon
  10. Vec3 epsilon = Vec3::sReplicate(1.0e-12f);
  11. // Zero & one
  12. Vec3 zero = Vec3::sZero();
  13. Vec3 one = Vec3::sReplicate(1.0f);
  14. // Find vectors for two edges sharing inV0
  15. Vec3 e1 = inV1 - inV0;
  16. Vec3 e2 = inV2 - inV0;
  17. // Begin calculating determinant - also used to calculate u parameter
  18. Vec3 p = inDirection.Cross(e2);
  19. // if determinant is near zero, ray lies in plane of triangle
  20. Vec3 det = Vec3::sReplicate(e1.Dot(p));
  21. // Check if determinant is near zero
  22. UVec4 det_near_zero = Vec3::sLess(det.Abs(), epsilon);
  23. // When the determinant is near zero, set it to one to avoid dividing by zero
  24. det = Vec3::sSelect(det, Vec3::sReplicate(1.0f), det_near_zero);
  25. // Calculate distance from inV0 to ray origin
  26. Vec3 s = inOrigin - inV0;
  27. // Calculate u parameter
  28. Vec3 u = Vec3::sReplicate(s.Dot(p)) / det;
  29. // Prepare to test v parameter
  30. Vec3 q = s.Cross(e1);
  31. // Calculate v parameter
  32. Vec3 v = Vec3::sReplicate(inDirection.Dot(q)) / det;
  33. // Get intersection point
  34. Vec3 t = Vec3::sReplicate(e2.Dot(q)) / det;
  35. // Check if there is an intersection
  36. UVec4 no_intersection =
  37. UVec4::sOr
  38. (
  39. UVec4::sOr
  40. (
  41. UVec4::sOr
  42. (
  43. det_near_zero,
  44. Vec3::sLess(u, zero)
  45. ),
  46. UVec4::sOr
  47. (
  48. Vec3::sLess(v, zero),
  49. Vec3::sGreater(u + v, one)
  50. )
  51. ),
  52. Vec3::sLess(t, zero)
  53. );
  54. // Select intersection point or FLT_MAX based on if there is an intersection or not
  55. return Vec3::sSelect(t, Vec3::sReplicate(FLT_MAX), no_intersection).GetX();
  56. }
  57. /// Intersect ray with 4 triangles in SOA format, returns 4 vector of closest points or FLT_MAX if no hit (uses bit tricks to do less divisions)
  58. JPH_INLINE Vec4 RayTriangle4(Vec3Arg inOrigin, Vec3Arg inDirection, Vec4Arg inV0X, Vec4Arg inV0Y, Vec4Arg inV0Z, Vec4Arg inV1X, Vec4Arg inV1Y, Vec4Arg inV1Z, Vec4Arg inV2X, Vec4Arg inV2Y, Vec4Arg inV2Z)
  59. {
  60. // Epsilon
  61. Vec4 epsilon = Vec4::sReplicate(1.0e-12f);
  62. // Zero
  63. Vec4 zero = Vec4::sZero();
  64. // Find vectors for two edges sharing inV0
  65. Vec4 e1x = inV1X - inV0X;
  66. Vec4 e1y = inV1Y - inV0Y;
  67. Vec4 e1z = inV1Z - inV0Z;
  68. Vec4 e2x = inV2X - inV0X;
  69. Vec4 e2y = inV2Y - inV0Y;
  70. Vec4 e2z = inV2Z - inV0Z;
  71. // Get direction vector components
  72. Vec4 dx = inDirection.SplatX();
  73. Vec4 dy = inDirection.SplatY();
  74. Vec4 dz = inDirection.SplatZ();
  75. // Begin calculating determinant - also used to calculate u parameter
  76. Vec4 px = dy * e2z - dz * e2y;
  77. Vec4 py = dz * e2x - dx * e2z;
  78. Vec4 pz = dx * e2y - dy * e2x;
  79. // if determinant is near zero, ray lies in plane of triangle
  80. Vec4 det = e1x * px + e1y * py + e1z * pz;
  81. // Get sign bit for determinant and make positive
  82. Vec4 det_sign = Vec4::sAnd(det, UVec4::sReplicate(0x80000000).ReinterpretAsFloat());
  83. det = Vec4::sXor(det, det_sign);
  84. // Check which determinants are near zero
  85. UVec4 det_near_zero = Vec4::sLess(det, epsilon);
  86. // Set components of the determinant to 1 that are near zero to avoid dividing by zero
  87. det = Vec4::sSelect(det, Vec4::sReplicate(1.0f), det_near_zero);
  88. // Calculate distance from inV0 to ray origin
  89. Vec4 sx = inOrigin.SplatX() - inV0X;
  90. Vec4 sy = inOrigin.SplatY() - inV0Y;
  91. Vec4 sz = inOrigin.SplatZ() - inV0Z;
  92. // Calculate u parameter and flip sign if determinant was negative
  93. Vec4 u = Vec4::sXor(sx * px + sy * py + sz * pz, det_sign);
  94. // Prepare to test v parameter
  95. Vec4 qx = sy * e1z - sz * e1y;
  96. Vec4 qy = sz * e1x - sx * e1z;
  97. Vec4 qz = sx * e1y - sy * e1x;
  98. // Calculate v parameter and flip sign if determinant was negative
  99. Vec4 v = Vec4::sXor(dx * qx + dy * qy + dz * qz, det_sign);
  100. // Get intersection point and flip sign if determinant was negative
  101. Vec4 t = Vec4::sXor(e2x * qx + e2y * qy + e2z * qz, det_sign);
  102. // Check if there is an intersection
  103. UVec4 no_intersection =
  104. UVec4::sOr
  105. (
  106. UVec4::sOr
  107. (
  108. UVec4::sOr
  109. (
  110. det_near_zero,
  111. Vec4::sLess(u, zero)
  112. ),
  113. UVec4::sOr
  114. (
  115. Vec4::sLess(v, zero),
  116. Vec4::sGreater(u + v, det)
  117. )
  118. ),
  119. Vec4::sLess(t, zero)
  120. );
  121. // Select intersection point or FLT_MAX based on if there is an intersection or not
  122. return Vec4::sSelect(t / det, Vec4::sReplicate(FLT_MAX), no_intersection);
  123. }
  124. JPH_NAMESPACE_END