util_math_intersect.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright 2011-2017 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __UTIL_MATH_INTERSECT_H__
  17. #define __UTIL_MATH_INTERSECT_H__
  18. CCL_NAMESPACE_BEGIN
  19. /* Ray Intersection */
  20. ccl_device bool ray_sphere_intersect(float3 ray_P,
  21. float3 ray_D,
  22. float ray_t,
  23. float3 sphere_P,
  24. float sphere_radius,
  25. float3 *isect_P,
  26. float *isect_t)
  27. {
  28. const float3 d = sphere_P - ray_P;
  29. const float radiussq = sphere_radius * sphere_radius;
  30. const float tsq = dot(d, d);
  31. if (tsq > radiussq) {
  32. /* Ray origin outside sphere. */
  33. const float tp = dot(d, ray_D);
  34. if (tp < 0.0f) {
  35. /* Ray points away from sphere. */
  36. return false;
  37. }
  38. const float dsq = tsq - tp * tp; /* pythagoras */
  39. if (dsq > radiussq) {
  40. /* Closest point on ray outside sphere. */
  41. return false;
  42. }
  43. const float t = tp - sqrtf(radiussq - dsq); /* pythagoras */
  44. if (t < ray_t) {
  45. *isect_t = t;
  46. *isect_P = ray_P + ray_D * t;
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. ccl_device bool ray_aligned_disk_intersect(float3 ray_P,
  53. float3 ray_D,
  54. float ray_t,
  55. float3 disk_P,
  56. float disk_radius,
  57. float3 *isect_P,
  58. float *isect_t)
  59. {
  60. /* Aligned disk normal. */
  61. float disk_t;
  62. const float3 disk_N = normalize_len(ray_P - disk_P, &disk_t);
  63. const float div = dot(ray_D, disk_N);
  64. if (UNLIKELY(div == 0.0f)) {
  65. return false;
  66. }
  67. /* Compute t to intersection point. */
  68. const float t = -disk_t / div;
  69. if (t < 0.0f || t > ray_t) {
  70. return false;
  71. }
  72. /* Test if within radius. */
  73. float3 P = ray_P + ray_D * t;
  74. if (len_squared(P - disk_P) > disk_radius * disk_radius) {
  75. return false;
  76. }
  77. *isect_P = P;
  78. *isect_t = t;
  79. return true;
  80. }
  81. ccl_device_forceinline bool ray_triangle_intersect(float3 ray_P,
  82. float3 ray_dir,
  83. float ray_t,
  84. #if defined(__KERNEL_SSE2__) && defined(__KERNEL_SSE__)
  85. const ssef *ssef_verts,
  86. #else
  87. const float3 tri_a,
  88. const float3 tri_b,
  89. const float3 tri_c,
  90. #endif
  91. float *isect_u,
  92. float *isect_v,
  93. float *isect_t)
  94. {
  95. #if defined(__KERNEL_SSE2__) && defined(__KERNEL_SSE__)
  96. typedef ssef float3;
  97. const float3 tri_a(ssef_verts[0]);
  98. const float3 tri_b(ssef_verts[1]);
  99. const float3 tri_c(ssef_verts[2]);
  100. const float3 P(ray_P);
  101. const float3 dir(ray_dir);
  102. #else
  103. # define dot3(a, b) dot(a, b)
  104. const float3 P = ray_P;
  105. const float3 dir = ray_dir;
  106. #endif
  107. /* Calculate vertices relative to ray origin. */
  108. const float3 v0 = tri_c - P;
  109. const float3 v1 = tri_a - P;
  110. const float3 v2 = tri_b - P;
  111. /* Calculate triangle edges. */
  112. const float3 e0 = v2 - v0;
  113. const float3 e1 = v0 - v1;
  114. const float3 e2 = v1 - v2;
  115. /* Perform edge tests. */
  116. #if defined(__KERNEL_SSE2__) && defined(__KERNEL_SSE__)
  117. const float3 crossU = cross(v2 + v0, e0);
  118. const float3 crossV = cross(v0 + v1, e1);
  119. const float3 crossW = cross(v1 + v2, e2);
  120. ssef crossX(crossU);
  121. ssef crossY(crossV);
  122. ssef crossZ(crossW);
  123. ssef zero = _mm_setzero_ps();
  124. _MM_TRANSPOSE4_PS(crossX, crossY, crossZ, zero);
  125. const ssef dirX(ray_dir.x);
  126. const ssef dirY(ray_dir.y);
  127. const ssef dirZ(ray_dir.z);
  128. ssef UVWW = madd(crossX, dirX, madd(crossY, dirY, crossZ * dirZ));
  129. #else /* __KERNEL_SSE2__ */
  130. const float U = dot(cross(v2 + v0, e0), ray_dir);
  131. const float V = dot(cross(v0 + v1, e1), ray_dir);
  132. const float W = dot(cross(v1 + v2, e2), ray_dir);
  133. #endif /* __KERNEL_SSE2__ */
  134. #if defined(__KERNEL_SSE2__) && defined(__KERNEL_SSE__)
  135. int uvw_sign = movemask(UVWW) & 0x7;
  136. if (uvw_sign != 0) {
  137. if (uvw_sign != 0x7) {
  138. return false;
  139. }
  140. }
  141. #else
  142. const float minUVW = min(U, min(V, W));
  143. const float maxUVW = max(U, max(V, W));
  144. if (minUVW < 0.0f && maxUVW > 0.0f) {
  145. return false;
  146. }
  147. #endif
  148. /* Calculate geometry normal and denominator. */
  149. const float3 Ng1 = cross(e1, e0);
  150. // const Vec3vfM Ng1 = stable_triangle_normal(e2,e1,e0);
  151. const float3 Ng = Ng1 + Ng1;
  152. const float den = dot3(Ng, dir);
  153. /* Avoid division by 0. */
  154. if (UNLIKELY(den == 0.0f)) {
  155. return false;
  156. }
  157. /* Perform depth test. */
  158. const float T = dot3(v0, Ng);
  159. const int sign_den = (__float_as_int(den) & 0x80000000);
  160. const float sign_T = xor_signmask(T, sign_den);
  161. if ((sign_T < 0.0f) || (sign_T > ray_t * xor_signmask(den, sign_den))) {
  162. return false;
  163. }
  164. const float inv_den = 1.0f / den;
  165. #if defined(__KERNEL_SSE2__) && defined(__KERNEL_SSE__)
  166. UVWW *= inv_den;
  167. _mm_store_ss(isect_u, UVWW);
  168. _mm_store_ss(isect_v, shuffle<1, 1, 3, 3>(UVWW));
  169. #else
  170. *isect_u = U * inv_den;
  171. *isect_v = V * inv_den;
  172. #endif
  173. *isect_t = T * inv_den;
  174. return true;
  175. #undef dot3
  176. }
  177. /* Tests for an intersection between a ray and a quad defined by
  178. * its midpoint, normal and sides.
  179. * If ellipse is true, hits outside the ellipse that's enclosed by the
  180. * quad are rejected.
  181. */
  182. ccl_device bool ray_quad_intersect(float3 ray_P,
  183. float3 ray_D,
  184. float ray_mint,
  185. float ray_maxt,
  186. float3 quad_P,
  187. float3 quad_u,
  188. float3 quad_v,
  189. float3 quad_n,
  190. float3 *isect_P,
  191. float *isect_t,
  192. float *isect_u,
  193. float *isect_v,
  194. bool ellipse)
  195. {
  196. /* Perform intersection test. */
  197. float t = -(dot(ray_P, quad_n) - dot(quad_P, quad_n)) / dot(ray_D, quad_n);
  198. if (t < ray_mint || t > ray_maxt) {
  199. return false;
  200. }
  201. const float3 hit = ray_P + t * ray_D;
  202. const float3 inplane = hit - quad_P;
  203. const float u = dot(inplane, quad_u) / dot(quad_u, quad_u);
  204. if (u < -0.5f || u > 0.5f) {
  205. return false;
  206. }
  207. const float v = dot(inplane, quad_v) / dot(quad_v, quad_v);
  208. if (v < -0.5f || v > 0.5f) {
  209. return false;
  210. }
  211. if (ellipse && (u * u + v * v > 0.25f)) {
  212. return false;
  213. }
  214. /* Store the result. */
  215. /* TODO(sergey): Check whether we can avoid some checks here. */
  216. if (isect_P != NULL)
  217. *isect_P = hit;
  218. if (isect_t != NULL)
  219. *isect_t = t;
  220. if (isect_u != NULL)
  221. *isect_u = u + 0.5f;
  222. if (isect_v != NULL)
  223. *isect_v = v + 0.5f;
  224. return true;
  225. }
  226. CCL_NAMESPACE_END
  227. #endif /* __UTIL_MATH_INTERSECT_H__ */