sphere_intersector.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "../common/ray.h"
  5. #include "../common/scene_points.h"
  6. #include "curve_intersector_precalculations.h"
  7. namespace embree
  8. {
  9. namespace isa
  10. {
  11. template<int M>
  12. struct SphereIntersectorHitM
  13. {
  14. __forceinline SphereIntersectorHitM() {}
  15. __forceinline SphereIntersectorHitM(const vfloat<M>& t, const Vec3vf<M>& Ng)
  16. : vt(t), vNg(Ng) {}
  17. __forceinline void finalize() {}
  18. __forceinline Vec2f uv(const size_t i) const {
  19. return Vec2f(0.0f, 0.0f);
  20. }
  21. __forceinline Vec2vf<M> uv() const {
  22. return Vec2vf<M>(0.0f, 0.0f);
  23. }
  24. __forceinline float t(const size_t i) const {
  25. return vt[i];
  26. }
  27. __forceinline vfloat<M> t() const {
  28. return vt;
  29. }
  30. __forceinline Vec3fa Ng(const size_t i) const {
  31. return Vec3fa(vNg.x[i], vNg.y[i], vNg.z[i]);
  32. }
  33. __forceinline Vec3vf<M> Ng() const {
  34. return vNg;
  35. }
  36. public:
  37. vfloat<M> vt;
  38. Vec3vf<M> vNg;
  39. };
  40. template<>
  41. struct SphereIntersectorHitM<1>
  42. {
  43. __forceinline SphereIntersectorHitM() {}
  44. __forceinline SphereIntersectorHitM(const float& t, const Vec3f& Ng)
  45. : vt(t), vNg(Ng) {}
  46. __forceinline void finalize() {}
  47. __forceinline Vec2f uv() const {
  48. return Vec2f(0.0f, 0.0f);
  49. }
  50. __forceinline float t() const {
  51. return vt;
  52. }
  53. __forceinline Vec3f Ng() const {
  54. return vNg;
  55. }
  56. public:
  57. float vt;
  58. Vec3f vNg;
  59. };
  60. template<int M>
  61. struct SphereIntersector1
  62. {
  63. typedef CurvePrecalculations1 Precalculations;
  64. template<typename Ray, typename Epilog>
  65. static __forceinline bool intersect(
  66. const vbool<M>& valid_i, Ray& ray,
  67. const Precalculations& pre, const Vec4vf<M>& v0, const Epilog& epilog)
  68. {
  69. vbool<M> valid = valid_i;
  70. const vfloat<M> rd2 = rcp(dot(ray.dir, ray.dir));
  71. const Vec3vf<M> ray_org(ray.org.x, ray.org.y, ray.org.z);
  72. const Vec3vf<M> ray_dir(ray.dir.x, ray.dir.y, ray.dir.z);
  73. const Vec3vf<M> center = v0.xyz();
  74. const vfloat<M> radius = v0.w;
  75. const Vec3vf<M> c0 = center - ray_org;
  76. const vfloat<M> projC0 = dot(c0, ray_dir) * rd2;
  77. const Vec3vf<M> perp = c0 - projC0 * ray_dir;
  78. const vfloat<M> l2 = dot(perp, perp);
  79. const vfloat<M> r2 = radius * radius;
  80. valid &= (l2 <= r2);
  81. if (unlikely(none(valid)))
  82. return false;
  83. const vfloat<M> td = sqrt((r2 - l2) * rd2);
  84. const vfloat<M> t_front = projC0 - td;
  85. const vfloat<M> t_back = projC0 + td;
  86. const vbool<M> valid_front = valid & (ray.tnear() <= t_front) & (t_front <= ray.tfar);
  87. /* check if there is a first hit */
  88. #if defined (EMBREE_BACKFACE_CULLING_SPHERES)
  89. /* check if there is a first hit */
  90. const vbool<M> valid_first = valid_front;
  91. #else
  92. const vbool<M> valid_back = valid & (ray.tnear() <= t_back ) & (t_back <= ray.tfar);
  93. const vbool<M> valid_first = valid_front | valid_back;
  94. #endif
  95. if (unlikely(none(valid_first)))
  96. return false;
  97. /* construct first hit */
  98. const vfloat<M> td_front = -td;
  99. const vfloat<M> td_back = +td;
  100. const vfloat<M> t_first = select(valid_front, t_front, t_back);
  101. const Vec3vf<M> Ng_first = select(valid_front, td_front, td_back) * ray_dir - perp;
  102. SphereIntersectorHitM<M> hit(t_first, Ng_first);
  103. /* invoke intersection filter for first hit */
  104. const bool is_hit_first = epilog(valid_first, hit);
  105. #if defined (EMBREE_BACKFACE_CULLING_SPHERES)
  106. return is_hit_first;
  107. #else
  108. /* check for possible second hits before potentially accepted hit */
  109. const vfloat<M> t_second = t_back;
  110. const vbool<M> valid_second = valid_front & valid_back & (t_second <= ray.tfar);
  111. if (unlikely(none(valid_second)))
  112. return is_hit_first;
  113. /* invoke intersection filter for second hit */
  114. const Vec3vf<M> Ng_second = td_back * ray_dir - perp;
  115. hit = SphereIntersectorHitM<M> (t_second, Ng_second);
  116. const bool is_hit_second = epilog(valid_second, hit);
  117. return is_hit_first | is_hit_second;
  118. #endif
  119. }
  120. template<typename Epilog>
  121. static __forceinline bool intersect(
  122. const vbool<M>& valid_i, Ray& ray, RayQueryContext* context, const Points* geom,
  123. const Precalculations& pre, const Vec4vf<M>& v0i, const Epilog& epilog)
  124. {
  125. const Vec3vf<M> ray_org(ray.org.x, ray.org.y, ray.org.z);
  126. const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
  127. return intersect(valid_i,ray,pre,v0,epilog);
  128. }
  129. };
  130. template<int M, int K>
  131. struct SphereIntersectorK
  132. {
  133. typedef CurvePrecalculationsK<K> Precalculations;
  134. template<typename Epilog>
  135. static __forceinline bool intersect(const vbool<M>& valid_i,
  136. RayK<K>& ray, size_t k,
  137. RayQueryContext* context,
  138. const Points* geom,
  139. const Precalculations& pre,
  140. const Vec4vf<M>& v0i,
  141. const Epilog& epilog)
  142. {
  143. vbool<M> valid = valid_i;
  144. const Vec3vf<M> ray_org(ray.org.x[k], ray.org.y[k], ray.org.z[k]);
  145. const Vec3vf<M> ray_dir(ray.dir.x[k], ray.dir.y[k], ray.dir.z[k]);
  146. const vfloat<M> rd2 = rcp(dot(ray_dir, ray_dir));
  147. const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
  148. const Vec3vf<M> center = v0.xyz();
  149. const vfloat<M> radius = v0.w;
  150. const Vec3vf<M> c0 = center - ray_org;
  151. const vfloat<M> projC0 = dot(c0, ray_dir) * rd2;
  152. const Vec3vf<M> perp = c0 - projC0 * ray_dir;
  153. const vfloat<M> l2 = dot(perp, perp);
  154. const vfloat<M> r2 = radius * radius;
  155. valid &= (l2 <= r2);
  156. if (unlikely(none(valid)))
  157. return false;
  158. const vfloat<M> td = sqrt((r2 - l2) * rd2);
  159. const vfloat<M> t_front = projC0 - td;
  160. const vfloat<M> t_back = projC0 + td;
  161. const vbool<M> valid_front = valid & (ray.tnear()[k] <= t_front) & (t_front <= ray.tfar[k]);
  162. /* check if there is a first hit */
  163. #if defined (EMBREE_BACKFACE_CULLING_SPHERES)
  164. const vbool<M> valid_first = valid_front;
  165. #else
  166. const vbool<M> valid_back = valid & (ray.tnear()[k] <= t_back ) & (t_back <= ray.tfar[k]);
  167. const vbool<M> valid_first = valid_front | valid_back;
  168. #endif
  169. if (unlikely(none(valid_first)))
  170. return false;
  171. /* construct first hit */
  172. const vfloat<M> td_front = -td;
  173. const vfloat<M> td_back = +td;
  174. const vfloat<M> t_first = select(valid_front, t_front, t_back);
  175. const Vec3vf<M> Ng_first = select(valid_front, td_front, td_back) * ray_dir - perp;
  176. SphereIntersectorHitM<M> hit(t_first, Ng_first);
  177. /* invoke intersection filter for first hit */
  178. const bool is_hit_first = epilog(valid_first, hit);
  179. #if defined (EMBREE_BACKFACE_CULLING_SPHERES)
  180. return is_hit_first;
  181. #else
  182. /* check for possible second hits before potentially accepted hit */
  183. const vfloat<M> t_second = t_back;
  184. const vbool<M> valid_second = valid_front & valid_back & (t_second <= ray.tfar[k]);
  185. if (unlikely(none(valid_second)))
  186. return is_hit_first;
  187. /* invoke intersection filter for second hit */
  188. const Vec3vf<M> Ng_second = td_back * ray_dir - perp;
  189. hit = SphereIntersectorHitM<M> (t_second, Ng_second);
  190. const bool is_hit_second = epilog(valid_second, hit);
  191. return is_hit_first | is_hit_second;
  192. #endif
  193. }
  194. };
  195. } // namespace isa
  196. } // namespace embree